Beispiel #1
0
    def jac(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(time_of_day_seconds, RO2, H2O, temp,
                               numpy.zeros((equations)), numpy.zeros((63)))
        #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_function(
            rates, y_asnumpy, numpy.zeros((len(y_asnumpy), len(y_asnumpy))))
        #pdb.set_trace()

        return dydt_dydt
Beispiel #2
0
    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