openPMD_raytrace_API  0.1.0
openPMD_io.hh
Go to the documentation of this file.
1 #ifndef RAYTRACE_API_HH
2 #define RAYTRACE_API_HH
3 #include "ray.hh"
5 #include <openPMD/openPMD.hpp> // openPMD C++ API
6 #include <string>
7 
8 #include <exception>
9 
10 namespace raytracing {
11 #define ITER 1
12 
20 class openPMD_io {
21  // Auxiliary classes that are private, so not part of the public API
22 private:
46  class Rays {
47  // public to make it available to openPMD_io methods
48  public:
53  template <typename T> class Record {
54  std::vector<T> _vals;
55  T _min, _max;
56 
57  public:
58  Record(): _vals(), _min(), _max() { clear(); }
59  const std::vector<T>& vals(void) const { return _vals; };
60  std::vector<T>& vals(void) { return _vals; };
61  T min(void) const { return _min; };
62  T max(void) const { return _max; };
63 
64  // used when filling before writing
65  void push_back(T val) {
66  _vals.push_back(val);
67  if (_min > val) (_min) = val;
68  if (_max < val) (_max) = val;
69  }
70 
71  // this is used when reading from the openPMD file
72  void store(const T* vec, size_t n, float min, float max) {
73  // clear();
74  _vals.insert(_vals.begin(), vec, vec + n);
75  _min = min;
76  _max = max;
77  }
78 
79  void clear_chunk(void) { _vals.clear(); }
80 
81  void clear(void) {
82  std::numeric_limits<T> lim;
83  _max = lim.min();
84  _min = lim.max();
85  _vals.clear();
86  }
87  const T operator[](size_t i) const { return _vals[i]; }; // cannot modify
88  }; // end of Record class
89 
90  //------------------------------ public memebers
91  public:
92  // I make the Records public members to avoid writing methods to access them
93  //
94  // the 3D components are in separate vectors because this is the way the openPMD API
95  // wants them to be
96  Record<float> _x, _y, _z, // position
97  _dx, _dy, _dz, // direction (vx^2+vy^2+vz^2) = 1
98  _sx, _sy, _sz, // non-photon polarization
99  _sPolAx, _sPolAy, _sPolAz, _sPolPh, // photon s-polarization amplitude
100  _pPolAx, _pPolAy, _pPolAz, _pPolPh, // photon p-polarization amplitude
101  _wavelength, // wavelength
102  _time, _weight; // ray time, weight
104  Record<int> _status; // alive status
105 
106  //------------------------------ private memebers
107  private:
108  size_t _size = 0; // number of stored rays = min(chunk_size, remaining rays to read)
109  size_t _read = 0; // current index when reading
110 
111  //------------------------------ public methods
112  public:
114  Rays();
115 
119  void push(const Ray& this_ray);
120 
127  Ray pop(bool next = true);
128 
130  void clear_chunk(void) {
131  _size = 0;
132  _read = 0;
133 
134  _x.clear_chunk();
135  _y.clear_chunk();
136  _z.clear_chunk();
137 
138  _dx.clear_chunk();
139  _dy.clear_chunk();
140  _dz.clear_chunk();
141 
142  _sx.clear_chunk();
143  _sy.clear_chunk();
144  _sz.clear_chunk();
145 
146  _sPolAx.clear_chunk();
147  _sPolAy.clear_chunk();
148  _sPolAz.clear_chunk();
149  _pPolAx.clear_chunk();
150  _pPolAy.clear_chunk();
151  _pPolAz.clear_chunk();
152 
153  _sPolPh.clear_chunk();
154  _pPolPh.clear_chunk();
155 
156  _wavelength.clear_chunk();
157 
158  _time.clear_chunk();
159  _weight.clear_chunk();
160 
161  _id.clear_chunk();
162  _status.clear_chunk();
163  }
165  void clear(void) {
166  _size = 0;
167  _read = 0;
168 
169  _x.clear();
170  _y.clear();
171  _z.clear();
172 
173  _dx.clear();
174  _dy.clear();
175  _dz.clear();
176 
177  _sx.clear();
178  _sy.clear();
179  _sz.clear();
180 
181  _sPolAx.clear();
182  _sPolAy.clear();
183  _sPolAz.clear();
184  _pPolAx.clear();
185  _pPolAy.clear();
186  _pPolAz.clear();
187 
188  _sPolPh.clear();
189  _pPolPh.clear();
190 
191  _wavelength.clear();
192 
193  _time.clear();
194  _weight.clear();
195 
196  _id.clear();
197  _status.clear();
198  };
199 
201  size_t size() const { return _size; };
202 
204  void size(size_t s) {
205  _size = s;
206  if (_x.vals().size() != s)
207  throw std::runtime_error(
208  "size of stored Rays and one of its records is different");
209  };
210 
215  bool is_chunk_finished(void) { return _read == _size; }
216  }; // end of Rays class
217 
218 public:
225  explicit openPMD_io(
226  const std::string& filename,
227  const std::string mc_code_name =
228  "",
229  const std::string mc_code_version = "",
230  const std::string instrument_name = "",
231  const std::string name_current_component =
232  ""
233  );
234 
235  /***************************************************************/
238 
246  void init_write(std::string particle_species,
247  unsigned long long int n_rays,
248 
249  unsigned int iter = 1
250  );
252 
259  void
260  init_rays(std::string particle_species, unsigned long long int n_rays, unsigned int iter);
261 
263  void trace_write(Ray this_ray);
264 
268  void save_write(void);
270 
271  /***************************************************************/
274 
276  unsigned long long int
277  init_read(std::string pdgId,
278  unsigned int iter = 1,
279  unsigned long long int n_rays = 0,
280  unsigned int repeat =
281  1
282  );
283 
292  Ray trace_read(void);
293 
294  void set_gravity_direction(float x, float y, float z);
295 
296  void get_gravity_direction(float* x, float* y, float* z);
297  void get_horizontal_direction(float* x, float* y, float* z);
298  void set_horizontal_direction(float* x, float* y, float* z);
300 private:
301  void load_chunk(void);
302 
303 private:
304  // parameters defined at construction
305  std::string _name;
306  std::string _mc_code_name;
307  std::string _mc_code_version;
308  std::string _instrument_name;
309  std::string _name_current_component;
310  unsigned int _i_repeat, _n_repeat;
311  unsigned long long int _nrays, _max_allowed_rays;
312 
313  // internal usage
314  // openPMD::Access _access_mode;
315  openPMD::Offset _offset;
316  bool _isWriteMode;
317  std::unique_ptr<openPMD::Series> _series;
318  Rays _rays;
319  Ray _last_ray;
320  unsigned int _iter;
321  std::string _particle_species;
322 
323  //------------------------------ set of helper methods
324  inline openPMD::Iteration& iter_pmd(unsigned int iter) { return _series->iterations[iter]; }
325 
326  // returns the current particle species from the current iteration
327  inline openPMD::ParticleSpecies& rays_pmd(void) {
328  auto i = iter_pmd(_iter);
329  return i.particles[_particle_species];
330  }
331 
332  // returns the given particle species from the current iteration
333  inline openPMD::ParticleSpecies& rays_pmd(std::string particle_species) {
334  _particle_species = particle_species;
335  auto i = iter_pmd(_iter);
336  return i.particles[particle_species];
337  }
338 
344  void init_ray_prop(std::string name,
345  openPMD::Dataset& dataset,
346  bool isScalar,
347  std::map<openPMD::UnitDimension, double> const& dims =
348  {{openPMD::UnitDimension::L, 0.}},
349  double unitSI = 0.);
350 };
351 
352 } // namespace raytracing
353 #endif
I/O API for the ray trace extension of the openPMD format.
Definition: openPMD_io.hh:20
void init_write(std::string particle_species, unsigned long long int n_rays, unsigned int iter=1)
initializes the "series" object from the openPMD API in WRITE MODE
Definition: openPMD_io.cc:128
template utility class to simplify implementation It is a vector that also stores min and max values ...
Definition: openPMD_io.hh:53
Generic ray, containing all the ray information being stored by the API into the openPMD file...
Definition: ray.hh:39
defines the maximum number of rays that can be stored in memory before dumping to file ...
Definition: openPMD_io.hh:10
void trace_write(Ray this_ray)
save ray properties for further writing to file by save_write()
Definition: openPMD_io.cc:394
void init_rays(std::string particle_species, unsigned long long int n_rays, unsigned int iter)
declare the ray particle species in the file
Definition: openPMD_io.cc:80
Ray trace_read(void)
Read rays from file and returns the next in the list.
Definition: openPMD_io.cc:408
unsigned long long int init_read(std::string pdgId, unsigned int iter=1, unsigned long long int n_rays=0, unsigned int repeat=1)
initializes the "series" object from the openPMD API in READ MODE
Definition: openPMD_io.cc:343
void save_write(void)
Flushes the output to file before closing it.
Definition: openPMD_io.cc:182
openPMD_io(const std::string &filename, const std::string mc_code_name="", const std::string mc_code_version="", const std::string instrument_name="", const std::string name_current_component="")
constructor
Definition: openPMD_io.cc:36