EDepSimArbElecField.cc
Go to the documentation of this file.
1 // License and Disclaimer
2 //
3 // For use with software authored by the Geant4 Collaboration
4 //
5 // MIT License
6 //
7 // Copyright (c) 2020 Andrew Cudd
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining a
10 // copy of this software and associated documentation files (the "Software"),
11 // to deal in the Software without restriction, including without limitation
12 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 // and/or sell copies of the Software, and to permit persons to whom the
14 // Software is furnished to do so, subject to the following conditions:
15 //
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
18 //
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 // DEALINGS IN THE SOFTWARE.
26 //
27 //
28 // Class for storing and handling an arbitrary electric field.
29 //
30 // History:
31 // - 2020.04.14 A.Cudd created
32 // - 2020.07.28 C.McGrew updated license with permission of A.Cudd
33 //
34 // -------------------------------------------------------------------
35 
36 #include "EDepSimArbElecField.hh"
37 
39 {
40 }
41 
43 {
44  m_filename = fname;
45  std::fstream fin(fname, std::fstream::in);
46 
47  if(!fin.is_open())
48  {
49  EDepSimError("Can't read " << fname << std::endl);
50  return false;
51  }
52  else
53  {
54  EDepSimLog("Reading " << fname << " ...");
55  int xcount{-1}, ycount{-1}, zcount{-1};
56  double xcurr{0}, ycurr{0}, zcurr{0};
58 
59  while(std::getline(fin >> std::ws, line))
60  {
61  std::stringstream ss(line);
62 
63  if(ss.str().front() == '#')
64  continue;
65 
66  ss >> m_offset[0] >> m_offset[1] >> m_offset[2]
67  >> m_delta[0] >> m_delta[1] >> m_delta[2];
68  break;
69  }
70 
71  while(std::getline(fin >> std::ws, line))
72  {
73  double x{0}, y{0}, z{0}, fx{0}, fy{0}, fz{0}, f{0};
74  std::stringstream ss(line);
75 
76  if(ss.str().front() == '#')
77  continue;
78 
79  ss >> x >> y >> z >> fx >> fy >> fz >> f;
80 
81  if(std::abs(x - xcurr) > 0.0 || xcount < 0)
82  {
83  xcurr = x;
84  xcount += 1;
85  ycount = -1;
86  m_field_x.emplace_back(std::vector<std::vector<double>>{});
87  m_field_y.emplace_back(std::vector<std::vector<double>>{});
88  m_field_z.emplace_back(std::vector<std::vector<double>>{});
89  }
90 
91  if(std::abs(y - ycurr) > 0.0 || ycount < 0)
92  {
93  ycurr = y;
94  ycount += 1;
95  zcount = -1;
96  m_field_x[xcount].emplace_back(std::vector<double>{});
97  m_field_y[xcount].emplace_back(std::vector<double>{});
98  m_field_z[xcount].emplace_back(std::vector<double>{});
99  }
100 
101  m_field_x[xcount][ycount].push_back(fx * (volt/cm));
102  m_field_y[xcount][ycount].push_back(fy * (volt/cm));
103  m_field_z[xcount][ycount].push_back(fz * (volt/cm));
104 
105  if(std::abs(z - zcurr) > 0.0 || zcount < 0)
106  {
107  zcurr = z;
108  zcount += 1;
109  }
110  }
111  }
112 
113  return true;
114 }
115 
116 void EDepSim::ArbElecField::GetFieldValue(const G4double pos[4], G4double* field) const
117 {
118  const double x = pos[0];
119  const double y = pos[1];
120  const double z = pos[2];
121 
122  EDepSim::Cubic interp;
123  field[3] = interp.interpolate(x, y, z, m_field_x, m_delta[0], m_delta[1], m_delta[2], m_offset[0], m_offset[1], m_offset[2]);
124  field[4] = interp.interpolate(x, y, z, m_field_y, m_delta[0], m_delta[1], m_delta[2], m_offset[0], m_offset[1], m_offset[2]);
125  field[5] = interp.interpolate(x, y, z, m_field_z, m_delta[0], m_delta[1], m_delta[2], m_offset[0], m_offset[1], m_offset[2]);
126 }
127 
129 {
130  EDepSimLog("Printing values for electric field.");
131  EDepSimLog("m_filename : " << m_filename
132  << "\nm_offset : " << m_offset[0] << ", " << m_offset[1] << ", " << m_offset[2]
133  << "\nm_delta : " << m_delta[0] << ", " << m_delta[1] << ", " << m_delta[2]);
134 }
static constexpr double cm
Definition: Units.h:68
#define EDepSimLog(outStream)
Definition: EDepSimLog.hh:717
std::string string
Definition: nybbler.cc:12
struct vector vector
std::array< double, 3 > m_delta
std::vector< std::vector< std::vector< double > > > m_field_z
T abs(T value)
std::vector< std::vector< std::vector< double > > > m_field_y
volt_as<> volt
Type of potential stored in volts, in double precision.
virtual void GetFieldValue(const G4double pos[4], G4double *field) const
#define EDepSimError(outStream)
Definition: EDepSimLog.hh:503
void line(double t, double *p, double &x, double &y, double &z)
double interpolate(const double *point, const std::vector< std::vector< std::vector< double >>> &g, const double *delta, const double *offset) const
std::array< double, 3 > m_offset
bool ReadFile(const std::string &fname)
list x
Definition: train.py:276
QTextStream & endl(QTextStream &s)
std::vector< std::vector< std::vector< double > > > m_field_x