Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-31 09:22:18

0001 //
0002 // ********************************************************************
0003 // * License and Disclaimer                                           *
0004 // *                                                                  *
0005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
0006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
0007 // * conditions of the Geant4 Software License,  included in the file *
0008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
0009 // * include a list of copyright holders.                             *
0010 // *                                                                  *
0011 // * Neither the authors of this software system, nor their employing *
0012 // * institutes,nor the agencies providing financial support for this *
0013 // * work  make  any representation or  warranty, express or implied, *
0014 // * regarding  this  software system or assume any liability for its *
0015 // * use.  Please see the license in the file  LICENSE  and URL above *
0016 // * for the full disclaimer and the limitation of liability.         *
0017 // *                                                                  *
0018 // * This  code  implementation is the result of  the  scientific and *
0019 // * technical work of the GEANT4 collaboration.                      *
0020 // * By using,  copying,  modifying or  distributing the software (or *
0021 // * any work based  on the software)  you  agree  to acknowledge its *
0022 // * use  in  resulting  scientific  publications,  and indicate your *
0023 // * acceptance of all terms of the Geant4 Software license.          *
0024 // ********************************************************************
0025 //
0026 // Hadrontherapy advanced example for Geant4
0027 // See more at: https://twiki.cern.ch/twiki/bin/view/Geant4/AdvancedExamplesHadrontherapy
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    //The format file is: X Y Z Ex Ey Ez
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 ); // Open the file for reading.
0053 
0054   // Ignore first blank line
0055   char buffer[256];
0056   file.getline(buffer,256);
0057 
0058   // Read table dimensions
0059   file >> nx >> ny >> nz; // Note dodgy order
0060 
0061   G4cout << "  [ Number of values x,y,z: "
0062      << nx << " " << ny << " " << nz << " ] "
0063      << G4endl;
0064 
0065   // Set up storage space for table
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   // Read in the data
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   // G4cout << " Read values of field from file " << filename << G4endl;
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   // Should really check that the limits are not the wrong way around.
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     // Position of given point within region, normalized to the range
0147     // [0,1]
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     // Need addresses of these to pass to modf below.
0157     // modf uses its second argument as an OUTPUT argument.
0158     double xdindex, ydindex, zdindex;
0159 
0160     // Position of the point within the cuboid defined by the
0161     // nearest surrounding tabulated points
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     // The indices of the nearest tabulated point whose coordinates
0167     // are all less than those of the given point
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       // Check that the point is within the defined region
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         // Full 3-dimensional version
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 //In order to obtain the output file with the magnetic components read from a particle passing in the magnetic field
0228 /*  std::ofstream MagneticField("MagneticField.out", std::ios::app);
0229        MagneticField<<   Bfield[0] << '\t' << "   "
0230             <<   Bfield[1] << '\t' << "    "
0231             <<   Bfield[2] << '\t' << "   "
0232             <<   point[0] << '\t' << "   "
0233             <<   point[1] << '\t' << "    "
0234             <<   point[2] << '\t' << "   "
0235             << std::endl;*/
0236 
0237 }