Garfield++ 5.0
A toolkit for the detailed simulation of particle detectors based on ionisation measurement in gases and semiconductors
Loading...
Searching...
No Matches
ComponentFieldMap.hh
Go to the documentation of this file.
1#ifndef G_COMPONENT_FIELD_MAP_H
2#define G_COMPONENT_FIELD_MAP_H
3
4#include <array>
5#include <iostream>
6#include <map>
7#include <memory>
8#include <vector>
9
10#include "Component.hh"
11#include "TMatrixD.h"
12#include "TVectorD.h"
13#include "TetrahedralTree.hh"
14
15namespace Garfield {
16
17/// Base class for components based on finite-element field maps.
18
20 public:
21 /// Default constructor.
23 /// Constructor
24 ComponentFieldMap(const std::string& name);
25 /// Destructor
26 virtual ~ComponentFieldMap();
27
28 /// Check element aspect ratio.
29 bool Check();
30
31 /// Show x, y, z, V and angular ranges
32 void PrintRange();
33
34 /// List all currently defined materials
35 void PrintMaterials();
36 /// Flag a field map material as a drift medium.
37 void DriftMedium(const size_t imat);
38 /// Flag a field map materials as a non-drift medium.
39 void NotDriftMedium(const size_t imat);
40 /// Return the number of materials in the field map.
41 size_t GetNumberOfMaterials() const { return m_materials.size(); }
42 /// Return the relative permittivity of a field map material.
43 double GetPermittivity(const size_t imat) const;
44 /// Return the conductivity of a field map material.
45 double GetConductivity(const size_t imat) const;
46 /// Associate a field map material with a Medium object.
47 void SetMedium(const size_t imat, Medium* medium);
48 /// Return the Medium associated to a field map material.
49 Medium* GetMedium(const size_t imat) const;
51 /// Associate all field map materials with a relative permittivity
52 /// of unity to a given Medium class.
53 void SetGas(Medium* medium);
54
55 /// Return the number of mesh elements.
56 virtual size_t GetNumberOfElements() const { return m_elements.size(); }
57 /// Return the volume and aspect ratio of a mesh element.
58 bool GetElement(const size_t i, double& vol, double& dmin,
59 double& dmax) const;
60 /// Return the material and node indices of a mesh element.
61 virtual bool GetElement(const size_t i, size_t& mat, bool& drift,
62 std::vector<size_t>& nodes) const;
63 virtual size_t GetNumberOfNodes() const { return m_nodes.size(); }
64 virtual bool GetNode(const size_t i, double& x, double& y, double& z) const;
65 double GetPotential(const size_t i) const;
66
67 // Options
68 void EnableCheckMapIndices(const bool on = true) {
69 m_checkMultipleElement = on;
70 }
71 /// Option to eliminate mesh elements in conductors (default: on).
72 void EnableDeleteBackgroundElements(const bool on = true) {
74 }
75
76 /// Enable or disable the usage of the tetrahedral tree
77 /// for searching the element in the mesh.
78 void EnableTetrahedralTreeForElementSearch(const bool on = true) {
79 m_useTetrahedralTree = on;
80 }
81
82 /// Enable or disable warnings that the calculation of the local
83 /// coordinates did not achieve the requested precision.
84 void EnableConvergenceWarnings(const bool on = true) {
86 }
87
88 Medium* GetMedium(const double x, const double y, const double z) override;
89 void ElectricField(const double x, const double y, const double z, double& ex,
90 double& ey, double& ez, Medium*& m, int& status) override;
91 void ElectricField(const double x, const double y, const double z, double& ex,
92 double& ey, double& ez, double& v, Medium*& m,
93 int& status) override;
95 void WeightingField(const double x, const double y, const double z,
96 double& wx, double& wy, double& wz,
97 const std::string& label) override;
98
99 double WeightingPotential(const double x, const double y, const double z,
100 const std::string& label) override;
101
102 double DelayedWeightingPotential(double x, double y, double z, const double t,
103 const std::string& label) override;
104
105 bool IsInBoundingBox(const double x, const double y, const double z) const {
106 return x >= m_minBoundingBox[0] && x <= m_maxBoundingBox[0] &&
107 y >= m_minBoundingBox[1] && y <= m_maxBoundingBox[1] &&
108 z >= m_minBoundingBox[2] && y <= m_maxBoundingBox[2];
109 }
110
111 bool GetBoundingBox(double& xmin, double& ymin, double& zmin, double& xmax,
112 double& ymax, double& zmax) override;
113 bool GetElementaryCell(double& xmin, double& ymin, double& zmin, double& xmax,
114 double& ymax, double& zmax) override;
115
116 bool GetVoltageRange(double& vmin, double& vmax) override {
117 vmin = m_mapvmin;
118 vmax = m_mapvmax;
119 return true;
120 }
121
122 /** Makes a weighting potential copy of a imported map which can be translated
123 * and rotated. \param label name of new electrode \param labelSource name of
124 * the source electrode that will be copied \param x translation in the
125 * x-direction. \param y translation in the y-direction. \param z translation
126 * in the z-direction. \param alpha rotation around the x-axis. \param beta
127 * rotation around the y-axis. \param gamma rotation around the z-axis.
128 */
129 void CopyWeightingPotential(const std::string& label,
130 const std::string& labelSource, const double x,
131 const double y, const double z,
132 const double alpha, const double beta,
133 const double gamma);
134
135 friend class ViewFEMesh;
136
137 protected:
138 bool m_is3d = true;
139
146
147 // Elements
148 struct Element {
149 // Nodes
150 int emap[10];
151 // Material
152 unsigned int matmap;
153 };
154 std::vector<Element> m_elements;
155 std::vector<int> m_elementIndices;
156 // Degeneracy flags.
157 std::vector<bool> m_degenerate;
158 // Bounding boxes of the elements.
159 std::vector<std::array<double, 3> > m_bbMin;
160 std::vector<std::array<double, 3> > m_bbMax;
161
162 std::vector<std::array<std::array<double, 3>, 4> > m_w12;
163
164 // Nodes
165 struct Node {
166 // Coordinates
167 double x, y, z;
168 };
169 std::vector<Node> m_nodes;
170
171 // Potentials.
172 std::vector<double> m_pot;
173 // Weighting potentials.
174 std::map<std::string, std::vector<double> > m_wpot;
175 // Delayed weighting potentials.
176 std::map<std::string, std::vector<std::vector<double> > > m_dwpot;
177
178 // Materials
179 struct Material {
180 // Permittivity
181 double eps;
182 // Resistivity
183 double ohm;
185 // Associated medium
187 };
188 std::vector<Material> m_materials;
189
190 // Weighting potential copy
192 // Source
193 std::string source;
194 TMatrixD rot = TMatrixD(3,3);
195 TVectorD trans = TVectorD(3);
196 };
197
198 // Weighting potential copies.
199 std::map<std::string, WeightingFieldCopy> m_wfieldCopies;
200
201 std::vector<double> m_wdtimes;
202
203 // Bounding box
204 bool m_hasBoundingBox = false;
205 std::array<double, 3> m_minBoundingBox = {{0., 0., 0.}};
206 std::array<double, 3> m_maxBoundingBox = {{0., 0., 0.}};
207
208 // Ranges and periodicities
209 std::array<double, 3> m_mapmin = {{0., 0., 0.}};
210 std::array<double, 3> m_mapmax = {{0., 0., 0.}};
211 std::array<double, 3> m_mapamin = {{0., 0., 0.}};
212 std::array<double, 3> m_mapamax = {{0., 0., 0.}};
213 std::array<double, 3> m_mapna = {{0., 0., 0.}};
214 std::array<double, 3> m_cells = {{0., 0., 0.}};
215
216 double m_mapvmin = 0.;
217 double m_mapvmax = 0.;
218
219 std::array<bool, 3> m_setang;
220
221 // Option to delete meshing in conductors
223
224 // Warnings flag
225 bool m_warning = false;
226 unsigned int m_nWarnings = 0;
227
228 // Print warnings about failed convergence when refining
229 // isoparametric coordinates.
231
232 // Get the scaling factor for a given length unit.
233 static double ScalingFactor(std::string unit);
234
235 // Reset the component.
236 void Reset() override;
237
238 void Prepare();
239
240 // Calculate x, y, z, V and angular ranges.
241 virtual void SetRange();
242
243 // Periodicities
244 void UpdatePeriodicity() override {
247 }
248 void UpdatePeriodicity2d();
250
251 /// Find lowest epsilon, check for eps = 0, set default drift media flags.
253
254 /// Compute the electric/weighting field.
255 int Field(const double x, const double y, const double z,
256 double& fx, double& fy, double& fz, int& iel,
257 const std::vector<double>& potentials) const;
258 /// Compute the electrostatic/weighting potential.
259 double Potential(const double x, const double y, const double z,
260 const std::vector<double>& potentials) const;
261 /// Interpolate the potential in a triangle.
262 static double Potential3(const std::array<double, 6>& v,
263 const std::array<double, 3>& t);
264 /// Interpolate the field in a triangle.
265 static void Field3(const std::array<double, 6>& v,
266 const std::array<double, 3>& t, double jac[4][4],
267 const double det, double& ex, double& ey);
268 /// Interpolate the potential in a curved quadrilateral.
269 static double Potential5(const std::array<double, 8>& v,
270 const std::array<double, 2>& t);
271 /// Interpolate the field in a curved quadrilateral.
272 static void Field5(const std::array<double, 8>& v,
273 const std::array<double, 2>& t, double jac[4][4],
274 const double det, double& ex, double& ey);
275 /// Interpolate the potential in a curved quadratic tetrahedron.
276 static double Potential13(const std::array<double, 10>& v,
277 const std::array<double, 4>& t);
278 /// Interpolate the field in a curved quadratic tetrahedron.
279 static void Field13(const std::array<double, 10>& v,
280 const std::array<double, 4>& t, double jac[4][4],
281 const double det, double& ex, double& ey, double& ez);
282 /// Find the element for a point in curved quadratic quadrilaterals.
283 int FindElement5(const double x, const double y, double& t1,
284 double& t2, double& t3, double& t4, double jac[4][4],
285 double& det) const;
286 /// Find the element for a point in curved quadratic tetrahedra.
287 int FindElement13(const double x, const double y, const double z, double& t1,
288 double& t2, double& t3, double& t4, double jac[4][4],
289 double& det) const;
290 /// Find the element for a point in a cube.
291 int FindElementCube(const double x, const double y, const double z,
292 double& t1, double& t2, double& t3, TMatrixD*& jac,
293 std::vector<TMatrixD*>& dN) const;
294
295 /// Move (xpos, ypos, zpos) to field map coordinates.
296 void MapCoordinates(double& xpos, double& ypos, double& zpos, bool& xmirrored,
297 bool& ymirrored, bool& zmirrored, double& rcoordinate,
298 double& rotation) const;
299 /// Move (ex, ey, ez) to global coordinates.
300 void UnmapFields(double& ex, double& ey, double& ez,
301 const double xpos, const double ypos, const double zpos,
302 const bool xmirrored, const bool ymirrored,
303 const bool zmirrored, const double rcoordinate,
304 const double rotation) const;
305
306 static int ReadInteger(char* token, int def, bool& error);
307 static double ReadDouble(char* token, double def, bool& error);
308
309 virtual double GetElementVolume(const size_t i) const;
310 virtual void GetAspectRatio(const size_t i, double& dmin, double& dmax) const;
311
312 void PrintWarning(const std::string& header);
313 void PrintNotReady(const std::string& header) const;
314 void PrintCouldNotOpen(const std::string& header,
315 const std::string& filename) const;
316 void PrintElement(const std::string& header, const double x, const double y,
317 const double z, const double t1, const double t2,
318 const double t3, const double t4, const size_t i,
319 const std::vector<double>& potential) const;
320 /// Interpolation of potential between two time slices.
321 void TimeInterpolation(const double t, double& f0, double& f1, int& i0,
322 int& i1);
323
324 private:
325 /// Scan for multiple elements that contain a point
326 bool m_checkMultipleElement = false;
327
328 // Tetrahedral tree
329 bool m_useTetrahedralTree = true;
330 std::unique_ptr<TetrahedralTree> m_octree;
331
332 /// Flag to check if bounding boxes of elements are cached
333 bool m_cacheElemBoundingBoxes = false;
334
335 /// Calculate local coordinates for curved quadratic triangles.
336 int Coordinates3(const double x, const double y,
337 double& t1, double& t2, double& t3, double& t4,
338 double jac[4][4], double& det,
339 const std::array<double, 8>& xn,
340 const std::array<double, 8>& yn) const;
341 /// Calculate local coordinates for linear quadrilaterals.
342 int Coordinates4(const double x, const double y,
343 double& t1, double& t2, double& t3, double& t4,
344 double& det,
345 const std::array<double, 8>& xn,
346 const std::array<double, 8>& yn) const;
347 /// Calculate local coordinates for curved quadratic quadrilaterals.
348 int Coordinates5(const double x, const double y,
349 double& t1, double& t2, double& t3, double& t4,
350 double jac[4][4], double& det,
351 const std::array<double, 8>& xn,
352 const std::array<double, 8>& yn) const;
353 /// Calculate local coordinates in linear tetrahedra.
354 void Coordinates12(const double x, const double y, const double z,
355 double& t1, double& t2, double& t3, double& t4,
356 const std::array<double, 10>& xn,
357 const std::array<double, 10>& yn,
358 const std::array<double, 10>& zn,
359 const std::array<std::array<double, 3>, 4>& w) const;
360
361 /// Calculate local coordinates for curved quadratic tetrahedra.
362 int Coordinates13(const double x, const double y, const double z,
363 double& t1, double& t2, double& t3, double& t4,
364 double jac[4][4], double& det,
365 const std::array<double, 10>& xn,
366 const std::array<double, 10>& yn,
367 const std::array<double, 10>& zn,
368 const std::array<std::array<double, 3>, 4>& w) const;
369 /// Calculate local coordinates for a cube.
370 int CoordinatesCube(const double x, const double y, const double z,
371 double& t1, double& t2, double& t3, TMatrixD*& jac,
372 std::vector<TMatrixD*>& dN, const Element& element) const;
373
374 /// Calculate Jacobian for curved quadratic triangles.
375 static void Jacobian3(const std::array<double, 8>& xn,
376 const std::array<double, 8>& yn,
377 const double u, const double v, const double w,
378 double& det, double jac[4][4]);
379 /// Calculate Jacobian for curved quadratic quadrilaterals.
380 static void Jacobian5(const std::array<double, 8>& xn,
381 const std::array<double, 8>& yn,
382 const double u, const double v,
383 double& det, double jac[4][4]);
384 /// Calculate Jacobian for curved quadratic tetrahedra.
385 static void Jacobian13(const std::array<double, 10>& xn,
386 const std::array<double, 10>& yn,
387 const std::array<double, 10>& zn,
388 const double fourt0, const double fourt1,
389 const double fourt2, const double fourt3,
390 double& det, double jac[4][4]);
391 /// Calculate Jacobian for a cube.
392 void JacobianCube(const Element& element, const double t1, const double t2,
393 const double t3, TMatrixD*& jac,
394 std::vector<TMatrixD*>& dN) const;
395
396 static std::array<std::array<double, 3>, 4> Weights12(
397 const std::array<double, 10>& xn, const std::array<double, 10>& yn,
398 const std::array<double, 10>& zn);
399
400 /// Calculate the bounding boxes of all elements after initialization.
401 void CalculateElementBoundingBoxes();
402
403 /// Initialize the tetrahedral tree.
404 bool InitializeTetrahedralTree();
405
406};
407} // namespace Garfield
408
409#endif
virtual void GetAspectRatio(const size_t i, double &dmin, double &dmax) const
static double Potential3(const std::array< double, 6 > &v, const std::array< double, 3 > &t)
Interpolate the potential in a triangle.
int FindElement5(const double x, const double y, double &t1, double &t2, double &t3, double &t4, double jac[4][4], double &det) const
Find the element for a point in curved quadratic quadrilaterals.
double GetConductivity(const size_t imat) const
Return the conductivity of a field map material.
virtual bool GetNode(const size_t i, double &x, double &y, double &z) const
double DelayedWeightingPotential(double x, double y, double z, const double t, const std::string &label) override
double WeightingPotential(const double x, const double y, const double z, const std::string &label) override
void PrintRange()
Show x, y, z, V and angular ranges.
void PrintMaterials()
List all currently defined materials.
bool GetVoltageRange(double &vmin, double &vmax) override
Calculate the voltage range [V].
bool SetDefaultDriftMedium()
Find lowest epsilon, check for eps = 0, set default drift media flags.
std::map< std::string, WeightingFieldCopy > m_wfieldCopies
static void Field5(const std::array< double, 8 > &v, const std::array< double, 2 > &t, double jac[4][4], const double det, double &ex, double &ey)
Interpolate the field in a curved quadrilateral.
void TimeInterpolation(const double t, double &f0, double &f1, int &i0, int &i1)
Interpolation of potential between two time slices.
void NotDriftMedium(const size_t imat)
Flag a field map materials as a non-drift medium.
std::array< double, 3 > m_mapamin
void ElectricField(const double x, const double y, const double z, double &ex, double &ey, double &ez, Medium *&m, int &status) override
static double ReadDouble(char *token, double def, bool &error)
void EnableDeleteBackgroundElements(const bool on=true)
Option to eliminate mesh elements in conductors (default: on).
double GetPotential(const size_t i) const
virtual double GetElementVolume(const size_t i) const
int FindElement13(const double x, const double y, const double z, double &t1, double &t2, double &t3, double &t4, double jac[4][4], double &det) const
Find the element for a point in curved quadratic tetrahedra.
void DriftMedium(const size_t imat)
Flag a field map material as a drift medium.
void MapCoordinates(double &xpos, double &ypos, double &zpos, bool &xmirrored, bool &ymirrored, bool &zmirrored, double &rcoordinate, double &rotation) const
Move (xpos, ypos, zpos) to field map coordinates.
std::vector< std::array< double, 3 > > m_bbMax
void PrintWarning(const std::string &header)
std::array< double, 3 > m_maxBoundingBox
std::array< double, 3 > m_mapmin
void SetMedium(const size_t imat, Medium *medium)
Associate a field map material with a Medium object.
size_t GetNumberOfMaterials() const
Return the number of materials in the field map.
std::array< double, 3 > m_minBoundingBox
static double ScalingFactor(std::string unit)
double Potential(const double x, const double y, const double z, const std::vector< double > &potentials) const
Compute the electrostatic/weighting potential.
void UpdatePeriodicity() override
Verify periodicities.
double GetPermittivity(const size_t imat) const
Return the relative permittivity of a field map material.
bool GetBoundingBox(double &xmin, double &ymin, double &zmin, double &xmax, double &ymax, double &zmax) override
Get the bounding box coordinates.
static int ReadInteger(char *token, int def, bool &error)
std::vector< Material > m_materials
virtual size_t GetNumberOfElements() const
Return the number of mesh elements.
static void Field3(const std::array< double, 6 > &v, const std::array< double, 3 > &t, double jac[4][4], const double det, double &ex, double &ey)
Interpolate the field in a triangle.
virtual size_t GetNumberOfNodes() const
void UnmapFields(double &ex, double &ey, double &ez, const double xpos, const double ypos, const double zpos, const bool xmirrored, const bool ymirrored, const bool zmirrored, const double rcoordinate, const double rotation) const
Move (ex, ey, ez) to global coordinates.
static void Field13(const std::array< double, 10 > &v, const std::array< double, 4 > &t, double jac[4][4], const double det, double &ex, double &ey, double &ez)
Interpolate the field in a curved quadratic tetrahedron.
void WeightingField(const double x, const double y, const double z, double &wx, double &wy, double &wz, const std::string &label) override
static double Potential5(const std::array< double, 8 > &v, const std::array< double, 2 > &t)
Interpolate the potential in a curved quadrilateral.
void CopyWeightingPotential(const std::string &label, const std::string &labelSource, const double x, const double y, const double z, const double alpha, const double beta, const double gamma)
virtual ~ComponentFieldMap()
Destructor.
static double Potential13(const std::array< double, 10 > &v, const std::array< double, 4 > &t)
Interpolate the potential in a curved quadratic tetrahedron.
int Field(const double x, const double y, const double z, double &fx, double &fy, double &fz, int &iel, const std::vector< double > &potentials) const
Compute the electric/weighting field.
std::array< double, 3 > m_mapna
std::vector< std::array< std::array< double, 3 >, 4 > > m_w12
ComponentFieldMap()=delete
Default constructor.
void EnableTetrahedralTreeForElementSearch(const bool on=true)
bool GetElement(const size_t i, double &vol, double &dmin, double &dmax) const
Return the volume and aspect ratio of a mesh element.
void PrintCouldNotOpen(const std::string &header, const std::string &filename) const
std::map< std::string, std::vector< double > > m_wpot
bool Check()
Check element aspect ratio.
int FindElementCube(const double x, const double y, const double z, double &t1, double &t2, double &t3, TMatrixD *&jac, std::vector< TMatrixD * > &dN) const
Find the element for a point in a cube.
std::vector< double > m_wdtimes
std::vector< int > m_elementIndices
bool GetElementaryCell(double &xmin, double &ymin, double &zmin, double &xmax, double &ymax, double &zmax) override
Get the coordinates of the elementary cell.
std::vector< Element > m_elements
std::array< double, 3 > m_mapamax
std::array< double, 3 > m_cells
void EnableConvergenceWarnings(const bool on=true)
std::array< bool, 3 > m_setang
Medium * GetMedium(const size_t imat) const
Return the Medium associated to a field map material.
bool IsInBoundingBox(const double x, const double y, const double z) const
void PrintElement(const std::string &header, const double x, const double y, const double z, const double t1, const double t2, const double t3, const double t4, const size_t i, const std::vector< double > &potential) const
void EnableCheckMapIndices(const bool on=true)
std::array< double, 3 > m_mapmax
std::map< std::string, std::vector< std::vector< double > > > m_dwpot
void Reset() override
Reset the component.
std::vector< std::array< double, 3 > > m_bbMin
void PrintNotReady(const std::string &header) const
virtual void ElectricField(const double x, const double y, const double z, double &ex, double &ey, double &ez, Medium *&m, int &status)=0
virtual Medium * GetMedium(const double x, const double y, const double z)
Get the medium at a given location (x, y, z).
Definition Component.cc:24
Component()=delete
Default constructor.
Abstract base class for media.
Definition Medium.hh:16