# Load spectral data ################################################################################# dataDirectory = os.path.abspath( os.path.join( os.path.dirname( os.path.abspath(inspect.getfile(inspect.currentframe()))), 'data_sets')) filename = os.path.join(dataDirectory, 'Dij.txt') D_frame = read_spectral_data_from_txt(filename) # build dae block for optimization problems ################################################################################# builder = TemplateBuilder() components = {'A': 1e-3, 'B': 0, 'C': 0} builder.add_mixture_component(components) builder.add_parameter('k1', bounds=(0.1, 4.0)) builder.add_parameter('k2', bounds=(0.01, 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) opt_model = builder.create_pyomo_model(0.0, 15.0) #################################################################################
builder.add_mixture_component(components) # add algebraics algebraics = [0, 1, 2, 3, 4, 5] # the indices of the rate rxns # note the fifth component. Which basically works as an input builder.add_algebraic_variable(algebraics) params = dict() params['k0'] = 49.7796 params['k1'] = 8.93156 params['k2'] = 1.31765 params['k3'] = 0.310870 params['k4'] = 3.87809 builder.add_parameter(params) # add additional state variables extra_states = dict() extra_states['V'] = 0.0629418 builder.add_complementary_state_variable(extra_states) # stoichiometric coefficients gammas = dict() gammas['AH'] = [-1, 0, 0, -1, 0] gammas['B'] = [-1, 0, 0, 0, 1] gammas['C'] = [0, -1, 1, 0, 0] gammas['BH+'] = [1, 0, 0, 0, -1] gammas['A-'] = [1, -1, 1, 1, 0] gammas['AC-'] = [0, 1, -1, -1, -1]
dataDirectory = os.path.abspath( os.path.join( os.path.dirname( os.path.abspath(inspect.getfile(inspect.currentframe()))), 'data_sets')) filename = os.path.join(dataDirectory, 'Dij.txt') D_frame = read_spectral_data_from_txt(filename) sD_frame = snv(dataFrame=D_frame) fD_frame = savitzky_golay(dataFrame=sD_frame, window_size=15, orderPoly=9) # Then we build dae block for as described in the section 4.2.1. Note the addition # of the data using .add_spectral_data ################################################################################# builder = TemplateBuilder() components = {'A': 1e-3, 'B': 0, 'C': 0} builder.add_mixture_component(components) builder.add_parameter('k1', init=1, bounds=(0.2, 4.0)) builder.add_parameter('k2', init=0.2, bounds=(0.05, 1.0)) builder.add_spectral_data(fD_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) opt_model = builder.create_pyomo_model(0.0, 10.0) #=========================================================================
################################################################################# dataDirectory = os.path.abspath( os.path.join( os.path.dirname( os.path.abspath(inspect.getfile(inspect.currentframe()))), '..', 'data_sets')) filename = os.path.join(dataDirectory, 'Dij_case51c.txt') D_frame = read_spectral_data_from_txt(filename) ###################################### builder = TemplateBuilder() components = {'A': 1, 'B': 0.8, 'C': 0, 'D': 0} builder.add_mixture_component(components) # note the parameter is not fixed builder.add_parameter('k1', bounds=(0.0, 5.0)) 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)
import matplotlib.pyplot as plt import sys import os if __name__ == "__main__": with_plots = True if len(sys.argv) == 2: if int(sys.argv[1]): with_plots = False # create template model builder = TemplateBuilder() components = {'A': 1, 'B': 0.8, 'C': 0, 'D': 0} builder.add_mixture_component(components) builder.add_parameter('k1', 2.0) builder.add_parameter('k2', 1.0) # 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) # create an instance of a pyomo model template
#========================================================================= # First we will look to generate our data for each of our components in order # to build the C-matrix that we desire. We will look to give our species different # noise levels in order to also test whether the scaling for multiple levels works builder = TemplateBuilder() builder.add_mixture_component('A', 0.5) builder.add_mixture_component('B', 0.0) builder.add_mixture_component('C', 0.0) builder.add_mixture_component('D', 0.01) builder.add_mixture_component('E', 0.0) builder.add_mixture_component('F', 0.3) builder.add_mixture_component('G', 0.5) builder.add_mixture_component('H', 0.0) #Following this we add the kinetic parameters builder.add_parameter('k1', 0.3) builder.add_parameter('k2', 0.1) builder.add_parameter('k3', 0.1) builder.add_parameter('k4', 0.4) builder.add_parameter('k5', 0.02) builder.add_parameter('k6', 0.5) # define explicit system of ODEs def rule_odes(m, t): exprs = dict() exprs['A'] = -m.P['k1'] * m.Z[t, 'A'] - m.P['k4'] * m.Z[ t, 'A'] - m.P['k5'] * m.Z[t, 'E'] * m.Z[t, 'A'] exprs['B'] = m.P['k1'] * m.Z[t, 'A'] - m.P['k2'] * m.Z[ 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']
plt.title("Concentration Profile with Noise ") plt.show() #========================================================================= #USER INPUT SECTION - Parameter Estimation #======================================================================== #Here with fixed variances! sigmas={'C': 1e-10, 'B': 1e-10, 'A': 1e-10, 'device': 1e-10} #Now introduce parameters as non fixed pyomo_model.del_component(params) builder.add_parameter('k_p',bounds=(0.0,8e7)) model = builder.create_pyomo_model(0.0,20.0) p_estimator = ParameterEstimator(model) p_estimator.apply_discretization('dae.collocation', nfe=40, ncp=3, scheme='LAGRANGE-RADAU') # # Again we provide options for the solver options = dict() # finally we run the optimization results_pyomo = p_estimator.run_opt('k_aug', tee=True, solver_opts=options, variances=sigmas, covariance=True, #inputs_sub=inputs_sub,
import matplotlib.pyplot as plt import sys import os if __name__ == "__main__": with_plots = True if len(sys.argv)==2: if int(sys.argv[1]): with_plots = False # create template model builder = TemplateBuilder() components = {'A':0.21,'B':0.21,'C':0} builder.add_mixture_component(components) builder.add_parameter('k1',0.00539048) # 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'] return exprs builder.set_odes_rule(rule_odes) # create an instance of a casadi model template casadi_model = builder.create_casadi_model(0.0,1000.0) # create instance of simulator
D_frame2 = read_spectral_data_from_txt(filename2) #This function can be used to remove a certain number of wavelengths from data # in this case only every 2nd wavelength is included D_frame1 = decrease_wavelengths(D_frame1, A_set=3) #Here we add noise to datasets in order to make our data differenct between experiments D_frame2 = add_noise_to_signal(D_frame2, 0.000000011) D_frame2 = decrease_wavelengths(D_frame2, A_set=3) ################################################################################# builder = TemplateBuilder() components = {'A': 1e-3, 'B': 0, 'C': 0} builder.add_mixture_component(components) builder.add_parameter('k1', init=1.0, bounds=(0.00, 10)) #There is also the option of providing initial values: Just add init=... as additional argument as above. builder.add_parameter('k2', init=0.224, bounds=(0.0, 10)) # If you have multiple experiments, you need to add your experimental datasets to a dictionary: datasets = {'Exp1': D_frame1, 'Exp2': D_frame2} # Additionally, we do not add the spectral data to the TemplateBuilder, rather supplying the # TemplateBuilder before data is added as an argument into the function # 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']
################################################################################# dataDirectory = os.path.abspath( os.path.join( os.path.dirname( os.path.abspath(inspect.getfile(inspect.currentframe()))), 'data_sets')) filename = os.path.join(dataDirectory, 'Dij.txt') D_frame = read_spectral_data_from_txt(filename) # Then we build dae block for as described in the section 4.2.1. Note the addition # of the data using .add_spectral_data ################################################################################# builder = TemplateBuilder() components = {'A': 1e-3, 'B': 0, 'C': 0} builder.add_mixture_component(components) builder.add_parameter('k1', init=4.0, bounds=(0.0, 5.0)) # There is also the option of providing initial values: Just add init=... as additional argument as above. 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) builder.bound_profile(var='S', bounds=(0, 200)) opt_model = builder.create_pyomo_model(0.0, 10.0)
os.path.dirname( os.path.abspath(inspect.getfile(inspect.currentframe()))), '../data_sets')) filename = os.path.join(dataDirectory, 'Dij.txt') D_frame = read_spectral_data_from_txt(filename) basic_pca(D_frame, n=5, with_plots=with_plots) D_frame = savitzky_golay(dataFrame=D_frame, window_size=17, orderPoly=5) # Then we build dae block for as described in the section 4.2.1. Note the addition # of the data using .add_spectral_data ################################################################################## builder = TemplateBuilder() components = {'A': 1e-3, 'B': 0, 'C': 0} builder.add_mixture_component(components) builder.add_parameter('k1', init=2, bounds=(0.0, 5.0)) #There is also the option of providing initial values: Just add init=... as additional argument as above. builder.add_parameter('k2', init=0.2, bounds=(0.0, 5.0)) # 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) # 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
import matplotlib.pyplot as plt import sys if __name__ == "__main__": with_plots = True if len(sys.argv) == 2: if int(sys.argv[1]): with_plots = False # create template model builder = TemplateBuilder() builder.add_mixture_component('A', 1) builder.add_mixture_component('B', 0) builder.add_algebraic_variable('ra') builder.add_parameter('k', 0.01) # define explicit system of ODEs def rule_odes(m, t): exprs = dict() exprs['A'] = -m.Y[t, 'ra'] exprs['B'] = m.Y[t, 'ra'] return exprs builder.set_odes_rule(rule_odes) def rule_algebraics(m, t): algebraics = list() algebraics.append(m.Y[t, 'ra'] - m.P['k'] * m.Z[t, 'A']) return algebraics
# Load Temp data: dataDirectory = os.path.abspath( os.path.join( os.path.dirname( os.path.abspath(inspect.getfile(inspect.currentframe()))), 'data_sets')) Ttraj = os.path.join(dataDirectory, 'Tempvalues.csv') fixed_Ttraj = read_absorption_data_from_csv(Ttraj) params = dict() params['k1'] = 1.0 params['k2Tr'] = 0.2265 params['E'] = 2. builder.add_parameter(params) # add additional state variables extra_states = dict() extra_states['V'] = 0.0629418 builder.add_complementary_state_variable(extra_states) # stoichiometric coefficients gammas = dict() gammas['A'] = [-1, 0] gammas['B'] = [1, -1] gammas['C'] = [0, 1] # define system of DAEs
import sys import os if __name__ == "__main__": with_plots = True if len(sys.argv) == 2: if int(sys.argv[1]): with_plots = False # create template model builder = TemplateBuilder() builder.add_mixture_component('A', 1) builder.add_mixture_component('B', 0) builder.add_mixture_component('C', 0) builder.add_parameter('k1', 0.3) builder.add_parameter('k2', 0.05) 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) # create an instance of a casadi model template casadi_model = builder.create_casadi_model(0.0, 12.0) # create instance of simulator
#========================================================================= # First we will look to generate our data for each of our components in order # to build the C-matrix that we desire. We will look to give our species different # noise levels in order to also test whether the scaling for multiple levels works builder = TemplateBuilder() builder.add_mixture_component('A', 0.5) builder.add_mixture_component('B', 0.0) builder.add_mixture_component('C', 0.0) builder.add_mixture_component('D', 0.01) builder.add_mixture_component('E', 0.0) builder.add_mixture_component('F', 0.3) builder.add_mixture_component('G', 0.5) builder.add_mixture_component('H', 0.0) #Following this we add the kinetic parameters builder.add_parameter('k1', 0.3) builder.add_parameter('k2', 0.1) builder.add_parameter('k3', 0.1) builder.add_parameter('k4', 0.4) builder.add_parameter('k5', 0.02) builder.add_parameter('k6', 0.5) # define explicit system of ODEs def rule_odes(m, t): exprs = dict() exprs['A'] = -m.P['k1'] * m.Z[t, 'A'] - m.P['k4'] * m.Z[ t, 'A'] - m.P['k5'] * m.Z[t, 'E'] * m.Z[t, 'A'] exprs['B'] = m.P['k1'] * m.Z[t, 'A'] - m.P['k2'] * m.Z[ 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']