Geant4  10.03.p03
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PurgMagTabulatedField3D.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 // Code developed by:
27 // S.Larsson and J. Generowicz.
28 //
29 // *************************************
30 // * *
31 // * PurgMagTabulatedField3D.cc *
32 // * *
33 // *************************************
34 //
35 // $Id: PurgMagTabulatedField3D.cc 84477 2014-10-16 08:44:04Z gcosmo $
36 //
37 
39 #include "G4SystemOfUnits.hh"
40 #include "G4AutoLock.hh"
41 
42 namespace{
43  G4Mutex myPurgMagTabulatedField3DLock = G4MUTEX_INITIALIZER;
44 }
45 
47  double zOffset )
48  :fZoffset(zOffset),invertX(false),invertY(false),invertZ(false)
49 {
50 
51  double lenUnit= meter;
52  double fieldUnit= tesla;
53  G4cout << "\n-----------------------------------------------------------"
54  << "\n Magnetic field"
55  << "\n-----------------------------------------------------------";
56 
57  G4cout << "\n ---> " "Reading the field grid from " << filename << " ... " << endl;
58 
59  //
60  //This is a thread-local class and we have to avoid that all workers open the
61  //file at the same time
62  G4AutoLock lock(&myPurgMagTabulatedField3DLock);
63 
64  ifstream file( filename ); // Open the file for reading.
65 
66  if (!file.is_open())
67  {
69  ed << "Could not open input file " << filename << G4endl;
70  G4Exception("PurgMagTabulatedField3D::PurgMagTabulatedField3D",
71  "pugmag001",FatalException,ed);
72  }
73 
74  // Ignore first blank line
75  char buffer[256];
76  file.getline(buffer,256);
77 
78  // Read table dimensions
79  file >> nx >> ny >> nz; // Note dodgy order
80 
81  G4cout << " [ Number of values x,y,z: "
82  << nx << " " << ny << " " << nz << " ] "
83  << endl;
84 
85  // Set up storage space for table
86  xField.resize( nx );
87  yField.resize( nx );
88  zField.resize( nx );
89  int ix, iy, iz;
90  for (ix=0; ix<nx; ix++) {
91  xField[ix].resize(ny);
92  yField[ix].resize(ny);
93  zField[ix].resize(ny);
94  for (iy=0; iy<ny; iy++) {
95  xField[ix][iy].resize(nz);
96  yField[ix][iy].resize(nz);
97  zField[ix][iy].resize(nz);
98  }
99  }
100 
101  // Ignore other header information
102  // The first line whose second character is '0' is considered to
103  // be the last line of the header.
104  do {
105  file.getline(buffer,256);
106  } while ( buffer[1]!='0');
107 
108  // Read in the data
109  double xval,yval,zval,bx,by,bz;
110  double permeability; // Not used in this example.
111  for (ix=0; ix<nx; ix++) {
112  for (iy=0; iy<ny; iy++) {
113  for (iz=0; iz<nz; iz++) {
114  file >> xval >> yval >> zval >> bx >> by >> bz >> permeability;
115  if ( ix==0 && iy==0 && iz==0 ) {
116  minx = xval * lenUnit;
117  miny = yval * lenUnit;
118  minz = zval * lenUnit;
119  }
120  xField[ix][iy][iz] = bx * fieldUnit;
121  yField[ix][iy][iz] = by * fieldUnit;
122  zField[ix][iy][iz] = bz * fieldUnit;
123  }
124  }
125  }
126  file.close();
127 
128  lock.unlock();
129 
130  maxx = xval * lenUnit;
131  maxy = yval * lenUnit;
132  maxz = zval * lenUnit;
133 
134  G4cout << "\n ---> ... done reading " << endl;
135 
136  // G4cout << " Read values of field from file " << filename << endl;
137  G4cout << " ---> assumed the order: x, y, z, Bx, By, Bz "
138  << "\n ---> Min values x,y,z: "
139  << minx/cm << " " << miny/cm << " " << minz/cm << " cm "
140  << "\n ---> Max values x,y,z: "
141  << maxx/cm << " " << maxy/cm << " " << maxz/cm << " cm "
142  << "\n ---> The field will be offset by " << zOffset/cm << " cm " << endl;
143 
144  // Should really check that the limits are not the wrong way around.
145  if (maxx < minx) {swap(maxx,minx); invertX = true;}
146  if (maxy < miny) {swap(maxy,miny); invertY = true;}
147  if (maxz < minz) {swap(maxz,minz); invertZ = true;}
148  G4cout << "\nAfter reordering if neccesary"
149  << "\n ---> Min values x,y,z: "
150  << minx/cm << " " << miny/cm << " " << minz/cm << " cm "
151  << " \n ---> Max values x,y,z: "
152  << maxx/cm << " " << maxy/cm << " " << maxz/cm << " cm ";
153 
154  dx = maxx - minx;
155  dy = maxy - miny;
156  dz = maxz - minz;
157  G4cout << "\n ---> Dif values x,y,z (range): "
158  << dx/cm << " " << dy/cm << " " << dz/cm << " cm in z "
159  << "\n-----------------------------------------------------------" << endl;
160 }
161 
162 void PurgMagTabulatedField3D::GetFieldValue(const double point[4],
163  double *Bfield ) const
164 {
165 
166  double x = point[0];
167  double y = point[1];
168  double z = point[2] + fZoffset;
169 
170  // Check that the point is within the defined region
171  if ( x>=minx && x<=maxx &&
172  y>=miny && y<=maxy &&
173  z>=minz && z<=maxz ) {
174 
175  // Position of given point within region, normalized to the range
176  // [0,1]
177  double xfraction = (x - minx) / dx;
178  double yfraction = (y - miny) / dy;
179  double zfraction = (z - minz) / dz;
180 
181  if (invertX) { xfraction = 1 - xfraction;}
182  if (invertY) { yfraction = 1 - yfraction;}
183  if (invertZ) { zfraction = 1 - zfraction;}
184 
185  // Need addresses of these to pass to modf below.
186  // modf uses its second argument as an OUTPUT argument.
187  double xdindex, ydindex, zdindex;
188 
189  // Position of the point within the cuboid defined by the
190  // nearest surrounding tabulated points
191  double xlocal = ( std::modf(xfraction*(nx-1), &xdindex));
192  double ylocal = ( std::modf(yfraction*(ny-1), &ydindex));
193  double zlocal = ( std::modf(zfraction*(nz-1), &zdindex));
194 
195  // The indices of the nearest tabulated point whose coordinates
196  // are all less than those of the given point
197  int xindex = static_cast<int>(xdindex);
198  int yindex = static_cast<int>(ydindex);
199  int zindex = static_cast<int>(zdindex);
200 
201 
202 #ifdef DEBUG_INTERPOLATING_FIELD
203  G4cout << "Local x,y,z: " << xlocal << " " << ylocal << " " << zlocal << endl;
204  G4cout << "Index x,y,z: " << xindex << " " << yindex << " " << zindex << endl;
205  double valx0z0, mulx0z0, valx1z0, mulx1z0;
206  double valx0z1, mulx0z1, valx1z1, mulx1z1;
207  valx0z0= table[xindex ][0][zindex]; mulx0z0= (1-xlocal) * (1-zlocal);
208  valx1z0= table[xindex+1][0][zindex]; mulx1z0= xlocal * (1-zlocal);
209  valx0z1= table[xindex ][0][zindex+1]; mulx0z1= (1-xlocal) * zlocal;
210  valx1z1= table[xindex+1][0][zindex+1]; mulx1z1= xlocal * zlocal;
211 #endif
212 
213  // Full 3-dimensional version
214  Bfield[0] =
215  xField[xindex ][yindex ][zindex ] * (1-xlocal) * (1-ylocal) * (1-zlocal) +
216  xField[xindex ][yindex ][zindex+1] * (1-xlocal) * (1-ylocal) * zlocal +
217  xField[xindex ][yindex+1][zindex ] * (1-xlocal) * ylocal * (1-zlocal) +
218  xField[xindex ][yindex+1][zindex+1] * (1-xlocal) * ylocal * zlocal +
219  xField[xindex+1][yindex ][zindex ] * xlocal * (1-ylocal) * (1-zlocal) +
220  xField[xindex+1][yindex ][zindex+1] * xlocal * (1-ylocal) * zlocal +
221  xField[xindex+1][yindex+1][zindex ] * xlocal * ylocal * (1-zlocal) +
222  xField[xindex+1][yindex+1][zindex+1] * xlocal * ylocal * zlocal ;
223  Bfield[1] =
224  yField[xindex ][yindex ][zindex ] * (1-xlocal) * (1-ylocal) * (1-zlocal) +
225  yField[xindex ][yindex ][zindex+1] * (1-xlocal) * (1-ylocal) * zlocal +
226  yField[xindex ][yindex+1][zindex ] * (1-xlocal) * ylocal * (1-zlocal) +
227  yField[xindex ][yindex+1][zindex+1] * (1-xlocal) * ylocal * zlocal +
228  yField[xindex+1][yindex ][zindex ] * xlocal * (1-ylocal) * (1-zlocal) +
229  yField[xindex+1][yindex ][zindex+1] * xlocal * (1-ylocal) * zlocal +
230  yField[xindex+1][yindex+1][zindex ] * xlocal * ylocal * (1-zlocal) +
231  yField[xindex+1][yindex+1][zindex+1] * xlocal * ylocal * zlocal ;
232  Bfield[2] =
233  zField[xindex ][yindex ][zindex ] * (1-xlocal) * (1-ylocal) * (1-zlocal) +
234  zField[xindex ][yindex ][zindex+1] * (1-xlocal) * (1-ylocal) * zlocal +
235  zField[xindex ][yindex+1][zindex ] * (1-xlocal) * ylocal * (1-zlocal) +
236  zField[xindex ][yindex+1][zindex+1] * (1-xlocal) * ylocal * zlocal +
237  zField[xindex+1][yindex ][zindex ] * xlocal * (1-ylocal) * (1-zlocal) +
238  zField[xindex+1][yindex ][zindex+1] * xlocal * (1-ylocal) * zlocal +
239  zField[xindex+1][yindex+1][zindex ] * xlocal * ylocal * (1-zlocal) +
240  zField[xindex+1][yindex+1][zindex+1] * xlocal * ylocal * zlocal ;
241 
242  } else {
243  Bfield[0] = 0.0;
244  Bfield[1] = 0.0;
245  Bfield[2] = 0.0;
246  }
247 }
248 
static constexpr double tesla
Definition: G4SIunits.hh:268
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
#define buffer
Definition: xmlparse.cc:628
tuple x
Definition: test.py:50
#define G4MUTEX_INITIALIZER
Definition: G4Threading.hh:175
static constexpr double meter
Definition: G4SIunits.hh:82
void GetFieldValue(const double Point[4], double *Bfield) const
G4GLOB_DLL std::ostream G4cout
static constexpr double cm
Definition: G4SIunits.hh:119
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
G4int G4Mutex
Definition: G4Threading.hh:173
tuple z
Definition: test.py:28
#define G4endl
Definition: G4ios.hh:61
PurgMagTabulatedField3D(const char *filename, double zOffset)