Branch data Line data Source code
1 : : // *****************************************************************************
2 : : /*!
3 : : \file src/Control/Inciter/InputDeck/LuaParser.hpp
4 : : \copyright 2019-2023 Triad National Security, LLC.
5 : : All rights reserved. See the LICENSE file for details.
6 : : \brief Inciter's lua input deck file parser
7 : : \details This file declares the input deck, i.e., control file, parser for
8 : : the computational shock hydrodynamics tool, Inciter.
9 : : */
10 : : // *****************************************************************************
11 : : #ifndef InciterLuaParser_h
12 : : #define InciterLuaParser_h
13 : :
14 : : #include "NoWarning/sol.hpp"
15 : :
16 : : #include "Inciter/CmdLine/CmdLine.hpp"
17 : : #include "InputDeck.hpp"
18 : :
19 : : namespace tk { class Print; }
20 : :
21 : : namespace inciter {
22 : :
23 : : //! \brief Control file lua-parser for Inciter.
24 : : //! \details This class is used to interface with sol2, for the purpose of
25 : : //! parsing the control file for the computational shock hydrodynamics tool,
26 : : //! Inciter.
27 : : class LuaParser {
28 : :
29 : : public:
30 : : //! Constructor
31 : : explicit LuaParser( const tk::Print& print,
32 : : const ctr::CmdLine& cmdline,
33 : : ctr::InputDeck& inputdeck );
34 : :
35 : : //! Store lua inputdeck in custom struct
36 : : void storeInputDeck(
37 : : const sol::table& lua_ideck,
38 : : ctr::InputDeck& gideck );
39 : :
40 : : //! Check and store material property into inpudeck storage
41 : : void checkStoreMatProp(
42 : : const sol::table table,
43 : : const std::string key,
44 : : std::size_t vecsize,
45 : : std::vector< tk::real >& storage );
46 : :
47 : : //! Check and store material property vector into inpudeck storage
48 : : void checkStoreMatPropVec(
49 : : const sol::table table,
50 : : const std::string key,
51 : : std::size_t nspec,
52 : : std::size_t vecsize,
53 : : std::vector<std::vector< tk::real >>& storage );
54 : :
55 : : //! Check and store material property vector of vectors into inpudeck storage
56 : : void checkStoreMatPropVecVec(
57 : : const sol::table table,
58 : : const std::string key,
59 : : std::size_t nspec,
60 : : std::size_t vecsize1,
61 : : std::size_t vecsize2,
62 : : std::vector<std::vector<std::vector< tk::real >>>& storage );
63 : :
64 : : //! Check and store field output variables
65 : : void addOutVar(
66 : : const std::string& varname,
67 : : const std::string& alias,
68 : : std::vector< char >& depv,
69 : : std::size_t nmat,
70 : : std::size_t nspec,
71 : : inciter::ctr::PDEType pde,
72 : : tk::Centering c,
73 : : std::vector< inciter::ctr::OutVar >& foutvar );
74 : :
75 : : private:
76 : : const std::string m_filename; //!< Name of file to parse
77 : :
78 : : //! Assign parameter to inputdeck entry if specified, else default
79 : : //! \tparam N Type of parameter being read/assigned
80 : : //! \param[in] table Sol-table which contains said parameter
81 : : //! \param[in] key Key for said parameter in Sol-table
82 : : //! \param[in,out] storage Storage space in inputdeck where said parameter
83 : : //! is to be stored
84 : : //! \param[in] dflt Default value of said parameter, if unspecified
85 : : template< typename N > void
86 : 7203 : storeIfSpecd(
87 : : const sol::table table,
88 : : const std::string key,
89 : : N& storage,
90 : : const N dflt )
91 : : {
92 [ + - ]: 14406 : auto sol_var = table[key];
93 [ + - ][ + + ]: 7203 : if (sol_var.valid())
94 [ + - ]: 1561 : storage = sol_var;
95 : : else
96 [ - - ]: 5642 : storage = dflt;
97 : 7203 : }
98 : :
99 : : //! Assign Option to inputdeck entry if specified, else default
100 : : //! \tparam Op Option-Type of parameter being read/assigned
101 : : //! \tparam OpClass Option-class of parameter being read/assigned
102 : : //! \param[in] table Sol-table which contains said parameter
103 : : //! \param[in] key Key for said parameter in Sol-table
104 : : //! \param[in,out] storage Storage space in inputdeck where said parameter
105 : : //! is to be stored
106 : : //! \param[in] dflt Default value of said parameter, if unspecified
107 : : template< typename Op, class OpClass > void
108 : 1739 : storeOptIfSpecd(
109 : : const sol::table table,
110 : : const std::string key,
111 : : Op& storage,
112 : : const Op dflt )
113 : : {
114 [ + - ]: 3478 : OpClass opt;
115 [ + - ]: 3478 : auto sol_var = table[key];
116 [ + - ][ + + ]: 1739 : if (sol_var.valid())
117 [ + - ][ + - ]: 872 : storage = opt.value(sol_var);
118 : : else
119 : 867 : storage = dflt;
120 : 1739 : }
121 : :
122 : : //! Assign vector parameter to inputdeck entry if specified, else default
123 : : //! \tparam N Type of parameter vector being read/assigned
124 : : //! \param[in] table Sol-table which contains said parameter
125 : : //! \param[in] key Key for said parameter in Sol-table
126 : : //! \param[in,out] storage Storage space in inputdeck where said parameter
127 : : //! is to be stored
128 : : //! \param[in] dflt Default value of said parameter, if unspecified
129 : : template< typename N > void
130 : 3242 : storeVecIfSpecd(
131 : : const sol::table table,
132 : : const std::string key,
133 : : std::vector< N >& storage,
134 : : const std::vector< N >& dflt )
135 : : {
136 [ + - ]: 6484 : auto sol_vec = table[key];
137 [ + - ][ + + ]: 3242 : if (sol_vec.valid()) {
138 [ + - ][ + - ]: 4212 : for (std::size_t i=0; i<sol::table(sol_vec).size(); ++i)
[ + + ]
139 [ + - ][ + - ]: 3365 : storage.push_back(sol_vec[i+1]);
[ + - ]
140 : : }
141 : : else
142 [ + - ]: 2395 : storage = dflt;
143 : 3242 : }
144 : :
145 : : //! Assign vector of Options to inputdeck entry if specified, else default
146 : : //! \tparam Op Option-Type of parameter being read/assigned
147 : : //! \tparam OpClass Option-class of parameter being read/assigned
148 : : //! \param[in] table Sol-table which contains said parameter
149 : : //! \param[in] key Key for said parameter in Sol-table
150 : : //! \param[in,out] storage Storage space in inputdeck where said parameter
151 : : //! is to be stored
152 : : //! \param[in] dflt Default value of said parameter, if unspecified
153 : : template< typename Op, class OpClass > void
154 : 20 : storeOptVecIfSpecd(
155 : : const sol::table table,
156 : : const std::string key,
157 : : std::vector< Op >& storage,
158 : : const std::vector< Op >& dflt )
159 : : {
160 [ + - ]: 40 : OpClass opt;
161 [ + - ]: 40 : auto sol_vec = table[key];
162 [ + - ][ + - ]: 20 : if (sol_vec.valid()) {
163 [ + - ][ + - ]: 77 : for (std::size_t i=0; i<sol::table(sol_vec).size(); ++i)
[ + + ]
164 [ + - ][ + - ]: 57 : storage.push_back(opt.value(sol_vec[i+1]));
[ + - ][ + - ]
165 : : }
166 : : else
167 [ - - ]: 0 : storage = dflt;
168 : 20 : }
169 : :
170 : : //! \brief Check validity of keywords within a particular block
171 : : //! \tparam tags Tags addressing the said block in the input deck
172 : : //! \param[in] block Sol table of the input deck block read in from the
173 : : //! lua file
174 : : //! \param[in] blk_name Name of the block, for error clarity
175 : : template< typename... tags >
176 : 786 : void checkBlock(const sol::table& block, const std::string& blk_name) const
177 : : {
178 [ + + ]: 2608 : for (const auto& kvp : block) {
179 : 1822 : bool is_valid(false);
180 [ + - ]: 3644 : auto ukw = kvp.first.as<std::string>();
181 [ + - ][ + - ]: 1822 : brigand::for_each< tags... >( checkKw(ukw, is_valid) );
182 [ - + ]: 1822 : if (!is_valid)
183 [ - - ][ - - ]: 0 : Throw("Invalid keyword '" + ukw + "' in '" + blk_name + "' block.");
[ - - ][ - - ]
[ - - ][ - - ]
184 : : }
185 : 786 : }
186 : :
187 : : // Check if a keyword matches the existing ones
188 : : struct checkKw {
189 : : std::string user_kw;
190 : : // reference to bool keeping track of kw-match
191 : : bool& is_valid;
192 : : // Constructor
193 : 1822 : checkKw( const std::string& ukw, bool& isv ) :
194 : 1822 : user_kw(ukw), is_valid(isv) {}
195 : : //! Function to call for each keyword type
196 : 20891 : template< typename U > void operator()( brigand::type_<U> ) {
197 : 20891 : auto spec_key = U::name();
198 : : // only check if not previously matched
199 [ + + ]: 20891 : if (!is_valid) {
200 [ + + ]: 8236 : if (user_kw == spec_key) is_valid = true;
201 : 6414 : else is_valid = false;
202 : : }
203 : 20891 : }
204 : : };
205 : : };
206 : :
207 : : } // namespace inciter
208 : :
209 : : #endif // InciterLuaParser_h
|