ahuora-builder 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ahuora_builder/__init__.py +0 -0
- ahuora_builder/arc_manager.py +33 -0
- ahuora_builder/build_state.py +57 -0
- ahuora_builder/custom/PIDController.py +494 -0
- ahuora_builder/custom/PySMOModel.py +178 -0
- ahuora_builder/custom/SimpleEffectivenessHX_DH.py +727 -0
- ahuora_builder/custom/__init__.py +0 -0
- ahuora_builder/custom/add_initial_dynamics.py +35 -0
- ahuora_builder/custom/custom_compressor.py +107 -0
- ahuora_builder/custom/custom_cooler.py +33 -0
- ahuora_builder/custom/custom_heat_exchanger.py +183 -0
- ahuora_builder/custom/custom_heat_exchanger_1d.py +258 -0
- ahuora_builder/custom/custom_heater.py +41 -0
- ahuora_builder/custom/custom_pressure_changer.py +34 -0
- ahuora_builder/custom/custom_pump.py +107 -0
- ahuora_builder/custom/custom_separator.py +371 -0
- ahuora_builder/custom/custom_tank.py +133 -0
- ahuora_builder/custom/custom_turbine.py +132 -0
- ahuora_builder/custom/custom_valve.py +300 -0
- ahuora_builder/custom/custom_variable.py +29 -0
- ahuora_builder/custom/direct_steam_injection.py +371 -0
- ahuora_builder/custom/energy/__init__.py +0 -0
- ahuora_builder/custom/energy/acBus.py +280 -0
- ahuora_builder/custom/energy/ac_property_package.py +279 -0
- ahuora_builder/custom/energy/battery.py +170 -0
- ahuora_builder/custom/energy/bus.py +182 -0
- ahuora_builder/custom/energy/energy_mixer.py +195 -0
- ahuora_builder/custom/energy/energy_splitter.py +228 -0
- ahuora_builder/custom/energy/grid.py +173 -0
- ahuora_builder/custom/energy/hydro.py +169 -0
- ahuora_builder/custom/energy/link.py +137 -0
- ahuora_builder/custom/energy/load.py +155 -0
- ahuora_builder/custom/energy/mainDistributionBoard.py +257 -0
- ahuora_builder/custom/energy/power_property_package.py +253 -0
- ahuora_builder/custom/energy/solar.py +176 -0
- ahuora_builder/custom/energy/storage.py +230 -0
- ahuora_builder/custom/energy/storage_wrapper +0 -0
- ahuora_builder/custom/energy/tests/__init__.py +0 -0
- ahuora_builder/custom/energy/tests/test_bus.py +44 -0
- ahuora_builder/custom/energy/tests/test_energy_mixer.py +46 -0
- ahuora_builder/custom/energy/tests/test_mdb.py +49 -0
- ahuora_builder/custom/energy/transformer.py +187 -0
- ahuora_builder/custom/energy/transformer_property_package.py +267 -0
- ahuora_builder/custom/energy/transmissionLine.py +228 -0
- ahuora_builder/custom/energy/wind.py +206 -0
- ahuora_builder/custom/hda_ideal_VLE.py +1341 -0
- ahuora_builder/custom/hda_reaction.py +182 -0
- ahuora_builder/custom/heat_exchanger_1d_wrapper.py +31 -0
- ahuora_builder/custom/integration_block.py +106 -0
- ahuora_builder/custom/inverted.py +81 -0
- ahuora_builder/custom/performance_curves.py +1 -0
- ahuora_builder/custom/reactions/__init__.py +0 -0
- ahuora_builder/custom/reactions/hda_stoich.py +10 -0
- ahuora_builder/custom/simple_separator.py +680 -0
- ahuora_builder/custom/tests/__init__.py +0 -0
- ahuora_builder/custom/tests/test_SimpleEffectivenessHX_DH.py +91 -0
- ahuora_builder/custom/tests/test_custom_tank.py +70 -0
- ahuora_builder/custom/tests/test_direct_steam_injection.py +41 -0
- ahuora_builder/custom/tests/test_simple_separator.py +46 -0
- ahuora_builder/custom/tests/test_waterpipe.py +46 -0
- ahuora_builder/custom/thermal_utility_systems/desuperheater.py +624 -0
- ahuora_builder/custom/thermal_utility_systems/header.py +889 -0
- ahuora_builder/custom/thermal_utility_systems/simple_heat_pump.py +567 -0
- ahuora_builder/custom/thermal_utility_systems/steam_header.py +353 -0
- ahuora_builder/custom/thermal_utility_systems/steam_user.py +944 -0
- ahuora_builder/custom/thermal_utility_systems/temp.py +349 -0
- ahuora_builder/custom/thermal_utility_systems/tests/test_desuperheater.py +142 -0
- ahuora_builder/custom/thermal_utility_systems/tests/test_header.py +998 -0
- ahuora_builder/custom/thermal_utility_systems/tests/test_ntu_hx.py +129 -0
- ahuora_builder/custom/thermal_utility_systems/tests/test_simple_heat_pump.py +120 -0
- ahuora_builder/custom/thermal_utility_systems/tests/test_steam_header.py +703 -0
- ahuora_builder/custom/thermal_utility_systems/tests/test_steam_user.py +277 -0
- ahuora_builder/custom/thermal_utility_systems/tests/test_waterpipe.py +36 -0
- ahuora_builder/custom/thermal_utility_systems/tests/test_willans_turbine.py +253 -0
- ahuora_builder/custom/thermal_utility_systems/willans_turbine.py +804 -0
- ahuora_builder/custom/translator.py +129 -0
- ahuora_builder/custom/updated_pressure_changer.py +1404 -0
- ahuora_builder/custom/valve_wrapper.py +38 -0
- ahuora_builder/custom/water_tank_with_units.py +456 -0
- ahuora_builder/diagnostics/__init__.py +0 -0
- ahuora_builder/diagnostics/infeasibilities.py +40 -0
- ahuora_builder/diagnostics/tests/__init__.py +0 -0
- ahuora_builder/diagnostics/tests/test_infeasibilities.py +28 -0
- ahuora_builder/flowsheet_manager.py +542 -0
- ahuora_builder/flowsheet_manager_type.py +20 -0
- ahuora_builder/generate_python_file.py +440 -0
- ahuora_builder/methods/BlockContext.py +84 -0
- ahuora_builder/methods/__init__.py +0 -0
- ahuora_builder/methods/adapter.py +355 -0
- ahuora_builder/methods/adapter_library.py +549 -0
- ahuora_builder/methods/adapter_methods.py +80 -0
- ahuora_builder/methods/expression_parsing.py +105 -0
- ahuora_builder/methods/load_unit_model.py +147 -0
- ahuora_builder/methods/slice_manipulation.py +7 -0
- ahuora_builder/methods/tests/__init__.py +0 -0
- ahuora_builder/methods/tests/test_expression_parsing.py +15 -0
- ahuora_builder/methods/units_handler.py +129 -0
- ahuora_builder/ml_wizard.py +101 -0
- ahuora_builder/port_manager.py +20 -0
- ahuora_builder/properties_manager.py +44 -0
- ahuora_builder/property_package_manager.py +78 -0
- ahuora_builder/solver.py +38 -0
- ahuora_builder/tear_manager.py +98 -0
- ahuora_builder/tests/__init__.py +0 -0
- ahuora_builder/tests/test_generate_python_file/__init__.py +0 -0
- ahuora_builder/tests/test_generate_python_file/configurations/compressor_generated.py +63 -0
- ahuora_builder/tests/test_generate_python_file/configurations/heat_exchanger_generated.py +70 -0
- ahuora_builder/tests/test_generate_python_file/configurations/pump_generated.py +84 -0
- ahuora_builder/tests/test_generate_python_file/configurations/recycle_generated.py +73 -0
- ahuora_builder/tests/test_generate_python_file/test_generate_python_file.py +108 -0
- ahuora_builder/tests/test_solver/__init__.py +0 -0
- ahuora_builder/tests/test_solver/configurations/BT_PR.json +59 -0
- ahuora_builder/tests/test_solver/configurations/BT_PR_solved.json +59 -0
- ahuora_builder/tests/test_solver/configurations/bus.json +99 -0
- ahuora_builder/tests/test_solver/configurations/bus_solved.json +50 -0
- ahuora_builder/tests/test_solver/configurations/compound_separator.json +377 -0
- ahuora_builder/tests/test_solver/configurations/compound_separator_solved.json +374 -0
- ahuora_builder/tests/test_solver/configurations/compressor.json +38 -0
- ahuora_builder/tests/test_solver/configurations/compressor_solved.json +68 -0
- ahuora_builder/tests/test_solver/configurations/constraints.json +44 -0
- ahuora_builder/tests/test_solver/configurations/constraints_solved.json +59 -0
- ahuora_builder/tests/test_solver/configurations/control.json +39 -0
- ahuora_builder/tests/test_solver/configurations/control_solved.json +68 -0
- ahuora_builder/tests/test_solver/configurations/dynamic_tank.json +733 -0
- ahuora_builder/tests/test_solver/configurations/dynamic_tank_solved.json +846 -0
- ahuora_builder/tests/test_solver/configurations/elimination.json +39 -0
- ahuora_builder/tests/test_solver/configurations/elimination_solved.json +68 -0
- ahuora_builder/tests/test_solver/configurations/expressions.json +68 -0
- ahuora_builder/tests/test_solver/configurations/expressions_solved.json +104 -0
- ahuora_builder/tests/test_solver/configurations/header.json +1192 -0
- ahuora_builder/tests/test_solver/configurations/header_solved.json +761 -0
- ahuora_builder/tests/test_solver/configurations/heat_exchanger.json +63 -0
- ahuora_builder/tests/test_solver/configurations/heat_exchanger_solved.json +104 -0
- ahuora_builder/tests/test_solver/configurations/heat_pump.json +137 -0
- ahuora_builder/tests/test_solver/configurations/heat_pump_solved.json +104 -0
- ahuora_builder/tests/test_solver/configurations/machine_learning.json +2156 -0
- ahuora_builder/tests/test_solver/configurations/machine_learning_solved.json +266 -0
- ahuora_builder/tests/test_solver/configurations/mass_flow_tear.json +77 -0
- ahuora_builder/tests/test_solver/configurations/mass_flow_tear_solved.json +68 -0
- ahuora_builder/tests/test_solver/configurations/milk_heater.json +521 -0
- ahuora_builder/tests/test_solver/configurations/milk_heater_solved.json +311 -0
- ahuora_builder/tests/test_solver/configurations/mixer.json +44 -0
- ahuora_builder/tests/test_solver/configurations/mixer_solved.json +86 -0
- ahuora_builder/tests/test_solver/configurations/optimization.json +62 -0
- ahuora_builder/tests/test_solver/configurations/optimization_solved.json +59 -0
- ahuora_builder/tests/test_solver/configurations/propane_heat_pump.json +167 -0
- ahuora_builder/tests/test_solver/configurations/propane_heat_pump_solved.json +158 -0
- ahuora_builder/tests/test_solver/configurations/propane_recycle.json +141 -0
- ahuora_builder/tests/test_solver/configurations/propane_recycle_solved.json +104 -0
- ahuora_builder/tests/test_solver/configurations/pump.json +64 -0
- ahuora_builder/tests/test_solver/configurations/pump_solved.json +59 -0
- ahuora_builder/tests/test_solver/configurations/pump_unit_conversions.json +63 -0
- ahuora_builder/tests/test_solver/configurations/recycle.json +49 -0
- ahuora_builder/tests/test_solver/configurations/recycle_solved.json +50 -0
- ahuora_builder/tests/test_solver/configurations/sb_vapor_frac.json +29 -0
- ahuora_builder/tests/test_solver/configurations/sb_vapor_frac_solved.json +29 -0
- ahuora_builder/tests/test_solver/configurations/solar.json +67 -0
- ahuora_builder/tests/test_solver/configurations/solar_solved.json +50 -0
- ahuora_builder/tests/test_solver/configurations/vapor_frac_target.json +67 -0
- ahuora_builder/tests/test_solver/configurations/vapor_frac_target_solved.json +68 -0
- ahuora_builder/tests/test_solver/test_solve_models.py +250 -0
- ahuora_builder/timing.py +65 -0
- ahuora_builder/types/__init__.py +1 -0
- ahuora_builder/unit_model_manager.py +48 -0
- ahuora_builder-0.1.0.dist-info/METADATA +14 -0
- ahuora_builder-0.1.0.dist-info/RECORD +167 -0
- ahuora_builder-0.1.0.dist-info/WHEEL +4 -0
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
# Methods and helper functions to train and use a surrogate valve.
|
|
2
|
+
from pyomo.core.base.expression import ScalarExpression
|
|
3
|
+
from idaes.core import MaterialBalanceType, ControlVolume0DBlock, declare_process_block_class, EnergyBalanceType, MomentumBalanceType, MaterialBalanceType, useDefault, UnitModelBlockData
|
|
4
|
+
from pyomo.common.config import ConfigBlock, ConfigValue, In
|
|
5
|
+
from idaes.core.surrogate.surrogate_block import SurrogateBlock
|
|
6
|
+
from idaes.core.util.config import is_physical_parameter_block
|
|
7
|
+
from ahuora_builder.methods.expression_parsing import get_property_from_id
|
|
8
|
+
from idaes.core.surrogate.pysmo_surrogate import PysmoSurrogate
|
|
9
|
+
from io import StringIO
|
|
10
|
+
import json
|
|
11
|
+
from pyomo.environ import Var, Constraint
|
|
12
|
+
from pyomo.environ import (
|
|
13
|
+
Constraint,
|
|
14
|
+
Set,
|
|
15
|
+
Var,
|
|
16
|
+
Suffix,
|
|
17
|
+
units as pyunits,
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
def make_control_volume(unit, name, config):
|
|
21
|
+
if config.dynamic is not False:
|
|
22
|
+
raise ValueError('SurrogateValve does not support dynamics')
|
|
23
|
+
if config.has_holdup is not False:
|
|
24
|
+
raise ValueError('SurrogateValve does not support holdup')
|
|
25
|
+
|
|
26
|
+
control_volume = ControlVolume0DBlock(
|
|
27
|
+
dynamic=config.dynamic,
|
|
28
|
+
has_holdup=config.has_holdup,
|
|
29
|
+
property_package=config.property_package,
|
|
30
|
+
property_package_args=config.property_package_args)
|
|
31
|
+
|
|
32
|
+
# Add the control volume block to the unit
|
|
33
|
+
setattr(unit, name, control_volume)
|
|
34
|
+
|
|
35
|
+
control_volume.add_state_blocks(has_phase_equilibrium=config.has_phase_equilibrium)
|
|
36
|
+
# control_volume.add_material_balances(balance_type=config.material_balance_type,
|
|
37
|
+
# has_phase_equilibrium=config.has_phase_equilibrium)
|
|
38
|
+
# control_volume.add_total_enthalpy_balances(has_heat_of_reaction=False,
|
|
39
|
+
# has_heat_transfer=False,
|
|
40
|
+
# has_work_transfer=False)
|
|
41
|
+
@declare_process_block_class("PySMOModel")
|
|
42
|
+
class PySMOModelData(UnitModelBlockData):
|
|
43
|
+
CONFIG = UnitModelBlockData.CONFIG()
|
|
44
|
+
# Declare all the standard config arguments for the control_volume
|
|
45
|
+
CONFIG.declare("material_balance_type", ConfigValue(default=MaterialBalanceType.componentPhase, domain=In(MaterialBalanceType)))
|
|
46
|
+
CONFIG.declare("energy_balance_type", ConfigValue(default=EnergyBalanceType.enthalpyTotal, domain=In([EnergyBalanceType.enthalpyTotal])))
|
|
47
|
+
CONFIG.declare("momentum_balance_type", ConfigValue(default=MomentumBalanceType.none, domain=In([MomentumBalanceType.none])))
|
|
48
|
+
CONFIG.declare("has_phase_equilibrium", ConfigValue(default=False, domain=In([False])))
|
|
49
|
+
CONFIG.declare("has_pressure_change", ConfigValue(default=False, domain=In([False])))
|
|
50
|
+
CONFIG.declare("property_package", ConfigValue(default=useDefault, domain=is_physical_parameter_block))
|
|
51
|
+
CONFIG.declare("property_package_args", ConfigBlock(implicit=True))
|
|
52
|
+
# no other args need to be declared, we are just hardcoding the valve model.
|
|
53
|
+
CONFIG.declare("model", ConfigValue())
|
|
54
|
+
CONFIG.declare("ids", ConfigValue())
|
|
55
|
+
CONFIG.declare("unitopNames", ConfigValue())
|
|
56
|
+
CONFIG.declare(
|
|
57
|
+
"num_inlets",
|
|
58
|
+
ConfigValue(
|
|
59
|
+
default=False,
|
|
60
|
+
domain=int,
|
|
61
|
+
description="Number of inlets to add",
|
|
62
|
+
doc="Number of inlets to add",
|
|
63
|
+
),
|
|
64
|
+
)
|
|
65
|
+
CONFIG.declare(
|
|
66
|
+
"num_outlets",
|
|
67
|
+
ConfigValue(
|
|
68
|
+
default=False,
|
|
69
|
+
domain=int,
|
|
70
|
+
description="Number of outlets to add",
|
|
71
|
+
doc="Number of outlets to add",
|
|
72
|
+
),
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
def build(self):
|
|
76
|
+
super(PySMOModelData, self).build()
|
|
77
|
+
self.CONFIG.dynamic = False
|
|
78
|
+
self.CONFIG.has_holdup = False
|
|
79
|
+
# This function handles adding the control volume block to the unit,
|
|
80
|
+
# and addiing the necessary material and energy balances.
|
|
81
|
+
make_control_volume(self, "control_volume", self.CONFIG)
|
|
82
|
+
|
|
83
|
+
# self.add_inlet_port()
|
|
84
|
+
# self.add_outlet_port()
|
|
85
|
+
|
|
86
|
+
# Defining parameters of state block class
|
|
87
|
+
tmp_dict = dict(**self.config.property_package_args)
|
|
88
|
+
tmp_dict["parameters"] = self.config.property_package
|
|
89
|
+
tmp_dict["defined_state"] = True # inlet block is an inlet
|
|
90
|
+
|
|
91
|
+
# Add state blocks for inlet, outlet, and waste
|
|
92
|
+
# These include the state variables and any other properties on demand
|
|
93
|
+
num_inlets = self.config.num_inlets
|
|
94
|
+
self.inlet_list = [ "inlet_" + str(i+1) for i in range(num_inlets) ]
|
|
95
|
+
self.inlet_set = Set(initialize=self.inlet_list)
|
|
96
|
+
self.inlet_blocks = []
|
|
97
|
+
|
|
98
|
+
for name in self.inlet_list:
|
|
99
|
+
# add properties_inlet_1, properties_inlet2 etc
|
|
100
|
+
state_block = self.config.property_package.state_block_class(
|
|
101
|
+
self.flowsheet().config.time, doc="inlet ml", **tmp_dict
|
|
102
|
+
)
|
|
103
|
+
self.inlet_blocks.append(state_block)
|
|
104
|
+
# Dynamic equivalent to self.properties_inlet_1 = stateblock
|
|
105
|
+
setattr(self,"properties_" + name, state_block)
|
|
106
|
+
# also add the port
|
|
107
|
+
self.add_port(name=name,block=state_block)
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
# Add outlet state blocks
|
|
111
|
+
|
|
112
|
+
num_outlets = self.config.num_outlets
|
|
113
|
+
self.outlet_list = [ "outlet_" + str(i+1) for i in range(num_outlets) ]
|
|
114
|
+
self.outlet_set = Set(initialize=self.outlet_list)
|
|
115
|
+
self.outlet_blocks = []
|
|
116
|
+
|
|
117
|
+
for name in self.outlet_list:
|
|
118
|
+
tmp_dict["defined_state"] = False
|
|
119
|
+
state_block = self.config.property_package.state_block_class(
|
|
120
|
+
self.flowsheet().config.time, doc="outlet ml", **tmp_dict
|
|
121
|
+
)
|
|
122
|
+
self.outlet_blocks.append(state_block)
|
|
123
|
+
setattr(self,"properties_" + name, state_block)
|
|
124
|
+
self.add_port(name=name,block=state_block)
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
# Add variables for custom properties
|
|
129
|
+
names = self.config.unitopNames
|
|
130
|
+
for name in names:
|
|
131
|
+
self.add_component(
|
|
132
|
+
name, Var(self.flowsheet().time, initialize=10)
|
|
133
|
+
)
|
|
134
|
+
|
|
135
|
+
def initialize(self, *args, **kwargs):
|
|
136
|
+
model_data = self.config.model
|
|
137
|
+
json_str = json.dumps(model_data)
|
|
138
|
+
f = StringIO(json_str)
|
|
139
|
+
|
|
140
|
+
model = PysmoSurrogate.load(f)
|
|
141
|
+
ids = self.config.ids
|
|
142
|
+
fs = self.flowsheet()
|
|
143
|
+
# TODO: Make surrogate models work with dynamics. This involves making a surrogate model for each time step.
|
|
144
|
+
# Not sure if idaes has a framework for doing this or not.
|
|
145
|
+
# For now, we are just assuming steady state and time_index=0.
|
|
146
|
+
inputs = [get_property_from_id(fs, i,0) for i in ids["input"]]
|
|
147
|
+
outputs = [get_property_from_id(fs, i,0) for i in ids["output"]]
|
|
148
|
+
|
|
149
|
+
self.check_is_expression(inputs)
|
|
150
|
+
self.check_is_expression(outputs)
|
|
151
|
+
|
|
152
|
+
self.surrogate = SurrogateBlock(concrete=True)
|
|
153
|
+
self.surrogate.build_model(model,input_vars=inputs, output_vars=outputs)
|
|
154
|
+
|
|
155
|
+
def check_is_expression(self, vars):
|
|
156
|
+
for index, var in enumerate(vars):
|
|
157
|
+
if isinstance(var, ScalarExpression):
|
|
158
|
+
name = f"{var.name}_{index}"
|
|
159
|
+
new_var = Var(self.flowsheet().time, initialize=1)
|
|
160
|
+
self.add_component(name, new_var)
|
|
161
|
+
def constraint_rule(model, t):
|
|
162
|
+
if var.is_indexed():
|
|
163
|
+
return new_var[t] == var[t]
|
|
164
|
+
else:
|
|
165
|
+
return new_var[t] == var
|
|
166
|
+
|
|
167
|
+
self.add_component(f"{name}_constraint", Constraint(self.flowsheet().time, rule=constraint_rule))
|
|
168
|
+
vars[index] = new_var
|
|
169
|
+
|
|
170
|
+
def _get_stream_table_contents(self, time_point=0):
|
|
171
|
+
|
|
172
|
+
io_dict = {}
|
|
173
|
+
for inlet_name in self.inlet_list:
|
|
174
|
+
io_dict[inlet_name] = getattr(self, inlet_name) # get a reference to the port
|
|
175
|
+
|
|
176
|
+
out_dict = {}
|
|
177
|
+
for outlet_name in self.outlet_list:
|
|
178
|
+
out_dict[outlet_name] = getattr(self, outlet_name) # get a reference to the port
|