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