Skip to content

Commit eede30c

Browse files
committed
everybody now use Get<T>
1 parent dc19398 commit eede30c

File tree

35 files changed

+161
-193
lines changed

35 files changed

+161
-193
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77
## Upcoming
88
### Changed
99
- slight optimisation of the cfl estimation for parabolic terms (makes a difference when several explicit parabolic terms are used simultaneously)
10+
- use input::Get<T> and input::GetOrSet<T> instead of the old input:GetInt, input:GetReal... the new functions have a better error handling, and also allows explicit default values.
11+
- ensure that error messages are sent to std::cerr using a dedicated stream
1012

1113
### Added
1214
- isotropic thermal diffusion (anisotropic diffusion in MHD will come later)

doc/source/programmingguide.rst

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -207,14 +207,16 @@ It provides accessors such as
207207
// Accessor to input parameters
208208
// the arguments are always: BlockName, EntryName, ParameterNumber (starting from 0)
209209

210-
// Read a string
211-
std::string GetString(std::string, std::string, int);
210+
// Read a parameter of type T from the input file and throw an error if it cannot be found
211+
// T can be a std::string or a number (real, int, double, float, int64_t, ...)
212+
template<typename T>
213+
T Get(std::string blockName, std::string paramName, int num);
212214

213-
// Read a real number
214-
real GetReal(std::string, std::string, int);
215+
// Read a parameter of type T from the input file. Set it to default if it cannot be found.
216+
// T can be a std::string or a number (real, int, double, float, int64_t, ...)
217+
template<typename T>
218+
T GetOrSet(std::string blockName, std::string paramName, int num, T default);
215219

216-
// Read an integer
217-
int GetInt(std::string, std::string, int);
218220

219221
// Check that a block/entry is present
220222
int CheckEntry(std::string, std::string);
@@ -236,11 +238,14 @@ instance of ``Input`` is allocated in ``myInput``:
236238

237239
.. code-block:: c++
238240

239-
real firstParameter = myInput.GetReal("MyBlock","myentry",0) // firstParameter=1.0
240-
real secondParameter = myInput.GetReal("MyBlock","myentry",1) // secondParameter=0.0
241+
real firstParameter = myInput.Get<real>("MyBlock","myentry",0) // firstParameter=1.0
242+
real secondParameter = myInput.Get<real>("MyBlock","myentry",1) // secondParameter=0.0
243+
real thirdParameter = myInput.GetOrSet<real>("MyBlock","myentry",2, 0.0) // thirdParameter default to 0.0
241244

242-
If a parameter is not found, *Idefix* will log an error and exit. One can use the ``CheckEntry``
243-
method to check if a parameter is set in the ini file before trying to access it.
245+
246+
If a parameter is not found, ``Get<T>`` will log an error and exit. One can use the ``CheckEntry``
247+
method to check if a parameter is set in the ini file before trying to access it, or use ``GetOrSet<T>``
248+
with a default value, as in the example above.
244249

245250
.. tip::
246251
Command line options are also parsed by the ``Input`` class. These options are stored in a

doc/source/reference/idefix.ini.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ allows for comments, which should start with ``#``.
99

1010
.. tip::
1111
Note that you can add arbitray sections and entries in the input file freely. *Idefix* will automatically read and store them on startup. They are then accessible in the code using the
12-
``Input::GetReal(..)``, ``Input::GetInt(...)`` and ``Input::GetString(..)`` methods defined in the ``Input`` class (see :ref:`inputClass`)
12+
``Input::Get<T>(..)`` and ``Input::GetOrSet<T>`` methods defined in the ``Input`` class (see :ref:`inputClass`).
1313

1414
``Grid`` section
1515
--------------------
@@ -116,7 +116,7 @@ This section is used by the hydrodynamics class of *Idefix*. It defines the hydr
116116
| | | | to be enrolled with ``EnrollIsoSoundSpeed(IsoSoundSpeedFunc)`` |
117117
| | | | (see :ref:`functionEnrollment`). In this case, the second parameter is not used. |
118118
+----------------+-------------------------+---------------------------------------------------------------------------------------------+
119-
| gamma | float | Adiabatic index when ISOTHERMAL is not defined |
119+
| gamma | float | Adiabatic index when ISOTHERMAL is not defined. Default to 5/3 if not set. |
120120
+----------------+-------------------------+---------------------------------------------------------------------------------------------+
121121
| resistivity | string, string, (float) | | Switches on Ohmic diffusion. |
122122
| | | | The first parameter can be ``explicit`` or ``rkl``. When ``explicit``, diffusion is |

doc/source/reference/setup.cpp.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ constructor which reads a parameter from the .ini file and enroll the user-defin
111111
// Setup constructor
112112
Setup::Setup(Input &input, Grid &grid, DataBlock &data, Output &output) {
113113
// Read some parameter from the ini file
114-
Mass = input.GetReal("Setup","mass",0);
114+
Mass = input.Get<real>("Setup","mass",0);
115115

116116
// Enroll the user-defined potential
117117
data.hydro.EnrollGravPotential(&Potential);

src/dataBlock/fargo.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ void Fargo::Init(Input &input, DataBlock *data) {
140140
}
141141

142142
if(input.CheckBlock("Fargo")) {
143-
std::string opType = input.GetString("Fargo","velocity",0);
143+
std::string opType = input.Get<std::string>("Fargo","velocity",0);
144144
if(opType.compare("userdef")==0) {
145145
this->type=userdef;
146146
} else if(opType.compare("shearingbox")==0) {
@@ -152,13 +152,11 @@ void Fargo::Init(Input &input, DataBlock *data) {
152152
IDEFIX_ERROR("Unknown fargo velocity in the input file. "
153153
"Only userdef and shearingbox are allowed");
154154
}
155-
if(input.CheckEntry("Fargo","maxShift")>=0) {
156-
this->maxShift = input.GetInt("Fargo", "maxShift",0);
157-
}
155+
this->maxShift = input.GetOrSet<int>("Fargo", "maxShift",0, 10);
158156
} else {
159157
// DEPRECATED: initialisation from the [Hydro] block
160158
if(input.CheckEntry("Hydro","fargo")>=0) {
161-
std::string opType = input.GetString("Hydro","fargo",0);
159+
std::string opType = input.Get<std::string>("Hydro","fargo",0);
162160
if(opType.compare("userdef")==0) {
163161
this->type=userdef;
164162
} else if(opType.compare("shearingbox")==0) {

src/dataBlock/fargo.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class Fargo {
4444
int beg[3];
4545
int end[3];
4646
int nghost[3];
47-
int maxShift{10}; //< maximum number of cells along which we plan to shift.
47+
int maxShift; //< maximum number of cells along which we plan to shift.
4848
real dtMax{0}; //< Maximum allowable dt for a given Fargo velocity
4949
//< when domain decomposition is enabled
5050
bool velocityHasBeenComputed{false};

src/gravity/gravity.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,13 @@ void Gravity::Init(Input &input, DataBlock *datain) {
1919
if(nPotential >=0) {
2020
this->havePotential = true;
2121
for(int i = 0 ; i < nPotential ; i++) {
22-
std::string potentialString = input.GetString("Gravity","potential",i);
22+
std::string potentialString = input.Get<std::string>("Gravity","potential",i);
2323
if(potentialString.compare("userdef") == 0) {
2424
this->haveUserDefPotential = true;
2525
idfx::cout << "Gravity: Enabling user-defined gravitational potential" << std::endl;
2626
} else if (potentialString.compare("central") == 0) {
2727
this->haveCentralMassPotential = true;
28-
if(input.CheckEntry("Gravity","Mcentral") >= 0) {
29-
this->centralMass = input.GetReal("Gravity","Mcentral",0);
30-
}
28+
this->centralMass = input.GetOrSet<real>("Gravity","Mcentral",0, 1.0);
3129
idfx::cout << "Gravity: Enabling central mass gravitational potential with M="
3230
<< this->centralMass << std::endl;
3331
} else if (potentialString.compare("selfgravity") == 0) {
@@ -41,7 +39,7 @@ void Gravity::Init(Input &input, DataBlock *datain) {
4139

4240
// Body Force
4341
if(input.CheckEntry("Gravity","bodyForce")>=0) {
44-
std::string potentialString = input.GetString("Gravity","bodyForce",0);
42+
std::string potentialString = input.Get<std::string>("Gravity","bodyForce",0);
4543
if(potentialString.compare("userdef") == 0) {
4644
this->haveBodyForce = true;
4745
idfx::cout << "Gravity:: Enabling user-defined body force" << std::endl;

src/gravity/gravity.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class Gravity {
5353
private:
5454
bool haveInitialisedPotential{false}; ///< whether a potential has already been initialised
5555
bool haveInitialisedBodyForce{false}; ///< whether a body force has already been initialised
56-
real centralMass{1.0}; ///< central mass parameter when central mass potential
56+
real centralMass; ///< central mass parameter when central mass potential
5757
///< is enabled
5858
DataBlock *data;
5959

src/grid.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Grid::Grid(Input &input) {
2626
npoints[dir] = 1;
2727
nghost[dir] = 0;
2828
std::string label = std::string("X")+std::to_string(dir+1)+std::string("-grid");
29-
int numPatch = input.GetInt("Grid",label,0);
29+
int numPatch = input.Get<int>("Grid",label,0);
3030

3131
if(dir<DIMENSIONS) {
3232
#if ORDER < 4
@@ -36,7 +36,7 @@ Grid::Grid(Input &input) {
3636
#endif
3737
npoints[dir] = 0;
3838
for(int patch = 0; patch < numPatch ; patch++) {
39-
npoints[dir] += input.GetInt("Grid",label,2+3*patch );
39+
npoints[dir] += input.Get<int>("Grid",label,2+3*patch );
4040
}
4141
}
4242
}
@@ -47,7 +47,7 @@ Grid::Grid(Input &input) {
4747
np_int[dir] = npoints[dir];
4848

4949
std::string label = std::string("X")+std::to_string(dir+1)+std::string("-beg");
50-
std::string boundary = input.GetString("Boundary",label,0);
50+
std::string boundary = input.Get<std::string>("Boundary",label,0);
5151

5252
if(boundary.compare("outflow") == 0) {
5353
lbound[dir] = outflow;
@@ -74,7 +74,7 @@ Grid::Grid(Input &input) {
7474
}
7575

7676
label = std::string("X")+std::to_string(dir+1)+std::string("-end");
77-
boundary = input.GetString("Boundary",label,0);
77+
boundary = input.Get<std::string>("Boundary",label,0);
7878
if(boundary.compare("outflow") == 0) {
7979
rbound[dir] = outflow;
8080
} else if(boundary.compare("periodic") == 0) {
@@ -143,7 +143,7 @@ Grid::Grid(Input &input) {
143143
// Manual domain decomposition (with -dec option)
144144
int ntot=1;
145145
for(int dir=0 ; dir < DIMENSIONS; dir++) {
146-
nproc[dir] = input.GetInt("CommandLine","dec",dir);
146+
nproc[dir] = input.Get<int>("CommandLine","dec",dir);
147147
// Check that the dimension is effectively divisible by number of procs
148148
if(np_int[dir] % nproc[dir])
149149
IDEFIX_ERROR("Grid size must be a multiple of the domain decomposition");

src/gridHost.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ void GridHost::MakeGrid(Input &input) {
5555
idfx::cout << "GridHost::MakeGrid: " << std::endl;
5656
for(int dir = 0 ; dir < 3 ; dir++) {
5757
std::string label = std::string("X")+std::to_string(dir+1)+std::string("-grid");
58-
int numPatch = input.GetInt("Grid",label,0);
58+
int numPatch = input.Get<int>("Grid",label,0);
5959

60-
xstart[dir] = input.GetReal("Grid",label,1);
61-
xend[dir] = input.GetReal("Grid",label,4+(numPatch-1)*3);
60+
xstart[dir] = input.Get<real>("Grid",label,1);
61+
xend[dir] = input.Get<real>("Grid",label,4+(numPatch-1)*3);
6262

6363
this->xbeg[dir] = xstart[dir];
6464
this->xend[dir] = xend[dir];
@@ -68,10 +68,10 @@ void GridHost::MakeGrid(Input &input) {
6868
// Loop on all the patches
6969
int idxstart = nghost[dir];
7070
for(int patch = 0 ; patch < numPatch ; patch++) {
71-
std::string patchType = input.GetString("Grid",label,3+patch*3);
72-
real patchStart = input.GetReal("Grid",label,1+patch*3);
73-
real patchEnd = input.GetReal("Grid",label,4+patch*3);
74-
int patchSize = input.GetInt("Grid",label,2+patch*3);
71+
std::string patchType = input.Get<std::string>("Grid",label,3+patch*3);
72+
real patchStart = input.Get<real>("Grid",label,1+patch*3);
73+
real patchEnd = input.Get<real>("Grid",label,4+patch*3);
74+
int patchSize = input.Get<int>("Grid",label,2+patch*3);
7575

7676
// If this is the first or last patch, also define ghost cells
7777
int ghostStart = 0;
@@ -119,14 +119,14 @@ void GridHost::MakeGrid(Input &input) {
119119
"from a non-existent patch");
120120
}
121121
// Check that the reference patch is a uniform one
122-
if(input.GetString("Grid",label,3+3*refPatch).compare("u")) {
122+
if(input.Get<std::string>("Grid",label,3+3*refPatch).compare("u")) {
123123
IDEFIX_ERROR("You're attempting to construct a stretched patch "
124124
"from a non-uniform grid");
125125
}
126126
// Ok, we have a well-behaved reference patch, compute dx from the reference patch
127-
real refPatchStart = input.GetReal("Grid",label,1+refPatch*3);
128-
real refPatchEnd = input.GetReal("Grid",label,4+refPatch*3);
129-
int refPatchSize = input.GetInt("Grid",label,2+refPatch*3);
127+
real refPatchStart = input.Get<real>("Grid",label,1+refPatch*3);
128+
real refPatchEnd = input.Get<real>("Grid",label,4+refPatch*3);
129+
int refPatchSize = input.Get<int>("Grid",label,2+refPatch*3);
130130
double delta = (refPatchEnd-refPatchStart)/refPatchSize;
131131
double logdelta = log((patchEnd-patchStart)/delta);
132132

0 commit comments

Comments
 (0)