IDAES / idaes-pse

The IDAES Process Systems Engineering Framework
https://idaes-pse.readthedocs.io/
Other
206 stars 228 forks source link

Initialization failure using default_initializer #1412

Closed JavalVyas2000 closed 1 month ago

JavalVyas2000 commented 1 month ago

I am trying to initialize the unit models in HDA flowsheet using default_initializer, this method does fails to initialize most of the unit models in the actual flowsheet. I tried to run a part of the flowsheet and that too resulted in failure. Below is the code that I am running and the error that I got with the same.

from pyomo.environ import (
    Constraint,
    Var,
    ConcreteModel,
    Expression,
    Objective,
    TransformationFactory,
    value,
)

import pyomo.environ as pyo

# Todo: Import the above mentioned tools from pyomo.network
from pyomo.network import Arc, SequentialDecomposition
from idaes.core import FlowsheetBlock

from idaes.models.unit_models import (
    PressureChanger,
    Mixer,
    Separator as Splitter,
    Heater,
    CSTR,
    Flash,
    Translator,
)

from idaes.models_extra.column_models import TrayColumn
from idaes.models_extra.column_models.condenser import CondenserType, TemperatureSpec
# Utility tools to put together the flowsheet and calculate the degrees of freedom
from idaes.models.unit_models.pressure_changer import ThermodynamicAssumption
from idaes.core.util.model_statistics import degrees_of_freedom
from idaes.core.util.initialization import propagate_state
from idaes.core.solvers import get_solver
import idaes.core.util.scaling as iscale

# Import idaes logger to set output levels
import idaes.logger as idaeslog

from idaes_examples.mod.hda import hda_reaction as reaction_props
from idaes.models.properties.activity_coeff_models.BTX_activity_coeff_VLE import (
    BTXParameterBlock,
)

from idaes_examples.mod.hda.hda_ideal_VLE import HDAParameterBlock

def main():
    # Create a Pyomo Concrete Model to contain the problem
    m = ConcreteModel()

    # Add a steady state flowsheet block to the model
    m.fs = FlowsheetBlock(dynamic=False)
    # Property package for benzene, toluene, hydrogen, methane mixture
    m.fs.BTHM_params = HDAParameterBlock()

    # Property package for the benzene-toluene mixture
    m.fs.BT_params = BTXParameterBlock(
        valid_phase=("Liq", "Vap"), activity_coeff_model="Ideal"
    )

    # Reaction package for the HDA reaction
    m.fs.reaction_params = reaction_props.HDAReactionParameterBlock(
        property_package=m.fs.BTHM_params
    )
    # Adding the mixer M101 to the flowsheet
    m.fs.M101 = Mixer(
        property_package=m.fs.BTHM_params,
        inlet_list=["toluene_feed", "hydrogen_feed", "vapor_recycle"],
    )

    # Adding the heater H101 to the flowsheet
    m.fs.H101 = Heater(property_package=m.fs.BTHM_params, has_phase_equilibrium=True)

    m.fs.s03 = Arc(source=m.fs.M101.outlet, destination=m.fs.H101.inlet)

    TransformationFactory("network.expand_arcs").apply_to(m)

    m.fs.M101.toluene_feed.flow_mol_phase_comp[0, "Vap", "benzene"].fix(1e-5)
    m.fs.M101.toluene_feed.flow_mol_phase_comp[0, "Vap", "toluene"].fix(1e-5)
    m.fs.M101.toluene_feed.flow_mol_phase_comp[0, "Vap", "hydrogen"].fix(1e-5)
    m.fs.M101.toluene_feed.flow_mol_phase_comp[0, "Vap", "methane"].fix(1e-5)
    m.fs.M101.toluene_feed.flow_mol_phase_comp[0, "Liq", "benzene"].fix(1e-5)
    m.fs.M101.toluene_feed.flow_mol_phase_comp[0, "Liq", "toluene"].fix(0.30)
    m.fs.M101.toluene_feed.flow_mol_phase_comp[0, "Liq", "hydrogen"].fix(1e-5)
    m.fs.M101.toluene_feed.flow_mol_phase_comp[0, "Liq", "methane"].fix(1e-5)
    m.fs.M101.toluene_feed.temperature.fix(303.2)
    m.fs.M101.toluene_feed.pressure.fix(350000)

    m.fs.M101.hydrogen_feed.flow_mol_phase_comp[0, "Vap", "benzene"].fix(1e-5)
    m.fs.M101.hydrogen_feed.flow_mol_phase_comp[0, "Vap", "toluene"].fix(1e-5)
    m.fs.M101.hydrogen_feed.flow_mol_phase_comp[0, "Vap", "hydrogen"].fix(0.30)
    m.fs.M101.hydrogen_feed.flow_mol_phase_comp[0, "Vap", "methane"].fix(0.02)
    m.fs.M101.hydrogen_feed.flow_mol_phase_comp[0, "Liq", "benzene"].fix(1e-5)
    m.fs.M101.hydrogen_feed.flow_mol_phase_comp[0, "Liq", "toluene"].fix(1e-5)
    m.fs.M101.hydrogen_feed.flow_mol_phase_comp[0, "Liq", "hydrogen"].fix(1e-5)
    m.fs.M101.hydrogen_feed.flow_mol_phase_comp[0, "Liq", "methane"].fix(1e-5)
    m.fs.M101.hydrogen_feed.temperature.fix(303.2)
    m.fs.M101.hydrogen_feed.pressure.fix(350000)

    # Fix the temperature of the outlet from the heater H101
    m.fs.H101.outlet.temperature.fix(600)

    iscale.set_scaling_factor(m.fs.H101.control_volume.heat, 1e-2)

    m.scaling_factor = pyo.Suffix(direction=pyo.Suffix.EXPORT)

    def function(unit):
        print(unit)
        print(unit.default_initializer())
        initializer = unit.default_initializer()
        initializer.initialize(unit, output_level=idaeslog.DEBUG)

    units=[m.fs.M101,m.fs.H101]
    for i in units:
        function(i)

if __name__=='__main__':
    main()

The error I got is as follows:

Traceback (most recent call last):
  File "C:\Users\javal\Desktop\Internship\IDAES-examples\idaes_examples\notebooks\docs\flowsheets\trial.py", line 117, in <module>
    main()
  File "C:\Users\javal\Desktop\Internship\IDAES-examples\idaes_examples\notebooks\docs\flowsheets\trial.py", line 114, in main
    function(i)
  File "C:\Users\javal\Desktop\Internship\IDAES-examples\idaes_examples\notebooks\docs\flowsheets\trial.py", line 110, in function
    initializer.initialize(unit, output_level=idaeslog.DEBUG)
  File "c:\users\javal\desktop\internship\idaes-pse\idaes\core\initialization\initializer_base.py", line 192, in initialize
    results = self.initialization_routine(model)
  File "c:\users\javal\desktop\internship\idaes-pse\idaes\models\unit_models\mixer.py", line 125, in initialization_routine
    iinit.initialize(i_block)
  File "c:\users\javal\desktop\internship\idaes-pse\idaes\core\initialization\initializer_base.py", line 182, in initialize
    self.fix_initialization_states(model)
  File "c:\users\javal\desktop\internship\idaes-pse\idaes\core\initialization\initializer_base.py", line 279, in fix_initialization_states
    model.fix_initialization_states()
  File "c:\users\javal\desktop\internship\idaes-pse\idaes\core\base\property_base.py", line 314, in fix_initialization_states
    raise NotImplementedError
NotImplementedError
andrewlee94 commented 1 month ago

@JavalVyas2000 Looking throguh the traceback, that error is actually originating the the HDA property block. Check your property package has the fix_initialization_states method.

JavalVyas2000 commented 1 month ago

Thank you @andrewlee94