コード例 #1
0
    def simulate(self, Tend, nIntervals, gridWidth):

        problem = Explicit_Problem(self.rhs, self.y0)
        problem.name = 'CVode'
        # solver.rhs = self.right_hand_side
        problem.handle_result = self.handle_result
        problem.state_events = self.state_events
        problem.handle_event = self.handle_event
        problem.time_events = self.time_events
        problem.finalize = self.finalize

        simulation = CVode(problem)

        # Change multistep method: 'adams' or 'VDF'
        if self.discr == 'Adams':
            simulation.discr = 'Adams'
            simulation.maxord = 12
        else:
            simulation.discr = 'BDF'
            simulation.maxord = 5
        # Change iteration algorithm: functional(FixedPoint) or newton
        if self.iter == 'FixedPoint':
            simulation.iter = 'FixedPoint'
        else:
            simulation.iter = 'Newton'

        # Sets additional parameters
        simulation.atol = self.atol
        simulation.rtol = self.rtol
        simulation.verbosity = self.verbosity
        if hasattr(simulation, 'continuous_output'):
            simulation.continuous_output = False  # default 0, if one step approach should be used
        elif hasattr(simulation, 'report_continuously'):
            simulation.report_continuously = False  # default 0, if one step approach should be used

        # '''Initialize problem '''
        # self.t_cur = self.t0
        # self.y_cur = self.y0

        # Calculate nOutputIntervals:
        if gridWidth <> None:
            nOutputIntervals = int((Tend - self.t0) / gridWidth)
        else:
            nOutputIntervals = nIntervals
        # Check for feasible input parameters
        if nOutputIntervals == 0:
            print 'Error: gridWidth too high or nIntervals set to 0! Continue with nIntervals=1'
            nOutputIntervals = 1
        # Perform simulation
        simulation.simulate(
            Tend, nOutputIntervals
        )  # to get the values: t_new, y_new = simulation.simulate
コード例 #2
0
    def simulate(self, Tend, nIntervals, gridWidth):

        problem = Explicit_Problem(self.rhs, self.y0)
        problem.name = 'CVode'
        # solver.rhs = self.right_hand_side
        problem.handle_result = self.handle_result
        problem.state_events = self.state_events
        problem.handle_event = self.handle_event
        problem.time_events = self.time_events
        problem.finalize = self.finalize

        simulation = CVode(problem)

        # Change multistep method: 'adams' or 'VDF'
        if self.discr == 'Adams':
            simulation.discr = 'Adams'
            simulation.maxord = 12
        else:
            simulation.discr = 'BDF'
            simulation.maxord = 5
        # Change iteration algorithm: functional(FixedPoint) or newton
        if self.iter == 'FixedPoint':
            simulation.iter = 'FixedPoint'
        else:
            simulation.iter = 'Newton'

        # Sets additional parameters
        simulation.atol = self.atol
        simulation.rtol = self.rtol
        simulation.verbosity = self.verbosity
        if hasattr(simulation, 'continuous_output'):
            simulation.continuous_output = False  # default 0, if one step approach should be used
        elif hasattr(simulation, 'report_continuously'):
            simulation.report_continuously = False  # default 0, if one step approach should be used

        # '''Initialize problem '''
        # self.t_cur = self.t0
        # self.y_cur = self.y0

        # Calculate nOutputIntervals:
        if gridWidth <> None:
            nOutputIntervals = int((Tend - self.t0) / gridWidth)
        else:
            nOutputIntervals = nIntervals
        # Check for feasible input parameters
        if nOutputIntervals == 0:
            print 'Error: gridWidth too high or nIntervals set to 0! Continue with nIntervals=1'
            nOutputIntervals = 1
        # Perform simulation
        simulation.simulate(Tend, nOutputIntervals)  # to get the values: t_new, y_new = simulation.simulate
コード例 #3
0
def doSimulate():

    theta0 = 1.0  # radianer, startvinkel från lodrätt

    tfinal = 3

    title = 'k = {0}, stretch = {1}, {2}'.format(k, stretch, solver)

    r0 = 1.0 + stretch
    x0 = r0 * np.sin(theta0)
    y0 = -r0 * np.cos(theta0)
    t0 = 0.0

    y_init = np.array([x0, y0, 0, 0])

    ElasticSpring = Explicit_Problem(rhs, y_init, t0)
    if solver.lower() == "cvode":
        sim = CVode(ElasticSpring)
    elif solver.lower() == "bdf_2":
        sim = BDF_2(ElasticSpring)
    elif solver.lower() == "ee":
        sim = EE(ElasticSpring)
    else:
        sim == None
        raise ValueError('Expected "CVode", "EE" or "BDF_2"')
    sim.report_continuously = False

    npoints = 100 * tfinal

    #t,y = sim.simulate(tfinal,npoints)
    try:
        #for i in [1]:
        t, y = sim.simulate(tfinal)
        xpos, ypos = y[:, 0], y[:, 1]
        #plt.plot(t,y[:,0:2])
        plt.plot(xpos, ypos)
        plt.plot(xpos[0], ypos[0], 'or')
        plt.xlabel('y_1')
        plt.ylabel('y_2')
        plt.title(title)
        plt.axis('equal')
        plt.show()
    except Explicit_ODE_Exception as e:
        print(e.message)
        print("for the case {0}.".format(title))
コード例 #4
0
    def make_explicit_sim(self):
        explicit_sim = CVode(self.explicit_problem)
        explicit_sim.iter = 'Newton'
        explicit_sim.discr = 'BDF'
        explicit_sim.rtol = 1e-7
        explicit_sim.atol = 1e-7
        explicit_sim.sensmethod = 'SIMULTANEOUS'
        explicit_sim.suppress_sens = True
        explicit_sim.report_continuously = False
        explicit_sim.usesens = False
        explicit_sim.verbosity = 50

        if self.use_jac and self.model_jac is not None:
            explicit_sim.usejac = True

        else:
            explicit_sim.usejac = False

        return explicit_sim
コード例 #5
0
ファイル: model.py プロジェクト: mortbauer/limitsofgrowth
 def s_cvode_natural(self,params):
     from assimulo.problem import Explicit_Problem
     from assimulo.solvers import CVode
     problem = Explicit_Problem(lambda t,x,p:self.create_dx(p)(t,x)['f'](),
                                [1.0,1.0,1.0],0,
                                [params[p] for p in self.params])
     sim = CVode(problem)
     sim.report_continuously = True
     t,x = sim.simulate(250,self.time.shape[0]-1)
     dataframe = pandas.DataFrame(x,
             columns=['population','burden','economy'])
     d = {}
     sens = np.array(sim.p_sol)
     for i,col in enumerate(self.cols):
         for j,param in enumerate(
             ('birthrate','deathrate','regenerationrate',
              'burdenrate','economyaim','growthrate')):
             d['{0},{1}'.format(col,param)] = sens[j,:,i]
     dataframe_sens = pandas.DataFrame(d,index=self.time)
     return dataframe_sens
コード例 #6
0
ファイル: Simulation.py プロジェクト: SysMo/SmoWeb
	def prepareSimulation(self, params = None):
		if params == None:
			params = AttributeDict({
				'absTol' : 1e-6, 
				'relTol' : 1e-6,
			})
		
		#Define an explicit solver 
		simSolver = CVode(self) 
		#Create a CVode solver
		#Sets the parameters 
		#simSolver.verbosity = LOUD
		simSolver.report_continuously = True
		simSolver.iter = 'Newton' #Default 'FixedPoint'
		simSolver.discr = 'BDF' #Default 'Adams'
		#simSolver.discr = 'Adams' 
		simSolver.atol = [params.absTol]	#Default 1e-6 
		simSolver.rtol = params.relTol 	#Default 1e-6		
		simSolver.problem_info['step_events'] = True # activates step events
		#simSolver.maxh = 1.0
		simSolver.store_event_points = True
		self.simSolver = simSolver
コード例 #7
0
ファイル: Simulation.py プロジェクト: birdol/SmoWeb
    def prepareSimulation(self, params=None):
        if params == None:
            params = AttributeDict({
                'absTol': 1e-6,
                'relTol': 1e-6,
            })

        #Define an explicit solver
        simSolver = CVode(self)
        #Create a CVode solver
        #Sets the parameters
        #simSolver.verbosity = LOUD
        simSolver.report_continuously = True
        simSolver.iter = 'Newton'  #Default 'FixedPoint'
        simSolver.discr = 'BDF'  #Default 'Adams'
        #simSolver.discr = 'Adams'
        simSolver.atol = [params.absTol]  #Default 1e-6
        simSolver.rtol = params.relTol  #Default 1e-6
        simSolver.problem_info['step_events'] = True  # activates step events
        #simSolver.maxh = 1.0
        simSolver.store_event_points = True
        self.simSolver = simSolver
コード例 #8
0
def run_example(with_plots=True):
    """
    Example of the use of CVode for a differential equation
    with a iscontinuity (state event) and the need for an event iteration.
    
    on return:
    
       - :dfn:`exp_mod`    problem instance
    
       - :dfn:`exp_sim`    solver instance
    """
    #Create an instance of the problem
    exp_mod = Extended_Problem()  #Create the problem

    exp_sim = CVode(exp_mod)  #Create the solver

    exp_sim.verbosity = 0
    exp_sim.report_continuously = True

    #Simulate
    t, y = exp_sim.simulate(
        10.0, 1000)  #Simulate 10 seconds with 1000 communications points
    exp_sim.print_event_data()

    #Plot
    if with_plots:
        import pylab as P
        P.plot(t, y)
        P.title(exp_mod.name)
        P.ylabel('States')
        P.xlabel('Time')
        P.show()

    #Basic test
    nose.tools.assert_almost_equal(y[-1][0], 8.0)
    nose.tools.assert_almost_equal(y[-1][1], 3.0)
    nose.tools.assert_almost_equal(y[-1][2], 2.0)

    return exp_mod, exp_sim
コード例 #9
0
    def simulate(self, Tend, nIntervals, gridWidth):

        # define assimulo problem:(has to be done here because of the starting value in Explicit_Problem
        solver = Explicit_Problem(self.rhs, self.y0)
        ''' *******DELETE LATER '''''''''
#        problem.handle_event = handle_event
#        problem.state_events = state_events
#        problem.init_mode = init_mode

        solver.handle_result = self.handle_result


        solver.name = 'Simple Explicit Example'
        simulation = CVode(solver)  # Create a RungeKutta34 solver
        # simulation.inith = 0.1 #Sets the initial step, default = 0.01

        # Change multistep method: 'adams' or 'VDF'
        if self.discr == 'Adams':
            simulation.discr = 'Adams'
            simulation.maxord = 12
        else:
            simulation.discr = 'BDF'
            simulation.maxord = 5

        # Change iteration algorithm: functional(FixedPoint) or newton
        if self.iter == 'FixedPoint':
            simulation.iter = 'FixedPoint'
        else:
            simulation.iter = 'Newton'

        # Sets additional parameters
        simulation.atol = self.atol
        simulation.rtol = self.rtol
        simulation.verbosity = 0
        if hasattr(simulation, 'continuous_output'):
            simulation.continuous_output = False  # default 0, if one step approach should be used
        elif hasattr(simulation, 'report_continuously'):
            simulation.report_continuously = False  # default 0, if one step approach should be used

        # Create Solver and set settings
#        noRootFunctions = np.size(self.state_events(self.t0, np.array(self.y0)))

#        solver = sundials.CVodeSolver(RHS = self.f, ROOT = self.rootf, SW = [False]*noRootFunctions,
#                       abstol = self.atol, reltol = self.rtol)
        # solver.settings.JAC = None   #Add user-dependent jacobian here

        '''Initialize problem '''
#        solver.init(self.t0, self.y0)
        self.handle_result(self.t0, self.y0)
        nextTimeEvent = self.time_events(self.t0, self.y0)
        self.t_cur = self.t0
        self.y_cur = self.y0
        state_event = False
#
#
        if gridWidth <> None:
            nOutputIntervals = int((Tend - self.t0) / gridWidth)
        else:
            nOutputIntervals = nIntervals
        # Define step length depending on if gridWidth or nIntervals has been chosen
        if nOutputIntervals > 0:
            # Last point on grid (does not have to be Tend:)
            if(gridWidth <> None):
                dOutput = gridWidth
            else:
                dOutput = (Tend - self.t0) / nIntervals
        else:
            dOutput = Tend

        outputStepCounter = long(1)
        nextOutputPoint = min(self.t0 + dOutput, Tend)

        while self.t_cur < Tend:

            # Time-Event detection and step time adjustment
            if nextTimeEvent is None or nextOutputPoint < nextTimeEvent:
                time_event = False
                self.t_cur = nextOutputPoint
            else:
                time_event = True
                self.t_cur = nextTimeEvent



            try:
#                #Integrator step
#                self.y_cur = solver.step(self.t_cur)
#                self.y_cur = np.array(self.y_cur)
#                state_event = False
                # Simulate




                # take a step to next output point:
                t_new, y_new = simulation.simulate(self.t_cur)  # 5, 10) #5, 10  self.t_cur self.t_cur  2. argument nsteps Simulate 5 seconds
                # t_new, y_new are both vectors of the time and states at t_cur and all intermediate
                # points before it! So take last values:
                self.t_cur = t_new[-1]
                self.y_cur = y_new[-1]
                state_event = False

            except:
                import sys
                print "Unexpected error:", sys.exc_info()[0]
#            except CVodeRootException, info:
#                self.t_cur = info.t
#                self.y_cur = info.y
#                self.y_cur = np.array(self.y_cur)
#                time_event = False
#                state_event = True
#
#
            # Depending on events have been detected do different tasks
            if time_event or state_event:
                event_info = [state_event, time_event]
                if not self.handle_event(self, event_info):
                    break
                solver.init(self.t_cur, self.y_cur)

                nextTimeEvent = self.time_events(self.t_cur, self.y_cur)
                # If no timeEvent happens:
                if nextTimeEvent <= self.t_cur:
                    nextTimeEvent = None

            if self.t_cur == nextOutputPoint:
                # Write output if not happened before:
                if not time_event and not state_event:
                    self.handle_result(nextOutputPoint, self.y_cur)
                outputStepCounter += 1
                nextOutputPoint = min(self.t0 + outputStepCounter * dOutput, Tend)

        self.finalize()
コード例 #10
0
def run_example(with_plots=True):
    r"""
    Example for the use of the stability limit detection algorithm
    in CVode.
    
    .. math::
       
        \dot y_1 &= y_2 \\
        \dot y_2 &= \mu ((1.-y_1^2) y_2-y_1) \\
        \dot y_3 &= sin(ty_2)

    with :math:`\mu=\frac{1}{5} 10^3`.

    on return:
    
       - :dfn:`exp_mod`    problem instance
    
       - :dfn:`exp_sim`    solver instance

    """
    class Extended_Problem(Explicit_Problem):
        order = []
        
        def handle_result(self, solver, t, y):
            Explicit_Problem.handle_result(self, solver, t, y)
            self.order.append(solver.get_last_order())
            
    eps = 5.e-3
    my = 1./eps
    
    #Define the rhs
    def f(t,y):
        yd_0 = y[1]
        yd_1 = my*((1.-y[0]**2)*y[1]-y[0])
        yd_2 = N.sin(t*y[1])
        
        return N.array([yd_0,yd_1, yd_2])
    
    y0 = [2.0,-0.6, 0.1] #Initial conditions
    
    #Define an Assimulo problem
    exp_mod = Extended_Problem(f,y0, 
                          name = "CVode: Stability problem")
    
    #Define an explicit solver
    exp_sim = CVode(exp_mod) #Create a CVode solver
    
    #Sets the parameters
    exp_sim.stablimdet = True
    exp_sim.report_continuously = True
    
    #Simulate
    t, y = exp_sim.simulate(2.0) #Simulate 2 seconds
    
    #Plot
    if with_plots:
        P.subplot(211)
        P.plot(t,y[:,2])
        P.ylabel("State: $y_1$")
        P.subplot(212)
        P.plot(t,exp_mod.order)
        P.ylabel("Order")
        P.suptitle(exp_mod.name)
        P.xlabel("Time")
        P.show()

    #Basic test
    x1 = y[:,0]
    assert N.abs(x1[-1]-1.8601438) < 1e-1 #For test purpose
    
    return exp_mod, exp_sim
コード例 #11
0
def run_simulation(filename, save_output, start_time, temp, RH, RO2_indices,
                   H2O, PInit, y_cond, input_dict, simulation_time, batch_step,
                   plot_mass):

    from assimulo.solvers import RodasODE, CVode, RungeKutta4, LSODAR  #Choose solver accoring to your need.
    from assimulo.problem import Explicit_Problem

    # In this function, we import functions that have been pre-compiled for use in the ODE solver
    # The function that calculates the RHS of the ODE is also defined within this function, such
    # that it can be used by the Assimulo solvers

    # The variables passed to this function are defined as follows:

    #-------------------------------------------------------------------------------------
    #-------------------------------------------------------------------------------------
    # define the ODE function to be called
    def dydt_func(t, y):
        """
        This function defines the right-hand side [RHS] of the ordinary differential equations [ODEs] to be solved
        input:
        • t - time variable [internal to solver]
        • y - array holding concentrations of all compounds in both gas and particulate [molecules/cc]
        output:
        dydt - the dy_dt of each compound in both gas and particulate phase [molecules/cc.sec]
        """

        dy_dt = numpy.zeros((total_length_y, 1), )

        #pdb.set_trace()
        # Calculate time of day
        time_of_day_seconds = start_time + t

        #pdb.set_trace()
        # make sure the y array is not a list. Assimulo uses lists
        y_asnumpy = numpy.array(y)
        Model_temp = temp
        #pdb.set_trace()
        #Calculate the concentration of RO2 species, using an index file created during parsing
        RO2 = numpy.sum(y[RO2_indices])

        #Calculate reaction rate for each equation.
        # Note that H2O will change in parcel mode
        # The time_of_day_seconds is used for photolysis rates - need to change this if want constant values
        rates = evaluate_rates_fortran(RO2, H2O, Model_temp,
                                       time_of_day_seconds)
        #pdb.set_trace()
        # Calculate product of all reactants and stochiometry for each reaction [A^a*B^b etc]
        reactants = reactants_fortran(y_asnumpy[0:num_species - 1])
        #pdb.set_trace()
        #Multiply product of reactants with rate coefficient to get reaction rate
        reactants = numpy.multiply(reactants, rates)
        #pdb.set_trace()
        # Now use reaction rates with the loss_gain matri to calculate the final dydt for each compound
        # With the assimulo solvers we need to output numpy arrays
        dydt_gas = loss_gain_fortran(reactants)
        #pdb.set_trace()

        dy_dt[0:num_species - 1, 0] = dydt_gas

        # Change the saturation vapour pressure of water
        # Need to re-think the change of organic vapour pressures with temperature.
        # At the moment this is kept constant as re-calulation using UManSysProp very slow
        sat_vap_water = numpy.exp((-0.58002206E4 / Model_temp) + 0.13914993E1 - \
        (0.48640239E-1 * Model_temp) + (0.41764768E-4 * (Model_temp**2.0E0))- \
        (0.14452093E-7 * (Model_temp**3.0E0)) + (0.65459673E1 * numpy.log(Model_temp)))
        sat_vp[-1] = (numpy.log10(sat_vap_water * 9.86923E-6))
        Psat = numpy.power(10.0, sat_vp)

        # Convert the concentration of each component in the gas phase into a partial pressure using the ideal gas law
        # Units are Pascals
        Pressure_gas = (y_asnumpy[0:num_species, ] /
                        NA) * 8.314E+6 * Model_temp  #[using R]

        core_mass_array = numpy.multiply(ycore_asnumpy / NA, core_molw_asnumpy)

        ####### Calculate the thermal conductivity of gases according to the new temperature ########
        K_water_vapour = (
            5.69 + 0.017 *
            (Model_temp - 273.15)) * 1e-3 * 4.187  #[W/mK []has to be in W/m.K]
        # Use this value for all organics, for now. If you start using a non-zero enthalpy of
        # vapourisation, this needs to change.
        therm_cond_air = K_water_vapour

        #----------------------------------------------------------------------------
        #F2c) Extract the current gas phase concentrations to be used in pressure difference calculations
        C_g_i_t = y_asnumpy[0:num_species, ]
        #Set the values for oxidants etc to 0 as will force no mass transfer
        #C_g_i_t[ignore_index]=0.0
        C_g_i_t = C_g_i_t[include_index]

        #pdb.set_trace()

        total_SOA_mass,aw_array,size_array,dy_dt_calc = dydt_partition_fortran(y_asnumpy,ycore_asnumpy,core_dissociation, \
        core_mass_array,y_density_array_asnumpy,core_density_array_asnumpy,ignore_index_fortran,y_mw,Psat, \
        DStar_org_asnumpy,alpha_d_org_asnumpy,C_g_i_t,N_perbin,gamma_gas_asnumpy,Latent_heat_asnumpy,GRAV, \
        Updraft,sigma,NA,kb,Rv,R_gas,Model_temp,cp,Ra,Lv_water_vapour)

        #pdb.set_trace()

        # Add the calculated gains/losses to the complete dy_dt array
        dy_dt[0:num_species + (num_species_condensed * num_bins),
              0] += dy_dt_calc[:]

        #pdb.set_trace()

        #----------------------------------------------------------------------------
        #F4) Now calculate the change in water vapour mixing ratio.
        #To do this we need to know what the index key for the very last element is
        #pdb.set_trace()
        #pdb.set_trace()
        #print "elapsed time=", elapsedTime
        dydt_func.total_SOA_mass = total_SOA_mass
        dydt_func.size_array = size_array
        dydt_func.temp = Model_temp
        dydt_func.RH = Pressure_gas[-1] / (Psat[-1] * 101325.0)
        dydt_func.water_activity = aw_array

        #----------------------------------------------------------------------------
        return dy_dt

    #-------------------------------------------------------------------------------------
    #-------------------------------------------------------------------------------------

    #import static compilation of Fortran functions for use in ODE solver
    print("Importing pre-compiled Fortran modules")
    from rate_coeff_f2py import evaluate_rates as evaluate_rates_fortran
    from reactants_conc_f2py import reactants as reactants_fortran
    from loss_gain_f2py import loss_gain as loss_gain_fortran
    from partition_f2py import dydt_partition as dydt_partition_fortran

    # 'Unpack' variables from input_dict
    species_dict = input_dict['species_dict']
    species_dict2array = input_dict['species_dict2array']
    species_initial_conc = input_dict['species_initial_conc']
    equations = input_dict['equations']
    num_species = input_dict['num_species']
    num_species_condensed = input_dict['num_species_condensed']
    y_density_array_asnumpy = input_dict['y_density_array_asnumpy']
    y_mw = input_dict['y_mw']
    sat_vp = input_dict['sat_vp']
    Delta_H = input_dict['Delta_H']
    Latent_heat_asnumpy = input_dict['Latent_heat_asnumpy']
    DStar_org_asnumpy = input_dict['DStar_org_asnumpy']
    alpha_d_org_asnumpy = input_dict['alpha_d_org_asnumpy']
    gamma_gas_asnumpy = input_dict['gamma_gas_asnumpy']
    Updraft = input_dict['Updraft']
    GRAV = input_dict['GRAV']
    Rv = input_dict['Rv']
    Ra = input_dict['Ra']
    R_gas = input_dict['R_gas']
    R_gas_other = input_dict['R_gas_other']
    cp = input_dict['cp']
    sigma = input_dict['sigma']
    NA = input_dict['NA']
    kb = input_dict['kb']
    Lv_water_vapour = input_dict['Lv_water_vapour']
    ignore_index = input_dict['ignore_index']
    ignore_index_fortran = input_dict['ignore_index_fortran']
    ycore_asnumpy = input_dict['ycore_asnumpy']
    core_density_array_asnumpy = input_dict['core_density_array_asnumpy']
    y_cond = input_dict['y_cond_initial']
    num_bins = input_dict['num_bins']
    core_molw_asnumpy = input_dict['core_molw_asnumpy']
    core_dissociation = input_dict['core_dissociation']
    N_perbin = input_dict['N_perbin']
    include_index = input_dict['include_index']

    # pdb.set_trace()

    #Specify some starting concentrations [ppt]
    Cfactor = 2.55e+10  #ppb-to-molecules/cc

    # Create variables required to initialise ODE
    y0 = [0] * (num_species + num_species_condensed * num_bins
                )  #Initial concentrations, set to 0
    t0 = 0.0  #T0

    # Define species concentrations in ppb fr the gas phase
    # You have already set this in the front end script, and now we populate the y array with those concentrations
    for specie in species_initial_conc.keys():
        if specie is not 'H2O':
            y0[species_dict2array[specie]] = species_initial_conc[
                specie] * Cfactor  #convert from pbb to molcules/cc
        elif specie is 'H2O':
            y0[species_dict2array[specie]] = species_initial_conc[specie]

    # Now add the initial condensed phase [including water]
    #pdb.set_trace()
    y0[num_species:num_species +
       ((num_bins) * num_species_condensed)] = y_cond[:]
    #pdb.set_trace()

    #Set the total_time of the simulation to 0 [havent done anything yet]
    total_time = 0.0

    # Define a 'key' that represents the end of the composition variables to track
    total_length_y = len(y0)
    key = num_species + ((num_bins) * num_species) - 1

    #pdb.set_trace()

    # Now run through the simulation in batches.
    # I do this to enable testing of coupling processes. Some initial investigations with non-ideality in
    # the condensed phase indicated that even defining a maximum step was not enough for ODE solvers to
    # overshoot a stable region. It also helps with in-simulation debugging. Its up to you if you want to keep this.
    # To not run in batches, just define one batch as your total simulation time. This will reduce any overhead with
    # initialising the solvers
    # Set total simulation time and batch steps in seconds

    # Note also that the current module outputs solver information after each batch step. This can be turned off and the
    # the batch step change for increased speed
    # simulation_time= 3600.0
    # batch_step=300.0
    t_array = []
    time_step = 0
    number_steps = int(
        simulation_time /
        batch_step)  # Just cycling through 3 steps to get to a solution

    # Define a matrix that stores values as outputs from the end of each batch step. Again, you can remove
    # the need to run in batches. You can tell the Assimulo solvers the frequency of outputs.
    y_matrix = numpy.zeros((int(number_steps), len(y0)))
    # Also define arrays and matrices that hold information such as total SOA mass
    SOA_matrix = numpy.zeros((int(number_steps), 1))
    size_matrix = numpy.zeros((int(number_steps), num_bins))

    print("Starting simulation")

    # In the following, we can
    while total_time < simulation_time:

        if total_time == 0.0:
            #Define an Assimulo problem
            #Define an explicit solver
            exp_mod = Explicit_Problem(dydt_func, y0, t0, name=filename)

        else:
            y0 = y_output[
                -1, :]  # Take the output from the last batch as the start of this
            exp_mod = Explicit_Problem(dydt_func, y0, t0, name=filename)

        # Define ODE parameters.
        # Initial steps might be slower than mid-simulation. It varies.
        #exp_mod.jac = dydt_jac
        # Define which ODE solver you want to use
        exp_sim = CVode(exp_mod)
        tol_list = [1.0e-2] * len(y0)
        exp_sim.atol = tol_list  #Default 1e-6
        exp_sim.rtol = 1.0e-4  #Default 1e-6
        exp_sim.inith = 1.0e-6  #Initial step-size
        #exp_sim.discr = 'Adams'
        exp_sim.maxh = 100.0
        # Use of a jacobian makes a big differece in simulation time. This is relatively
        # easy to define for a gas phase - not sure for an aerosol phase with composition
        # dependent processes.
        exp_sim.usejac = False  # To be provided as an option in future update.
        #exp_sim.fac1 = 0.05
        #exp_sim.fac2 = 50.0
        exp_sim.report_continuously = True
        exp_sim.maxncf = 1000
        #Sets the parameters
        t_output, y_output = exp_sim.simulate(
            batch_step)  #Simulate 'batch' seconds
        total_time += batch_step
        t_array.append(
            total_time
        )  # Save the output from the end step, of the current batch, to a matrix
        y_matrix[time_step, :] = y_output[-1, :]
        SOA_matrix[time_step, 0] = dydt_func.total_SOA_mass
        size_matrix[time_step, :] = dydt_func.size_array
        print("SOA [micrograms/m3] = ", dydt_func.total_SOA_mass)

        #now save this information into a matrix for later plotting.
        time_step += 1

    if save_output is True:

        print(
            "Saving the model output as a pickled object for later retrieval")
        # save the dictionary to a file for later retrieval - have to do each seperately.
        with open(filename + '_y_output.pickle', 'wb') as handle:
            pickle.dump(y_matrix, handle, protocol=pickle.HIGHEST_PROTOCOL)
        with open(filename + '_t_output.pickle', 'wb') as handle:
            pickle.dump(t_array, handle, protocol=pickle.HIGHEST_PROTOCOL)
        with open(filename + '_SOA_output.pickle', 'wb') as handle:
            pickle.dump(SOA_matrix, handle, protocol=pickle.HIGHEST_PROTOCOL)
        with open(filename + '_size_output.pickle', 'wb') as handle:
            pickle.dump(size_matrix, handle, protocol=pickle.HIGHEST_PROTOCOL)
        with open(filename + 'include_index.pickle', 'wb') as handle:
            pickle.dump(include_index,
                        handle,
                        protocol=pickle.HIGHEST_PROTOCOL)

    #pdb.set_trace()
    #Plot the change in concentration over time for a given specie. For the user to change / remove
    #In a future release I will add this as a seperate module
    if plot_mass is True:
        try:
            P.plot(t_array, SOA_matrix[:, 0], marker='o')
            P.title(exp_mod.name)
            P.ylabel("SOA mass [micrograms/m3]")
            P.xlabel("Time [seconds] since start of simulation")
            P.show()
        except:
            print(
                "There is a problem using Matplotlib in your environment. If using this within a docker container, you will need to transfer the data to the host or configure your container to enable graphical displays. More information can be found at http://wiki.ros.org/docker/Tutorials/GUI "
            )
コード例 #12
0
def run_example(with_plots=True):
    r"""
    This example shows how to use Assimulo and CVode for simulating sensitivities
    for initial conditions.

    .. math::
    
       \dot y_1 &= -(k_{01}+k_{21}+k_{31}) y_1 + k_{12} y_2 + k_{13} y_3 + b_1\\
       \dot y_2 &= k_{21} y_1 - (k_{02}+k_{12}) y_2 \\
       \dot y_3 &= k_{31} y_1 - k_{13} y_3
     
    with the parameter dependent inital conditions 
    :math:`y_1(0) = 0, y_2(0) = 0, y_3(0) = 0` . The initial values are taken as parameters :math:`p_1,p_2,p_3`
    for the computation of the sensitivity matrix, 
    see http://sundials.2283335.n4.nabble.com/Forward-sensitivities-for-initial-conditions-td3239724.html
    
    on return:
    
       - :dfn:`exp_mod`    problem instance
    
       - :dfn:`exp_sim`    solver instance
    
    """
    def f(t, y, p):
        y1, y2, y3 = y
        k01 = 0.0211
        k02 = 0.0162
        k21 = 0.0111
        k12 = 0.0124
        k31 = 0.0039
        k13 = 0.000035
        b1 = 49.3

        yd_0 = -(k01 + k21 + k31) * y1 + k12 * y2 + k13 * y3 + b1
        yd_1 = k21 * y1 - (k02 + k12) * y2
        yd_2 = k31 * y1 - k13 * y3

        return N.array([yd_0, yd_1, yd_2])

    #The initial conditions
    y0 = [0.0, 0.0, 0.0]  #Initial conditions for y
    p0 = [0.0, 0.0, 0.0]  #Initial conditions for parameters
    yS0 = N.array([[1, 0, 0], [0, 1, 0], [0, 0, 1.]])

    #Create an Assimulo explicit problem
    exp_mod = Explicit_Problem(f,
                               y0,
                               p0=p0,
                               name='Example: Computing Sensitivities')

    #Sets the options to the problem
    exp_mod.yS0 = yS0

    #Create an Assimulo explicit solver (CVode)
    exp_sim = CVode(exp_mod)

    #Sets the paramters
    exp_sim.iter = 'Newton'
    exp_sim.discr = 'BDF'
    exp_sim.rtol = 1e-7
    exp_sim.atol = 1e-6
    exp_sim.pbar = [
        1, 1, 1
    ]  #pbar is used to estimate the tolerances for the parameters
    exp_sim.report_continuously = True  #Need to be able to store the result using the interpolate methods
    exp_sim.sensmethod = 'SIMULTANEOUS'  #Defines the sensitvity method used
    exp_sim.suppress_sens = False  #Dont suppress the sensitivity variables in the error test.

    #Simulate
    t, y = exp_sim.simulate(400)  #Simulate 400 seconds

    #Basic test
    nose.tools.assert_almost_equal(y[-1][0], 1577.6552477, 5)
    nose.tools.assert_almost_equal(y[-1][1], 611.9574565, 5)
    nose.tools.assert_almost_equal(y[-1][2], 2215.88563217, 5)
    nose.tools.assert_almost_equal(exp_sim.p_sol[0][1][0], 1.0)

    #Plot
    if with_plots:
        title_text = r"Sensitivity w.r.t.  ${}$"
        legend_text = r"$\mathrm{{d}}{}/\mathrm{{d}}{}$"
        P.figure(1)
        P.subplot(221)
        P.plot(t,
               N.array(exp_sim.p_sol[0])[:, 0], t,
               N.array(exp_sim.p_sol[0])[:, 1], t,
               N.array(exp_sim.p_sol[0])[:, 2])
        P.title(title_text.format('p_1'))
        P.legend((legend_text.format('y_1',
                                     'p_1'), legend_text.format('y_1', 'p_2'),
                  legend_text.format('y_1', 'p_3')))
        P.subplot(222)
        P.plot(t,
               N.array(exp_sim.p_sol[1])[:, 0], t,
               N.array(exp_sim.p_sol[1])[:, 1], t,
               N.array(exp_sim.p_sol[1])[:, 2])
        P.title(title_text.format('p_2'))
        P.legend((legend_text.format('y_2',
                                     'p_1'), legend_text.format('y_2', 'p_2'),
                  legend_text.format('y_2', 'p_3')))
        P.subplot(223)
        P.plot(t,
               N.array(exp_sim.p_sol[2])[:, 0], t,
               N.array(exp_sim.p_sol[2])[:, 1], t,
               N.array(exp_sim.p_sol[2])[:, 2])
        P.title(title_text.format('p_3'))
        P.legend((legend_text.format('y_3',
                                     'p_1'), legend_text.format('y_3', 'p_2'),
                  legend_text.format('y_3', 'p_3')))
        P.subplot(224)
        P.title('ODE Solution')
        P.plot(t, y)
        P.suptitle(exp_mod.name)
        P.show()

        return exp_mod, exp_sim
コード例 #13
0
def run_example(with_plots=True):
    """
    This is the same example from the Sundials package (cvsRoberts_FSA_dns.c)

    This simple example problem for CVode, due to Robertson, 
    is from chemical kinetics, and consists of the following three 
    equations:

    .. math:: 
    
       \dot y_1 &= -p_1 y_1 + p_2 y_2 y_3 \\
       \dot y_2 &= p_1 y_1 - p_2 y_2 y_3 - p_3 y_2^2 \\
       \dot y_3 &= p_3  y_2^2
    
    on return:
    
       - :dfn:`exp_mod`    problem instance
    
       - :dfn:`exp_sim`    solver instance
    """
    def f(t, y, p):
        p3 = 3.0e7

        yd_0 = -p[0] * y[0] + p[1] * y[1] * y[2]
        yd_1 = p[0] * y[0] - p[1] * y[1] * y[2] - p3 * y[1]**2
        yd_2 = p3 * y[1]**2

        return N.array([yd_0, yd_1, yd_2])

    #The initial conditions
    y0 = [1.0, 0.0, 0.0]  #Initial conditions for y

    #Create an Assimulo explicit problem
    exp_mod = Explicit_Problem(f,
                               y0,
                               name='Sundials test example: Chemical kinetics')

    #Sets the options to the problem
    exp_mod.p0 = [0.040, 1.0e4]  #Initial conditions for parameters
    exp_mod.pbar = [0.040, 1.0e4]

    #Create an Assimulo explicit solver (CVode)
    exp_sim = CVode(exp_mod)

    #Sets the paramters
    exp_sim.iter = 'Newton'
    exp_sim.discr = 'BDF'
    exp_sim.rtol = 1.e-4
    exp_sim.atol = N.array([1.0e-8, 1.0e-14, 1.0e-6])
    exp_sim.sensmethod = 'SIMULTANEOUS'  #Defines the sensitvity method used
    exp_sim.suppress_sens = False  #Dont suppress the sensitivity variables in the error test.
    exp_sim.report_continuously = True

    #Simulate
    t, y = exp_sim.simulate(
        4, 400)  #Simulate 4 seconds with 400 communication points

    #Plot
    if with_plots:
        import pylab as P
        P.plot(t, y)
        P.xlabel('Time')
        P.ylabel('State')
        P.title(exp_mod.name)
        P.show()

    #Basic test
    nose.tools.assert_almost_equal(y[-1][0], 9.05518032e-01, 4)
    nose.tools.assert_almost_equal(y[-1][1], 2.24046805e-05, 4)
    nose.tools.assert_almost_equal(y[-1][2], 9.44595637e-02, 4)
    nose.tools.assert_almost_equal(
        exp_sim.p_sol[0][-1][0], -1.8761,
        2)  #Values taken from the example in Sundials
    nose.tools.assert_almost_equal(exp_sim.p_sol[1][-1][0], 2.9614e-06, 8)

    return exp_mod, exp_sim
コード例 #14
0
def run_simulation(filename, start_time, save_output, temp, RH, RO2_indices,
                   H2O, input_dict, simulation_time, batch_step):

    from assimulo.solvers import RodasODE, CVode  #Choose solver accoring to your need.
    from assimulo.problem import Explicit_Problem

    # In this function, we import functions that have been pre-compiled for use in the ODE solver
    # The function that calculates the RHS of the ODE is also defined within this function, such
    # that it can be used by the Assimulo solvers

    # The variables passed to this function are defined as follows:

    #-------------------------------------------------------------------------------------
    # define the ODE function to be called
    def dydt_func(t, y):
        """
        This function defines the right-hand side [RHS] of the ordinary differential equations [ODEs] to be solved
        input:
        • t - time variable [internal to solver]
        • y - array holding concentrations of all compounds in both gas and particulate [molecules/cc]
        output:
        dydt - the dy_dt of each compound in both gas and particulate phase [molecules/cc.sec]
        """

        #pdb.set_trace()
        # Calculate time of day
        time_of_day_seconds = start_time + t

        # make sure the y array is not a list. Assimulo uses lists
        y_asnumpy = numpy.array(y)

        #Calculate the concentration of RO2 species, using an index file created during parsing
        RO2 = numpy.sum(y[RO2_indices])

        #Calculate reaction rate for each equation.
        # Note that H2O will change in parcel mode
        # The time_of_day_seconds is used for photolysis rates - need to change this if want constant values
        rates = evaluate_rates_fortran(RO2, H2O, temp, time_of_day_seconds)
        #pdb.set_trace()
        # Calculate product of all reactants and stochiometry for each reaction [A^a*B^b etc]
        reactants = reactants_fortran(y_asnumpy)
        #pdb.set_trace()
        #Multiply product of reactants with rate coefficient to get reaction rate
        reactants = numpy.multiply(reactants, rates)
        #pdb.set_trace()
        # Now use reaction rates with the loss_gain matri to calculate the final dydt for each compound
        # With the assimulo solvers we need to output numpy arrays
        dydt = loss_gain_fortran(reactants)
        #pdb.set_trace()

        return dydt

    #-------------------------------------------------------------------------------------
    #-------------------------------------------------------------------------------------
    # define jacobian function to be called
    def jacobian(t, y):
        """
        This function defines Jacobian of the ordinary differential equations [ODEs] to be solved
        input:
        • t - time variable [internal to solver]
        • y - array holding concentrations of all compounds in both gas and particulate [molecules/cc]
        output:
        dydt_dydt - the N_compounds x N_compounds matrix of Jacobian values
        """

        # Different solvers might call jacobian at different stages, so we have to redo some calculations here
        # Calculate time of day
        time_of_day_seconds = start_time + t

        # make sure the y array is not a list. Assimulo uses lists
        y_asnumpy = numpy.array(y)

        #Calculate the concentration of RO2 species, using an index file created during parsing
        RO2 = numpy.sum(y[RO2_indices])

        #Calculate reaction rate for each equation.
        # Note that H2O will change in parcel mode
        rates = evaluate_rates_fortran(RO2, H2O, temp, time_of_day_seconds)
        #pdb.set_trace()
        # Now use reaction rates with the loss_gain matrix to calculate the final dydt for each compound
        # With the assimulo solvers we need to output numpy arrays
        dydt_dydt = jacobian_fortran(rates, y_asnumpy)
        #pdb.set_trace()
        return dydt_dydt

    #-------------------------------------------------------------------------------------

    #import static compilation of Fortran functions for use in ODE solver
    print("Importing pre-compiled Fortran modules")
    from rate_coeff_f2py import evaluate_rates as evaluate_rates_fortran
    from reactants_conc_f2py import reactants as reactants_fortran
    from loss_gain_f2py import loss_gain as loss_gain_fortran
    from jacobian_f2py import jacobian as jacobian_fortran

    # 'Unpack' variables from input_dict
    species_dict = input_dict['species_dict']
    species_dict2array = input_dict['species_dict2array']
    species_initial_conc = input_dict['species_initial_conc']
    equations = input_dict['equations']

    #Specify some starting concentrations [ppt]
    Cfactor = 2.55e+10  #ppb-to-molecules/cc

    # Create variables required to initialise ODE
    num_species = len(species_dict.keys())
    y0 = [0] * num_species  #Initial concentrations, set to 0
    t0 = 0.0  #T0

    # Define species concentrations in ppb
    # You have already set this in the front end script, and now we populate the y array with those concentrations
    for specie in species_initial_conc.keys():
        y0[species_dict2array[specie]] = species_initial_conc[
            specie] * Cfactor  #convert from pbb to molcules/cc

    #Set the total_time of the simulation to 0 [havent done anything yet]
    total_time = 0.0

    # Now run through the simulation in batches.
    # I do this to enable testing of coupling processes. Some initial investigations with non-ideality in
    # the condensed phase indicated that even defining a maximum step was not enough for ODE solvers to
    # overshoot a stable region. It also helps with in-simulation debugging. Its up to you if you want to keep this.
    # To not run in batches, just define one batch as your total simulation time. This will reduce any overhead with
    # initialising the solvers
    # Set total simulation time and batch steps in seconds

    # Note also that the current module outputs solver information after each batch step. This can be turned off and the
    # the batch step change for increased speed
    #simulation_time= 3600.0
    #batch_step=100.0
    t_array = []
    time_step = 0
    number_steps = int(
        simulation_time /
        batch_step)  # Just cycling through 3 steps to get to a solution

    # Define a matrix that stores values as outputs from the end of each batch step. Again, you can remove
    # the need to run in batches. You can tell the Assimulo solvers the frequency of outputs.
    y_matrix = numpy.zeros((int(number_steps), len(y0)))

    print("Starting simulation")

    # In the following, we can
    while total_time < simulation_time:

        if total_time == 0.0:
            #Define an Assimulo problem
            #Define an explicit solver
            exp_mod = Explicit_Problem(dydt_func, y0, t0, name=filename)

        else:
            y0 = y_output[
                -1, :]  # Take the output from the last batch as the start of this
            exp_mod = Explicit_Problem(dydt_func, y0, t0, name=filename)

        # Define ODE parameters.
        # Initial steps might be slower than mid-simulation. It varies.
        #exp_mod.jac = dydt_jac
        # Define which ODE solver you want to use
        exp_sim = CVode(exp_mod)
        tol_list = [1.0e-3] * num_species
        exp_sim.atol = tol_list  #Default 1e-6
        exp_sim.rtol = 0.03  #Default 1e-6
        exp_sim.inith = 1.0e-6  #Initial step-size
        #exp_sim.discr = 'Adams'
        exp_sim.maxh = 100.0
        # Use of a jacobian makes a big differece in simulation time. This is relatively
        # easy to define for a gas phase - not sure for an aerosol phase with composition
        # dependent processes.
        exp_sim.usejac = True  # To be provided as an option in future update.
        #exp_sim.fac1 = 0.05
        #exp_sim.fac2 = 50.0
        exp_sim.report_continuously = True
        exp_sim.maxncf = 1000
        #Sets the parameters
        t_output, y_output = exp_sim.simulate(
            batch_step)  #Simulate 'batch' seconds
        total_time += batch_step
        t_array.append(
            total_time
        )  # Save the output from the end step, of the current batch, to a matrix
        y_matrix[time_step, :] = y_output[-1, :]

        #now save this information into a matrix for later plotting.
        time_step += 1

    # Do you want to save the generated matrix of outputs?
    if save_output:
        numpy.save(filename + '_output', y_matrix)
        df = pd.DataFrame(y_matrix)
        df.to_csv(filename + "_output_matrix.csv")
        w = csv.writer(open(filename + "_output_names.csv", "w"))
        for specie, number in species_dict2array.items():
            w.writerow([specie, number])

    with_plots = True

    #pdb.set_trace()
    #Plot the change in concentration over time for a given specie. For the user to change / remove
    #In a future release I will add this as a seperate module
    if with_plots:

        try:
            P.plot(t_array,
                   numpy.log10(y_matrix[:, species_dict2array['APINENE']]),
                   marker='o',
                   label="APINENE")
            P.plot(t_array,
                   numpy.log10(y_matrix[:, species_dict2array['PINONIC']]),
                   marker='o',
                   label="PINONIC")
            P.title(exp_mod.name)
            P.legend(loc='upper left')
            P.ylabel("Concetration log10[molecules/cc]")
            P.xlabel("Time [seconds] since start of simulation")
            P.show()
        except:
            print(
                "There is a problem using Matplotlib in your environment. If using this within a docker container, you will need to transfer the data to the host or configure your container to enable graphical displays. More information can be found at http://wiki.ros.org/docker/Tutorials/GUI "
            )
コード例 #15
0
    def simulate(self, Tend, nIntervals, gridWidth):

        # define assimulo problem:(has to be done here because of the starting value in Explicit_Problem
        solver = Explicit_Problem(self.rhs, self.y0)
        ''' *******DELETE LATER '''''''''
#        problem.handle_event = handle_event
#        problem.state_events = state_events
#        problem.init_mode = init_mode

        solver.handle_result = self.handle_result


        solver.name = 'Simple Explicit Example'
        simulation = CVode(solver)  # Create a RungeKutta34 solver
        # simulation.inith = 0.1 #Sets the initial step, default = 0.01

        # Change multistep method: 'adams' or 'VDF'
        if self.discr == 'Adams':
            simulation.discr = 'Adams'
            simulation.maxord = 12
        else:
            simulation.discr = 'BDF'
            simulation.maxord = 5

        # Change iteration algorithm: functional(FixedPoint) or newton
        if self.iter == 'FixedPoint':
            simulation.iter = 'FixedPoint'
        else:
            simulation.iter = 'Newton'

        # Sets additional parameters
        simulation.atol = self.atol
        simulation.rtol = self.rtol
        simulation.verbosity = 0
        if hasattr(simulation, 'continuous_output'):
            simulation.continuous_output = False  # default 0, if one step approach should be used
        elif hasattr(simulation, 'report_continuously'):
            simulation.report_continuously = False  # default 0, if one step approach should be used

        # Create Solver and set settings
#        noRootFunctions = np.size(self.state_events(self.t0, np.array(self.y0)))

#        solver = sundials.CVodeSolver(RHS = self.f, ROOT = self.rootf, SW = [False]*noRootFunctions,
#                       abstol = self.atol, reltol = self.rtol)
        # solver.settings.JAC = None   #Add user-dependent jacobian here

        '''Initialize problem '''
#        solver.init(self.t0, self.y0)
        self.handle_result(self.t0, self.y0)
        nextTimeEvent = self.time_events(self.t0, self.y0)
        self.t_cur = self.t0
        self.y_cur = self.y0
        state_event = False
#
#
        if gridWidth <> None:
            nOutputIntervals = int((Tend - self.t0) / gridWidth)
        else:
            nOutputIntervals = nIntervals
        # Define step length depending on if gridWidth or nIntervals has been chosen
        if nOutputIntervals > 0:
            # Last point on grid (does not have to be Tend:)
            if(gridWidth <> None):
                dOutput = gridWidth
            else:
                dOutput = (Tend - self.t0) / nIntervals
        else:
            dOutput = Tend

        outputStepCounter = long(1)
        nextOutputPoint = min(self.t0 + dOutput, Tend)

        while self.t_cur < Tend:

            # Time-Event detection and step time adjustment
            if nextTimeEvent is None or nextOutputPoint < nextTimeEvent:
                time_event = False
                self.t_cur = nextOutputPoint
            else:
                time_event = True
                self.t_cur = nextTimeEvent



            try:
#                #Integrator step
#                self.y_cur = solver.step(self.t_cur)
#                self.y_cur = np.array(self.y_cur)
#                state_event = False
                # Simulate




                # take a step to next output point:
                t_new, y_new = simulation.simulate(self.t_cur)  # 5, 10) #5, 10  self.t_cur self.t_cur  2. argument nsteps Simulate 5 seconds
                # t_new, y_new are both vectors of the time and states at t_cur and all intermediate
                # points before it! So take last values:
                self.t_cur = t_new[-1]
                self.y_cur = y_new[-1]
                state_event = False

            except:
                import sys
                print "Unexpected error:", sys.exc_info()[0]
#            except CVodeRootException, info:
#                self.t_cur = info.t
#                self.y_cur = info.y
#                self.y_cur = np.array(self.y_cur)
#                time_event = False
#                state_event = True
#
#
            # Depending on events have been detected do different tasks
            if time_event or state_event:
                event_info = [state_event, time_event]
                if not self.handle_event(self, event_info):
                    break
                solver.init(self.t_cur, self.y_cur)

                nextTimeEvent = self.time_events(self.t_cur, self.y_cur)
                # If no timeEvent happens:
                if nextTimeEvent <= self.t_cur:
                    nextTimeEvent = None

            if self.t_cur == nextOutputPoint:
                # Write output if not happened before:
                if not time_event and not state_event:
                    self.handle_result(nextOutputPoint, self.y_cur)
                outputStepCounter += 1
                nextOutputPoint = min(self.t0 + outputStepCounter * dOutput, Tend)

        self.finalize()
コード例 #16
0
ファイル: ODE_solver.py プロジェクト: Mbex/PyBox
def run_simulation(filename, save_output, start_time, temp, RH, RO2_indices,
                   H2O, input_dict, simulation_time, batch_step):

    from assimulo.solvers import RodasODE, CVode  #Choose solver accoring to your need.
    from assimulo.problem import Explicit_Problem

    # In this function, we import functions that have been pre-compiled for use in the ODE solver
    # The function that calculates the RHS of the ODE is also defined within this function, such
    # that it can be used by the Assimulo solvers

    # In the standard Python version [not using Numba] I use Sparse matrix operations in calculating loss/gain of each compound.
    # This function loads the matrix created at the beginning of the module.
    def load_sparse_csr(filename):
        loader = numpy.load('loss_gain_' + filename + '.npz')
        return csr_matrix(
            (loader['data'], loader['indices'], loader['indptr']),
            shape=loader['shape'])

    def load_sparse_csr_reactants(filename):
        loader = numpy.load('reactants_indices_sparse_' + filename + '.npz')
        return csr_matrix(
            (loader['data'], loader['indices'], loader['indptr']),
            shape=loader['shape'])

    #-------------------------------------------------------------------------------------
    # define the ODE function to be called
    def dydt_func(t, y):
        """
        This function defines the right-hand side [RHS] of the ordinary differential equations [ODEs] to be solved
        input:
        • t - time variable [internal to solver]
        • y - array holding concentrations of all compounds in both gas and particulate [molecules/cc]
        output:
        dydt - the dy_dt of each compound in both gas and particulate phase [molecules/cc.sec]
        """

        #pdb.set_trace()
        #Here we use the pre-created Numba based functions to arrive at our value for dydt
        # Calculate time of day
        time_of_day_seconds = start_time + t

        # make sure the y array is not a list. Assimulo uses lists
        y_asnumpy = numpy.array(y)

        #pdb.set_trace()
        # reactants=numpy.zeros((equations),)

        #pdb.set_trace()
        #Calculate the concentration of RO2 species, using an index file created during parsing
        RO2 = numpy.sum(y[RO2_indices])

        #Calculate reaction rate for each equation.
        # Note that H2O will change in parcel mode [to be changed in the full aerosol mode]
        # The time_of_day_seconds is used for photolysis rates - need to change this if want constant values
        #pdb.set_trace()
        rates = evaluate_rates(time_of_day_seconds, RO2, H2O, temp,
                               numpy.zeros((equations)), numpy.zeros((63)))

        # Calculate product of all reactants and stochiometry for each reaction [A^a*B^b etc]
        reactants = reactant_product(y_asnumpy, equations,
                                     numpy.zeros((equations)))

        #Multiply product of reactants with rate coefficient to get reaction rate
        #pdb.set_trace()
        reactants = numpy.multiply(reactants, rates)

        # Now use reaction rates with the loss_gain information in a pre-created Numba file to calculate the final dydt for each compound
        dydt = dydt_eval(numpy.zeros((len(y_asnumpy))), reactants)

        #pdb.set_trace()

        ############ Development place-holder ##############
        # ----------------------------------------------------------------------------------
        # The following demonstrates the same procedure but using only Numpy and pure python
        # For the full MCM this is too slow, but is useful for demonstrations and testing

        #Calculate reaction rate for each equation.
        ## rates=test(time_of_day_seconds,RO2,H2O,temp)

        # Calculate product of all reactants and stochiometry for each reaction [A^a*B^b etc]
        # Take the approach of using sparse matrix operations from a python perspective
        # This approach uses the rule of logarithms and sparse matrix multiplication

        ##temp_array=reactants_indices_sparse @ numpy.log(y_asnumpy)
        ##indices=numpy.where(temp_array > 0.0)
        ##reactants[indices]=numpy.exp(temp_array[indices])

        #Multiply product of reactants with rate coefficient to get reaction rate
        ## reactants = numpy.multiply(reactants,rates)

        # Now use reaction rates with the loss_gain matri to calculate the final dydt for each compound
        # With the assimulo solvers we need to output numpy arrays

        ##dydt=numpy.array(loss_gain @ reactants)
        # ----------------------------------------------------------------------------------

        return dydt

    #-------------------------------------------------------------------------------------

    print(
        "Importing Numba modules [compiling if first import or clean build...please be patient]"
    )
    #import Numba functions for use in ODE solver
    from Rate_coefficients_numba import evaluate_rates
    from Reactants_conc_numba import reactants as reactant_product
    from Loss_Gain_numba import dydt as dydt_eval

    # 'Unpack' variables from input_dict
    species_dict = input_dict['species_dict']
    species_dict2array = input_dict['species_dict2array']
    species_initial_conc = input_dict['species_initial_conc']
    equations = input_dict['equations']

    # Set dive by zero to ignore for use of any sparse matrix multiplication
    numpy.errstate(divide='ignore')

    # --- For Numpy and pure Python runs ----
    # Load the sparse matrix used in calculating the reactant products and dydt function
    ## reactants_indices_sparse = load_sparse_csr_reactants(filename)
    ## loss_gain = load_sparse_csr(filename)

    #Specify some starting concentrations [ppt]
    Cfactor = 2.55e+10  #ppb-to-molecules/cc

    # Create variables required to initialise ODE
    num_species = len(species_dict.keys())
    y0 = [0] * num_species  #Initial concentrations, set to 0
    t0 = 0.0  #T0

    # Define species concentrations in ppb
    # You have already set this in the front end script, and now we populate the y array with those concentrations
    for specie in species_initial_conc.keys():
        y0[species_dict2array[specie]] = species_initial_conc[
            specie] * Cfactor  #convert from pbb to molcules/cc

    #Set the total_time of the simulation to 0 [havent done anything yet]
    total_time = 0.0

    # Now run through the simulation in batches.
    # I do this to enable testing of coupling processes. Some initial investigations with non-ideality in
    # the condensed phase indicated that even defining a maximum step was not enough for ODE solvers to
    # overshoot a stable region. It also helps with in-simulation debugging. Its up to you if you want to keep this.
    # To not run in batches, just define one batch as your total simulation time. This will reduce any overhead with
    # initialising the solvers
    # Set total simulation time and batch steps in seconds

    # Note also that the current module outputs solver information after each batch step. This can be turned off and the
    # the batch step change for increased speed
    # simulation_time= 3600.0 # seconds
    # batch_step=100.0 # seconds
    t_array = []
    time_step = 0
    number_steps = int(
        simulation_time /
        batch_step)  # Just cycling through 3 steps to get to a solution

    # Define a matrix that stores values as outputs from the end of each batch step. Again, you can remove
    # the need to run in batches. You can tell the Assimulo solvers the frequency of outputs.
    y_matrix = numpy.zeros((int(number_steps), len(y0)))

    print("Starting simulation")

    #pdb.set_trace()

    while total_time < simulation_time:

        if total_time == 0.0:
            #Define an Assimulo problem
            #Define an explicit solver
            #pdb.set_trace()
            exp_mod = Explicit_Problem(dydt_func, y0, t0, name=filename)

        else:
            y0 = y_output[
                -1, :]  # Take the output from the last batch as the start of this
            exp_mod = Explicit_Problem(dydt_func, y0, t0, name=filename)

        # Define ODE parameters.
        # Initial steps might be slower than mid-simulation. It varies.
        #exp_mod.jac = dydt_jac
        # Define which ODE solver you want to use
        exp_sim = CVode(exp_mod)
        tol_list = [1.0e-3] * num_species
        exp_sim.atol = tol_list  #Default 1e-6
        exp_sim.rtol = 1e-6  #Default 1e-6
        exp_sim.inith = 1.0e-6  #Initial step-size
        #exp_sim.discr = 'Adams'
        exp_sim.maxh = 100.0
        # Use of a jacobian makes a big differece in simulation time. This is relatively
        # easy to define for a gas phase - not sure for an aerosol phase with composition
        # dependent processes.
        exp_sim.usejac = False  # To be provided as an option in future update. See Fortran variant for use of Jacobian
        #exp_sim.fac1 = 0.05
        #exp_sim.fac2 = 50.0
        exp_sim.report_continuously = True
        exp_sim.maxncf = 1000
        #Sets the parameters
        t_output, y_output = exp_sim.simulate(
            batch_step)  #Simulate 'batch' seconds
        total_time += batch_step
        t_array.append(
            total_time
        )  # Save the output from the end step, of the current batch, to a matrix
        y_matrix[time_step, :] = y_output[-1, :]

        #pdb.set_trace()

        #now save this information into a matrix for later plotting.
        time_step += 1

    # Do you want to save the generated matrix of outputs?
    if save_output:
        numpy.save(filename + '_output', y_matrix)
        df = pd.DataFrame(y_matrix)
        df.to_csv(filename + "_output_matrix.csv")
        w = csv.writer(open(filename + "_output_names.csv", "w"))
        for specie, number in species_dict2array.items():
            w.writerow([specie, number])

    with_plots = True

    #pdb.set_trace()
    #Plot the change in concentration over time for a given specie. For the user to change / remove
    #In a future release I will add this as a seperate module
    if with_plots:
        try:
            P.plot(t_array,
                   numpy.log10(y_matrix[:, species_dict2array['APINENE']]),
                   marker='o',
                   label="APINENE")
            P.plot(t_array,
                   numpy.log10(y_matrix[:, species_dict2array['PINONIC']]),
                   marker='o',
                   label="PINONIC")
            P.title(exp_mod.name)
            P.legend(loc='upper left')
            P.ylabel("Concetration log10[molecules/cc]")
            P.xlabel("Time [seconds] since start of simulation")
            P.show()
        except:
            print(
                "There is a problem using Matplotlib in your environment. If using this within a docker container, you will need to transfer the data to the host or configure your container to enable graphical displays. More information can be found at http://wiki.ros.org/docker/Tutorials/GUI "
            )
コード例 #17
0
def run_example(with_plots=True):
    r"""
    This is the same example from the Sundials package (cvsRoberts_FSA_dns.c)
    Its purpose is to demonstrate the use of parameters in the differential equation.

    This simple example problem for CVode, due to Robertson
    see http://www.dm.uniba.it/~testset/problems/rober.php, 
    is from chemical kinetics, and consists of the system:
    
    .. math:: 
    
       \dot y_1 &= -p_1 y_1 + p_2 y_2 y_3 \\
       \dot y_2 &= p_1 y_1 - p_2 y_2 y_3 - p_3 y_2^2 \\
       \dot y_3 &= p_3  y_ 2^2
       
    
    on return:
    
       - :dfn:`exp_mod`    problem instance
    
       - :dfn:`exp_sim`    solver instance
    
    """
    
    def f(t, y, p):
        
        yd_0 = -p[0]*y[0]+p[1]*y[1]*y[2] 
        yd_1 = p[0]*y[0]-p[1]*y[1]*y[2]-p[2]*y[1]**2 
        yd_2 = p[2]*y[1]**2
        
        return N.array([yd_0,yd_1,yd_2])
        
    def jac(t,y, p):
        J = N.array([[-p[0], p[1]*y[2], p[1]*y[1]],
                     [p[0], -p[1]*y[2]-2*p[2]*y[1], -p[1]*y[1]],
                     [0.0, 2*p[2]*y[1],0.0]])
        return J
        
    def fsens(t, y, s, p):
        J = N.array([[-p[0], p[1]*y[2], p[1]*y[1]],
                     [p[0], -p[1]*y[2]-2*p[2]*y[1], -p[1]*y[1]],
                     [0.0, 2*p[2]*y[1],0.0]])
        P = N.array([[-y[0],y[1]*y[2],0],
                     [y[0], -y[1]*y[2], -y[1]**2],
                     [0,0,y[1]**2]])
        return J.dot(s)+P
    
    #The initial conditions
    y0 = [1.0,0.0,0.0]          #Initial conditions for y
    
    #Create an Assimulo explicit problem
    exp_mod = Explicit_Problem(f,y0, name='Robertson Chemical Kinetics Example')
    exp_mod.rhs_sens = fsens
    exp_mod.jac = jac
    
    #Sets the options to the problem
    exp_mod.p0 = [0.040, 1.0e4, 3.0e7]  #Initial conditions for parameters
    exp_mod.pbar = [0.040, 1.0e4, 3.0e7]

    #Create an Assimulo explicit solver (CVode)
    exp_sim = CVode(exp_mod)
    
    #Sets the solver paramters
    exp_sim.iter = 'Newton'
    exp_sim.discr = 'BDF'
    exp_sim.rtol = 1.e-4
    exp_sim.atol = N.array([1.0e-8, 1.0e-14, 1.0e-6])
    exp_sim.sensmethod = 'SIMULTANEOUS' #Defines the sensitvity method used
    exp_sim.suppress_sens = False       #Dont suppress the sensitivity variables in the error test.
    exp_sim.report_continuously = True

    #Simulate
    t, y = exp_sim.simulate(4,400) #Simulate 4 seconds with 400 communication points
    
    #Basic test
    nose.tools.assert_almost_equal(y[-1][0], 9.05518032e-01, 4)
    nose.tools.assert_almost_equal(y[-1][1], 2.24046805e-05, 4)
    nose.tools.assert_almost_equal(y[-1][2], 9.44595637e-02, 4)
    nose.tools.assert_almost_equal(exp_sim.p_sol[0][-1][0], -1.8761, 2) #Values taken from the example in Sundials
    nose.tools.assert_almost_equal(exp_sim.p_sol[1][-1][0], 2.9614e-06, 8)
    nose.tools.assert_almost_equal(exp_sim.p_sol[2][-1][0], -4.9334e-10, 12)
    
    #Plot
    if with_plots:
        P.plot(t, y)
        P.title(exp_mod.name)
        P.xlabel('Time')
        P.ylabel('State')
        P.show()  
        
    return exp_mod, exp_sim