Geant4  10.02.p01
HadrontherapyMagneticField3D.cc
Go to the documentation of this file.
1 //
2 // ********************************************************************
3 // * License and Disclaimer *
4 // * *
5 // * The Geant4 software is copyright of the Copyright Holders of *
6 // * the Geant4 Collaboration. It is provided under the terms and *
7 // * conditions of the Geant4 Software License, included in the file *
8 // * LICENSE and available at http://cern.ch/geant4/license . These *
9 // * include a list of copyright holders. *
10 // * *
11 // * Neither the authors of this software system, nor their employing *
12 // * institutes,nor the agencies providing financial support for this *
13 // * work make any representation or warranty, express or implied, *
14 // * regarding this software system or assume any liability for its *
15 // * use. Please see the license in the file LICENSE and URL above *
16 // * for the full disclaimer and the limitation of liability. *
17 // * *
18 // * This code implementation is the result of the scientific and *
19 // * technical work of the GEANT4 collaboration. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
25 //
26 // Hadrontherapy advanced example for Geant4
27 // See more at: https://twiki.cern.ch/twiki/bin/view/Geant4/AdvancedExamplesHadrontherapy
28 
30 #include "G4SystemOfUnits.hh"
31 #include "G4AutoLock.hh"
32 
33 namespace{ G4Mutex MyHadrontherapyLock=G4MUTEX_INITIALIZER; }
34 
35 HadrontherapyMagneticField3D::HadrontherapyMagneticField3D( const char* filename, double xOffset )
36  :fXoffset(xOffset),invertX(false),invertY(false),invertZ(false)
37 {
38  //The format file is: X Y Z Ex Ey Ez
39 
40  double lenUnit= meter;
41  double fieldUnit= tesla;
42  G4cout << "\n-----------------------------------------------------------"
43  << "\n Magnetic field"
44  << "\n-----------------------------------------------------------";
45 
46 
47  G4cout << "\n ---> " "Reading the field grid from " << filename << " ... " << endl;
48  G4AutoLock lock(&MyHadrontherapyLock);
49 
50  ifstream file( filename ); // Open the file for reading.
51 
52  // Ignore first blank line
53  char buffer[256];
54  file.getline(buffer,256);
55 
56  // Read table dimensions
57  file >> nx >> ny >> nz; // Note dodgy order
58 
59  G4cout << " [ Number of values x,y,z: "
60  << nx << " " << ny << " " << nz << " ] "
61  << endl;
62 
63  // Set up storage space for table
64  xField.resize( nx );
65  yField.resize( nx );
66  zField.resize( nx );
67  int ix, iy, iz;
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);
76  }
77  }
78 
79  // Read in the data
80  G4double xval=0.;
81  G4double yval=0.;
82  G4double zval=0.;
83  G4double bx=0.;
84  G4double by=0.;
85  G4double bz=0.;
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;
94  }
95  xField[ix][iy][iz] = bx * fieldUnit;
96  yField[ix][iy][iz] = by * fieldUnit;
97  zField[ix][iy][iz] = bz * fieldUnit;
98  }
99  }
100  }
101  file.close();
102 
103  lock.unlock();
104 
105  maxx = xval * lenUnit;
106  maxy = yval * lenUnit;
107  maxz = zval * lenUnit;
108 
109  G4cout << "\n ---> ... done reading " << endl;
110 
111  // G4cout << " Read values of field from file " << filename << 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;
118 
119  // Should really check that the limits are not the wrong way around.
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 ";
128 
129  dx = maxx - minx;
130  dy = maxy - miny;
131  dz = maxz - minz;
132  G4cout << "\n ---> Dif values x,y,z (range): "
133  << dx/cm << " " << dy/cm << " " << dz/cm << " cm in z "
134  << "\n-----------------------------------------------------------" << endl;
135 }
136 
138  double *Bfield ) const
139 {
140  double x = point[0]+ fXoffset;
141  double y = point[1];
142  double z = point[2];
143 
144  // Check that the point is within the defined region
145  if ( x>=minx && x<maxx &&
146  y>=miny && y<maxy &&
147  z>=minz && z<maxz ) {
148  // Position of given point within region, normalized to the range
149  // [0,1]
150  double xfraction = (x - minx) / dx;
151  double yfraction = (y - miny) / dy;
152  double zfraction = (z - minz) / dz;
153 
154  if (invertX) { xfraction = 1 - xfraction;}
155  if (invertY) { yfraction = 1 - yfraction;}
156  if (invertZ) { zfraction = 1 - zfraction;}
157 
158  // Need addresses of these to pass to modf below.
159  // modf uses its second argument as an OUTPUT argument.
160  double xdindex, ydindex, zdindex;
161 
162  // Position of the point within the cuboid defined by the
163  // nearest surrounding tabulated points
164  double xlocal = ( std::modf(xfraction*(nx-1), &xdindex));
165  double ylocal = ( std::modf(yfraction*(ny-1), &ydindex));
166  double zlocal = ( std::modf(zfraction*(nz-1), &zdindex));
167 
168  // The indices of the nearest tabulated point whose coordinates
169  // are all less than those of the given point
170  int xindex = static_cast<int>(xdindex);
171  int yindex = static_cast<int>(ydindex);
172  int zindex = static_cast<int>(zdindex);
173 
174 
175 #ifdef DEBUG_INTERPOLATING_FIELD
176  G4cout << "Local x,y,z: " << xlocal << " " << ylocal << " " << zlocal << endl;
177  G4cout << "Index x,y,z: " << xindex << " " << yindex << " " << zindex << endl;
178  double valx0z0, mulx0z0, valx1z0, mulx1z0;
179  double valx0z1, mulx0z1, valx1z1, mulx1z1;
180  valx0z0= table[xindex ][0][zindex]; mulx0z0= (1-xlocal) * (1-zlocal);
181  valx1z0= table[xindex+1][0][zindex]; mulx1z0= xlocal * (1-zlocal);
182  valx0z1= table[xindex ][0][zindex+1]; mulx0z1= (1-xlocal) * zlocal;
183  valx1z1= table[xindex+1][0][zindex+1]; mulx1z1= xlocal * zlocal;
184 #endif
185  // Full 3-dimensional version
186  Bfield[0] =
187  xField[xindex ][yindex ][zindex ] * (1-xlocal) * (1-ylocal) * (1-zlocal) +
188  xField[xindex ][yindex ][zindex+1] * (1-xlocal) * (1-ylocal) * zlocal +
189  xField[xindex ][yindex+1][zindex ] * (1-xlocal) * ylocal * (1-zlocal) +
190  xField[xindex ][yindex+1][zindex+1] * (1-xlocal) * ylocal * zlocal +
191  xField[xindex+1][yindex ][zindex ] * xlocal * (1-ylocal) * (1-zlocal) +
192  xField[xindex+1][yindex ][zindex+1] * xlocal * (1-ylocal) * zlocal +
193  xField[xindex+1][yindex+1][zindex ] * xlocal * ylocal * (1-zlocal) +
194  xField[xindex+1][yindex+1][zindex+1] * xlocal * ylocal * zlocal ;
195 
196  Bfield[1] =
197  yField[xindex ][yindex ][zindex ] * (1-xlocal) * (1-ylocal) * (1-zlocal) +
198  yField[xindex ][yindex ][zindex+1] * (1-xlocal) * (1-ylocal) * zlocal +
199  yField[xindex ][yindex+1][zindex ] * (1-xlocal) * ylocal * (1-zlocal) +
200  yField[xindex ][yindex+1][zindex+1] * (1-xlocal) * ylocal * zlocal +
201  yField[xindex+1][yindex ][zindex ] * xlocal * (1-ylocal) * (1-zlocal) +
202  yField[xindex+1][yindex ][zindex+1] * xlocal * (1-ylocal) * zlocal +
203  yField[xindex+1][yindex+1][zindex ] * xlocal * ylocal * (1-zlocal) +
204  yField[xindex+1][yindex+1][zindex+1] * xlocal * ylocal * zlocal ;
205 
206  Bfield[2] =
207  zField[xindex ][yindex ][zindex ] * (1-xlocal) * (1-ylocal) * (1-zlocal) +
208  zField[xindex ][yindex ][zindex+1] * (1-xlocal) * (1-ylocal) * zlocal +
209  zField[xindex ][yindex+1][zindex ] * (1-xlocal) * ylocal * (1-zlocal) +
210  zField[xindex ][yindex+1][zindex+1] * (1-xlocal) * ylocal * zlocal +
211  zField[xindex+1][yindex ][zindex ] * xlocal * (1-ylocal) * (1-zlocal) +
212  zField[xindex+1][yindex ][zindex+1] * xlocal * (1-ylocal) * zlocal +
213  zField[xindex+1][yindex+1][zindex ] * xlocal * ylocal * (1-zlocal) +
214  zField[xindex+1][yindex+1][zindex+1] * xlocal * ylocal * zlocal ;
215 
216  } else {
217  Bfield[0] = 0.0;
218  Bfield[1] = 0.0;
219  Bfield[2] = 0.0;
220  }
221 //In order to obtain the output file with the magnetic components read from a particle passing in the magnetic field
222 /* std::ofstream MagneticField("MagneticField.out", std::ios::app);
223  MagneticField<< Bfield[0] << '\t' << " "
224  << Bfield[1] << '\t' << " "
225  << Bfield[2] << '\t' << " "
226  << point[0] << '\t' << " "
227  << point[1] << '\t' << " "
228  << point[2] << '\t' << " "
229  << G4endl;*/
230 
231 }
232 
static const double cm
Definition: G4SIunits.hh:118
vector< vector< vector< double > > > xField
G4double z
Definition: TRTMaterials.hh:39
#define buffer
Definition: xmlparse.cc:628
HadrontherapyMagneticField3D(const char *filename, double xOffset)
vector< vector< vector< double > > > zField
#define G4MUTEX_INITIALIZER
Definition: G4Threading.hh:175
void GetFieldValue(const double Point[4], double *Bfield) const
G4GLOB_DLL std::ostream G4cout
vector< vector< vector< double > > > yField
static const double meter
Definition: G4SIunits.hh:81
G4double iz
Definition: TRTMaterials.hh:39
G4int G4Mutex
Definition: G4Threading.hh:173
const G4double x[NPOINTSGL]
static const double tesla
Definition: G4SIunits.hh:265
double G4double
Definition: G4Types.hh:76