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 : 173 : 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 : : std::vector< char >& depv,
68 : : std::size_t nmat,
69 : : std::size_t nspec,
70 : : inciter::ctr::PDEType pde,
71 : : tk::Centering c,
72 : : std::vector< inciter::ctr::OutVar >& foutvar );
73 : :
74 : : private:
75 : : const std::string m_filename; //!< Name of file to parse
76 : :
77 : : //! Assign parameter to inputdeck entry if specified, else default
78 : : //! \tparam N Type of parameter being read/assigned
79 : : //! \param[in] table Sol-table which contains said parameter
80 : : //! \param[in] key Key for said parameter in Sol-table
81 : : //! \param[in,out] storage Storage space in inputdeck where said parameter
82 : : //! is to be stored
83 : : //! \param[in] dflt Default value of said parameter, if unspecified
84 : : template< typename N > void
85 : 7912 : storeIfSpecd(
86 : : const sol::table table,
87 : : const std::string key,
88 : : N& storage,
89 : : const N dflt )
90 : : {
91 : : auto sol_var = table[key];
92 [ + - ][ + + ]: 7912 : if (sol_var.valid())
93 : 2208 : storage = sol_var;
94 : : else
95 : 5897 : storage = dflt;
96 : 7912 : }
97 : :
98 : : //! Assign Option to inputdeck entry if specified, else default
99 : : //! \tparam Op Option-Type of parameter being read/assigned
100 : : //! \tparam OpClass Option-class of parameter being read/assigned
101 : : //! \param[in] table Sol-table which contains said parameter
102 : : //! \param[in] key Key for said parameter in Sol-table
103 : : //! \param[in,out] storage Storage space in inputdeck where said parameter
104 : : //! is to be stored
105 : : //! \param[in] dflt Default value of said parameter, if unspecified
106 : : template< typename Op, class OpClass > void
107 : 1624 : storeOptIfSpecd(
108 : : const sol::table table,
109 : : const std::string key,
110 : : Op& storage,
111 : : const Op dflt )
112 : : {
113 : 1624 : OpClass opt;
114 : : auto sol_var = table[key];
115 [ + - ][ + + ]: 1624 : if (sol_var.valid())
116 [ + + ]: 904 : storage = opt.value(sol_var);
117 : : else
118 : 810 : storage = dflt;
119 : 1624 : }
120 : :
121 : : //! Assign vector parameter to inputdeck entry if specified, else default
122 : : //! \tparam N Type of parameter vector being read/assigned
123 : : //! \param[in] table Sol-table which contains said parameter
124 : : //! \param[in] key Key for said parameter in Sol-table
125 : : //! \param[in,out] storage Storage space in inputdeck where said parameter
126 : : //! is to be stored
127 : : //! \param[in] dflt Default value of said parameter, if unspecified
128 : : template< typename N > void
129 : 3109 : storeVecIfSpecd(
130 : : const sol::table table,
131 : : const std::string key,
132 : : std::vector< N >& storage,
133 : : const std::vector< N >& dflt )
134 : : {
135 : : auto sol_vec = table[key];
136 [ + - ][ + + ]: 3109 : if (sol_vec.valid()) {
137 [ + - ][ + + ]: 8702 : for (std::size_t i=0; i<sol::table(sol_vec).size(); ++i)
138 [ + - ][ + - ]: 6710 : storage.push_back(sol_vec[i+1]);
[ - + ][ - - ]
[ - - ]
139 : : }
140 : : else
141 [ + - ]: 2113 : storage = dflt;
142 : 3109 : }
143 : :
144 : : //! Assign vector of Options to inputdeck entry if specified, else default
145 : : //! \tparam Op Option-Type of parameter being read/assigned
146 : : //! \tparam OpClass Option-class of parameter being read/assigned
147 : : //! \param[in] table Sol-table which contains said parameter
148 : : //! \param[in] key Key for said parameter in Sol-table
149 : : //! \param[in,out] storage Storage space in inputdeck where said parameter
150 : : //! is to be stored
151 : : //! \param[in] dflt Default value of said parameter, if unspecified
152 : : template< typename Op, class OpClass > void
153 : 20 : storeOptVecIfSpecd(
154 : : const sol::table table,
155 : : const std::string key,
156 : : std::vector< Op >& storage,
157 : : const std::vector< Op >& dflt )
158 : : {
159 : 20 : OpClass opt;
160 : : auto sol_vec = table[key];
161 [ + - ][ + - ]: 20 : if (sol_vec.valid()) {
162 [ + - ][ + + ]: 154 : for (std::size_t i=0; i<sol::table(sol_vec).size(); ++i)
163 [ + - ][ + - ]: 141 : storage.push_back(opt.value(sol_vec[i+1]));
[ + + ][ - + ]
[ - - ][ - - ]
[ - - ]
164 : : }
165 : : else
166 [ - - ]: 0 : storage = dflt;
167 : 20 : }
168 : :
169 : : //! \brief Check validity of keywords within a particular block
170 : : //! \tparam tags Tags addressing the said block in the input deck
171 : : //! \param[in] block Sol table of the input deck block read in from the
172 : : //! lua file
173 : : //! \param[in] blk_name Name of the block, for error clarity
174 : : template< typename... tags >
175 : 825 : void checkBlock(const sol::table& block, const std::string& blk_name) const
176 : : {
177 [ + + ]: 3941 : for (const auto& kvp : block) {
178 : 2291 : bool is_valid(false);
179 : : auto ukw = kvp.first.as<std::string>();
180 [ + + ][ - - ]: 2291 : brigand::for_each< tags... >( checkKw(ukw, is_valid) );
181 [ - + ]: 2291 : if (!is_valid)
182 [ - - ][ - - ]: 0 : Throw("Invalid keyword '" + ukw + "' in '" + blk_name + "' block.");
[ - - ][ - - ]
[ - - ][ - - ]
[ - - ][ - - ]
[ - - ][ - - ]
[ - - ][ - - ]
[ - - ][ - - ]
183 : : }
184 : 825 : }
185 : :
186 : : // Check if a keyword matches the existing ones
187 [ - - ][ - - ]: 6873 : struct checkKw {
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ - - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
188 : : std::string user_kw;
189 : : // reference to bool keeping track of kw-match
190 : : bool& is_valid;
191 : : // Constructor
192 [ - - ][ + - ]: 2291 : checkKw( const std::string& ukw, bool& isv ) :
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
193 [ - - ][ + - ]: 2291 : user_kw(ukw), is_valid(isv) {}
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
194 : : //! Function to call for each keyword type
195 : : template< typename U > void operator()( brigand::type_<U> ) {
196 : : auto spec_key = U::name();
197 : : // only check if not previously matched
198 : : if (!is_valid) {
199 : : if (user_kw == spec_key) is_valid = true;
200 : : else is_valid = false;
201 : : }
202 : : }
203 : : };
204 : : };
205 : :
206 : : } // namespace inciter
207 : :
208 : : #endif // InciterLuaParser_h
|