OPALX (Object Oriented Parallel Accelerator Library for Exascal) MINIorX
OPALX
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//
18
20#include "Fields/Fieldmap.hpp"
21#include "Physics/Physics.h"
23
24void FM3DH5BlockBase::openFileMPIOCollective(const std::string aFilename) {
25 h5_prop_t props = H5CreateFileProp();
26 MPI_Comm comm = ippl::Comm->getCommunicator();
27 if (H5SetPropFileMPIOCollective(props, &comm) == H5_ERR) {
29 "FM3DH5BlockBase::openFileMPIOCollective () ", "Cannot set MPIO collective!");
30 }
31 file_m = H5OpenFile(aFilename.c_str(), H5_O_RDONLY, props);
32 if (file_m == (h5_file_t)H5_ERR) {
34 "FM3DH5BlockBase::openFileMPIOCollective () ", "Cannot open file '" + aFilename + "'!");
35 }
36 H5CloseProp(props);
37}
38
40 long long num_steps = H5GetNumSteps(file_m);
41 if (num_steps <= 0) {
42 if (num_steps == 0) {
44 "FM3DH5BlockBase::getNumSteps () ",
45 "Number of time-steps in file '" + Filename_m + "' is zero!");
46 } else {
48 "FM3DH5BlockBase::getNumSteps () ",
49 "Query number of time-steps in file '" + Filename_m + "' failed!");
50 }
51 }
52 return num_steps;
53}
54
55void FM3DH5BlockBase::setStep(long long step) {
56 if (H5SetStep(file_m, step) == H5_ERR) {
58 "FM3DH5BlockBase::setStep () ",
59 "Cannot set time-step to " + std::to_string(step) + " in file '" + Filename_m + "'!");
60 }
61}
62
63void FM3DH5BlockBase::getFieldInfo(const char* name) {
64 long long last_step = getNumSteps() - 1;
65 setStep(last_step);
66 h5_size_t grid_rank;
67 h5_size_t grid_dims[3];
68 h5_size_t field_dims;
69 h5_int64_t ftype;
70 if (H5BlockGetFieldInfoByName(file_m, name, &grid_rank, grid_dims, &field_dims, &ftype)
71 == H5_ERR) {
73 "FM3DH5BlockBase::GetFieldInfo () ", "Query of field info for " + std::string(name)
74 + " in time-step " + std::to_string(last_step)
75 + " in file '" + Filename_m + "' failed!");
76 }
77 num_gridpx_m = grid_dims[0];
78 num_gridpy_m = grid_dims[1];
79 num_gridpz_m = grid_dims[2];
80
81 if (H5Block3dGetFieldSpacing(file_m, "Efield", &hx_m, &hy_m, &hz_m) == H5_ERR) {
83 "FM3DH5BlockBase::GetFieldInfo () ",
84 "Query of field spacing"
85 " in time-step "
86 + std::to_string(last_step) + " in file '" + Filename_m + "' failed!");
87 }
88
89 if (H5Block3dGetFieldOrigin(file_m, "Efield", &xbegin_m, &ybegin_m, &zbegin_m) == H5_ERR) {
91 "FM3DH5BlockBase::GetFieldInfo () ",
92 "Query of field origin"
93 " in time-step "
94 + std::to_string(last_step) + " in file '" + Filename_m + "' failed!");
95 }
96 xend_m = xbegin_m + (num_gridpx_m - 1) * hx_m;
97 yend_m = ybegin_m + (num_gridpy_m - 1) * hy_m;
98 zend_m = zbegin_m + (num_gridpz_m - 1) * hz_m;
99}
100
102 if (H5ReadFileAttribFloat64(file_m, "Resonance Frequency(Hz)", &frequency_m) == H5_ERR) {
104 "FM3DH5BlockBase::GetResonanceFrequency () ",
105 "Cannot read file attribute 'Resonance Frequency(Hz)'"
106 " in file '"
107 + Filename_m + "'!");
108 }
110}
111
112void FM3DH5BlockBase::readField(const char* name, double* x, double* y, double* z) {
113 if (H5Block3dSetView(file_m, 0, num_gridpx_m - 1, 0, num_gridpy_m - 1, 0, num_gridpz_m - 1)
114 == H5_ERR) {
116 "FM3DH5BlockBase::ReadField () ",
117 "Cannot set view "
118 "0, "
119 + std::to_string(num_gridpx_m) + "0, " + std::to_string(num_gridpy_m) + "0, "
120 + std::to_string(num_gridpz_m) + " in file '" + Filename_m + "'!");
121 }
122 if (H5Block3dReadVector3dFieldFloat64(file_m, name, x, y, z) == H5_ERR) {
124 "FM3DH5BlockBase::ReadField () ",
125 "Cannot read field " + std::string(name) + " in file '" + Filename_m + "'!");
126 }
127}
128
130 if (H5CloseFile(file_m) == H5_ERR) {
132 "FM3DH5BlockBase::closeFile () ", "Error closing file '" + Filename_m + "'!");
133 }
134}
135
137 const std::vector<double>& data, const IndexTriplet& idx, unsigned short corner) const {
138 unsigned short switchX = ((corner & HX) >> 2);
139 unsigned short switchY = ((corner & HY) >> 1);
140 unsigned short switchZ = (corner & HZ);
141 double factorX = 0.5 + (1 - 2 * switchX) * (0.5 - idx.weight(0));
142 double factorY = 0.5 + (1 - 2 * switchY) * (0.5 - idx.weight(1));
143 double factorZ = 0.5 + (1 - 2 * switchZ) * (0.5 - idx.weight(2));
144
145 unsigned long i = idx.i + switchX, j = idx.j + switchY, k = idx.k + switchZ;
146
147 return factorX * factorY * factorZ * data[getIndex(i, j, k)];
148}
149
151 const std::vector<double>& field_strength_x, const std::vector<double>& field_strength_y,
152 const std::vector<double>& field_strength_z, const Vector_t<double, 3>& X) const {
153 IndexTriplet idx = getIndex(X);
154 Vector_t<double, 3> result{0.0};
155
156 result[0] =
157 (getWeightedData(field_strength_x, idx, LX | LY | LZ)
158 + getWeightedData(field_strength_x, idx, LX | LY | HZ)
159 + getWeightedData(field_strength_x, idx, LX | HY | LZ)
160 + getWeightedData(field_strength_x, idx, LX | HY | HZ)
161 + getWeightedData(field_strength_x, idx, HX | LY | LZ)
162 + getWeightedData(field_strength_x, idx, HX | LY | HZ)
163 + getWeightedData(field_strength_x, idx, HX | HY | LZ)
164 + getWeightedData(field_strength_x, idx, HX | HY | HZ));
165
166 result[1] =
167 (getWeightedData(field_strength_y, idx, LX | LY | LZ)
168 + getWeightedData(field_strength_y, idx, LX | LY | HZ)
169 + getWeightedData(field_strength_y, idx, LX | HY | LZ)
170 + getWeightedData(field_strength_y, idx, LX | HY | HZ)
171 + getWeightedData(field_strength_y, idx, HX | LY | LZ)
172 + getWeightedData(field_strength_y, idx, HX | LY | HZ)
173 + getWeightedData(field_strength_y, idx, HX | HY | LZ)
174 + getWeightedData(field_strength_y, idx, HX | HY | HZ));
175
176 result[2] =
177 (getWeightedData(field_strength_z, idx, LX | LY | LZ)
178 + getWeightedData(field_strength_z, idx, LX | LY | HZ)
179 + getWeightedData(field_strength_z, idx, LX | HY | LZ)
180 + getWeightedData(field_strength_z, idx, LX | HY | HZ)
181 + getWeightedData(field_strength_z, idx, HX | LY | LZ)
182 + getWeightedData(field_strength_z, idx, HX | LY | HZ)
183 + getWeightedData(field_strength_z, idx, HX | HY | LZ)
184 + getWeightedData(field_strength_z, idx, HX | HY | HZ));
185
186 return result;
187}
188
189void FM3DH5BlockBase::getInfo(Inform* msg) {
190 (*msg) << Filename_m << " (3D dynamic) "
191 << " xini= " << xbegin_m << " xfinal= " << xend_m << " yini= " << ybegin_m
192 << " yfinal= " << yend_m << " zini= " << zbegin_m << " zfinal= " << zend_m << " [mm] "
193 << endl;
194 (*msg) << " hx= " << hx_m << " hy= " << hy_m << " hz= " << hz_m << " [mm] " << endl;
195}
196
198 return frequency_m;
199}
200
202 frequency_m = freq;
203}
204
205void FM3DH5BlockBase::getOnaxisEz(std::vector<std::pair<double, double>>& F) {
206 F.resize(num_gridpz_m);
207
208 double Ez_max = 0.0;
209 const double dz = (zend_m - zbegin_m) / (num_gridpz_m - 1);
210 const int index_x = -static_cast<int>(std::floor(xbegin_m / hx_m));
211 const double lever_x = -xbegin_m / hx_m - index_x;
212
213 const int index_y = -static_cast<int>(std::floor(ybegin_m / hy_m));
214 const double lever_y = -ybegin_m / hy_m - index_y;
215 long idx = index_x + index_y * num_gridpx_m + num_gridpx_m * num_gridpy_m;
216 for (int i = 0; i < num_gridpz_m; i++, idx += num_gridpy_m * num_gridpx_m) {
217 F[i].first = dz * i;
218 F[i].second = (1.0 - lever_x) * (1.0 - lever_y) * FieldstrengthEz_m[idx]
219 + lever_x * (1.0 - lever_y) * FieldstrengthEz_m[idx + 1]
220 + (1.0 - lever_x) * lever_y * FieldstrengthEz_m[idx + num_gridpx_m]
221 + lever_x * lever_y * FieldstrengthEz_m[idx + num_gridpx_m + 1];
222
223 if (std::abs(F[i].second) > Ez_max) {
224 Ez_max = std::abs(F[i].second);
225 }
226 F[i].second /= Ez_max;
227 }
228}
ippl::Vector< T, Dim > Vector_t
constexpr double two_pi
The value of.
Definition Physics.h:33
std::string Filename_m
Definition Fieldmap.h:120
virtual void setFrequency(double freq)
void getFieldInfo(const char *)
long long getNumSteps(void)
void openFileMPIOCollective(const std::string aFilename)
unsigned long getIndex(unsigned int i, unsigned int j, unsigned int k) const
std::vector< double > FieldstrengthEz_m
virtual double getFrequency() const
void setStep(const long long)
virtual void getOnaxisEz(std::vector< std::pair< double, double > > &F)
double getWeightedData(const std::vector< double > &data, const IndexTriplet &idx, unsigned short corner) const
void getResonanceFrequency(void)
void readField(const char *name, double *x, double *y, double *z)
virtual void getInfo(Inform *msg)
Vector_t< double, 3 > interpolateTrilinearly(const std::vector< double > &, const std::vector< double > &, const std::vector< double > &, const Vector_t< double, 3 > &X) const
Vector_t< double, 3 > weight