Quinoa all test code coverage report
Current view: top level - DiffEq/Beta - Beta.hpp (source / functions) Hit Total Coverage
Commit: Quinoa_v0.3-957-gb4f0efae0 Lines: 17 17 100.0 %
Date: 2021-11-09 15:14:18 Functions: 2 16 12.5 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 16 22 72.7 %

           Branch data     Line data    Source code
       1                 :            : // *****************************************************************************
       2                 :            : /*!
       3                 :            :   \file      src/DiffEq/Beta/Beta.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 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).
      13                 :            : 
      14                 :            :     In a nutshell, the equation integrated governs a set of scalars,
      15                 :            :     \f$0\!\le\!Y_\alpha\f$, \f$\alpha\!=\!1,\dots,N\f$, as
      16                 :            : 
      17                 :            :     @m_class{m-show-m}
      18                 :            : 
      19                 :            :     \f[
      20                 :            :        \mathrm{d}Y_\alpha(t) = \frac{b_\alpha}{2}\left(S_\alpha - Y_\alpha\right)
      21                 :            :        \mathrm{d}t + \sqrt{\kappa_\alpha Y_\alpha(1-Y_\alpha)}
      22                 :            :        \mathrm{d}W_\alpha(t), \qquad \alpha=1,\dots,N
      23                 :            :     \f]
      24                 :            : 
      25                 :            :     @m_class{m-hide-m}
      26                 :            : 
      27                 :            :     \f[ \begin{split}
      28                 :            :        \mathrm{d}Y_\alpha(t) = \frac{b_\alpha}{2}\left(S_\alpha - Y_\alpha\right)
      29                 :            :        \mathrm{d}t + \sqrt{\kappa_\alpha Y_\alpha(1-Y_\alpha)}
      30                 :            :        \mathrm{d}W_\alpha(t), \\ \alpha=1,\dots,N
      31                 :            :     \end{split} \f]
      32                 :            : 
      33                 :            :     with parameter vectors \f$b_\alpha > 0\f$, \f$\kappa_\alpha > 0\f$, and \f$0
      34                 :            :     < S_\alpha < 1\f$. Here
      35                 :            :     \f$\mathrm{d}W_\alpha(t)\f$ is an isotropic vector-valued [Wiener
      36                 :            :     process](http://en.wikipedia.org/wiki/Wiener_process) with independent
      37                 :            :     increments. The invariant distribution is the joint beta distribution. This
      38                 :            :     system of SDEs consists of N independent equations. For
      39                 :            :     more on the beta SDE, see https://doi.org/10.1080/14685248.2010.510843.
      40                 :            : */
      41                 :            : // *****************************************************************************
      42                 :            : #ifndef Beta_h
      43                 :            : #define Beta_h
      44                 :            : 
      45                 :            : #include <vector>
      46                 :            : #include <cmath>
      47                 :            : 
      48                 :            : #include "InitPolicy.hpp"
      49                 :            : #include "BetaCoeffPolicy.hpp"
      50                 :            : #include "RNG.hpp"
      51                 :            : #include "Particles.hpp"
      52                 :            : 
      53                 :            : namespace walker {
      54                 :            : 
      55                 :            : extern ctr::InputDeck g_inputdeck;
      56                 :            : extern std::map< tk::ctr::RawRNGType, tk::RNG > g_rng;
      57                 :            : 
      58                 :            : //! \brief Beta SDE used polymorphically with DiffEq
      59                 :            : //! \details The template arguments specify policies and are used to configure
      60                 :            : //!   the behavior of the class. The policies are:
      61                 :            : //!   - Init - initialization policy, see DiffEq/InitPolicy.h
      62                 :            : //!   - Coefficients - coefficients policy, see DiffEq/BetaCoeffPolicy.h
      63                 :            : template< class Init, class Coefficients >
      64                 :            : class Beta {
      65                 :            : 
      66                 :            :   private:
      67                 :            :     using ncomp_t = tk::ctr::ncomp_t;
      68                 :            : 
      69                 :            :   public:
      70                 :            :     //! \brief Constructor
      71                 :            :     //! \param[in] c Index specifying which system of beta SDEs to construct.
      72                 :            :     //!   There can be multiple beta ... end blocks in a control file. This
      73                 :            :     //!   index specifies which beta SDE system to instantiate. The index
      74                 :            :     //!   corresponds to the order in which the beta ... end blocks are given
      75                 :            :     //!   the control file.
      76                 :         11 :     explicit Beta( ncomp_t c ) :
      77                 :            :       m_c( c ),
      78                 :            :       m_depvar( g_inputdeck.get< tag::param, tag::beta, tag::depvar >().at(c) ),
      79                 :            :       m_ncomp( g_inputdeck.get< tag::component >().get< tag::beta >().at(c) ),
      80                 :         11 :       m_offset( g_inputdeck.get< tag::component >().offset< tag::beta >(c) ),
      81                 :         11 :       m_rng( g_rng.at( tk::ctr::raw(
      82                 :            :         g_inputdeck.get< tag::param, tag::beta, tag::rng >().at(c) ) ) ),
      83                 :            :       m_b(),
      84                 :            :       m_S(),
      85                 :            :       m_k(),
      86                 :         11 :       coeff( m_ncomp,
      87                 :            :              g_inputdeck.get< tag::param, tag::beta, tag::b >().at(c),
      88                 :            :              g_inputdeck.get< tag::param, tag::beta, tag::S >().at(c),
      89                 :            :              g_inputdeck.get< tag::param, tag::beta, tag::kappa >().at(c),
      90 [ -  + ][ -  + ]:         33 :              m_b, m_S, m_k ) {}
         [ -  + ][ -  + ]
                 [ +  - ]
      91                 :            : 
      92                 :            :     //! Initalize SDE, prepare for time integration
      93                 :            :     //! \param[in] stream Thread (or more precisely stream) ID 
      94                 :            :     //! \param[in,out] particles Array of particle properties 
      95                 :            :     void initialize( int stream, tk::Particles& particles ) {
      96                 :            :       //! Set initial conditions using initialization policy
      97                 :            :       Init::template
      98                 :            :         init< tag::beta >
      99                 :         47 :             ( g_inputdeck, m_rng, stream, particles, m_c, m_ncomp, m_offset );
     100                 :            :     }
     101                 :            : 
     102                 :            :     //! \brief Advance particles according to the system of beta SDEs
     103                 :            :     //! \param[in,out] particles Array of particle properties
     104                 :            :     //! \param[in] stream Thread (or more precisely stream) ID
     105                 :            :     //! \param[in] dt Time step size
     106                 :     469953 :     void advance( tk::Particles& particles,
     107                 :            :                   int stream,
     108                 :            :                   tk::real dt,
     109                 :            :                   tk::real,
     110                 :            :                   const std::map< tk::ctr::Product, tk::real >& )
     111                 :            :     {
     112                 :     469953 :       const auto npar = particles.nunk();
     113         [ +  + ]:   40465953 :       for (auto p=decltype(npar){0}; p<npar; ++p) {
     114                 :            :         // Generate Gaussian random numbers with zero mean and unit variance
     115                 :   39996000 :         std::vector< tk::real > dW( m_ncomp );
     116         [ +  - ]:   39996000 :         m_rng.gaussian( stream, m_ncomp, dW.data() );
     117                 :            : 
     118                 :            :         // Advance all m_ncomp scalars
     119         [ +  + ]:  239976000 :         for (ncomp_t i=0; i<m_ncomp; ++i) {
     120         [ +  + ]:  199980000 :           tk::real& par = particles( p, i, m_offset );
     121         [ +  + ]:  199980000 :           tk::real d = m_k[i] * par * (1.0 - par) * dt;
     122         [ +  + ]:  199980000 :           d = (d > 0.0 ? std::sqrt(d) : 0.0);
     123                 :  199980000 :           par += 0.5*m_b[i]*(m_S[i] - par)*dt + d*dW[i];
     124                 :            :         }
     125                 :            :       }
     126                 :     469953 :     }
     127                 :            : 
     128                 :            :   private:
     129                 :            :     const ncomp_t m_c;                  //!< Equation system index
     130                 :            :     const char m_depvar;                //!< Dependent variable
     131                 :            :     const ncomp_t m_ncomp;              //!< Number of components
     132                 :            :     const ncomp_t m_offset;             //!< Offset SDE operates from
     133                 :            :     const tk::RNG& m_rng;               //!< Random number generator
     134                 :            : 
     135                 :            :     //! Coefficients
     136                 :            :     std::vector< kw::sde_b::info::expect::type > m_b;
     137                 :            :     std::vector< kw::sde_S::info::expect::type > m_S;
     138                 :            :     std::vector< kw::sde_kappa::info::expect::type > m_k;
     139                 :            : 
     140                 :            :     //! Coefficients policy
     141                 :            :     Coefficients coeff;
     142                 :            : };
     143                 :            : 
     144                 :            : } // walker::
     145                 :            : 
     146                 :            : #endif // Beta_h

Generated by: LCOV version 1.14