exprs['C'] = m.P['k1'] * m.Z[t, 'A'] * m.Z[t, 'B'] return exprs builder.set_odes_rule(rule_odes) # create an instance of a pyomo model template # the template includes # - Z variables indexed over time and components names e.g. m.Z[t,'A'] # - P parameters indexed over the parameter names e.g. m.P['k'] pyomo_model = builder.create_pyomo_model(0.0, 1000.0) # create instance of simulator simulator = PyomoSimulator(pyomo_model) # defines the discrete points wanted in the concentration profile simulator.apply_discretization('dae.collocation', nfe=200, ncp=3, scheme='LAGRANGE-RADAU') # simulate results_pyomo = simulator.run_sim('ipopt', tee=True) # display concentration results if with_plots: results_pyomo.Z.plot.line(legend=True) plt.xlabel("time (s)") plt.ylabel("Concentration (mol/L)") plt.title("Concentration Profile") plt.show()
initialization = pd.read_csv(filename_initY,index_col=0) sim.initialize_from_trajectory('Y',initialization) sim.fix_from_trajectory('Y','Csat',fixed_traj) sim.fix_from_trajectory('Y','f',fixed_traj) with open("f0.txt", "w") as f: for t in sim.model.alltime: val = value(sim.model.Y[t, 'f']) f.write('\t' + str(t) + '\t' + str(val) + '\n') f.close() options = {'halt_on_ampl_error' :'yes'} results = sim.run_sim('ipopt', tee=True, solver_opts=options) if with_plots: # display concentration results results.Z.plot.line(legend=True) plt.xlabel("time (s)") plt.ylabel("Concentration (mol/L)") plt.title("Concentration Profile") C.plot() plt.figure() results.Y['Csat'].plot.line() plt.plot(fixed_traj['Csat'],'*') plt.xlabel("time (s)")
# create an instance of a pyomo model template # the template includes # - Z variables indexed over time and components names e.g. m.Z[t,'A'] # - P parameters indexed over the parameter names e.g. m.P['k'] pyomo_model = builder.create_pyomo_model(0.0, 200.0) # create instance of simulator simulator = PyomoSimulator(pyomo_model) # defines the discrete points wanted in the concentration profile simulator.apply_discretization('dae.collocation', nfe=30, ncp=1, scheme='LAGRANGE-RADAU') # simulate results_pyomo = simulator.run_sim('ipopt', tee=True) # second model to scale and initialize scaled_model = builder.create_pyomo_model(0.0, 200.0) scaled_sim = PyomoSimulator(scaled_model) scaled_sim.apply_discretization('dae.collocation', nfe=30, ncp=1, scheme='LAGRANGE-RADAU') scaled_sim.initialize_from_trajectory('Z', results_pyomo.Z) scaled_sim.initialize_from_trajectory('dZdt', results_pyomo.dZdt) scaled_sim.scale_variables_from_trajectory('Z', results_pyomo.Z) scaled_sim.scale_variables_from_trajectory('dZdt', results_pyomo.dZdt)
return exprs builder.set_odes_rule(rule_odes) sim_model = builder.create_pyomo_model(0.0, 40.0) # create instance of simulator simulator = PyomoSimulator(sim_model) simulator.apply_discretization('dae.collocation', nfe=4, ncp=1, scheme='LAGRANGE-RADAU') # simulate sigmas = {'device': 1e-10, 'A': 1e-5, 'B': 1e-7} results_sim = simulator.run_sim('ipopt', tee=True, variances=sigmas, seed=123453256) if with_plots: results_sim.C.plot.line() results_sim.S.plot.line() plt.show() ################################################################################# builder = TemplateBuilder() builder.add_mixture_component(concentrations) builder.add_parameter('k', bounds=(0.0, 1.0)) builder.add_spectral_data(results_sim.D) builder.set_odes_rule(rule_odes)
# Once the model is described we run the simulator # call FESimulator # FESimulator re-constructs the current TemplateBuilder into fe_factory syntax # there is no need to call PyomoSimulator any more as FESimulator is a child class sim = PyomoSimulator(opt_model) # defines the discrete points wanted in the concentration profile sim.apply_discretization('dae.collocation', nfe=50, ncp=3, scheme='LAGRANGE-RADAU') #this will allow for the fe_factory to run the element by element march forward along #the elements and also automatically initialize the PyomoSimulator model, allowing #for the use of the run_sim() function as before. We only need to provide the inputs #to this function as an argument dictionary init = sim.run_sim(solver = 'ipopt', tee = True) if with_plots: init.Z.plot.line(legend=True) plt.xlabel("time (min)") plt.ylabel("Concentration ((mol /cm3))") plt.title("Concentration Profile") plt.show() print("Simulation is done") data = add_noise_to_signal(init.Z,0.02) if with_plots: