spectrochempy.ActionMassKinetics
- class ActionMassKinetics(reactions, species_concentrations, arrhenius, T=298.0, **kwargs)[source][source]
An object which stores a reaction network of elementary reactions.
It stores its rate parameterization, set(s) of initial concentrations, temperature profile(s), with methods for evaluating production rates and concentration profiles assuming action mass kinetic and closed reactor.
- Parameters:
reactions (‘dict’ or
list
ortuple
ofstr
) – Strings giving then_reactions
chemical equation of the network. Reactants and products must be separated by a"->"
or “→” symbol, The name of each species should match a key of thespecies
dictionary. Examples:"A + B -> C"
or"2A → D"
species :dict
orlist
ortuple
ofdict
Dictionary or list of dictionaries giving the initial concentrations for then_species
species.arrhenius (array-like) – Iterable of shape
n_reactions
x 1,n_reactions
x 2 orn_reactions
x 3 with either the isothermal rate constants (\(k_1\), …, \(k_n\)) or the Arrhenius rate parameters ((\(A_1\), \(b_1\), \(Ea_1\)), … (\(A_n\), \(b_n\), \(Ea_n\))) or ((\(A_1\), \(Ea_1\)), …)). If a 2-column iterable is provided the temperature exponents are set to 0.T (
float
,Quantity
,callable
orlist
ortuple
of , or None, optional default: None) – Temperature. If None, or not given, the system is considered isothermal and T = 298.0 If it is not a temperature quantity, the unit is assumed to be in Kelvin. A function can also be provided which output a temperatureT
in K vs. timet
.
Examples
# A simple A → B → C: >>> reactions = (“A -> B”, “B -> C”) >>> species_concentrations = {“A”: 1.0, “B”: 0.0, “C”: 0.0} >>> time = np.arange(0, 10) >>> k_exp = np.array(((1.0e8, 52.0e3), (1.0e8, 50.0e3))) >>> kin_exp = scp.ActionMassKinetics(reactions, species_concentrations, k_exp, T=298.0) >>> C_exp = kin_exp.integrate(time) >>> info_(f”Concentrations at t = 4 : {C_exp[4.].data}”) Concentrations at t = 4 : [[ 0.7355 0.1879 0.07666]]
# Several sets of experimental conditions can be used. In this case,
species
,T
# are set using lists or tuples of the same length, even if only one is changed: >>> species_concentrations = ({“A”: 1.0, “B”: 0.0, “C”: 0.0}, {“A”: 1.0, “B”: 0.0, “C”: 0.0}) >>> T = (298.0, 308.0) >>> time = (np.arange(0, 10), np.arange(0, 5)) >>> kin_exp = scp.ActionMassKinetics(reactions, species_concentrations, k_exp, T=T) >>> C_exp = kin_exp.integrate(time) >>> info_(f”Concentrations at {T[0]}K, t = 4 : {C_exp[0][4.].data}”) >>> info_(f”Concentrations at {T[1]}K, t = 4 : {C_exp[1][4.].data}”) Concentrations at 298.0K, t = 4 : [[ 0.7355 0.1879 0.07666]] Concentrations at 308.0K, t = 4 : [[ 0.5448 0.236 0.2192]]Attributes Summary
Stoichiometry matrix A.
Stoichiometry matrix B.
Concentrations.
Number of reaction reactions.
Number of species.
Components names.
Methods Summary
fit_to_concentrations
(self, Cexp, iexp, ...)Fit rate parameters and concentrations to a concentration profile.
integrate
(self, t[, k_dt, method, left_op, ...])Integrate the kinetic equations at times
t
.Attributes Documentation
- init_concentrations
Concentrations.
- n_reactions
Number of reaction reactions.
- n_species
Number of species.
- species
Components names.
Methods Documentation
- fit_to_concentrations(self, Cexp, iexp, i2iexp, dict_param_to_optimize, ivp_solver_kwargs=None, optimizer_kwargs=None)[source][source]
Fit rate parameters and concentrations to a concentration profile.
- Parameters:
Cexp (
NDDataset
orlist
ottuple
of NDDatasets) – Experimental concentration profiles on which to fit the model. each set of concentrations can contain more concentration profiles than those to fit.iexp (
int
) – Indexes of experimental concentration profiles on which the model will be fitted.i2iexp (
int
) – Correspondence between optimized (external) concentration profile and experimental concentration profile.dict_param_to_optimize (
dict
or None) – rate parameters to optimize. Keys should be ‘k[i].A’ and ‘k[i].Ea’ for pre-exponential factor.ivp_solver_kwargs (
dict
) – keyword arguments for the ode solver. Defaults are the same as forsolve_ivp
, except formethod=LSDOA
optimizer_kwargs (
dict
) – keyword arguments the optimization (seeminimize
).
- Returns:
dict
– A result dictionary.
- integrate(self, t, k_dt=None, method='LSODA', left_op=None, c_names=None, use_jac=False, atol=1e-6, rtol=1e-3, **kwargs)[source][source]
Integrate the kinetic equations at times
t
.This function computes and integrates the set of kinetic differential equations given the initial concentration values.
- Parameters:
t (array-like of shape (
t_points
,) or list or tuple of)`arrays-like`. – Iterable with time values or sets of timle values at which the concentrations are computed.
k_dt (
float
orNone') -- Resolution of the time grid used to compute `k(T(t))
. Used only for non isothermal reaction.method (
str
orOdeSolver
, optional, default:'LSODA'
) – Integration method to use:'LSODA'
(default): Adams/BDF method with automatic stiffness detection and switching.'RK45'
(default): Explicit Runge-Kutta method of order 5(4).'RK23'
: Explicit Runge-Kutta method of order 3(2).'DOP853'
: Explicit Runge-Kutta method of order 8.'Radau'
: Implicit Runge-Kutta method of the Radau IIA family of order 5.'BDF'
: Implicit multi-step variable-order (1 to 5) method based on a backward differentiation formula for the derivative approximation.
‘LSODA’ is generally faster and seems a good choice for the systems treated in scpy so far. Explicit Runge-Kutta methods (‘RK23’, ‘RK45’, ‘DOP853’) can be used for non-stiff problems and implicit methods (‘Radau’, ‘BDF’) for stiff problems. Among Runge-Kutta methods, ‘DOP853’ is recommended for solving with high precision (low values of
rtol
andatol
). If not sure, first try to run ‘RK45’. If it makes unusually many iterations, diverges, or fails, your problem is likely to be stiff and you should use ‘Radau’ or ‘BDF’. You can also pass an arbitrary class derived fromOdeSolver
which implements the solver.left_op (array_like, optional) – A (m x n_species) array to left multiply the (n_species, n_times) array obtained after integration:
C.T = left_op @ C.T
. Can be used to pool and/or remove some concentration profiles in/from the output matrix of concentrationsc_names (list of str) – List of names for each concentration profile. Used if
left_op
is not None to name/rename the output concentration profiles.use_jac (
Bool
) – Whether to use the jacobian. Useful for stiff problems, can slightly increase the execution time for non-stiff problems.atol, rtol (
float
orarray_like
, optional) – Relative and absolute tolerances. The solver keeps the local error estimates less thanatol + rtol * abs(y)
. Herertol
controls a relative accuracy (number of correct digits), whileatol
controls absolute accuracy (number of correct decimal places). To achieve the desiredrtol
, setatol < min(rtol * C)
so thatrtol
dominates the allowable error. Ifatol > rtol * C
the number of correct digits is not guaranteed. Conversely, to achieve the desiredatol
setrtol
such thatmax(rtol * C) < atol
is always smaller than atol. If components of C have different scales, it might be beneficial to set different atol values for different components by passing array_like with shape (n_species,) for atol. Default values arertol=1e-3
andatol=1e-6
.**kwargs – Additional keyword parameters. See Other Parameters.
- Other Parameters:
- Returns:
C (
ndarray
orNDDataset
, shape (t_points
,n_species
)) – Values of the solution at timest
.meta (Bunch object with the following fields defined:) –
t : ndarray, shape (t_points,) Time points.
sol :
OdeSolution
or None Found solution asOdeSolution
instance; None ifdense_output
was set to False.t_events :
list
ofndarray
orNone
Contains for each event type a list of arrays at which an event of that type event was detected.None
if events` was None.y_events :
list
ofndarray
orNone
For each value oft_events
, the corresponding value of the solution.None
if events wasNone
.nfev :
int
Number of evaluations of the right-hand side.njev :
int
Number of evaluations of the Jacobian.nlu :
int
Number of LU decompositions.status :
int
Reason for a successful algorithm termination:0 : The solver successfully reached the end of
tspan
.1 : A termination event occurred.
message :
str
Human-readable description of the termination reason.
Examples using spectrochempy.ActionMassKinetics