Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer 3 // * License and Disclaimer * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/ 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. 9 // * include a list of copyright holders. * 10 // * 10 // * * 11 // * Neither the authors of this software syst 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitatio 16 // * for the full disclaimer and the limitation of liability. * 17 // * 17 // * * 18 // * This code implementation is the result 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboratio 19 // * technical work of the GEANT4 collaboration. * 20 // * By using, copying, modifying or distri 20 // * By using, copying, modifying or distributing the software (or * 21 // * any work based on the software) you ag 21 // * any work based on the software) you agree to acknowledge its * 22 // * use in resulting scientific publicati 22 // * use in resulting scientific publications, and indicate your * 23 // * acceptance of all terms of the Geant4 Sof 23 // * acceptance of all terms of the Geant4 Software license. * 24 // ******************************************* 24 // ******************************************************************** 25 // 25 // 26 // Code developed by: 26 // Code developed by: 27 // S.Larsson and J. Generowicz. 27 // S.Larsson and J. Generowicz. 28 // 28 // 29 // ************************************* 29 // ************************************* 30 // * * 30 // * * 31 // * PurgMagTabulatedField3D.cc * 31 // * PurgMagTabulatedField3D.cc * 32 // * * 32 // * * 33 // ************************************* 33 // ************************************* 34 // 34 // >> 35 // $Id: PurgMagTabulatedField3D.cc 84477 2014-10-16 08:44:04Z gcosmo $ 35 // 36 // 36 37 37 #include "PurgMagTabulatedField3D.hh" 38 #include "PurgMagTabulatedField3D.hh" 38 #include "G4SystemOfUnits.hh" 39 #include "G4SystemOfUnits.hh" 39 #include "G4AutoLock.hh" 40 #include "G4AutoLock.hh" 40 41 41 namespace{ 42 namespace{ 42 G4Mutex myPurgMagTabulatedField3DLock = G4MU 43 G4Mutex myPurgMagTabulatedField3DLock = G4MUTEX_INITIALIZER; 43 } 44 } 44 45 45 using namespace std; << 46 << 47 PurgMagTabulatedField3D::PurgMagTabulatedField 46 PurgMagTabulatedField3D::PurgMagTabulatedField3D(const char* filename, 48 double zOffset ) 47 double zOffset ) 49 :fZoffset(zOffset),invertX(false),invertY(fa 48 :fZoffset(zOffset),invertX(false),invertY(false),invertZ(false) 50 { 49 { 51 50 52 double lenUnit= meter; 51 double lenUnit= meter; 53 double fieldUnit= tesla; 52 double fieldUnit= tesla; 54 G4cout << "\n------------------------------- 53 G4cout << "\n-----------------------------------------------------------" 55 << "\n Magnetic field" 54 << "\n Magnetic field" 56 << "\n------------------------------------- 55 << "\n-----------------------------------------------------------"; 57 56 58 G4cout << "\n ---> " "Reading the field grid << 57 G4cout << "\n ---> " "Reading the field grid from " << filename << " ... " << endl; 59 58 60 // 59 // 61 //This is a thread-local class and we have t 60 //This is a thread-local class and we have to avoid that all workers open the 62 //file at the same time 61 //file at the same time 63 G4AutoLock lock(&myPurgMagTabulatedField3DLo 62 G4AutoLock lock(&myPurgMagTabulatedField3DLock); 64 63 65 ifstream file( filename ); // Open the file 64 ifstream file( filename ); // Open the file for reading. 66 65 67 if (!file.is_open()) 66 if (!file.is_open()) 68 { 67 { 69 G4ExceptionDescription ed; 68 G4ExceptionDescription ed; 70 ed << "Could not open input file " << fi << 69 ed << "Could not open input file " << filename << G4endl; 71 G4Exception("PurgMagTabulatedField3D::Pu 70 G4Exception("PurgMagTabulatedField3D::PurgMagTabulatedField3D", 72 "pugmag001",FatalException,ed); 71 "pugmag001",FatalException,ed); 73 } 72 } 74 73 75 // Ignore first blank line 74 // Ignore first blank line 76 char buffer[256]; 75 char buffer[256]; 77 file.getline(buffer,256); 76 file.getline(buffer,256); 78 77 79 // Read table dimensions 78 // Read table dimensions 80 file >> nx >> ny >> nz; // Note dodgy order 79 file >> nx >> ny >> nz; // Note dodgy order 81 80 82 G4cout << " [ Number of values x,y,z: " 81 G4cout << " [ Number of values x,y,z: " 83 << nx << " " << ny << " " << nz << " ] " 82 << nx << " " << ny << " " << nz << " ] " 84 << G4endl; << 83 << endl; 85 84 86 // Set up storage space for table 85 // Set up storage space for table 87 xField.resize( nx ); 86 xField.resize( nx ); 88 yField.resize( nx ); 87 yField.resize( nx ); 89 zField.resize( nx ); 88 zField.resize( nx ); 90 int ix, iy, iz; 89 int ix, iy, iz; 91 for (ix=0; ix<nx; ix++) { 90 for (ix=0; ix<nx; ix++) { 92 xField[ix].resize(ny); 91 xField[ix].resize(ny); 93 yField[ix].resize(ny); 92 yField[ix].resize(ny); 94 zField[ix].resize(ny); 93 zField[ix].resize(ny); 95 for (iy=0; iy<ny; iy++) { 94 for (iy=0; iy<ny; iy++) { 96 xField[ix][iy].resize(nz); 95 xField[ix][iy].resize(nz); 97 yField[ix][iy].resize(nz); 96 yField[ix][iy].resize(nz); 98 zField[ix][iy].resize(nz); 97 zField[ix][iy].resize(nz); 99 } 98 } 100 } 99 } 101 100 102 // Ignore other header information 101 // Ignore other header information 103 // The first line whose second character is 102 // The first line whose second character is '0' is considered to 104 // be the last line of the header. 103 // be the last line of the header. 105 do { 104 do { 106 file.getline(buffer,256); 105 file.getline(buffer,256); 107 } while ( buffer[1]!='0'); 106 } while ( buffer[1]!='0'); 108 107 109 // Read in the data 108 // Read in the data 110 double xval,yval,zval,bx,by,bz; 109 double xval,yval,zval,bx,by,bz; 111 double permeability; // Not used in this exa 110 double permeability; // Not used in this example. 112 for (ix=0; ix<nx; ix++) { 111 for (ix=0; ix<nx; ix++) { 113 for (iy=0; iy<ny; iy++) { 112 for (iy=0; iy<ny; iy++) { 114 for (iz=0; iz<nz; iz++) { 113 for (iz=0; iz<nz; iz++) { 115 file >> xval >> yval >> zval >> bx >> 114 file >> xval >> yval >> zval >> bx >> by >> bz >> permeability; 116 if ( ix==0 && iy==0 && iz==0 ) { 115 if ( ix==0 && iy==0 && iz==0 ) { 117 minx = xval * lenUnit; 116 minx = xval * lenUnit; 118 miny = yval * lenUnit; 117 miny = yval * lenUnit; 119 minz = zval * lenUnit; 118 minz = zval * lenUnit; 120 } 119 } 121 xField[ix][iy][iz] = bx * fieldUnit; 120 xField[ix][iy][iz] = bx * fieldUnit; 122 yField[ix][iy][iz] = by * fieldUnit; 121 yField[ix][iy][iz] = by * fieldUnit; 123 zField[ix][iy][iz] = bz * fieldUnit; 122 zField[ix][iy][iz] = bz * fieldUnit; 124 } 123 } 125 } 124 } 126 } 125 } 127 file.close(); 126 file.close(); 128 127 129 lock.unlock(); 128 lock.unlock(); 130 129 131 maxx = xval * lenUnit; 130 maxx = xval * lenUnit; 132 maxy = yval * lenUnit; 131 maxy = yval * lenUnit; 133 maxz = zval * lenUnit; 132 maxz = zval * lenUnit; 134 133 135 G4cout << "\n ---> ... done reading " << G4e << 134 G4cout << "\n ---> ... done reading " << endl; 136 135 137 // G4cout << " Read values of field from fil << 136 // G4cout << " Read values of field from file " << filename << endl; 138 G4cout << " ---> assumed the order: x, y, z 137 G4cout << " ---> assumed the order: x, y, z, Bx, By, Bz " 139 << "\n ---> Min values x,y,z: " 138 << "\n ---> Min values x,y,z: " 140 << minx/cm << " " << miny/cm << " " << minz 139 << minx/cm << " " << miny/cm << " " << minz/cm << " cm " 141 << "\n ---> Max values x,y,z: " 140 << "\n ---> Max values x,y,z: " 142 << maxx/cm << " " << maxy/cm << " " << maxz 141 << maxx/cm << " " << maxy/cm << " " << maxz/cm << " cm " 143 << "\n ---> The field will be offset by " < << 142 << "\n ---> The field will be offset by " << zOffset/cm << " cm " << endl; 144 143 145 // Should really check that the limits are n 144 // Should really check that the limits are not the wrong way around. 146 if (maxx < minx) {swap(maxx,minx); invertX = 145 if (maxx < minx) {swap(maxx,minx); invertX = true;} 147 if (maxy < miny) {swap(maxy,miny); invertY = 146 if (maxy < miny) {swap(maxy,miny); invertY = true;} 148 if (maxz < minz) {swap(maxz,minz); invertZ = 147 if (maxz < minz) {swap(maxz,minz); invertZ = true;} 149 G4cout << "\nAfter reordering if neccesary" 148 G4cout << "\nAfter reordering if neccesary" 150 << "\n ---> Min values x,y,z: " 149 << "\n ---> Min values x,y,z: " 151 << minx/cm << " " << miny/cm << " " << minz 150 << minx/cm << " " << miny/cm << " " << minz/cm << " cm " 152 << " \n ---> Max values x,y,z: " 151 << " \n ---> Max values x,y,z: " 153 << maxx/cm << " " << maxy/cm << " " << maxz 152 << maxx/cm << " " << maxy/cm << " " << maxz/cm << " cm "; 154 153 155 dx = maxx - minx; 154 dx = maxx - minx; 156 dy = maxy - miny; 155 dy = maxy - miny; 157 dz = maxz - minz; 156 dz = maxz - minz; 158 G4cout << "\n ---> Dif values x,y,z (range): 157 G4cout << "\n ---> Dif values x,y,z (range): " 159 << dx/cm << " " << dy/cm << " " << dz/cm << 158 << dx/cm << " " << dy/cm << " " << dz/cm << " cm in z " 160 << "\n------------------------------------- << 159 << "\n-----------------------------------------------------------" << endl; 161 } 160 } 162 161 163 void PurgMagTabulatedField3D::GetFieldValue(co 162 void PurgMagTabulatedField3D::GetFieldValue(const double point[4], 164 double *Bfield ) const 163 double *Bfield ) const 165 { 164 { 166 165 167 double x = point[0]; 166 double x = point[0]; 168 double y = point[1]; 167 double y = point[1]; 169 double z = point[2] + fZoffset; 168 double z = point[2] + fZoffset; 170 169 171 // Check that the point is within the define 170 // Check that the point is within the defined region 172 if ( x>=minx && x<=maxx && 171 if ( x>=minx && x<=maxx && 173 y>=miny && y<=maxy && 172 y>=miny && y<=maxy && 174 z>=minz && z<=maxz ) { 173 z>=minz && z<=maxz ) { 175 174 176 // Position of given point within region, 175 // Position of given point within region, normalized to the range 177 // [0,1] 176 // [0,1] 178 double xfraction = (x - minx) / dx; 177 double xfraction = (x - minx) / dx; 179 double yfraction = (y - miny) / dy; 178 double yfraction = (y - miny) / dy; 180 double zfraction = (z - minz) / dz; 179 double zfraction = (z - minz) / dz; 181 180 182 if (invertX) { xfraction = 1 - xfraction;} 181 if (invertX) { xfraction = 1 - xfraction;} 183 if (invertY) { yfraction = 1 - yfraction;} 182 if (invertY) { yfraction = 1 - yfraction;} 184 if (invertZ) { zfraction = 1 - zfraction;} 183 if (invertZ) { zfraction = 1 - zfraction;} 185 184 186 // Need addresses of these to pass to modf 185 // Need addresses of these to pass to modf below. 187 // modf uses its second argument as an OUT 186 // modf uses its second argument as an OUTPUT argument. 188 double xdindex, ydindex, zdindex; 187 double xdindex, ydindex, zdindex; 189 188 190 // Position of the point within the cuboid 189 // Position of the point within the cuboid defined by the 191 // nearest surrounding tabulated points 190 // nearest surrounding tabulated points 192 double xlocal = ( std::modf(xfraction*(nx- 191 double xlocal = ( std::modf(xfraction*(nx-1), &xdindex)); 193 double ylocal = ( std::modf(yfraction*(ny- 192 double ylocal = ( std::modf(yfraction*(ny-1), &ydindex)); 194 double zlocal = ( std::modf(zfraction*(nz- 193 double zlocal = ( std::modf(zfraction*(nz-1), &zdindex)); 195 194 196 // The indices of the nearest tabulated po 195 // The indices of the nearest tabulated point whose coordinates 197 // are all less than those of the given po 196 // are all less than those of the given point 198 int xindex = static_cast<int>(xdindex); 197 int xindex = static_cast<int>(xdindex); 199 int yindex = static_cast<int>(ydindex); 198 int yindex = static_cast<int>(ydindex); 200 int zindex = static_cast<int>(zdindex); 199 int zindex = static_cast<int>(zdindex); 201 200 202 201 203 #ifdef DEBUG_INTERPOLATING_FIELD 202 #ifdef DEBUG_INTERPOLATING_FIELD 204 G4cout << "Local x,y,z: " << xlocal << " " << 203 G4cout << "Local x,y,z: " << xlocal << " " << ylocal << " " << zlocal << endl; 205 G4cout << "Index x,y,z: " << xindex << " " << 204 G4cout << "Index x,y,z: " << xindex << " " << yindex << " " << zindex << endl; 206 double valx0z0, mulx0z0, valx1z0, mulx1z0; 205 double valx0z0, mulx0z0, valx1z0, mulx1z0; 207 double valx0z1, mulx0z1, valx1z1, mulx1z1; 206 double valx0z1, mulx0z1, valx1z1, mulx1z1; 208 valx0z0= table[xindex ][0][zindex]; mulx 207 valx0z0= table[xindex ][0][zindex]; mulx0z0= (1-xlocal) * (1-zlocal); 209 valx1z0= table[xindex+1][0][zindex]; mulx 208 valx1z0= table[xindex+1][0][zindex]; mulx1z0= xlocal * (1-zlocal); 210 valx0z1= table[xindex ][0][zindex+1]; mul 209 valx0z1= table[xindex ][0][zindex+1]; mulx0z1= (1-xlocal) * zlocal; 211 valx1z1= table[xindex+1][0][zindex+1]; mul 210 valx1z1= table[xindex+1][0][zindex+1]; mulx1z1= xlocal * zlocal; 212 #endif 211 #endif 213 212 214 // Full 3-dimensional version 213 // Full 3-dimensional version 215 Bfield[0] = 214 Bfield[0] = 216 xField[xindex ][yindex ][zindex ] * ( 215 xField[xindex ][yindex ][zindex ] * (1-xlocal) * (1-ylocal) * (1-zlocal) + 217 xField[xindex ][yindex ][zindex+1] * ( 216 xField[xindex ][yindex ][zindex+1] * (1-xlocal) * (1-ylocal) * zlocal + 218 xField[xindex ][yindex+1][zindex ] * ( 217 xField[xindex ][yindex+1][zindex ] * (1-xlocal) * ylocal * (1-zlocal) + 219 xField[xindex ][yindex+1][zindex+1] * ( 218 xField[xindex ][yindex+1][zindex+1] * (1-xlocal) * ylocal * zlocal + 220 xField[xindex+1][yindex ][zindex ] * 219 xField[xindex+1][yindex ][zindex ] * xlocal * (1-ylocal) * (1-zlocal) + 221 xField[xindex+1][yindex ][zindex+1] * 220 xField[xindex+1][yindex ][zindex+1] * xlocal * (1-ylocal) * zlocal + 222 xField[xindex+1][yindex+1][zindex ] * 221 xField[xindex+1][yindex+1][zindex ] * xlocal * ylocal * (1-zlocal) + 223 xField[xindex+1][yindex+1][zindex+1] * 222 xField[xindex+1][yindex+1][zindex+1] * xlocal * ylocal * zlocal ; 224 Bfield[1] = 223 Bfield[1] = 225 yField[xindex ][yindex ][zindex ] * ( 224 yField[xindex ][yindex ][zindex ] * (1-xlocal) * (1-ylocal) * (1-zlocal) + 226 yField[xindex ][yindex ][zindex+1] * ( 225 yField[xindex ][yindex ][zindex+1] * (1-xlocal) * (1-ylocal) * zlocal + 227 yField[xindex ][yindex+1][zindex ] * ( 226 yField[xindex ][yindex+1][zindex ] * (1-xlocal) * ylocal * (1-zlocal) + 228 yField[xindex ][yindex+1][zindex+1] * ( 227 yField[xindex ][yindex+1][zindex+1] * (1-xlocal) * ylocal * zlocal + 229 yField[xindex+1][yindex ][zindex ] * 228 yField[xindex+1][yindex ][zindex ] * xlocal * (1-ylocal) * (1-zlocal) + 230 yField[xindex+1][yindex ][zindex+1] * 229 yField[xindex+1][yindex ][zindex+1] * xlocal * (1-ylocal) * zlocal + 231 yField[xindex+1][yindex+1][zindex ] * 230 yField[xindex+1][yindex+1][zindex ] * xlocal * ylocal * (1-zlocal) + 232 yField[xindex+1][yindex+1][zindex+1] * 231 yField[xindex+1][yindex+1][zindex+1] * xlocal * ylocal * zlocal ; 233 Bfield[2] = 232 Bfield[2] = 234 zField[xindex ][yindex ][zindex ] * ( 233 zField[xindex ][yindex ][zindex ] * (1-xlocal) * (1-ylocal) * (1-zlocal) + 235 zField[xindex ][yindex ][zindex+1] * ( 234 zField[xindex ][yindex ][zindex+1] * (1-xlocal) * (1-ylocal) * zlocal + 236 zField[xindex ][yindex+1][zindex ] * ( 235 zField[xindex ][yindex+1][zindex ] * (1-xlocal) * ylocal * (1-zlocal) + 237 zField[xindex ][yindex+1][zindex+1] * ( 236 zField[xindex ][yindex+1][zindex+1] * (1-xlocal) * ylocal * zlocal + 238 zField[xindex+1][yindex ][zindex ] * 237 zField[xindex+1][yindex ][zindex ] * xlocal * (1-ylocal) * (1-zlocal) + 239 zField[xindex+1][yindex ][zindex+1] * 238 zField[xindex+1][yindex ][zindex+1] * xlocal * (1-ylocal) * zlocal + 240 zField[xindex+1][yindex+1][zindex ] * 239 zField[xindex+1][yindex+1][zindex ] * xlocal * ylocal * (1-zlocal) + 241 zField[xindex+1][yindex+1][zindex+1] * 240 zField[xindex+1][yindex+1][zindex+1] * xlocal * ylocal * zlocal ; 242 241 243 } else { 242 } else { 244 Bfield[0] = 0.0; 243 Bfield[0] = 0.0; 245 Bfield[1] = 0.0; 244 Bfield[1] = 0.0; 246 Bfield[2] = 0.0; 245 Bfield[2] = 0.0; 247 } 246 } 248 } 247 } 249 248 250 249