OPAL (Object Oriented Parallel Accelerator Library) 2024.2
OPAL
FM3DH5BlockBase.cpp
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//
19
20#include "Physics/Physics.h"
22
23#include <cstdlib>
24#include <limits>
25#include <string>
26
27#include "H5hut.h"
28
30 const std::string& filename
31 ) {
32 h5_prop_t props = H5CreateFileProp ();
33 MPI_Comm comm = Ippl::getComm();
34 if (H5SetPropFileMPIOCollective (props, &comm) == H5_ERR) {
36 "_FM3DH5BlockBase::openFileMPIOCollective () ",
37 "Cannot set MPIO collective!");
38 }
39 file_m = H5OpenFile (filename.c_str(), H5_O_RDONLY, props);
40 if (file_m == (h5_file_t)H5_ERR) {
42 "_FM3DH5BlockBase::openFileMPIOCollective () ",
43 "Cannot open file '" + filename + "'!");
44 }
45 H5CloseProp (props);
46}
47
49 long long num_steps = H5GetNumSteps(file_m);
50 if (num_steps <= 0) {
51 if (num_steps == 0) {
53 "_FM3DH5BlockBase::getNumSteps () ",
54 "Number of time-steps in file '" + Filename_m + "' is zero!");
55 } else {
57 "_FM3DH5BlockBase::getNumSteps () ",
58 "Query number of time-steps in file '" + Filename_m + "' failed!");
59 }
60 }
61 return num_steps;
62}
63
64void _FM3DH5BlockBase::setStep (long long step) {
65 if (H5SetStep(file_m, step) == H5_ERR) {
67 "_FM3DH5BlockBase::setStep () ",
68 "Cannot set time-step to " + std::to_string (step) +
69 " in file '" + Filename_m + "'!");
70 }
71}
72
74 long long last_step = getNumSteps () - 1;
75 setStep (last_step);
76 h5_size_t grid_rank;
77 h5_size_t grid_dims[3];
78 h5_size_t field_dims;
79 h5_int64_t ftype;
80 if (H5BlockGetFieldInfoByName (
81 file_m, name,
82 &grid_rank, grid_dims, &field_dims, &ftype
83 ) == H5_ERR) {
85 "_FM3DH5BlockBase::GetFieldInfo () ",
86 "Query of field info for " + std::string (name) +
87 " in time-step " + std::to_string (last_step) +
88 " in file '" + Filename_m + "' failed!");
89 }
90 num_gridpx_m = grid_dims[0];
91 num_gridpy_m = grid_dims[1];
92 num_gridpz_m = grid_dims[2];
93
94 if (H5Block3dGetFieldSpacing (
95 file_m, "Efield", &hx_m, &hy_m, &hz_m
96 ) == H5_ERR) {
98 "_FM3DH5BlockBase::GetFieldInfo () ",
99 "Query of field spacing"
100 " in time-step " + std::to_string (last_step) +
101 " in file '" + Filename_m + "' failed!");
102 }
103
104 inv_hx_m = 1.0 / hx_m;
105 inv_hy_m = 1.0 / hy_m;
106 inv_hz_m = 1.0 / hz_m;
107
108 if (H5Block3dGetFieldOrigin(
109 file_m, "Efield", &xbegin_m, &ybegin_m, &zbegin_m) == H5_ERR) {
111 "_FM3DH5BlockBase::GetFieldInfo () ",
112 "Query of field origin"
113 " in time-step " + std::to_string (last_step) +
114 " in file '" + Filename_m + "' failed!");
115 }
116 xend_m = xbegin_m + (num_gridpx_m - 1) * hx_m;
117 yend_m = ybegin_m + (num_gridpy_m - 1) * hy_m;
118 zend_m = zbegin_m + (num_gridpz_m - 1) * hz_m;
119}
120
122 if (H5ReadFileAttribFloat64 (
123 file_m, "Resonance Frequency(Hz)", &frequency_m
124 ) == H5_ERR) {
126 "_FM3DH5BlockBase::GetResonanceFrequency () ",
127 "Cannot read file attribute 'Resonance Frequency(Hz)'"
128 " in file '" + Filename_m + "'!");
129 }
131}
132
134 const char* name,
135 double* x,
136 double* y,
137 double* z
138 ) {
139 if (H5Block3dSetView(file_m,
140 0, num_gridpx_m - 1,
141 0, num_gridpy_m - 1,
142 0, num_gridpz_m - 1) == H5_ERR) {
144 "_FM3DH5BlockBase::ReadField () ",
145 "Cannot set view "
146 "0, " + std::to_string (num_gridpx_m) +
147 "0, " + std::to_string (num_gridpy_m) +
148 "0, " + std::to_string (num_gridpz_m) +
149 " in file '" + Filename_m + "'!");
150 }
151 if (H5Block3dReadVector3dFieldFloat64 (
152 file_m,
153 name,
154 x, y, z) == H5_ERR) {
156 "_FM3DH5BlockBase::ReadField () ",
157 "Cannot read field " + std::string (name) +
158 " in file '" + Filename_m + "'!");
159 }
160}
161
163 const FieldArray& x,
164 const FieldArray& y,
165 const FieldArray& z
166 ) const {
167 const std::size_t n = x.size ();
168 const std::size_t nodes = Ippl::getNodes ();
169 const std::size_t rank = Ippl::myNode ();
170 const std::size_t begin = n * rank / nodes;
171 const std::size_t end = n * (rank + 1) / nodes;
172
173 int local = (x.anyNonZero (begin, end) ||
174 y.anyNonZero (begin, end) ||
175 z.anyNonZero (begin, end)) ? 1 : 0;
176 int global = 0;
177 MPI_Allreduce (&local, &global, 1, MPI_INT, MPI_MAX, Ippl::getComm ());
178 return global != 0;
179}
180
182 if (H5CloseFile (file_m) == H5_ERR) {
184 "_FM3DH5BlockBase::closeFile () ",
185 "Error closing file '" + Filename_m + "'!");
186 }
187}
188
190 const FieldArray& data,
191 const IndexTriplet& idx,
192 unsigned short corner
193 ) const {
194 unsigned short switchX = ((corner & HX) >> 2);
195 unsigned short switchY = ((corner & HY) >> 1);
196 unsigned short switchZ = (corner & HZ);
197 double factorX = 0.5 + (1 - 2 * switchX) * (0.5 - idx.weight(0));
198 double factorY = 0.5 + (1 - 2 * switchY) * (0.5 - idx.weight(1));
199 double factorZ = 0.5 + (1 - 2 * switchZ) * (0.5 - idx.weight(2));
200
201 unsigned long i = idx.i + switchX, j = idx.j + switchY, k = idx.k + switchZ;
202
203 return factorX * factorY * factorZ * data[getIndex(i, j, k)];
204}
205
207 const FieldArray& field_strength_x,
208 const FieldArray& field_strength_y,
209 const FieldArray& field_strength_z,
210 const Vector_t& X
211 ) const {
212 IndexTriplet idx = getIndex (X);
213 Vector_t result{0.0};
214
215 result[0] = (getWeightedData(field_strength_x, idx, LX|LY|LZ) +
216 getWeightedData(field_strength_x, idx, LX|LY|HZ) +
217 getWeightedData(field_strength_x, idx, LX|HY|LZ) +
218 getWeightedData(field_strength_x, idx, LX|HY|HZ) +
219 getWeightedData(field_strength_x, idx, HX|LY|LZ) +
220 getWeightedData(field_strength_x, idx, HX|LY|HZ) +
221 getWeightedData(field_strength_x, idx, HX|HY|LZ) +
222 getWeightedData(field_strength_x, idx, HX|HY|HZ));
223
224 result[1] = (getWeightedData(field_strength_y, idx, LX|LY|LZ) +
225 getWeightedData(field_strength_y, idx, LX|LY|HZ) +
226 getWeightedData(field_strength_y, idx, LX|HY|LZ) +
227 getWeightedData(field_strength_y, idx, LX|HY|HZ) +
228 getWeightedData(field_strength_y, idx, HX|LY|LZ) +
229 getWeightedData(field_strength_y, idx, HX|LY|HZ) +
230 getWeightedData(field_strength_y, idx, HX|HY|LZ) +
231 getWeightedData(field_strength_y, idx, HX|HY|HZ));
232
233 result[2] = (getWeightedData(field_strength_z, idx, LX|LY|LZ) +
234 getWeightedData(field_strength_z, idx, LX|LY|HZ) +
235 getWeightedData(field_strength_z, idx, LX|HY|LZ) +
236 getWeightedData(field_strength_z, idx, LX|HY|HZ) +
237 getWeightedData(field_strength_z, idx, HX|LY|LZ) +
238 getWeightedData(field_strength_z, idx, HX|LY|HZ) +
239 getWeightedData(field_strength_z, idx, HX|HY|LZ) +
240 getWeightedData(field_strength_z, idx, HX|HY|HZ));
241
242 return result;
243}
244
246 (*msg) << Filename_m << " (3D dynamic) "
247 << " xini= " << xbegin_m << " xfinal= " << xend_m
248 << " yini= " << ybegin_m << " yfinal= " << yend_m
249 << " zini= " << zbegin_m << " zfinal= " << zend_m << " [mm] " << endl;
250 (*msg) << " hx= " << hx_m <<" hy= " << hy_m <<" hz= " << hz_m << " [mm] " <<endl;
251}
252
254 return frequency_m;
255}
256
258 frequency_m = freq;
259}
260
262 std::vector<std::pair<double, double>>& F
263 ) {
264 F.resize(num_gridpz_m);
265
266 double Ez_max = 0.0;
267 const double dz = (zend_m - zbegin_m) / (num_gridpz_m - 1);
268 if (!hasE_m) {
269 // readMap() released an Efield that is zero everywhere. Normalising the zero
270 // profile below used to give 0/0, so return the same NaN the arrays would have.
271 for (unsigned long int i = 0; i < num_gridpz_m; i++) {
272 F[i].first = dz * i;
273 F[i].second = std::numeric_limits<double>::quiet_NaN();
274 }
275 return;
276 }
277 const int index_x = -static_cast<int>(std::floor(xbegin_m / hx_m));
278 const double lever_x = -xbegin_m / hx_m - index_x;
279
280 const int index_y = -static_cast<int>(std::floor(ybegin_m / hy_m));
281 const double lever_y = -ybegin_m / hy_m - index_y;
282 long idx = index_x + index_y*num_gridpx_m + num_gridpx_m*num_gridpy_m;
283 for (unsigned long int i = 0;
284 i < num_gridpz_m;
285 i++, idx += num_gridpy_m * num_gridpx_m
286 ) {
287 F[i].first = dz * i;
288 F[i].second =
289 (1.0 - lever_x) * (1.0 - lever_y) * FieldstrengthEz_m[idx]
290 + lever_x * (1.0 - lever_y) * FieldstrengthEz_m[idx + 1]
291 + (1.0 - lever_x) * lever_y * FieldstrengthEz_m[idx + num_gridpx_m]
292 + lever_x * lever_y * FieldstrengthEz_m[idx + num_gridpx_m + 1];
293
294 if(std::abs(F[i].second) > Ez_max) {
295 Ez_max = std::abs(F[i].second);
296 }
297 F[i].second /= Ez_max;
298 }
299}
301 const char* name,
302 long long step,
303 FieldArray& x,
304 FieldArray& y,
305 FieldArray& z,
306 std::size_t n
307 ) {
308 static bool disabled = (std::getenv ("OPAL_NO_MMAP_FIELDMAPS") != nullptr);
309 if (disabled) {
311 << "field map '" << Filename_m << "' " << name
312 << ": reading into private memory (OPAL_NO_MMAP_FIELDMAPS is set)" << endl);
313 return false;
314 }
315
316 const std::string base =
317 "/Step#" + std::to_string (step) + "/Block/" + std::string (name) + "/";
318 FieldArray* comp[3] = {&x, &y, &z};
319 std::string why;
320 for (int i = 0; i < 3; ++ i) {
321 if (!comp[i]->tryMap (Filename_m, base + std::to_string (i), n, why)) {
322 // All or nothing: a half-mapped field would be silently wrong.
323 for (int j = 0; j < 3; ++ j) {
324 comp[j]->reset ();
325 }
327 << "field map '" << Filename_m << "' " << name
328 << ": reading into private memory (" << why << ")" << endl);
329 return false;
330 }
331 }
333 << "field map '" << Filename_m << "' " << name
334 << ": mapped read-only, "
335 << (3 * n * sizeof (double)) / (1024 * 1024) << " MiB shared per node" << endl);
336 return true;
337}
PartBunchBase< T, Dim >::ConstIterator end(PartBunchBase< T, Dim > const &bunch)
PartBunchBase< T, Dim >::ConstIterator begin(PartBunchBase< T, Dim > const &bunch)
#define X(arg)
Definition fftpack.cpp:105
#define INFOMSG(msg)
Definition IpplInfo.h:348
Inform & endl(Inform &inf)
Definition Inform.cpp:42
Inform & level1(Inform &inf)
Definition Inform.cpp:45
const std::string name
constexpr double two_pi
The value of.
Definition Physics.h:33
std::size_t size() const
Number of samples. Zero for an array that has not been filled.
Definition FieldArray.h:47
void reset()
Release the buffer or the mapping.
bool anyNonZero(std::size_t begin, std::size_t end) const
std::string Filename_m
Definition Fieldmap.h:118
virtual double getFrequency() const
bool anyNonZero(const FieldArray &x, const FieldArray &y, const FieldArray &z) const
void setStep(const long long)
void getResonanceFrequency(void)
virtual void getInfo(Inform *msg)
unsigned int num_gridpx_m
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
void getFieldInfo(const char *)
virtual void getOnaxisEz(std::vector< std::pair< double, double > > &F)
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)
long long getNumSteps(void)
unsigned int num_gridpz_m
unsigned int num_gridpy_m
FieldArray FieldstrengthEz_m
bool tryMapComponents(const char *name, long long step, FieldArray &x, FieldArray &y, FieldArray &z, std::size_t n)
static MPI_Comm getComm()
Definition IpplInfo.h:152
static int getNodes()
Definition IpplInfo.cpp:689
static int myNode()
Definition IpplInfo.cpp:710