36 :fXoffset(xOffset),invertX(false),invertY(false),invertZ(false)
40 double lenUnit=
meter;
41 double fieldUnit=
tesla;
42 G4cout <<
"\n-----------------------------------------------------------"
43 <<
"\n Magnetic field"
44 <<
"\n-----------------------------------------------------------";
47 G4cout <<
"\n ---> " "Reading the field grid from " << filename <<
" ... " << endl;
50 ifstream file( filename );
54 file.getline(buffer,256);
57 file >> nx >> ny >> nz;
59 G4cout <<
" [ Number of values x,y,z: "
60 << nx <<
" " << ny <<
" " << nz <<
" ] "
68 for (ix=0; ix<nx; ix++) {
69 xField[ix].resize(ny);
70 yField[ix].resize(ny);
71 zField[ix].resize(ny);
72 for (iy=0; iy<ny; iy++) {
73 xField[ix][iy].resize(nz);
74 yField[ix][iy].resize(nz);
75 zField[ix][iy].resize(nz);
86 for (ix=0; ix<nx; ix++) {
87 for (iy=0; iy<ny; iy++) {
88 for (iz=0; iz<nz; iz++) {
89 file >> xval >> yval >> zval >> bx >> by >> bz ;
90 if ( ix==0 && iy==0 && iz==0 ) {
91 minx = xval * lenUnit;
92 miny = yval * lenUnit;
93 minz = zval * lenUnit;
95 xField[ix][iy][iz] = bx * fieldUnit;
96 yField[ix][iy][iz] = by * fieldUnit;
97 zField[ix][iy][iz] = bz * fieldUnit;
105 maxx = xval * lenUnit;
106 maxy = yval * lenUnit;
107 maxz = zval * lenUnit;
109 G4cout <<
"\n ---> ... done reading " << endl;
112 G4cout <<
" ---> assumed the order: x, y, z, Bx, By, Bz "
113 <<
"\n ---> Min values x,y,z: "
114 << minx/
cm <<
" " << miny/
cm <<
" " << minz/
cm <<
" cm "
115 <<
"\n ---> Max values x,y,z: "
116 << maxx/
cm <<
" " << maxy/
cm <<
" " << maxz/
cm <<
" cm "
117 <<
"\n ---> The field will be offset by " << xOffset/
cm <<
" cm " << endl;
120 if (maxx < minx) {swap(maxx,minx); invertX =
true;}
121 if (maxy < miny) {swap(maxy,miny); invertY =
true;}
122 if (maxz < minz) {swap(maxz,minz); invertZ =
true;}
123 G4cout <<
"\nAfter reordering if neccesary"
124 <<
"\n ---> Min values x,y,z: "
125 << minx/
cm <<
" " << miny/
cm <<
" " << minz/
cm <<
" cm "
126 <<
" \n ---> Max values x,y,z: "
127 << maxx/
cm <<
" " << maxy/
cm <<
" " << maxz/
cm <<
" cm ";
132 G4cout <<
"\n ---> Dif values x,y,z (range): "
133 << dx/
cm <<
" " << dy/
cm <<
" " << dz/
cm <<
" cm in z "
134 <<
"\n-----------------------------------------------------------" << endl;
138 double *Bfield )
const
140 double x = point[0]+ fXoffset;
146 double xfraction = (x - minx) / dx;
147 double yfraction = (y - miny) / dy;
148 double zfraction = (z - minz) / dz;
150 if (invertX) { xfraction = 1 - xfraction;}
151 if (invertY) { yfraction = 1 - yfraction;}
152 if (invertZ) { zfraction = 1 - zfraction;}
156 double xdindex, ydindex, zdindex;
160 double xlocal = ( std::modf(xfraction*(nx-1), &xdindex));
161 double ylocal = ( std::modf(yfraction*(ny-1), &ydindex));
162 double zlocal = ( std::modf(zfraction*(nz-1), &zdindex));
166 int xindex =
static_cast<int>(std::floor(xdindex));
167 int yindex =
static_cast<int>(std::floor(ydindex));
168 int zindex =
static_cast<int>(std::floor(zdindex));
171 if ((xindex < 0) || (xindex >= nx - 1) ||
172 (yindex < 0) || (yindex >= ny - 1) ||
173 (zindex < 0) || (zindex >= nz - 1))
182 #ifdef DEBUG_INTERPOLATING_FIELD
183 G4cout <<
"Local x,y,z: " << xlocal <<
" " << ylocal <<
" " << zlocal << endl;
184 G4cout <<
"Index x,y,z: " << xindex <<
" " << yindex <<
" " << zindex << endl;
185 double valx0z0, mulx0z0, valx1z0, mulx1z0;
186 double valx0z1, mulx0z1, valx1z1, mulx1z1;
187 valx0z0= table[xindex ][0][zindex]; mulx0z0= (1-xlocal) * (1-zlocal);
188 valx1z0= table[xindex+1][0][zindex]; mulx1z0= xlocal * (1-zlocal);
189 valx0z1= table[xindex ][0][zindex+1]; mulx0z1= (1-xlocal) * zlocal;
190 valx1z1= table[xindex+1][0][zindex+1]; mulx1z1= xlocal * zlocal;
195 xField[xindex ][yindex ][zindex ] * (1-xlocal) * (1-ylocal) * (1-zlocal) +
196 xField[xindex ][yindex ][zindex+1] * (1-xlocal) * (1-ylocal) * zlocal +
197 xField[xindex ][yindex+1][zindex ] * (1-xlocal) * ylocal * (1-zlocal) +
198 xField[xindex ][yindex+1][zindex+1] * (1-xlocal) * ylocal * zlocal +
199 xField[xindex+1][yindex ][zindex ] * xlocal * (1-ylocal) * (1-zlocal) +
200 xField[xindex+1][yindex ][zindex+1] * xlocal * (1-ylocal) * zlocal +
201 xField[xindex+1][yindex+1][zindex ] * xlocal * ylocal * (1-zlocal) +
202 xField[xindex+1][yindex+1][zindex+1] * xlocal * ylocal * zlocal ;
205 yField[xindex ][yindex ][zindex ] * (1-xlocal) * (1-ylocal) * (1-zlocal) +
206 yField[xindex ][yindex ][zindex+1] * (1-xlocal) * (1-ylocal) * zlocal +
207 yField[xindex ][yindex+1][zindex ] * (1-xlocal) * ylocal * (1-zlocal) +
208 yField[xindex ][yindex+1][zindex+1] * (1-xlocal) * ylocal * zlocal +
209 yField[xindex+1][yindex ][zindex ] * xlocal * (1-ylocal) * (1-zlocal) +
210 yField[xindex+1][yindex ][zindex+1] * xlocal * (1-ylocal) * zlocal +
211 yField[xindex+1][yindex+1][zindex ] * xlocal * ylocal * (1-zlocal) +
212 yField[xindex+1][yindex+1][zindex+1] * xlocal * ylocal * zlocal ;
215 zField[xindex ][yindex ][zindex ] * (1-xlocal) * (1-ylocal) * (1-zlocal) +
216 zField[xindex ][yindex ][zindex+1] * (1-xlocal) * (1-ylocal) * zlocal +
217 zField[xindex ][yindex+1][zindex ] * (1-xlocal) * ylocal * (1-zlocal) +
218 zField[xindex ][yindex+1][zindex+1] * (1-xlocal) * ylocal * zlocal +
219 zField[xindex+1][yindex ][zindex ] * xlocal * (1-ylocal) * (1-zlocal) +
220 zField[xindex+1][yindex ][zindex+1] * xlocal * (1-ylocal) * zlocal +
221 zField[xindex+1][yindex+1][zindex ] * xlocal * ylocal * (1-zlocal) +
222 zField[xindex+1][yindex+1][zindex+1] * xlocal * ylocal * zlocal ;
static constexpr double tesla
HadrontherapyMagneticField3D(const char *filename, double xOffset)
#define G4MUTEX_INITIALIZER
void GetFieldValue(const double Point[4], double *Bfield) const
static constexpr double meter
G4GLOB_DLL std::ostream G4cout
static constexpr double cm