Branch data Line data Source code
1 : : // *****************************************************************************
2 : : /*!
3 : : \file src/DiffEq/Beta/MixMassFractionBeta.hpp
4 : : \copyright 2012-2015 J. Bakosi,
5 : : 2016-2018 Los Alamos National Security, LLC.,
6 : : 2019-2021 Triad National Security, LLC.
7 : : All rights reserved. See the LICENSE file for details.
8 : : \brief System of mix mass-fraction beta SDEs
9 : : \details This file implements the time integration of a system of stochastic
10 : : differential equations (SDEs) with linear drift and quadratic diagonal
11 : : diffusion, whose invariant is the joint [beta
12 : : distribution](http://en.wikipedia.org/wiki/Beta_distribution). There are two
13 : : differences compared to the plain beta SDE (see DiffEq/Beta.h):
14 : :
15 : : - First, the parameters, b, and kappa are specified via functions that
16 : : constrain the beta SDE to be consistent with the turbulent mixing process.
17 : : In particular, the SDE is made consistent with the no-mix and fully mixed
18 : : limits. See, e.g., MixMassFractionBetaCoeffConst::update().
19 : :
20 : : - Second, there two additional random variables computed, the same as also
21 : : computed by the mass-fraction beta equation, see also
22 : : DiffEq/MassFractionBeta.h.
23 : :
24 : : In a nutshell, the equation integrated governs a set of scalars,
25 : : \f$0\!\le\!Y_\alpha\f$, \f$\alpha\!=\!1,\dots,N\f$, as
26 : :
27 : : @m_class{m-show-m}
28 : :
29 : : \f[
30 : : \mathrm{d}Y_\alpha(t) = \frac{b_\alpha}{2}\left(S_\alpha - Y_\alpha\right)
31 : : \mathrm{d}t + \sqrt{\kappa_\alpha Y_\alpha(1-Y_\alpha)}
32 : : \mathrm{d}W_\alpha(t), \qquad \alpha=1,\dots,N
33 : : \f]
34 : :
35 : : @m_class{m-hide-m}
36 : :
37 : : \f[ \begin{split}
38 : : \mathrm{d}Y_\alpha(t) = \frac{b_\alpha}{2}\left(S_\alpha - Y_\alpha\right)
39 : : \mathrm{d}t + \sqrt{\kappa_\alpha Y_\alpha(1-Y_\alpha)}
40 : : \mathrm{d}W_\alpha(t), \\ \alpha=1,\dots,N
41 : : \end{split} \f]
42 : :
43 : : with parameter vectors \f$b_\alpha = \Theta b'_\alpha > 0\f$, \f$
44 : : \newcommand{\irv}[1]{\langle{#1^2}\rangle} \kappa_\alpha = \kappa' \irv{x} >
45 : : 0\f$, and \f$0 < S_\alpha < 1\f$. This is similar to DiffEq/Beta.h, but the
46 : : parameters, \f$b\f$ and \f$\kappa\f$ constrained. Here \f$
47 : : \newcommand{\irv}[1]{\langle{#1^2}\rangle}
48 : : \newcommand{\irmean}[1]{{\langle{#1}\rangle}} \Theta = 1 - \irv{x} /
49 : : [ \irmean{Y} (1-\irmean{Y}) ]\f$. The fluctuation about the mean, \f$
50 : : \newcommand{\irmean}[1]{{\langle{#1}\rangle}} \irmean{Y} \f$, is defined as
51 : : usual: \f$ \newcommand{\irmean}[1]{{\langle{#1}\rangle}} x = Y - \irmean{Y}
52 : : \f$, and \f$b'\f$ and \f$ \kappa'\f$ are user-specified constants. Also,
53 : : \f$\mathrm{d}W_\alpha(t)\f$ is an isotropic vector-valued
54 : : [Wiener process](http://en.wikipedia.org/wiki/Wiener_process) with
55 : : independent increments. The invariant distribution is the joint beta
56 : : distribution. This system of SDEs consists of N independent equations. For
57 : : more on the beta SDE, see https://doi.org/10.1080/14685248.2010.510843.
58 : :
59 : : In addition to integrating the above SDE, there are two additional functions
60 : : of \f$ Y_\alpha \f$ are computed as
61 : : \f[ \begin{aligned}
62 : : \rho(Y_\alpha) & = \frac{ \rho_{2\alpha} }{ 1 + r_\alpha Y_\alpha } \\
63 : : V(Y_\alpha) & = \frac{1}{ \rho(Y_\alpha) }
64 : : \end{aligned} \f]
65 : : These equations compute the instantaneous mixture density, \f$ \rho \f$, and
66 : : instantaneous specific volume, \f$ V_\alpha \f$, for equation \f$ \alpha \f$
67 : : in the system. These quantities are used in binary mixing of
68 : : variable-density turbulence between two fluids with constant densities, \f$
69 : : \rho_1, \f$ and \f$ \rho_2 \f$. The additional parameters, \f$ \rho_2 \f$
70 : : and \f$ r \f$ are user input parameters and kept constant during
71 : : integration. Since we compute the above variables, \f$\rho,\f$ and \f$V\f$,
72 : : and call them mixture density and specific volume, respectively, \f$Y\f$,
73 : : governed by the beta SDE is a mass fraction.
74 : :
75 : : _All of this is unpublished, but will be linked in here once published_.
76 : : */
77 : : // *****************************************************************************
78 : : #ifndef MixMassFractionBeta_h
79 : : #define MixMassFractionBeta_h
80 : :
81 : : #include <vector>
82 : : #include <tuple>
83 : :
84 : : #include "InitPolicy.hpp"
85 : : #include "MixMassFractionBetaCoeffPolicy.hpp"
86 : : #include "RNG.hpp"
87 : : #include "Particles.hpp"
88 : : #include "Table.hpp"
89 : : #include "CoupledEq.hpp"
90 : : #include "HydroTimeScales.hpp"
91 : : #include "HydroProductions.hpp"
92 : : #include "Walker/Options/HydroTimeScales.hpp"
93 : : #include "Walker/Options/HydroProductions.hpp"
94 : :
95 : : namespace walker {
96 : :
97 : : extern ctr::InputDeck g_inputdeck;
98 : : extern std::map< tk::ctr::RawRNGType, tk::RNG > g_rng;
99 : :
100 : : //! \brief MixMassFractionBeta SDE used polymorphically with DiffEq
101 : : //! \details The template arguments specify policies and are used to configure
102 : : //! the behavior of the class. The policies are:
103 : : //! - Init - initialization policy, see DiffEq/InitPolicy.h
104 : : //! - Coefficients - coefficients policy, see
105 : : //! DiffEq/MixMassFractionBetaCoeffPolicy.h
106 : : template< class Init, class Coefficients >
107 : : class MixMassFractionBeta {
108 : :
109 : : private:
110 : : using ncomp_t = tk::ctr::ncomp_t;
111 : : using eq = tag::mixmassfracbeta;
112 : :
113 : : public:
114 : : //! Number of derived variables computed by the SDE
115 : : //! \details Derived variables: density, specific volume, 1 - mass fraction
116 : : //! \warning If you change this, you must also change the constant in
117 : : //! Velocity::m_mixmassfracbeta_ncomp in DiffEq/Velocity/Velocity.hpp.
118 : : //! To see where, search for NUMDERIVED there.
119 : : static const std::size_t NUMDERIVED = 3;
120 : :
121 : : //! \brief Constructor
122 : : //! \param[in] c Index specifying which system of mix mass-fraction beta
123 : : //! SDEs to construct. There can be multiple mixmassfracbeta ... end blocks
124 : : //! in a control file. This index specifies which mix mass-fraction beta
125 : : //! SDE system to instantiate. The index corresponds to the order in which
126 : : //! the mixmassfracbeta ... end blocks are given the control file.
127 : 15 : explicit MixMassFractionBeta( ncomp_t c ) :
128 : : m_c( c ),
129 : 15 : m_depvar( g_inputdeck.get< tag::param, eq, tag::depvar >().at(c) ),
130 : : // divide by the number of derived variables computed, see derived()
131 : 15 : m_ncomp( g_inputdeck.get< tag::component >().get< eq >().at(c) /
132 : : (NUMDERIVED + 1) ),
133 : 15 : m_offset( g_inputdeck.get< tag::component >().offset< eq >(c) ),
134 [ + - ]: 15 : m_rng( g_rng.at( tk::ctr::raw(
135 : 15 : g_inputdeck.get< tag::param, eq, tag::rng >().at(c) ) ) ),
136 : 15 : m_solve( g_inputdeck.get< tag::param, eq, tag::solve >().at(c) ),
137 : 15 : m_velocity_coupled( coupled< eq, tag::velocity >( c ) ),
138 : 15 : m_velocity_depvar( depvar< eq, tag::velocity >( c ) ),
139 : 30 : m_velocity_offset( offset< eq, tag::velocity, tag::velocity_id >( c ) ),
140 : : m_velocity_solve(
141 : 15 : m_velocity_coupled ?
142 : 0 : g_inputdeck.get< tag::param, tag::velocity, tag::solve >().at(
143 : : system_id< eq, tag::velocity, tag::velocity_id >( c ) ) :
144 : : ctr::DepvarType::FULLVAR ),
145 : 15 : m_dissipation_coupled( coupled< eq, tag::dissipation >( c ) ),
146 : 15 : m_dissipation_depvar( depvar< eq, tag::dissipation >( c ) ),
147 : : m_dissipation_offset(
148 : 30 : offset< eq, tag::dissipation, tag::dissipation_id >( c ) ),
149 : : m_dY( initScalarGradient() ),
150 : : m_bprime(),
151 : : m_S(),
152 : : m_kprime(),
153 : : m_rho2(),
154 : : m_r(),
155 : : m_b(),
156 : : m_k(),
157 : : coeff(
158 : 15 : m_ncomp,
159 [ + - ]: 15 : g_inputdeck.get< tag::param, eq, tag::bprime >().at(c),
160 [ + - ]: 15 : g_inputdeck.get< tag::param, eq, tag::S >().at(c),
161 [ + - ]: 15 : g_inputdeck.get< tag::param, eq, tag::kappaprime >().at(c),
162 [ + - ]: 15 : g_inputdeck.get< tag::param, eq, tag::rho2 >().at(c),
163 [ + - ]: 15 : g_inputdeck.get< tag::param, eq, tag::r >().at(c),
164 [ - + ][ + - ]: 105 : m_bprime, m_S, m_kprime, m_rho2, m_r, m_b, m_k )
165 : : {
166 : : // Zero prescribed scalar gradient if full variable is solved for
167 [ + - ][ + - ]: 15 : if (m_solve == ctr::DepvarType::FULLVAR) m_dY.fill( 0.0 );
168 : : // Populate inverse hydrodynamics time scales and hydrodyanmics
169 : : // production/dissipation extracted from DNS
170 [ + + ]: 15 : if ( Coefficients::type() == ctr::CoeffPolicyType::HYDROTIMESCALE ) {
171 : : // Configure inverse hydrodyanmics time scale from DNS
172 : : const auto& hts =
173 [ + - ]: 4 : g_inputdeck.get< tag::param, eq, tag::hydrotimescales >().at(c);
174 [ + - ]: 8 : ctr::HydroTimeScales ot;
175 : : // cppcheck-suppress useStlAlgorithm
176 [ + + ][ + - ]: 24 : for (auto t : hts) m_hts.push_back( ot.table(t) );
[ + - ]
177 [ - + ][ - - ]: 4 : Assert( m_hts.size() == m_ncomp, "Number of inverse hydro time scale "
[ - - ][ - - ]
178 : : "tables associated does not match the components integrated" );
179 : :
180 : : // Configure hydrodyanmics production/dissipation from DNS
181 : : const auto& hp =
182 [ + - ]: 4 : g_inputdeck.get< tag::param, eq, tag::hydroproductions >().at(c);
183 [ + - ]: 8 : ctr::HydroProductions op;
184 : : // cppcheck-suppress useStlAlgorithm
185 [ + + ][ + - ]: 24 : for (auto t : hp) m_hp.push_back( op.table(t) );
[ + - ]
186 [ - + ][ - - ]: 4 : Assert( m_hp.size() == m_ncomp, "Number of hydro "
[ - - ][ - - ]
187 : : "production/dissipation tables associated does not match the "
188 : : "components integrated" );
189 : : }
190 : 15 : }
191 : :
192 : : //! Initalize SDE, prepare for time integration
193 : : //! \param[in] stream Thread (or more precisely stream) ID
194 : : //! \param[in,out] particles Array of particle properties
195 : 51 : void initialize( int stream, tk::Particles& particles ) {
196 : : //! Set initial conditions using initialization policy
197 : : Init::template init< eq >
198 : 51 : ( g_inputdeck, m_rng, stream, particles, m_c, m_ncomp, m_offset );
199 : : // Initialize values derived from primary prognostic variable
200 : 51 : const auto npar = particles.nunk();
201 [ + + ]: 304051 : for (auto p=decltype(npar){0}; p<npar; ++p)
202 [ + + ]: 1824000 : for (ncomp_t i=0; i<m_ncomp; ++i) {
203 [ - + ][ - - ]: 1520000 : Assert( particles( p, i, m_offset ) > 0.0, "Beta IC out of bounds!" );
[ - - ][ - - ]
204 [ - + ][ - - ]: 1520000 : Assert( particles( p, i, m_offset ) < 1.0, "Beta IC out of bounds!" );
[ - - ][ - - ]
205 : 1520000 : derived( particles, p, i );
206 : : }
207 : 51 : }
208 : :
209 : : //! \brief Advance particles according to the system of mix mass-fraction
210 : : //! beta SDEs
211 : : //! \param[in,out] particles Array of particle properties
212 : : //! \param[in] stream Thread (or more precisely stream) ID
213 : : //! \param[in] dt Time step size
214 : : //! \param[in] t Physical time of the simulation
215 : : //! \param[in] moments Map of statistical moments
216 : 23536 : void advance( tk::Particles& particles,
217 : : int stream,
218 : : tk::real dt,
219 : : tk::real t,
220 : : const std::map< tk::ctr::Product, tk::real >& moments )
221 : : {
222 : : // Update SDE coefficients
223 : 23536 : coeff.update( m_depvar, m_dissipation_depvar, m_velocity_depvar,
224 : 23536 : m_velocity_solve, m_solve, m_ncomp, moments, m_bprime,
225 : 23536 : m_kprime, m_rho2, m_r, m_hts, m_hp, m_b, m_k, m_S, t );
226 : :
227 : 23536 : const auto eps = std::numeric_limits< tk::real >::epsilon();
228 : :
229 : : // Advance particles
230 : 23536 : const auto npar = particles.nunk();
231 [ + + ]: 4723536 : for (auto p=decltype(npar){0}; p<npar; ++p) {
232 : : // Generate Gaussian random numbers with zero mean and unit variance
233 [ + - ]: 9400000 : std::vector< tk::real > dW( m_ncomp );
234 [ + - ]: 4700000 : m_rng.gaussian( stream, m_ncomp, dW.data() );
235 : :
236 : : // Access coupled particle velocity
237 : 4700000 : tk::real u = 0.0, v = 0.0, w = 0.0;
238 : : using std::abs;
239 [ + - ][ + - ]: 4700000 : if (abs(m_dY[0]) > eps || abs(m_dY[1]) > eps || abs(m_dY[2]) > eps) {
[ - + ][ - + ]
240 [ - - ]: 0 : u = particles( p, 0, m_velocity_offset );
241 [ - - ]: 0 : v = particles( p, 1, m_velocity_offset );
242 [ - - ]: 0 : w = particles( p, 2, m_velocity_offset );
243 : : }
244 : :
245 : : // Advance all m_ncomp scalars
246 [ + + ]: 28200000 : for (ncomp_t i=0; i<m_ncomp; ++i) {
247 [ + - ]: 23500000 : tk::real& Y = particles( p, i, m_offset );
248 : 23500000 : tk::real d = m_k[i] * Y * (1.0 - Y) * dt;
249 [ + + ]: 23500000 : d = (d > 0.0 ? std::sqrt(d) : 0.0);
250 : 23500000 : Y += 0.5*m_b[i]*(m_S[i] - Y)*dt + d*dW[i]
251 : 23500000 : - (m_dY[0]*u - m_dY[1]*v - m_dY[2]*w)*dt;
252 : : // Compute instantaneous values derived from updated Y
253 [ + - ]: 23500000 : derived( particles, p, i );
254 : : }
255 : : }
256 : 23536 : }
257 : :
258 : : private:
259 : : const ncomp_t m_c; //!< Equation system index
260 : : const char m_depvar; //!< Dependent variable
261 : : const ncomp_t m_ncomp; //!< Number of components
262 : : const ncomp_t m_offset; //!< Offset SDE operates from
263 : : const tk::RNG& m_rng; //!< Random number generator
264 : : const ctr::DepvarType m_solve; //!< Depndent variable to solve for
265 : :
266 : : const bool m_velocity_coupled; //!< True if coupled to velocity
267 : : const char m_velocity_depvar; //!< Coupled velocity dependent variable
268 : : const ncomp_t m_velocity_offset; //!< Offset of coupled velocity eq
269 : : //! Quantity the coupled velocity eq solves for
270 : : const ctr::DepvarType m_velocity_solve;
271 : :
272 : : const bool m_dissipation_coupled; //!< True if coupled to dissipation
273 : : const char m_dissipation_depvar; //!< Depvar of coupled dissipation eq
274 : : const ncomp_t m_dissipation_offset; //!< Offset of coupled dissipation eq
275 : :
276 : : std::array< tk::real, 3 > m_dY; //! Prescribed mean scalar gradient
277 : :
278 : : //! Coefficients
279 : : std::vector< kw::sde_bprime::info::expect::type > m_bprime;
280 : : std::vector< kw::sde_S::info::expect::type > m_S;
281 : : std::vector< kw::sde_kappaprime::info::expect::type > m_kprime;
282 : : std::vector< kw::sde_rho2::info::expect::type > m_rho2;
283 : : std::vector< kw::sde_r::info::expect::type > m_r;
284 : : std::vector< kw::sde_b::info::expect::type > m_b;
285 : : std::vector< kw::sde_kappa::info::expect::type > m_k;
286 : :
287 : : //! Coefficients policy
288 : : Coefficients coeff;
289 : :
290 : : //! Selected inverse hydrodynamics time scales (if used) for each component
291 : : //! \details This is only used if the coefficients policy is
292 : : //! MixMassFracBetaCoeffHydroTimeScale. See constructor.
293 : : std::vector< tk::Table<1> > m_hts;
294 : :
295 : : //! Selected hydrodynamics production/dissipation (if used) for each comp.
296 : : //! \details This is only used if the coefficients policy is
297 : : //! MixMassFracBetaCoeffHydroTimeScale. See constructor.
298 : : std::vector< tk::Table<1> > m_hp;
299 : :
300 : : //! \brief Return density for mass fraction
301 : : //! \details Functional wrapper around the dependent variable of the beta
302 : : //! SDE. This function returns the instantaneous density, rho,
303 : : //! based on the mass fraction, Y, and parameters rho2 and r'.
304 : : //! \param[in] Y Instantaneous value of the mass fraction, Y
305 : : //! \param[in] i Index specifying which (of multiple) parameters to use
306 : : //! \return Instantaneous value of the density, rho
307 : 25020000 : tk::real rho( tk::real Y, ncomp_t i ) const {
308 : 25020000 : return m_rho2[i] / ( 1.0 + m_r[i] * Y );
309 : : }
310 : :
311 : : //! \brief Return specific volume for mass fraction
312 : : //! \details Functional wrapper around the dependent variable of the beta
313 : : //! SDE. This function returns the instantaneous specific volume, V,
314 : : //! based on the mass fraction, Y, and parameters rho2 and r'.
315 : : //! \param[in] Y Instantaneous value of the mass fraction, Y
316 : : //! \param[in] i Index specifying which (of multiple) parameters to use
317 : : //! \return Instantaneous value of the specific volume, V
318 : 25020000 : tk::real vol( tk::real Y, ncomp_t i ) const {
319 : 25020000 : return ( 1.0 + m_r[i] * Y ) / m_rho2[i];
320 : : }
321 : :
322 : : //! Compute instantaneous values derived from updated Y
323 : : //! \param[in,out] particles Particle properties array
324 : : //! \param[in] p Particle index
325 : : //! \param[in] i Component index
326 : 25020000 : void derived( tk::Particles& particles, ncomp_t p, ncomp_t i ) const {
327 : 25020000 : tk::real& Y = particles( p, i, m_offset );
328 : 25020000 : particles( p, m_ncomp+i, m_offset ) = rho( Y, i );
329 : 25020000 : particles( p, m_ncomp*2+i, m_offset ) = vol( Y, i );
330 : 25020000 : particles( p, m_ncomp*3+i, m_offset ) = 1.0 - Y;
331 : 25020000 : }
332 : :
333 : : //! Initialize imposed mean scalar gradient from user input
334 : 15 : std::array< tk::real, 3 > initScalarGradient() {
335 : 15 : const auto& mg = g_inputdeck.get< tag::param, eq, tag::mean_gradient >();
336 : 15 : std::array< tk::real, 3 > dY{{ 0.0, 0.0, 0.0 }};
337 [ - + ]: 15 : if (mg.size() > m_c) {
338 : 0 : const auto& g = mg[ m_c ];
339 : 0 : dY = {{ g[0], g[1], g[2] }};
340 : : }
341 : : Assert( dY.size() == 3, "Mean scalar gradient vector size must be 3" );
342 : 15 : return dY;
343 : : }
344 : : };
345 : :
346 : : } // walker::
347 : :
348 : : #endif // MixMassFractionBeta_h
|