GCC Code Coverage Report


Directory: ../../../builds/dumux-repositories/
File: dumux/dumux/common/parameters.cc
Date: 2025-04-12 19:19:20
Exec Total Coverage
Lines: 99 114 86.8%
Functions: 13 15 86.7%
Branches: 141 330 42.7%

Line Branch Exec Source
1 // -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 // vi: set et ts=4 sw=4 sts=4:
3 //
4 // SPDX-FileCopyrightText: Copyright © DuMux Project contributors, see AUTHORS.md in root folder
5 // SPDX-License-Identifier: GPL-3.0-or-later
6 //
7 /*!
8 * \file
9 * \ingroup Core
10 * \brief The infrastructure to retrieve run-time parameters from Dune::ParameterTrees.
11 */
12
13 #include <config.h>
14
15 #include <iostream>
16 #include <list>
17 #include <sstream>
18 #include <unordered_map>
19 #include <fstream>
20 #include <functional>
21
22 #include <dune/common/parametertree.hh>
23 #include <dune/common/parametertreeparser.hh>
24 #include <dune/common/parallel/mpihelper.hh>
25
26 #include <dumux/common/exceptions.hh>
27 #include <dumux/common/parameters.hh>
28 #include <dumux/common/loggingparametertree.hh>
29
30 namespace Dumux {
31
32 // Initialize the parameter tree singletons
33 15 void Parameters::init(int argc, char **argv, const Usage& usage)
34 {
35
1/2
✓ Branch 2 taken 15 times.
✗ Branch 3 not taken.
30 init(argc, argv, [] (Dune::ParameterTree&) {}, "", usage);
36 15 }
37
38 // Initialize the parameter tree singletons
39 38 void Parameters::init(int argc, char **argv,
40 std::string parameterFileName,
41 const Usage& usage)
42 {
43
1/2
✓ Branch 2 taken 38 times.
✗ Branch 3 not taken.
76 init(argc, argv, [] (Dune::ParameterTree&) {}, parameterFileName, usage);
44 38 }
45
46 // Initialize the parameter tree singletons
47 void Parameters::init(int argc, char **argv,
48 const DefaultParams& defaultParams,
49 const Usage& usage)
50 {
51 init(argc, argv, defaultParams, "", usage);
52 }
53
54 // Initialize the parameter tree
55 677 void Parameters::init(int argc, char **argv,
56 const DefaultParams& defaultParams,
57 std::string parameterFileName,
58 const Usage& usage)
59 {
60 677 const auto& mpiHelper = Dune::MPIHelper::instance(argc, argv);
61
62 // check whether the user wanted to see the help message
63
2/2
✓ Branch 0 taken 2909 times.
✓ Branch 1 taken 677 times.
3586 for (int i = 1; i < argc; ++i)
64 {
65
5/12
✓ Branch 1 taken 2909 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2909 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 2909 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2909 times.
✗ Branch 9 not taken.
✗ Branch 11 not taken.
✓ Branch 12 taken 2909 times.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
5818 if (std::string("--help") == argv[i] || std::string("-h") == argv[i])
66 {
67 // return usage message and return;
68 if (mpiHelper.rank() == 0)
69 usage(argv[0], defaultUsageMessage(argv[0]));
70
71 exit(0);
72 }
73 }
74
75 // apply the default parameters
76
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 677 times.
677 defaultParams(defaultParamTree_());
77 677 applyGlobalDefaults_(defaultParamTree_());
78
79 // parse parameters from the command line
80 677 const auto commandLineArgs = parseCommandLine(argc, argv);
81
2/4
✓ Branch 1 taken 677 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 677 times.
✗ Branch 5 not taken.
677 mergeTree_(paramTree_(), commandLineArgs);
82
83 // overwrite parameter file if one was specified on the command line
84
2/4
✓ Branch 1 taken 677 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 677 times.
✗ Branch 5 not taken.
677 parameterFileName = commandLineArgs.get<std::string>("ParameterFile", parameterFileName);
85
86 // otherwise use the default name (executable name + .input)
87
2/2
✓ Branch 0 taken 79 times.
✓ Branch 1 taken 598 times.
677 if (parameterFileName.empty())
88 {
89 237 parameterFileName = [&](){
90 158 std::string defaultName = std::string(argv[0]) + ".input";
91
1/2
✓ Branch 1 taken 79 times.
✗ Branch 2 not taken.
79 std::ifstream pFile(defaultName.c_str());
92
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 75 times.
79 if (pFile.is_open())
93 4 return defaultName;
94
95
1/2
✓ Branch 1 taken 75 times.
✗ Branch 2 not taken.
75 defaultName = "params.input";
96
2/4
✓ Branch 1 taken 75 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 75 times.
✗ Branch 5 not taken.
75 pFile = std::ifstream(defaultName.c_str());
97
2/2
✓ Branch 0 taken 63 times.
✓ Branch 1 taken 12 times.
75 if (pFile.is_open())
98 63 return defaultName;
99 else
100
1/2
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
12 return std::string("");
101
1/2
✓ Branch 1 taken 79 times.
✗ Branch 2 not taken.
158 }();
102
103 // if no parameter file was given and also no default names where found, continue without
104
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 67 times.
79 if (parameterFileName.empty())
105 {
106
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 10 times.
12 if (mpiHelper.size() > 1)
107
3/6
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 2 times.
✗ Branch 8 not taken.
2 std::cout << "Rank " << mpiHelper.rank() << ": ";
108
1/2
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
12 std::cout << "No parameter file found. Continuing without parameter file.\n";
109
110 12 return;
111 }
112 else
113 {
114
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 57 times.
67 if (mpiHelper.size() > 1)
115
3/6
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 10 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 10 times.
✗ Branch 8 not taken.
10 std::cout << "Rank " << mpiHelper.rank() << ": ";
116
1/2
✓ Branch 1 taken 67 times.
✗ Branch 2 not taken.
67 std::cout << "No parameter file given. "
117 << "Defaulting to '"
118 << parameterFileName
119
3/6
✓ Branch 1 taken 67 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 67 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 67 times.
✗ Branch 8 not taken.
67 << "' for input file.\n";
120 }
121 }
122
123
2/2
✓ Branch 0 taken 74 times.
✓ Branch 1 taken 591 times.
665 if (mpiHelper.size() > 1)
124
3/6
✓ Branch 1 taken 74 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 74 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 74 times.
✗ Branch 8 not taken.
74 std::cout << "Rank " << mpiHelper.rank() << ": ";
125
3/6
✓ Branch 1 taken 665 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 665 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 665 times.
✗ Branch 8 not taken.
665 std::cout << "Reading parameters from file " << parameterFileName << ".\n";
126
127 // read parameters from the file without overwriting the command line params
128 // because the command line arguments have precedence
129 // let Dune do the error checking if the file exists
130
3/6
✓ Branch 1 taken 665 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 665 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 665 times.
✗ Branch 8 not taken.
1330 Dune::ParameterTreeParser::readINITree(parameterFileName,
131 paramTree_(),
132 /*overwrite=*/false);
133 677 }
134
135 // Initialize the parameter tree
136 29 void Parameters::init(const DefaultParams& params,
137 const DefaultParams& defaultParams)
138 {
139 // apply the parameters
140
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 29 times.
29 params(paramTree_());
141 // apply the default parameters
142
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 29 times.
29 defaultParams(defaultParamTree_());
143 29 applyGlobalDefaults_(defaultParamTree_());
144 29 }
145
146 // Initialize the parameter tree
147 void Parameters::init(const std::string& parameterFileName,
148 const DefaultParams& params,
149 bool inputFileOverwritesParams,
150 const DefaultParams& defaultParams)
151 {
152 // apply the parameters
153 params(paramTree_());
154
155 // read parameters from the input file
156 Dune::ParameterTreeParser::readINITree(parameterFileName, paramTree_(), inputFileOverwritesParams);
157
158 // apply the default parameters
159 defaultParams(defaultParamTree_());
160 applyGlobalDefaults_(defaultParamTree_());
161 }
162
163 // prints all used and unused parameters
164 455 void Parameters::print()
165 {
166 455 getTree().reportAll();
167 455 }
168
169 // Parse command line arguments into a parameter tree
170 677 Dune::ParameterTree Parameters::parseCommandLine(int argc, char **argv)
171 {
172 677 Dune::ParameterTree commandLineArgs;
173
2/2
✓ Branch 1 taken 1731 times.
✓ Branch 2 taken 677 times.
2408 for (int i = 1; i < argc; ++i)
174 {
175
3/4
✓ Branch 0 taken 553 times.
✓ Branch 1 taken 1178 times.
✓ Branch 2 taken 553 times.
✗ Branch 3 not taken.
1731 if (argv[i][0] != '-' && i == 1)
176 {
177 // try to pass first argument as parameter file
178
3/6
✓ Branch 1 taken 553 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 553 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 553 times.
✗ Branch 8 not taken.
553 commandLineArgs["ParameterFile"] = argv[1];
179 553 continue;
180 }
181
182
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1178 times.
1178 if (argv[i][0] != '-')
183 DUNE_THROW(ParameterException, "-> Command line argument " << i << " (='" << argv[i] << "') is invalid. <-");
184
185
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1178 times.
1178 if (i+1 == argc)
186 DUNE_THROW(ParameterException, "-> No argument given for parameter '" << argv[i] << "'! <-");
187
188 // check for the ParameterFile argument
189
3/4
✓ Branch 1 taken 1178 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 7 times.
✓ Branch 5 taken 1171 times.
1178 if (argv[i]+1 == std::string("ParameterFile")) // +1 removes the '-'
190 {
191
3/6
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 7 times.
✗ Branch 8 not taken.
7 commandLineArgs["ParameterFile"] = argv[i+1];
192 7 ++i;
193 }
194
195 // add all other options as key value pairs
196 else
197 {
198 // read a -MyOpt VALUE option
199
1/2
✓ Branch 1 taken 1171 times.
✗ Branch 2 not taken.
1171 std::string paramName = argv[i]+1; // +1 removes the '-'
200
1/2
✓ Branch 1 taken 1171 times.
✗ Branch 2 not taken.
1171 std::string paramValue = argv[i+1];
201 1171 ++i; // In the case of '-MyOpt VALUE' each pair counts as two arguments
202
203 // Put the key=value pair into the parameter tree
204
2/4
✓ Branch 1 taken 1171 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1171 times.
✗ Branch 5 not taken.
2342 commandLineArgs[paramName] = paramValue;
205 1171 }
206 }
207 677 return commandLineArgs;
208 }
209
210 // get the parameter tree singleton
211 486652 const LoggingParameterTree& Parameters::getTree()
212 {
213
6/10
✓ Branch 0 taken 692 times.
✓ Branch 1 taken 485960 times.
✓ Branch 3 taken 692 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 692 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 692 times.
✗ Branch 10 not taken.
✓ Branch 12 taken 692 times.
✗ Branch 13 not taken.
486652 static LoggingParameterTree tree(paramTree_(), defaultParamTree_());
214 486652 return tree;
215 }
216
217 // the actual internal parameter tree storing all user-specfied runtime parameters
218 2063 Dune::ParameterTree& Parameters::paramTree_()
219 {
220
4/6
✓ Branch 0 taken 694 times.
✓ Branch 1 taken 1369 times.
✓ Branch 3 taken 694 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 694 times.
✗ Branch 7 not taken.
2063 static Dune::ParameterTree tree;
221 2063 return tree;
222 }
223
224 // the parameter tree storing the Dumux global defaults for some parameters
225 2104 Dune::ParameterTree& Parameters::defaultParamTree_()
226 {
227
4/6
✓ Branch 0 taken 694 times.
✓ Branch 1 taken 1410 times.
✓ Branch 3 taken 694 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 694 times.
✗ Branch 7 not taken.
2104 static Dune::ParameterTree tree;
228 2104 return tree;
229 }
230
231 706 void Parameters::applyGlobalDefaults_(Dune::ParameterTree& params)
232 {
233 // global defaults
234 706 Dune::ParameterTree defaultParams;
235
236 // parameters in the implicit group
237
3/6
✓ Branch 1 taken 706 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 706 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 706 times.
✗ Branch 8 not taken.
706 defaultParams["Flux.UpwindWeight"] = "1.0";
238
3/6
✓ Branch 1 taken 706 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 706 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 706 times.
✗ Branch 8 not taken.
706 defaultParams["Implicit.EnableJacobianRecycling"] = "false";
239
240 // parameters in the assembly group
241
3/6
✓ Branch 1 taken 706 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 706 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 706 times.
✗ Branch 8 not taken.
706 defaultParams["Assembly.NumericDifferenceMethod"] = "1";
242
243 // parameters in the problem group
244
3/6
✓ Branch 1 taken 706 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 706 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 706 times.
✗ Branch 8 not taken.
706 defaultParams["Problem.EnableGravity"] = "true";
245
3/6
✓ Branch 1 taken 706 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 706 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 706 times.
✗ Branch 8 not taken.
706 defaultParams["Problem.EnableInertiaTerms"] = "true";
246
247 // parameters in the time loop group
248
3/6
✓ Branch 1 taken 706 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 706 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 706 times.
✗ Branch 8 not taken.
706 defaultParams["TimeLoop.MaxTimeStepSize"] = "1e300";
249
3/6
✓ Branch 1 taken 706 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 706 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 706 times.
✗ Branch 8 not taken.
706 defaultParams["TimeLoop.MaxTimeStepDivisions"] = "10";
250
251 // parameters in the vtk group
252
3/6
✓ Branch 1 taken 706 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 706 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 706 times.
✗ Branch 8 not taken.
706 defaultParams["Vtk.AddVelocity"] = "false";
253
3/6
✓ Branch 1 taken 706 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 706 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 706 times.
✗ Branch 8 not taken.
706 defaultParams["Vtk.AddProcessRank"] = "true";
254
255 // parameters in the mpfa group
256
3/6
✓ Branch 1 taken 706 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 706 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 706 times.
✗ Branch 8 not taken.
706 defaultParams["MPFA.Q"] = "0.0";
257
258 // merge the global default tree but do not overwrite if the parameter already exists
259
1/2
✓ Branch 1 taken 706 times.
✗ Branch 2 not taken.
706 mergeTree_(params, defaultParams, false);
260 706 }
261
262 // merge source into target tree
263 1383 void Parameters::mergeTree_(Dune::ParameterTree& target, const Dune::ParameterTree& source, bool overwrite)
264
1/2
✓ Branch 2 taken 1383 times.
✗ Branch 3 not taken.
2766 { mergeTreeImpl_(target, source, overwrite, ""); }
265
266 // recursively merge all elements
267 7369 void Parameters::mergeTreeImpl_(Dune::ParameterTree& target, const Dune::ParameterTree& source, bool overwrite, const std::string& group)
268 {
269
2/2
✓ Branch 0 taken 1383 times.
✓ Branch 1 taken 5986 times.
7369 const auto prefix = group.empty() ? "" : group + ".";
270
3/4
✓ Branch 1 taken 7369 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 8773 times.
✓ Branch 4 taken 7369 times.
16142 for (const auto& key : source.getValueKeys())
271
4/6
✓ Branch 0 taken 7060 times.
✓ Branch 1 taken 1713 times.
✓ Branch 3 taken 7060 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 7060 times.
✗ Branch 6 not taken.
8773 if (overwrite || !target.hasKey(key))
272
4/8
✓ Branch 1 taken 8773 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8773 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 8773 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 8773 times.
✗ Branch 11 not taken.
17546 target[prefix + key] = source[key];
273
274
3/4
✓ Branch 1 taken 7369 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 5986 times.
✓ Branch 4 taken 7369 times.
13355 for (const auto& subKey : source.getSubKeys())
275
3/6
✓ Branch 1 taken 5986 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 5986 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 5986 times.
✗ Branch 8 not taken.
11972 mergeTreeImpl_(target, source.sub(subKey), overwrite, prefix + subKey);
276 7369 }
277
278 } // end namespace Dumux
279