OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
FM3DH5Block.cpp
Go to the documentation of this file.
1 #include "Fields/FM3DH5Block.h"
2 #include "Fields/Fieldmap.hpp"
3 #include "H5hut.h"
4 #include "Physics/Physics.h"
5 
6 #include <fstream>
7 #include <ios>
8 
9 #include <assert.h>
10 
11 using namespace std;
12 using Physics::mu_0;
13 
14 FM3DH5Block::FM3DH5Block(std::string aFilename):Fieldmap(aFilename) {
15  h5_err_t h5err;
16 #if defined (NDEBUG)
17  (void)h5err;
18 #endif
19  h5_size_t grid_rank;
20  h5_size_t grid_dims[3];
21  h5_size_t field_dims;
22  char name[20];
23  h5_size_t len_name = 20;
24  h5_int64_t ftype;
25 
27 
28  h5_prop_t props = H5CreateFileProp ();
29  MPI_Comm comm = Ippl::getComm();
30  h5err = H5SetPropFileMPIOCollective (props, &comm);
31  assert (h5err != H5_ERR);
32  h5_file_t file = H5OpenFile (aFilename.c_str(), H5_O_RDONLY, props);
33  assert (file != (h5_file_t)H5_ERR);
34  H5CloseProp (props);
35 
36  h5_int64_t last_step = H5GetNumSteps(file) - 1;
37  assert (last_step >= 0);
38  h5err = H5SetStep(file, last_step);
39  assert (h5err != H5_ERR);
40 
41  h5_int64_t num_fields = H5BlockGetNumFields(file);
42  assert (num_fields != H5_ERR);
43 
44  for(h5_ssize_t i = 0; i < num_fields; ++ i) {
45  h5err = H5BlockGetFieldInfo(file, (h5_size_t)i, name, len_name, &grid_rank, grid_dims, &field_dims, &ftype);
46  assert (h5err != H5_ERR);
47  if(strcmp(name, "Efield") == 0) {
48  num_gridpx_m = grid_dims[0];
49  num_gridpy_m = grid_dims[1];
50  num_gridpz_m = grid_dims[2];
51  }
52  }
53  h5err = H5Block3dGetFieldSpacing(file, "Efield", &hx_m, &hy_m, &hz_m);
54  assert (h5err != H5_ERR);
55  h5err = H5Block3dGetFieldOrigin(file, "Efield", &xbegin_m, &ybegin_m, &zbegin_m);
56  assert (h5err != H5_ERR);
57  xend_m = xbegin_m + (num_gridpx_m - 1) * hx_m;
58  yend_m = ybegin_m + (num_gridpy_m - 1) * hy_m;
59  zend_m = zbegin_m + (num_gridpz_m - 1) * hz_m;
60 
61  // xcentral_idx_m = static_cast<int>(fabs(xbegin_m) / hx_m);
62  // ycentral_idx_m = static_cast<int>(fabs(ybegin_m) / hy_m);
63 
64 
65  h5err = H5ReadFileAttribFloat64(file, "Resonance Frequency(Hz)", &frequency_m);
66  assert (h5err != H5_ERR);
68 
69  h5err = H5CloseFile(file);
70  assert (h5err != H5_ERR);
71 }
72 
73 
75  freeMap();
76 }
77 
79  if (!FieldstrengthEz_m.empty()) {
80  return;
81  }
82  h5_err_t h5err;
83 #if defined (NDEBUG)
84  (void)h5err;
85 #endif
86  h5_prop_t props = H5CreateFileProp ();
87  MPI_Comm comm = Ippl::getComm();
88  h5err = H5SetPropFileMPIOCollective (props, &comm);
89  assert (h5err != H5_ERR);
90  h5_file_t file = H5OpenFile (Filename_m.c_str(), H5_O_RDONLY, props);
91  assert (file != (h5_file_t)H5_ERR);
92  H5CloseProp (props);
93 
94  long field_size = 0;
95 
96  h5_int64_t last_step = H5GetNumSteps(file) - 1;
97  h5err = H5SetStep(file, last_step);
98  assert (h5err != H5_ERR);
99 
100  field_size = (num_gridpx_m * num_gridpy_m * num_gridpz_m);
101  FieldstrengthEx_m.resize(field_size);
102  FieldstrengthEy_m.resize(field_size);
103  FieldstrengthEz_m.resize(field_size);
104  FieldstrengthHx_m.resize(field_size);
105  FieldstrengthHy_m.resize(field_size);
106  FieldstrengthHz_m.resize(field_size);
107  h5err = H5Block3dSetView(file,
108  0, num_gridpx_m - 1,
109  0, num_gridpy_m - 1,
110  0, num_gridpz_m - 1);
111  assert (h5err != H5_ERR);
112  h5err = H5Block3dReadVector3dFieldFloat64(
113  file,
114  "Efield",
115  &(FieldstrengthEx_m[0]),
116  &(FieldstrengthEy_m[0]),
117  &(FieldstrengthEz_m[0]));
118  assert (h5err != H5_ERR);
119  h5err = H5Block3dReadVector3dFieldFloat64(
120  file,
121  "Hfield",
122  &(FieldstrengthHx_m[0]),
123  &(FieldstrengthHy_m[0]),
124  &(FieldstrengthHz_m[0]));
125  assert (h5err != H5_ERR);
126 
127  h5err = H5CloseFile(file);
128  assert (h5err != H5_ERR);
129 
130  INFOMSG(level3 << typeset_msg("fieldmap '" + Filename_m + "' read", "info") << endl);
131 }
132 
134  if(!FieldstrengthEz_m.empty()) {
135  FieldstrengthEx_m.clear();
136  FieldstrengthEy_m.clear();
137  FieldstrengthEz_m.clear();
138  FieldstrengthHx_m.clear();
139  FieldstrengthHy_m.clear();
140  FieldstrengthHz_m.clear();
141 
142  INFOMSG(level3 << typeset_msg("freed fieldmap '" + Filename_m + "'", "info") << endl);
143  }
144 }
145 
147  const int index_x = static_cast<int>(floor((R(0) - xbegin_m) / hx_m));
148  const double lever_x = (R(0) - xbegin_m) / hx_m - index_x;
149 
150  const int index_y = static_cast<int>(floor((R(1) - ybegin_m) / hy_m));
151  const double lever_y = (R(1) - ybegin_m) / hy_m - index_y;
152 
153  const int index_z = (int)floor((R(2) - zbegin_m) / hz_m);
154  const double lever_z = (R(2) - zbegin_m) / hz_m - index_z;
155 
156  if((index_z < 0) || (index_z + 2 > num_gridpz_m)) {
157  return false;
158  }
159 
160  if(index_x < 0) {
161  return false;
162  }
163  if(index_x + 2 > num_gridpx_m) {
164  return false;
165  }
166 
167  if(index_y < 0) {
168  return false;
169  }
170  if(index_y + 2 > num_gridpy_m) {
171  return false;
172  }
173 
174  const long index1 = index_x + (index_y + index_z * num_gridpy_m) * num_gridpx_m;
175 
176 
177 
178  E(0) += (1.0 - lever_x) * (1.0 - lever_y) * (1.0 - lever_z) * FieldstrengthEx_m[index1]
179  + lever_x * (1.0 - lever_y) * (1.0 - lever_z) * FieldstrengthEx_m[index1 + 1]
180  + (1.0 - lever_x) * lever_y * (1.0 - lever_z) * FieldstrengthEx_m[index1 + num_gridpx_m]
181  + lever_x * lever_y * (1.0 - lever_z) * FieldstrengthEx_m[index1 + num_gridpx_m + 1]
182  + (1.0 - lever_x) * (1.0 - lever_y) * lever_z * FieldstrengthEx_m[index1 + num_gridpx_m * num_gridpy_m]
183  + lever_x * (1.0 - lever_y) * lever_z * FieldstrengthEx_m[index1 + num_gridpx_m * num_gridpy_m + 1]
184  + (1.0 - lever_x) * lever_y * lever_z * FieldstrengthEx_m[index1 + num_gridpx_m * num_gridpy_m + num_gridpx_m]
185  + lever_x * lever_y * lever_z * FieldstrengthEx_m[index1 + num_gridpx_m * num_gridpy_m + num_gridpx_m + 1];
186  // msg<<R(0)<<", "<<R(1)<<", "<<R(2)<<", "<<index1<<", "<<index_x<<", "<<index_y<<", "<<index_z<<", "<<FieldstrengthEx_m[index1]<<", "<<FieldstrengthEy_m[index1]<<", "<<FieldstrengthEz_m[index1]<< endl;
187 
188  E(1) += (1.0 - lever_x) * (1.0 - lever_y) * (1.0 - lever_z) * FieldstrengthEy_m[index1]
189  + lever_x * (1.0 - lever_y) * (1.0 - lever_z) * FieldstrengthEy_m[index1 + 1]
190  + (1.0 - lever_x) * lever_y * (1.0 - lever_z) * FieldstrengthEy_m[index1 + num_gridpx_m]
191  + lever_x * lever_y * (1.0 - lever_z) * FieldstrengthEy_m[index1 + num_gridpx_m + 1]
192  + (1.0 - lever_x) * (1.0 - lever_y) * lever_z * FieldstrengthEy_m[index1 + num_gridpx_m * num_gridpy_m]
193  + lever_x * (1.0 - lever_y) * lever_z * FieldstrengthEy_m[index1 + num_gridpx_m * num_gridpy_m + 1]
194  + (1.0 - lever_x) * lever_y * lever_z * FieldstrengthEy_m[index1 + num_gridpx_m * num_gridpy_m + num_gridpx_m]
195  + lever_x * lever_y * lever_z * FieldstrengthEy_m[index1 + num_gridpx_m * num_gridpy_m + num_gridpx_m + 1];
196 
197  E(2) += (1.0 - lever_x) * (1.0 - lever_y) * (1.0 - lever_z) * FieldstrengthEz_m[index1]
198  + lever_x * (1.0 - lever_y) * (1.0 - lever_z) * FieldstrengthEz_m[index1 + 1]
199  + (1.0 - lever_x) * lever_y * (1.0 - lever_z) * FieldstrengthEz_m[index1 + num_gridpx_m]
200  + lever_x * lever_y * (1.0 - lever_z) * FieldstrengthEz_m[index1 + num_gridpx_m + 1]
201  + (1.0 - lever_x) * (1.0 - lever_y) * lever_z * FieldstrengthEz_m[index1 + num_gridpx_m * num_gridpy_m]
202  + lever_x * (1.0 - lever_y) * lever_z * FieldstrengthEz_m[index1 + num_gridpx_m * num_gridpy_m + 1]
203  + (1.0 - lever_x) * lever_y * lever_z * FieldstrengthEz_m[index1 + num_gridpx_m * num_gridpy_m + num_gridpx_m]
204  + lever_x * lever_y * lever_z * FieldstrengthEz_m[index1 + num_gridpx_m * num_gridpy_m + num_gridpx_m + 1];
205 
206  B(0) += ((1.0 - lever_x) * (1.0 - lever_y) * (1.0 - lever_z) * FieldstrengthHx_m[index1]
207  + lever_x * (1.0 - lever_y) * (1.0 - lever_z) * FieldstrengthHx_m[index1 + 1]
208  + (1.0 - lever_x) * lever_y * (1.0 - lever_z) * FieldstrengthHx_m[index1 + num_gridpx_m]
209  + lever_x * lever_y * (1.0 - lever_z) * FieldstrengthHx_m[index1 + num_gridpx_m + 1]
210  + (1.0 - lever_x) * (1.0 - lever_y) * lever_z * FieldstrengthHx_m[index1 + num_gridpx_m * num_gridpy_m]
211  + lever_x * (1.0 - lever_y) * lever_z * FieldstrengthHx_m[index1 + num_gridpx_m * num_gridpy_m + 1]
212  + (1.0 - lever_x) * lever_y * lever_z * FieldstrengthHx_m[index1 + num_gridpx_m * num_gridpy_m + num_gridpx_m]
213  + lever_x * lever_y * lever_z * FieldstrengthHx_m[index1 + num_gridpx_m * num_gridpy_m + num_gridpx_m + 1]);
214 
215  B(1) += ((1.0 - lever_x) * (1.0 - lever_y) * (1.0 - lever_z) * FieldstrengthHy_m[index1]
216  + lever_x * (1.0 - lever_y) * (1.0 - lever_z) * FieldstrengthHy_m[index1 + 1]
217  + (1.0 - lever_x) * lever_y * (1.0 - lever_z) * FieldstrengthHy_m[index1 + num_gridpx_m]
218  + lever_x * lever_y * (1.0 - lever_z) * FieldstrengthHy_m[index1 + num_gridpx_m + 1]
219  + (1.0 - lever_x) * (1.0 - lever_y) * lever_z * FieldstrengthHy_m[index1 + num_gridpx_m * num_gridpy_m]
220  + lever_x * (1.0 - lever_y) * lever_z * FieldstrengthHy_m[index1 + num_gridpx_m * num_gridpy_m + 1]
221  + (1.0 - lever_x) * lever_y * lever_z * FieldstrengthHy_m[index1 + num_gridpx_m * num_gridpy_m + num_gridpx_m]
222  + lever_x * lever_y * lever_z * FieldstrengthHy_m[index1 + num_gridpx_m * num_gridpy_m + num_gridpx_m + 1]);
223 
224  B(2) += ((1.0 - lever_x) * (1.0 - lever_y) * (1.0 - lever_z) * FieldstrengthHz_m[index1]
225  + lever_x * (1.0 - lever_y) * (1.0 - lever_z) * FieldstrengthHz_m[index1 + 1]
226  + (1.0 - lever_x) * lever_y * (1.0 - lever_z) * FieldstrengthHz_m[index1 + num_gridpx_m]
227  + lever_x * lever_y * (1.0 - lever_z) * FieldstrengthHz_m[index1 + num_gridpx_m + 1]
228  + (1.0 - lever_x) * (1.0 - lever_y) * lever_z * FieldstrengthHz_m[index1 + num_gridpx_m * num_gridpy_m]
229  + lever_x * (1.0 - lever_y) * lever_z * FieldstrengthHz_m[index1 + num_gridpx_m * num_gridpy_m + 1]
230  + (1.0 - lever_x) * lever_y * lever_z * FieldstrengthHz_m[index1 + num_gridpx_m * num_gridpy_m + num_gridpx_m]
231  + lever_x * lever_y * lever_z * FieldstrengthHz_m[index1 + num_gridpx_m * num_gridpy_m + num_gridpx_m + 1]);
232 
233 
234  return false;
235 }
236 
238  return false;
239 }
240 
241 void FM3DH5Block::getFieldDimensions(double &zBegin, double &zEnd, double &rBegin, double &rEnd) const {
242  zBegin = zbegin_m;
243  zEnd = zend_m;
244  rBegin = xbegin_m;
245  rEnd = xend_m;
246 }
247 void FM3DH5Block::getFieldDimensions(double &xIni, double &xFinal, double &yIni, double &yFinal, double &zIni, double &zFinal) const {
248  xIni = xbegin_m;
249  xFinal = xend_m;
250  yIni = ybegin_m;
251  yFinal = yend_m;
252  zIni = zbegin_m;
253  zFinal = zend_m;
254  //INFOMSG("xIni " << xIni << "xFinal " << xFinal << endl);
255 }
257 
259  (*msg) << Filename_m << " (3D dynamic) "
260  << " xini= " << xbegin_m << " xfinal= " << xend_m
261  << " yini= " << ybegin_m << " yfinal= " << yend_m
262  << " zini= " << zbegin_m << " zfinal= " << zend_m << " [mm] " << endl;
263  (*msg) << " hx= " << hx_m <<" hy= " << hy_m <<" hz= " << hz_m << " [mm] " <<endl;
264 }
265 
267  return frequency_m;
268 }
269 
270 void FM3DH5Block::setFrequency(double freq) {
271  frequency_m = freq;
272 }
273 
274 void FM3DH5Block::getOnaxisEz(vector<pair<double, double> > & F) {
275  double Ez_max = 0.0, dz = (zend_m - zbegin_m) / (num_gridpz_m - 1);
276  const int index_x = -static_cast<int>(floor(xbegin_m / hx_m));
277  const double lever_x = -xbegin_m / hx_m - index_x;
278 
279  const int index_y = -static_cast<int>(floor(ybegin_m / hy_m));
280  const double lever_y = -ybegin_m / hy_m - index_y;
281 
282  long index1 = index_x + index_y * num_gridpx_m;
283 
284  F.resize(num_gridpz_m);
285 
286  for(int i = 0; i < num_gridpz_m; ++ i) {
287  F[i].first = dz * i;
288  F[i].second = (1.0 - lever_x) * (1.0 - lever_y) * FieldstrengthEz_m[index1 + num_gridpx_m * num_gridpy_m]
289  + lever_x * (1.0 - lever_y) * FieldstrengthEz_m[index1 + num_gridpx_m * num_gridpy_m + 1]
290  + (1.0 - lever_x) * lever_y * FieldstrengthEz_m[index1 + num_gridpx_m * num_gridpy_m + num_gridpx_m]
291  + lever_x * lever_y * FieldstrengthEz_m[index1 + num_gridpx_m * num_gridpy_m + num_gridpx_m + 1];
292 
293 
294  if(fabs(F[i].second) > Ez_max) {
295  Ez_max = fabs(F[i].second);
296  }
297  index1 += num_gridpy_m * num_gridpx_m;
298  }
299 
300  for(int i = 0; i < num_gridpz_m; ++ i) {
301  F[i].second /= Ez_max;
302  }
303 }
304 
305 // vi: set et ts=4 sw=4 sts=4:
306 // Local Variables:
307 // mode:c
308 // c-basic-offset: 4
309 // indent-tabs-mode:nil
310 // End:
std::vector< h5_float64_t > FieldstrengthEx_m
Definition: FM3DH5Block.h:31
virtual void freeMap()
h5_int64_t num_gridpy_m
Definition: FM3DH5Block.h:54
h5_float64_t xend_m
Definition: FM3DH5Block.h:40
h5_float64_t frequency_m
Definition: FM3DH5Block.h:37
virtual void setFrequency(double freq)
DiffDirection
Definition: Fieldmap.h:54
PETE_TUTree< FnFabs, typename T::PETE_Expr_t > fabs(const PETE_Expr< T > &l)
Definition: PETE.h:815
h5_float64_t xbegin_m
Definition: FM3DH5Block.h:39
constexpr double two_pi
The value of .
Definition: Physics.h:34
static std::string typeset_msg(const std::string &msg, const std::string &title)
Definition: Fieldmap.cpp:647
virtual bool getFieldstrength(const Vector_t &R, Vector_t &E, Vector_t &B) const
h5_float64_t ybegin_m
Definition: FM3DH5Block.h:43
h5_float64_t zbegin_m
Definition: FM3DH5Block.h:47
std::vector< h5_float64_t > FieldstrengthHy_m
Definition: FM3DH5Block.h:35
FM3DH5Block(std::string aFilename)
Definition: FM3DH5Block.cpp:14
constexpr double mu_0
The permeability of vacuum in Vs/Am.
Definition: Physics.h:55
h5_float64_t hx_m
Definition: FM3DH5Block.h:50
h5_int64_t num_gridpz_m
Definition: FM3DH5Block.h:55
#define INFOMSG(msg)
Definition: IpplInfo.h:397
virtual bool getFieldDerivative(const Vector_t &R, Vector_t &E, Vector_t &B, const DiffDirection &dir) const
virtual void getInfo(Inform *msg)
static MPI_Comm getComm()
Definition: IpplInfo.h:178
std::vector< h5_float64_t > FieldstrengthEz_m
Definition: FM3DH5Block.h:30
virtual void getFieldDimensions(double &zBegin, double &zEnd, double &rBegin, double &rEnd) const
std::vector< h5_float64_t > FieldstrengthEy_m
Definition: FM3DH5Block.h:32
std::vector< h5_float64_t > FieldstrengthHz_m
Definition: FM3DH5Block.h:33
virtual void readMap()
Definition: FM3DH5Block.cpp:78
const std::string name
std::vector< h5_float64_t > FieldstrengthHx_m
Definition: FM3DH5Block.h:34
virtual void getOnaxisEz(std::vector< std::pair< double, double > > &F)
std::string Filename_m
Definition: Fieldmap.h:110
virtual double getFrequency() const
MapType Type
Definition: Fieldmap.h:107
virtual void swap()
Inform & level3(Inform &inf)
Definition: Inform.cpp:47
h5_int64_t num_gridpx_m
Definition: FM3DH5Block.h:53
h5_float64_t hy_m
Definition: FM3DH5Block.h:51
h5_float64_t zend_m
Definition: FM3DH5Block.h:48
h5_float64_t yend_m
Definition: FM3DH5Block.h:44
Definition: Inform.h:41
h5_float64_t hz_m
Definition: FM3DH5Block.h:52
PETE_TUTree< FnFloor, typename T::PETE_Expr_t > floor(const PETE_Expr< T > &l)
Definition: PETE.h:816
Inform & endl(Inform &inf)
Definition: Inform.cpp:42