Skip to content

Commit

Permalink
towards a better behaved model
Browse files Browse the repository at this point in the history
  • Loading branch information
dallan-keylogic committed Aug 14, 2023
1 parent 2c524b3 commit ca2c217
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 96 deletions.
180 changes: 93 additions & 87 deletions idaes/models/unit_models/separator.py
Original file line number Diff line number Diff line change
Expand Up @@ -1258,7 +1258,7 @@ def partition_outlet_flows(self, mb, outlet_list):

# Add empty Port objects
for o in outlet_list:
p_obj = Port(noruleinit=True, doc="Outlet Port")
p_obj = Port(doc="Outlet Port")
setattr(self, o, p_obj)

# Iterate over members to create References or Expressions
Expand Down Expand Up @@ -1453,38 +1453,40 @@ def e_rule(b, t, j):
return self.eps

else:
def e_rule_factory(l_name_alias):
def e_rule(b, t, j):
mfp = mb[t].component(
"{0}_phase{1}".format(l_name_alias[:-5], l_name_alias[-5:])
)

def e_rule(b, t, j):
mfp = mb[t].component(
"{0}_phase{1}".format(l_name[:-5], l_name[-5:])
)

if mfp is None:
_raise_split_indexed_fail_err(self.name, s)
if mfp is None:
_raise_split_indexed_fail_err(self.name, s)

for p in mb.phase_list:
if (p, j) in pc_set:
if (
self.config.split_basis
== SplittingType.phaseFlow
):
s_check = split_map[p]
elif (
self.config.split_basis
== SplittingType.phaseComponentFlow
):
s_check = split_map[p, j]
else:
raise BurntToast(
"{} This should not happen. Please"
" report this bug to the IDAES "
"developers.".format(self.name)
)
for p in mb.phase_list:
if (p, j) in pc_set:
if (
self.config.split_basis
== SplittingType.phaseFlow
):
s_check = split_map[p]
elif (
self.config.split_basis
== SplittingType.phaseComponentFlow
):
s_check = split_map[p, j]
else:
raise BurntToast(
"{} This should not happen. Please"
" report this bug to the IDAES "
"developers.".format(self.name)
)

if s_check == o:
return mfp[p, j]
# else:
return self.eps
if s_check == o:
return mfp[p, j]
return self.eps
return e_rule
e_rule = e_rule_factory(l_name)


e_obj = VarLikeExpression(
self.flowsheet().time,
Expand All @@ -1493,78 +1495,82 @@ def e_rule(b, t, j):
)

else:
# Unindexed variable
def e_rule(b, t):
if self.config.split_basis == SplittingType.phaseFlow:
ivar = mb[t].component(l_name + "_phase")
if ivar is not None:
for p in mb.phase_list:
if split_map[p] == o:
return ivar[p]
else:
continue
else:
ivar = mb[t].component(l_name + "_phase_comp")
# Variable unindexed by phase or component
def e_rule_factory(l_name_alias):
def e_rule(b, t):
if self.config.split_basis == SplittingType.phaseFlow:
ivar = mb[t].component(l_name_alias + "_phase")
if ivar is not None:
for p in mb.phase_list:
if split_map[p] == o:
return sum(
ivar[p, j]
for j in mb.component_list
if (p, j) in pc_set
)
return ivar[p]
else:
continue
else:
_raise_split_unindexed_fail_err(self.name, s)

elif self.config.split_basis == SplittingType.componentFlow:
ivar = mb[t].component(l_name + "_comp")
if ivar is not None:
for j in mb.component_list:
if split_map[j] == o:
return ivar[j]
ivar = mb[t].component(l_name_alias + "_phase_comp")
if ivar is not None:
for p in mb.phase_list:
if split_map[p] == o:
return sum(
ivar[p, j]
for j in mb.component_list
if (p, j) in pc_set
)
else:
continue
else:
continue
else:
ivar = mb[t].component(l_name + "_phase_comp")
_raise_split_unindexed_fail_err(self.name, s)

elif self.config.split_basis == SplittingType.componentFlow:
ivar = mb[t].component(l_name_alias + "_comp")
if ivar is not None:
for j in mb.component_list:
if split_map[j] == o:
return sum(
ivar[p, j]
for p in mb.phase_list
if (p, j) in pc_set
)
return ivar[j]
else:
continue
else:
ivar = mb[t].component(l_name_alias + "_phase_comp")
if ivar is not None:
for j in mb.component_list:
if split_map[j] == o:
return sum(
ivar[p, j]
for p in mb.phase_list
if (p, j) in pc_set
)
else:
continue
else:
_raise_split_unindexed_fail_err(self.name, s)
elif (
self.config.split_basis
== SplittingType.phaseComponentFlow
):
ivar = mb[t].component(l_name_alias + "_phase_comp")
if ivar is not None:
for p in mb.phase_list:
for j in mb.component_list:
if (
split_map[p, j] == o
and (p, j) in pc_set
):
return ivar[p, j]
else:
continue
else:
_raise_split_unindexed_fail_err(self.name, s)
elif (
self.config.split_basis
== SplittingType.phaseComponentFlow
):
ivar = mb[t].component(l_name + "_phase_comp")
if ivar is not None:
for p in mb.phase_list:
for j in mb.component_list:
if (
split_map[p, j] == o
and (p, j) in pc_set
):
return ivar[p, j]
else:
continue
else:
_raise_split_unindexed_fail_err(self.name, s)
else:
# Unrecognised split tupe
raise BurntToast(
"{} received unrecognised value for "
"split_basis argument. This should never "
"happen, so please contact the IDAES "
"developers with this bug.".format(self.name)
)
# Unrecognised split tupe
raise BurntToast(
"{} received unrecognised value for "
"split_basis argument. This should never "
"happen, so please contact the IDAES "
"developers with this bug.".format(self.name)
)
return e_rule

e_rule = e_rule_factory(l_name)

e_obj = VarLikeExpression(self.flowsheet().time, rule=e_rule)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@

__author__ = "Douglas Allan"


from functools import partial

from pyomo.common.config import ConfigValue, ConfigBlock
import pyomo.environ as pyo

Expand Down Expand Up @@ -198,14 +201,14 @@ def rule_mole_frac(blk, t, port, comps):
self,
f"{port_name}_temperature_eqn",
pyo.Constraint(
tset, rule=lambda blk, t: rule_temperature(blk, t, props, port)
tset, rule=partial(rule_temperature, props=props, port=port)
),
)
setattr(
self,
f"{port_name}_pressure_eqn",
pyo.Constraint(
tset, rule=lambda blk, t: rule_pressure(blk, t, props, port)
tset, rule=partial(rule_pressure, props=props, port=port)
),
)
setattr(
Expand All @@ -214,9 +217,7 @@ def rule_mole_frac(blk, t, port, comps):
pyo.Constraint(
tset,
side_comps,
rule=lambda blk, t, j: rule_flow_mol_comp(
blk, t, j, props, port
),
rule=partial(rule_flow_mol_comp, props=props, port=port),
),
)
if direction == "in":
Expand All @@ -225,9 +226,7 @@ def rule_mole_frac(blk, t, port, comps):
f"{port_name}_mole_frac_eqn",
pyo.Constraint(
tset,
rule=lambda blk, t: rule_mole_frac(
blk, t, port, side_comps
),
rule=partial(rule_mole_frac, port=port, comps=side_comps),
),
)
if direction == "out":
Expand All @@ -237,7 +236,7 @@ def rule_mole_frac(blk, t, port, comps):
pyo.Constraint(
tset,
absent_comp_list,
rule=lambda blk, t, j: rule_absent_comp(blk, t, j, props),
rule=partial(rule_absent_comp, props=props),
),
)
# Add a different port at the module level
Expand Down

0 comments on commit ca2c217

Please sign in to comment.