Geant4  10.01.p02
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 // *************************************
27 // * *
28 // * HadrontherapyMagneticField3D.cc *
29 // * *
30 // *************************************
31 //
32 //
33 
35 #include "G4SystemOfUnits.hh"
36 #include "G4AutoLock.hh"
37 
38 namespace{ G4Mutex MyHadrontherapyLock=G4MUTEX_INITIALIZER; }
39 
40 HadrontherapyMagneticField3D::HadrontherapyMagneticField3D( const char* filename, double xOffset )
41  :fXoffset(xOffset),invertX(false),invertY(false),invertZ(false)
42 {
43  //The format file is: X Y Z Ex Ey Ez
44 
45  double lenUnit= meter;
46  double fieldUnit= tesla;
47  G4cout << "\n-----------------------------------------------------------"
48  << "\n Magnetic field"
49  << "\n-----------------------------------------------------------";
50 
51 
52  G4cout << "\n ---> " "Reading the field grid from " << filename << " ... " << endl;
53  G4AutoLock lock(&MyHadrontherapyLock);
54 
55  ifstream file( filename ); // Open the file for reading.
56 
57  // Ignore first blank line
58  char buffer[256];
59  file.getline(buffer,256);
60 
61  // Read table dimensions
62  file >> nx >> ny >> nz; // Note dodgy order
63 
64  G4cout << " [ Number of values x,y,z: "
65  << nx << " " << ny << " " << nz << " ] "
66  << endl;
67 
68  // Set up storage space for table
69  xField.resize( nx );
70  yField.resize( nx );
71  zField.resize( nx );
72  int ix, iy, iz;
73  for (ix=0; ix<nx; ix++) {
74  xField[ix].resize(ny);
75  yField[ix].resize(ny);
76  zField[ix].resize(ny);
77  for (iy=0; iy<ny; iy++) {
78  xField[ix][iy].resize(nz);
79  yField[ix][iy].resize(nz);
80  zField[ix][iy].resize(nz);
81  }
82  }
83 
84  // Read in the data
85  G4double xval=0.;
86  G4double yval=0.;
87  G4double zval=0.;
88  G4double bx=0.;
89  G4double by=0.;
90  G4double bz=0.;
91  for (ix=0; ix<nx; ix++) {
92  for (iy=0; iy<ny; iy++) {
93  for (iz=0; iz<nz; iz++) {
94  file >> xval >> yval >> zval >> bx >> by >> bz ;
95  if ( ix==0 && iy==0 && iz==0 ) {
96  minx = xval * lenUnit;
97  miny = yval * lenUnit;
98  minz = zval * lenUnit;
99  }
100  xField[ix][iy][iz] = bx * fieldUnit;
101  yField[ix][iy][iz] = by * fieldUnit;
102  zField[ix][iy][iz] = bz * fieldUnit;
103  }
104  }
105  }
106  file.close();
107 
108  lock.unlock();
109 
110  maxx = xval * lenUnit;
111  maxy = yval * lenUnit;
112  maxz = zval * lenUnit;
113 
114  G4cout << "\n ---> ... done reading " << endl;
115 
116  // G4cout << " Read values of field from file " << filename << endl;
117  G4cout << " ---> assumed the order: x, y, z, Bx, By, Bz "
118  << "\n ---> Min values x,y,z: "
119  << minx/cm << " " << miny/cm << " " << minz/cm << " cm "
120  << "\n ---> Max values x,y,z: "
121  << maxx/cm << " " << maxy/cm << " " << maxz/cm << " cm "
122  << "\n ---> The field will be offset by " << xOffset/cm << " cm " << endl;
123 
124  // Should really check that the limits are not the wrong way around.
125  if (maxx < minx) {swap(maxx,minx); invertX = true;}
126  if (maxy < miny) {swap(maxy,miny); invertY = true;}
127  if (maxz < minz) {swap(maxz,minz); invertZ = true;}
128  G4cout << "\nAfter reordering if neccesary"
129  << "\n ---> Min values x,y,z: "
130  << minx/cm << " " << miny/cm << " " << minz/cm << " cm "
131  << " \n ---> Max values x,y,z: "
132  << maxx/cm << " " << maxy/cm << " " << maxz/cm << " cm ";
133 
134  dx = maxx - minx;
135  dy = maxy - miny;
136  dz = maxz - minz;
137  G4cout << "\n ---> Dif values x,y,z (range): "
138  << dx/cm << " " << dy/cm << " " << dz/cm << " cm in z "
139  << "\n-----------------------------------------------------------" << endl;
140 }
141 
143  double *Bfield ) const
144 {
145  double x = point[0]+ fXoffset;
146  double y = point[1];
147  double z = point[2];
148 
149  // Check that the point is within the defined region
150  if ( x>=minx && x<maxx &&
151  y>=miny && y<maxy &&
152  z>=minz && z<maxz ) {
153  // Position of given point within region, normalized to the range
154  // [0,1]
155  double xfraction = (x - minx) / dx;
156  double yfraction = (y - miny) / dy;
157  double zfraction = (z - minz) / dz;
158 
159  if (invertX) { xfraction = 1 - xfraction;}
160  if (invertY) { yfraction = 1 - yfraction;}
161  if (invertZ) { zfraction = 1 - zfraction;}
162 
163  // Need addresses of these to pass to modf below.
164  // modf uses its second argument as an OUTPUT argument.
165  double xdindex, ydindex, zdindex;
166 
167  // Position of the point within the cuboid defined by the
168  // nearest surrounding tabulated points
169  double xlocal = ( std::modf(xfraction*(nx-1), &xdindex));
170  double ylocal = ( std::modf(yfraction*(ny-1), &ydindex));
171  double zlocal = ( std::modf(zfraction*(nz-1), &zdindex));
172 
173  // The indices of the nearest tabulated point whose coordinates
174  // are all less than those of the given point
175  int xindex = static_cast<int>(xdindex);
176  int yindex = static_cast<int>(ydindex);
177  int zindex = static_cast<int>(zdindex);
178 
179 
180 #ifdef DEBUG_INTERPOLATING_FIELD
181  G4cout << "Local x,y,z: " << xlocal << " " << ylocal << " " << zlocal << endl;
182  G4cout << "Index x,y,z: " << xindex << " " << yindex << " " << zindex << endl;
183  double valx0z0, mulx0z0, valx1z0, mulx1z0;
184  double valx0z1, mulx0z1, valx1z1, mulx1z1;
185  valx0z0= table[xindex ][0][zindex]; mulx0z0= (1-xlocal) * (1-zlocal);
186  valx1z0= table[xindex+1][0][zindex]; mulx1z0= xlocal * (1-zlocal);
187  valx0z1= table[xindex ][0][zindex+1]; mulx0z1= (1-xlocal) * zlocal;
188  valx1z1= table[xindex+1][0][zindex+1]; mulx1z1= xlocal * zlocal;
189 #endif
190  // Full 3-dimensional version
191  Bfield[0] =
192  xField[xindex ][yindex ][zindex ] * (1-xlocal) * (1-ylocal) * (1-zlocal) +
193  xField[xindex ][yindex ][zindex+1] * (1-xlocal) * (1-ylocal) * zlocal +
194  xField[xindex ][yindex+1][zindex ] * (1-xlocal) * ylocal * (1-zlocal) +
195  xField[xindex ][yindex+1][zindex+1] * (1-xlocal) * ylocal * zlocal +
196  xField[xindex+1][yindex ][zindex ] * xlocal * (1-ylocal) * (1-zlocal) +
197  xField[xindex+1][yindex ][zindex+1] * xlocal * (1-ylocal) * zlocal +
198  xField[xindex+1][yindex+1][zindex ] * xlocal * ylocal * (1-zlocal) +
199  xField[xindex+1][yindex+1][zindex+1] * xlocal * ylocal * zlocal ;
200 
201  Bfield[1] =
202  yField[xindex ][yindex ][zindex ] * (1-xlocal) * (1-ylocal) * (1-zlocal) +
203  yField[xindex ][yindex ][zindex+1] * (1-xlocal) * (1-ylocal) * zlocal +
204  yField[xindex ][yindex+1][zindex ] * (1-xlocal) * ylocal * (1-zlocal) +
205  yField[xindex ][yindex+1][zindex+1] * (1-xlocal) * ylocal * zlocal +
206  yField[xindex+1][yindex ][zindex ] * xlocal * (1-ylocal) * (1-zlocal) +
207  yField[xindex+1][yindex ][zindex+1] * xlocal * (1-ylocal) * zlocal +
208  yField[xindex+1][yindex+1][zindex ] * xlocal * ylocal * (1-zlocal) +
209  yField[xindex+1][yindex+1][zindex+1] * xlocal * ylocal * zlocal ;
210 
211  Bfield[2] =
212  zField[xindex ][yindex ][zindex ] * (1-xlocal) * (1-ylocal) * (1-zlocal) +
213  zField[xindex ][yindex ][zindex+1] * (1-xlocal) * (1-ylocal) * zlocal +
214  zField[xindex ][yindex+1][zindex ] * (1-xlocal) * ylocal * (1-zlocal) +
215  zField[xindex ][yindex+1][zindex+1] * (1-xlocal) * ylocal * zlocal +
216  zField[xindex+1][yindex ][zindex ] * xlocal * (1-ylocal) * (1-zlocal) +
217  zField[xindex+1][yindex ][zindex+1] * xlocal * (1-ylocal) * zlocal +
218  zField[xindex+1][yindex+1][zindex ] * xlocal * ylocal * (1-zlocal) +
219  zField[xindex+1][yindex+1][zindex+1] * xlocal * ylocal * zlocal ;
220 
221  } else {
222  Bfield[0] = 0.0;
223  Bfield[1] = 0.0;
224  Bfield[2] = 0.0;
225  }
226 //In order to obtain the output file with the magnetic components read from a particle passing in the magnetic field
227 /* std::ofstream MagneticField("MagneticField.out", std::ios::app);
228  MagneticField<< Bfield[0] << '\t' << " "
229  << Bfield[1] << '\t' << " "
230  << Bfield[2] << '\t' << " "
231  << point[0] << '\t' << " "
232  << point[1] << '\t' << " "
233  << point[2] << '\t' << " "
234  << G4endl;*/
235 
236 }
237 
static const double cm
Definition: G4SIunits.hh:106
vector< vector< vector< double > > > xField
G4double z
Definition: TRTMaterials.hh:39
#define buffer
Definition: xmlparse.cc:611
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:72
G4double iz
Definition: TRTMaterials.hh:39
G4int G4Mutex
Definition: G4Threading.hh:173
static const double tesla
Definition: G4SIunits.hh:247
double G4double
Definition: G4Types.hh:76