aspen_pysys.case

Package containing all Pythonic functionality for HYSYS simulation cases.

1# Copyright 2026 Hariidaran Tamilmaran
2
3"""Package containing all Pythonic functionality for HYSYS simulation cases."""
4
5from aspen_pysys.case.flowsheet import HysysFlowsheet
6from aspen_pysys.case.hysys_case import HysysCase
7from aspen_pysys.case.solver import HysysFlowsheetSolver
8
9__all__ = ["HysysCase", "HysysFlowsheet", "HysysFlowsheetSolver"]
class HysysCase(aspen_pysys.base.named_object.HysysNamedObject):
19class HysysCase(HysysNamedObject):
20    """Class that represents a simulation case on the HYSYS app."""
21
22    def __init__(self, case_object: HysysObjReadable) -> None:
23        super().__init__(self, case_object)
24
25        full_name = self.get_str("FullName")
26        self._path = full_name
27
28    def __repr__(self) -> str:
29        return f"HYSYS Case: {self.path()}"
30
31    def path(self) -> Path:
32        """Get the filepath.
33
34        Returns:
35            Path: Filepath
36        """
37        return Path(self._path)
38
39    def get_flowsheet(self) -> HysysFlowsheet:
40        """Get the flowsheet.
41
42        Returns:
43            HysysFlowsheet: Flowsheet
44        """
45        return HysysFlowsheet(self)
46
47    def close(self, should_save_changes: bool = False) -> None:  # noqa: FBT001, FBT002
48        """Close the simulation case.
49
50        Args:
51            should_save_changes (bool, optional): If the file should be saved before closing. Defaults to False.
52        """  # noqa: E501
53        self.get_func("Close").call(should_save_changes, str(self.path()))
54
55    def activate(self) -> None:
56        """Activate the simulation case in the HYSYS app."""
57        self.get_func("Activate").call()
58
59    def save(self) -> None:
60        """Save the simulation case."""
61        self.get_func("Save").call()
62
63    def save_as(self, new_name: str) -> None:
64        """Save the simulation case under a different file name."""
65        self.get_func("SaveAs").call(new_name)
66
67    def get_solver(self) -> HysysFlowsheetSolver:
68        """Get the flowsheet solver.
69
70        Returns:
71            HysysFlowsheetSolver: Solver
72        """
73        return HysysFlowsheetSolver(self)

Class that represents a simulation case on the HYSYS app.

HysysCase(case_object: HysysObjReadable)
22    def __init__(self, case_object: HysysObjReadable) -> None:
23        super().__init__(self, case_object)
24
25        full_name = self.get_str("FullName")
26        self._path = full_name

Create a Pythonic representation of a named object from the HYSYS app.

Arguments:
  • connection (HysysCase): Simulation case
  • obj (HysysNamedObjReadable): Object
Raises:
  • PysysError: Object cannot be a named object as it does not have a name.
def path(self) -> pathlib.Path:
31    def path(self) -> Path:
32        """Get the filepath.
33
34        Returns:
35            Path: Filepath
36        """
37        return Path(self._path)

Get the filepath.

Returns:

Path: Filepath

def get_flowsheet(self) -> HysysFlowsheet:
39    def get_flowsheet(self) -> HysysFlowsheet:
40        """Get the flowsheet.
41
42        Returns:
43            HysysFlowsheet: Flowsheet
44        """
45        return HysysFlowsheet(self)

Get the flowsheet.

Returns:

HysysFlowsheet: Flowsheet

def close(self, should_save_changes: bool = False) -> None:
47    def close(self, should_save_changes: bool = False) -> None:  # noqa: FBT001, FBT002
48        """Close the simulation case.
49
50        Args:
51            should_save_changes (bool, optional): If the file should be saved before closing. Defaults to False.
52        """  # noqa: E501
53        self.get_func("Close").call(should_save_changes, str(self.path()))

Close the simulation case.

Arguments:
  • should_save_changes (bool, optional): If the file should be saved before closing. Defaults to False.
def activate(self) -> None:
55    def activate(self) -> None:
56        """Activate the simulation case in the HYSYS app."""
57        self.get_func("Activate").call()

Activate the simulation case in the HYSYS app.

def save(self) -> None:
59    def save(self) -> None:
60        """Save the simulation case."""
61        self.get_func("Save").call()

Save the simulation case.

def save_as(self, new_name: str) -> None:
63    def save_as(self, new_name: str) -> None:
64        """Save the simulation case under a different file name."""
65        self.get_func("SaveAs").call(new_name)

Save the simulation case under a different file name.

def get_solver(self) -> HysysFlowsheetSolver:
67    def get_solver(self) -> HysysFlowsheetSolver:
68        """Get the flowsheet solver.
69
70        Returns:
71            HysysFlowsheetSolver: Solver
72        """
73        return HysysFlowsheetSolver(self)

Get the flowsheet solver.

Returns:

HysysFlowsheetSolver: Solver

class HysysFlowsheet(aspen_pysys.base.named_object.HysysNamedObject):
 37class HysysFlowsheet(HysysNamedObject):
 38    """Class that represents a flowsheet on the HYSYS app."""
 39
 40    def __init__(self, simcase: HysysCase) -> None:
 41        super().__init__(simcase, simcase.get_obj("Flowsheet"))
 42
 43        self._streams: HysysDictionary[HysysProcessStream] = self.get_dict(
 44            STREAMS_STR
 45        ).map(HysysModel.FACTORY.get_process_stream)
 46
 47        self._material_streams: HysysDictionary[HysysMaterialStream] = self.get_dict(
 48            MATERIAL_STREAMS_STR
 49        ).map(HysysModel.FACTORY.get_material_stream)
 50
 51        self._energy_streams: HysysDictionary[HysysEnergyStream] = self.get_dict(
 52            ENERGY_STREAMS_STR
 53        ).map(HysysModel.FACTORY.get_energy_stream)
 54
 55        self._unit_operations: HysysDictionary[HysysUnitOperation] = self.get_dict(
 56            OPERATIONS_STR
 57        ).map(HysysModel.FACTORY.get_unit_operation)
 58
 59    def get_fluid_package(self) -> HysysObject:
 60        """Get the fluid package.
 61
 62        Returns:
 63            HysysObject: Fluid package
 64        """
 65        return self.get_obj("FluidPackage")
 66
 67    def get_components(self) -> HysysDictionary[HysysObject]:
 68        """Get the components.
 69
 70        Returns:
 71            HysysDictionary[HysysObject]: Components
 72        """
 73        return self.get_fluid_package().get_dict("Components")
 74
 75    def get_component_names(self) -> tuple[str]:
 76        """Get the names of the components.
 77
 78        Returns:
 79            tuple[str]: Component names
 80        """
 81        return self.get_components().keys()
 82
 83    def get_component_indices(self, names: tuple[str, ...] | list[str]) -> tuple[int]:
 84        """Get the indices of the components.
 85
 86        Args:
 87            names (tuple[str, ...] | list[str]): Component names
 88
 89        Returns:
 90            tuple[int]: Component indices according to the names
 91
 92        Raises:
 93            PysysError: Component indices are None.
 94        """
 95        value = self.get_fluid_package().get_func("ComponentIndices").call(names)
 96
 97        if value is None:
 98            message = "Component indices are None."
 99            raise PysysError(message)
100
101        with value as array:
102            return array
103
104    def get_streams(self) -> HysysDictionary[HysysProcessStream]:
105        """Get all streams, both material and energy.
106
107        Returns:
108            HysysDictionary[HysysProcessStream]: Streams
109        """
110        return self._streams
111
112    def get_material_streams(self) -> HysysDictionary[HysysMaterialStream]:
113        """Get the material streams in the flowsheet.
114
115        Returns:
116            HysysDictionary[HysysMaterialStream]: Material streams
117        """
118        return self._material_streams
119
120    def get_material_stream(self, name: str) -> HysysMaterialStream:
121        """Get a material stream in the flowsheet.
122
123        Args:
124            name (str): Name of material stream
125
126        Returns:
127            HysysMaterialStream: Material stream
128        """
129        return _get_model(
130            name,
131            self._material_streams,
132        )
133
134    def add_material_stream(self, name: str) -> HysysMaterialStream:
135        """Add a material stream.
136
137        Args:
138            name (str): Name of stream
139
140        Returns:
141            HysysMaterialStream: Added stream
142
143        Raises:
144            PysysError: When stream may not have been added.
145        """
146        obj = self._material_streams.add(name)
147
148        if obj is None:
149            message = "Material stream may not have been added."
150            raise PysysError(message)
151
152        return HysysModel.FACTORY.get_material_stream(HysysNamedObject.from_obj(obj))
153
154    def get_energy_streams(self) -> HysysDictionary[HysysEnergyStream]:
155        """Get the energy streams in the flowsheet.
156
157        Returns:
158            HysysDictionary[HysysEnergyStream]: Energy streams
159        """
160        return self._energy_streams
161
162    def get_energy_stream(self, name: str) -> HysysEnergyStream:
163        """Get an energy stream in the flowsheet.
164
165        Args:
166            name (str): Name of energy stream
167
168        Returns:
169            HysysEnergyStream: Energy stream
170        """
171        return _get_model(
172            name,
173            self._energy_streams,
174        )
175
176    def add_energy_stream(self, name: str) -> HysysEnergyStream:
177        """Add an energy stream to the flowsheet.
178
179        Args:
180            name (str): Name of stream
181
182        Returns:
183            HysysEnergyStream: Added stream
184
185        Raises:
186            PysysError: When stream may not have been added.
187        """
188        obj = self._energy_streams.add(name)
189
190        if obj is None:
191            message = "Energy stream may not have been added."
192            raise PysysError(message)
193
194        return HysysEnergyStream.from_obj(obj)
195
196    def get_unit_operation(
197        self,
198        name: str,
199    ) -> HysysUnitOperation:
200        """Get a unit operation in the flowsheet.
201
202        Args:
203            name (str): Name of unit operation
204
205        Returns:
206            HysysUnitOperation: Unit operation
207        """
208        return _get_model(name, self._unit_operations)
209
210    def get_unit_operations(self) -> HysysDictionary[HysysUnitOperation]:
211        """Get the unit operations in the flowsheet.
212
213        Returns:
214            HysysDictionary[HysysUnitOperation]: Unit operations
215        """
216        return self._unit_operations
217
218    def add_unit_operation(
219        self,
220        name: str,
221        unit_op_type: HysysUnitOpType,
222    ) -> HysysUnitOperation:
223        """Add a unit operation.
224
225        Args:
226            name (str): Unit operation name
227            unit_op_type (HysysUnitOpType): Unit operation type
228
229        Returns:
230            HysysUnitOperation: Added unit operation
231
232        Raises:
233            PysysError: When unit operation of unknown type cannot be added.
234            PysysError: When unit operation may not have been added.
235        """
236        try:
237            obj = self._unit_operations.add(name, unit_op_type)
238        except KeyError as err:
239            message = f"Unit operation of type '{unit_op_type}' cannot be added."
240            raise PysysError(message) from err
241
242        if obj is None:
243            message = "Unit operation may not have been added."
244            raise PysysError(message)
245
246        return HysysUnitOperation.from_obj(obj)
247
248    def filter_operations(
249        self,
250        unit_op_type: HysysUnitOpType,
251    ) -> tuple[HysysUnitOperation, ...]:
252        """Filter unit operations by class.
253
254        Args:
255            unit_op_type (HysysUnitOpType): Unit operation type
256
257        Returns:
258            tuple[HysysUnitOperation, ...]: Filtered unit operations
259        """
260        return tuple(
261            self.get_unit_operations()
262            .filter(
263                lambda _, operation: operation.get_operation_type() == unit_op_type,
264            )
265            .values(),
266        )

Class that represents a flowsheet on the HYSYS app.

HysysFlowsheet(simcase: HysysCase)
40    def __init__(self, simcase: HysysCase) -> None:
41        super().__init__(simcase, simcase.get_obj("Flowsheet"))
42
43        self._streams: HysysDictionary[HysysProcessStream] = self.get_dict(
44            STREAMS_STR
45        ).map(HysysModel.FACTORY.get_process_stream)
46
47        self._material_streams: HysysDictionary[HysysMaterialStream] = self.get_dict(
48            MATERIAL_STREAMS_STR
49        ).map(HysysModel.FACTORY.get_material_stream)
50
51        self._energy_streams: HysysDictionary[HysysEnergyStream] = self.get_dict(
52            ENERGY_STREAMS_STR
53        ).map(HysysModel.FACTORY.get_energy_stream)
54
55        self._unit_operations: HysysDictionary[HysysUnitOperation] = self.get_dict(
56            OPERATIONS_STR
57        ).map(HysysModel.FACTORY.get_unit_operation)

Create a Pythonic representation of a named object from the HYSYS app.

Arguments:
  • connection (HysysCase): Simulation case
  • obj (HysysNamedObjReadable): Object
Raises:
  • PysysError: Object cannot be a named object as it does not have a name.
def get_fluid_package(self) -> aspen_pysys.base.HysysObject:
59    def get_fluid_package(self) -> HysysObject:
60        """Get the fluid package.
61
62        Returns:
63            HysysObject: Fluid package
64        """
65        return self.get_obj("FluidPackage")

Get the fluid package.

Returns:

HysysObject: Fluid package

67    def get_components(self) -> HysysDictionary[HysysObject]:
68        """Get the components.
69
70        Returns:
71            HysysDictionary[HysysObject]: Components
72        """
73        return self.get_fluid_package().get_dict("Components")

Get the components.

Returns:

HysysDictionary[HysysObject]: Components

def get_component_names(self) -> tuple[str]:
75    def get_component_names(self) -> tuple[str]:
76        """Get the names of the components.
77
78        Returns:
79            tuple[str]: Component names
80        """
81        return self.get_components().keys()

Get the names of the components.

Returns:

tuple[str]: Component names

def get_component_indices(self, names: tuple[str, ...] | list[str]) -> tuple[int]:
 83    def get_component_indices(self, names: tuple[str, ...] | list[str]) -> tuple[int]:
 84        """Get the indices of the components.
 85
 86        Args:
 87            names (tuple[str, ...] | list[str]): Component names
 88
 89        Returns:
 90            tuple[int]: Component indices according to the names
 91
 92        Raises:
 93            PysysError: Component indices are None.
 94        """
 95        value = self.get_fluid_package().get_func("ComponentIndices").call(names)
 96
 97        if value is None:
 98            message = "Component indices are None."
 99            raise PysysError(message)
100
101        with value as array:
102            return array

Get the indices of the components.

Arguments:
  • names (tuple[str, ...] | list[str]): Component names
Returns:

tuple[int]: Component indices according to the names

Raises:
  • PysysError: Component indices are None.
104    def get_streams(self) -> HysysDictionary[HysysProcessStream]:
105        """Get all streams, both material and energy.
106
107        Returns:
108            HysysDictionary[HysysProcessStream]: Streams
109        """
110        return self._streams

Get all streams, both material and energy.

Returns:

HysysDictionary[HysysProcessStream]: Streams

112    def get_material_streams(self) -> HysysDictionary[HysysMaterialStream]:
113        """Get the material streams in the flowsheet.
114
115        Returns:
116            HysysDictionary[HysysMaterialStream]: Material streams
117        """
118        return self._material_streams

Get the material streams in the flowsheet.

Returns:

HysysDictionary[HysysMaterialStream]: Material streams

def get_material_stream( self, name: str) -> aspen_pysys.model.process_stream.HysysMaterialStream:
120    def get_material_stream(self, name: str) -> HysysMaterialStream:
121        """Get a material stream in the flowsheet.
122
123        Args:
124            name (str): Name of material stream
125
126        Returns:
127            HysysMaterialStream: Material stream
128        """
129        return _get_model(
130            name,
131            self._material_streams,
132        )

Get a material stream in the flowsheet.

Arguments:
  • name (str): Name of material stream
Returns:

HysysMaterialStream: Material stream

def add_material_stream( self, name: str) -> aspen_pysys.model.process_stream.HysysMaterialStream:
134    def add_material_stream(self, name: str) -> HysysMaterialStream:
135        """Add a material stream.
136
137        Args:
138            name (str): Name of stream
139
140        Returns:
141            HysysMaterialStream: Added stream
142
143        Raises:
144            PysysError: When stream may not have been added.
145        """
146        obj = self._material_streams.add(name)
147
148        if obj is None:
149            message = "Material stream may not have been added."
150            raise PysysError(message)
151
152        return HysysModel.FACTORY.get_material_stream(HysysNamedObject.from_obj(obj))

Add a material stream.

Arguments:
  • name (str): Name of stream
Returns:

HysysMaterialStream: Added stream

Raises:
  • PysysError: When stream may not have been added.
154    def get_energy_streams(self) -> HysysDictionary[HysysEnergyStream]:
155        """Get the energy streams in the flowsheet.
156
157        Returns:
158            HysysDictionary[HysysEnergyStream]: Energy streams
159        """
160        return self._energy_streams

Get the energy streams in the flowsheet.

Returns:

HysysDictionary[HysysEnergyStream]: Energy streams

def get_energy_stream( self, name: str) -> aspen_pysys.model.process_stream.HysysEnergyStream:
162    def get_energy_stream(self, name: str) -> HysysEnergyStream:
163        """Get an energy stream in the flowsheet.
164
165        Args:
166            name (str): Name of energy stream
167
168        Returns:
169            HysysEnergyStream: Energy stream
170        """
171        return _get_model(
172            name,
173            self._energy_streams,
174        )

Get an energy stream in the flowsheet.

Arguments:
  • name (str): Name of energy stream
Returns:

HysysEnergyStream: Energy stream

def add_energy_stream( self, name: str) -> aspen_pysys.model.process_stream.HysysEnergyStream:
176    def add_energy_stream(self, name: str) -> HysysEnergyStream:
177        """Add an energy stream to the flowsheet.
178
179        Args:
180            name (str): Name of stream
181
182        Returns:
183            HysysEnergyStream: Added stream
184
185        Raises:
186            PysysError: When stream may not have been added.
187        """
188        obj = self._energy_streams.add(name)
189
190        if obj is None:
191            message = "Energy stream may not have been added."
192            raise PysysError(message)
193
194        return HysysEnergyStream.from_obj(obj)

Add an energy stream to the flowsheet.

Arguments:
  • name (str): Name of stream
Returns:

HysysEnergyStream: Added stream

Raises:
  • PysysError: When stream may not have been added.
def get_unit_operation( self, name: str) -> aspen_pysys.model.unit_operation.HysysUnitOperation:
196    def get_unit_operation(
197        self,
198        name: str,
199    ) -> HysysUnitOperation:
200        """Get a unit operation in the flowsheet.
201
202        Args:
203            name (str): Name of unit operation
204
205        Returns:
206            HysysUnitOperation: Unit operation
207        """
208        return _get_model(name, self._unit_operations)

Get a unit operation in the flowsheet.

Arguments:
  • name (str): Name of unit operation
Returns:

HysysUnitOperation: Unit operation

210    def get_unit_operations(self) -> HysysDictionary[HysysUnitOperation]:
211        """Get the unit operations in the flowsheet.
212
213        Returns:
214            HysysDictionary[HysysUnitOperation]: Unit operations
215        """
216        return self._unit_operations

Get the unit operations in the flowsheet.

Returns:

HysysDictionary[HysysUnitOperation]: Unit operations

def add_unit_operation( self, name: str, unit_op_type: aspen_pysys.model.unit_operation.HysysUnitOpType) -> aspen_pysys.model.unit_operation.HysysUnitOperation:
218    def add_unit_operation(
219        self,
220        name: str,
221        unit_op_type: HysysUnitOpType,
222    ) -> HysysUnitOperation:
223        """Add a unit operation.
224
225        Args:
226            name (str): Unit operation name
227            unit_op_type (HysysUnitOpType): Unit operation type
228
229        Returns:
230            HysysUnitOperation: Added unit operation
231
232        Raises:
233            PysysError: When unit operation of unknown type cannot be added.
234            PysysError: When unit operation may not have been added.
235        """
236        try:
237            obj = self._unit_operations.add(name, unit_op_type)
238        except KeyError as err:
239            message = f"Unit operation of type '{unit_op_type}' cannot be added."
240            raise PysysError(message) from err
241
242        if obj is None:
243            message = "Unit operation may not have been added."
244            raise PysysError(message)
245
246        return HysysUnitOperation.from_obj(obj)

Add a unit operation.

Arguments:
  • name (str): Unit operation name
  • unit_op_type (HysysUnitOpType): Unit operation type
Returns:

HysysUnitOperation: Added unit operation

Raises:
  • PysysError: When unit operation of unknown type cannot be added.
  • PysysError: When unit operation may not have been added.
def filter_operations( self, unit_op_type: aspen_pysys.model.unit_operation.HysysUnitOpType) -> tuple[aspen_pysys.model.unit_operation.HysysUnitOperation, ...]:
248    def filter_operations(
249        self,
250        unit_op_type: HysysUnitOpType,
251    ) -> tuple[HysysUnitOperation, ...]:
252        """Filter unit operations by class.
253
254        Args:
255            unit_op_type (HysysUnitOpType): Unit operation type
256
257        Returns:
258            tuple[HysysUnitOperation, ...]: Filtered unit operations
259        """
260        return tuple(
261            self.get_unit_operations()
262            .filter(
263                lambda _, operation: operation.get_operation_type() == unit_op_type,
264            )
265            .values(),
266        )

Filter unit operations by class.

Arguments:
  • unit_op_type (HysysUnitOpType): Unit operation type
Returns:

tuple[HysysUnitOperation, ...]: Filtered unit operations

class HysysFlowsheetSolver(aspen_pysys.base.named_object.HysysNamedObject):
14class HysysFlowsheetSolver(HysysNamedObject):
15    """Class that represents a flowsheet solver on the HYSYS app."""
16
17    def __init__(self, simcase: HysysCase) -> None:
18        super().__init__(simcase, simcase.get_obj("Solver"))
19
20    def activate(self) -> None:
21        """Activate the solver."""
22        self.set_bool("CanSolve", value=True)
23
24    def hold(self) -> None:
25        """Hold the solver."""
26        self.set_bool("CanSolve", value=False)
27
28    def has_solved(self) -> bool:
29        """Check if the simulation is currently solved.
30
31        Returns:
32            bool: If the simulation is currently solved
33        """
34        return self.get_bool("CanSolve")
35
36    def is_solving(self) -> bool:
37        """Check if the simulation is currently solving.
38
39        Returns:
40            bool: If the simulation is currently solving
41        """
42        return self.get_bool("IsSolving")

Class that represents a flowsheet solver on the HYSYS app.

HysysFlowsheetSolver(simcase: HysysCase)
17    def __init__(self, simcase: HysysCase) -> None:
18        super().__init__(simcase, simcase.get_obj("Solver"))

Create a Pythonic representation of a named object from the HYSYS app.

Arguments:
  • connection (HysysCase): Simulation case
  • obj (HysysNamedObjReadable): Object
Raises:
  • PysysError: Object cannot be a named object as it does not have a name.
def activate(self) -> None:
20    def activate(self) -> None:
21        """Activate the solver."""
22        self.set_bool("CanSolve", value=True)

Activate the solver.

def hold(self) -> None:
24    def hold(self) -> None:
25        """Hold the solver."""
26        self.set_bool("CanSolve", value=False)

Hold the solver.

def has_solved(self) -> bool:
28    def has_solved(self) -> bool:
29        """Check if the simulation is currently solved.
30
31        Returns:
32            bool: If the simulation is currently solved
33        """
34        return self.get_bool("CanSolve")

Check if the simulation is currently solved.

Returns:

bool: If the simulation is currently solved

def is_solving(self) -> bool:
36    def is_solving(self) -> bool:
37        """Check if the simulation is currently solving.
38
39        Returns:
40            bool: If the simulation is currently solving
41        """
42        return self.get_bool("IsSolving")

Check if the simulation is currently solving.

Returns:

bool: If the simulation is currently solving