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