OPAL (Object Oriented Parallel Accelerator Library) 2024.2
OPAL
FM3DH5BlockBase.h
Go to the documentation of this file.
1//
2// Class FM3DH5BlockBase
3// Base class for 3D field-maps in stored in H5hut files.
4//
5// Copyright (c) 2020, Achim Gsell, Paul Scherrer Institut, Villigen PSI, Switzerland
6// All rights reserved.
7//
8// This file is part of OPAL.
9//
10// OPAL is free software: you can redistribute it and/or modify
11// it under the terms of the GNU General Public License as published by
12// the Free Software Foundation, either version 3 of the License, or
13// (at your option) any later version.
14//
15// You should have received a copy of the GNU General Public License
16// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
17//
18#ifndef CLASSIC_FIELDMAP3DH5BLOCKBASE_H
19#define CLASSIC_FIELDMAP3DH5BLOCKBASE_H
20
21#include "Algorithms/Vektor.h"
22#include "Fields/FieldArray.h"
23#include "Fields/Fieldmap.h"
24
25#include <h5core/h5_types.h>
26
27#include <algorithm>
28#include <cmath>
29#include <cstddef>
30#include <memory>
31#include <string>
32#include <utility>
33#include <vector>
34
35class Inform;
36static_assert (sizeof(double) == sizeof (h5_float64_t),
37 "double and h5_float64_t are not the same type" );
38static_assert (sizeof(long long) == sizeof (h5_int64_t),
39 "long long and h5_int64_t are not the same type" );
40
41class _FM3DH5BlockBase: virtual public _Fieldmap {
42
43public:
44 virtual void readMap (
45 ) {};
46
47 virtual void freeMap (
48 ) {};
49
50 virtual bool getFieldstrength (
51 const Vector_t& /*R*/, Vector_t& /*E*/, Vector_t& /*B*/) const = 0;
52
53 virtual void getFieldDimensions (
54 double &zBegin, double &zEnd
55 ) const {
56 zBegin = zbegin_m;
57 zEnd = zend_m;
58 }
59
60 virtual void getFieldDimensions (
61 double &xIni, double &xFinal,
62 double &yIni, double &yFinal,
63 double &zIni, double &zFinal
64 ) const {
65 xIni = xbegin_m;
66 xFinal = xend_m;
67 yIni = ybegin_m;
68 yFinal = yend_m;
69 zIni = zbegin_m;
70 zFinal = zend_m;
71 }
72
73 virtual bool getFieldDerivative (
74 const Vector_t &/*R*/,
75 Vector_t &/*E*/,
76 Vector_t &/*B*/,
77 const DiffDirection &/*dir*/
78 ) const {
79 return false;
80 }
81
82 virtual void swap(
83 ) {};
84
85 virtual void getInfo (
86 Inform *msg);
87
88 virtual double getFrequency (
89 ) const;
90
91 virtual void setFrequency (
92 double freq);
93
94 virtual void getOnaxisEz (
95 std::vector<std::pair<double, double> >& F);
96
97protected:
99 ) {};
100
102 ) {};
103
105 const std::string& filename);
106
107 long long getNumSteps (
108 void);
109
110 void setStep (
111 const long long);
112
113 void getFieldInfo (
114 const char*);
115
117 void);
118
119 void readField (
120 const char* name,
121 double* x,
122 double* y,
123 double* z
124 );
125
129 bool tryMapComponents (
130 const char* name,
131 long long step,
132 FieldArray& x,
133 FieldArray& y,
134 FieldArray& z,
135 std::size_t n
136 );
137
141 bool anyNonZero (
142 const FieldArray& x,
143 const FieldArray& y,
144 const FieldArray& z
145 ) const;
146
147 void closeFile (
148 void);
149
150 virtual bool isInside (
151 const Vector_t &r
152 ) const {
153 return ((r(0) >= xbegin_m && r(0) < xend_m) &&
154 (r(1) >= ybegin_m && r(1) < yend_m) &&
155 (r(2) >= zbegin_m && r(2) < zend_m));
156 }
157
159 unsigned int i;
160 unsigned int j;
161 unsigned int k;
164 i(0),
165 j(0),
166 k(0),
167 weight(0.0)
168 {}
169 };
170
171 /*
172 The 3-dimensional fieldmaps are stored in a 1-dimensional arrays.
173 Please note that the FORTRAN indexing scheme is used in H5hut!
174
175 This functions maps the 3-dimensional index (i, j, k) to the
176 corresponding index in the 1-dimensional array.
177 */
178 unsigned long getIndex (
179 unsigned int i,
180 unsigned int j,
181 unsigned int k
182 ) const {
183 unsigned long result = j + k * num_gridpy_m;
184 result = i + result * num_gridpx_m;
185
186 return result;
187 }
188
189 /*
190 Compute grid indices for a point X.
191
192 Before calling this function a Isinside(X) test must
193 to be performed!
194
195 2-dim example with num_gridpx_m = 5, num_gridpy_m = 4
196
197 3 +----+----+----+----+
198 | | | | |
199 2 +----+----+----+----+
200 | | | | X|
201 1 +----+----+----+----+
202 | | | | |
203 0 +----+----+----+----+
204 0 1 2 3 4
205
206 idx.x = 3
207 idx.y = 1
208
209 Notes:
210 In above example: max for idx.x is num_gridpx_m - 2 which is 3.
211
212 If X is close to a cell border, it can happen that the computation
213 of the index is wrong due to rounding errors. In the example above
214 idx.i == 4 instead of 3. To mitigate this issue we use __float128.
215 To avoid out-of-bound errors we set the index to the max allowed
216 value, if it is out of bound.
217
218 std::fmod() doesn't support __float128 types! Best we can use is
219 long double.
220 */
222 IndexTriplet idx;
223
224 // Derive the cell index and the in-cell weight from the SAME quantity, so the
225 // two can never disagree, and keep the weight normalised to [0,1) as
226 // getWeightedData() requires. Plain double throughout: the input X is already
227 // double, so long double bought no accuracy, while fmodl() is a libm call and
228 // x87 arithmetic does not vectorise.
229 const double fx = (X(0) - xbegin_m) * inv_hx_m;
230 const double fy = (X(1) - ybegin_m) * inv_hy_m;
231 const double fz = (X(2) - zbegin_m) * inv_hz_m;
232
233 // floor() before the cast: a negative fx would make the unsigned conversion
234 // undefined. isInside() is supposed to exclude that, but this is one multiply
235 // away from being free and removes the trap.
236 const double ax = std::floor(fx);
237 const double ay = std::floor(fy);
238 const double az = std::floor(fz);
239
240 idx.i = (unsigned int)std::min(std::max(ax, 0.0), (double)(num_gridpx_m - 2));
241 idx.j = (unsigned int)std::min(std::max(ay, 0.0), (double)(num_gridpy_m - 2));
242 idx.k = (unsigned int)std::min(std::max(az, 0.0), (double)(num_gridpz_m - 2));
243
244 // Measured against idx.* rather than a*, so that when the clamp fires the
245 // weight still describes the cell we actually selected.
246 idx.weight(0) = fx - (double)idx.i;
247 idx.weight(1) = fy - (double)idx.j;
248 idx.weight(2) = fz - (double)idx.k;
249
250 return idx;
251 }
252
253 double getWeightedData (
254 const FieldArray& data,
255 const IndexTriplet& idx,
256 unsigned short corner) const;
257
259 const FieldArray&,
260 const FieldArray&,
261 const FieldArray&,
262 const Vector_t& X) const;
263
264 enum : unsigned short {
265 LX = 0, // low X
266 LY = 0, // low Y
267 LZ = 0, // low Z
268 HX = 4, // high X
269 HY = 2, // high Y
270 HZ = 1}; // high Z
271
272 h5_file_t file_m;
280 bool hasE_m = true;
281
282 double xbegin_m;
283 double xend_m;
284
285 double ybegin_m;
286 double yend_m;
287
288 double zbegin_m;
289 double zend_m;
290
291 double hx_m;
292 double hy_m;
293 double hz_m;
295 // Reciprocals of the grid spacing, cached in getFieldInfo(). getIndex() runs
296 // ~16x per particle per step; a division there is pure waste.
297 double inv_hx_m;
298 double inv_hy_m;
299 double inv_hz_m;
300
301 unsigned int num_gridpx_m;
302 unsigned int num_gridpy_m;
303 unsigned int num_gridpz_m;
306
307 bool swap_m;
308 friend class _Fieldmap;
309};
310
311using FM3DH5BlockBase = std::shared_ptr<_FM3DH5BlockBase>;
312
313#endif
DiffDirection
Definition Fieldmap.h:55
std::shared_ptr< _FM3DH5BlockBase > FM3DH5BlockBase
#define X(arg)
Definition fftpack.cpp:105
const std::string name
virtual bool isInside(const Vector_t &r) const
virtual double getFrequency() const
bool anyNonZero(const FieldArray &x, const FieldArray &y, const FieldArray &z) const
void setStep(const long long)
virtual bool getFieldstrength(const Vector_t &, Vector_t &, Vector_t &) const =0
FieldArray FieldstrengthEx_m
void getResonanceFrequency(void)
virtual bool getFieldDerivative(const Vector_t &, Vector_t &, Vector_t &, const DiffDirection &) const
virtual void getInfo(Inform *msg)
IndexTriplet getIndex(const Vector_t &X) const
unsigned int num_gridpx_m
virtual ~_FM3DH5BlockBase()
unsigned long getIndex(unsigned int i, unsigned int j, unsigned int k) const
virtual void setFrequency(double freq)
double getWeightedData(const FieldArray &data, const IndexTriplet &idx, unsigned short corner) const
virtual void swap()
void getFieldInfo(const char *)
virtual void getOnaxisEz(std::vector< std::pair< double, double > > &F)
virtual void readMap()
Vector_t interpolateTrilinearly(const FieldArray &, const FieldArray &, const FieldArray &, const Vector_t &X) const
void readField(const char *name, double *x, double *y, double *z)
void openFileMPIOCollective(const std::string &filename)
virtual void freeMap()
long long getNumSteps(void)
unsigned int num_gridpz_m
unsigned int num_gridpy_m
FieldArray FieldstrengthEz_m
virtual void getFieldDimensions(double &xIni, double &xFinal, double &yIni, double &yFinal, double &zIni, double &zFinal) const
FieldArray FieldstrengthEy_m
virtual void getFieldDimensions(double &zBegin, double &zEnd) const
bool tryMapComponents(const char *name, long long step, FieldArray &x, FieldArray &y, FieldArray &z, std::size_t n)