Skip to content

Commit

Permalink
Adding attributes to hold units used by MSContactor (#1396)
Browse files Browse the repository at this point in the history
* Adding attributes to hold units used by MSContactor

* Removing unneccessary argument and fixing tests
  • Loading branch information
Andrew Lee authored Apr 16, 2024
1 parent 45c8ff0 commit 3a1d54a
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 97 deletions.
65 changes: 31 additions & 34 deletions idaes/models/unit_models/mscontactor.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,17 +385,21 @@ def build(self):
# Call UnitModel.build
super().build()

# Placeholders for things we will get from first StateBlock
self.flow_basis = None
self.uom = None

self._verify_inputs()
flow_basis, uom = self._build_state_blocks()
self._build_state_blocks()

if self.config.heterogeneous_reactions is not None:
self._build_heterogeneous_reaction_blocks()

self._add_geometry(uom)
self._add_geometry()

self._build_material_balance_constraints(flow_basis, uom)
self._build_energy_balance_constraints(uom)
self._build_pressure_balance_constraints(uom)
self._build_material_balance_constraints()
self._build_energy_balance_constraints()
self._build_pressure_balance_constraints()
self._build_ports()

def _verify_inputs(self):
Expand Down Expand Up @@ -461,10 +465,6 @@ def _verify_inputs(self):

def _build_state_blocks(self):
# Build state blocks
# Placeholders for things we will get from first StateBlock
flow_basis = None
uom = None

for stream, pconfig in self.config.streams.items():
ppack = pconfig.property_package

Expand Down Expand Up @@ -514,19 +514,19 @@ def _build_state_blocks(self):

tref = self.flowsheet().time.first()
sref = self.elements.first()
if flow_basis is None:
if self.flow_basis is None:
# Set unit level flow basis and units from first stream

flow_basis = state[tref, sref].get_material_flow_basis()
uom = state[tref, sref].params.get_metadata().derived_units
self.flow_basis = state[tref, sref].get_material_flow_basis()
self.uom = state[tref, sref].params.get_metadata().derived_units
else:
# Check that flow bases are consistent
if not state[tref, sref].get_material_flow_basis() == flow_basis:
if not state[tref, sref].get_material_flow_basis() == self.flow_basis:
raise ConfigurationError(
f"Property packages use different flow bases: ExtractionCascade "
f"requires all property packages to use the same basis. "
f"{stream} uses {state[tref, sref].get_material_flow_basis()}, "
f"whilst first stream uses {flow_basis}."
f"whilst first stream uses {self.flow_basis}."
)

# Build reactions blocks if provided
Expand All @@ -543,8 +543,6 @@ def _build_state_blocks(self):
)
self.add_component(stream + "_reactions", reactions)

return flow_basis, uom

def _build_heterogeneous_reaction_blocks(self):
rpack = self.config.heterogeneous_reactions
rpack_args = self.config.heterogeneous_reactions_args
Expand All @@ -569,14 +567,14 @@ def _build_heterogeneous_reaction_blocks(self):
"reactions (reaction_idx)."
)

def _add_geometry(self, uom):
def _add_geometry(self):
if self.config.has_holdup:
# Add volume for each element
# TODO: Assuming constant volume for now
self.volume = Var(
self.elements,
initialize=1,
units=uom.VOLUME,
units=self.uom.VOLUME,
doc="Volume of element",
)
self.volume_frac_stream = Var(
Expand All @@ -600,14 +598,14 @@ def sum_volume_frac(b, t, e):
phase_list = getattr(self, stream).phase_list
_add_phase_fractions(self, stream, phase_list)

def _build_material_balance_constraints(self, flow_basis, uom):
def _build_material_balance_constraints(self):
# Get units for transfer terms
if flow_basis is MaterialFlowBasis.molar:
mb_units = uom.FLOW_MOLE
hu_units = uom.AMOUNT
elif flow_basis is MaterialFlowBasis.mass:
mb_units = uom.FLOW_MASS
hu_units = uom.MASS
if self.flow_basis is MaterialFlowBasis.molar:
mb_units = self.uom.FLOW_MOLE
hu_units = self.uom.AMOUNT
elif self.flow_basis is MaterialFlowBasis.mass:
mb_units = self.uom.FLOW_MASS
hu_units = self.uom.MASS
else:
# Flow type other, so cannot determine units
mb_units = None
Expand Down Expand Up @@ -895,9 +893,8 @@ def _build_material_balance_constraints(self, flow_basis, uom):
)
self.add_component(stream + "_material_balance", mbal)

def _build_energy_balance_constraints(self, uom):
def _build_energy_balance_constraints(self):
# Energy Balances

for stream, pconfig in self.config.streams.items():
if pconfig.has_energy_balance:
state_block = getattr(self, stream)
Expand All @@ -912,7 +909,7 @@ def _build_energy_balance_constraints(self, uom):
domain=Reals,
initialize=1.0,
doc="Energy holdup of stream in element",
units=uom.ENERGY,
units=self.uom.ENERGY,
)
self.add_component(
stream + "_energy_holdup",
Expand All @@ -939,7 +936,7 @@ def _build_energy_balance_constraints(self, uom):
energy_holdup,
wrt=self.flowsheet().time,
doc="Energy accumulation for in element",
units=uom.POWER,
units=self.uom.POWER,
)
self.add_component(
stream + "_energy_accumulation",
Expand All @@ -951,7 +948,7 @@ def _build_energy_balance_constraints(self, uom):
self.flowsheet().time,
self.elements,
initialize=0,
units=uom.POWER,
units=self.uom.POWER,
doc=f"External heat transfer term for stream {stream}",
)
self.add_component(stream + "_heat", heat)
Expand All @@ -962,12 +959,12 @@ def _build_energy_balance_constraints(self, uom):
rule=partial(
_energy_balance_rule,
stream=stream,
uom=uom,
uom=self.uom,
),
)
self.add_component(stream + "_energy_balance", ebal)

def _build_pressure_balance_constraints(self, uom):
def _build_pressure_balance_constraints(self):
# Pressure Balances
for stream, pconfig in self.config.streams.items():
if pconfig.has_pressure_balance:
Expand All @@ -977,7 +974,7 @@ def _build_pressure_balance_constraints(self, uom):
self.flowsheet().time,
self.elements,
initialize=0,
units=uom.PRESSURE,
units=self.uom.PRESSURE,
doc=f"DeltaP term for stream {stream}",
)
self.add_component(stream + "_deltaP", deltaP)
Expand All @@ -988,7 +985,7 @@ def _build_pressure_balance_constraints(self, uom):
rule=partial(
_pressure_balance_rule,
stream=stream,
uom=uom,
uom=self.uom,
),
)
self.add_component(stream + "_pressure_balance", pbal)
Expand Down
Loading

0 comments on commit 3a1d54a

Please sign in to comment.