Skip to content

Commit 8a20105

Browse files
committed
Communicate FieldPerp consistently with other Fields
- Add `Field::field_type()` virtual method - Removes `Mesh::communicate(FieldPerp&)` overload
1 parent 82fe1de commit 8a20105

File tree

12 files changed

+156
-163
lines changed

12 files changed

+156
-163
lines changed

include/bout/field.hxx

Lines changed: 7 additions & 1 deletion
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
@@ -30,6 +30,7 @@ class Field;
3030
#define FIELD_H
3131

3232
#include <cmath>
33+
#include <cstdint>
3334
#include <cstdio>
3435
#include <optional>
3536
#include <string>
@@ -120,6 +121,11 @@ public:
120121
/// Get the total number of points
121122
virtual int size() const = 0;
122123

124+
/// Enum to distinguish the different kinds of Fields
125+
enum class FieldType : std::uint8_t { field3d, field2d, fieldperp };
126+
/// Is this an instance of `Field3D`, `Field2D`, or `FieldPerp`?
127+
virtual FieldType field_type() const = 0;
128+
123129
friend void swap(Field& first, Field& second) noexcept {
124130
using std::swap;
125131
swap(static_cast<FieldData&>(first), static_cast<FieldData&>(second));

include/bout/field2d.hxx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ public:
252252
// FieldData virtual functions
253253

254254
bool is3D() const override { return false; }
255+
FieldType field_type() const override { return FieldType::field2d; }
255256

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

include/bout/field3d.hxx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Copyright 2010 B.D.Dudson, S.Farley, M.V.Umansky, X.Q.Xu
33
*
44
* Contact: Ben Dudson, bd512@york.ac.uk
5-
*
5+
*
66
* This file is part of BOUT++.
77
*
88
* BOUT++ is free software: you can redistribute it and/or modify
@@ -361,7 +361,7 @@ public:
361361
* Direct access to the underlying data array
362362
*
363363
* If CHECK > 2 then bounds checking is performed
364-
*
364+
*
365365
* If CHECK <= 2 then no checks are performed, to
366366
* allow inlining and optimisation of inner loops
367367
*/
@@ -474,6 +474,7 @@ public:
474474

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

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

include/bout/fieldgroup.hxx

Lines changed: 22 additions & 35 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,18 +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.
128-
template <typename... Ts>
129-
void add(FieldData& t, Ts&... ts) {
130-
add(t); // Add the first using functions above
131-
add(ts...); // Add the rest
132-
}
133-
134123
template <typename... Ts>
135-
void add(Field3D& t, Ts&... ts) {
124+
void add(Field& t, Ts&... ts) {
136125
add(t); // Add the first using functions above
137126
add(ts...); // Add the rest
138127
}
@@ -165,16 +154,14 @@ public:
165154
}
166155

167156
/// Iteration over all fields
168-
using iterator = std::vector<FieldData*>::iterator;
169-
iterator begin() { return fvec.begin(); }
170-
iterator end() { return fvec.end(); }
157+
auto begin() { return fvec.begin(); }
158+
auto end() { return fvec.end(); }
171159

172160
/// 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(); }
161+
auto begin() const { return fvec.cbegin(); }
162+
auto end() const { return fvec.cend(); }
176163

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

179166
/// Iteration over 3D fields
180167
const std::vector<Field3D*>& field3d() const { return f3vec; }
@@ -183,8 +170,8 @@ public:
183170
void makeUnique();
184171

185172
private:
186-
std::vector<FieldData*> fvec; // Vector of fields
187-
std::vector<Field3D*> f3vec; // Vector of 3D fields
173+
std::vector<Field*> fvec; // Vector of fields
174+
std::vector<Field3D*> f3vec; // Vector of 3D fields
188175
};
189176

190177
/// Combine two FieldGroups

include/bout/fieldperp.hxx

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Copyright 2010 B.D.Dudson, S.Farley, M.V.Umansky, X.Q.Xu
66
*
77
* Contact: Ben Dudson, bd512@york.ac.uk
8-
*
8+
*
99
* This file is part of BOUT++.
1010
*
1111
* BOUT++ is free software: you can redistribute it and/or modify
@@ -45,8 +45,8 @@ class Field3D; // #include "bout/field3d.hxx"
4545

4646
/*!
4747
* Represents a 2D field perpendicular to the magnetic field
48-
* at a particular index in Y, which only varies in X-Z.
49-
*
48+
* at a particular index in Y, which only varies in X-Z.
49+
*
5050
* Primarily used inside field solvers
5151
*/
5252
class FieldPerp : public Field {
@@ -205,7 +205,7 @@ public:
205205

206206
/*!
207207
* Access to the underlying data array at a given x,z index
208-
*
208+
*
209209
* If CHECK > 2 then bounds checking is performed, otherwise
210210
* no checks are performed
211211
*/
@@ -241,9 +241,9 @@ public:
241241
}
242242

243243
/*!
244-
* Access to the underlying data array. (X,Y,Z) indices for consistency with
244+
* Access to the underlying data array. (X,Y,Z) indices for consistency with
245245
* other field types
246-
*
246+
*
247247
*/
248248
BoutReal& operator()(int jx, int UNUSED(jy), int jz) { return (*this)(jx, jz); }
249249

@@ -252,7 +252,7 @@ public:
252252
}
253253

254254
/*!
255-
* Addition, modifying in-place.
255+
* Addition, modifying in-place.
256256
* This loops over the entire domain, including guard/boundary cells
257257
*/
258258
FieldPerp& operator+=(const FieldPerp& rhs);
@@ -261,7 +261,7 @@ public:
261261
FieldPerp& operator+=(BoutReal rhs);
262262

263263
/*!
264-
* Subtraction, modifying in place.
264+
* Subtraction, modifying in place.
265265
* This loops over the entire domain, including guard/boundary cells
266266
*/
267267
FieldPerp& operator-=(const FieldPerp& rhs);
@@ -270,7 +270,7 @@ public:
270270
FieldPerp& operator-=(BoutReal rhs);
271271

272272
/*!
273-
* Multiplication, modifying in place.
273+
* Multiplication, modifying in place.
274274
* This loops over the entire domain, including guard/boundary cells
275275
*/
276276
FieldPerp& operator*=(const FieldPerp& rhs);
@@ -279,7 +279,7 @@ public:
279279
FieldPerp& operator*=(BoutReal rhs);
280280

281281
/*!
282-
* Division, modifying in place.
282+
* Division, modifying in place.
283283
* This loops over the entire domain, including guard/boundary cells
284284
*/
285285
FieldPerp& operator/=(const FieldPerp& rhs);
@@ -301,6 +301,7 @@ public:
301301
int getNz() const override { return nz; };
302302

303303
bool is3D() const override { return false; }
304+
FieldType field_type() const override { return FieldType::fieldperp; }
304305

305306
friend void swap(FieldPerp& first, FieldPerp& second) noexcept;
306307

include/bout/mesh.hxx

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
*
44
* Interface for mesh classes. Contains standard variables and useful
55
* routines.
6-
*
6+
*
77
* Changelog
88
* =========
99
*
1010
* 2014-12 Ben Dudson <bd512@york.ac.uk>
1111
* * Removing coordinate system into separate
1212
* Coordinates class
1313
* * Adding index derivative functions from derivs.cxx
14-
*
14+
*
1515
* 2010-06 Ben Dudson, Sean Farley
1616
* * Initial version, adapted from GridData class
1717
* * Incorporates code from topology.cpp and Communicator
@@ -20,7 +20,7 @@
2020
* Copyright 2010-2025 BOUT++ contributors
2121
*
2222
* Contact: Ben Dudson, dudson2@llnl.gov
23-
*
23+
*
2424
* This file is part of BOUT++.
2525
*
2626
* BOUT++ is free software: you can redistribute it and/or modify
@@ -65,6 +65,7 @@ class Mesh;
6565
#include <optional>
6666
#include <set>
6767
#include <string>
68+
#include <vector>
6869

6970
class BoundaryRegion;
7071
class BoundaryRegionPar;
@@ -301,11 +302,6 @@ public:
301302
/// @param g The group of fields to communicate. Guard cells will be modified
302303
void communicateYZ(FieldGroup& g);
303304

304-
/*!
305-
* Communicate an X-Z field
306-
*/
307-
virtual void communicate(FieldPerp& f);
308-
309305
/*!
310306
* Send a list of FieldData objects
311307
* Packs arguments into a FieldGroup and passes
@@ -815,8 +811,8 @@ protected:
815811
const std::vector<int> readInts(const std::string& name, int n);
816812

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

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

0 commit comments

Comments
 (0)