OPAL (Object Oriented Parallel Accelerator Library)  2024.1
OPAL
BoundaryGeometry.h
Go to the documentation of this file.
1 //
2 // Implementation of the BoundaryGeometry class
3 //
4 // Copyright (c) 200x - 2020, Achim Gsell,
5 // 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 
33 #ifndef _OPAL_BOUNDARY_GEOMETRY_H
34 #define _OPAL_BOUNDARY_GEOMETRY_H
35 
36 class OpalBeamline;
37 class ElementBase;
38 
40 #include "Attributes/Attributes.h"
41 #include "Utilities/Util.h"
42 #include "Utility/IpplTimings.h"
43 #include "Utility/PAssert.h"
44 
45 #include <gsl/gsl_rng.h>
46 
47 #include <array>
48 #include <unordered_map>
49 #include <unordered_set>
50 #include <vector>
51 
52 enum class Topology: unsigned short {
54  BOXCORNER,
55  ELLIPTIC
56 };
57 
58 class BoundaryGeometry : public Definition {
59 
60 public:
62  virtual ~BoundaryGeometry();
63 
64  virtual bool canReplaceBy (
65  Object* object);
66 
67  virtual BoundaryGeometry* clone (
68  const std::string& name);
69 
70  // Check the GEOMETRY data.
71  virtual void execute ();
72 
73  // Find named GEOMETRY.
74  static BoundaryGeometry* find (
75  const std::string& name);
76 
77  // Update the GEOMETRY data.
78  virtual void update ();
79 
80  void updateElement (
81  ElementBase* element);
82 
83  void initialize ();
84 
85  int partInside (
86  const Vector_t& r,
87  const Vector_t& v,
88  const double dt,
89  Vector_t& intecoords,
90  int& triId);
91 
92  Inform& printInfo (
93  Inform& os) const;
94 
95  void writeGeomToVtk (std::string fn);
96 
97  inline std::string getFilename() const {
99  }
100 
101  inline Topology getTopology() const {
102  static const std::unordered_map<std::string, Topology> stringTopology_s = {
103  {"RECTANGULAR", Topology::RECTANGULAR},
104  {"BOXCORNER", Topology::BOXCORNER},
105  {"ELLIPTIC", Topology::ELLIPTIC}
106  };
107  Topology topo = stringTopology_s.at(Attributes::getString(itsAttr[TOPO]));
108  return topo;
109  }
110 
111  inline double getA() {
112  return (double)Attributes::getReal(itsAttr[A]);
113  }
114 
115  inline double getB() {
116  return (double)Attributes::getReal(itsAttr[B]);
117  }
118 
119  inline double getC() {
120  return (double)Attributes::getReal(itsAttr[C]);
121  }
122 
123  inline double getS() {
124  return (double)Attributes::getReal(itsAttr[S]);
125  }
126 
127  inline double getLength() {
128  return (double)Attributes::getReal(itsAttr[LENGTH]);
129  }
130 
131  inline double getL1() {
132  return (double)Attributes::getReal(itsAttr[L1]);
133  }
134 
135  inline double getL2() {
136  return (double)Attributes::getReal(itsAttr[L2]);
137  }
138 
142  inline size_t getNumBFaces () {
143  return Triangles_m.size();
144  }
145 
149  inline Vector_t gethr () {
150  return voxelMesh_m.sizeOfVoxel;
151  }
155  inline Vektor<int, 3> getnr () {
156  return voxelMesh_m.nr_m;
157  }
158 
162  inline Vector_t getmincoords () {
163  return minExtent_m;
164  }
168  inline Vector_t getmaxcoords () {
169  return maxExtent_m;
170  }
171 
172  inline bool getInsidePoint (Vector_t& pt) {
173  if (haveInsidePoint_m == false) {
174  return false;
175  }
176  pt = insidePoint_m;
177  return true;
178  }
179 
180  bool findInsidePoint (void);
181 
183  const Vector_t& P,
184  const Vector_t& v,
185  Vector_t& I);
186 
187  int fastIsInside (
188  const Vector_t& reference_pt, // [in] a reference point
189  const Vector_t& P // [in] point to test
190  );
191 
192  enum DebugFlags {
193  debug_isInside = 0x0001,
199  };
200 
201  inline void enableDebug(enum DebugFlags flags) {
202  debugFlags_m |= flags;
203  }
204 
205  inline void disableDebug(enum DebugFlags flags) {
206  debugFlags_m &= ~flags;
207  }
208 
209 private:
210  bool isInside (
211  const Vector_t& P // [in] point to test
212  );
213 
215  const int triangle_id,
216  const int i,
217  const int j,
218  const int k);
219 
221  const Vector_t&,
222  const Vector_t&,
223  Vector_t&,
224  int&
225  );
226 
228  const Vector_t& P0,
229  const Vector_t& P1,
230  Vector_t& intersection_pt,
231  int& triangle_id
232  );
233 
234  std::string h5FileName_m; // H5hut filename
235 
236  std::vector<Vector_t> Points_m; // geometry point coordinates
237  std::vector<std::array<unsigned int,4>> Triangles_m; // boundary faces defined via point IDs
238  // please note: 4 is correct, historical reasons!
239 
240  std::vector<Vector_t> TriNormals_m; // oriented normal vector of triangles
241  std::vector<double> TriAreas_m; // area of triangles
242 
243  Vector_t minExtent_m; // minimum of geometry coordinate.
244  Vector_t maxExtent_m; // maximum of geometry coordinate.
245 
246  struct {
250  Vektor<int, 3> nr_m; // number of intervals of geometry in X,Y,Z direction
251  std::unordered_map<int, // map voxel IDs ->
252  std::unordered_set<int>> ids; // intersecting triangles
253 
254  } voxelMesh_m;
255 
257 
259  Vector_t insidePoint_m; // attribute INSIDEPOINT
260 
261  gsl_rng *randGen_m; //
262 
263  IpplTimings::TimerRef Tinitialize_m; // initialize geometry
268 
270  void operator= (const BoundaryGeometry&);
271 
272  // Clone constructor.
273  BoundaryGeometry(const std::string& name, BoundaryGeometry* parent);
274 
275  inline const Vector_t& getPoint (const int triangle_id, const int vertex_id) {
276  PAssert (1 <= vertex_id && vertex_id <=3);
277  return Points_m[Triangles_m[triangle_id][vertex_id]];
278  }
279 
284  };
285 
287  const enum INTERSECTION_TESTS kind,
288  const Vector_t& P0,
289  const Vector_t& P1,
290  const int triangle_id,
291  Vector_t& I);
292 
293  inline int mapVoxelIndices2ID (const int i, const int j, const int k);
294  inline Vector_t mapIndices2Voxel (const int, const int, const int);
295  inline Vector_t mapPoint2Voxel (const Vector_t&);
296  inline void computeMeshVoxelization (void);
297 
298  enum {
299  FGEOM, // file holding the geometry
300  LENGTH, // length of elliptic tube or boxcorner
301  S, // start of the geometry
302  L1, // in case of BOXCORNER first part of geometry with height B
303  L2, // in case of BOXCORNER second part of geometry with height B-C
304  A, // major semi-axis of elliptic tube
305  B, // minor semi-axis of ellitpic tube
306  C, // in case of BOXCORNER height of corner
307  TOPO, // RECTANGULAR, BOXCORNER, ELLIPTIC if FGEOM is selected TOPO is over-written
308  ZSHIFT, // Shift in z direction
309  XYZSCALE, // Multiplicative scaling factor for coordinates
310  XSCALE, // Multiplicative scaling factor for x-coordinates
311  YSCALE, // Multiplicative scaling factor for y-coordinates
312  ZSCALE, // Multiplicative scaling factor for z-coordinates
315  };
316 };
317 
318 inline Inform &operator<< (Inform& os, const BoundaryGeometry& b) {
319  return b.printInfo (os);
320 }
321 #endif
bool getInsidePoint(Vector_t &pt)
virtual BoundaryGeometry * clone(const std::string &name)
Return a clone.
Vector_t mapPoint2Voxel(const Vector_t &)
The base class for all OPAL objects.
Definition: Object.h:48
std::string getString(const Attribute &attr)
Get string value.
Definition: Attributes.cpp:343
int mapVoxelIndices2ID(const int i, const int j, const int k)
int intersectTriangleVoxel(const int triangle_id, const int i, const int j, const int k)
std::ostream & operator<<(std::ostream &os, const Attribute &attr)
Definition: Attribute.cpp:169
void disableDebug(enum DebugFlags flags)
Definition: TSVMeta.h:24
virtual void update()
Update this object.
std::vector< std::array< unsigned int, 4 > > Triangles_m
int intersectLineTriangle(const enum INTERSECTION_TESTS kind, const Vector_t &P0, const Vector_t &P1, const int triangle_id, Vector_t &I)
IpplTimings::TimerRef TRayTrace_m
int partInside(const Vector_t &r, const Vector_t &v, const double dt, Vector_t &intecoords, int &triId)
virtual void execute()
Execute the command.
static BoundaryGeometry * find(const std::string &name)
int intersectLineSegmentBoundary(const Vector_t &P0, const Vector_t &P1, Vector_t &intersection_pt, int &triangle_id)
std::unordered_map< int, std::unordered_set< int > > ids
bool isInside(const Vector_t &P)
Timing::TimerRef TimerRef
Definition: IpplTimings.h:176
Vector_t getmaxcoords()
void enableDebug(enum DebugFlags flags)
std::string getFilename() const
std::vector< double > TriAreas_m
int fastIsInside(const Vector_t &reference_pt, const Vector_t &P)
std::vector< Vector_t > TriNormals_m
Definition: Inform.h:42
IpplTimings::TimerRef TfastIsInside_m
void operator=(const BoundaryGeometry &)
Inform & printInfo(Inform &os) const
IpplTimings::TimerRef Tinitialize_m
std::string h5FileName_m
Vektor< int, 3 > nr_m
Topology
std::vector< Attribute > itsAttr
The object attributes.
Definition: Object.h:216
virtual bool canReplaceBy(Object *object)
Test if replacement is allowed.
Vektor< int, 3 > getnr()
Topology getTopology() const
const std::string name
int intersectTinyLineSegmentBoundary(const Vector_t &, const Vector_t &, Vector_t &, int &)
double getReal(const Attribute &attr)
Return real value.
Definition: Attributes.cpp:252
void computeMeshVoxelization(void)
The base class for all OPAL definitions.
Definition: Definition.h:30
#define PAssert(c)
Definition: PAssert.h:102
void updateElement(ElementBase *element)
IpplTimings::TimerRef TisInside_m
struct BoundaryGeometry::@71 voxelMesh_m
IpplTimings::TimerRef TPartInside_m
Vector_t mapIndices2Voxel(const int, const int, const int)
Vector_t getmincoords()
void writeGeomToVtk(std::string fn)
const Vector_t & getPoint(const int triangle_id, const int vertex_id)
int intersectRayBoundary(const Vector_t &P, const Vector_t &v, Vector_t &I)
std::vector< Vector_t > Points_m