Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
__pycache__
*.pyc
env
.idea

104 changes: 60 additions & 44 deletions HYSYS_python_spreadsheets.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'''
"""
Aspen HYSYS - Python interface
for spreadsheets connection

Expand All @@ -7,89 +7,105 @@
-------------------------------------------------------------------------------
Date: 21/06/2019
Update: 01/01/2020
'''
"""
# built-in
import os # Import operating system interface
from typing import Tuple

# third-party
import win32com.client as win32 # Import COM

def Aspen_connection(File_name, Spreadsheet_name, Unit_operation_name,
hy_visible=1, active=0):
'''
INPUTS
File_name: String -- File name, including quoting. Example: 'test.hsc'
Spreadsheet_name: Tuple -- Spreadsheet names in the Aspen HYSYS file
including quoting.
Unit_operation_name: Tuple -- Unit operation name in the Aspen HYSYS
file including quoting.
hy_visible: Integer -- 1 for Visible, 0 for No Visible
active: Integer -- 1 for Active, 0 for No Active

OUTPUTS
Hysys: Class -- It is a class that collects all the important information
of the flowsheet.

class Hysys:
hysys_case = ...
solver = ...
spreadsheets = ...
material_streams = ...
energy_streams = ...
unit_operation = ...


def aspen_connection(file_name: str,
spreadsheet_name: Tuple[str, ...],
unit_operation_name: Tuple[str, ...],
hy_visible: int = 1,
active: int = 0):
"""
method for connection to hysys (.hsc) files
Args:
file_name: File name, including quoting. Example: 'test.hsc'
spreadsheet_name: Spreadsheet names in the Aspen HYSYS file including quoting.
unit_operation_name: Unit operation name in the Aspen HYSYS file including quoting.
hy_visible: 1 for Visible, 0 for No Visible
active: 1 for Active, 0 for No Active

Returns:
Hysys: Class -- It is a class that collects all the important information of the flowsheet.

NOTE: To check useful pathways to variables in Aspen HYSYS use the Object
browser from VBA or Matlab and the "HYSYS Customization Guide".
'''
"""

# 1.0 Obtain the path to the Aspen file
hyFilePath = os.path.abspath(File_name)
absolute_path = os.path.abspath(file_name)

# 2.0 Initialize Aspen HYSYS application
print(' ')
print(' Connecting to the Aspen HYSYS.... Please wait ')

# This gets the registered name of Aspen HYSYS
HyApp = win32.Dispatch('HYSYS.Application')
# ToDo read dock of win32.Dispatch
hysys_app = win32.Dispatch('HYSYS.Application')

# 3.0 Open Aspen HYSYS file
if active == 0:
HyCase = HyApp.SimulationCases.Open(hyFilePath)
hysys_case = hysys_app.SimulationCases.Open(absolute_path)

# 3.1 Get the active Aspen HYSYS document
elif active == 1:
HyCase = HyApp.ActiveDocument
hysys_case = hysys_app.ActiveDocument
else:
raise Exception('Argument for input variable "active" is not valid')

# 4.0 Aspen HYSYS Environment Visible
HyCase.Visible = hy_visible
hysys_case.Visible = hy_visible

# 5.0 Aspen HYSYS File Name
HySysFile = HyCase.Title.Value
file_name = hysys_case.Title.Value
print(' ')
print('Aspen HYSYS file name: ', HySysFile)
print('Aspen HYSYS file name: ', file_name)

# 6.0 Aspen HYSYS Thermodynamic Fluid Package Name
package_name = HyCase.Flowsheet.FluidPackage.PropertyPackageName
package_name = hysys_case.Flowsheet.FluidPackage.PropertyPackageName
print('Aspen HYSYS Fluid Package: ', package_name)
print(' ')

# 7.0 Spreadsheets
ss_dict = {}
for ss in Spreadsheet_name:
spsh = HyCase.Flowsheet.Operations.Item(ss)
for ss in spreadsheet_name:
spsh = hysys_case.Flowsheet.Operations.Item(ss)
ss_dict[ss] = spsh

# 8.0 Solver to stop or start the running
Solver = HyCase.Solver
solver = hysys_case.Solver

# 9.0 Material streams
MaterialStreams = HyCase.Flowsheet.MaterialStreams
EnergyStreams = HyCase.Flowsheet.EnergyStreams
material_streams = hysys_case.Flowsheet.MaterialStreams
energy_streams = hysys_case.Flowsheet.EnergyStreams

# 10.0 Unit operations
uo_dict = {}
for uo in Unit_operation_name:
unop = HyCase.Flowsheet.Operations.Item(uo)
for uo in unit_operation_name:
unop = hysys_case.Flowsheet.Operations.Item(uo)
uo_dict[uo] = unop

# 11.0 Collect everything into a class
class Hysys:
pass
Hysys.HyCase = HyCase
Hysys.SS = ss_dict
Hysys.Solver = Solver
Hysys.MaterialStreams = MaterialStreams
Hysys.EnergyStreams = EnergyStreams
Hysys.UO = uo_dict
Hysys.hysys_case = hysys_case
Hysys.spreadsheets = ss_dict
Hysys.solver = solver
Hysys.material_streams = material_streams
Hysys.energy_streams = energy_streams
Hysys.unit_operation = uo_dict

print(' --- PLEASE! Be aware of the unit handling of this interface--- ')
print(' --- Python SI Unit Set only --- ')
Expand All @@ -105,7 +121,7 @@ class Hysys:
print(' Energy flowrate: kJ/s')
print('**************************************************************** ')
print(' ')
print( ' Aspen HYSYS-Python Interface has been established succesfully!')
print(' Aspen HYSYS-Python Interface has been established succesfully!')
print(' ')
return(Hysys)
return Hysys

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,4 @@ An example of use is provided in the ```Test_1.py``` where I show the importance

## License

This repository contains a [MIT LICENSE](https://github.com/edgarsmdn/Aspen_HYSYS_Python/blob/main/LICENSE)
This repository contains a [MIT LICENSE](https://github.com/AlxndrKlbk/pyhys/blob/main/LICENSE)
Binary file modified Test_1.bk0
Binary file not shown.
Binary file modified Test_1.hsc
Binary file not shown.
65 changes: 36 additions & 29 deletions Test_1.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'''
"""
Test 1 for the
Aspen HYSYS - Python interface
for spreadsheets connection
Expand All @@ -10,62 +10,69 @@

This is a test file for the Aspen HYSYS - Python connection using a flowsheet
with multiple units.
'''
from HYSYS_python_spreadsheets import Aspen_connection
"""
from HYSYS_python_spreadsheets import aspen_connection
import numpy as np
import matplotlib.pyplot as plt
import time

# 1.0 Data of the Aspen HYSYS file
File = 'Test_1.hsc'
File = 'Test_1.hsc'
Spreadsheets = ('SS_Flash', 'SS_turbine', 'SS_Distillation')
Units = ('Cooler', 'Flash Drum', 'Heater', 'Valve', 'Reactor',
'Distillation Column', 'Turbine', 'Pump')
Units = ('Cooler', 'Flash Drum', 'Heater', 'Valve', 'Reactor', 'Distillation Column', 'Turbine', 'Pump')

# 2.0 Perform connection
Test_1 = Aspen_connection(File, Spreadsheets, Units)
Turbine = Test_1.SS['SS_turbine']
Efficiency = Turbine.Cell(1,0) # .Cell(Column,Row) starting from 0
Generation = Turbine.Cell(1,1)
ori_eff = Efficiency.CellValue
solver = Test_1.Solver
Test_1 = aspen_connection(File, Spreadsheets, Units)
Turbine = Test_1.spreadsheets['SS_turbine']
Efficiency = Turbine.Cell(1, 0) # .Cell(Column,Row) starting from 0
Generation = Turbine.Cell(1, 1)
ori_eff = Efficiency.CellValue
solver = Test_1.solver

print('Original Turbine efficiency: ', ori_eff)

# 3.0 CONVERGENCE TIME PROBLEM
points = 10
eff = np.zeros(points); gen = np.zeros(points)
eff = np.zeros(points)
gen = np.zeros(points)
fig, axs = plt.subplots(1, 2)

## 3.1 Not waiting for solver to stop
# 3.1 Not waiting for solver to stop
for i in range(points):
eff[i] = Efficiency.CellValue
gen[i] = Generation.CellValue
Efficiency.CellValue = eff[i] + 1
### 3.11 Plot

# 3.11 Plot
axs[0].plot(eff, gen, 'ro')
axs[0].set_xlabel('Turbine Efficiency'); axs[0].set_ylabel('Energy Generation [kJ/s]')
axs[0].set_xlabel('Turbine Efficiency')
axs[0].set_ylabel('Energy Generation [kJ/s]')
axs[0].set_title('Not waiting Solver')

## 3.2 Come back to original
eff = np.zeros(points); gen = np.zeros(points); Efficiency.CellValue = ori_eff
# 3.2 Come back to original
eff = np.zeros(points)
gen = np.zeros(points)
Efficiency.CellValue = ori_eff

## 3.3 Waiting for solver to stop
# 3.3 Waiting for solver to stop
for i in range(points):
eff[i] = Efficiency.CellValue
gen[i] = Generation.CellValue
solver.CanSolve = False # Turn off the solving mode
eff[i] = Efficiency.CellValue
gen[i] = Generation.CellValue
solver.CanSolve = False # Turn off the solving mode
Efficiency.CellValue = eff[i] + 1
solver.CanSolve = True # Turn on the solving mode
while solver.IsSolving == True:
solver.CanSolve = True # Turn on the solving mode
while solver.IsSolving:
time.sleep(0.001)


### 3.31 Plot

# 3.31 Plot
axs[1].plot(eff, gen, 'ro')
axs[1].set_xlabel('Turbine Efficiency'); axs[0].set_ylabel('Energy Generation [kJ/s]')
axs[1].set_xlabel('Turbine Efficiency')
axs[0].set_ylabel('Energy Generation [kJ/s]')
axs[1].set_title('Waiting solver')
plt.tight_layout()
plt.show()
plt.close()


# Return to original
Efficiency.CellValue = ori_eff
Efficiency.CellValue = ori_eff
Binary file removed __pycache__/HYSYS_python_spreadsheets.cpython-37.pyc
Binary file not shown.
12 changes: 12 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
contourpy==1.0.7
cycler==0.11.0
fonttools==4.39.4
kiwisolver==1.4.4
matplotlib==3.7.1
numpy==1.24.3
packaging==23.1
Pillow==9.5.0
pyparsing==3.0.9
python-dateutil==2.8.2
pywin32==306
six==1.16.0