19 double yOffset,
double zOffset)
29 ifstream file(filename);
33 EXCEPTION_RAISE(
"FileDNE",
"The field map file '" + std::string(filename) +
37 G4cout <<
"-----------------------------------------------------------"
39 G4cout <<
" Magnetic Field Map 3D" << G4endl;
40 G4cout <<
"-----------------------------------------------------------"
43 G4cout <<
"Reading the field grid from " << filename <<
" ... " << endl;
44 G4cout <<
" Offsets: " << xOffset <<
" " << yOffset <<
" " << zOffset
49 file.getline(buffer, 256);
52 file >> nx_ >> ny_ >> nz_;
54 G4cout <<
" Number of values: " << nx_ <<
" " << ny_ <<
" " << nz_ << G4endl;
61 for (ix = 0; ix < nx_; ix++) {
62 xField_[ix].resize(ny_);
63 yField_[ix].resize(ny_);
64 zField_[ix].resize(ny_);
65 for (iy = 0; iy < ny_; iy++) {
66 xField_[ix][iy].resize(nz_);
67 yField_[ix][iy].resize(nz_);
68 zField_[ix][iy].resize(nz_);
76 file.getline(buffer, 256);
77 }
while (buffer[1] !=
'0');
80 double xval, yval, zval, bx, by, bz;
81 for (ix = 0; ix < nx_; ix++) {
82 for (iy = 0; iy < ny_; iy++) {
83 for (iz = 0; iz < nz_; iz++) {
84 file >> xval >> yval >> zval >> bx >> by >> bz;
85 if (ix == 0 && iy == 0 && iz == 0) {
90 xField_[ix][iy][iz] = bx;
91 yField_[ix][iy][iz] = by;
92 zField_[ix][iy][iz] = bz;
102 G4cout <<
" ... done reading " << G4endl << G4endl;
103 G4cout <<
"Read values of field from file " << filename << G4endl;
104 G4cout <<
" Assumed the order: x, y, z, Bx, By, Bz" << G4endl;
105 G4cout <<
" Min values: " << minx_ <<
" " << miny_ <<
" " << minz_ <<
" mm "
107 G4cout <<
" Max values: " << maxx_ <<
" " << maxy_ <<
" " << maxz_ <<
" mm "
109 G4cout <<
" Field offsets: " << xOffset_ <<
" " << yOffset_ <<
" "
110 << zOffset_ <<
" mm " << G4endl << G4endl;
126 G4cout <<
"After reordering if necessary" << G4endl;
127 G4cout <<
" Min values: " << minx_ <<
" " << miny_ <<
" " << minz_ <<
" mm "
129 G4cout <<
" Max values: " << maxx_ <<
" " << maxy_ <<
" " << maxz_ <<
" mm "
137 G4cout <<
" Range of values: " << dx_ <<
" " << dy_ <<
" " << dz_ <<
" mm"
139 G4cout <<
"Done loading field map" << G4endl << G4endl;
140 G4cout <<
"-----------------------------------------------------------"
145 double* bfield)
const {
146 double x = point[0] - xOffset_;
147 double y = point[1] - yOffset_;
148 double z = point[2] - zOffset_;
152 if (x >= minx_ && x < maxx_ - eps && y >= miny_ && y < maxy_ - eps &&
153 z >= minz_ && z < maxz_ - eps) {
156 double xfraction = (x - minx_) / dx_;
157 double yfraction = (y - miny_) / dy_;
158 double zfraction = (z - minz_) / dz_;
161 xfraction = 1 - xfraction;
164 yfraction = 1 - yfraction;
167 zfraction = 1 - zfraction;
172 double xdindex, ydindex, zdindex;
176 double xlocal = (std::modf(xfraction * (nx_ - 1), &xdindex));
177 double ylocal = (std::modf(yfraction * (ny_ - 1), &ydindex));
178 double zlocal = (std::modf(zfraction * (nz_ - 1), &zdindex));
182 int xindex =
static_cast<int>(xdindex);
183 int yindex =
static_cast<int>(ydindex);
184 int zindex =
static_cast<int>(zdindex);
186#ifdef DEBUG_INTERPOLATING_FIELD
187 G4cout <<
"Local x,y,z: " << xlocal <<
" " << ylocal <<
" " << zlocal
189 G4cout <<
"Index x,y,z: " << xindex <<
" " << yindex <<
" " << zindex
191 double valx0z0, mulx0z0, valx1z0, mulx1z0;
192 double valx0z1, mulx0z1, valx1z1, mulx1z1;
193 valx0z0 = table[xindex][0][zindex];
194 mulx0z0 = (1 - xlocal) * (1 - zlocal);
195 valx1z0 = table[xindex + 1][0][zindex];
196 mulx1z0 = xlocal * (1 - zlocal);
197 valx0z1 = table[xindex][0][zindex + 1];
198 mulx0z1 = (1 - xlocal) * zlocal;
199 valx1z1 = table[xindex + 1][0][zindex + 1];
200 mulx1z1 = xlocal * zlocal;
205 xField_[xindex][yindex][zindex] * (1 - xlocal) * (1 - ylocal) *
207 xField_[xindex][yindex][zindex + 1] * (1 - xlocal) * (1 - ylocal) *
209 xField_[xindex][yindex + 1][zindex] * (1 - xlocal) * ylocal *
211 xField_[xindex][yindex + 1][zindex + 1] * (1 - xlocal) * ylocal *
213 xField_[xindex + 1][yindex][zindex] * xlocal * (1 - ylocal) *
215 xField_[xindex + 1][yindex][zindex + 1] * xlocal * (1 - ylocal) *
217 xField_[xindex + 1][yindex + 1][zindex] * xlocal * ylocal *
219 xField_[xindex + 1][yindex + 1][zindex + 1] * xlocal * ylocal * zlocal;
221 yField_[xindex][yindex][zindex] * (1 - xlocal) * (1 - ylocal) *
223 yField_[xindex][yindex][zindex + 1] * (1 - xlocal) * (1 - ylocal) *
225 yField_[xindex][yindex + 1][zindex] * (1 - xlocal) * ylocal *
227 yField_[xindex][yindex + 1][zindex + 1] * (1 - xlocal) * ylocal *
229 yField_[xindex + 1][yindex][zindex] * xlocal * (1 - ylocal) *
231 yField_[xindex + 1][yindex][zindex + 1] * xlocal * (1 - ylocal) *
233 yField_[xindex + 1][yindex + 1][zindex] * xlocal * ylocal *
235 yField_[xindex + 1][yindex + 1][zindex + 1] * xlocal * ylocal * zlocal;
237 zField_[xindex][yindex][zindex] * (1 - xlocal) * (1 - ylocal) *
239 zField_[xindex][yindex][zindex + 1] * (1 - xlocal) * (1 - ylocal) *
241 zField_[xindex][yindex + 1][zindex] * (1 - xlocal) * ylocal *
243 zField_[xindex][yindex + 1][zindex + 1] * (1 - xlocal) * ylocal *
245 zField_[xindex + 1][yindex][zindex] * xlocal * (1 - ylocal) *
247 zField_[xindex + 1][yindex][zindex + 1] * xlocal * (1 - ylocal) *
249 zField_[xindex + 1][yindex + 1][zindex] * xlocal * ylocal *
251 zField_[xindex + 1][yindex + 1][zindex + 1] * xlocal * ylocal * zlocal;