Branch data Line data Source code
1 : : // *****************************************************************************
2 : : /*!
3 : : \file src/DiffEq/DiffEqFactory.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 Differential equations factory
9 : : \details This file declares the type for a differential equations factory.
10 : : */
11 : : // *****************************************************************************
12 : : #ifndef DiffEqFactory_h
13 : : #define DiffEqFactory_h
14 : :
15 : : #include <map>
16 : : #include <functional>
17 : :
18 : : #include "DiffEq.hpp"
19 : : #include "Factory.hpp"
20 : : #include "SystemComponents.hpp"
21 : : #include "Walker/Options/DiffEq.hpp"
22 : :
23 : : namespace walker {
24 : :
25 : : //! Differential equation factory: keys associated to their constructors
26 : : using DiffEqFactory =
27 : : std::map< ctr::DiffEqKey,
28 : : std::function< DiffEq(const tk::ctr::ncomp_t&) > >;
29 : :
30 : : //! \brief Function object for registering a differential equation into the
31 : : //! differential equation factory
32 : : //! \details This functor is repeatedly called by brigand's cartesian_product,
33 : : //! sweeping all combinations of the differential equation policies. The
34 : : //! purpose of template template is to simplify client code as that will
35 : : //! not have to specify the template arguments of the template argument
36 : : //! (the policies of Eq), since we can figure it out here. See also
37 : : //! http://stackoverflow.com/a/214900
38 : : template< template< class, class > class Eq >
39 : : struct registerDiffEq {
40 : : //! Need to store the reference to factory we are registering into
41 : : DiffEqFactory& factory;
42 : : //! Need to store which differential equation we are registering
43 : : const ctr::DiffEqType type;
44 : : //! Constructor, also count number of unique equation types registered
45 : 4114 : explicit registerDiffEq( DiffEqFactory& f,
46 : : ctr::DiffEqType t,
47 : : std::set< ctr::DiffEqType >& eqTypes ) :
48 : 4114 : factory( f ), type( t ) { eqTypes.insert( t ); }
49 : : //! \brief Function call operator called with tk::cartesian_product for
50 : : //! each unique sequence of policy combinations
51 : : template< typename U > void operator()( brigand::type_<U> ) {
52 : : // Get Initialization policy: first type of brigand::list U
53 : : using InitPolicy = typename brigand::front< U >;
54 : : // Get coefficients policy: last type of brigand::list U
55 : : using CoeffPolicy = typename brigand::back< U >;
56 : : // Build differential equation key
57 : : ctr::DiffEqKey key{{ type, InitPolicy::type(), CoeffPolicy::type() }};
58 : : // Register equation (with policies given by brigand::list U) into
59 : : // factory
60 : : tk::recordModelLate< DiffEq, Eq< InitPolicy, CoeffPolicy > >
61 : 5984 : ( factory, key, static_cast<tk::ctr::ncomp_t>(0) );
62 : : }
63 : : };
64 : :
65 : : //! \brief Convert and return values from vector as string
66 : : //! \param[in] v Vector whose components to return as a string
67 : : //! \return Concatenated string of values read from a vector
68 : : template< typename V >
69 : 356 : std::string parameters( const V& v ) {
70 : 712 : std::stringstream s;
71 [ + - ]: 356 : s << "{ ";
72 [ + + ][ + - ]: 1466 : for (auto p : v) s << p << ' ';
73 [ + - ]: 356 : s << "}";
74 : 356 : return s.str();
75 : : }
76 : :
77 : : //! \brief Return names of options (tk::Toggle) from vector as a string
78 : : //! \param[in] opt Option instance (inheriting from tk::Toggle)
79 : : //! \param[in] v Option vector whose names of components to return
80 : : //! \return Concatenated string of option names read from option vector
81 : : template< class Option, class OptTypeVec >
82 : 2 : std::string options( const Option& opt, const OptTypeVec& v ) {
83 : 4 : std::stringstream s;
84 [ + - ]: 2 : s << "{ ";
85 [ + + ]: 12 : for (auto o : v) s << opt.name(o) << ' ';
86 [ + - ]: 2 : s << "}";
87 : 2 : return s.str();
88 : : }
89 : :
90 : : //! \brief Insert spike information (used to specify delta PDFs) into info
91 : : //! vector
92 : : //! \param[in,out] nfo Info vector of string-pairs to insert to
93 : : //! \param[in] spike Vector of vectors specifying spike info
94 : : template< typename Info, typename VV >
95 : 66 : void spikes( Info& nfo, const VV& spike ) {
96 : : std::size_t i = 0;
97 [ + + ]: 86 : for (const auto& s : spike)
98 : : // cppcheck-suppress useStlAlgorithm
99 [ + - ][ + - ]: 60 : nfo.emplace_back( "delta spikes [" + std::to_string(++i) + ":" +
[ + - ][ + - ]
[ + - ][ - + ]
[ - + ][ - + ]
[ - + ][ - - ]
[ - - ][ - - ]
[ - - ]
100 [ + - ][ + - ]: 80 : std::to_string( s.size()/2 ) + "]",
[ - + ][ - + ]
[ - - ][ - - ]
101 : 40 : parameters( s ) );
102 : 66 : }
103 : :
104 : : //! \brief Insert betapdf information (used to specify beta PDFs) into info
105 : : //! vector
106 : : //! \param[in,out] nfo Info vector of string-pairs to insert to
107 : : //! \param[in] betapdf Vector of vectors specifying betapdf info
108 : : template< typename Info, typename VV >
109 : 66 : void betapdfs( Info& nfo, const VV& betapdf ) {
110 : : std::size_t i = 0;
111 [ + + ]: 91 : for (const auto& s : betapdf)
112 : : // cppcheck-suppress useStlAlgorithm
113 [ + - ][ + - ]: 50 : nfo.emplace_back( "beta pds [" + std::to_string(++i) + "]",
[ + - ][ + - ]
[ - + ][ - + ]
[ - + ][ - - ]
[ - - ][ - - ]
114 : 50 : parameters( s ) );
115 : 66 : }
116 : :
117 : : } // walker::
118 : :
119 : : #endif // DiffEqFactory_h
|