Directory: | ./ |
---|---|
File: | Core/getPotCustomized.cpp |
Date: | 2024-04-14 07:32:34 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 320 | 469 | 68.2% |
Branches: | 251 | 929 | 27.0% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | // ______ ______ _ _ _____ ______ | ||
2 | // | ____| ____| | (_)/ ____| | ____| | ||
3 | // | |__ | |__ | | _| (___ ___| |__ | ||
4 | // | __| | __| | | | |\___ \ / __| __| | ||
5 | // | | | |____| |____| |____) | (__| |____ | ||
6 | // |_| |______|______|_|_____/ \___|______| | ||
7 | // Finite Elements for Life Sciences and Engineering | ||
8 | // | ||
9 | // License: LGL2.1 License | ||
10 | // FELiScE default license: LICENSE in root folder | ||
11 | // | ||
12 | // Main authors: David Froger | ||
13 | // | ||
14 | |||
15 | // System includes | ||
16 | #include <cstdarg> | ||
17 | #include <cstdio> | ||
18 | #include <cstdlib> | ||
19 | #include <sstream> | ||
20 | |||
21 | // External includes | ||
22 | |||
23 | // Project includes | ||
24 | #include "Core/getPotCustomized.hpp" | ||
25 | |||
26 | namespace felisce | ||
27 | { | ||
28 | |||
29 | /* ================================================================ */ | ||
30 | /* ElementFieldDynamicValue */ | ||
31 | /* ================================================================ */ | ||
32 | |||
33 | std::size_t ElementFieldDynamicValue::numAllComp = 3; | ||
34 | Component ElementFieldDynamicValue::allComp[3] = {Comp1,Comp2,Comp3}; | ||
35 | |||
36 | // init,delete functions | ||
37 | |||
38 | ✗ | ElementFieldDynamicValue::ElementFieldDynamicValue(): | |
39 | ✗ | numCompMax_(3), | |
40 | ✗ | name_("Undefined"), | |
41 | ✗ | numComp_(-1) { | |
42 | ✗ | initialize("Undefined"); | |
43 | } | ||
44 | |||
45 | 6 | ElementFieldDynamicValue::ElementFieldDynamicValue(const char *aName): | |
46 | 6 | numCompMax_(3), | |
47 |
1/2✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
|
6 | name_("Undefined"), |
48 |
6/6✓ Branch 1 taken 18 times.
✓ Branch 2 taken 6 times.
✓ Branch 4 taken 18 times.
✓ Branch 5 taken 6 times.
✓ Branch 7 taken 18 times.
✓ Branch 8 taken 6 times.
|
60 | numComp_(-1) { |
49 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | initialize(aName); |
50 |
0/12✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
|
6 | } |
51 | |||
52 | ✗ | ElementFieldDynamicValue::~ElementFieldDynamicValue() = default; | |
53 | |||
54 | 6 | void ElementFieldDynamicValue::initialize(const char *aName) { | |
55 | 6 | name_ = aName; | |
56 | |||
57 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 6 times.
|
24 | for (int iComp=0 ; iComp<numCompMax_ ; ++iComp) { |
58 | 18 | typeValueOfElementField_[iComp] = (TypeValueOfElementField) -1; | |
59 | 18 | constant_[iComp] = -9999.; | |
60 | 18 | fileName_[iComp] = "Undefined"; | |
61 | 18 | functionName_[iComp] = "Undefined"; | |
62 | 18 | isReady_[iComp] = false; | |
63 | } | ||
64 | 6 | } | |
65 | |||
66 | // std::set functions | ||
67 | 6 | void ElementFieldDynamicValue::setElementFieldType(ElementFieldType type) { | |
68 | 6 | elementFieldType_ = type; | |
69 | 6 | } | |
70 | |||
71 | 10 | void ElementFieldDynamicValue::setTypeValueOfElementField(TypeValueOfElementField type, Component Comp) { | |
72 | 10 | int iComp = CompToIndex_(Comp); | |
73 | 10 | typeValueOfElementField_[iComp] = type; | |
74 | 10 | } | |
75 | |||
76 | 6 | void ElementFieldDynamicValue::setNumComp(int numCmp) { | |
77 | 6 | numComp_ = numCmp; | |
78 | 6 | } | |
79 | |||
80 | 6 | void ElementFieldDynamicValue::setConstant(double constVal, Component Comp) { | |
81 | 6 | int iComp = CompToIndex_(Comp); | |
82 | 6 | constant_[iComp] = constVal; | |
83 | 6 | isReady_[iComp] = true; | |
84 | 6 | } | |
85 | |||
86 | ✗ | void ElementFieldDynamicValue::setFileName(std::string filename, Component Comp) { | |
87 | ✗ | int iComp = CompToIndex_(Comp); | |
88 | ✗ | fileName_[iComp] = filename; | |
89 | } | ||
90 | |||
91 | 4 | void ElementFieldDynamicValue::setFunctionName(std::string funcName, Component Comp) { | |
92 | 4 | int iComp = CompToIndex_(Comp); | |
93 | 4 | functionName_[iComp] = funcName; | |
94 | 4 | } | |
95 | |||
96 | 4 | void ElementFieldDynamicValue::setCallbackXYZT(const CallbackXYZT &callabackXYZT, Component Comp) { | |
97 | 4 | int iComp = CompToIndex_(Comp); | |
98 | |||
99 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
|
4 | if ( typeValueOfElementField(Comp) != FROM_FUNCTION) { |
100 | ✗ | std::ostringstream msg; | |
101 | msg << "Attemp to std::set Component '" | ||
102 | ✗ | << strComponent[(int)Comp] << "' " | |
103 | << "of ElementFieldDynamicValue with a callback, " | ||
104 | << "but value type for this component is " | ||
105 | ✗ | << strTypeValueOfElementField[(int)Comp] << std::endl; | |
106 | ✗ | FEL_ERROR(msg.str()); | |
107 | } | ||
108 | |||
109 | 4 | callbackXYZT_[iComp] = callabackXYZT; | |
110 | 4 | isReady_[iComp] = true; | |
111 | 4 | } | |
112 | |||
113 | |||
114 | // access functions | ||
115 | |||
116 | ✗ | std::string ElementFieldDynamicValue::name() { | |
117 | ✗ | return name_; | |
118 | } | ||
119 | |||
120 | 40960 | ElementFieldType ElementFieldDynamicValue::elementFieldType() { | |
121 | 40960 | return elementFieldType_; | |
122 | } | ||
123 | |||
124 | 24588 | TypeValueOfElementField ElementFieldDynamicValue::typeValueOfElementField(Component Comp) { | |
125 | 24588 | int iComp = CompToIndex_(Comp); | |
126 | 24588 | return typeValueOfElementField_[iComp]; | |
127 | } | ||
128 | |||
129 | 57360 | int ElementFieldDynamicValue::numComp() const { | |
130 | 57360 | return numComp_; | |
131 | } | ||
132 | |||
133 | 10 | double ElementFieldDynamicValue::constant(Component Comp) { | |
134 | 10 | int iComp = CompToIndex_(Comp); | |
135 | 10 | return constant_[iComp]; | |
136 | } | ||
137 | |||
138 | ✗ | std::string ElementFieldDynamicValue::fileName(Component Comp) { | |
139 | ✗ | int iComp = CompToIndex_(Comp); | |
140 | ✗ | return fileName_[iComp]; | |
141 | } | ||
142 | |||
143 | 8 | std::string ElementFieldDynamicValue::functionName(Component Comp) { | |
144 | 8 | int iComp = CompToIndex_(Comp); | |
145 | 8 | return functionName_[iComp]; | |
146 | } | ||
147 | |||
148 | 24576 | CallbackXYZT ElementFieldDynamicValue::callbackXYZT(Component Comp) { | |
149 | 24576 | int iComp = CompToIndex_(Comp); | |
150 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24576 times.
|
24576 | FEL_ASSERT( isReady_[iComp] ); |
151 | 24576 | return callbackXYZT_[iComp]; | |
152 | } | ||
153 | |||
154 | // other public functions | ||
155 | |||
156 | ✗ | void ElementFieldDynamicValue::display() { | |
157 | |||
158 | // title | ||
159 | ✗ | std::cout << "ElementFieldDynamicValue" << std::endl; | |
160 | |||
161 | |||
162 | // name | ||
163 | std::cout << " name : " | ||
164 | ✗ | << name_ | |
165 | ✗ | << std::endl; | |
166 | |||
167 | // elementFieldType | ||
168 | ✗ | int itype = (int) elementFieldType_; | |
169 | ✗ | FEL_ASSERT_LT( -2, itype); | |
170 | ✗ | FEL_ASSERT_LT( itype, (int) numElementFieldType ); | |
171 | std::cout << " elementFieldType : " | ||
172 | ✗ | << (itype==-1 ? "Undefined" : strElementFieldType[itype]) | |
173 | ✗ | << std::endl; | |
174 | |||
175 | // numComp | ||
176 | ✗ | FEL_ASSERT_LT(numComp_,numCompMax_+1); | |
177 | std::cout << " numComp : " | ||
178 | ✗ | << (numComp_==-1 ? "Undefined" : std::to_string(numComp_).c_str()) | |
179 | ✗ | << std::endl; | |
180 | |||
181 | int vtype; | ||
182 | |||
183 | ✗ | for (int iComp=0 ; iComp<numComp_ ; ++iComp) { | |
184 | |||
185 | ✗ | std::cout << "" << " iComp = " << iComp+1 << std::endl; | |
186 | |||
187 | // typeValueOfElementField | ||
188 | ✗ | vtype = (int) typeValueOfElementField_[iComp]; | |
189 | ✗ | FEL_ASSERT_LT( -2, vtype); | |
190 | ✗ | FEL_ASSERT_LT( vtype, (int) numTypeValueOfElementField); | |
191 | std::cout << " typeValueOfElementField : " | ||
192 | ✗ | << (vtype==-1 ? "Undefined" : strTypeValueOfElementField[vtype]) | |
193 | ✗ | << std::endl; | |
194 | |||
195 | ✗ | switch (vtype) { | |
196 | ✗ | case FROM_CONSTANT: | |
197 | ✗ | std::cout << " Constant : " << constant_[iComp] << std::endl; | |
198 | ✗ | break; | |
199 | ✗ | case FROM_FILE: | |
200 | ✗ | std::cout << " fileName : " << fileName_[iComp] << std::endl; | |
201 | ✗ | break; | |
202 | ✗ | case FROM_FUNCTION: | |
203 | ✗ | std::cout << " functionName : " << functionName_[iComp] << std::endl; | |
204 | ✗ | break; | |
205 | ✗ | default: | |
206 | // We have check this could not append | ||
207 | ✗ | FEL_ERROR("There is a bug with TypeValueOfElementField."); | |
208 | break; | ||
209 | } | ||
210 | |||
211 | ✗ | std::cout << " isReady : " << (isReady_[iComp] ? "Yes" : "No") << std::endl; | |
212 | |||
213 | } | ||
214 | } | ||
215 | |||
216 | // private function | ||
217 | 49206 | int ElementFieldDynamicValue::CompToIndex_(Component Comp) { | |
218 | 49206 | int index = 0; | |
219 | |||
220 |
3/5✓ Branch 0 taken 49194 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
49206 | switch (Comp) { |
221 | 49194 | case Comp1: | |
222 | 49194 | index = 0; | |
223 | 49194 | break; | |
224 | 6 | case Comp2: | |
225 | 6 | index = 1; | |
226 | 6 | break; | |
227 | 6 | case Comp3: | |
228 | 6 | index = 2; | |
229 | 6 | break; | |
230 | ✗ | case CompNA: | |
231 | case Comp12: | ||
232 | case Comp13: | ||
233 | case Comp23: | ||
234 | case Comp123: | ||
235 | ✗ | std::ostringstream msg; | |
236 | msg << "ElementFieldDynamicValue Component must be Comp1, Comp2, or Comp3, " | ||
237 | ✗ | << "got " << (int) Comp << "."; | |
238 | ✗ | FEL_ERROR(msg.str()); | |
239 | } | ||
240 | |||
241 | 49206 | return index; | |
242 | } | ||
243 | |||
244 | /* ================================================================ */ | ||
245 | /* constructor, desctructor, initalize */ | ||
246 | /* ================================================================ */ | ||
247 | |||
248 | 465 | GetPotCustomized::GetPotCustomized(): | |
249 | 465 | m_getpotFile(nullptr), | |
250 | 465 | m_getpotCl(nullptr), | |
251 |
1/2✓ Branch 2 taken 465 times.
✗ Branch 3 not taken.
|
465 | m_filename("data"), |
252 |
1/2✓ Branch 4 taken 465 times.
✗ Branch 5 not taken.
|
930 | prefixOptionFelisce("--felisce-") { |
253 | 465 | } | |
254 | |||
255 | 465 | void GetPotCustomized::initialize(int argc,char **argv,std::string filename) { | |
256 |
1/2✓ Branch 1 taken 465 times.
✗ Branch 2 not taken.
|
465 | m_filename = filename; |
257 | |||
258 | //check file exists | ||
259 |
1/2✓ Branch 2 taken 465 times.
✗ Branch 3 not taken.
|
465 | std::ifstream tmpFile(m_filename.c_str()); |
260 |
2/4✓ Branch 1 taken 465 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 465 times.
|
465 | if (! tmpFile.good() ) { |
261 | ✗ | std::ostringstream msg; | |
262 | ✗ | msg << "Bad data file '" << m_filename << "'. " | |
263 | ✗ | << "File does not exists.'"; | |
264 | ✗ | FEL_ERROR(msg.str()); | |
265 | } | ||
266 |
1/2✓ Branch 1 taken 465 times.
✗ Branch 2 not taken.
|
465 | tmpFile.close(); |
267 | |||
268 |
6/12✓ Branch 2 taken 465 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 465 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 465 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 465 times.
✗ Branch 12 not taken.
✓ Branch 14 taken 465 times.
✗ Branch 15 not taken.
✓ Branch 17 taken 465 times.
✗ Branch 18 not taken.
|
465 | m_getpotFile = new GetPot(filename.c_str()); |
269 |
3/6✓ Branch 1 taken 465 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 465 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 465 times.
✗ Branch 8 not taken.
|
465 | m_getpotCl = new GetPot(argc, argv); |
270 | |||
271 | 465 | } | |
272 | |||
273 | 465 | GetPotCustomized::~GetPotCustomized() { | |
274 |
1/2✓ Branch 0 taken 465 times.
✗ Branch 1 not taken.
|
465 | if(m_getpotFile) { |
275 |
1/2✓ Branch 0 taken 465 times.
✗ Branch 1 not taken.
|
465 | delete m_getpotFile; |
276 | 465 | m_getpotFile=nullptr; | |
277 | } | ||
278 |
1/2✓ Branch 0 taken 465 times.
✗ Branch 1 not taken.
|
465 | if(m_getpotCl) { |
279 |
1/2✓ Branch 0 taken 465 times.
✗ Branch 1 not taken.
|
465 | delete m_getpotCl; |
280 | 465 | m_getpotCl=nullptr; | |
281 | } | ||
282 | 465 | } | |
283 | |||
284 | /* ================================================================ */ | ||
285 | /* get scalar */ | ||
286 | /* ================================================================ */ | ||
287 | |||
288 | // std::string | ||
289 | 28840 | void GetPotCustomized::get(std::string& result, const char *name, const char *defaultValue, const char *help) { | |
290 |
1/2✓ Branch 1 taken 28840 times.
✗ Branch 2 not taken.
|
28840 | std::string paramFromFile = m_paramFromFile(name); |
291 |
1/2✓ Branch 1 taken 28840 times.
✗ Branch 2 not taken.
|
28840 | std::string paramFromCl = m_paramFromCl(name); |
292 | |||
293 |
3/6✓ Branch 2 taken 28840 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 28840 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 28840 times.
✗ Branch 9 not taken.
|
28840 | result = (*m_getpotFile)( paramFromFile.c_str(), defaultValue); |
294 |
3/6✓ Branch 3 taken 28840 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 28840 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 28840 times.
✗ Branch 10 not taken.
|
28840 | result = (*m_getpotCl) ( paramFromCl.c_str(), result.c_str()); |
295 |
3/4✓ Branch 0 taken 28830 times.
✓ Branch 1 taken 10 times.
✓ Branch 3 taken 28830 times.
✗ Branch 4 not taken.
|
28840 | if (help != nullptr) m_addToHelp(name,"STRING",defaultValue,help); |
296 | 28840 | } | |
297 | |||
298 | // integer | ||
299 | 39525 | void GetPotCustomized::get(int& result, const char *name, int defaultValue, const char *help) { | |
300 |
1/2✓ Branch 1 taken 39525 times.
✗ Branch 2 not taken.
|
39525 | validateIntParam(name); |
301 | |||
302 |
1/2✓ Branch 1 taken 39525 times.
✗ Branch 2 not taken.
|
39525 | std::string paramFromFile = m_paramFromFile(name); |
303 |
1/2✓ Branch 1 taken 39525 times.
✗ Branch 2 not taken.
|
39525 | std::string paramFromCl = m_paramFromCl(name); |
304 | |||
305 |
2/4✓ Branch 2 taken 39525 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 39525 times.
✗ Branch 6 not taken.
|
39525 | result = (*m_getpotFile)( paramFromFile.c_str(), defaultValue); |
306 |
2/4✓ Branch 2 taken 39525 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 39525 times.
✗ Branch 6 not taken.
|
39525 | result = (*m_getpotCl) ( paramFromCl.c_str(), result); |
307 |
2/4✓ Branch 0 taken 39525 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 39525 times.
✗ Branch 4 not taken.
|
39525 | if (help != nullptr) m_addToHelp(name,"INTEGER",defaultValue,help); |
308 | 39525 | } | |
309 | |||
310 | // double | ||
311 | 106020 | void GetPotCustomized::get(double& result, const char *name, double defaultValue, const char *help) { | |
312 |
1/2✓ Branch 1 taken 106020 times.
✗ Branch 2 not taken.
|
106020 | validateDoubleParam(name); |
313 | |||
314 |
1/2✓ Branch 1 taken 106020 times.
✗ Branch 2 not taken.
|
106020 | std::string paramFromFile = m_paramFromFile(name); |
315 |
1/2✓ Branch 1 taken 106020 times.
✗ Branch 2 not taken.
|
106020 | std::string paramFromCl = m_paramFromCl(name); |
316 | |||
317 |
2/4✓ Branch 2 taken 106020 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 106020 times.
✗ Branch 6 not taken.
|
106020 | result = (*m_getpotFile)( paramFromFile.c_str(), defaultValue); |
318 |
2/4✓ Branch 2 taken 106020 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 106020 times.
✗ Branch 6 not taken.
|
106020 | result = (*m_getpotCl) ( paramFromCl.c_str(), result); |
319 |
2/4✓ Branch 0 taken 106020 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 106020 times.
✗ Branch 4 not taken.
|
106020 | if (help != nullptr) m_addToHelp(name,"DOUBLE",defaultValue,help); |
320 | 106020 | } | |
321 | |||
322 | // boolean | ||
323 | 65100 | void GetPotCustomized::get(bool& result, const char *name, bool defaultValue, const char *help) { | |
324 |
1/2✓ Branch 1 taken 65100 times.
✗ Branch 2 not taken.
|
65100 | std::string paramFromFile = m_paramFromFile(name); |
325 |
1/2✓ Branch 1 taken 65100 times.
✗ Branch 2 not taken.
|
65100 | std::string paramFromCl = m_paramFromCl(name); |
326 | |||
327 | 65100 | std::string resultStr; | |
328 |
4/8✓ Branch 1 taken 65100 times.
✗ Branch 2 not taken.
✓ Branch 6 taken 65100 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 65100 times.
✗ Branch 10 not taken.
✓ Branch 12 taken 65100 times.
✗ Branch 13 not taken.
|
65100 | resultStr = (*m_getpotFile)( paramFromFile.c_str(), bool2string(defaultValue).c_str() ); |
329 |
3/6✓ Branch 3 taken 65100 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 65100 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 65100 times.
✗ Branch 10 not taken.
|
65100 | resultStr = (*m_getpotCl) ( paramFromCl.c_str(), resultStr.c_str() ); |
330 | |||
331 | // convert std::string value to bool value | ||
332 | try { | ||
333 |
1/2✓ Branch 1 taken 65100 times.
✗ Branch 2 not taken.
|
65100 | result = string2bool(resultStr); |
334 | ✗ | } catch(...) { | |
335 | ✗ | FEL_ERROR( "In file '" + m_filename | |
336 | + "', section '" + m_section | ||
337 | + "', parameter '" + name | ||
338 | + "' expecting double, got '" + resultStr | ||
339 | + "'." | ||
340 | ); | ||
341 | } | ||
342 | |||
343 |
3/4✓ Branch 0 taken 64635 times.
✓ Branch 1 taken 465 times.
✓ Branch 3 taken 64635 times.
✗ Branch 4 not taken.
|
65100 | if (help != nullptr) m_addToHelp(name,"[true|false]",defaultValue,help); |
344 | 65100 | } | |
345 | |||
346 | /* ================================================================ */ | ||
347 | /* get std::vector */ | ||
348 | /* ================================================================ */ | ||
349 | |||
350 | // std::string std::vector | ||
351 | 21855 | void GetPotCustomized::get(std::vector<std::string>& result, const char *name, const char *defaultValue, const char *help) { | |
352 | |||
353 | 21855 | std::string onestring; | |
354 | |||
355 |
1/2✓ Branch 1 taken 21855 times.
✗ Branch 2 not taken.
|
21855 | std::string paramFromFile = m_paramFromFile(name); |
356 |
1/2✓ Branch 1 taken 21855 times.
✗ Branch 2 not taken.
|
21855 | std::string paramFromCl = m_paramFromCl(name); |
357 | |||
358 |
3/6✓ Branch 2 taken 21855 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 21855 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 21855 times.
✗ Branch 9 not taken.
|
21855 | onestring = (*m_getpotFile)( paramFromFile.c_str(), defaultValue); |
359 |
3/6✓ Branch 3 taken 21855 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 21855 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 21855 times.
✗ Branch 10 not taken.
|
21855 | onestring = (*m_getpotCl) ( paramFromCl.c_str(), onestring.c_str()); |
360 | |||
361 | // split "foo bar baz" into {"foo", "bar", "baz"} | ||
362 |
2/4✓ Branch 2 taken 21855 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 21855 times.
✗ Branch 6 not taken.
|
21855 | split(onestring,result," \n\t"); |
363 |
2/4✓ Branch 0 taken 21855 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 21855 times.
✗ Branch 4 not taken.
|
21855 | if (help != nullptr) m_addToHelp(name,"STRING VECTOR",defaultValue,help); |
364 | 21855 | } | |
365 | |||
366 | // integer std::vector | ||
367 | 39990 | void GetPotCustomized::get(std::vector<int>& result, const char *name, const char *defaultValue, const char *help) { | |
368 |
1/2✓ Branch 1 taken 39990 times.
✗ Branch 2 not taken.
|
39990 | std::string paramFromFile = m_paramFromFile(name); |
369 |
1/2✓ Branch 1 taken 39990 times.
✗ Branch 2 not taken.
|
39990 | std::string paramFromCl = m_paramFromCl(name); |
370 | |||
371 | 39990 | std::string onestring; | |
372 |
3/6✓ Branch 2 taken 39990 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 39990 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 39990 times.
✗ Branch 9 not taken.
|
39990 | onestring = (*m_getpotFile)( paramFromFile.c_str(), defaultValue); |
373 |
3/6✓ Branch 3 taken 39990 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 39990 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 39990 times.
✗ Branch 10 not taken.
|
39990 | onestring = (*m_getpotCl) ( paramFromCl.c_str(), onestring.c_str()); |
374 | |||
375 | // split "1 2 3" into {"1", "2", "3"} | ||
376 | 39990 | std::vector<std::string> vecstring; | |
377 | |||
378 |
2/4✓ Branch 2 taken 39990 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 39990 times.
✗ Branch 6 not taken.
|
39990 | split(onestring,vecstring," \n\t"); |
379 | |||
380 | |||
381 | // convert std::string elements to int elements | ||
382 | 39990 | result.clear(); | |
383 |
2/2✓ Branch 1 taken 16794 times.
✓ Branch 2 taken 39990 times.
|
56784 | for ( std::size_t i = 0; i < vecstring.size(); i++) { |
384 | try { | ||
385 |
2/4✓ Branch 2 taken 16794 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 16794 times.
✗ Branch 6 not taken.
|
16794 | result.push_back( std::stoi( vecstring[i] ) ); |
386 | ✗ | } catch( const std::invalid_argument& ) { | |
387 | ✗ | FEL_ERROR( "In file '" + m_filename | |
388 | + "', section '" + m_section | ||
389 | + "', parameter '" + name | ||
390 | + "' expecting integer, got '" + vecstring[i] | ||
391 | + "'." | ||
392 | ) ; | ||
393 | } | ||
394 | } | ||
395 |
2/4✓ Branch 0 taken 39990 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 39990 times.
✗ Branch 4 not taken.
|
39990 | if (help != nullptr) m_addToHelp(name,"INTEGER VECTOR",defaultValue,help); |
396 | 39990 | } | |
397 | |||
398 | // double std::vector | ||
399 | 16275 | void GetPotCustomized::get(std::vector<double>& result, const char *name, const char *defaultValue, const char *help) { | |
400 |
1/2✓ Branch 1 taken 16275 times.
✗ Branch 2 not taken.
|
16275 | std::string paramFromFile = m_paramFromFile(name); |
401 |
1/2✓ Branch 1 taken 16275 times.
✗ Branch 2 not taken.
|
16275 | std::string paramFromCl = m_paramFromCl(name); |
402 | |||
403 | 16275 | std::string onestring; | |
404 |
3/6✓ Branch 2 taken 16275 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 16275 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 16275 times.
✗ Branch 9 not taken.
|
16275 | onestring = (*m_getpotFile)( paramFromFile.c_str(), defaultValue); |
405 |
3/6✓ Branch 3 taken 16275 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 16275 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 16275 times.
✗ Branch 10 not taken.
|
16275 | onestring = (*m_getpotCl) ( paramFromCl.c_str(), onestring.c_str()); |
406 | |||
407 | // split "1.0 2.0 3.0" into {"1.0", "2.0", "3.0"} | ||
408 | 16275 | std::vector<std::string> vecstring; | |
409 |
2/4✓ Branch 2 taken 16275 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 16275 times.
✗ Branch 6 not taken.
|
16275 | split(onestring,vecstring," \n\t"); |
410 | |||
411 | // convert std::string elements to double elements | ||
412 | 16275 | result.clear(); | |
413 |
2/2✓ Branch 1 taken 15252 times.
✓ Branch 2 taken 16275 times.
|
31527 | for ( std::size_t i = 0; i < vecstring.size(); i++) { |
414 | try { | ||
415 |
2/4✓ Branch 2 taken 15252 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 15252 times.
✗ Branch 6 not taken.
|
15252 | result.push_back( std::stod( vecstring[i] ) ); |
416 | ✗ | } catch( const std::invalid_argument& ) { | |
417 | ✗ | FEL_ERROR( "In file '" + m_filename | |
418 | + "', section '" + m_section | ||
419 | + "', parameter '" + name | ||
420 | + "' expecting double, got '" + vecstring[i] | ||
421 | + "'." | ||
422 | ) ; | ||
423 | } | ||
424 | } | ||
425 |
2/4✓ Branch 0 taken 16275 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 16275 times.
✗ Branch 4 not taken.
|
16275 | if (help != nullptr) m_addToHelp(name,"DOUBLE VECTOR",defaultValue,help); |
426 | 16275 | } | |
427 | |||
428 | // boolean std::vector | ||
429 | 1395 | void GetPotCustomized::get(std::vector<bool>& result, const char *name, const char *defaultValue, const char *help) { | |
430 | |||
431 | 1395 | std::string onestring; | |
432 | |||
433 |
1/2✓ Branch 1 taken 1395 times.
✗ Branch 2 not taken.
|
1395 | std::string paramFromFile = m_paramFromFile(name); |
434 |
1/2✓ Branch 1 taken 1395 times.
✗ Branch 2 not taken.
|
1395 | std::string paramFromCl = m_paramFromCl(name); |
435 | |||
436 | |||
437 |
3/6✓ Branch 2 taken 1395 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1395 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 1395 times.
✗ Branch 9 not taken.
|
1395 | onestring = (*m_getpotFile)( paramFromFile.c_str(), defaultValue); |
438 |
3/6✓ Branch 3 taken 1395 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 1395 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 1395 times.
✗ Branch 10 not taken.
|
1395 | onestring = (*m_getpotCl) ( paramFromCl.c_str(), onestring.c_str()); |
439 | |||
440 | // split "true false true" into {"true", "false", "true"} | ||
441 | 1395 | std::vector<std::string> vecstring; | |
442 |
2/4✓ Branch 2 taken 1395 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1395 times.
✗ Branch 6 not taken.
|
1395 | split(onestring,vecstring," \n\t"); |
443 | |||
444 | // convert std::string elements to boolean elements | ||
445 | 1395 | result.clear(); | |
446 |
2/2✓ Branch 1 taken 1009 times.
✓ Branch 2 taken 1395 times.
|
2404 | for ( std::size_t i = 0; i < vecstring.size(); i++) { |
447 | try { | ||
448 |
2/4✓ Branch 2 taken 1009 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1009 times.
✗ Branch 6 not taken.
|
1009 | result.push_back( string2bool( vecstring[i] ) ); |
449 | } | ||
450 | |||
451 | ✗ | catch(...) { | |
452 | ✗ | FEL_ERROR( "In file '" + m_filename | |
453 | + "', section '" + m_section | ||
454 | + "', variable '" + name | ||
455 | + "' expecting 'true' or 'false', got '" + vecstring[i] | ||
456 | + "'." | ||
457 | ) ; | ||
458 | } | ||
459 | } | ||
460 |
2/4✓ Branch 0 taken 1395 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 1395 times.
✗ Branch 4 not taken.
|
1395 | if (help != nullptr) m_addToHelp(name,"BOOLEAN VECTOR",defaultValue,help); |
461 | 1395 | } | |
462 | |||
463 | /* ================================================================ */ | ||
464 | /* require scalar */ | ||
465 | /* ================================================================ */ | ||
466 | |||
467 | // std::string | ||
468 | 10 | void GetPotCustomized::require(std::string& result, const char *name, const char *help) { | |
469 | 10 | raiseErrorIfNotExists(name); | |
470 | 10 | get(result,name,"",help); | |
471 | 10 | } | |
472 | |||
473 | // integer | ||
474 | ✗ | void GetPotCustomized::require(int& result, const char *name, const char *help) { | |
475 | ✗ | raiseErrorIfNotExists(name); | |
476 | ✗ | get(result,name,0,help); | |
477 | } | ||
478 | |||
479 | // double | ||
480 | ✗ | void GetPotCustomized::require(double& result, const char *name, const char *help) { | |
481 | ✗ | raiseErrorIfNotExists(name); | |
482 | ✗ | get(result,name,0.,help); | |
483 | } | ||
484 | |||
485 | // boolean | ||
486 | ✗ | void GetPotCustomized::require(bool& result, const char *name, const char *help) { | |
487 | ✗ | raiseErrorIfNotExists(name); | |
488 | ✗ | get(result,name,false,help); | |
489 | } | ||
490 | |||
491 | /* ================================================================ */ | ||
492 | /* require std::vector */ | ||
493 | /* ================================================================ */ | ||
494 | |||
495 | // std::string std::vector | ||
496 | ✗ | void GetPotCustomized::require(std::vector<std::string>& result, const char *name, const char *help) { | |
497 | ✗ | raiseErrorIfNotExists(name); | |
498 | ✗ | get(result,name,"",help); | |
499 | } | ||
500 | |||
501 | // integer std::vector | ||
502 | ✗ | void GetPotCustomized::require(std::vector<int> & result, const char *name, const char *help) { | |
503 | ✗ | raiseErrorIfNotExists(name); | |
504 | ✗ | get(result,name,"",help); | |
505 | } | ||
506 | |||
507 | // double std::vector | ||
508 | ✗ | void GetPotCustomized::require(std::vector<double>& result, const char *name, const char *help) { | |
509 | ✗ | raiseErrorIfNotExists(name); | |
510 | ✗ | get(result,name,"",help); | |
511 | } | ||
512 | |||
513 | // boolean std::vector | ||
514 | ✗ | void GetPotCustomized::require(std::vector<bool> & result, const char *name, const char *help) { | |
515 | ✗ | raiseErrorIfNotExists(name); | |
516 | ✗ | get(result,name,"",help); | |
517 | } | ||
518 | |||
519 | /* ================================================================ */ | ||
520 | /* ElementFieldDynamicValue */ | ||
521 | /* ================================================================ */ | ||
522 | |||
523 | 465 | std::set<std::string> GetPotCustomized::getElementFieldSection() { | |
524 | |||
525 | std::vector<std::string> unusedVariables = | ||
526 |
1/2✓ Branch 1 taken 465 times.
✗ Branch 2 not taken.
|
465 | (*m_getpotFile).unidentified_variables( m_identifiedVariablesFile); |
527 | |||
528 |
1/2✓ Branch 2 taken 465 times.
✗ Branch 3 not taken.
|
465 | std::string prefix = "ElementField:"; |
529 |
1/2✓ Branch 2 taken 465 times.
✗ Branch 3 not taken.
|
465 | std::string delimiters = "/"; |
530 | |||
531 | 465 | std::vector<std::string> sectionAndName; | |
532 | 465 | std::set<std::string> elementFieldSection; | |
533 | |||
534 |
2/2✓ Branch 1 taken 16 times.
✓ Branch 2 taken 465 times.
|
481 | for (std::size_t i=0 ; i != unusedVariables.size() ; ++i) { |
535 | |||
536 |
1/2✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
|
16 | std::string variable = unusedVariables[i]; // "section/name" |
537 | |||
538 |
2/4✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 16 times.
✗ Branch 4 not taken.
|
16 | if (startswith(variable,prefix)) { |
539 | |||
540 | // split "section/name" into ["section","name"] | ||
541 |
1/2✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
|
16 | split(variable, sectionAndName, delimiters); |
542 | |||
543 |
1/2✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
|
16 | elementFieldSection.insert(sectionAndName[0]); // "section" |
544 | } | ||
545 | 16 | } | |
546 | |||
547 | 930 | return elementFieldSection; | |
548 | 465 | } | |
549 | |||
550 | 6 | ElementFieldDynamicValue* GetPotCustomized::getElementFieldDynamicValue(std::string section) { | |
551 | 6 | std::vector<std::string> words; | |
552 |
1/2✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
|
6 | const std::string space = " "; |
553 | |||
554 | // read name | ||
555 |
2/4✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
|
6 | std::string name = elementFieldNameFromSection(section); |
556 | |||
557 | // create new ElementFieldDynamicValue | ||
558 |
2/4✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 6 times.
✗ Branch 6 not taken.
|
6 | ElementFieldDynamicValue *res = new ElementFieldDynamicValue(name.c_str()); |
559 |
2/4✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
|
6 | moveToSection(section); |
560 | |||
561 | // read ElementFieldType | ||
562 | ElementFieldType elementFieldType; | ||
563 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | require(elementFieldType,"type",(int) numElementFieldType,strElementFieldType); |
564 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | res->setElementFieldType(elementFieldType); |
565 | |||
566 | // read numComp | ||
567 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | raiseErrorIfNotExists("Comp1"); |
568 | 6 | int numComp = 1; | |
569 |
3/4✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 4 times.
|
6 | if ( exists("Comp2") ) { |
570 | 2 | numComp = 2; | |
571 |
2/4✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
|
2 | if (exists("Comp3")) { |
572 | 2 | numComp = 3; | |
573 | } | ||
574 | } else { | ||
575 |
1/2✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
|
4 | raiseErrorIfExists("Comp3"); |
576 | } | ||
577 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | res->setNumComp(numComp); |
578 | |||
579 | // read TypeValueOfElementField and corresponding value | ||
580 | Component Comp; | ||
581 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 6 times.
|
16 | for (int iComp=0 ; iComp < numComp ; ++iComp) { |
582 | 10 | Comp = res->allComp[iComp]; | |
583 | |||
584 | // data file contains for example a line: | ||
585 | // Comp1 = "FROM_FUNCTION f0" | ||
586 | // (Comp1,Comp2,Comp3 authorized). | ||
587 | |||
588 | // read data file line | ||
589 |
1/2✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
|
10 | std::string strName = strComponent[Comp]; // strName = "Comp1" |
590 | 10 | std::string strValue; | |
591 |
1/2✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
|
10 | require(strValue,strName.c_str()); // strValue = "FROM_FUNCTION f0" |
592 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | split(strValue, words, space); // words = {"FROM_FUNCTION", "f0"} |
593 | |||
594 | // check right value ("FROM_FUNCTION f0") is two blank-separated words | ||
595 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
|
10 | if (words.size() != 2) { |
596 | ✗ | std::ostringstream msg; | |
597 | ✗ | msg << "Bad data file '" << m_filename << "'. " | |
598 | ✗ | << "'In section '" << m_section | |
599 | << "', parameter '" << strName | ||
600 | << "' expected: 'FROM_[CONSTANT|FILE|FUNCTION] value', " | ||
601 | ✗ | << "got: '" << strValue << "'."; | |
602 | ✗ | FEL_ERROR(msg.str()); | |
603 | } | ||
604 | |||
605 | // convert typeValueOfElementField std::string to enum ("FROM_FUNCTION" -> FROM_FUNCTION) | ||
606 |
1/2✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
|
10 | int buf = ppcharIndex( |
607 | numTypeValueOfElementField, | ||
608 | strTypeValueOfElementField, | ||
609 | 10 | words[0].c_str()); | |
610 | |||
611 | // check the conversion is successfull | ||
612 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
|
10 | if (buf == -1) { |
613 | ✗ | std::ostringstream msg; | |
614 | ✗ | msg << "Bad data file '" << m_filename << "'. " | |
615 | ✗ | << "'In section '" << m_section | |
616 | << "', parameter '" << strName | ||
617 | << "' expected: 'FROM_[CONSTANT|FILE|FUNCTION]', " | ||
618 | ✗ | << "got: '" << words[0] << "'."; | |
619 | ✗ | FEL_ERROR(msg.str()); | |
620 | } | ||
621 | |||
622 | 10 | TypeValueOfElementField typeValueOfElementField = static_cast<TypeValueOfElementField>(buf); | |
623 | |||
624 | |||
625 | // record typeValueOfElementField in the object that the function will return | ||
626 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | res->setTypeValueOfElementField(typeValueOfElementField,Comp); |
627 | |||
628 | //check elementFieldType and typeValueOfElementField are compatible | ||
629 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 4 times.
|
10 | if (elementFieldType==CONSTANT_FIELD) { |
630 |
2/4✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
|
6 | if (typeValueOfElementField==FROM_FUNCTION || |
631 | typeValueOfElementField==FROM_FILE) { | ||
632 | ✗ | std::ostringstream msg; | |
633 | ✗ | msg << "Bad data file '" << m_filename << "'. " | |
634 | ✗ | << "'In section '" << m_section | |
635 | << "', parameter '" << strName | ||
636 | << "', only FROM_CONSTANT are allowed for CONSTANT_FIELD, " | ||
637 | ✗ | << "got: '" << strTypeValueOfElementField[typeValueOfElementField] << "'."; | |
638 | ✗ | FEL_ERROR(msg.str()); | |
639 | } | ||
640 | } | ||
641 | |||
642 | // read and record correponding value ("f0") | ||
643 |
2/4✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
|
10 | switch (typeValueOfElementField) { |
644 | 6 | case FROM_CONSTANT: | |
645 | try { | ||
646 |
1/2✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
|
6 | const double constant = std::stod(words[1]); |
647 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | res->setConstant(constant,Comp); |
648 | ✗ | } catch( const std::invalid_argument&) { | |
649 | ✗ | FEL_ERROR( "In file '" + m_filename | |
650 | + "', section '" + m_section | ||
651 | + "', parameter '" + name | ||
652 | + "' expecting double, got '" + words[1] | ||
653 | + "'." | ||
654 | ) ; | ||
655 | } | ||
656 | 6 | break; | |
657 | ✗ | case FROM_FILE: | |
658 | ✗ | res->setFileName(words[1],Comp); | |
659 | ✗ | break; | |
660 | 4 | case FROM_FUNCTION: | |
661 |
2/4✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 4 times.
✗ Branch 6 not taken.
|
4 | res->setFunctionName(words[1],Comp); |
662 | 4 | break; | |
663 | // Default case should appear with a warning at compile time instead of an error in runtime | ||
664 | // (that's truly the point of using enums as switch cases) | ||
665 | // default: | ||
666 | // We have check this could not append | ||
667 | // FEL_ERROR("There is a bug with TypeValueOfElementField."); | ||
668 | // break; | ||
669 | } | ||
670 | 10 | } | |
671 | |||
672 | 6 | return res; | |
673 | 6 | } | |
674 | |||
675 | 12 | std::string GetPotCustomized::elementFieldNameFromSection(std::string section) { | |
676 | |||
677 | 12 | std::string name; | |
678 | |||
679 | // check that section std::string is like "ElementField_thename" | ||
680 |
1/2✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
|
12 | std::string prefix = "ElementField:"; |
681 |
4/8✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 12 times.
✗ Branch 4 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 12 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 12 times.
|
12 | if (! startswith(section,prefix) || section.length() <= prefix.length() ) { |
682 | ✗ | std::ostringstream msg; | |
683 | ✗ | msg << "Bad data file '" << m_filename << "'. " | |
684 | << "Failed to extract elementField name from section '" | ||
685 | ✗ | << section << "'."; | |
686 | |||
687 | ✗ | FEL_ERROR(msg.str()); | |
688 | } | ||
689 | |||
690 | // get "thename" from "ElementField:thename" | ||
691 |
1/2✓ Branch 4 taken 12 times.
✗ Branch 5 not taken.
|
12 | name = section.substr(prefix.length(), section.length()-prefix.length() ); |
692 | |||
693 | 24 | return name; | |
694 | 12 | } | |
695 | |||
696 | /* ================================================================ */ | ||
697 | /* various functions */ | ||
698 | /* ================================================================ */ | ||
699 | |||
700 | // move to a section | ||
701 | 33021 | void GetPotCustomized:: moveToSection(std::string section) { | |
702 | 33021 | m_section = section; | |
703 | 33021 | } | |
704 | |||
705 | // detect ufos | ||
706 | 465 | void GetPotCustomized::checkUndefinedVariable() { | |
707 | |||
708 |
1/2✓ Branch 1 taken 465 times.
✗ Branch 2 not taken.
|
465 | m_identifiedVariablesCl.emplace_back("--felisce-cfg"); |
709 | |||
710 | // configuration file ufos | ||
711 | std::vector<std::string> variable_ufos = | ||
712 |
1/2✓ Branch 1 taken 465 times.
✗ Branch 2 not taken.
|
465 | (*m_getpotFile).unidentified_variables( m_identifiedVariablesFile); |
713 | |||
714 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 465 times.
|
465 | if (variable_ufos.size()) { |
715 | ✗ | std::cout << "Incorrect input file, the following variables are undefined:" << std::endl; | |
716 | ✗ | for (auto it = variable_ufos.begin(); it != variable_ufos.end(); ++it) { | |
717 | ✗ | std::cout << " " << *it << std::endl ; | |
718 | } | ||
719 | ✗ | FEL_ERROR("'Incorrect input file.'"); | |
720 | } | ||
721 | |||
722 | // command line ufos | ||
723 | 465 | std::vector<std::string> current; | |
724 | 465 | std::vector<std::string> all; | |
725 | 465 | std::vector<std::string> empty; | |
726 | |||
727 | // the follwing suffer strange getpot bugs, but it's almost good | ||
728 | // it may become wrong if getpot bugs are fixed... | ||
729 | |||
730 | // variables | ||
731 |
1/2✓ Branch 1 taken 465 times.
✗ Branch 2 not taken.
|
465 | all = (*m_getpotCl).unidentified_variables(m_identifiedVariablesCl); |
732 | |||
733 | // options | ||
734 |
1/2✓ Branch 1 taken 465 times.
✗ Branch 2 not taken.
|
465 | current = (*m_getpotCl).unidentified_options(); |
735 |
1/2✗ Branch 4 not taken.
✓ Branch 5 taken 465 times.
|
465 | for (auto it = current.begin(); it != current.end(); ++it) { |
736 | ✗ | if ( it->find('=') == std::string::npos ) | |
737 | ✗ | all.push_back( *it ); | |
738 | } | ||
739 | |||
740 | // nominuses | ||
741 |
1/2✓ Branch 1 taken 465 times.
✗ Branch 2 not taken.
|
465 | current = (*m_getpotCl).unidentified_nominuses(m_identifiedVariablesCl); |
742 |
1/2✓ Branch 5 taken 465 times.
✗ Branch 6 not taken.
|
465 | all.insert( all.end(), current.begin(), current.end() ); |
743 | |||
744 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 465 times.
|
465 | if ( all.size()) { |
745 | ✗ | std::cout << "The following options passed to command line are incorrect:" << std::endl; | |
746 | |||
747 | ✗ | for (auto it = all.begin(); it != all.end(); ++it) | |
748 | ✗ | std::cout << " " << *it << std::endl ; | |
749 | |||
750 | ✗ | std::cout << std::endl << "Good syntax is for example (use --help for a list of section and variable):" << std::endl; | |
751 | ✗ | std::cout << "--felisce-section-variable='astring'" << std::endl; | |
752 | ✗ | std::cout << "--felisce-section-variable='true'" << std::endl; | |
753 | ✗ | std::cout << "--felisce-section-variable='1. 2. 3.'" << std::endl << std::endl; | |
754 | ✗ | FEL_ERROR("'Incorrect command line option(s)"); | |
755 | } | ||
756 | 465 | } | |
757 | |||
758 | // check that blank-separated std::string lists have the same lengths | ||
759 | 465 | void GetPotCustomized::assertSameLength(int count, ...) { | |
760 | |||
761 | va_list args; | ||
762 | char *name; | ||
763 | 465 | char *name_ref = nullptr; | |
764 | 465 | std::size_t length, length_ref = 0.; | |
765 | |||
766 | 465 | va_start(args,count); | |
767 | |||
768 | // getting the length of the first blank-separated std::string list. | ||
769 | // this will be the reference | ||
770 |
1/2✓ Branch 0 taken 465 times.
✗ Branch 1 not taken.
|
465 | if (count>0) { |
771 | 465 | name_ref = va_arg(args,char*); | |
772 |
1/2✓ Branch 1 taken 465 times.
✗ Branch 2 not taken.
|
465 | length_ref = getLength(name_ref); |
773 | } | ||
774 | |||
775 | // comparing all the others lengths with the reference. | ||
776 | // if one length is not equal to the reference, this is an error. | ||
777 |
2/2✓ Branch 0 taken 465 times.
✓ Branch 1 taken 465 times.
|
930 | for (int i = 1; i < count; i++) { |
778 | 465 | name = va_arg(args,char*); | |
779 |
1/2✓ Branch 1 taken 465 times.
✗ Branch 2 not taken.
|
465 | length = getLength(name); |
780 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 465 times.
|
465 | if (length != length_ref) { |
781 | ✗ | std::ostringstream msg; | |
782 | ✗ | msg << "bad input file: "; | |
783 | ✗ | msg << "In section '" << m_section << "', "; | |
784 | ✗ | msg << "'" << name << "' (length=" << length << ")"; | |
785 | ✗ | msg << " and '" << name_ref << "' (length=" << length_ref << ")"; | |
786 | ✗ | msg << " must have the same length,"; | |
787 | ✗ | FEL_ERROR(msg.str()); | |
788 | } | ||
789 | } | ||
790 | |||
791 | 465 | va_end(args); | |
792 | 465 | } | |
793 | |||
794 | 465 | std::unordered_map<std::string, std::vector<std::string> > GetPotCustomized::getHelp() { | |
795 | 465 | return m_help; | |
796 | } | ||
797 | |||
798 | 34 | bool GetPotCustomized::exists(const char *name) { | |
799 | 34 | std::string valueStr; | |
800 | |||
801 |
1/2✓ Branch 1 taken 34 times.
✗ Branch 2 not taken.
|
34 | std::string paramFromFile = m_paramFromFile(name); |
802 |
1/2✓ Branch 1 taken 34 times.
✗ Branch 2 not taken.
|
34 | std::string paramFromCl = m_paramFromCl(name); |
803 | |||
804 | |||
805 | // check that the value read as a std::string is not "" | ||
806 |
3/6✓ Branch 2 taken 34 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 34 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 34 times.
✗ Branch 9 not taken.
|
34 | valueStr = (*m_getpotFile)( paramFromFile.c_str(), ""); |
807 |
3/6✓ Branch 3 taken 34 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 34 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 34 times.
✗ Branch 10 not taken.
|
34 | valueStr = (*m_getpotCl) ( paramFromCl.c_str(), valueStr.c_str()); |
808 | |||
809 |
2/2✓ Branch 1 taken 8 times.
✓ Branch 2 taken 26 times.
|
34 | if (valueStr=="") |
810 | 8 | return false; | |
811 | else | ||
812 | 26 | return true; | |
813 | 34 | } | |
814 | |||
815 | |||
816 | 22 | void GetPotCustomized::raiseErrorIfNotExists(const char *name) { | |
817 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 22 times.
|
22 | if (! exists(name) ) { |
818 | ✗ | std::ostringstream msg; | |
819 | ✗ | msg << "Bad data file '" << m_filename << "':" | |
820 | ✗ | << "In section '" << m_section << "', " | |
821 | ✗ | << "'" << name << "' is recquired."; | |
822 | ✗ | FEL_ERROR(msg.str().c_str()); | |
823 | } | ||
824 | 22 | } | |
825 | |||
826 | 4 | void GetPotCustomized::raiseErrorIfExists(const char *name) { | |
827 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
|
4 | if ( exists(name) ) { |
828 | ✗ | std::ostringstream msg; | |
829 | ✗ | msg << "Bad data file '" << m_filename << "':" | |
830 | ✗ | << "In section '" << m_section << "', " | |
831 | ✗ | << "'" << name << "' is incompatible with other data."; | |
832 | ✗ | FEL_ERROR(msg.str().c_str()); | |
833 | } | ||
834 | 4 | } | |
835 | // get the length of a blank-separated std::string list | ||
836 | 930 | std::size_t GetPotCustomized::getLength(const char *name) { | |
837 |
2/4✓ Branch 1 taken 930 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 930 times.
✗ Branch 5 not taken.
|
930 | std::string path = m_section + '/' + name; |
838 |
3/6✓ Branch 3 taken 930 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 930 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 930 times.
✗ Branch 10 not taken.
|
1860 | std::string str = (*m_getpotFile)(path.c_str(),""); |
839 | 930 | std::vector <std::string> vstring; | |
840 |
2/4✓ Branch 2 taken 930 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 930 times.
✗ Branch 6 not taken.
|
930 | split(str,vstring," \n\t"); |
841 | 1860 | return vstring.size(); | |
842 | 930 | } | |
843 | |||
844 | 39525 | void GetPotCustomized::validateIntParam(const char* name) { | |
845 | 39525 | std::string resultStr; | |
846 | |||
847 |
1/2✓ Branch 1 taken 39525 times.
✗ Branch 2 not taken.
|
39525 | std::string paramFromFile = m_paramFromFile(name); |
848 |
1/2✓ Branch 1 taken 39525 times.
✗ Branch 2 not taken.
|
39525 | std::string paramFromCl = m_paramFromCl(name); |
849 | |||
850 | |||
851 |
3/6✓ Branch 2 taken 39525 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 39525 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 39525 times.
✗ Branch 9 not taken.
|
39525 | resultStr = (*m_getpotFile)( paramFromFile.c_str(), "1"); |
852 |
3/6✓ Branch 3 taken 39525 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 39525 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 39525 times.
✗ Branch 10 not taken.
|
39525 | resultStr = (*m_getpotCl) ( paramFromCl.c_str(), resultStr.c_str()); |
853 | try { | ||
854 |
1/2✓ Branch 1 taken 39525 times.
✗ Branch 2 not taken.
|
39525 | std::stoi(resultStr); |
855 | ✗ | } catch( const std::invalid_argument&) { | |
856 | ✗ | FEL_ERROR( "In file '" + m_filename | |
857 | + "', section '" + m_section | ||
858 | + "', parameter '" + name | ||
859 | + "' expecting integer, got '" + resultStr | ||
860 | + "'." | ||
861 | ) ; | ||
862 | } | ||
863 | 39525 | } | |
864 | |||
865 | 106020 | void GetPotCustomized::validateDoubleParam(const char* name) { | |
866 | 106020 | std::string resultStr; | |
867 | |||
868 |
1/2✓ Branch 1 taken 106020 times.
✗ Branch 2 not taken.
|
106020 | std::string paramFromFile = m_paramFromFile(name); |
869 |
1/2✓ Branch 1 taken 106020 times.
✗ Branch 2 not taken.
|
106020 | std::string paramFromCl = m_paramFromCl(name); |
870 | |||
871 |
3/6✓ Branch 2 taken 106020 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 106020 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 106020 times.
✗ Branch 9 not taken.
|
106020 | resultStr = (*m_getpotFile)( paramFromFile.c_str(), "1."); |
872 |
3/6✓ Branch 3 taken 106020 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 106020 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 106020 times.
✗ Branch 10 not taken.
|
106020 | resultStr = (*m_getpotCl) ( paramFromCl.c_str(), resultStr.c_str()); |
873 | try { | ||
874 |
1/2✓ Branch 1 taken 106020 times.
✗ Branch 2 not taken.
|
106020 | std::stod(resultStr); |
875 | ✗ | } catch( const std::invalid_argument&) { | |
876 | ✗ | FEL_ERROR( "In file '" + m_filename | |
877 | + "', section '" + m_section | ||
878 | + "', parameter '" + name | ||
879 | + "' expecting double, got '" + resultStr | ||
880 | + "'." | ||
881 | ) ; | ||
882 | } | ||
883 | 106020 | } | |
884 | |||
885 | |||
886 | /* ================================================================ */ | ||
887 | /* privates functions */ | ||
888 | /* ================================================================ */ | ||
889 | |||
890 | 464585 | std::string GetPotCustomized::m_paramFromFile(const char *name) { | |
891 |
4/8✓ Branch 2 taken 464585 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 464585 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 464585 times.
✗ Branch 10 not taken.
✓ Branch 12 taken 464585 times.
✗ Branch 13 not taken.
|
929170 | std::string pathInFile = m_section + (std::string) "/" + (std::string) name; |
892 | |||
893 | // register this section+variable as valid, for latter ufo detection | ||
894 |
1/2✓ Branch 1 taken 464585 times.
✗ Branch 2 not taken.
|
464585 | m_identifiedVariablesFile.push_back(pathInFile); |
895 | |||
896 | 464585 | return pathInFile; | |
897 | } | ||
898 | |||
899 | 464585 | std::string GetPotCustomized::m_paramFromCl(const char *name) { | |
900 | |||
901 | // the option name is: --felisce-section-variable | ||
902 | 464585 | std::string option_name = prefixOptionFelisce | |
903 |
1/2✓ Branch 1 taken 464585 times.
✗ Branch 2 not taken.
|
929170 | + m_section |
904 |
1/2✓ Branch 1 taken 464585 times.
✗ Branch 2 not taken.
|
929170 | + "-" |
905 |
2/4✓ Branch 2 taken 464585 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 464585 times.
✗ Branch 6 not taken.
|
1393755 | + std::string(name) |
906 | ; | ||
907 | |||
908 | // register this option as valid, for latter ufo detection | ||
909 |
1/2✓ Branch 1 taken 464585 times.
✗ Branch 2 not taken.
|
464585 | m_identifiedVariablesCl.push_back(option_name); |
910 | |||
911 | 464585 | return option_name; | |
912 | } | ||
913 | |||
914 | |||
915 | ✗ | void GetPotCustomized::addToIdentifiedVariables(const char *name) { | |
916 | ✗ | m_identifiedVariablesCl.emplace_back(name); | |
917 | ✗ | m_identifiedVariablesFile.emplace_back(name); | |
918 | } | ||
919 | |||
920 | } | ||
921 |