Geant4  10.03.p02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PurgMagTabulatedField3D Class Reference

#include <PurgMagTabulatedField3D.hh>

Inheritance diagram for PurgMagTabulatedField3D:
Collaboration diagram for PurgMagTabulatedField3D:

Public Member Functions

 PurgMagTabulatedField3D (const char *filename, double zOffset)
 
void GetFieldValue (const double Point[4], double *Bfield) const
 
- Public Member Functions inherited from G4MagneticField
 G4MagneticField ()
 
virtual ~G4MagneticField ()
 
 G4MagneticField (const G4MagneticField &r)
 
G4MagneticFieldoperator= (const G4MagneticField &p)
 
G4bool DoesFieldChangeEnergy () const
 
- Public Member Functions inherited from G4ElectroMagneticField
 G4ElectroMagneticField ()
 
virtual ~G4ElectroMagneticField ()
 
 G4ElectroMagneticField (const G4ElectroMagneticField &r)
 
G4ElectroMagneticFieldoperator= (const G4ElectroMagneticField &p)
 
- Public Member Functions inherited from G4Field
 G4Field (G4bool gravityOn=false)
 
 G4Field (const G4Field &)
 
virtual ~G4Field ()
 
G4Fieldoperator= (const G4Field &p)
 
G4bool IsGravityActive () const
 
void SetGravityActive (G4bool OnOffFlag)
 
virtual G4FieldClone () const
 

Detailed Description

Definition at line 48 of file PurgMagTabulatedField3D.hh.

Constructor & Destructor Documentation

PurgMagTabulatedField3D::PurgMagTabulatedField3D ( const char *  filename,
double  zOffset 
)

Definition at line 46 of file PurgMagTabulatedField3D.cc.

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 }
static constexpr double tesla
Definition: G4SIunits.hh:268
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
#define buffer
Definition: xmlparse.cc:628
static constexpr double meter
Definition: G4SIunits.hh:82
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
#define G4endl
Definition: G4ios.hh:61

Here is the call graph for this function:

Member Function Documentation

void PurgMagTabulatedField3D::GetFieldValue ( const double  Point[4],
double *  Bfield 
) const
virtual

Implements G4MagneticField.

Definition at line 162 of file PurgMagTabulatedField3D.cc.

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 }
tuple x
Definition: test.py:50
G4GLOB_DLL std::ostream G4cout
tuple z
Definition: test.py:28

The documentation for this class was generated from the following files: