Skip to content

Commit d9f2a66

Browse files
authored
Merge pull request #3323 from boutproject/communicate-fieldperp-v2
Communicate `FieldPerp` consistently with other Fields
2 parents e465496 + 0b71d58 commit d9f2a66

File tree

16 files changed

+164
-177
lines changed

16 files changed

+164
-177
lines changed

include/bout/field2d.hxx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,7 @@ public:
252252
Field2D& operator/=(BoutReal rhs);
253253

254254
// FieldData virtual functions
255-
256-
bool is3D() const override { return false; }
255+
FieldType field_type() const override { return FieldType::field2d; }
257256

258257
#if CHECK > 0
259258
void doneComms() override { bndry_xin = bndry_xout = bndry_yup = bndry_ydown = true; }

include/bout/field3d.hxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ public:
474474
///@}
475475

476476
// FieldData virtual functions
477-
bool is3D() const override { return true; }
477+
FieldType field_type() const override { return FieldType::field3d; }
478478

479479
#if CHECK > 0
480480
void doneComms() override { bndry_xin = bndry_xout = bndry_yup = bndry_ydown = true; }

include/bout/field_data.hxx

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Copyright 2010 B.D.Dudson, S.Farley, M.V.Umansky, X.Q.Xu
77
*
88
* Contact: Ben Dudson, bd512@york.ac.uk
9-
*
9+
*
1010
* This file is part of BOUT++.
1111
*
1212
* BOUT++ is free software: you can redistribute it and/or modify
@@ -33,6 +33,7 @@ class FieldData;
3333
#include "bout/bout_types.hxx"
3434
#include "bout/unused.hxx"
3535

36+
#include <cstdint>
3637
#include <map>
3738
#include <memory>
3839
#include <string>
@@ -71,13 +72,22 @@ public:
7172
/// Get variable location
7273
virtual CELL_LOC getLocation() const;
7374

75+
/// Enum to distinguish the different kinds of Fields
76+
enum class FieldType : std::uint8_t { field3d, field2d, fieldperp };
77+
/// Is this an instance of `Field3D`, `Field2D`, or `FieldPerp`?
78+
virtual FieldType field_type() const = 0;
79+
7480
// Defines interface which must be implemented
7581
/// True if variable is 3D
76-
virtual bool is3D() const = 0;
82+
[[deprecated("Use `field_type()` instead")]]
83+
bool is3D() const {
84+
return field_type() == FieldType::field3d;
85+
}
86+
7787
/// Number of BoutReals in one element
7888
virtual int elementSize() const { return 1; }
7989

80-
virtual void doneComms(){}; // Notifies that communications done
90+
virtual void doneComms() {}; // Notifies that communications done
8191

8292
// Boundary conditions
8393
void setBoundary(const std::string& name); ///< Set the boundary conditions
@@ -86,13 +96,13 @@ public:
8696
copyBoundary(const FieldData& f); ///< Copy the boundary conditions from another field
8797

8898
virtual void applyBoundary(bool UNUSED(init) = false) {}
89-
virtual void applyTDerivBoundary(){};
99+
virtual void applyTDerivBoundary() {};
90100

91-
virtual void applyParallelBoundary(){};
92-
virtual void applyParallelBoundary(BoutReal UNUSED(t)){};
93-
virtual void applyParallelBoundary(const std::string& UNUSED(condition)){};
101+
virtual void applyParallelBoundary() {};
102+
virtual void applyParallelBoundary(BoutReal UNUSED(t)) {};
103+
virtual void applyParallelBoundary(const std::string& UNUSED(condition)) {};
94104
virtual void applyParallelBoundary(const std::string& UNUSED(region),
95-
const std::string& UNUSED(condition)){};
105+
const std::string& UNUSED(condition)) {};
96106
// JMAD
97107
void addBndryFunction(FuncPtr userfunc, BndryLoc location);
98108
void addBndryGenerator(FieldGeneratorPtr gen, BndryLoc location);

include/bout/fieldgroup.hxx

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,33 @@
11
#ifndef BOUT_FIELDGROUP_H
22
#define BOUT_FIELDGROUP_H
33

4-
#include "bout/field_data.hxx"
5-
#include <bout/field3d.hxx>
6-
4+
#include <bout/traits.hxx>
75
#include <bout/vector2d.hxx>
86
#include <bout/vector3d.hxx>
97

108
#include <vector>
119

12-
#include <algorithm>
10+
class Field2D;
11+
class Field3D;
12+
class FieldPerp;
13+
class Field;
1314

1415
/// Group together fields for easier communication
1516
///
16-
/// Note: The FieldData class is used as a base class,
17-
/// which is inherited by Field2D, Field3D, Vector2D and Vector3D
18-
/// however Vector2D and Vector3D are stored by reference to their
19-
/// components (x,y,z) as Field2D or Field3D objects.
17+
/// Note: The `Field` class is used as a base class,
18+
/// which is inherited by `Field2D`, `Field3D`, `FieldPerp`;
19+
/// however `Vector2D` and `Vector3D` are stored by reference to their
20+
/// components ``(x, y, z)`` as `Field2D` or `Field3D` objects.
2021
class FieldGroup {
2122
public:
2223
FieldGroup() = default;
2324
FieldGroup(const FieldGroup& other) = default;
2425
FieldGroup(FieldGroup&& other) = default;
2526
FieldGroup& operator=(const FieldGroup& other) = default;
2627
FieldGroup& operator=(FieldGroup&& other) = default;
28+
~FieldGroup() = default;
2729

28-
/// Constructor with a single FieldData \p f
29-
FieldGroup(FieldData& f) { fvec.push_back(&f); }
30-
31-
/// Constructor with a single Field3D \p f
30+
FieldGroup(Field& f) { fvec.push_back(&f); }
3231
FieldGroup(Field3D& f) {
3332
fvec.push_back(&f);
3433
f3vec.push_back(&f);
@@ -56,7 +55,7 @@ public:
5655
}
5756

5857
/// Variadic constructor. Allows an arbitrary number of
59-
/// FieldData arguments
58+
/// Field arguments
6059
///
6160
/// The explicit keyword prevents FieldGroup being constructed with arbitrary
6261
/// types. In particular arguments to add() cannot be implicitly converted
@@ -78,12 +77,12 @@ public:
7877
return *this;
7978
}
8079

81-
/// Add a FieldData \p f to the group.
80+
/// Add a Field \p f to the group.
8281
///
8382
/// A pointer to this field will be stored internally,
8483
/// so the lifetime of this variable should be longer
8584
/// than the lifetime of this group.
86-
void add(FieldData& f) { fvec.push_back(&f); }
85+
void add(Field& f) { fvec.push_back(&f); }
8786

8887
// Add a 3D field \p f, which goes into both vectors.
8988
//
@@ -121,12 +120,8 @@ public:
121120
}
122121

123122
/// Add multiple fields to this group
124-
///
125-
/// This is a variadic template which allows Field3D objects to be
126-
/// treated as a special case. An arbitrary number of fields can be
127-
/// added.
128123
template <typename... Ts>
129-
void add(FieldData& t, Ts&... ts) {
124+
void add(Field& t, Ts&... ts) {
130125
add(t); // Add the first using functions above
131126
add(ts...); // Add the rest
132127
}
@@ -165,16 +160,14 @@ public:
165160
}
166161

167162
/// Iteration over all fields
168-
using iterator = std::vector<FieldData*>::iterator;
169-
iterator begin() { return fvec.begin(); }
170-
iterator end() { return fvec.end(); }
163+
auto begin() { return fvec.begin(); }
164+
auto end() { return fvec.end(); }
171165

172166
/// Const iteration over all fields
173-
using const_iterator = std::vector<FieldData*>::const_iterator;
174-
const_iterator begin() const { return fvec.begin(); }
175-
const_iterator end() const { return fvec.end(); }
167+
auto begin() const { return fvec.cbegin(); }
168+
auto end() const { return fvec.cend(); }
176169

177-
const std::vector<FieldData*>& get() const { return fvec; }
170+
const std::vector<Field*>& get() const { return fvec; }
178171

179172
/// Iteration over 3D fields
180173
const std::vector<Field3D*>& field3d() const { return f3vec; }
@@ -183,8 +176,8 @@ public:
183176
void makeUnique();
184177

185178
private:
186-
std::vector<FieldData*> fvec; // Vector of fields
187-
std::vector<Field3D*> f3vec; // Vector of 3D fields
179+
std::vector<Field*> fvec; // Vector of fields
180+
std::vector<Field3D*> f3vec; // Vector of 3D fields
188181
};
189182

190183
/// Combine two FieldGroups

include/bout/fieldperp.hxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ public:
303303
*/
304304
int getNz() const override { return nz; };
305305

306-
bool is3D() const override { return false; }
306+
FieldType field_type() const override { return FieldType::fieldperp; }
307307

308308
friend void swap(FieldPerp& first, FieldPerp& second) noexcept;
309309

include/bout/mesh.hxx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ class Mesh;
6363
#include <optional>
6464
#include <set>
6565
#include <string>
66+
#include <vector>
6667

6768
class BoundaryRegion;
6869
class BoundaryRegionPar;
@@ -299,11 +300,6 @@ public:
299300
/// @param g The group of fields to communicate. Guard cells will be modified
300301
void communicateYZ(FieldGroup& g);
301302

302-
/*!
303-
* Communicate an X-Z field
304-
*/
305-
virtual void communicate(FieldPerp& f);
306-
307303
/*!
308304
* Send a list of FieldData objects
309305
* Packs arguments into a FieldGroup and passes
@@ -814,8 +810,8 @@ protected:
814810
const std::vector<int> readInts(const std::string& name, int n);
815811

816812
/// Calculates the size of a message for a given x and y range
817-
int msg_len(const std::vector<FieldData*>& var_list, int xge, int xlt, int yge,
818-
int ylt);
813+
int msg_len(const std::vector<Field*>& var_list, int xge, int xlt, int yge,
814+
int ylt) const;
819815

820816
/// Initialise derivatives
821817
void derivs_init(Options* options);

include/bout/vector2d.hxx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* Copyright 2010 B.D.Dudson, S.Farley, M.V.Umansky, X.Q.Xu
1414
*
1515
* Contact: Ben Dudson, bd512@york.ac.uk
16-
*
16+
*
1717
* This file is part of BOUT++.
1818
*
1919
* BOUT++ is free software: you can redistribute it and/or modify
@@ -142,7 +142,7 @@ public:
142142
CELL_LOC getLocation() const override;
143143

144144
// FieldData virtual functions
145-
bool is3D() const override { return false; }
145+
FieldType field_type() const override { return FieldType::field2d; }
146146
int elementSize() const override { return 3; }
147147

148148
/// Apply boundary condition to all fields

include/bout/vector3d.hxx

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* Copyright 2010 B.D.Dudson, S.Farley, M.V.Umansky, X.Q.Xu
1010
*
1111
* Contact: Ben Dudson, bd512@york.ac.uk
12-
*
12+
*
1313
* This file is part of BOUT++.
1414
*
1515
* BOUT++ is free software: you can redistribute it and/or modify
@@ -45,10 +45,10 @@ class Vector2D;
4545
* -------
4646
*
4747
* Vector3D f;
48-
*
48+
*
4949
* a.x; // Error! a.x not allocated
5050
*
51-
*
51+
*
5252
*/
5353
class Vector3D : public FieldData {
5454
public:
@@ -70,7 +70,7 @@ public:
7070
~Vector3D() override;
7171

7272
/*!
73-
* The components of the vector. These can be
73+
* The components of the vector. These can be
7474
* either co- or contra-variant, depending on
7575
* the boolean flag "covariant"
7676
*/
@@ -92,8 +92,8 @@ public:
9292
bool covariant{true};
9393

9494
/*!
95-
* In-place conversion to covariant form.
96-
*
95+
* In-place conversion to covariant form.
96+
*
9797
* If already covariant (covariant = true) then does nothing
9898
* If contravariant, multiplies by metric tensor g_{ij}
9999
*/
@@ -104,7 +104,7 @@ public:
104104
*
105105
* If already contravariant (covariant = false) then does nothing
106106
* If covariant, multiplies by metric tensor g^{ij}
107-
*
107+
*
108108
*/
109109
void toContravariant();
110110

@@ -114,14 +114,14 @@ public:
114114
* The first time this is called, a new Vector3D object is created.
115115
* Subsequent calls return a pointer to this same object
116116
*
117-
* For convenience, a standalone function "ddt" exists, so that
117+
* For convenience, a standalone function "ddt" exists, so that
118118
*
119119
* ddt(v) is equivalent to *(v.timeDeriv())
120-
*
120+
*
121121
* This does some book-keeping to ensure that the time derivative
122122
* of the components is the same as the components of the time derivative
123123
*
124-
* ddt(v).x == ddt(v.x)
124+
* ddt(v).x == ddt(v.x)
125125
*/
126126
Vector3D* timeDeriv();
127127

@@ -172,7 +172,7 @@ public:
172172
CELL_LOC getLocation() const override;
173173

174174
// FieldData virtual functions
175-
bool is3D() const override { return true; }
175+
FieldType field_type() const override { return FieldType::field3d; }
176176
int elementSize() const override { return 3; }
177177

178178
void applyBoundary(bool init = false) override;
@@ -203,7 +203,7 @@ const Vector3D cross(const Vector3D& lhs, const Vector2D& rhs);
203203

204204
/*!
205205
* Absolute magnitude (modulus) of a vector |v|
206-
*
206+
*
207207
* sqrt( v.x^2 + v.y^2 + v.z^2 )
208208
*/
209209
const Field3D abs(const Vector3D& v, const std::string& region = "RGN_ALL");

0 commit comments

Comments
 (0)