for c in m.mixture_components: exprs[c] = gammas[c][0] * m.Y[t, '0'] + gammas[c][1] * m.Y[ t, '1'] + gammas[c][2] * m.Y[t, '2'] + gammas[c][3] * m.Y[ t, '3'] + gammas[c][4] * m.Y[ t, '4'] - exprs['V'] / V * m.Z[t, c] if c == 'C': exprs[c] += 0.02247311828 / (m.X[t, 'V'] * 210) * step return exprs builder.set_odes_rule(rule_odes) #Add time points where feed as discrete jump should take place: feed_times = [101.035, 400., 303.126] builder.add_feed_times(feed_times) model = builder.create_pyomo_model(0, 600) #========================================================================= #USER INPUT SECTION - FE Factory #========================================================================= # 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 = FESimulator(model) # defines the discrete points wanted in the concentration profile sim.apply_discretization('dae.collocation', nfe=50, ncp=3, scheme='LAGRANGE-RADAU')
vol_sum = 0.0 for c in m.mixture_components: vol_sum += partial_vol[c]*(sum(gammas[c][j]*m.Y[t,'r{}'.format(j)] for j in range(6))+ epsilon[c]*f/V*Cin) exprs['V'] = V*m.Y[t,'v_sum'] # mass balances for c in m.mixture_components: exprs[c] = sum(gammas[c][j]*m.Y[t,'r{}'.format(j)] for j in range(6))+ epsilon[c]*f/V*Cin - m.Y[t,'v_sum']*m.Z[t,c] exprs['Masa'] = 180.157*V*m.Y[t,'r5'] exprs['Msa'] = -138.121*V*m.Y[t,'r4'] return exprs builder.set_odes_rule(rule_odes) model = builder.create_pyomo_model(0.0,210.5257) #========================================================================= #USER INPUT SECTION - SIMULATION #========================================================================= sim = PyomoSimulator(model) # defines the discrete points wanted in the concentration profile sim.apply_discretization('dae.collocation',nfe=100,ncp=3,scheme='LAGRANGE-RADAU') fe_l = sim.model.alltime.get_finite_elements() fe_list = [fe_l[i + 1] - fe_l[i] for i in range(0, len(fe_l) - 1)] nfe = len(fe_list) #: Create a list with the step-size print(nfe) # sys.exit() # good initialization
# note the parameter is not fixed builder.add_parameter('k1', bounds=(0.0, 5.0)) builder.add_parameter('k2', bounds=(0.0, 1.0)) builder.add_spectral_data(D_frame) # define explicit system of ODEs def rule_odes(m, t): exprs = dict() exprs['A'] = -m.P['k1'] * m.Z[t, 'A'] exprs['B'] = m.P['k1'] * m.Z[t, 'A'] - m.P['k2'] * m.Z[t, 'B'] exprs['C'] = m.P['k2'] * m.Z[t, 'B'] return exprs builder.set_odes_rule(rule_odes) pyomo_model = builder.create_pyomo_model(0.0, 10.0) optimizer = ParameterEstimator(pyomo_model) optimizer.apply_discretization('dae.collocation', nfe=60, ncp=1, scheme='LAGRANGE-RADAU') # Provide good initial guess p_guess = {'k1': 4.0, 'k2': 0.5} raw_results = optimizer.run_lsq_given_P('ipopt', p_guess, tee=False) optimizer.initialize_from_trajectory('Z', raw_results.Z) optimizer.initialize_from_trajectory('S', raw_results.S) optimizer.initialize_from_trajectory('C', raw_results.C)
os.path.abspath(inspect.getfile(inspect.currentframe()))), '..', 'data_sets')) # filename = os.path.join(dataDirectory,'Dij_case52a.txt') filename = os.path.join(dataDirectory, 'Sij_small.txt') write_absorption_data_to_txt(filename, S_frame) # define explicit system of ODEs def rule_odes(m, t): exprs = dict() exprs['A'] = -m.P['k'] * m.Z[t, 'A'] exprs['B'] = m.P['k'] * m.Z[t, 'A'] 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)
# note the parameter is not fixed builder.add_parameter('k1', bounds=(0.0, 1.0)) builder.add_spectral_data(D_frame) # define explicit system of ODEs def rule_odes2(m, t): exprs = dict() exprs['A'] = -m.P['k1'] * m.Z[t, 'A'] * m.Z[t, 'B'] exprs['B'] = -m.P['k1'] * m.Z[t, 'A'] * m.Z[t, 'B'] exprs['C'] = m.P['k1'] * m.Z[t, 'A'] * m.Z[t, 'B'] return exprs builder.set_odes_rule(rule_odes2) pyomo_model2 = builder.create_pyomo_model(0.0, 1077.9) #pyomo_model2.P['k1'].value = 0.006655 #pyomo_model2.P['k1'].fixed = True v_estimator = VarianceEstimator(pyomo_model2) v_estimator.apply_discretization('dae.collocation', nfe=60, ncp=3, scheme='LAGRANGE-RADAU') # Provide good initial guess p_guess = {'k1': 0.006655} raw_results = v_estimator.run_lsq_given_P('ipopt', p_guess, tee=False)
exprs['B'] = m.P['k1']*m.Z[t,'A']-m.P['k2']*m.Z[t,'B'] exprs['C'] = m.P['k2']*m.Z[t,'B'] exprs['D'] = -m.P['k3']*m.Z[t,'D'] exprs['E'] = m.P['k3']*m.Z[t,'D'] return exprs builder.set_odes_rule(rule_odes) builder.add_measurement_times([i for i in frange(0., 10., 0.1)]) #Add time points where feed as discrete jump should take place: #feed_times=[101.035, 303.126]#, 400. #builder.add_feed_times(feed_times) model = builder.create_pyomo_model(0, 10) builder.add_absorption_data(S_frame) write_absorption_data_to_txt('Sij_multexp1.txt', S_frame) model = builder.create_pyomo_model(0., 10.) #========================================================================= #USER INPUT SECTION - FE Factory #========================================================================= # 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 = FESimulator(model)
builder.add_parameter('k2', bounds=(0.0, 5.0)) builder.add_spectral_data(D_frame) # define explicit system of ODEs def rule_odes(m, t): exprs = dict() exprs['A'] = -m.P['k1'] * m.Z[t, 'A'] * m.Z[t, 'B'] exprs['B'] = -m.P['k1'] * m.Z[t, 'A'] * m.Z[t, 'B'] exprs['C'] = m.P['k1'] * m.Z[t, 'A'] * m.Z[ t, 'B'] - 2 * m.P['k2'] * m.Z[t, 'C']**2 exprs['D'] = m.P['k2'] * m.Z[t, 'C']**2 return exprs builder.set_odes_rule(rule_odes) opt_model = builder.create_pyomo_model(0.0, 12.0) #========================================================================= # USER INPUT SECTION - VARIANCE ESTIMATION #========================================================================= v_estimator = VarianceEstimator(opt_model) v_estimator.apply_discretization('dae.collocation', nfe=60, ncp=3, scheme='LAGRANGE-RADAU') # Provide good initial guess p_guess = {'k1': 4.0, 'k2': 2.0}
builder.add_parameter('k', 0.01) # define explicit system of ODEs def rule_odes(m, t): exprs = dict() exprs['A'] = -m.P['k'] * m.Z[t, 'A'] exprs['B'] = m.P['k'] * m.Z[t, 'A'] 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, 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)
builder2.add_mixture_component({'A':1,'B':0}) # note the parameter is not fixed builder2.add_parameter('k',bounds=(0.0,1)) builder2.add_spectral_data(D_frame) # define explicit system of ODEs def rule_odes(m,t): exprs = dict() exprs['A'] = -m.P['k']*m.Z[t,'A'] exprs['B'] = m.P['k']*m.Z[t,'A'] return exprs builder2.set_odes_rule(rule_odes) pyomo_model = builder2.create_pyomo_model(0.0,200.0) optimizer = ParameterEstimator(pyomo_model) optimizer.apply_discretization('dae.collocation',nfe=100,ncp=3,scheme='LAGRANGE-RADAU') # Provide good initial guess p_guess = {'k':0.01} raw_results = optimizer.run_lsq_given_P('ipopt',p_guess,tee=False) optimizer.initialize_from_trajectory('Z',raw_results.Z) optimizer.initialize_from_trajectory('S',raw_results.S) optimizer.initialize_from_trajectory('C',raw_results.C) sigmas = {'device':1.94554e-6,
# note the parameter is not fixed builder.add_parameter('k1', bounds=(0.0, 1.0)) builder.add_spectral_data(D_frame) # define explicit system of ODEs def rule_odes2(m, t): exprs = dict() exprs['A'] = -m.P['k1'] * m.Z[t, 'A'] * m.Z[t, 'B'] exprs['B'] = -m.P['k1'] * m.Z[t, 'A'] * m.Z[t, 'B'] exprs['C'] = m.P['k1'] * m.Z[t, 'A'] * m.Z[t, 'B'] return exprs builder.set_odes_rule(rule_odes2) pyomo_model2 = builder.create_pyomo_model(0.0, 200.0) optimizer = ParameterEstimator(pyomo_model2) optimizer.apply_discretization('dae.collocation', nfe=60, ncp=3, scheme='LAGRANGE-RADAU') # Provide good initial guess p_guess = {'k1': 0.006655} raw_results = optimizer.run_lsq_given_P('ipopt', p_guess, tee=False) optimizer.initialize_from_trajectory('Z', raw_results.Z) optimizer.initialize_from_trajectory('S', raw_results.S) optimizer.initialize_from_trajectory('dZdt', raw_results.dZdt)
V = m.X[t, 'V'] # mass balances for c in m.mixture_components: exprs[c] = gammas[c][0] * m.Y[t, '0'] + gammas[c][1] * m.Y[t, '1']+gammas[c][2] * m.Y[t, '2']+gammas[c][3] * m.Y[t, '3']+ gammas[c][4] * m.Y[t, '4'] - exprs['V'] / V * m.Z[t, c] if c == 'C': exprs[c] += 0.02247311828 / (m.X[t, 'V'] * 210) * step return exprs builder.set_odes_rule(rule_odes) #Add time points where feed as discrete jump should take place: feed_times=[101.035, 303.126]#, 400. builder.add_feed_times(feed_times) model = builder.create_pyomo_model(0, 600) non_abs = ['F'] builder.set_non_absorbing_species(model, non_abs) #Load data: dataDirectory = os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))), 'data_sets')) filenameD = os.path.join(dataDirectory,'FeCaseexamplewithoutTemp_D_data_input_noiselesspoints2.csv') D_frame = read_spectral_data_from_csv(filenameD) builder.add_spectral_data(D_frame) model = builder.create_pyomo_model(0., 600.) #Absorbing Simulation for Initialization: #========================================================================= #USER INPUT SECTION - FE Factory
def rule_odes2(m, t): exprs = dict() exprs['A2'] = -m.P['k1'] * m.X[t, 'A2'] exprs['B'] = m.P['k1'] * m.X[t, 'A2'] - m.P['k2'] * m.Z[t, 'B'] exprs['C'] = m.P['k2'] * m.Z[t, 'B'] return exprs builder.set_odes_rule(rule_odes) builder2.set_odes_rule(rule_odes2) start_time1 = 0.0 start_time2 = 0.0 end_time1 = 10.0 end_time2 = 10.0 modelexp1 = builder.create_pyomo_model(start_time1, end_time1) builder22 = TemplateBuilder() builder22.add_mixture_component(components) builder22.add_parameter('k1', init=1.0, bounds=(0.0, 10)) # There is also the option of providing initial values: Just add init=... as additional argument as above. builder22.add_parameter('k2', init=0.224, bounds=(0.0, 10)) # add complementary state variable: builder22.add_complementary_state_variable(extra_states2) builder22.set_odes_rule(rule_odes2) modelexp2 = builder22.create_pyomo_model(start_time2, end_time2) #Define our start and end times for the experiment, as we had done in the past, now related to #the dataset names initextra = {'Exp1': 'A', 'Exp2': 'A2'} start_time = {'Exp1': start_time1, 'Exp2': start_time2}
t, 'B'] - m.P['k3'] * m.Z[t, 'B'] exprs['C'] = m.P['k2'] * m.Z[t, 'B'] - m.P['k4'] * m.Z[t, 'C'] exprs['D'] = m.P['k4'] * m.Z[t, 'A'] - m.P['k3'] * m.Z[t, 'D'] exprs['E'] = m.P['k3'] * m.Z[t, 'B'] - m.P['k5'] * m.Z[t, 'E'] * m.Z[ t, 'A'] exprs['F'] = m.P['k5'] * m.Z[t, 'E'] * m.Z[t, 'A'] - m.P['k6'] * m.Z[ t, 'G']**2 * m.Z[t, 'F'] exprs['G'] = -m.P['k6'] * m.Z[t, 'G']**2 * m.Z[t, 'F'] exprs['H'] = m.P['k6'] * m.Z[t, 'G']**2 * m.Z[t, 'F'] return exprs #builder.add_concentration_data(D_frame) #Add these ODEs to our model template builder.set_odes_rule(rule_odes) opt_model = builder.create_pyomo_model(0.0, 20.0) # 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
def rule_odes(m, t): exprs = dict() exprs['A'] = -m.P['k1'] * m.Z[t, 'A'] exprs['B'] = m.P['k1'] * m.Z[t, 'A'] - m.P['k2'] * m.Z[t, 'B'] exprs['C'] = m.P['k2'] * m.Z[t, 'B'] return exprs builder.set_odes_rule(rule_odes) # Notice that to use the wavelength subset optimization we need to use make a copy of the builder # before we add the spectral data matrix builder_before_data = builder builder.add_spectral_data(D_frame) end_time = 10 opt_model = builder.create_pyomo_model(0.0, end_time) #fD_frame = savitzky_golay(dataFrame = D_frame, window_size = 17, orderPoly = 3, orderDeriv=1) #========================================================================= #USER INPUT SECTION - VARIANCE ESTIMATION #========================================================================= # For this problem we have an input D matrix that has some noise in it # We can therefore use the variance estimator described in the Overview section # of the documentation and Section 4.3.3 nfe = 100 ncp = 3 v_estimator = VarianceEstimator(opt_model) v_estimator.apply_discretization('dae.collocation', nfe=nfe, ncp=ncp,