OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
FM3DMagnetoStaticH5Block.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 
39  h5err = H5SetStep(file, last_step);
40  assert (h5err != H5_ERR);
41 
42  h5_int64_t num_fields = H5BlockGetNumFields(file);
43  assert (num_fields != H5_ERR);
44 
45  for(h5_ssize_t i = 0; i < num_fields; ++ i) {
46  h5_int64_t ftype;
47  h5err = H5BlockGetFieldInfo(file, (h5_size_t)i, name, len_name, &grid_rank, grid_dims, &field_dims, &ftype);
48  assert (h5err != H5_ERR);
49 
50  if(strcmp(name, "Efield") == 0) {
51  num_gridpx_m = grid_dims[0];
52  num_gridpy_m = grid_dims[1];
53  num_gridpz_m = grid_dims[2];
54  }
55  }
56  h5err = H5Block3dGetFieldSpacing(file, "Efield", &hx_m, &hy_m, &hz_m);
57  assert (h5err != H5_ERR);
58 
59  h5err = H5Block3dGetFieldOrigin(file, "Efield", &xbegin_m, &ybegin_m, &zbegin_m);
60  assert (h5err != H5_ERR);
61 
62  xend_m = xbegin_m + (num_gridpx_m - 1) * hx_m;
63  yend_m = ybegin_m + (num_gridpy_m - 1) * hy_m;
64  zend_m = zbegin_m + (num_gridpz_m - 1) * hz_m;
65 
66  // xcentral_idx_m = static_cast<int>(fabs(xbegin_m) / hx_m);
67  // ycentral_idx_m = static_cast<int>(fabs(ybegin_m) / hy_m);
68 
69 
70  h5err = H5ReadFileAttribFloat64(file, "Resonance Frequency(Hz)", &frequency_m);
71  assert (h5err != H5_ERR);
72 
74 
75  h5err = H5CloseFile(file);
76  assert (h5err != H5_ERR);
77 }
78 
79 
81  freeMap();
82 }
83 
85  if (!FieldstrengthEz_m.empty()) {
86  return;
87  }
88  h5_int64_t h5err;
89 #if defined (NDEBUG)
90  (void)h5err;
91 #endif
92 
93  h5_prop_t props = H5CreateFileProp ();
94  MPI_Comm comm = Ippl::getComm();
95  h5err = H5SetPropFileMPIOCollective (props, &comm);
96  assert (h5err != H5_ERR);
97  h5_file_t file = H5OpenFile (Filename_m.c_str(), H5_O_RDONLY, props);
98  assert (file != (h5_file_t)H5_ERR);
99  H5CloseProp (props);
100 
101  long field_size = 0;
102  int Nnodes = Ippl::getNodes();//min(20, Ippl::getNodes());
103  int Nz_avrg = static_cast<int>(floor(0.5 + num_gridpz_m / Nnodes));
104  int Nz_diff = Nz_avrg * Nnodes - num_gridpz_m;
105  int signNz = Nz_diff > 0 ? 1 : -1;
106  int *Nz_read_start = new int[Ippl::getNodes() + 1];
107  int *Nz_read_length = new int[Ippl::getNodes()];
108  int N_read_start;
109  int start = 0;
110  // int rbuf_size;
111 
112  h5_int64_t last_step = H5GetNumSteps(file) - 1;
113  assert (last_step >= 0);
114  h5err = H5SetStep(file, last_step);
115  assert (h5err != H5_ERR);
116 
117  for(int i = 0; i < abs(Nz_diff); ++ i) {
118  Nz_read_length[i] = Nz_avrg - signNz;
119  Nz_read_start[i] = start;
120  start += Nz_read_length[i];
121  }
122  for(int i = abs(Nz_diff); i < Nnodes; ++ i) {
123  Nz_read_length[i] = Nz_avrg;
124  Nz_read_start[i] = start;
125  start += Nz_read_length[i];
126  }
127  for(int i = Nnodes; i < Ippl::getNodes(); ++ i) {
128  Nz_read_length[i] = 0;
129  Nz_read_start[i] = start;
130  }
131  Nz_read_start[Ippl::getNodes()] = start;
132 
133  N_read_start = Nz_read_start[Ippl::myNode()] * num_gridpx_m * num_gridpy_m;
134 
135  // rbuf_size = max(Nz_avrg, Nz_avrg - signNz);
136  // std::unique_ptr<double> rbuf(new double[Ippl::getNodes() * rbuf_size]);
137 
138  h5err = H5Block3dSetView(file,
139  0, num_gridpx_m - 1,
140  0, num_gridpy_m - 1,
141  Nz_read_start[Ippl::myNode()], Nz_read_start[Ippl::myNode() + 1] - 1);
142  assert (h5err != H5_ERR);
143 
144  field_size = (num_gridpx_m * num_gridpy_m * num_gridpz_m);
145  FieldstrengthEx_m.resize(field_size);
146  FieldstrengthEy_m.resize(field_size);
147  FieldstrengthEz_m.resize(field_size);
148  FieldstrengthBx_m.resize(field_size);
149  FieldstrengthBy_m.resize(field_size);
150  FieldstrengthBz_m.resize(field_size);
151 
152  h5err = H5Block3dReadVector3dFieldFloat64(
153  file,
154  "Efield",
155  &(FieldstrengthEx_m[N_read_start]),
156  &(FieldstrengthEy_m[N_read_start]),
157  &(FieldstrengthEz_m[N_read_start]));
158  assert (h5err != H5_ERR);
159 
160  h5err = H5Block3dReadVector3dFieldFloat64(
161  file,
162  "Bfield",
163  &(FieldstrengthBx_m[N_read_start]),
164  &(FieldstrengthBy_m[N_read_start]),
165  &(FieldstrengthBz_m[N_read_start]));
166  assert (h5err != H5_ERR);
167 
168  for(int i = 0; i < Nnodes; ++ i) {
169  int N_read_start = Nz_read_start[i] * num_gridpx_m * num_gridpy_m;
170  int N_read_length = Nz_read_length[i] * num_gridpx_m * num_gridpy_m;
171  MPI_Bcast(&(FieldstrengthEx_m[N_read_start]), N_read_length, MPI_DOUBLE, i, Ippl::getComm());
172  MPI_Bcast(&(FieldstrengthEy_m[N_read_start]), N_read_length, MPI_DOUBLE, i, Ippl::getComm());
173  MPI_Bcast(&(FieldstrengthEz_m[N_read_start]), N_read_length, MPI_DOUBLE, i, Ippl::getComm());
174  MPI_Bcast(&(FieldstrengthBx_m[N_read_start]), N_read_length, MPI_DOUBLE, i, Ippl::getComm());
175  MPI_Bcast(&(FieldstrengthBy_m[N_read_start]), N_read_length, MPI_DOUBLE, i, Ippl::getComm());
176  MPI_Bcast(&(FieldstrengthBz_m[N_read_start]), N_read_length, MPI_DOUBLE, i, Ippl::getComm());
177  }
178 
179  h5err = H5CloseFile(file);
180  assert (h5err != H5_ERR);
181 
182  delete[] Nz_read_start;
183  delete[] Nz_read_length;
184 
185  INFOMSG(level3 << typeset_msg("read in fieldmap '" + Filename_m + "'", "info") << "\n"
186  << endl);
187 
188 }
189 
191  if(!FieldstrengthEz_m.empty()) {
192  FieldstrengthEx_m.clear();
193  FieldstrengthEy_m.clear();
194  FieldstrengthEz_m.clear();
195  FieldstrengthBx_m.clear();
196  FieldstrengthBy_m.clear();
197  FieldstrengthBz_m.clear();
198  INFOMSG(level3 << typeset_msg("freed fieldmap '" + Filename_m + "'", "info") << "\n"
199  << endl);
200  }
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  ERRORMSG("Field strength at R " << R << " out of field maps" << endl);
220  return false;// if x or y > xmax or ymax, 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.
221  }
222  if(index_x < 0 || index_y < 0) {
223  ERRORMSG("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) * FieldstrengthBx_m[index1]
256  + lever_x * (1.0 - lever_y) * (1.0 - lever_z) * FieldstrengthBx_m[index1 + 1]
257  + (1.0 - lever_x) * lever_y * (1.0 - lever_z) * FieldstrengthBx_m[index1 + num_gridpx_m]
258  + lever_x * lever_y * (1.0 - lever_z) * FieldstrengthBx_m[index1 + num_gridpx_m + 1]
259  + (1.0 - lever_x) * (1.0 - lever_y) * lever_z * FieldstrengthBx_m[index1 + num_gridpx_m * num_gridpy_m]
260  + lever_x * (1.0 - lever_y) * lever_z * FieldstrengthBx_m[index1 + num_gridpx_m * num_gridpy_m + 1]
261  + (1.0 - lever_x) * lever_y * lever_z * FieldstrengthBx_m[index1 + num_gridpx_m * num_gridpy_m + num_gridpx_m]
262  + lever_x * lever_y * lever_z * FieldstrengthBx_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) * FieldstrengthBy_m[index1]
265  + lever_x * (1.0 - lever_y) * (1.0 - lever_z) * FieldstrengthBy_m[index1 + 1]
266  + (1.0 - lever_x) * lever_y * (1.0 - lever_z) * FieldstrengthBy_m[index1 + num_gridpx_m]
267  + lever_x * lever_y * (1.0 - lever_z) * FieldstrengthBy_m[index1 + num_gridpx_m + 1]
268  + (1.0 - lever_x) * (1.0 - lever_y) * lever_z * FieldstrengthBy_m[index1 + num_gridpx_m * num_gridpy_m]
269  + lever_x * (1.0 - lever_y) * lever_z * FieldstrengthBy_m[index1 + num_gridpx_m * num_gridpy_m + 1]
270  + (1.0 - lever_x) * lever_y * lever_z * FieldstrengthBy_m[index1 + num_gridpx_m * num_gridpy_m + num_gridpx_m]
271  + lever_x * lever_y * lever_z * FieldstrengthBy_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) * FieldstrengthBz_m[index1]
274  + lever_x * (1.0 - lever_y) * (1.0 - lever_z) * FieldstrengthBz_m[index1 + 1]
275  + (1.0 - lever_x) * lever_y * (1.0 - lever_z) * FieldstrengthBz_m[index1 + num_gridpx_m]
276  + lever_x * lever_y * (1.0 - lever_z) * FieldstrengthBz_m[index1 + num_gridpx_m + 1]
277  + (1.0 - lever_x) * (1.0 - lever_y) * lever_z * FieldstrengthBz_m[index1 + num_gridpx_m * num_gridpy_m]
278  + lever_x * (1.0 - lever_y) * lever_z * FieldstrengthBz_m[index1 + num_gridpx_m * num_gridpy_m + 1]
279  + (1.0 - lever_x) * lever_y * lever_z * FieldstrengthBz_m[index1 + num_gridpx_m * num_gridpy_m + num_gridpx_m]
280  + lever_x * lever_y * lever_z * FieldstrengthBz_m[index1 + num_gridpx_m * num_gridpy_m + num_gridpx_m + 1]);
281 
282 
283  return false;
284 
285 }
286 
288  return false;
289 }
290 
291 void FM3DMagnetoStaticH5Block::getFieldDimensions(double &zBegin, double &zEnd, double &rBegin, double &rEnd) const {
292  zBegin = zbegin_m;
293  zEnd = zend_m;
294  rBegin = xbegin_m;
295  rEnd = xend_m;
296 }
297 void FM3DMagnetoStaticH5Block::getFieldDimensions(double &xIni, double &xFinal, double &yIni, double &yFinal, double &zIni, double &zFinal) const {}
298 
300 
302  (*msg) << Filename_m << " (3DMagnetoStaticH5Block); zini= " << zbegin_m << " m; zfinal= " << zend_m << " m;" << endl;
303 }
304 
306  return 0.0;
307 }
308 
310 { ;}
311 
312 // vi: set et ts=4 sw=4 sts=4:
313 // Local Variables:
314 // mode:c
315 // c-basic-offset: 4
316 // indent-tabs-mode:nil
317 // End:
static int getNodes()
Definition: IpplInfo.cpp:773
virtual void setFrequency(double freq)
PETE_TUTree< FnAbs, typename T::PETE_Expr_t > abs(const PETE_Expr< T > &l)
DiffDirection
Definition: Fieldmap.h:54
#define ERRORMSG(msg)
Definition: IpplInfo.h:399
constexpr double two_pi
The value of .
Definition: Physics.h:34
std::vector< h5_float64_t > FieldstrengthBz_m
static int myNode()
Definition: IpplInfo.cpp:794
virtual void getFieldDimensions(double &zBegin, double &zEnd, double &rBegin, double &rEnd) const
static std::string typeset_msg(const std::string &msg, const std::string &title)
Definition: Fieldmap.cpp:647
std::vector< h5_float64_t > FieldstrengthEx_m
virtual double getFrequency() const
virtual void getInfo(Inform *msg)
std::vector< h5_float64_t > FieldstrengthEz_m
virtual bool getFieldstrength(const Vector_t &R, Vector_t &E, Vector_t &B) const
FM3DMagnetoStaticH5Block(std::string aFilename)
constexpr double mu_0
The permeability of vacuum in Vs/Am.
Definition: Physics.h:55
#define INFOMSG(msg)
Definition: IpplInfo.h:397
static MPI_Comm getComm()
Definition: IpplInfo.h:178
std::vector< h5_float64_t > FieldstrengthBy_m
const std::string name
std::vector< h5_float64_t > FieldstrengthEy_m
virtual bool getFieldDerivative(const Vector_t &R, Vector_t &E, Vector_t &B, const DiffDirection &dir) const
std::string Filename_m
Definition: Fieldmap.h:110
std::vector< h5_float64_t > FieldstrengthBx_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