File indexing completed on 2025-01-31 09:22:18
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029 #include "HadrontherapyMagneticField3D.hh"
0030 #include "G4SystemOfUnits.hh"
0031 #include "G4AutoLock.hh"
0032
0033 namespace{ G4Mutex MyHadrontherapyLock=G4MUTEX_INITIALIZER; }
0034
0035 using namespace std;
0036
0037 HadrontherapyMagneticField3D::HadrontherapyMagneticField3D( const char* filename, double xOffset )
0038 :fXoffset(xOffset),invertX(false),invertY(false),invertZ(false)
0039 {
0040
0041
0042 double lenUnit= meter;
0043 double fieldUnit= tesla;
0044 G4cout << "\n-----------------------------------------------------------"
0045 << "\n Magnetic field"
0046 << "\n-----------------------------------------------------------";
0047
0048
0049 G4cout << "\n ---> " "Reading the field grid from " << filename << " ... " << G4endl;
0050 G4AutoLock lock(&MyHadrontherapyLock);
0051
0052 ifstream file( filename );
0053
0054
0055 char buffer[256];
0056 file.getline(buffer,256);
0057
0058
0059 file >> nx >> ny >> nz;
0060
0061 G4cout << " [ Number of values x,y,z: "
0062 << nx << " " << ny << " " << nz << " ] "
0063 << G4endl;
0064
0065
0066 xField.resize( nx );
0067 yField.resize( nx );
0068 zField.resize( nx );
0069 int ix, iy, iz;
0070 for (ix=0; ix<nx; ix++) {
0071 xField[ix].resize(ny);
0072 yField[ix].resize(ny);
0073 zField[ix].resize(ny);
0074 for (iy=0; iy<ny; iy++) {
0075 xField[ix][iy].resize(nz);
0076 yField[ix][iy].resize(nz);
0077 zField[ix][iy].resize(nz);
0078 }
0079 }
0080
0081
0082 G4double xval=0.;
0083 G4double yval=0.;
0084 G4double zval=0.;
0085 G4double bx=0.;
0086 G4double by=0.;
0087 G4double bz=0.;
0088 for (ix=0; ix<nx; ix++) {
0089 for (iy=0; iy<ny; iy++) {
0090 for (iz=0; iz<nz; iz++) {
0091 file >> xval >> yval >> zval >> bx >> by >> bz ;
0092 if ( ix==0 && iy==0 && iz==0 ) {
0093 minx = xval * lenUnit;
0094 miny = yval * lenUnit;
0095 minz = zval * lenUnit;
0096 }
0097 xField[ix][iy][iz] = bx * fieldUnit;
0098 yField[ix][iy][iz] = by * fieldUnit;
0099 zField[ix][iy][iz] = bz * fieldUnit;
0100 }
0101 }
0102 }
0103 file.close();
0104
0105 lock.unlock();
0106
0107 maxx = xval * lenUnit;
0108 maxy = yval * lenUnit;
0109 maxz = zval * lenUnit;
0110
0111 G4cout << "\n ---> ... done reading " << G4endl;
0112
0113
0114 G4cout << " ---> assumed the order: x, y, z, Bx, By, Bz "
0115 << "\n ---> Min values x,y,z: "
0116 << minx/cm << " " << miny/cm << " " << minz/cm << " cm "
0117 << "\n ---> Max values x,y,z: "
0118 << maxx/cm << " " << maxy/cm << " " << maxz/cm << " cm "
0119 << "\n ---> The field will be offset by " << xOffset/cm << " cm " << G4endl;
0120
0121
0122 if (maxx < minx) {swap(maxx,minx); invertX = true;}
0123 if (maxy < miny) {swap(maxy,miny); invertY = true;}
0124 if (maxz < minz) {swap(maxz,minz); invertZ = true;}
0125 G4cout << "\nAfter reordering if neccesary"
0126 << "\n ---> Min values x,y,z: "
0127 << minx/cm << " " << miny/cm << " " << minz/cm << " cm "
0128 << " \n ---> Max values x,y,z: "
0129 << maxx/cm << " " << maxy/cm << " " << maxz/cm << " cm ";
0130
0131 dx = maxx - minx;
0132 dy = maxy - miny;
0133 dz = maxz - minz;
0134 G4cout << "\n ---> Dif values x,y,z (range): "
0135 << dx/cm << " " << dy/cm << " " << dz/cm << " cm in z "
0136 << "\n-----------------------------------------------------------" << G4endl;
0137 }
0138
0139 void HadrontherapyMagneticField3D::GetFieldValue(const double point[4],
0140 double *Bfield ) const
0141 {
0142 double x = point[0]+ fXoffset;
0143 double y = point[1];
0144 double z = point[2];
0145
0146
0147
0148 double xfraction = (x - minx) / dx;
0149 double yfraction = (y - miny) / dy;
0150 double zfraction = (z - minz) / dz;
0151
0152 if (invertX) { xfraction = 1 - xfraction;}
0153 if (invertY) { yfraction = 1 - yfraction;}
0154 if (invertZ) { zfraction = 1 - zfraction;}
0155
0156
0157
0158 double xdindex, ydindex, zdindex;
0159
0160
0161
0162 double xlocal = ( std::modf(xfraction*(nx-1), &xdindex));
0163 double ylocal = ( std::modf(yfraction*(ny-1), &ydindex));
0164 double zlocal = ( std::modf(zfraction*(nz-1), &zdindex));
0165
0166
0167
0168 int xindex = static_cast<int>(std::floor(xdindex));
0169 int yindex = static_cast<int>(std::floor(ydindex));
0170 int zindex = static_cast<int>(std::floor(zdindex));
0171
0172
0173 if ((xindex < 0) || (xindex >= nx - 1) ||
0174 (yindex < 0) || (yindex >= ny - 1) ||
0175 (zindex < 0) || (zindex >= nz - 1))
0176 {
0177 Bfield[0] = 0.0;
0178 Bfield[1] = 0.0;
0179 Bfield[2] = 0.0;
0180 }
0181 else
0182 {
0183
0184 #ifdef DEBUG_INTERPOLATING_FIELD
0185 G4cout << "Local x,y,z: " << xlocal << " " << ylocal << " " << zlocal << G4endl;
0186 G4cout << "Index x,y,z: " << xindex << " " << yindex << " " << zindex << G4endl;
0187 double valx0z0, mulx0z0, valx1z0, mulx1z0;
0188 double valx0z1, mulx0z1, valx1z1, mulx1z1;
0189 valx0z0= table[xindex ][0][zindex]; mulx0z0= (1-xlocal) * (1-zlocal);
0190 valx1z0= table[xindex+1][0][zindex]; mulx1z0= xlocal * (1-zlocal);
0191 valx0z1= table[xindex ][0][zindex+1]; mulx0z1= (1-xlocal) * zlocal;
0192 valx1z1= table[xindex+1][0][zindex+1]; mulx1z1= xlocal * zlocal;
0193 #endif
0194
0195
0196 Bfield[0] =
0197 xField[xindex ][yindex ][zindex ] * (1-xlocal) * (1-ylocal) * (1-zlocal) +
0198 xField[xindex ][yindex ][zindex+1] * (1-xlocal) * (1-ylocal) * zlocal +
0199 xField[xindex ][yindex+1][zindex ] * (1-xlocal) * ylocal * (1-zlocal) +
0200 xField[xindex ][yindex+1][zindex+1] * (1-xlocal) * ylocal * zlocal +
0201 xField[xindex+1][yindex ][zindex ] * xlocal * (1-ylocal) * (1-zlocal) +
0202 xField[xindex+1][yindex ][zindex+1] * xlocal * (1-ylocal) * zlocal +
0203 xField[xindex+1][yindex+1][zindex ] * xlocal * ylocal * (1-zlocal) +
0204 xField[xindex+1][yindex+1][zindex+1] * xlocal * ylocal * zlocal ;
0205
0206 Bfield[1] =
0207 yField[xindex ][yindex ][zindex ] * (1-xlocal) * (1-ylocal) * (1-zlocal) +
0208 yField[xindex ][yindex ][zindex+1] * (1-xlocal) * (1-ylocal) * zlocal +
0209 yField[xindex ][yindex+1][zindex ] * (1-xlocal) * ylocal * (1-zlocal) +
0210 yField[xindex ][yindex+1][zindex+1] * (1-xlocal) * ylocal * zlocal +
0211 yField[xindex+1][yindex ][zindex ] * xlocal * (1-ylocal) * (1-zlocal) +
0212 yField[xindex+1][yindex ][zindex+1] * xlocal * (1-ylocal) * zlocal +
0213 yField[xindex+1][yindex+1][zindex ] * xlocal * ylocal * (1-zlocal) +
0214 yField[xindex+1][yindex+1][zindex+1] * xlocal * ylocal * zlocal ;
0215
0216 Bfield[2] =
0217 zField[xindex ][yindex ][zindex ] * (1-xlocal) * (1-ylocal) * (1-zlocal) +
0218 zField[xindex ][yindex ][zindex+1] * (1-xlocal) * (1-ylocal) * zlocal +
0219 zField[xindex ][yindex+1][zindex ] * (1-xlocal) * ylocal * (1-zlocal) +
0220 zField[xindex ][yindex+1][zindex+1] * (1-xlocal) * ylocal * zlocal +
0221 zField[xindex+1][yindex ][zindex ] * xlocal * (1-ylocal) * (1-zlocal) +
0222 zField[xindex+1][yindex ][zindex+1] * xlocal * (1-ylocal) * zlocal +
0223 zField[xindex+1][yindex+1][zindex ] * xlocal * ylocal * (1-zlocal) +
0224 zField[xindex+1][yindex+1][zindex+1] * xlocal * ylocal * zlocal ;
0225 }
0226
0227
0228
0229
0230
0231
0232
0233
0234
0235
0236
0237 }