コード例 #1
0
ファイル: FESimulator.py プロジェクト: schenkch/kipet
    def __init__(self, model):
        """
            FESimulator class:
    
                This class is just an interface that allows the user of Kipet to easily implement the more general 
                fe_factory class designed by David M. Thierry without having to re-write the model to fit those
                arguments. It takes in a standard Kipet/Pyomo model, rewrites it and calls fe_factory.
                More information on fe_factory is included in that class description.
    
                Args:
                    model (ConcreteModel): The original Pyomo model created in the Kipet script
        """
        super(FESimulator, self).__init__(model)
        self.p_sim =  PyomoSimulator(model)
        self.c_sim = self.p_sim.model.clone()
        self.param_dict = {}
        self.param_name = "P"

        # check all parameters are fixed before simulating
        for p_sim_data in six.itervalues(self.p_sim.model.P):
            if not p_sim_data.fixed:
                raise RuntimeError('For simulation fix all parameters. Parameter {} is unfixed'.format(p_sim_data.cname()))

        #Build the parameter dictionary in the format that fe_factory uses    
        for k,v in six.iteritems(self.p_sim.model.P):
            self.param_dict["P",k] = v.value

        #Build the initial condition dictionary in the format that fe_factory uses
        self.ics_ = {} 

        for t, k in six.iteritems(self.p_sim.model.Z):
            st = self.p_sim.model.start_time
            if t[0] == st:
                self.ics_['Z', t[1]] = k.value
                
        #Now to set the additional state values
        for t, v in six.iteritems(self.p_sim.model.X):
            if t[0] == st:
                self.ics_['X',t[1]] = v.value
コード例 #2
0
ファイル: ode_sim.py プロジェクト: schenkch/kipet
        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 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")
コード例 #3
0
        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

    filename_initZ = os.path.join(dataDirectory, 'init_Z.csv')#Use absolute paths
    initialization = pd.read_csv(filename_initZ,index_col=0)
    sim.initialize_from_trajectory('Z',initialization)
    filename_initX = os.path.join(dataDirectory, 'init_X.csv')#Use absolute paths
    initialization = pd.read_csv(filename_initX,index_col=0)
    sim.initialize_from_trajectory('X',initialization)
コード例 #4
0
        os.path.join(
            os.path.dirname(
                os.path.abspath(inspect.getfile(inspect.currentframe()))),
            'data_sets'))
    filename = os.path.join(dataDirectory, 'trimmed.csv')

    D_frame = read_spectral_data_from_csv(filename)
    meas_times = sorted(D_frame.index)

    model = builder.create_pyomo_model(0, 600)

    #=========================================================================
    #USER INPUT SECTION - FE Factory
    #=========================================================================

    sim = PyomoSimulator(model)
    mod = sim.model.clone()

    # defines the discrete points wanted in the concentration profile
    sim.apply_discretization('dae.collocation',
                             nfe=5,
                             ncp=3,
                             scheme='LAGRANGE-RADAU')

    #: we now need to explicitly tell the initial conditions and parameter values
    param_name = "P"
    param_dict = {}
    param_dict["P", "k0"] = 49.7796
    param_dict["P", "k1"] = 8.93156
    param_dict["P", "k2"] = 1.31765
    param_dict["P", "k3"] = 0.310870
コード例 #5
0
    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)

    if with_plots:

        results_sim.C.plot.line()
コード例 #6
0
    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)
    scaled_sim.apply_discretization('dae.collocation',
                                    nfe=30,
                                    ncp=1,
コード例 #7
0
        
        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 
    #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))")