def test_build_global_params(self, model): assert model.frame.base_currency == pyunits.MUSD_2018 assert model.frame.base_period == pyunits.year assert len(model.frame.defined_flows) == 14 for f in model.frame.defined_flows: assert f in ["electricity", "alum", "ammonia", "anti-scalant", "caustic_soda", "chlorine", "ferric_chloride", "hydrochloric_acid", "hydrogen_peroxide", "ion_exchange_resin", "lime", "sodium_bisulfite", "sodium_chloride", "sulfuric_acid"] assert number_unfixed_variables(model.frame) == 0 assert isinstance(model.frame.plant_lifetime, Var) assert value(model.frame.plant_lifetime) == 30 assert isinstance(model.frame.utilization_factor, Var) assert value(model.frame.utilization_factor) == 1 assert isinstance(model.frame.land_cost_percent_FCI, Var) assert value(model.frame.land_cost_percent_FCI) == 0.0015 assert isinstance(model.frame.working_capital_percent_FCI, Var) assert value(model.frame.working_capital_percent_FCI) == 0.05 assert isinstance(model.frame.salaries_percent_FCI, Var) assert value(model.frame.salaries_percent_FCI) == 0.001 assert isinstance(model.frame.benefit_percent_of_salary, Var) assert value(model.frame.benefit_percent_of_salary) == 0.9 assert isinstance(model.frame.maintenance_costs_percent_FCI, Var) assert value(model.frame.maintenance_costs_percent_FCI) == 0.008 assert isinstance(model.frame.laboratory_fees_percent_FCI, Var) assert value(model.frame.laboratory_fees_percent_FCI) == 0.003 assert isinstance(model.frame.insurance_and_taxes_percent_FCI, Var) assert value(model.frame.insurance_and_taxes_percent_FCI) == 0.002 assert isinstance(model.frame.wacc, Var) assert value(model.frame.wacc) == 0.05 assert isinstance(model.frame.capital_recovery_factor, Expression) assert value(model.frame.capital_recovery_factor) == pytest.approx( 0.0650514, rel=1e-5) assert isinstance(model.frame.TPEC, Var) assert value(model.frame.TPEC) == 3.4 assert isinstance(model.frame.TIC, Var) assert value(model.frame.TIC) == 1.65
def initialize(blk, flow_mol_phase_comp=None, temperature=None, pressure=None, state_vars_fixed=False, hold_state=False, outlvl=1, solver='ipopt', optarg={'tol': 1e-8}): """ Initialisation routine for property package. Keyword Arguments: flow_mol_phase_comp : value at which to initialize phase-component flows (default=None) pressure : value at which to initialize pressure (default=None) temperature : value at which to initialize temperature (default=None) outlvl : sets output level of initialisation routine * 0 = no output (default) * 1 = return solver state for each step in routine * 2 = include solver output infomation (tee=True) optarg : solver options dictionary object (default=None) state_vars_fixed: Flag to denote if state vars have already been fixed. - True - states have already been fixed by the control volume 1D. Control volume 0D does not fix the state vars, so will be False if this state block is used with 0D blocks. - False - states have not been fixed. The state block will deal with fixing/unfixing. solver : str indicating whcih solver to use during initialization (default = 'ipopt') hold_state : flag indicating whether the initialization routine should unfix any state variables fixed during initialization (default=False). - True - states varaibles are not unfixed, and a dict of returned containing flags for which states were fixed during initialization. - False - state variables are unfixed after initialization by calling the relase_state method Returns: If hold_states is True, returns a dict containing flags for which states were fixed during initialization. """ _log.info('Starting {} initialisation'.format(blk.name)) # Fix state variables if not already fixed if state_vars_fixed is False: Fflag = {} Pflag = {} Tflag = {} for k in blk.keys(): for p in blk[k]._params.phase_list: for j in blk[k]._params.component_list: if blk[k].flow_mol_phase_comp[p, j].fixed is True: Fflag[k, p, j] = True else: Fflag[k, p, j] = False if flow_mol_phase_comp is None: blk[k].flow_mol_phase_comp[p, j].fix( 1 / len(blk[k]._params.component_list)) else: blk[k].flow_mol_phase_comp[p, j].fix( flow_mol_phase_comp[p, j]) if blk[k].pressure.fixed is True: Pflag[k] = True else: Pflag[k] = False if pressure is None: blk[k].pressure.fix(101325.0) else: blk[k].pressure.fix(pressure) if blk[k].temperature.fixed is True: Tflag[k] = True else: Tflag[k] = False if temperature is None: blk[k].temperature.fix(325) else: blk[k].temperature.fix(temperature) # ----------------------------------------------------------------- # If input block, return flags, else release state flags = {"Fflag": Fflag, "Pflag": Pflag, "Tflag": Tflag} else: # Check when the state vars are fixed already result in dof 0 for k in blk.keys(): if degrees_of_freedom(blk[k]) != 0: raise Exception("State vars fixed but degrees of freedom " "for state block is not zero during " "initialization.") # Set solver options if outlvl > 1: stee = True else: stee = False if optarg is None: sopt = {'tol': 1e-8} else: sopt = optarg opt = SolverFactory('ipopt') opt.options = sopt # --------------------------------------------------------------------- # If present, initialize bubble and dew point calculations for k in blk.keys(): if hasattr(blk[k], "eq_temperature_dew"): calculate_variable_from_constraint(blk[k].temperature_dew, blk[k].eq_temperature_dew) if hasattr(blk[k], "eq_pressure_dew"): calculate_variable_from_constraint(blk[k].pressure_dew, blk[k].eq_pressure_dew) if outlvl > 0: _log.info("Dew and bubble points initialization for " "{} completed".format(blk.name)) # --------------------------------------------------------------------- # If flash, initialize T1 and Teq for k in blk.keys(): if (blk[k].config.has_phase_equilibrium and not blk[k].config.defined_state): blk[k]._t1.value = max(blk[k].temperature.value, blk[k].temperature_bubble.value) blk[k]._teq.value = min(blk[k]._t1.value, blk[k].temperature_dew.value) if outlvl > 0: _log.info("Equilibrium temperature initialization for " "{} completed".format(blk.name)) # --------------------------------------------------------------------- # Initialize flow rates and compositions # TODO : This will need ot be generalised more when we move to a # modular implementation for k in blk.keys(): # Deactivate equilibrium constraints, as state is fixed if hasattr(blk[k], 'equilibrium_constraint'): blk[k].equilibrium_constraint.deactivate() free_vars = 0 for k in blk.keys(): free_vars += number_unfixed_variables(blk[k]) if free_vars > 0: try: results = solve_indexed_blocks(opt, [blk], tee=stee) except: results = None else: results = None for k in blk.keys(): # Reactivate equilibrium constraints if hasattr(blk[k], 'equilibrium_constraint'): blk[k].equilibrium_constraint.activate() if outlvl > 0: if results is None or results.solver.termination_condition \ == TerminationCondition.optimal: _log.info("Property initialization for " "{} completed".format(blk.name)) else: _log.warning("Property initialization for " "{} failed".format(blk.name)) # --------------------------------------------------------------------- # Return state to initial conditions if state_vars_fixed is False: if hold_state is True: return flags else: blk.release_state(flags) if outlvl > 0: _log.info("Initialisation completed for {}".format(blk.name))
def initialize( self, state_args=None, state_vars_fixed=False, hold_state=False, outlvl=idaeslog.NOTSET, solver=None, optarg=None, ): """ Initialization routine for property package. Keyword Arguments: state_args : Dictionary with initial guesses for the state vars chosen. Note that if this method is triggered through the control volume, and if initial guesses were not provided at the unit model level, the control volume passes the inlet values as initial guess.The keys for the state_args dictionary are: flow_mass_phase_comp : value at which to initialize phase component flows pressure : value at which to initialize pressure temperature : value at which to initialize temperature outlvl : sets output level of initialization routine (default=idaeslog.NOTSET) optarg : solver options dictionary object (default=None) state_vars_fixed: Flag to denote if state vars have already been fixed. - True - states have already been fixed by the control volume 1D. Control volume 0D does not fix the state vars, so will be False if this state block is used with 0D blocks. - False - states have not been fixed. The state block will deal with fixing/unfixing. solver : Solver object to use during initialization if None is provided it will use the default solver for IDAES (default = None) hold_state : flag indicating whether the initialization routine should unfix any state variables fixed during initialization (default=False). - True - states variables are not unfixed, and a dict of returned containing flags for which states were fixed during initialization. - False - state variables are unfixed after initialization by calling the release_state method Returns: If hold_states is True, returns a dict containing flags for which states were fixed during initialization. """ # Get loggers init_log = idaeslog.getInitLogger(self.name, outlvl, tag="properties") solve_log = idaeslog.getSolveLogger(self.name, outlvl, tag="properties") # Set solver and options opt = get_solver(solver, optarg) # Fix state variables flags = fix_state_vars(self, state_args) # Check when the state vars are fixed already result in dof 0 for k in self.keys(): dof = degrees_of_freedom(self[k]) if dof != 0: raise PropertyPackageError( "\nWhile initializing {sb_name}, the degrees of freedom " "are {dof}, when zero is required. \nInitialization assumes " "that the state variables should be fixed and that no other " "variables are fixed. \nIf other properties have a " "predetermined value, use the calculate_state method " "before using initialize to determine the values for " "the state variables and avoid fixing the property variables." "".format(sb_name=self.name, dof=dof)) # --------------------------------------------------------------------- skip_solve = True # skip solve if only state variables are present for k in self.keys(): if number_unfixed_variables(self[k]) != 0: skip_solve = False if not skip_solve: # Initialize properties with idaeslog.solver_log(solve_log, idaeslog.DEBUG) as slc: results = solve_indexed_blocks(opt, [self], tee=slc.tee) init_log.info_high("Property initialization: {}.".format( idaeslog.condition(results))) # --------------------------------------------------------------------- # If input block, return flags, else release state if state_vars_fixed is False: if hold_state is True: return flags else: self.release_state(flags)
def initialize(blk, state_args=None, state_vars_fixed=False, hold_state=False, outlvl=idaeslog.NOTSET, solver=None, optarg=None): """ Initialization routine for property package. Keyword Arguments: state_args : Dictionary with initial guesses for the state vars chosen. Note that if this method is triggered through the control volume, and if initial guesses were not provided at the unit model level, the control volume passes the inlet values as initial guess.Keys for the state_args dictionary are: flow_mol, temperature, pressure and mole_frac_comp. outlvl : sets output level of initialization routine optarg : solver options dictionary object (default=None, use default solver options) solver : str indicating which solver to use during initialization (default = None) hold_state : flag indicating whether the initialization routine should unfix any state variables fixed during initialization (default=False). valid options: True : states varaibles are not unfixed, and a dict of returned containing flags for which states were fixed during initialization. False : state variables are unfixed after initialization by calling the relase_state method Returns: If hold_states is True, returns a dict containing flags for which states were fixed during initialization. """ init_log = idaeslog.getInitLogger(blk.name, outlvl, tag="properties") solve_log = idaeslog.getSolveLogger(blk.name, outlvl, tag="properties") init_log.info('Starting Vapor phase properties initialization') # Deactivate the constraints specific for non-inlet blocks i.e. # when defined state is False for k in blk.keys(): if blk[k].config.defined_state is False: blk[k].sum_component_eqn.deactivate() # Fix state variables if not already fixed if state_vars_fixed is False: flags = fix_state_vars(blk, state_args) for k in blk.keys(): dof = degrees_of_freedom(blk[k]) if dof != 0: raise RuntimeError( "{} - degrees of freedom for state block is not zero " "during initialization. DoF = {}".format(blk.name, dof)) # Create solver opt = get_solver(solver, optarg) # --------------------------------------------------------------------- # Initialise values for k in blk.keys(): for j in blk[k].component_list: if hasattr(blk[k], "cp_mol_comp_eqn"): calculate_variable_from_constraint(blk[k].cp_mol_comp[j], blk[k].cp_mol_comp_eqn[j]) if hasattr(blk[k], "flow_mol_comp_eqn"): calculate_variable_from_constraint(blk[k].flow_mol_comp[j], blk[k].flow_mol_comp_eqn[j]) if hasattr(blk[k], "cp_mol_comp_mean_eqn"): calculate_variable_from_constraint(blk[k].cp_mol_comp_mean[j], blk[k].cp_mol_comp_mean_eqn[j]) if hasattr(blk[k], "cp_mol_eqn"): calculate_variable_from_constraint(blk[k].cp_mol, blk[k].cp_mol_eqn) if hasattr(blk[k], "cp_mol_mean_eqn"): calculate_variable_from_constraint(blk[k].cp_mol_mean, blk[k].cp_mol_mean_eqn) if hasattr(blk[k], "enth_mean_eqn"): calculate_variable_from_constraint(blk[k].enth_mean, blk[k].enth_mean_eqn) # Solve property block if non-empty free_vars = 0 for k in blk.keys(): free_vars += number_unfixed_variables(blk[k]) if free_vars > 0: with idaeslog.solver_log(solve_log, idaeslog.DEBUG) as slc: res = solve_indexed_blocks(opt, [blk], tee=slc.tee) else: res = "" init_log.info("Vapor properties initialization complete {}.".format( idaeslog.condition(res))) # ---------------------------------------------------------------------- if state_vars_fixed is False: if hold_state is True: return flags else: blk.release_state(flags)
def initialize(blk, state_args={}, state_vars_fixed=False, hold_state=False, outlvl=idaeslog.NOTSET, solver='ipopt', optarg={'tol': 1e-8}): """ Initialization routine for property package. Keyword Arguments: state_args : Dictionary with initial guesses for the state vars chosen. Note that if this method is triggered through the control volume, and if initial guesses were not provied at the unit model level, the control volume passes the inlet values as initial guess.The keys for the state_args dictionary are: flow_mol_phase_comp : value at which to initialize phase component flows pressure : value at which to initialize pressure temperature : value at which to initialize temperature outlvl : sets output level of initialization routine * 0 = no output (default) * 1 = return solver state for each step in routine * 2 = include solver output infomation (tee=True) optarg : solver options dictionary object (default=None) state_vars_fixed: Flag to denote if state vars have already been fixed. - True - states have already been fixed by the control volume 1D. Control volume 0D does not fix the state vars, so will be False if this state block is used with 0D blocks. - False - states have not been fixed. The state block will deal with fixing/unfixing. solver : str indicating whcih solver to use during initialization (default = 'ipopt') hold_state : flag indicating whether the initialization routine should unfix any state variables fixed during initialization (default=False). - True - states varaibles are not unfixed, and a dict of returned containing flags for which states were fixed during initialization. - False - state variables are unfixed after initialization by calling the relase_state method Returns: If hold_states is True, returns a dict containing flags for which states were fixed during initialization. """ init_log = idaeslog.getInitLogger(blk.name, outlvl, tag="properties") solve_log = idaeslog.getSolveLogger(blk.name, outlvl, tag="properties") # Fix state variables if not already fixed if state_vars_fixed is False: flags = fix_state_vars(blk, state_args) else: # Check when the state vars are fixed already result in dof 0 for k in blk.keys(): if degrees_of_freedom(blk[k]) != 0: raise Exception("State vars fixed but degrees of freedom " "for state block is not zero during " "initialization.") # Set solver options if optarg is None: sopt = {"tol": 1e-8} else: sopt = optarg opt = SolverFactory('ipopt') opt.options = sopt # --------------------------------------------------------------------- # If present, initialize bubble and dew point calculations for k in blk.keys(): if hasattr(blk[k], "eq_temperature_dew"): calculate_variable_from_constraint(blk[k].temperature_dew, blk[k].eq_temperature_dew) if hasattr(blk[k], "eq_pressure_dew"): calculate_variable_from_constraint(blk[k].pressure_dew, blk[k].eq_pressure_dew) init_log.info_high("Initialization Step 1 - Dew and bubble points " "calculation completed.") # --------------------------------------------------------------------- # If flash, initialize T1 and Teq for k in blk.keys(): if (blk[k].config.has_phase_equilibrium and not blk[k].config.defined_state): blk[k]._t1.value = max(blk[k].temperature.value, blk[k].temperature_bubble.value) blk[k]._teq.value = min(blk[k]._t1.value, blk[k].temperature_dew.value) init_log.info_high("Initialization Step 2 - Equilibrium temperature " " calculation completed.") # --------------------------------------------------------------------- # Initialize flow rates and compositions # TODO : This will need to be generalised more when we move to a # modular implementation for k in blk.keys(): # Deactivate equilibrium constraints, as state is fixed if hasattr(blk[k], 'equilibrium_constraint'): blk[k].equilibrium_constraint.deactivate() free_vars = 0 for k in blk.keys(): free_vars += number_unfixed_variables(blk[k]) if free_vars > 0: try: with idaeslog.solver_log(solve_log, idaeslog.DEBUG) as slc: res = solve_indexed_blocks(opt, [blk], tee=slc.tee) except: res = None else: res = None for k in blk.keys(): # Reactivate equilibrium constraints if hasattr(blk[k], 'equilibrium_constraint'): blk[k].equilibrium_constraint.activate() # --------------------------------------------------------------------- # Return state to initial conditions if state_vars_fixed is False: if hold_state is True: return flags else: blk.release_state(flags) init_log.info("Initialization Complete")
def initialize(blk, state_args={}, state_vars_fixed=False, hold_state=False, outlvl=1, solver='ipopt', optarg={'tol': 1e-8}): """ Initialization routine for property package. Keyword Arguments: state_args : Dictionary with initial guesses for the state vars chosen. Note that if this method is triggered through the control volume, and if initial guesses were not provied at the unit model level, the control volume passes the inlet values as initial guess.The keys for the state_args dictionary are: flow_mol_phase_comp : value at which to initialize phase component flows pressure : value at which to initialize pressure temperature : value at which to initialize temperature outlvl : sets output level of initialization routine * 0 = no output (default) * 1 = return solver state for each step in routine * 2 = include solver output infomation (tee=True) optarg : solver options dictionary object (default=None) state_vars_fixed: Flag to denote if state vars have already been fixed. - True - states have already been fixed by the control volume 1D. Control volume 0D does not fix the state vars, so will be False if this state block is used with 0D blocks. - False - states have not been fixed. The state block will deal with fixing/unfixing. solver : str indicating whcih solver to use during initialization (default = 'ipopt') hold_state : flag indicating whether the initialization routine should unfix any state variables fixed during initialization (default=False). - True - states varaibles are not unfixed, and a dict of returned containing flags for which states were fixed during initialization. - False - state variables are unfixed after initialization by calling the relase_state method Returns: If hold_states is True, returns a dict containing flags for which states were fixed during initialization. """ _log.info('Starting {} initialization'.format(blk.name)) # Fix state variables if not already fixed if state_vars_fixed is False: flags = fix_state_vars(blk, state_args) else: # Check when the state vars are fixed already result in dof 0 for k in blk.keys(): if degrees_of_freedom(blk[k]) != 0: raise Exception("State vars fixed but degrees of freedom " "for state block is not zero during " "initialization.") # Set solver options if outlvl > 1: stee = True else: stee = False if optarg is None: sopt = {'tol': 1e-8} else: sopt = optarg opt = SolverFactory('ipopt') opt.options = sopt # --------------------------------------------------------------------- # Initialize flow rates and compositions free_vars = 0 for k in blk.keys(): free_vars += number_unfixed_variables(blk[k]) if free_vars > 0: try: results = solve_indexed_blocks(opt, [blk], tee=stee) except: results = None else: results = None if outlvl > 0: if results is None or results.solver.termination_condition \ == TerminationCondition.optimal: _log.info("Property initialization for " "{} completed".format(blk.name)) else: _log.warning("Property initialization for " "{} failed".format(blk.name)) # --------------------------------------------------------------------- # Return state to initial conditions if state_vars_fixed is False: if hold_state is True: return flags else: blk.release_state(flags) if outlvl > 0: _log.info("Initialization completed for {}".format(blk.name))
def initialize(blk, state_args={}, state_vars_fixed=False, hold_state=False, outlvl=idaeslog.NOTSET, solver=None, optarg=None): """ Initialization routine for property package. Keyword Arguments: state_args : Dictionary with initial guesses for the state vars chosen. The keys for the state_args dictionary are: flow_mol : value at which to initialize flow rate mole_frac_comp : dict of values to use when initializing mole fractions pressure : value at which to initialize pressure temperature : value at which to initialize temperature outlvl : sets logger output level for initialization routine optarg : solver options dictionary object (default=None) state_vars_fixed: Flag to denote if state vars have already been fixed. - True - states have already been fixed by the control volume 1D. Control volume 0D does not fix the state vars, so will be False if this state block is used with 0D blocks. - False - states have not been fixed. The state block will deal with fixing/unfixing. solver : str indicating whcih solver to use during initialization (default = None, use default solver) hold_state : flag indicating whether the initialization routine should unfix any state variables fixed during initialization (default=False). - True - states varaibles are not unfixed, and a dict of returned containing flags for which states were fixed during initialization. - False - state variables are unfixed after initialization by calling the relase_state method Returns: If hold_states is True, returns a dict containing flags for which states were fixed during initialization. """ init_log = idaeslog.getInitLogger(blk.name, outlvl, tag="properties") solve_log = idaeslog.getSolveLogger(blk.name, outlvl, tag="properties") # Fix state variables if not already fixed if state_vars_fixed is False: flags = fix_state_vars(blk, state_args) else: pass # Deactivate sum of mole fractions constraint for k in blk.keys(): if blk[k].config.defined_state is False: blk[k].mole_fraction_constraint.deactivate() # Check that degrees of freedom are zero after fixing state vars for k in blk.keys(): if degrees_of_freedom(blk[k]) != 0: raise Exception("State vars fixed but degrees of freedom " "for state block is not zero during " "initialization.") # Set solver options if optarg is None: optarg = {"tol": 1e-8} opt = get_solver(solver, optarg) # --------------------------------------------------------------------- # Initialize property calculations # Check that there is something to solve for free_vars = 0 for k in blk.keys(): free_vars += number_unfixed_variables(blk[k]) if free_vars > 0: # If there are free variables, call the solver to initialize try: with idaeslog.solver_log(solve_log, idaeslog.DEBUG) as slc: res = solve_indexed_blocks(opt, [blk], tee=slc.tee) except: res = None else: res = None init_log.info("Properties Initialized {}.".format( idaeslog.condition(res))) # --------------------------------------------------------------------- # Return state to initial conditions if state_vars_fixed is False: if hold_state is True: return flags else: blk.release_state(flags) init_log.info("Initialization Complete")