13#ifndef CHEMISTRY_REACTOR_H
14#define CHEMISTRY_REACTOR_H
16#include <Kokkos_Core.hpp>
43 if (n < 31)
return false;
45 return pattern.
nnz <= 0.6 *
static_cast<double>(n) * n;
66template <
typename MemorySpace = Kokkos::DefaultExecutionSpace::memory_space>
88 template <
typename Lanes>
92 return y[k] < -atol_Y - forcing_time * Kokkos::fmax(0.0, -forcing[k]) ? 1.0 : 0.0;
97 lanes.sum(
thermo.n_species, [&](
const uint32_t k) { return y[k] < -atol_Y ? 1.0 : 0.0; });
101 template <
typename Lanes>
106 template <
typename Lanes>
112 template <
typename Lanes>
115 lanes.sum(
thermo.n_species, [&](
const uint32_t k) { return (cp_R[k] - 1.0) * thermo.inv_W(k) * forcing[k]; });
128 template <
typename Lanes,
typename Powers>
130 const Powers & p,
const double cv,
const double inv_rho_cv,
131 const double dT_dt,
const double * h_RT,
const double * cp_R,
165 const double W_k = 1.0 /
thermo.inv_W(k);
173 template <
typename Lanes>
178 double * h_RT =
g_RT +
ns;
179 double * cp_R = h_RT +
ns;
186 double * partial =
q +
nr +
kinetics.derivatives_size();
188 const double T = y[
ns];
192 h_RT[k] =
thermo.h_RT(k, p);
194 cp_R[k] =
thermo.cp_R(k, p);
211 const double sum_e_g = lanes.sum(
215 lanes.single([&]() { f[
ns] =
dT_dt; });
221 compact_jacobian(lanes, y,
J, T, p, cv,
inv_rho_cv,
dT_dt, h_RT, cp_R,
omega,
domega_dT, d);
231 constexpr bool CM = Lanes::column_major;
254 const double W_k = 1.0 /
thermo.inv_W(k);
269template <
typename MemorySpace>
281template <
typename MemorySpace>
307template <
bool Sparse = false,
typename Lanes,
typename MemorySpace,
typename Observer = NoObserver>
310 const double dt,
double * Y,
double & T,
double & h,
314 double *
fast =
nullptr,
const double * forcing =
nullptr) {
317 double *
scratch =
J + (
Sparse ? sparse->jacobian_size() : n * n);
324 lanes.for_each(
ns, [&](
const uint32_t k) { y[k] = Kokkos::fmax(Y[k], 0.0); });
326 const double negative = lanes.sum(
ns, [&](
const uint32_t k) {
return Y[k] < 0.0 ? 1.0 : 0.0; });
331 const double sum = lanes.sum(
ns, [&](
const uint32_t k) {
return y[k]; });
332 lanes.for_each(
ns, [&](
const uint32_t k) { y[k] /= sum; });
336 lanes.single([&]() { y[
ns] =
T0; });
342 double * z = values + sparse->nnz;
346 u, sparse, forcing, dt};
348 result =
integrate(lanes, reactor,
solver, 0.0, dt, y, h, options.
integrator,
J,
vectors,
352 nullptr,
nullptr, forcing, dt};
354 result =
integrate(lanes, reactor,
solver, 0.0, dt, y, h, options.
integrator,
J,
vectors,
357 lanes.for_each(
ns, [&](
const uint32_t k) { y[k] = Kokkos::fmax(y[k], 0.0); });
359 const double total = lanes.sum(
ns, [&](
const uint32_t k) {
return y[k]; });
360 lanes.for_each(
ns, [&](
const uint32_t k) { Y[k] = y[k] / total; });
363 const double growth = 1.0 + dt * lanes.sum(
ns, [&](
const uint32_t k) {
return forcing[k]; });
372template <
typename MemorySpace,
typename Observer = NoObserver>
375 const double dt,
double * Y,
double & T,
double & h,
380 return advance_reactor<true>(
SerialLanes(), thermo, kinetics, rho, dt, Y, T, h, options, work, pivot,
383 return advance_reactor<false>(
SerialLanes(), thermo, kinetics, rho, dt, Y, T, h, options, work, pivot,
399 double t[3] = {0.0, 0.0, 0.0};
400 double g[3] = {0.0, 0.0, 0.0};
404 for (
int i = 0;
i < 2;
i++) {
410 if (++
count < 3 || !(
g[1] >
peak &&
g[1] >=
g[0] &&
g[1] >=
g[2]))
return;
412 const double num = (
t[1] -
t[0]) * (
t[1] -
t[0]) * (
g[1] -
g[2]) - (
t[1] -
t[2]) * (
t[1] -
t[2]) * (
g[1] -
g[0]);
413 const double den = (
t[1] -
t[0]) * (
g[1] -
g[2]) - (
t[1] -
t[2]) * (
g[1] -
g[0]);
KOKKOS_INLINE_FUNCTION rtype dot(const rtype *a, const rtype *b)
Compute the dot product of two arrays.
Definition common_math.h:32
Device tables of a mechanism's reactions: rates of progress, production rates and their analytical de...
KOKKOS_INLINE_FUNCTION RosenbrockResult advance_reactor(const Lanes &lanes, const ThermoTable< MemorySpace > &thermo, const KineticsTable< MemorySpace > &kinetics, const double rho, const double dt, double *Y, double &T, double &h, const ReactorOptions &options, double *work, uint32_t *pivot, Observer &&observer=Observer(), const SparseLUPattern< MemorySpace > *sparse=nullptr, double *fast=nullptr, const double *forcing=nullptr)
Advance one adiabatic constant-volume reactor over dt: integrate (Y, T), then clip negative mass frac...
Definition reactor.h:308
bool use_sparse_lu(const ReactorOptions &options, const Mechanism &mechanism)
Whether a mechanism's reactors use the sparse LU: as options.sparse says, or automatically from 30 sp...
Definition reactor.h:40
constexpr double GAS_CONSTANT
Definition mechanism.h:26
KOKKOS_INLINE_FUNCTION RosenbrockResult integrate(const Lanes &lanes, const System &system, const Solver &solver, const double t_start, const double t_end, double *y, double &h, const RosenbrockOptions &options, double *J, double *vectors, Observer &&observer=Observer())
Integrate dy/dt = f(y) from t_start to t_end with adaptive RODAS steps and error control sqrt(mean((e...
Definition rosenbrock.h:262
KOKKOS_INLINE_FUNCTION uint32_t reactor_fast_size(const KineticsTable< MemorySpace > &kinetics, const SparseLUPattern< MemorySpace > *sparse=nullptr)
Doubles of the part of advance_reactor()'s work memory that can be in fast (team scratch) memory: the...
Definition reactor.h:270
KOKKOS_INLINE_FUNCTION constexpr uint32_t rosenbrock_vectors_size(const uint32_t n)
Doubles of the vectors integrate() needs for a system of size n (besides the dense J).
Definition rosenbrock.h:176
KOKKOS_INLINE_FUNCTION uint32_t reactor_work_size(const KineticsTable< MemorySpace > &kinetics, const SparseLUPattern< MemorySpace > *sparse=nullptr)
Doubles of work memory advance_reactor() needs, the linear solver's included: dense,...
Definition reactor.h:282
Adaptive Rosenbrock integrator (RODAS) with dense LU, for one stiff autonomous ODE system per thread.
Static-pattern sparse LU of the reactor's Newton matrix for large mechanisms, with the dense rank-one...
Adiabatic, constant-volume reactor at density rho with state y = (Y_1 .. Y_Ns, T): dY_k/dt = W_k omeg...
Definition reactor.h:67
double * scratch
Definition reactor.h:72
KOKKOS_INLINE_FUNCTION void evaluate(const Lanes &lanes, const double *y, double *f, double *J) const
Definition reactor.h:174
KOKKOS_INLINE_FUNCTION bool admissible(const Lanes &lanes, const double *y) const
Definition reactor.h:89
KOKKOS_INLINE_FUNCTION void compact_jacobian(const Lanes &lanes, const double *y, double *J, const double T, const Powers &p, const double cv, const double inv_rho_cv, const double dT_dt, const double *h_RT, const double *cp_R, const double *omega, const double *domega_dT, const ReactionDerivatives &d) const
f(y) and, if J is not null, the Jacobian df/dy (in the lanes' layout, see dense_index),...
Definition reactor.h:129
const SparseLUPattern< MemorySpace > * sparse
Definition reactor.h:74
KOKKOS_INLINE_FUNCTION double forcing_temperature_derivative(const Lanes &lanes, const double *cp_R) const
d(sum_k e_k g_k) / dT = sum_k cv_k g_k.
Definition reactor.h:113
KOKKOS_INLINE_FUNCTION uint32_t size() const
Definition reactor.h:84
KOKKOS_INLINE_FUNCTION void rhs_jacobian(const Lanes &lanes, const double *y, double *f, double *J) const
Definition reactor.h:107
const KineticsTable< MemorySpace > & kinetics
Definition reactor.h:69
double * rank_one
Definition reactor.h:73
static KOKKOS_INLINE_FUNCTION uint32_t scratch_size(const KineticsTable< MemorySpace > &kinetics)
Definition reactor.h:79
const double * forcing
Definition reactor.h:75
double rho
Definition reactor.h:70
double forcing_time
Definition reactor.h:76
double atol_Y
Definition reactor.h:71
KOKKOS_INLINE_FUNCTION void rhs(const Lanes &lanes, const double *y, double *f) const
Definition reactor.h:102
KOKKOS_INLINE_FUNCTION double atol(const uint32_t i) const
Definition reactor.h:86
const ThermoTable< MemorySpace > & thermo
Definition reactor.h:68
Dense linear solver of integrate(): A = diagonal I - J factored by lu_factor in n * n doubles (LU,...
Definition rosenbrock.h:153
Ignition delay as the time of the maximum of dT/dt: the vertex of the parabola through the largest sa...
Definition reactor.h:394
double t_offset
Definition reactor.h:396
KOKKOS_INLINE_FUNCTION void operator()(const double time, const double *, const double *f)
Definition reactor.h:403
double g[3]
Definition reactor.h:400
double t_ignition
Definition reactor.h:397
uint32_t index
Definition reactor.h:395
double peak
Definition reactor.h:398
double t[3]
Definition reactor.h:399
uint32_t count
Definition reactor.h:401
KOKKOS_INLINE_FUNCTION bool ignited() const
Whether the reactor has ignited, at t_ignition.
Definition reactor.h:418
Reactions of a mechanism as flat device tables (compressed rows per reaction), in MemorySpace....
Definition kinetics.h:70
uint32_t n_species
Definition kinetics.h:74
Mass fractions held in a contiguous array.
Definition thermo.h:28
One ideal-gas phase of a Cantera YAML file: its elements, species and reactions.
Definition mechanism.h:145
uint32_t n_species() const
Definition mechanism.h:152
Per-reaction derivatives of the rates of progress, in one block of KineticsTable::derivatives_size() ...
Definition kinetics.h:35
static KOKKOS_INLINE_FUNCTION ReactionDerivatives at(double *work, const uint32_t n_reactions, const uint32_t n_forward, const uint32_t n_reverse)
Definition kinetics.h:43
double * dq_dT
Definition kinetics.h:36
int sparse
Definition reactor.h:30
double C_reg
Definition reactor.h:31
double atol_Y
Definition reactor.h:29
RosenbrockOptions integrator
Definition reactor.h:28
Definition rosenbrock.h:103
Definition rosenbrock.h:115
One thread does all the work of a cell.
Definition lanes.h:38
The pattern of a reactor Jacobian in (Y_1 .. Y_Ns, T) without its rank-one part, an elimination order...
Definition sparse_lu.h:46
uint32_t nnz
Definition sparse_lu.h:51
The linear solver of integrate() for SparseLUPattern: factor() gathers diagonal I - J_s from the comp...
Definition sparse_lu.h:449
Species thermodynamics of a mechanism on the device, in double precision in every build: per species ...
Definition thermo.h:43
KOKKOS_INLINE_FUNCTION void e_cv(const double T, const F_Y &y, double &e, double &cv) const
Mixture internal energy e [J/kg] and cv [J/(kg K)] at T.
Definition thermo.h:143
static KOKKOS_INLINE_FUNCTION Powers powers(const double T)
Definition thermo.h:59
KOKKOS_INLINE_FUNCTION double T_from_e(const double e, const F_Y &y, const double T_guess) const
Temperature at which the mixture has internal energy e: Newton from T_guess (300 K if outside the bra...
Definition thermo.h:161
uint32_t n_species
Definition thermo.h:44
CellSetup< ExecSpace > c
Definition teno_setup.cpp:1810
uint8_t r
Definition teno_setup.cpp:687
Scratch< Space > scratch
Definition teno_setup.cpp:680
uint16_t ns
Definition teno_setup.cpp:688
Device tables of species thermodynamics and thermally perfect mixture properties.