Skip to content

Commit

Permalink
Addressing Demo Flowsheet (Scaling) Failures (#1266)
Browse files Browse the repository at this point in the history
* Refine some scaling in demo flowsheet

* Run black

* Actually run black

* Try to add some more scaling, fix zero variable specs

* remove unused imports

* Clean up files

---------

Co-authored-by: Brandon Paul <[email protected]>
Co-authored-by: Brandon Paul <[email protected]>
  • Loading branch information
3 people authored Oct 16, 2023
1 parent aa5212a commit 494b086
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 37 deletions.
111 changes: 75 additions & 36 deletions idaes/models/flowsheets/demo_flowsheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
Constructs a basic flowsheet for a benzene-toluene system with a mixer, heater
and flash unit.
"""

from pyomo.environ import ConcreteModel, TransformationFactory
from pyomo.network import Arc

Expand Down Expand Up @@ -57,15 +56,28 @@ def build_flowsheet():

def set_scaling(m):
"""Set scaling for demo flowsheet"""
m.fs.BT_props.set_default_scaling(
"flow_mol_phase_comp", 1e2, index=("Liq", "benzene")

iscale.set_scaling_factor(m.fs.M01.inlet_1_state[0].flow_mol_phase, 1e6)
iscale.set_scaling_factor(m.fs.M01.inlet_1_state[0].flow_mol_phase["Liq"], 1e6)
iscale.set_scaling_factor(m.fs.M01.inlet_1_state[0].flow_mol_phase["Vap"], 1)
iscale.set_scaling_factor(
m.fs.M01.inlet_1_state[0].mole_frac_phase_comp["Liq", "benzene"], 1
)
m.fs.BT_props.set_default_scaling(
"flow_mol_phase_comp", 1e2, index=("Liq", "toluene")
iscale.set_scaling_factor(
m.fs.M01.inlet_1_state[0].mole_frac_phase_comp["Vap", "benzene"], 1
)
iscale.set_scaling_factor(
m.fs.M01.inlet_1_state[0].mole_frac_phase_comp["Liq", "toluene"], 1e5
)
iscale.set_scaling_factor(
m.fs.M01.inlet_1_state[0].mole_frac_phase_comp["Vap", "toluene"], 1e6
)
iscale.set_scaling_factor(
m.fs.M01.inlet_1_state[0].pressure_sat_comp["benzene"], 1e-5
)
iscale.set_scaling_factor(
m.fs.M01.inlet_1_state[0].pressure_sat_comp["toluene"], 1e-4
)

iscale.set_scaling_factor(m.fs.M01.inlet_1_state[0].mole_frac_comp["benzene"], 1)
iscale.set_scaling_factor(m.fs.M01.inlet_1_state[0].mole_frac_comp["toluene"], 1e4)
iscale.set_scaling_factor(
m.fs.M01.inlet_1_state[0].enth_mol_phase_comp["Liq", "benzene"], 1e-4
)
Expand All @@ -78,45 +90,42 @@ def set_scaling(m):
iscale.set_scaling_factor(
m.fs.M01.inlet_1_state[0].enth_mol_phase_comp["Vap", "toluene"], 1e-4
)
iscale.set_scaling_factor(m.fs.M01.inlet_1_state[0].eq_total, 1)
iscale.set_scaling_factor(m.fs.M01.inlet_1_state[0].eq_sum_mol_frac, 1)
iscale.set_scaling_factor(m.fs.M01.inlet_1_state[0].eq_comp["benzene"], 1)
iscale.set_scaling_factor(m.fs.M01.inlet_1_state[0].eq_comp["toluene"], 1)

iscale.set_scaling_factor(m.fs.M01.inlet_2_state[0].flow_mol_phase, 1e1)
iscale.set_scaling_factor(m.fs.M01.inlet_2_state[0].flow_mol_phase["Vap"], 1e1)

iscale.set_scaling_factor(
m.fs.M01.inlet_2_state[0].mole_frac_phase_comp["Liq", "benzene"], 1e5
)
iscale.set_scaling_factor(
m.fs.M01.inlet_1_state[0].eq_phase_equilibrium["benzene"], 1
m.fs.M01.inlet_2_state[0].mole_frac_phase_comp["Vap", "benzene"], 1e5
)
iscale.set_scaling_factor(
m.fs.M01.inlet_1_state[0].eq_phase_equilibrium["toluene"], 1
m.fs.M01.inlet_2_state[0].mole_frac_phase_comp["Liq", "toluene"], 1
)
iscale.set_scaling_factor(
m.fs.M01.inlet_2_state[0].mole_frac_phase_comp["Vap", "toluene"], 1
)
iscale.set_scaling_factor(m.fs.M01.inlet_1_state[0].eq_P_vap["benzene"], 1)
iscale.set_scaling_factor(m.fs.M01.inlet_1_state[0].eq_P_vap["toluene"], 1)

iscale.set_scaling_factor(m.fs.M01.inlet_2_state[0].mole_frac_comp["benzene"], 1e4)
iscale.set_scaling_factor(m.fs.M01.inlet_2_state[0].mole_frac_comp["toluene"], 1)
iscale.set_scaling_factor(
m.fs.M01.inlet_2_state[0].enth_mol_phase_comp["Liq", "benzene"], 1e-4
m.fs.M01.inlet_2_state[0].pressure_sat_comp["benzene"], 1e-6
)
iscale.set_scaling_factor(
m.fs.M01.inlet_2_state[0].enth_mol_phase_comp["Vap", "benzene"], 1e-4
m.fs.M01.inlet_2_state[0].pressure_sat_comp["toluene"], 1e-6
)

iscale.set_scaling_factor(
m.fs.M01.inlet_2_state[0].enth_mol_phase_comp["Liq", "toluene"], 1e-4
m.fs.M01.inlet_2_state[0].enth_mol_phase_comp["Liq", "benzene"], 1e-4
)
iscale.set_scaling_factor(
m.fs.M01.inlet_2_state[0].enth_mol_phase_comp["Vap", "toluene"], 1e-4
m.fs.M01.inlet_2_state[0].enth_mol_phase_comp["Vap", "benzene"], 1e-4
)
iscale.set_scaling_factor(m.fs.M01.inlet_2_state[0].eq_total, 1)
iscale.set_scaling_factor(m.fs.M01.inlet_2_state[0].eq_sum_mol_frac, 1)
iscale.set_scaling_factor(m.fs.M01.inlet_2_state[0].eq_comp["benzene"], 1)
iscale.set_scaling_factor(m.fs.M01.inlet_2_state[0].eq_comp["toluene"], 1)
iscale.set_scaling_factor(
m.fs.M01.inlet_2_state[0].eq_phase_equilibrium["benzene"], 1
m.fs.M01.inlet_2_state[0].enth_mol_phase_comp["Liq", "toluene"], 1e-4
)
iscale.set_scaling_factor(
m.fs.M01.inlet_2_state[0].eq_phase_equilibrium["toluene"], 1
m.fs.M01.inlet_2_state[0].enth_mol_phase_comp["Vap", "toluene"], 1e-4
)
iscale.set_scaling_factor(m.fs.M01.inlet_2_state[0].eq_P_vap["benzene"], 1)
iscale.set_scaling_factor(m.fs.M01.inlet_2_state[0].eq_P_vap["toluene"], 1)

iscale.set_scaling_factor(
m.fs.M01.mixed_state[0].enth_mol_phase_comp["Liq", "benzene"], 1e-4
Expand All @@ -130,6 +139,7 @@ def set_scaling(m):
iscale.set_scaling_factor(
m.fs.M01.mixed_state[0].enth_mol_phase_comp["Vap", "toluene"], 1e-4
)
iscale.set_scaling_factor(m.fs.M01.mixed_state[0].enthalpy_flow_terms["Liq"], 1)
iscale.set_scaling_factor(m.fs.M01.mixed_state[0].enthalpy_flow_terms["Vap"], 1)
iscale.set_scaling_factor(m.fs.M01.mixed_state[0].eq_total, 1)
iscale.set_scaling_factor(m.fs.M01.mixed_state[0].eq_sum_mol_frac, 1)
Expand All @@ -146,24 +156,35 @@ def set_scaling(m):
iscale.set_scaling_factor(m.fs.M01.mixed_state[0].eq_P_vap["toluene"], 1)

iscale.set_scaling_factor(m.fs.M01.mixed_state[0].pressure, 1e-5)
iscale.set_scaling_factor(m.fs.M01.mixed_state[0].pressure_sat_comp, 1e-5)
iscale.set_scaling_factor(m.fs.M01.mixed_state[0].temperature, 1e-2)
iscale.set_scaling_factor(m.fs.M01.mixed_state[0].flow_mol, 1)
iscale.set_scaling_factor(m.fs.M01.mixed_state[0].flow_mol_phase, 1)
iscale.set_scaling_factor(m.fs.M01.mixed_state[0].mole_frac_comp["benzene"], 1e1)
iscale.set_scaling_factor(m.fs.M01.mixed_state[0].mole_frac_comp["toluene"], 1e1)

iscale.set_scaling_factor(m.fs.H02.control_volume.heat, 1e-5)
iscale.set_scaling_factor(m.fs.H02.control_volume.properties_in[0].pressure, 1e-5)
iscale.set_scaling_factor(
m.fs.H02.control_volume.properties_in[0].pressure_sat_comp, 1e-5
)
iscale.set_scaling_factor(
m.fs.H02.control_volume.properties_in[0].temperature, 1e-2
)
iscale.set_scaling_factor(m.fs.H02.control_volume.properties_in[0].flow_mol, 1)
iscale.set_scaling_factor(
m.fs.H02.control_volume.properties_in[0].flow_mol_phase, 1
)
iscale.set_scaling_factor(
m.fs.H02.control_volume.properties_in[0].mole_frac_comp["benzene"], 1e1
)
iscale.set_scaling_factor(
m.fs.H02.control_volume.properties_in[0].mole_frac_comp["toluene"], 1e1
)
iscale.set_scaling_factor(m.fs.H02.control_volume.properties_out[0].flow_mol, 1)
iscale.set_scaling_factor(
m.fs.H02.control_volume.properties_out[0].flow_mol_phase, 1
)
iscale.set_scaling_factor(
m.fs.H02.control_volume.properties_out[0].mole_frac_comp["benzene"], 1e1
)
Expand All @@ -174,6 +195,9 @@ def set_scaling(m):
m.fs.H02.control_volume.properties_out[0].temperature, 1e-2
)
iscale.set_scaling_factor(m.fs.H02.control_volume.properties_out[0].pressure, 1e-5)
iscale.set_scaling_factor(
m.fs.H02.control_volume.properties_out[0].pressure_sat_comp, 1e-5
)
iscale.set_scaling_factor(
m.fs.H02.control_volume.properties_in[0].enth_mol_phase_comp["Liq", "benzene"],
1e-4,
Expand Down Expand Up @@ -287,10 +311,24 @@ def set_scaling(m):

iscale.set_scaling_factor(m.fs.F03.control_volume.heat, 1)
iscale.set_scaling_factor(m.fs.F03.control_volume.properties_in[0].pressure, 1e-5)
iscale.set_scaling_factor(
m.fs.F03.control_volume.properties_in[0].pressure_sat_comp, 1e-5
)
iscale.set_scaling_factor(m.fs.F03.control_volume.properties_out[0].pressure, 1e-5)
iscale.set_scaling_factor(
m.fs.F03.control_volume.properties_out[0].pressure_sat_comp, 1e-5
)
iscale.set_scaling_factor(
m.fs.F03.control_volume.properties_in[0].temperature, 1e-2
)
iscale.set_scaling_factor(m.fs.F03.control_volume.properties_in[0].flow_mol, 1)
iscale.set_scaling_factor(
m.fs.F03.control_volume.properties_in[0].flow_mol_phase, 1
)
iscale.set_scaling_factor(m.fs.F03.control_volume.properties_out[0].flow_mol, 1)
iscale.set_scaling_factor(
m.fs.F03.control_volume.properties_out[0].flow_mol_phase, 1
)
iscale.set_scaling_factor(
m.fs.F03.control_volume.properties_in[0].mole_frac_comp["benzene"], 1e1
)
Expand Down Expand Up @@ -415,22 +453,23 @@ def set_scaling(m):

def set_dof(m):
"""Set degrees of freedom for demo flowsheet"""
eps = 1e-5
m.fs.M01.inlet_1.flow_mol.fix(1.0)
m.fs.M01.inlet_1.mole_frac_comp[:, "benzene"].fix(1)
m.fs.M01.inlet_1.mole_frac_comp[:, "toluene"].fix(1e-5)
m.fs.M01.inlet_1.mole_frac_comp[:, "benzene"].fix(1 - eps)
m.fs.M01.inlet_1.mole_frac_comp[:, "toluene"].fix(eps)
m.fs.M01.inlet_1.pressure.fix(101325)
m.fs.M01.inlet_1.temperature.fix(370)

m.fs.M01.inlet_2.flow_mol.fix(1.0)
m.fs.M01.inlet_2.mole_frac_comp[:, "benzene"].fix(1e-5)
m.fs.M01.inlet_2.mole_frac_comp[:, "toluene"].fix(1)
m.fs.M01.inlet_2.mole_frac_comp[:, "benzene"].fix(eps)
m.fs.M01.inlet_2.mole_frac_comp[:, "toluene"].fix(1 - eps)
m.fs.M01.inlet_2.pressure.fix(1.3e5)
m.fs.M01.inlet_2.temperature.fix(380)

m.fs.H02.outlet.temperature[0].fix(370)

m.fs.F03.heat_duty.fix(0)
m.fs.F03.deltaP.fix(0)
m.fs.F03.heat_duty.fix(1e-6)
m.fs.F03.deltaP.fix(1e-6)


def initialize_flowsheet(m):
Expand Down
8 changes: 7 additions & 1 deletion idaes/models/flowsheets/tests/test_demo_flowsheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from idaes.models.flowsheets.demo_flowsheet import (
build_flowsheet,
set_dof,
set_scaling,
initialize_flowsheet,
solve_flowsheet,
)
Expand Down Expand Up @@ -59,6 +60,11 @@ def test_build_flowsheet(model):
assert degrees_of_freedom(model) == 13


@pytest.mark.unit
def test_set_scaling(model):
set_scaling(model)


@pytest.mark.unit
def test_set_dof(model):
set_dof(model)
Expand Down Expand Up @@ -95,7 +101,7 @@ def test_solve_flowsheet(model):
0.5, 1e-4
)
assert model.fs.M01.outlet.pressure[0].value == pytest.approx(101325, 1e-4)
assert model.fs.M01.outlet.temperature[0].value == pytest.approx(368.2, 1e-4)
assert model.fs.M01.outlet.temperature[0].value == pytest.approx(368.12, 1e-4)

assert model.fs.H02.outlet.flow_mol[0].value == pytest.approx(2.0, 1e-4)
assert model.fs.H02.outlet.mole_frac_comp[0, "benzene"].value == pytest.approx(
Expand Down

0 comments on commit 494b086

Please sign in to comment.