Garfield++ 4.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 <memory>
7#include <vector>
8#include "Component.hh"
9#include "TMatrixD.h"
10#include "TetrahedralTree.hh"
11
12namespace Garfield {
13
14/// Base class for components based on finite-element field maps.
15
17 public:
18 /// Default constructor.
20 /// Constructor
21 ComponentFieldMap(const std::string& name);
22 /// Destructor
23 virtual ~ComponentFieldMap();
24
25 /// Calculate x, y, z, V and angular ranges.
26 virtual void SetRange();
27 /// Show x, y, z, V and angular ranges
28 void PrintRange();
29
30 bool IsInBoundingBox(const double x, const double y, const double z) const {
31 return x >= m_minBoundingBox[0] && x <= m_maxBoundingBox[0] &&
32 y >= m_minBoundingBox[1] && y <= m_maxBoundingBox[1] &&
33 z >= m_minBoundingBox[2] && y <= m_maxBoundingBox[2];
34 }
35
36 bool GetBoundingBox(double& xmin, double& ymin, double& zmin,
37 double& xmax, double& ymax, double& zmax) override;
38 bool GetElementaryCell(double& xmin, double& ymin, double& zmin,
39 double& xmax, double& ymax, double& zmax) override;
40
41 bool GetVoltageRange(double& vmin, double& vmax) override {
42 vmin = m_mapvmin;
43 vmax = m_mapvmax;
44 return true;
45 }
46
47 /// List all currently defined materials
48 void PrintMaterials();
49 /// Flag a field map material as a drift medium.
50 void DriftMedium(const unsigned int imat);
51 /// Flag a field map materials as a non-drift medium.
52 void NotDriftMedium(const unsigned int imat);
53 /// Return the number of materials in the field map.
54 size_t GetNumberOfMaterials() const { return m_materials.size(); }
55 /// Return the permittivity of a field map material.
56 double GetPermittivity(const unsigned int imat) const;
57 /// Return the conductivity of a field map material.
58 double GetConductivity(const unsigned int imat) const;
59 /// Associate a field map material with a Medium class.
60 void SetMedium(const unsigned int imat, Medium* medium);
61 /// Return the Medium associated to a field map material.
62 Medium* GetMedium(const unsigned int i) const;
64
65 /// Return the number of mesh elements.
66 virtual size_t GetNumberOfElements() const { return m_elements.size(); }
67 /// Return the volume and aspect ratio of a mesh element.
68 bool GetElement(const size_t i, double& vol, double& dmin, double& dmax);
69 /// Return the material and node indices of a mesh element.
70 virtual bool GetElement(const size_t i, size_t& mat, bool& drift,
71 std::vector<size_t>& nodes) const;
72 virtual size_t GetNumberOfNodes() const { return m_nodes.size(); }
73 virtual bool GetNode(const size_t i, double& x, double& y, double& z) const;
74
75 // Options
76 void EnableCheckMapIndices(const bool on = true) {
77 m_checkMultipleElement = on;
78 if (on) m_lastElement = -1;
79 }
80 /// Option to eliminate mesh elements in conductors (default: on).
81 void EnableDeleteBackgroundElements(const bool on = true) {
83 }
84
85 /// Enable or disable the usage of the tetrahedral tree
86 /// for searching the element in the mesh.
87 void EnableTetrahedralTreeForElementSearch(const bool on = true) {
88 m_useTetrahedralTree = on;
89 }
90
91 /// Enable or disable warnings that the calculation of the local
92 /// coordinates did not achieve the requested precision.
93 void EnableConvergenceWarnings(const bool on = true) {
95 }
96
97 friend class ViewFEMesh;
98
99 protected:
100 bool m_is3d = true;
101
102 // Elements
103 struct Element {
104 // Nodes
105 int emap[10];
106 // Material
107 unsigned int matmap;
109 // Bounding box of the element
110 std::array<float, 3> bbMin;
111 std::array<float, 3> bbMax;
112 };
113 std::vector<Element> m_elements;
114
115 // Nodes
116 struct Node {
117 // Coordinates
118 double x, y, z;
119 // Potential
120 double v;
121 // Weighting potentials
122 std::vector<double> w;
123 // Delayed weighting potentials
124 std::vector<std::vector<double>> dw;
125 };
126 std::vector<Node> m_nodes;
127
128 // Materials
129 struct Material {
130 // Permittivity
131 double eps;
132 // Resistivity
133 double ohm;
135 // Associated medium
137 };
138 std::vector<Material> m_materials;
139
140 std::vector<std::string> m_wfields;
141 std::vector<bool> m_wfieldsOk;
142 std::vector<bool> m_dwfieldsOk;
143
144 std::vector<double> m_wdtimes;
145
146 // Bounding box
147 bool m_hasBoundingBox = false;
148 std::array<double, 3> m_minBoundingBox;
149 std::array<double, 3> m_maxBoundingBox;
150
151 // Ranges and periodicities
152 std::array<double, 3> m_mapmin;
153 std::array<double, 3> m_mapmax;
154 std::array<double, 3> m_mapamin;
155 std::array<double, 3> m_mapamax;
156 std::array<double, 3> m_mapna;
157 std::array<double, 3> m_cells;
158
159 double m_mapvmin = 0.;
160 double m_mapvmax = 0.;
161
162 std::array<bool, 3> m_setang;
163
164 // Option to delete meshing in conductors
166
167 // Warnings flag
168 bool m_warning = false;
169 unsigned int m_nWarnings = 0;
170
171 // Print warnings about failed convergence when refining
172 // isoparametric coordinates.
174
175 // Get the scaling factor for a given length unit.
176 static double ScalingFactor(std::string unit);
177
178 // Reset the component.
179 void Reset() override;
180
181 void Prepare();
182
183 // Periodicities
184 void UpdatePeriodicity2d();
186
187 /// Find lowest epsilon, check for eps = 0, set default drift media flags.
189
190 /// Find the element for a point in curved quadratic quadrilaterals.
191 int FindElement5(const double x, const double y, const double z, double& t1,
192 double& t2, double& t3, double& t4, double jac[4][4],
193 double& det);
194 /// Find the element for a point in curved quadratic tetrahedra.
195 int FindElement13(const double x, const double y, const double z, double& t1,
196 double& t2, double& t3, double& t4, double jac[4][4],
197 double& det);
198 /// Find the element for a point in a cube.
199 int FindElementCube(const double x, const double y, const double z,
200 double& t1, double& t2, double& t3, TMatrixD*& jac,
201 std::vector<TMatrixD*>& dN);
202
203 /// Move (xpos, ypos, zpos) to field map coordinates.
204 void MapCoordinates(double& xpos, double& ypos, double& zpos, bool& xmirrored,
205 bool& ymirrored, bool& zmirrored, double& rcoordinate,
206 double& rotation) const;
207 /// Move (ex, ey, ez) to global coordinates.
208 void UnmapFields(double& ex, double& ey, double& ez, double& xpos,
209 double& ypos, double& zpos, bool& xmirrored, bool& ymirrored,
210 bool& zmirrored, double& rcoordinate,
211 double& rotation) const;
212
213 int ReadInteger(char* token, int def, bool& error);
214 double ReadDouble(char* token, double def, bool& error);
215
216 virtual double GetElementVolume(const unsigned int i) = 0;
217 virtual void GetAspectRatio(const unsigned int i, double& dmin,
218 double& dmax) = 0;
219
220 size_t GetWeightingFieldIndex(const std::string& label) const;
221 size_t GetOrCreateWeightingFieldIndex(const std::string& label);
222
223 void PrintWarning(const std::string& header);
224 void PrintNotReady(const std::string& header) const;
225 void PrintCouldNotOpen(const std::string& header,
226 const std::string& filename) const;
227 void PrintElement(const std::string& header, const double x, const double y,
228 const double z, const double t1, const double t2,
229 const double t3, const double t4, const Element& element,
230 const unsigned int n, const int iw = -1) const;
231
232 private:
233 /// Scan for multiple elements that contain a point
234 bool m_checkMultipleElement = false;
235
236 // Tetrahedral tree
237 bool m_useTetrahedralTree = true;
238 std::unique_ptr<TetrahedralTree> m_octree;
239
240 /// Flag to check if bounding boxes of elements are cached
241 bool m_cacheElemBoundingBoxes = false;
242
243 /// Keep track of the last element found.
244 int m_lastElement = -1;
245
246 /// Calculate local coordinates for curved quadratic triangles.
247 int Coordinates3(double x, double y, double z, double& t1, double& t2,
248 double& t3, double& t4, double jac[4][4], double& det,
249 const Element& element) const;
250 /// Calculate local coordinates for linear quadrilaterals.
251 int Coordinates4(const double x, const double y, const double z, double& t1,
252 double& t2, double& t3, double& t4, double& det,
253 const Element& element) const;
254 /// Calculate local coordinates for curved quadratic quadrilaterals.
255 int Coordinates5(const double x, const double y, const double z, double& t1,
256 double& t2, double& t3, double& t4, double jac[4][4],
257 double& det, const Element& element) const;
258 /// Calculate local coordinates in linear tetrahedra.
259 void Coordinates12(const double x, const double y, const double z, double& t1,
260 double& t2, double& t3, double& t4,
261 const Element& element) const;
262 /// Calculate local coordinates for curved quadratic tetrahedra.
263 int Coordinates13(const double x, const double y, const double z, double& t1,
264 double& t2, double& t3, double& t4, double jac[4][4],
265 double& det, const Element& element) const;
266 /// Calculate local coordinates for a cube.
267 int CoordinatesCube(const double x, const double y, const double z,
268 double& t1, double& t2, double& t3, TMatrixD*& jac,
269 std::vector<TMatrixD*>& dN, const Element& element) const;
270
271 /// Calculate Jacobian for curved quadratic triangles.
272 void Jacobian3(const Element& element, const double u, const double v,
273 const double w, double& det, double jac[4][4]) const;
274 /// Calculate Jacobian for curved quadratic quadrilaterals.
275 void Jacobian5(const Element& element, const double u, const double v,
276 double& det, double jac[4][4]) const;
277 /// Calculate Jacobian for curved quadratic tetrahedra.
278 void Jacobian13(const Element& element, const double t, const double u,
279 const double v, const double w, double& det,
280 double jac[4][4]) const;
281 /// Calculate Jacobian for a cube.
282 void JacobianCube(const Element& element, const double t1, const double t2,
283 const double t3, TMatrixD*& jac,
284 std::vector<TMatrixD*>& dN) const;
285
286 /// Calculate the bounding boxes of all elements after initialization.
287 void CalculateElementBoundingBoxes();
288
289 /// Initialize the tetrahedral tree.
290 bool InitializeTetrahedralTree();
291};
292}
293
294#endif
Base class for components based on finite-element field maps.
void DriftMedium(const unsigned int imat)
Flag a field map material as a drift medium.
virtual bool GetNode(const size_t i, double &x, double &y, double &z) const
void PrintRange()
Show x, y, z, V and angular ranges.
virtual double GetElementVolume(const unsigned int i)=0
void SetMedium(const unsigned int imat, Medium *medium)
Associate a field map material with a Medium class.
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::array< double, 3 > m_mapamin
double ReadDouble(char *token, double def, bool &error)
void EnableDeleteBackgroundElements(const bool on=true)
Option to eliminate mesh elements in conductors (default: on).
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.
void PrintWarning(const std::string &header)
virtual void GetAspectRatio(const unsigned int i, double &dmin, double &dmax)=0
std::array< double, 3 > m_maxBoundingBox
std::array< double, 3 > m_mapmin
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)
bool GetBoundingBox(double &xmin, double &ymin, double &zmin, double &xmax, double &ymax, double &zmax) override
Get the bounding box coordinates.
int ReadInteger(char *token, int def, bool &error)
std::vector< Material > m_materials
virtual size_t GetNumberOfElements() const
Return the number of mesh elements.
virtual size_t GetNumberOfNodes() const
int FindElementCube(const double x, const double y, const double z, double &t1, double &t2, double &t3, TMatrixD *&jac, std::vector< TMatrixD * > &dN)
Find the element for a point in a cube.
std::vector< bool > m_wfieldsOk
virtual ~ComponentFieldMap()
Destructor.
size_t GetWeightingFieldIndex(const std::string &label) const
std::array< double, 3 > m_mapna
double GetConductivity(const unsigned int imat) const
Return the conductivity of a field map material.
ComponentFieldMap()=delete
Default constructor.
void EnableTetrahedralTreeForElementSearch(const bool on=true)
std::vector< bool > m_dwfieldsOk
void PrintCouldNotOpen(const std::string &header, const std::string &filename) const
int FindElement5(const double x, const double y, const double z, double &t1, double &t2, double &t3, double &t4, double jac[4][4], double &det)
Find the element for a point in curved quadratic quadrilaterals.
double GetPermittivity(const unsigned int imat) const
Return the permittivity of a field map material.
std::vector< double > m_wdtimes
std::vector< std::string > m_wfields
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)
Find the element for a point in curved quadratic tetrahedra.
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
Medium * GetMedium(const unsigned int i) const
Return the Medium associated to a field map material.
void NotDriftMedium(const unsigned int imat)
Flag a field map materials as a non-drift medium.
std::array< double, 3 > m_mapamax
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 Element &element, const unsigned int n, const int iw=-1) const
std::array< double, 3 > m_cells
void EnableConvergenceWarnings(const bool on=true)
std::array< bool, 3 > m_setang
virtual void SetRange()
Calculate x, y, z, V and angular ranges.
bool GetElement(const size_t i, double &vol, double &dmin, double &dmax)
Return the volume and aspect ratio of a mesh element.
bool IsInBoundingBox(const double x, const double y, const double z) const
void EnableCheckMapIndices(const bool on=true)
std::array< double, 3 > m_mapmax
void Reset() override
Reset the component.
void UnmapFields(double &ex, double &ey, double &ez, double &xpos, double &ypos, double &zpos, bool &xmirrored, bool &ymirrored, bool &zmirrored, double &rcoordinate, double &rotation) const
Move (ex, ey, ez) to global coordinates.
void PrintNotReady(const std::string &header) const
size_t GetOrCreateWeightingFieldIndex(const std::string &label)
Abstract base class for components.
Definition: Component.hh:13
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:22
Abstract base class for media.
Definition: Medium.hh:13
Draw the mesh of a field-map component.
Definition: ViewFEMesh.hh:21
std::vector< std::vector< double > > dw