Walker test code coverage report
Current view: top level - DiffEq/Beta - NumberFractionBeta.hpp (source / functions) Hit Total Coverage
Commit: test_coverage.info Lines: 33 33 100.0 %
Date: 2022-09-21 18:57:21 Functions: 5 40 12.5 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 18 30 60.0 %

           Branch data     Line data    Source code
       1                 :            : // *****************************************************************************
       2                 :            : /*!
       3                 :            :   \file      src/DiffEq/Beta/NumberFractionBeta.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 number-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). The main
      13                 :            :     difference compared to the plain beta SDE (see DiffEq/Beta.h), is that in
      14                 :            :     the number-fraction beta SDE the dependent variable, there are two
      15                 :            :     additional stochastic variables computed from the beta variables.
      16                 :            : 
      17                 :            :     In a nutshell, the equation integrated governs a set of scalars,
      18                 :            :     \f$\color[HTML]{dcdcdc}0\!\le\!X_\alpha\f$,
      19                 :            :     \f$\color[HTML]{dcdcdc}\alpha\!=\!1,\dots,N\f$, as
      20                 :            :     \f[
      21                 :            :        \mathrm{d}X_\alpha(t) = \frac{b_\alpha}{2}\left(S_\alpha - X_\alpha\right)
      22                 :            :        \mathrm{d}t + \sqrt{\kappa_\alpha X_\alpha(1-X_\alpha)}
      23                 :            :        \mathrm{d}W_\alpha(t), \qquad \alpha=1,\dots,N
      24                 :            :     \f]
      25                 :            :     with parameter vectors \f$\color[HTML]{dcdcdc}b_\alpha > 0\f$,
      26                 :            :     \f$\color[HTML]{dcdcdc}\kappa_\alpha > 0\f$, and \f$\color[HTML]{dcdcdc}0 <
      27                 :            :     S_\alpha < 1\f$. This is the same as in DiffEq/Beta.h. Here
      28                 :            :     \f$\color[HTML]{dcdcdc}\mathrm{d}W_\alpha(t)\f$ is an isotropic vector-valued
      29                 :            :     [Wiener process](http://en.wikipedia.org/wiki/Wiener_process) with independent
      30                 :            :     increments. The invariant distribution is the joint beta distribution. This
      31                 :            :     system of SDEs consists of N independent equations. For
      32                 :            :     more on the beta SDE, see https://doi.org/10.1080/14685248.2010.510843.
      33                 :            : 
      34                 :            :     In addition to integrating the above SDE, there are two additional functions
      35                 :            :     of \f$\color[HTML]{dcdcdc} X_\alpha \f$ are computed as
      36                 :            :     \f[ \begin{aligned}
      37                 :            :       \rho(X_\alpha) & = \rho_{2\alpha} ( 1 - r'_\alpha X_\alpha ) \\
      38                 :            :       V(X_\alpha) & = \frac{1}{ \rho(X\alpha) }
      39                 :            :     \end{aligned} \f]
      40                 :            :     These equations compute the instantaneous mixture density,
      41                 :            :     \f$\color[HTML]{dcdcdc} \rho \f$, and instantaneous specific volume,
      42                 :            :     \f$\color[HTML]{dcdcdc} V_\alpha \f$, for equation \f$\color[HTML]{dcdcdc}
      43                 :            :     \alpha \f$ in the system. These quantities are used in binary mixing of
      44                 :            :     variable-density turbulence between two fluids with constant densities,
      45                 :            :     \f$\color[HTML]{dcdcdc} \rho_1, \f$ and \f$\color[HTML]{dcdcdc} \rho_2 \f$. The
      46                 :            :     additional parameters, \f$\color[HTML]{dcdcdc} \rho_2 \f$ and
      47                 :            :     \f$\color[HTML]{dcdcdc} r' \f$ are user input parameters and kept constant
      48                 :            :     during integration. Since we compute the above variables,
      49                 :            :     \f$\color[HTML]{dcdcdc}\rho,\f$ and \f$\color[HTML]{dcdcdc}V\f$, and call them
      50                 :            :     mixture density and specific volume, respectively, \f$\color[HTML]{dcdcdc}X\f$,
      51                 :            :     governed by the beta SDE is a number (or mole) fraction, hence the name
      52                 :            :     number-fraction beta.
      53                 :            : 
      54                 :            :     _All of this is unpublished, but will be linked in here once published_.
      55                 :            : */
      56                 :            : // *****************************************************************************
      57                 :            : #ifndef NumberFractionBeta_h
      58                 :            : #define NumberFractionBeta_h
      59                 :            : 
      60                 :            : #include <vector>
      61                 :            : #include <cmath>
      62                 :            : 
      63                 :            : #include "InitPolicy.hpp"
      64                 :            : #include "NumberFractionBetaCoeffPolicy.hpp"
      65                 :            : #include "RNG.hpp"
      66                 :            : #include "Particles.hpp"
      67                 :            : 
      68                 :            : namespace walker {
      69                 :            : 
      70                 :            : extern ctr::InputDeck g_inputdeck;
      71                 :            : extern std::map< tk::ctr::RawRNGType, tk::RNG > g_rng;
      72                 :            : 
      73                 :            : //! \brief NumberFractionBeta SDE used polymorphically with DiffEq
      74                 :            : //! \details The template arguments specify policies and are used to configure
      75                 :            : //!   the behavior of the class. The policies are:
      76                 :            : //!   - Init - initialization policy, see DiffEq/InitPolicy.h
      77                 :            : //!   - Coefficients - coefficients policy, see
      78                 :            : //!     DiffEq/NumberFractionBetaCoeffPolicy.h
      79                 :            : template< class Init, class Coefficients >
      80                 :            : class NumberFractionBeta {
      81                 :            : 
      82                 :            :   private:
      83                 :            :     using ncomp_t = tk::ctr::ncomp_t;
      84                 :            : 
      85                 :            :   public:
      86                 :            :     //! \brief Constructor
      87                 :            :     //! \param[in] c Index specifying which system of number-fraction beta SDEs
      88                 :            :     //!   to construct. There can be multiple numfracbeta ... end blocks in a
      89                 :            :     //!   control file. This index specifies which number-fraction beta SDE
      90                 :            :     //!   system to instantiate. The index corresponds to the order in which the
      91                 :            :     //!   numfracbeta ... end blocks are given the control file.
      92                 :         11 :     explicit NumberFractionBeta( ncomp_t c ) :
      93                 :            :       m_c( c ),
      94                 :            :       m_depvar(
      95                 :         11 :         g_inputdeck.get< tag::param, tag::numfracbeta, tag::depvar >().at(c) ),
      96                 :            :       m_ncomp(
      97                 :         11 :         g_inputdeck.get< tag::component >().get< tag::numfracbeta >().at(c) / 3 ),
      98                 :            :       m_offset(
      99                 :         11 :         g_inputdeck.get< tag::component >().offset< tag::numfracbeta >(c) ),
     100         [ +  - ]:         11 :       m_rng( g_rng.at( tk::ctr::raw(
     101                 :         11 :         g_inputdeck.get< tag::param, tag::numfracbeta, tag::rng >().at(c) ) ) ),
     102                 :            :       m_b(),
     103                 :            :       m_S(),
     104                 :            :       m_k(),
     105                 :            :       m_rho2(),
     106                 :            :       m_rcomma(),
     107                 :         11 :       coeff( m_ncomp,
     108         [ +  - ]:         11 :              g_inputdeck.get< tag::param, tag::numfracbeta, tag::b >().at(c),
     109         [ +  - ]:         11 :              g_inputdeck.get< tag::param, tag::numfracbeta, tag::S >().at(c),
     110         [ +  - ]:         11 :              g_inputdeck.get< tag::param, tag::numfracbeta, tag::kappa >().at(c),
     111         [ +  - ]:         11 :              g_inputdeck.get< tag::param, tag::numfracbeta, tag::rho2 >().at(c),
     112         [ +  - ]:         11 :              g_inputdeck.get< tag::param, tag::numfracbeta, tag::rcomma >().at(c),
     113         [ +  - ]:         66 :              m_b, m_S, m_k, m_rho2, m_rcomma ) {}
     114                 :            : 
     115                 :            :     //! Initalize SDE, prepare for time integration
     116                 :            :     //! \param[in] stream Thread (or more precisely stream) ID 
     117                 :            :     //! \param[in,out] particles Array of particle properties 
     118                 :         40 :     void initialize( int stream, tk::Particles& particles ) {
     119                 :            :       //! Set initial conditions using initialization policy
     120                 :            :       Init::template
     121                 :            :         init< tag::numfracbeta >
     122                 :         40 :             ( g_inputdeck, m_rng, stream, particles, m_c, m_ncomp, m_offset );
     123                 :         40 :     }
     124                 :            : 
     125                 :            :     //! \brief Advance particles according to the system of number-fraction beta
     126                 :            :     //!    SDEs
     127                 :            :     //! \param[in,out] particles Array of particle properties
     128                 :            :     //! \param[in] stream Thread (or more precisely stream) ID
     129                 :            :     //! \param[in] dt Time step size
     130                 :     500000 :     void advance( tk::Particles& particles,
     131                 :            :                   int stream,
     132                 :            :                   tk::real dt,
     133                 :            :                   tk::real,
     134                 :            :                   const std::map< tk::ctr::Product, tk::real >& )
     135                 :            :     {
     136                 :            :       // Advance particles
     137                 :     500000 :       const auto npar = particles.nunk();
     138         [ +  + ]:    5487500 :       for (auto p=decltype(npar){0}; p<npar; ++p) {
     139                 :            :         // Generate Gaussian random numbers with zero mean and unit variance
     140         [ +  - ]:    9975000 :         std::vector< tk::real > dW( m_ncomp );
     141         [ +  - ]:    4987500 :         m_rng.gaussian( stream, m_ncomp, dW.data() );
     142                 :            :         // Advance all m_ncomp scalars
     143         [ +  + ]:   29925000 :         for (ncomp_t i=0; i<m_ncomp; ++i) {
     144         [ +  - ]:   24937500 :           tk::real& X = particles( p, i, m_offset );
     145                 :   24937500 :           tk::real d = m_k[i] * X * (1.0 - X) * dt;
     146         [ +  + ]:   24937500 :           d = (d > 0.0 ? std::sqrt(d) : 0.0);
     147                 :   24937500 :           X += 0.5*m_b[i]*(m_S[i] - X)*dt + d*dW[i];
     148                 :            :           // Compute instantaneous values derived from updated X
     149         [ +  - ]:   24937500 :           particles( p, m_ncomp+i, m_offset ) = rho( X, i );
     150         [ +  - ]:   24937500 :           particles( p, m_ncomp*2+i, m_offset ) = vol( X, i );
     151                 :            :         }
     152                 :            :       }
     153                 :     500000 :     }
     154                 :            : 
     155                 :            :   private:
     156                 :            :     const ncomp_t m_c;                  //!< Equation system index
     157                 :            :     const char m_depvar;                //!< Dependent variable
     158                 :            :     const ncomp_t m_ncomp;              //!< Number of components
     159                 :            :     const ncomp_t m_offset;             //!< Offset SDE operates from
     160                 :            :     const tk::RNG& m_rng;               //!< Random number generator
     161                 :            : 
     162                 :            :     //! Coefficients
     163                 :            :     std::vector< kw::sde_b::info::expect::type > m_b;
     164                 :            :     std::vector< kw::sde_S::info::expect::type > m_S;
     165                 :            :     std::vector< kw::sde_kappa::info::expect::type > m_k;
     166                 :            :     std::vector< kw::sde_rho2::info::expect::type > m_rho2;
     167                 :            :     std::vector< kw::sde_rcomma::info::expect::type > m_rcomma;
     168                 :            : 
     169                 :            :     //! Coefficients policy
     170                 :            :     Coefficients coeff;
     171                 :            : 
     172                 :            :     //! \brief Return density for mole fraction
     173                 :            :     //! \details Functional wrapper around the dependent variable of the beta
     174                 :            :     //!   SDE. This function returns the instantaneous density, rho,
     175                 :            :     //!   based on the number fraction, X, and parameters rho2 and r'.
     176                 :            :     //! \param[in] X Instantaneous value of the mole fraction, X
     177                 :            :     //! \param[in] i Index specifying which (of multiple) parameters to use
     178                 :            :     //! \return Instantaneous value of the density, rho
     179                 :   49875000 :     tk::real rho( tk::real X, ncomp_t i ) const {
     180                 :   49875000 :       return m_rho2[i] * ( 1.0 - m_rcomma[i] * X );
     181                 :            :     }
     182                 :            : 
     183                 :            :     //! \brief Return specific volume for mole fraction
     184                 :            :     //! \details Functional wrapper around the dependent variable of the beta
     185                 :            :     //!   SDE. This function returns the instantaneous specific volume, V,
     186                 :            :     //!   based on the number fraction, X, and parameters rho2 and r'.
     187                 :            :     //! \param[in] X Instantaneous value of the mole fraction, X
     188                 :            :     //! \param[in] i Index specifying which (of multiple) parameters to use
     189                 :            :     //! \return Instantaneous value of the specific volume, V
     190                 :   24937500 :     tk::real vol( tk::real X, ncomp_t i ) const {
     191                 :   24937500 :       return 1.0 / rho( X, i );
     192                 :            :     }
     193                 :            : };
     194                 :            : 
     195                 :            : } // walker::
     196                 :            : 
     197                 :            : #endif // NumberFractionBeta_h

Generated by: LCOV version 1.14