Garfield++ 4.0
A toolkit for the detailed simulation of particle detectors based on ionisation measurement in gases and semiconductors
Loading...
Searching...
No Matches
ComponentGrid.hh
Go to the documentation of this file.
1#ifndef G_COMPONENT_GRID_H
2#define G_COMPONENT_GRID_H
3
4#include <vector>
5
6#include "Component.hh"
7
8namespace Garfield {
9
10/// Component for interpolating field maps on a regular mesh.
11
12class ComponentGrid : public Component {
13 public:
14 /// Constructor
16 /// Destructor
18
19 void Clear() override { Reset(); }
20
21 void ElectricField(const double x, const double y, const double z, double& ex,
22 double& ey, double& ez, double& v, Medium*& m,
23 int& status) override;
24 void ElectricField(const double x, const double y, const double z, double& ex,
25 double& ey, double& ez, Medium*& m, int& status) override;
26
27 void WeightingField(const double x, const double y, const double z,
28 double& wx, double& wy, double& wz,
29 const std::string& label) override;
30 double WeightingPotential(const double x, const double y, const double z,
31 const std::string& label) override;
32 void DelayedWeightingField(const double x, const double y, const double z,
33 const double t, double& wx, double& wy, double& wz,
34 const std::string& label) override;
35
36 void MagneticField(const double x, const double y, const double z, double& bx,
37 double& by, double& bz, int& status) override;
38
39 Medium* GetMedium(const double x, const double y, const double z) override;
40
41 bool GetVoltageRange(double& vmin, double& vmax) override;
42 bool GetElectricFieldRange(double& exmin, double& exmax, double& eymin,
43 double& eymax, double& ezmin, double& ezmax);
44 bool GetBoundingBox(double& xmin, double& ymin, double& zmin,
45 double& xmax, double& ymax, double& zmax) override;
46 bool GetElementaryCell(double& xmin, double& ymin, double& zmin,
47 double& xmax, double& ymax, double& zmax) override;
48
49 /** Define the grid.
50 * \param nx,ny,nz number of nodes along \f$x, y, z\f$.
51 * \param xmin,xmax range along \f$x\f$.
52 * \param ymin,ymax range along \f$y\f$.
53 * \param zmin,zmax range along \f$z\f$.
54 */
55 bool SetMesh(const unsigned int nx, const unsigned int ny,
56 const unsigned int nz, const double xmin, const double xmax,
57 const double ymin, const double ymax, const double zmin,
58 const double zmax);
59 /// Retrieve the parameters of the grid.
60 bool GetMesh(unsigned int& nx, unsigned int& ny, unsigned int& nz,
61 double& xmin, double& xmax, double& ymin, double& ymax,
62 double& zmin, double& zmax) const;
63 /** Import electric field and potential values from a file.
64 * The file is supposed to contain one line for each grid point starting with
65 * - either two or three floating point numbers,
66 * specifying the coordinates (in cm) of the grid node or
67 * - two or three integers specifying the index of the node,
68 *
69 * followed by
70 * - two or three floating point numbers for the electric field (in V/cm),
71 * and (depending on the value of withPotential and withFlag),
72 * - a floating point number specifying the potential (in V), and
73 * - an integer flag indicating whether the point is in an active region (1)
74 * or not (0).
75 *
76 * Format types are:
77 * - "xy", "xyz": nodes are specified by their coordinates
78 * - "ij", "ijk": nodes are specified by their indices
79 */
80 bool LoadElectricField(const std::string& filename, const std::string& format,
81 const bool withPotential, const bool withFlag,
82 const double scaleX = 1., const double scaleE = 1.,
83 const double scaleP = 1.);
84
85 /// Import (prompt) weighting field from file.
86 bool LoadWeightingField(const std::string& filename,
87 const std::string& format, const bool withPotential,
88 const double scaleX = 1., const double scaleE = 1.,
89 const double scaleP = 1.);
90 /// Import delayed weighting field from file.
91 bool LoadWeightingField(const std::string& filename,
92 const std::string& format, const double time,
93 const bool withPotential, const double scaleX = 1.,
94 const double scaleE = 1., const double scaleP = 1.);
95 /// Offset coordinates in the weighting field, such that the
96 /// same numerical weighting field map can be used for electrodes at
97 /// different positions.
98 void SetWeightingFieldOffset(const double x, const double y, const double z);
99
100 /// Import magnetic field values from a file.
101 bool LoadMagneticField(const std::string& filename, const std::string& format,
102 const double scaleX = 1., const double scaleB = 1.);
103
104 /** Export the electric field and potential of a component to a text file.
105 * \param cmp Component object for which to export the field/potential
106 * \param filename name of the text file
107 * \param format "xy", "xyz", "ij" or "ijk", see @ref LoadElectricField
108 */
109 bool SaveElectricField(Component* cmp, const std::string& filename,
110 const std::string& format);
111 /** Export the weighting field and potential of a component to a text file.
112 * \param cmp Component object for which to export the field/potential
113 * \param id identifier of the weighting field
114 * \param filename name of the text file
115 * \param format "xy", "xyz", "ij" or "ijk", see @ref LoadElectricField
116 */
117 bool SaveWeightingField(Component* cmp, const std::string& id,
118 const std::string& filename,
119 const std::string& format);
120
121 /// Return the field at a given node.
122 bool GetElectricField(const unsigned int i, const unsigned int j,
123 const unsigned int k, double& v, double& ex, double& ey,
124 double& ez) const;
125
126 /// Set the medium.
127 void SetMedium(Medium* m);
128 /// Get the medium.
129 Medium* GetMedium() const { return m_medium; }
130
131 /// Print information about the mesh and the available data.
132 void Print();
133
134 /** Import electron attachment coefficients from a file.
135 * \param fname name of the text file.
136 * \param fmt format string ("XY", "XYZ", "IJ", "IJK").
137 * \param col column in the file which has the attachment coefficient.
138 * \param scaleX scaling factor to be applied to the coordinates.
139 */
140 bool LoadElectronAttachment(const std::string& fname,
141 const std::string& fmt,
142 const unsigned int col,
143 const double scaleX = 1.);
144 /// Import hole attachment coefficients from a file.
145 bool LoadHoleAttachment(const std::string& fname,
146 const std::string& fmt,
147 const unsigned int col,
148 const double scaleX = 1.);
149
150 bool HasAttachmentMap() const override {
151 return !(m_eAttachment.empty() && m_hAttachment.empty());
152 }
153 bool ElectronAttachment(const double x, const double y, const double z,
154 double& att) override;
155 bool HoleAttachment(const double x, const double y, const double z,
156 double& att) override;
157
158 /** Import a map of electron drift velocities from a file.
159 * \param fname name of the text file.
160 * \param fmt format string ("XY", "XYZ", "IJ", "IJK").
161 * \param scaleX scaling factor to be applied to the coordinates.
162 * \param scaleV scaling factor to be applied to the velocity components.
163 */
164 bool LoadElectronVelocity(const std::string& fname,
165 const std::string& fmt,
166 const double scaleX = 1.,
167 const double scaleV = 1.e-9);
168 /// Import a map of hole drift velocities from a file.
169 bool LoadHoleVelocity(const std::string& fname,
170 const std::string& fmt,
171 const double scaleX = 1.,
172 const double scaleV = 1.e-9);
173
174 bool HasVelocityMap() const override {
175 return !(m_eVelocity.empty() && m_hVelocity.empty());
176 }
177 bool ElectronVelocity(const double x, const double y, const double z,
178 double& vx, double& vy, double& vz) override;
179 bool HoleVelocity(const double x, const double y, const double z,
180 double& vx, double& vy, double& vz) override;
181 private:
182 enum class Format {
183 Unknown,
184 XY,
185 XYZ,
186 IJ,
187 IJK,
188 YXZ
189 };
190
191 Medium* m_medium = nullptr;
192 struct Node {
193 double fx, fy, fz; ///< Field
194 double v; ///< Potential
195 };
196
197 /// Electric field values and potentials.
198 std::vector<std::vector<std::vector<Node> > > m_efields;
199 /// Magnetic field values.
200 std::vector<std::vector<std::vector<Node> > > m_bfields;
201 /// Prompt weighting field values and potentials.
202 std::vector<std::vector<std::vector<Node> > > m_wfields;
203 /// Delayed weighting field values and potentials.
204 std::vector<std::vector<std::vector<std::vector<Node> > > > m_wdfields;
205 std::vector<double> m_wdtimes;
206 /// Attachment maps for electrons and holes.
207 std::vector<std::vector<std::vector<double> > > m_eAttachment;
208 std::vector<std::vector<std::vector<double> > > m_hAttachment;
209 /// Velocity maps for electrons and holes.
210 std::vector<std::vector<std::vector<Node> > > m_eVelocity;
211 std::vector<std::vector<std::vector<Node> > > m_hVelocity;
212 /// Active medium flag.
213 std::vector<std::vector<std::vector<bool> > > m_active;
214
215 // Dimensions of the mesh
216 std::array<unsigned int, 3> m_nX = {{1, 1, 1}};
217 std::array<double, 3> m_xMin = {{0., 0., 0.}};
218 std::array<double, 3> m_xMax = {{0., 0., 0.}};
219 std::array<double, 3> m_sX = {{0., 0., 0.}};
220
221 bool m_hasMesh = false;
222 bool m_hasPotential = false;
223
224 // Offset for weighting field
225 std::array<double, 3> m_wFieldOffset = {{0., 0., 0.}};
226
227 // Voltage range
228 double m_pMin = 0., m_pMax = 0.;
229
230 /// Read/determine mesh parameters from file.
231 bool LoadMesh(const std::string& filename, std::string format,
232 const double scaleX);
233
234 /// Read electric field and potential from file.
235 bool LoadData(const std::string& filename, std::string format,
236 const bool withPotential, const bool withFlag,
237 const double scaleX, const double scaleF, const double scaleP,
238 std::vector<std::vector<std::vector<Node> > >& field);
239 /// Load scalar data (e. g. attachment coefficients) from file.
240 bool LoadData(const std::string& filename, std::string format,
241 const double scaleX,
242 std::vector<std::vector<std::vector<double> > >& tab,
243 const unsigned int col);
244
245 void Reset() override;
246 void UpdatePeriodicity() override;
247
248 /// Interpolation of the field and potential at a given point.
249 bool GetField(const double x, const double y, const double z,
250 const std::vector<std::vector<std::vector<Node> > >& field,
251 double& fx, double& fy, double& fz, double& p, bool& active);
252 /// Interpolation in a table of scalars.
253 bool GetData(const double x, const double y, const double z,
254 const std::vector<std::vector<std::vector<double> > >& table,
255 double& value);
256
257 /// Reduce a coordinate to the basic cell (in case of periodicity).
258 double Reduce(const double xin, const double xmin, const double xmax,
259 const bool simplePeriodic, const bool mirrorPeriodic,
260 bool& isMirrored) const;
261 /// Set the dimensions of a table according to the mesh.
262 void Initialise(std::vector<std::vector<std::vector<Node> > >& fields);
263 /// Decode a format string.
264 Format GetFormat(std::string fmt);
265};
266} // namespace Garfield
267#endif
Component for interpolating field maps on a regular mesh.
bool ElectronVelocity(const double x, const double y, const double z, double &vx, double &vy, double &vz) override
Get the electron drift velocity.
bool LoadElectronVelocity(const std::string &fname, const std::string &fmt, const double scaleX=1., const double scaleV=1.e-9)
~ComponentGrid()
Destructor.
bool HasVelocityMap() const override
Does the component have velocity maps?
bool SaveElectricField(Component *cmp, const std::string &filename, const std::string &format)
bool SetMesh(const unsigned int nx, const unsigned int ny, const unsigned int nz, const double xmin, const double xmax, const double ymin, const double ymax, const double zmin, const double zmax)
bool GetElementaryCell(double &xmin, double &ymin, double &zmin, double &xmax, double &ymax, double &zmax) override
Get the coordinates of the elementary cell.
bool GetBoundingBox(double &xmin, double &ymin, double &zmin, double &xmax, double &ymax, double &zmax) override
Get the bounding box coordinates.
ComponentGrid()
Constructor.
bool LoadElectricField(const std::string &filename, const std::string &format, const bool withPotential, const bool withFlag, const double scaleX=1., const double scaleE=1., const double scaleP=1.)
void DelayedWeightingField(const double x, const double y, const double z, const double t, double &wx, double &wy, double &wz, const std::string &label) override
bool GetMesh(unsigned int &nx, unsigned int &ny, unsigned int &nz, double &xmin, double &xmax, double &ymin, double &ymax, double &zmin, double &zmax) const
Retrieve the parameters of the grid.
double WeightingPotential(const double x, const double y, const double z, const std::string &label) override
void Print()
Print information about the mesh and the available data.
bool GetVoltageRange(double &vmin, double &vmax) override
Calculate the voltage range [V].
void SetMedium(Medium *m)
Set the medium.
Medium * GetMedium() const
Get the medium.
bool LoadMagneticField(const std::string &filename, const std::string &format, const double scaleX=1., const double scaleB=1.)
Import magnetic field values from a file.
bool GetElectricField(const unsigned int i, const unsigned int j, const unsigned int k, double &v, double &ex, double &ey, double &ez) const
Return the field at a given node.
bool HoleVelocity(const double x, const double y, const double z, double &vx, double &vy, double &vz) override
Get the hole drift velocity.
bool LoadWeightingField(const std::string &filename, const std::string &format, const bool withPotential, const double scaleX=1., const double scaleE=1., const double scaleP=1.)
Import (prompt) weighting field from file.
void ElectricField(const double x, const double y, const double z, double &ex, double &ey, double &ez, double &v, Medium *&m, int &status) override
Calculate the drift field [V/cm] and potential [V] at (x, y, z).
bool LoadHoleVelocity(const std::string &fname, const std::string &fmt, const double scaleX=1., const double scaleV=1.e-9)
Import a map of hole drift velocities from a file.
void Clear() override
Reset.
bool HoleAttachment(const double x, const double y, const double z, double &att) override
Get the hole attachment coefficient.
bool GetElectricFieldRange(double &exmin, double &exmax, double &eymin, double &eymax, double &ezmin, double &ezmax)
void MagneticField(const double x, const double y, const double z, double &bx, double &by, double &bz, int &status) override
bool ElectronAttachment(const double x, const double y, const double z, double &att) override
Get the electron attachment coefficient.
void SetWeightingFieldOffset(const double x, const double y, const double z)
bool LoadHoleAttachment(const std::string &fname, const std::string &fmt, const unsigned int col, const double scaleX=1.)
Import hole attachment coefficients from a file.
bool SaveWeightingField(Component *cmp, const std::string &id, const std::string &filename, const std::string &format)
bool HasAttachmentMap() const override
Does the component have attachment maps?
void WeightingField(const double x, const double y, const double z, double &wx, double &wy, double &wz, const std::string &label) override
bool LoadElectronAttachment(const std::string &fname, const std::string &fmt, const unsigned int col, const double scaleX=1.)
Abstract base class for components.
Definition: Component.hh:13
Abstract base class for media.
Definition: Medium.hh:13