Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use namespace for global variables 'mesh' and 'dump' #1470

Merged
merged 43 commits into from
Feb 5, 2019
Merged
Show file tree
Hide file tree
Changes from 39 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
c9a8bc0
Move global variables into namespace bout::globals
johnomotani Dec 18, 2018
ffec7b6
Add namespaces to extern Mesh* mesh
johnomotani Dec 18, 2018
759a623
Make Solver::outputVars virtual, overload in Power
johnomotani Dec 18, 2018
de60d01
Remove use of global mesh in Vector3D::operator*
johnomotani Dec 18, 2018
eda81e7
Use local Mesh* in Datafile and DataFormat
johnomotani Dec 18, 2018
9d05657
Remove uses of global mesh pointer in BoutMesh
johnomotani Dec 18, 2018
41f8795
Remove uses of global mesh pointer in FCITransform/FCIMap
johnomotani Dec 18, 2018
38191f6
Replace some more uses of global mesh
johnomotani Dec 18, 2018
b494e99
Replace uses of global mesh pointer in boundary_standard.cxx
johnomotani Dec 18, 2018
b150d10
Remove uses of global mesh pointer in FV operations
johnomotani Dec 19, 2018
057ddf7
Remove uses of global mesh pointer in ParallelBoundaryOp
johnomotani Dec 19, 2018
17057e7
Remove reference to global mesh in smoothing.cxx
johnomotani Dec 19, 2018
7f5255f
Remove uses of global mesh in sourcex.cxx
johnomotani Dec 19, 2018
fb22382
Replace uses of global mesh in gyro_average.cxx
johnomotani Dec 19, 2018
ad932aa
Initialize result with the mesh of the input field
johnomotani Dec 19, 2018
1d6570d
Get mesh from already-used argument, restore UNUSED where possible
johnomotani Dec 19, 2018
6ce32e6
Use bout::globals in bout++.cxx
johnomotani Dec 18, 2018
a812c7c
Replace mesh with bout::globals::mesh
johnomotani Dec 18, 2018
4427ce4
More replacements mesh->bout::globals::mesh in default arguments
johnomotani Dec 18, 2018
e6ea110
Add namespace to global mesh in PhysicsModel::postInit()
johnomotani Dec 19, 2018
6deed69
Reduce uses of global mesh in solvers, add namespace to remaining
johnomotani Dec 19, 2018
7fff20f
Restore exactly original behaviour of mudq/mldq defaults
johnomotani Dec 19, 2018
aa1879e
Use globals namespace in boutcore
johnomotani Dec 19, 2018
95146f2
Use bout::globals namespace in InvertableOperator
johnomotani Dec 19, 2018
566614f
Use bout::globals namespace in unit tests
johnomotani Dec 19, 2018
e2d18ed
Add bout::globals:: in field_data.cxx
johnomotani Dec 20, 2018
721c1a8
Replace mesh->bout::globals::mesh in laplacexy.cxx
johnomotani Jan 2, 2019
653057f
Change dump->bout::globals::dump in boutcore
johnomotani Jan 2, 2019
8d762d7
Move 'using namespace bout::globals' to bout.hxx
johnomotani Dec 19, 2018
de8552f
Forward-declare Mesh in datafile.hxx
johnomotani Dec 19, 2018
0e204f0
Remove unused localmesh variables in Field2D and Field3D
johnomotani Dec 19, 2018
146f60b
Convert test-petsc_laplace to have mesh in input file
johnomotani Dec 19, 2018
e9356ab
Don't include mesh.hxx in globals.hxx
johnomotani Dec 19, 2018
71d0a9e
Use nullptr instead of bout::globals::mesh as default argument
johnomotani Dec 19, 2018
32b172c
nullptr default Mesh* argument to Laplacian implementations constructors
johnomotani Dec 19, 2018
caabb9c
Fix some errors, warnings
bendudson Dec 23, 2018
d0401ee
Use forward declares in datafile.hxx
johnomotani Jan 2, 2019
8886ff8
Use bout::globals::dump in SlepcSolver
johnomotani Jan 3, 2019
08cc9f2
Use bout::globals::dump in BOUTMAIN macro
johnomotani Jan 3, 2019
9d74be0
Merge branch 'next' into namespace_globals
johnomotani Feb 4, 2019
606c201
Pass mesh_in in initializer list of FCIMap::FCIMap constructor
johnomotani Feb 4, 2019
fc2db92
Add 'using namespace bout::globals;' for new unit test files.
johnomotani Feb 4, 2019
64cbef3
Use nullptr as default Mesh* argument for Laplacian::create
johnomotani Feb 5, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 7 additions & 3 deletions include/boundary_region.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ class BoundaryRegion;
#include <utility>

class Mesh;
extern Mesh* mesh;
namespace bout {
namespace globals {
extern Mesh* mesh; ///< Global mesh
} // namespace bout
} // namespace globals

/// Location of boundary
enum BndryLoc {BNDRY_XIN=1,
Expand All @@ -24,9 +28,9 @@ public:

BoundaryRegionBase() = delete;
BoundaryRegionBase(std::string name, Mesh *passmesh = nullptr)
: localmesh(passmesh ? passmesh : mesh), label(std::move(name)) {}
: localmesh(passmesh ? passmesh : bout::globals::mesh), label(std::move(name)) {}
BoundaryRegionBase(std::string name, BndryLoc loc, Mesh *passmesh = nullptr)
: localmesh(passmesh ? passmesh : mesh), label(std::move(name)), location(loc) {}
: localmesh(passmesh ? passmesh : bout::globals::mesh), label(std::move(name)), location(loc) {}

virtual ~BoundaryRegionBase() {}

Expand Down
8 changes: 8 additions & 0 deletions include/bout.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

#include "boutcomm.hxx"

#include <bout/mesh.hxx>
#include "globals.hxx"

#include "field2d.hxx"
Expand Down Expand Up @@ -65,6 +66,13 @@

const BoutReal BOUT_VERSION = BOUT_VERSION_DOUBLE; ///< Version number

#ifndef BOUT_NO_USING_NAMESPACE_BOUTGLOBALS
// Include using statement by default in user code.
// Macro allows us to include bout.hxx or physicsmodel.hxx without the using
// statement in library code.
using namespace bout::globals;
#endif // BOUT_NO_USING_NAMESPACE_BOUTGLOBALS

// BOUT++ main functions

/*!
Expand Down
8 changes: 8 additions & 0 deletions include/bout/fv_ops.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "../vector2d.hxx"

#include "../utils.hxx"
#include <bout/mesh.hxx>

namespace FV {
/*!
Expand Down Expand Up @@ -182,6 +183,10 @@ namespace FV {

ASSERT2(f_in.getLocation() == v_in.getLocation());

Mesh* mesh = f_in.getMesh();
ASSERT1(mesh == v_in.getMesh());
ASSERT1(mesh == wave_speed.getMesh());

CellEdges cellboundary;

Field3D f = mesh->toFieldAligned(f_in);
Expand Down Expand Up @@ -341,6 +346,9 @@ namespace FV {
const Field3D Div_f_v(const Field3D &n_in, const Vector3D &v, bool bndry_flux) {
ASSERT2(n_in.getLocation() == v.getLocation());

Mesh* mesh = n_in.getMesh();
ASSERT1(mesh == v.x.getMesh());

CellEdges cellboundary;

Coordinates *coord = n_in.getCoordinates();
Expand Down
4 changes: 2 additions & 2 deletions include/bout/invert/laplacexz.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@
class LaplaceXZ {
public:
LaplaceXZ(Mesh* m = nullptr, Options* UNUSED(options) = nullptr,
const CELL_LOC loc = CELL_CENTRE)
: localmesh(m==nullptr ? mesh : m), location(loc) {}
const CELL_LOC loc = CELL_CENTRE)
: localmesh(m == nullptr ? bout::globals::mesh : m), location(loc) {}
virtual ~LaplaceXZ() {}

virtual void setCoefs(const Field2D &A, const Field2D &B) = 0;
Expand Down
3 changes: 2 additions & 1 deletion include/bout/invertable_operator.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class InvertableOperator;

#ifdef BOUT_HAS_PETSC

#include <bout/mesh.hxx>
#include <bout/sys/timer.hxx>
#include <boutcomm.hxx>
#include <boutexception.hxx>
Expand Down Expand Up @@ -133,7 +134,7 @@ public:
: operatorFunction(func), preconditionerFunction(func),
opt(optIn == nullptr ? optIn
: Options::getRoot()->getSection("invertableOperator")),
localmesh(localmeshIn == nullptr ? mesh : localmeshIn), doneSetup(false) {
localmesh(localmeshIn == nullptr ? bout::globals::mesh : localmeshIn), doneSetup(false) {
AUTO_TRACE();
};

Expand Down
3 changes: 2 additions & 1 deletion include/bout/physicsmodel.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class PhysicsModel;
#include "solver.hxx"
#include "unused.hxx"
#include "bout/macro_for_each.hxx"

/*!
Base class for physics models
*/
Expand Down Expand Up @@ -310,7 +311,7 @@ private:
solver->setModel(model); \
Monitor * bout_monitor = new BoutMonitor(); \
solver->addMonitor(bout_monitor, Solver::BACK); \
solver->outputVars(dump); \
solver->outputVars(bout::globals::dump); \
solver->solve(); \
delete model; \
delete solver; \
Expand Down
4 changes: 3 additions & 1 deletion include/bout/solver.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ typedef int (*TimestepMonitorFunc)(Solver *solver, BoutReal simtime, BoutReal la
#include "vector2d.hxx"
#include "vector3d.hxx"

#define BOUT_NO_USING_NAMESPACE_BOUTGLOBALS
#include "physicsmodel.hxx"
#undef BOUT_NO_USING_NAMESPACE_BOUTGLOBALS

#include <string>
#include <list>
Expand Down Expand Up @@ -290,7 +292,7 @@ class Solver {
///
/// @param[inout] outputfile The file to add variable to
/// @param[in] save_repeat If true, add variables with time dimension
void outputVars(Datafile &outputfile, bool save_repeat=true);
virtual void outputVars(Datafile &outputfile, bool save_repeat=true);
ZedThree marked this conversation as resolved.
Show resolved Hide resolved

/*!
* Create a Solver object. This uses the "type" option
Expand Down
96 changes: 49 additions & 47 deletions include/datafile.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,18 @@ class Datafile;
#define __DATAFILE_H__

#include "bout_types.hxx"
#include "field2d.hxx"
#include "field3d.hxx"
#include "vector2d.hxx"
#include "vector3d.hxx"
#include "options.hxx"
#include "bout/macro_for_each.hxx"

#include "dataformat.hxx"

#include <cstdarg>
#include <cstdio>
class Mesh;
class Field2D;
class Field3D;
class Vector2D;
class Vector3D;

#include <vector>
#include <string>
Expand All @@ -37,7 +38,7 @@ class Datafile;
*/
class Datafile {
public:
Datafile(Options *opt = nullptr);
Datafile(Options *opt = nullptr, Mesh* mesh_in = nullptr);
Datafile(Datafile &&other) noexcept;
~Datafile(); // need to delete filename

Expand Down Expand Up @@ -78,6 +79,7 @@ class Datafile {
void setAttribute(const std::string &varname, const std::string &attrname, BoutReal value);

private:
Mesh* mesh;
bool parallel; // Use parallel formats?
bool flush; // Flush after every write?
bool guards; // Write guard cells?
Expand Down Expand Up @@ -137,63 +139,63 @@ class Datafile {
};

/// Write this variable once to the grid file
#define SAVE_ONCE1(var) dump.add(var, #var, 0);
#define SAVE_ONCE1(var) bout::globals::dump.add(var, #var, 0);
#define SAVE_ONCE2(var1, var2) { \
dump.add(var1, #var1, 0); \
dump.add(var2, #var2, 0);}
bout::globals::dump.add(var1, #var1, 0); \
bout::globals::dump.add(var2, #var2, 0);}
#define SAVE_ONCE3(var1, var2, var3) {\
dump.add(var1, #var1, 0); \
dump.add(var2, #var2, 0); \
dump.add(var3, #var3, 0);}
bout::globals::dump.add(var1, #var1, 0); \
bout::globals::dump.add(var2, #var2, 0); \
bout::globals::dump.add(var3, #var3, 0);}
#define SAVE_ONCE4(var1, var2, var3, var4) { \
dump.add(var1, #var1, 0); \
dump.add(var2, #var2, 0); \
dump.add(var3, #var3, 0); \
dump.add(var4, #var4, 0);}
bout::globals::dump.add(var1, #var1, 0); \
bout::globals::dump.add(var2, #var2, 0); \
bout::globals::dump.add(var3, #var3, 0); \
bout::globals::dump.add(var4, #var4, 0);}
#define SAVE_ONCE5(var1, var2, var3, var4, var5) {\
dump.add(var1, #var1, 0); \
dump.add(var2, #var2, 0); \
dump.add(var3, #var3, 0); \
dump.add(var4, #var4, 0); \
dump.add(var5, #var5, 0);}
bout::globals::dump.add(var1, #var1, 0); \
bout::globals::dump.add(var2, #var2, 0); \
bout::globals::dump.add(var3, #var3, 0); \
bout::globals::dump.add(var4, #var4, 0); \
bout::globals::dump.add(var5, #var5, 0);}
#define SAVE_ONCE6(var1, var2, var3, var4, var5, var6) {\
dump.add(var1, #var1, 0); \
dump.add(var2, #var2, 0); \
dump.add(var3, #var3, 0); \
dump.add(var4, #var4, 0); \
dump.add(var5, #var5, 0); \
dump.add(var6, #var6, 0);}
bout::globals::dump.add(var1, #var1, 0); \
bout::globals::dump.add(var2, #var2, 0); \
bout::globals::dump.add(var3, #var3, 0); \
bout::globals::dump.add(var4, #var4, 0); \
bout::globals::dump.add(var5, #var5, 0); \
bout::globals::dump.add(var6, #var6, 0);}

#define SAVE_ONCE(...) \
{ MACRO_FOR_EACH(SAVE_ONCE1, __VA_ARGS__) }

/// Write this variable every timestep
#define SAVE_REPEAT1(var) dump.add(var, #var, 1);
#define SAVE_REPEAT1(var) bout::globals::dump.add(var, #var, 1);
#define SAVE_REPEAT2(var1, var2) { \
dump.add(var1, #var1, 1); \
dump.add(var2, #var2, 1);}
bout::globals::dump.add(var1, #var1, 1); \
bout::globals::dump.add(var2, #var2, 1);}
#define SAVE_REPEAT3(var1, var2, var3) {\
dump.add(var1, #var1, 1); \
dump.add(var2, #var2, 1); \
dump.add(var3, #var3, 1);}
bout::globals::dump.add(var1, #var1, 1); \
bout::globals::dump.add(var2, #var2, 1); \
bout::globals::dump.add(var3, #var3, 1);}
#define SAVE_REPEAT4(var1, var2, var3, var4) { \
dump.add(var1, #var1, 1); \
dump.add(var2, #var2, 1); \
dump.add(var3, #var3, 1); \
dump.add(var4, #var4, 1);}
bout::globals::dump.add(var1, #var1, 1); \
bout::globals::dump.add(var2, #var2, 1); \
bout::globals::dump.add(var3, #var3, 1); \
bout::globals::dump.add(var4, #var4, 1);}
#define SAVE_REPEAT5(var1, var2, var3, var4, var5) {\
dump.add(var1, #var1, 1); \
dump.add(var2, #var2, 1); \
dump.add(var3, #var3, 1); \
dump.add(var4, #var4, 1); \
dump.add(var5, #var5, 1);}
bout::globals::dump.add(var1, #var1, 1); \
bout::globals::dump.add(var2, #var2, 1); \
bout::globals::dump.add(var3, #var3, 1); \
bout::globals::dump.add(var4, #var4, 1); \
bout::globals::dump.add(var5, #var5, 1);}
#define SAVE_REPEAT6(var1, var2, var3, var4, var5, var6) {\
dump.add(var1, #var1, 1); \
dump.add(var2, #var2, 1); \
dump.add(var3, #var3, 1); \
dump.add(var4, #var4, 1); \
dump.add(var5, #var5, 1); \
dump.add(var6, #var6, 1);}
bout::globals::dump.add(var1, #var1, 1); \
bout::globals::dump.add(var2, #var2, 1); \
bout::globals::dump.add(var3, #var3, 1); \
bout::globals::dump.add(var4, #var4, 1); \
bout::globals::dump.add(var5, #var5, 1); \
bout::globals::dump.add(var6, #var6, 1);}

#define SAVE_REPEAT(...) \
{ MACRO_FOR_EACH(SAVE_REPEAT1, __VA_ARGS__) }
Expand Down
6 changes: 6 additions & 0 deletions include/dataformat.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,12 @@ class DataFormat;
#include <memory>
#include <vector>

class Mesh;

// Can't copy, to control access to file
class DataFormat {
public:
DataFormat(Mesh* mesh_in = nullptr);
virtual ~DataFormat() { }
// File opening routines
virtual bool openr(const char *name) = 0;
Expand Down Expand Up @@ -195,6 +198,9 @@ class DataFormat {
/// -------
/// value A BoutReal attribute of the variable
virtual bool getAttribute(const std::string &varname, const std::string &attrname, BoutReal &value) = 0;

protected:
Mesh* mesh;
};

// For backwards compatability. In formatfactory.cxx
Expand Down
4 changes: 2 additions & 2 deletions include/field.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Field;

#include "bout_types.hxx"
#include "boutexception.hxx"
#include <globals.hxx>
#include "msg_stack.hxx"
#include "stencils.hxx"
#include <bout/rvec.hxx>
Expand All @@ -41,7 +42,6 @@ class Field;

class Mesh;
class Coordinates;
extern Mesh * mesh; ///< Global mesh

#ifdef TRACK
#include <string>
Expand Down Expand Up @@ -94,7 +94,7 @@ class Field {
if (fieldmesh){
return fieldmesh;
} else {
return mesh;
return bout::globals::mesh;
}
}

Expand Down
7 changes: 6 additions & 1 deletion include/globals.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@
#ifndef __GLOBALS_H__
#define __GLOBALS_H__

#include "bout/mesh.hxx"
#include "datafile.hxx"
#include "bout/macro_for_each.hxx"

class Mesh;

namespace bout {
namespace globals {
#ifndef GLOBALORIGIN
#define GLOBAL extern
#define SETTING(name, val) extern name
Expand Down Expand Up @@ -83,5 +86,7 @@ GLOBAL Datafile dump;

#undef GLOBAL
#undef SETTING
} // namespace globals
} // namespace bout

#endif // __GLOBALS_H__
2 changes: 1 addition & 1 deletion include/interpolation.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ protected:

public:
Interpolation(int y_offset = 0, Mesh* localmeshIn = nullptr)
: localmesh(localmeshIn == nullptr ? mesh : localmeshIn),
: localmesh(localmeshIn == nullptr ? bout::globals::mesh : localmeshIn),
skip_mask(*localmesh, false), y_offset(y_offset) {}
Interpolation(const BoutMask &mask, int y_offset = 0, Mesh *mesh = nullptr)
: Interpolation(y_offset, mesh) {
Expand Down
4 changes: 2 additions & 2 deletions include/invert_laplace.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ const int INVERT_OUT_RHS = 32768; ///< Use input value in RHS at outer boundary
/// Base class for Laplacian inversion
class Laplacian {
public:
Laplacian(Options *options = nullptr, const CELL_LOC loc = CELL_CENTRE, Mesh* mesh_in = mesh);
Laplacian(Options *options = nullptr, const CELL_LOC loc = CELL_CENTRE, Mesh* mesh_in = nullptr);
virtual ~Laplacian() {}

/// Set coefficients for inversion. Re-builds matrices if necessary
Expand Down Expand Up @@ -192,7 +192,7 @@ public:
*
* @param[in] opt The options section to use. By default "laplace" will be used
*/
static Laplacian *create(Options *opt = nullptr, const CELL_LOC loc = CELL_CENTRE, Mesh *mesh_in = mesh);
static Laplacian *create(Options *opt = nullptr, const CELL_LOC loc = CELL_CENTRE, Mesh *mesh_in = bout::globals::mesh);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should mesh_in really be nullptr here? Presumably this is passed to the constructor noted above (or equivalent) which can then choose what to do if it receives nullptr?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed -- nullptr is a better default, it's what we do in other bits of the code, as well as giving us some space to potential remove the global mesh entirely.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't just pass nullptr here because LaplaceFactory uses mesh_in to check whether we can use a serial solver

if(mesh_in->firstX() && mesh_in->lastX()) {
// Can use serial algorithm

I think this was originally to allow the default Laplace solver to switch between serial_tri and spt depending on whether a serial or parallel solver was needed, but now the default is cyclic in either case. I think it would be better now to allow any Laplace solver to be created, and move the check into the constructor of the serial solvers. Does that sound reasonable, and should I do it here or create a separate PR?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If LaplaceFactory gets a nullptr, it could just fetch the global mesh though?

LaplaceFactory should get rewritten anyway (e.g. using GenericFactory), so I would leave that to another PR

static Laplacian* defaultInstance(); ///< Return pointer to global singleton

static void cleanup(); ///< Frees all memory
Expand Down