OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
FM3DH5Block_nonscale.cpp
Go to the documentation of this file.
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 
15  Fieldmap(aFilename) {
16  h5_err_t h5err;
17 #if defined (NDEBUG)
18  (void)h5err;
19 #endif
20  h5_size_t grid_rank;
21  h5_size_t grid_dims[3];
22  h5_size_t field_dims;
23  char name[20];
24  h5_size_t len_name = 20;
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  h5_int64_t ftype;
46  h5err = H5BlockGetFieldInfo(file, (h5_size_t)i, name, len_name, &grid_rank, grid_dims, &field_dims, &ftype);
47  assert (h5err != H5_ERR);
48  if(strcmp(name, "Efield") == 0) {
49  num_gridpx_m = grid_dims[0];
50  num_gridpy_m = grid_dims[1];
51  num_gridpz_m = grid_dims[2];
52  }
53  }
54  h5err = H5Block3dGetFieldSpacing(file, "Efield", &hx_m, &hy_m, &hz_m);
55  assert (h5err != H5_ERR);
56 
57  h5err = H5Block3dGetFieldOrigin(file, "Efield", &xbegin_m, &ybegin_m, &zbegin_m);
58  assert (h5err != H5_ERR);
59 
60  xend_m = xbegin_m + (num_gridpx_m - 1) * hx_m;
61  yend_m = ybegin_m + (num_gridpy_m - 1) * hy_m;
62  zend_m = zbegin_m + (num_gridpz_m - 1) * hz_m;
63 
64  // xcentral_idx_m = static_cast<int>(fabs(xbegin_m) / hx_m);
65  // ycentral_idx_m = static_cast<int>(fabs(ybegin_m) / hy_m);
66 
67 
68  h5err = H5ReadFileAttribFloat64(file, "Resonance Frequency(Hz)", &frequency_m);
69  assert (h5err != H5_ERR);
70 
72 
73  h5err = H5CloseFile(file);
74  assert (h5err != H5_ERR);
75 }
76 
77 
79  freeMap();
80 }
81 
83  if (!FieldstrengthEz_m.empty()) {
84  return;
85  }
86  h5_int64_t h5err;
87 #if defined (NDEBUG)
88  (void)h5err;
89 #endif
90  h5_prop_t props = H5CreateFileProp ();
91  MPI_Comm comm = Ippl::getComm();
92  h5err = H5SetPropFileMPIOCollective (props, &comm);
93  assert (h5err != H5_ERR);
94  h5_file_t file = H5OpenFile (Filename_m.c_str(), H5_O_RDONLY, props);
95  assert (file != (h5_file_t)H5_ERR);
96  H5CloseProp (props);
97 
98  long field_size = 0;
99  int Nnodes = Ippl::getNodes();//min(20, Ippl::getNodes());
100  int Nz_avrg = static_cast<int>(floor(0.5 + num_gridpz_m / Nnodes));
101  int Nz_diff = Nz_avrg * Nnodes - num_gridpz_m;
102  int signNz = Nz_diff > 0 ? 1 : -1;
103  int *Nz_read_start = new int[Ippl::getNodes() + 1];
104  int *Nz_read_length = new int[Ippl::getNodes()];
105  int N_read_start;
106  int start = 0;
107  // int rbuf_size;
108 
109  h5_int64_t last_step = H5GetNumSteps(file) - 1;
110  h5err = H5SetStep(file, last_step);
111  assert (h5err != H5_ERR);
112 
113  for(int i = 0; i < abs(Nz_diff); ++ i) {
114  Nz_read_length[i] = Nz_avrg - signNz;
115  Nz_read_start[i] = start;
116  start += Nz_read_length[i];
117  }
118  for(int i = abs(Nz_diff); i < Nnodes; ++ i) {
119  Nz_read_length[i] = Nz_avrg;
120  Nz_read_start[i] = start;
121  start += Nz_read_length[i];
122  }
123  for(int i = Nnodes; i < Ippl::getNodes(); ++ i) {
124  Nz_read_length[i] = 0;
125  Nz_read_start[i] = start;
126  }
127  Nz_read_start[Ippl::getNodes()] = start;
128 
129  N_read_start = Nz_read_start[Ippl::myNode()] * num_gridpx_m * num_gridpy_m;
130 
131  // rbuf_size = max(Nz_avrg, Nz_avrg - signNz);
132  // std::unique_ptr<double> rbuf(new double[Ippl::getNodes() * rbuf_size]);
133 
134  h5err = H5Block3dSetView(file,
135  0, num_gridpx_m - 1,
136  0, num_gridpy_m - 1,
137  Nz_read_start[Ippl::myNode()], Nz_read_start[Ippl::myNode() + 1] - 1);
138  assert (h5err != H5_ERR);
139 
140  field_size = (num_gridpx_m * num_gridpy_m * num_gridpz_m);
141  FieldstrengthEx_m.resize(field_size);
142  FieldstrengthEy_m.resize(field_size);
143  FieldstrengthEz_m.resize(field_size);
144  FieldstrengthHx_m.resize(field_size);
145  FieldstrengthHy_m.resize(field_size);
146  FieldstrengthHz_m.resize(field_size);
147 
148  h5err = H5Block3dReadVector3dFieldFloat64 (
149  file, "Efield",
150  &(FieldstrengthEx_m[N_read_start]),
151  &(FieldstrengthEy_m[N_read_start]),
152  &(FieldstrengthEz_m[N_read_start]));
153  assert (h5err != H5_ERR);
154 
155  h5err = H5Block3dReadVector3dFieldFloat64 (
156  file, "Hfield",
157  &(FieldstrengthHx_m[N_read_start]),
158  &(FieldstrengthHy_m[N_read_start]),
159  &(FieldstrengthHz_m[N_read_start]));
160  assert (h5err != H5_ERR);
161 
162  for(int i = 0; i < Nnodes; ++ i) {
163  int N_read_start = Nz_read_start[i] * num_gridpx_m * num_gridpy_m;
164  int N_read_length = Nz_read_length[i] * num_gridpx_m * num_gridpy_m;
165  MPI_Bcast(&(FieldstrengthEx_m[N_read_start]), N_read_length, MPI_DOUBLE, i, Ippl::getComm());
166  MPI_Bcast(&(FieldstrengthEy_m[N_read_start]), N_read_length, MPI_DOUBLE, i, Ippl::getComm());
167  MPI_Bcast(&(FieldstrengthEz_m[N_read_start]), N_read_length, MPI_DOUBLE, i, Ippl::getComm());
168  MPI_Bcast(&(FieldstrengthHx_m[N_read_start]), N_read_length, MPI_DOUBLE, i, Ippl::getComm());
169  MPI_Bcast(&(FieldstrengthHy_m[N_read_start]), N_read_length, MPI_DOUBLE, i, Ippl::getComm());
170  MPI_Bcast(&(FieldstrengthHz_m[N_read_start]), N_read_length, MPI_DOUBLE, i, Ippl::getComm());
171  }
172 
173  h5err = H5CloseFile(file);
174  assert (h5err != H5_ERR);
175  delete[] Nz_read_start;
176  delete[] Nz_read_length;
177 
178  for(long i = 0; i < num_gridpx_m * num_gridpy_m * num_gridpz_m; i++) {
179  FieldstrengthEz_m[i] *= 1.0e6 ;
180  FieldstrengthEx_m[i] *= 1.0e6 ;
181  FieldstrengthEy_m[i] *= 1.0e6 ;
182  FieldstrengthHx_m[i] *= 1.0e6 * mu_0 ;
183  FieldstrengthHy_m[i] *= 1.0e6 * mu_0 ;
184  FieldstrengthHz_m[i] *= 1.0e6 * mu_0 ;
185  }
186  INFOMSG(level3 << typeset_msg("read in fieldmap '" + Filename_m + "'", "info") << "\n"
187  << endl);
188 }
189 
191  if(!FieldstrengthEz_m.empty()) {
192  FieldstrengthEx_m.clear();
193  FieldstrengthEy_m.clear();
194  FieldstrengthEz_m.clear();
195  FieldstrengthHx_m.clear();
196  FieldstrengthHy_m.clear();
197  FieldstrengthHz_m.clear();
198 
199  INFOMSG(level3 << typeset_msg("freed fieldmap '" + Filename_m + "'", "info") << "\n"
200  << endl);
201  }
202 }
203 
205  const int index_x = static_cast<int>(floor((R(0) - xbegin_m) / hx_m));
206  const double lever_x = (R(0) - xbegin_m) / hx_m - index_x;
207 
208  const int index_y = static_cast<int>(floor((R(1) - ybegin_m) / hy_m));
209  const double lever_y = (R(1) - ybegin_m) / hy_m - index_y;
210 
211  const int index_z = (int)floor((R(2) - zbegin_m) / hz_m);
212  const double lever_z = (R(2) - zbegin_m) / hz_m - index_z;
213 
214  if((index_z < 0) || (index_z + 2 > num_gridpz_m)) {
215  return false;
216  }
217 
218  if(index_x + 2 > num_gridpx_m || index_y + 2 > num_gridpy_m) {
219  return false;// do nothing just let E and B unchange in the element with this field map, we check if a particle outside domain in BoundaryGeometry;
220  }
221 
222  if(index_x < 0 || index_y < 0) {
223  //*gmsg<<"Field strength at R "<<R<<" out of field maps"<<endl;
224  return false;// if x or y < xmin or ymin, do nothing just let E and B unchange in the element with this field map, we check if a particle outside domain in BoundaryGeometry, not here. That's resonable if we have field mapped without covering entire boundary of an element.
225  }
226  const long index1 = index_x + (index_y + index_z * num_gridpy_m) * num_gridpx_m;
227 
228  E(0) += (1.0 - lever_x) * (1.0 - lever_y) * (1.0 - lever_z) * FieldstrengthEx_m[index1]
229  + lever_x * (1.0 - lever_y) * (1.0 - lever_z) * FieldstrengthEx_m[index1 + 1]
230  + (1.0 - lever_x) * lever_y * (1.0 - lever_z) * FieldstrengthEx_m[index1 + num_gridpx_m]
231  + lever_x * lever_y * (1.0 - lever_z) * FieldstrengthEx_m[index1 + num_gridpx_m + 1]
232  + (1.0 - lever_x) * (1.0 - lever_y) * lever_z * FieldstrengthEx_m[index1 + num_gridpx_m * num_gridpy_m]
233  + lever_x * (1.0 - lever_y) * lever_z * FieldstrengthEx_m[index1 + num_gridpx_m * num_gridpy_m + 1]
234  + (1.0 - lever_x) * lever_y * lever_z * FieldstrengthEx_m[index1 + num_gridpx_m * num_gridpy_m + num_gridpx_m]
235  + lever_x * lever_y * lever_z * FieldstrengthEx_m[index1 + num_gridpx_m * num_gridpy_m + num_gridpx_m + 1];
236 
237  E(1) += (1.0 - lever_x) * (1.0 - lever_y) * (1.0 - lever_z) * FieldstrengthEy_m[index1]
238  + lever_x * (1.0 - lever_y) * (1.0 - lever_z) * FieldstrengthEy_m[index1 + 1]
239  + (1.0 - lever_x) * lever_y * (1.0 - lever_z) * FieldstrengthEy_m[index1 + num_gridpx_m]
240  + lever_x * lever_y * (1.0 - lever_z) * FieldstrengthEy_m[index1 + num_gridpx_m + 1]
241  + (1.0 - lever_x) * (1.0 - lever_y) * lever_z * FieldstrengthEy_m[index1 + num_gridpx_m * num_gridpy_m]
242  + lever_x * (1.0 - lever_y) * lever_z * FieldstrengthEy_m[index1 + num_gridpx_m * num_gridpy_m + 1]
243  + (1.0 - lever_x) * lever_y * lever_z * FieldstrengthEy_m[index1 + num_gridpx_m * num_gridpy_m + num_gridpx_m]
244  + lever_x * lever_y * lever_z * FieldstrengthEy_m[index1 + num_gridpx_m * num_gridpy_m + num_gridpx_m + 1];
245 
246  E(2) += (1.0 - lever_x) * (1.0 - lever_y) * (1.0 - lever_z) * FieldstrengthEz_m[index1]
247  + lever_x * (1.0 - lever_y) * (1.0 - lever_z) * FieldstrengthEz_m[index1 + 1]
248  + (1.0 - lever_x) * lever_y * (1.0 - lever_z) * FieldstrengthEz_m[index1 + num_gridpx_m]
249  + lever_x * lever_y * (1.0 - lever_z) * FieldstrengthEz_m[index1 + num_gridpx_m + 1]
250  + (1.0 - lever_x) * (1.0 - lever_y) * lever_z * FieldstrengthEz_m[index1 + num_gridpx_m * num_gridpy_m]
251  + lever_x * (1.0 - lever_y) * lever_z * FieldstrengthEz_m[index1 + num_gridpx_m * num_gridpy_m + 1]
252  + (1.0 - lever_x) * lever_y * lever_z * FieldstrengthEz_m[index1 + num_gridpx_m * num_gridpy_m + num_gridpx_m]
253  + lever_x * lever_y * lever_z * FieldstrengthEz_m[index1 + num_gridpx_m * num_gridpy_m + num_gridpx_m + 1];
254 
255  B(0) += ((1.0 - lever_x) * (1.0 - lever_y) * (1.0 - lever_z) * FieldstrengthHx_m[index1]
256  + lever_x * (1.0 - lever_y) * (1.0 - lever_z) * FieldstrengthHx_m[index1 + 1]
257  + (1.0 - lever_x) * lever_y * (1.0 - lever_z) * FieldstrengthHx_m[index1 + num_gridpx_m]
258  + lever_x * lever_y * (1.0 - lever_z) * FieldstrengthHx_m[index1 + num_gridpx_m + 1]
259  + (1.0 - lever_x) * (1.0 - lever_y) * lever_z * FieldstrengthHx_m[index1 + num_gridpx_m * num_gridpy_m]
260  + lever_x * (1.0 - lever_y) * lever_z * FieldstrengthHx_m[index1 + num_gridpx_m * num_gridpy_m + 1]
261  + (1.0 - lever_x) * lever_y * lever_z * FieldstrengthHx_m[index1 + num_gridpx_m * num_gridpy_m + num_gridpx_m]
262  + lever_x * lever_y * lever_z * FieldstrengthHx_m[index1 + num_gridpx_m * num_gridpy_m + num_gridpx_m + 1]);
263 
264  B(1) += ((1.0 - lever_x) * (1.0 - lever_y) * (1.0 - lever_z) * FieldstrengthHy_m[index1]
265  + lever_x * (1.0 - lever_y) * (1.0 - lever_z) * FieldstrengthHy_m[index1 + 1]
266  + (1.0 - lever_x) * lever_y * (1.0 - lever_z) * FieldstrengthHy_m[index1 + num_gridpx_m]
267  + lever_x * lever_y * (1.0 - lever_z) * FieldstrengthHy_m[index1 + num_gridpx_m + 1]
268  + (1.0 - lever_x) * (1.0 - lever_y) * lever_z * FieldstrengthHy_m[index1 + num_gridpx_m * num_gridpy_m]
269  + lever_x * (1.0 - lever_y) * lever_z * FieldstrengthHy_m[index1 + num_gridpx_m * num_gridpy_m + 1]
270  + (1.0 - lever_x) * lever_y * lever_z * FieldstrengthHy_m[index1 + num_gridpx_m * num_gridpy_m + num_gridpx_m]
271  + lever_x * lever_y * lever_z * FieldstrengthHy_m[index1 + num_gridpx_m * num_gridpy_m + num_gridpx_m + 1]);
272 
273  B(2) += ((1.0 - lever_x) * (1.0 - lever_y) * (1.0 - lever_z) * FieldstrengthHz_m[index1]
274  + lever_x * (1.0 - lever_y) * (1.0 - lever_z) * FieldstrengthHz_m[index1 + 1]
275  + (1.0 - lever_x) * lever_y * (1.0 - lever_z) * FieldstrengthHz_m[index1 + num_gridpx_m]
276  + lever_x * lever_y * (1.0 - lever_z) * FieldstrengthHz_m[index1 + num_gridpx_m + 1]
277  + (1.0 - lever_x) * (1.0 - lever_y) * lever_z * FieldstrengthHz_m[index1 + num_gridpx_m * num_gridpy_m]
278  + lever_x * (1.0 - lever_y) * lever_z * FieldstrengthHz_m[index1 + num_gridpx_m * num_gridpy_m + 1]
279  + (1.0 - lever_x) * lever_y * lever_z * FieldstrengthHz_m[index1 + num_gridpx_m * num_gridpy_m + num_gridpx_m]
280  + lever_x * lever_y * lever_z * FieldstrengthHz_m[index1 + num_gridpx_m * num_gridpy_m + num_gridpx_m + 1]);
281 
282 
283  return false;
284 }
285 
287  return false;
288 }
289 
290 void FM3DH5Block_nonscale::getFieldDimensions(double &zBegin, double &zEnd, double &rBegin, double &rEnd) const {
291  zBegin = zbegin_m;
292  zEnd = zend_m;
293  rBegin = xbegin_m;
294  rEnd = xend_m;
295 }
296 void FM3DH5Block_nonscale::getFieldDimensions(double &xIni, double &xFinal, double &yIni, double &yFinal, double &zIni, double &zFinal) const {}
297 
299 
301  (*msg) << Filename_m << " (3D dynamic); zini= " << zbegin_m << " m; zfinal= " << zend_m << " m;" << endl;
302 }
303 
305  return frequency_m;
306 }
307 
309  frequency_m = freq;
310 }
311 
312 void FM3DH5Block_nonscale::getOnaxisEz(vector<pair<double, double> > & F) {
313  double Ez_max = 0.0, dz = (zend_m - zbegin_m) / (num_gridpz_m - 1);
314  const int index_x = -static_cast<int>(floor(xbegin_m / hx_m));
315  const double lever_x = -xbegin_m / hx_m - index_x;
316 
317  const int index_y = -static_cast<int>(floor(ybegin_m / hy_m));
318  const double lever_y = -ybegin_m / hy_m - index_y;
319 
320  long index1 = index_x + index_y * num_gridpx_m;
321 
322  F.resize(num_gridpz_m);
323 
324  for(int i = 0; i < num_gridpz_m; ++ i) {
325  F[i].first = dz * i;
326  F[i].second = (1.0 - lever_x) * (1.0 - lever_y) * FieldstrengthEz_m[index1 + num_gridpx_m * num_gridpy_m]
327  + lever_x * (1.0 - lever_y) * FieldstrengthEz_m[index1 + num_gridpx_m * num_gridpy_m + 1]
328  + (1.0 - lever_x) * lever_y * FieldstrengthEz_m[index1 + num_gridpx_m * num_gridpy_m + num_gridpx_m]
329  + lever_x * lever_y * FieldstrengthEz_m[index1 + num_gridpx_m * num_gridpy_m + num_gridpx_m + 1];
330 
331 
332  if(fabs(F[i].second) > Ez_max) {
333  Ez_max = fabs(F[i].second);
334  }
335  index1 += num_gridpy_m * num_gridpx_m;
336  }
337 
338  for(int i = 0; i < num_gridpz_m; ++ i) {
339  F[i].second /= Ez_max;
340  }
341 }
342 
343 // vi: set et ts=4 sw=4 sts=4:
344 // Local Variables:
345 // mode:c
346 // c-basic-offset: 4
347 // indent-tabs-mode:nil
348 // End:
virtual void getOnaxisEz(std::vector< std::pair< double, double > > &F)
static int getNodes()
Definition: IpplInfo.cpp:773
PETE_TUTree< FnAbs, typename T::PETE_Expr_t > abs(const PETE_Expr< T > &l)
DiffDirection
Definition: Fieldmap.h:54
std::vector< h5_float64_t > FieldstrengthEx_m
PETE_TUTree< FnFabs, typename T::PETE_Expr_t > fabs(const PETE_Expr< T > &l)
Definition: PETE.h:815
constexpr double two_pi
The value of .
Definition: Physics.h:34
virtual void getInfo(Inform *msg)
FM3DH5Block_nonscale(std::string aFilename)
static int myNode()
Definition: IpplInfo.cpp:794
virtual double getFrequency() const
static std::string typeset_msg(const std::string &msg, const std::string &title)
Definition: Fieldmap.cpp:647
std::vector< h5_float64_t > FieldstrengthHz_m
std::vector< h5_float64_t > FieldstrengthHy_m
std::vector< h5_float64_t > FieldstrengthEz_m
constexpr double mu_0
The permeability of vacuum in Vs/Am.
Definition: Physics.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 bool getFieldstrength(const Vector_t &R, Vector_t &E, Vector_t &B) const
static MPI_Comm getComm()
Definition: IpplInfo.h:178
std::vector< h5_float64_t > FieldstrengthHx_m
const std::string name
virtual void setFrequency(double freq)
std::string Filename_m
Definition: Fieldmap.h:110
std::vector< h5_float64_t > FieldstrengthEy_m
MapType Type
Definition: Fieldmap.h:107
Inform & level3(Inform &inf)
Definition: Inform.cpp:47
Definition: Inform.h:41
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
virtual void getFieldDimensions(double &zBegin, double &zEnd, double &rBegin, double &rEnd) const