Skip to content

Commit 21be533

Browse files
authored
Merge pull request #3222 from boutproject/field-variable-expression
Add ability to use variables from grid file in input file expressions
2 parents 6828e9a + 74310f6 commit 21be533

27 files changed

Lines changed: 520 additions & 257 deletions

include/bout/field2d.hxx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class Field2D;
4242
#include "bout/region.hxx"
4343

4444
#include <cstddef>
45+
#include <iostream>
4546
#include <ostream>
4647
#include <string>
4748

include/bout/invertable_operator.hxx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,9 @@ public:
135135
: operatorFunction(func), preconditionerFunction(func),
136136
opt(optIn == nullptr ? Options::getRoot()->getSection("invertableOperator")
137137
: optIn),
138-
localmesh(localmeshIn == nullptr ? bout::globals::mesh : localmeshIn), lib(opt){
138+
localmesh(localmeshIn == nullptr ? bout::globals::mesh : localmeshIn), lib(opt) {
139139

140-
};
140+
};
141141

142142
/// Destructor just has to cleanup the PETSc owned objects.
143143
~InvertableOperator() {

include/bout/mesh.hxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ public:
605605
virtual int getLocalZIndexNoBoundaries(int zglobal) const = 0;
606606

607607
/// Size of the mesh on this processor including guard/boundary cells
608-
int LocalNx, LocalNy, LocalNz;
608+
int LocalNx{0}, LocalNy{0}, LocalNz{0};
609609

610610
/// Local ranges of data (inclusive), excluding guard cells
611611
int xstart, xend, ystart, yend, zstart, zend;

include/bout/output.hxx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,13 +166,13 @@ class ConditionalOutput : public Output {
166166
public:
167167
/// @param[in] base The Output object which will be written to if enabled
168168
/// @param[in] enabled Should this be enabled by default?
169-
ConditionalOutput(Output* base, bool enabled = true) : base(base), enabled(enabled){};
169+
ConditionalOutput(Output* base, bool enabled = true) : base(base), enabled(enabled) {};
170170

171171
/// Constuctor taking ConditionalOutput. This allows several layers of conditions
172172
///
173173
/// @param[in] base A ConditionalOutput which will be written to if enabled
174174
///
175-
ConditionalOutput(ConditionalOutput* base) : base(base), enabled(base->enabled){};
175+
ConditionalOutput(ConditionalOutput* base) : base(base), enabled(base->enabled) {};
176176

177177
/// If enabled, writes a string using fmt formatting
178178
/// by calling base->write
@@ -237,7 +237,7 @@ private:
237237
/// output_debug << "debug message";
238238
/// compile but have no effect if BOUT_USE_OUTPUT_DEBUG is false
239239
template <typename T>
240-
DummyOutput& operator<<(DummyOutput& out, T const& UNUSED(t)) {
240+
DummyOutput& operator<<(DummyOutput& out, const T& UNUSED(t)) {
241241
return out;
242242
}
243243

@@ -261,7 +261,7 @@ inline ConditionalOutput& operator<<(ConditionalOutput& out, stream_manipulator
261261
}
262262

263263
template <typename T>
264-
ConditionalOutput& operator<<(ConditionalOutput& out, T const& t) {
264+
ConditionalOutput& operator<<(ConditionalOutput& out, const T& t) {
265265
if (out.isEnabled()) {
266266
*out.getBase() << t;
267267
}

include/bout/physicsmodel.hxx

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
/*!************************************************************************
22
* \file physicsmodel.hxx
3-
*
3+
*
44
* @brief Base class for Physics Models
5-
*
6-
*
5+
*
6+
*
77
*
88
* Changelog:
9-
*
9+
*
1010
* 2013-08 Ben Dudson <benjamin.dudson@york.ac.uk>
1111
* * Initial version
12-
*
12+
*
1313
**************************************************************************
1414
* Copyright 2013 B.D.Dudson
1515
*
1616
* Contact: Ben Dudson, bd512@york.ac.uk
17-
*
17+
*
1818
* This file is part of BOUT++.
1919
*
2020
* BOUT++ is free software: you can redistribute it and/or modify
@@ -167,9 +167,9 @@ public:
167167
*
168168
* Output
169169
* ------
170-
*
170+
*
171171
* The time derivatives will be put in the ddt() variables
172-
*
172+
*
173173
* Returns a flag: 0 indicates success, non-zero an error flag
174174
*/
175175
int runRHS(BoutReal time, bool linear = false);
@@ -203,7 +203,7 @@ public:
203203
bool hasPrecon();
204204

205205
/*!
206-
* Run the preconditioner. The system state should be in the
206+
* Run the preconditioner. The system state should be in the
207207
* evolving variables, and the vector to be solved in the ddt() variables.
208208
* The result will be put in the ddt() variables.
209209
*
@@ -219,7 +219,7 @@ public:
219219

220220
/*!
221221
* Run the Jacobian-vector multiplication function
222-
*
222+
*
223223
* Note: this is usually only called by the Solver
224224
*/
225225
int runJacobian(BoutReal t);
@@ -241,10 +241,10 @@ protected:
241241
// The init and rhs functions are implemented by user code to specify problem
242242
/*!
243243
* @brief This function is called once by the solver at the start of a simulation.
244-
*
244+
*
245245
* A valid PhysicsModel must implement this function
246-
*
247-
* Variables should be read from the inputs, and the variables to
246+
*
247+
* Variables should be read from the inputs, and the variables to
248248
* be evolved should be specified.
249249
*/
250250
virtual int init(bool restarting) = 0;
@@ -258,7 +258,7 @@ protected:
258258
/*!
259259
* @brief This function is called by the time integration solver
260260
* at least once per time step
261-
*
261+
*
262262
* Variables being evolved will be set by the solver
263263
* before the call, and this function must calculate
264264
* and set the time-derivatives.
@@ -278,10 +278,10 @@ protected:
278278
/// Add additional variables other than the evolving variables to the restart files
279279
virtual void restartVars(Options& options);
280280

281-
/*
281+
/*
282282
If split operator is set to true, then
283283
convective() and diffusive() are called instead of rhs()
284-
284+
285285
For implicit-explicit schemes, convective() will typically
286286
be treated explicitly, whilst diffusive() will be treated implicitly.
287287
For unsplit methods, both convective and diffusive will be called
@@ -334,7 +334,7 @@ protected:
334334
*
335335
* @param[in] var The variable to evolve
336336
* @param[in] name The name to use for variable initialisation and output
337-
*
337+
*
338338
* Note that the variable must not be destroyed (e.g. go out of scope)
339339
* after this call, since a pointer to \p var is stored in the solver.
340340
*
@@ -358,11 +358,11 @@ protected:
358358
* Specify a constrained variable \p var, which will be
359359
* adjusted to make \p F_var equal to zero.
360360
* If the solver does not support constraints then this will throw an exception
361-
*
361+
*
362362
* @param[in] var The variable the solver should modify
363363
* @param[in] F_var The control variable, which the user will set
364364
* @param[in] name The name to use for initialisation and output
365-
*
365+
*
366366
*/
367367
bool bout_constrain(Field3D& var, Field3D& F_var, const char* name);
368368

@@ -491,8 +491,7 @@ private:
491491

492492
/// Add fields to the solver.
493493
/// This should accept up to ten arguments
494-
#define SOLVE_FOR(...) \
495-
{ MACRO_FOR_EACH(SOLVE_FOR1, __VA_ARGS__) }
494+
#define SOLVE_FOR(...) {MACRO_FOR_EACH(SOLVE_FOR1, __VA_ARGS__)}
496495

497496
/// Write this variable once to the grid file
498497
#define SAVE_ONCE1(var) dump.addOnce(var, #var);
@@ -532,8 +531,7 @@ private:
532531
dump.addOnce(var6, #var6); \
533532
}
534533

535-
#define SAVE_ONCE(...) \
536-
{ MACRO_FOR_EACH(SAVE_ONCE1, __VA_ARGS__) }
534+
#define SAVE_ONCE(...) {MACRO_FOR_EACH(SAVE_ONCE1, __VA_ARGS__)}
537535

538536
/// Write this variable every timestep
539537
#define SAVE_REPEAT1(var) dump.addRepeat(var, #var);
@@ -573,7 +571,6 @@ private:
573571
dump.addRepeat(var6, #var6); \
574572
}
575573

576-
#define SAVE_REPEAT(...) \
577-
{ MACRO_FOR_EACH(SAVE_REPEAT1, __VA_ARGS__) }
574+
#define SAVE_REPEAT(...) {MACRO_FOR_EACH(SAVE_REPEAT1, __VA_ARGS__)}
578575

579576
#endif // BOUT_PHYSICS_MODEL_H

include/bout/sys/generator_context.hxx

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,27 @@ public:
2424
: Context(i.x(), i.y(), 0, (loc == CELL_ZLOW) ? CELL_CENTRE : loc, msh, t) {}
2525

2626
/// Specify a cell index, together with the cell location, mesh and time
27-
///
2827
Context(int ix, int iy, int iz, CELL_LOC loc, Mesh* msh, BoutReal t);
2928

30-
/// Specify the values directly
31-
Context(BoutReal x, BoutReal y, BoutReal z, Mesh* msh, BoutReal t);
32-
3329
/// If constructed without parameters, contains no values (null).
3430
/// Requesting x,y,z or t throws an exception
3531
Context() = default;
3632

3733
/// The location on the boundary
3834
Context(const BoundaryRegion* bndry, int iz, CELL_LOC loc, BoutReal t, Mesh* msh);
3935
Context(const BoundaryRegion* bndry, CELL_LOC loc, BoutReal t, Mesh* msh)
40-
: Context(bndry, 0, loc, t, msh){};
36+
: Context(bndry, 0, loc, t, msh) {};
4137

4238
BoutReal x() const { return get("x"); }
4339
BoutReal y() const { return get("y"); }
4440
BoutReal z() const { return get("z"); }
4541
BoutReal t() const { return get("t"); }
4642

43+
/// Cell indices
44+
int ix() const { return ix_; }
45+
int jy() const { return jy_; }
46+
int kz() const { return kz_; }
47+
4748
/// Set the value of a parameter with given name
4849
Context& set(const std::string& name, BoutReal value) {
4950
parameters[name] = value;
@@ -76,6 +77,10 @@ public:
7677
}
7778

7879
private:
80+
int ix_{0};
81+
int jy_{0};
82+
int kz_{0};
83+
7984
Mesh* localmesh{nullptr}; ///< The mesh on which the position is defined
8085

8186
/// Contains user-set values which can be set and retrieved

0 commit comments

Comments
 (0)