Geant4 Cross Reference |
1 // ******************************************* 1 2 // * License and Disclaimer 3 // * 4 // * The Geant4 software is copyright of th 5 // * the Geant4 Collaboration. It is provided 6 // * conditions of the Geant4 Software License 7 // * LICENSE and available at http://cern.ch/ 8 // * include a list of copyright holders. 9 // * 10 // * Neither the authors of this software syst 11 // * institutes,nor the agencies providing fin 12 // * work make any representation or warran 13 // * regarding this software system or assum 14 // * use. Please see the license in the file 15 // * for the full disclaimer and the limitatio 16 // * 17 // * This code implementation is the result 18 // * technical work of the GEANT4 collaboratio 19 // * By using, copying, modifying or distri 20 // * any work based on the software) you ag 21 // * use in resulting scientific publicati 22 // * acceptance of all terms of the Geant4 Sof 23 // ******************************************* 24 // 25 // G4BorisScheme implementation 26 // 27 // Author: Divyansh Tiwari, Google Summer of C 28 // Supervision: John Apostolakis,Renee Fatemi, 29 // ------------------------------------------- 30 31 #include "G4BorisScheme.hh" 32 #include "G4FieldUtils.hh" 33 #include"G4SystemOfUnits.hh" 34 #include "globals.hh" 35 #include "G4PhysicalConstants.hh" 36 37 #include "G4EquationOfMotion.hh" 38 //#include "G4EqMagElectricField.hh" 39 40 using namespace field_utils; 41 42 G4BorisScheme::G4BorisScheme( G4EquationOfMoti 43 G4int 44 : fEquation(equation), fnvar(nvar) 45 { 46 if (nvar <= 0) 47 { 48 G4Exception("G4BorisScheme::G4BorisScheme( 49 "GeomField0002", FatalExceptio 50 "Invalid number of variables; 51 } 52 } 53 54 void G4BorisScheme::DoStep(const G4double rest 55 G4double yOut 56 { 57 G4double yOut1Temp[G4FieldTrack::ncompSVEC]; 58 G4double yOut2Temp[G4FieldTrack::ncompSVEC]; 59 60 // Used the scheme described in the followin 61 UpdatePosition(restMass, charge, yIn, yOut1 62 UpdateVelocity(restMass, charge, yOut1Temp, 63 UpdatePosition(restMass, charge, yOut2Temp, 64 } 65 66 void G4BorisScheme::UpdatePosition(const G4dou 67 G4double yO 68 { 69 // Particle information 70 copy(yOut, yIn); 71 72 // Obtaining velocity 73 G4ThreeVector momentum_vec =G4ThreeVector( 74 G4double momentum_mag = momentum_vec.mag() 75 G4ThreeVector momentum_dir =(1.0/momentum_ 76 77 G4double velocity_mag = momentum_mag*(c_l) 78 G4ThreeVector velocity = momentum_dir*velo 79 80 //Obtaining the time step from the length 81 82 hstep /= velocity_mag*CLHEP::m; 83 84 // Updating the Position 85 for(G4int i = 0; i <3; i++ ) 86 { 87 G4double pos = yIn[i]/CLHEP::m; 88 pos += hstep*velocity[i]; 89 yOut[i] = pos*CLHEP::m; 90 } 91 } 92 93 void G4BorisScheme::UpdateVelocity(const G4dou 94 G4double yO 95 { 96 //Particle information 97 G4ThreeVector momentum_vec =G4ThreeVector( 98 G4double momentum_mag = momentum_vec.mag() 99 G4ThreeVector momentum_dir =(1.0/momentum_ 100 101 G4double gamma = std::sqrt(sqr(momentum_ma 102 103 G4double mass = (restMass/c_squared)/CLHEP 104 105 //Obtaining velocity 106 107 G4double velocity_mag = momentum_mag*(c_l) 108 G4ThreeVector velocity = momentum_dir*velo 109 110 ////Obtaining the time step from the lengt 111 112 hstep /= velocity_mag*CLHEP::m; 113 114 // Obtaining the field values 115 G4double dydx[G4FieldTrack::ncompSVEC]; 116 G4double fieldValue[6] ={0,0,0,0,0,0}; 117 fEquation->EvaluateRhsReturnB(yIn, dydx, f 118 119 //Initializing Vectors 120 G4ThreeVector B; 121 G4ThreeVector E; 122 copy(yOut, yIn); 123 for( G4int i = 0; i < 3; i++) 124 { 125 E[i] = fieldValue[i+3]/CLHEP::volt*CLH 126 B[i] = fieldValue[i]/CLHEP::tesla; 127 } 128 129 //Boris Algorithm 130 G4double qd = hstep*(charge/(2*mass*gamma) 131 G4ThreeVector h = qd*B; 132 G4ThreeVector u = velocity + qd*E; 133 G4double h_l = h[0]*h[0] + h[1]*h[1] + h[2 134 G4ThreeVector s_1 = (2*h)/(1 + h_l); 135 G4ThreeVector ud = u + (u + u.cross(h)).cr 136 G4ThreeVector v_fi = ud +qd*E; 137 G4double v_mag = std::sqrt(v_fi.mag2()); 138 G4ThreeVector v_dir = v_fi/v_mag; 139 G4double momen_mag = (restMass*v_mag)/(std 140 G4ThreeVector momen = momen_mag*v_dir; 141 142 // Storing the updated momentum 143 for(int i = 3; i < 6; i++) 144 { 145 yOut[i] = momen[i-3]; 146 } 147 } 148 149 // ------------------------------------------- 150 151 void G4BorisScheme::copy(G4double dst[], const 152 { 153 std::memcpy(dst, src, sizeof(G4double) * fnv 154 } 155 156 // ------------------------------------------- 157 // - Methods using the Boris Scheme Stepping t 158 // ------------------------------------------- 159 void G4BorisScheme:: 160 StepWithErrorEstimate(const G4double yIn[], G4 161 G4double yOut[], G4doubl 162 { 163 // Use two half-steps (comparing to a full 164 G4double yMid[G4FieldTrack::ncompSVEC]; 165 StepWithMidAndErrorEstimate(yIn, restMass, 166 } 167 168 // ------------------------------------------- 169 170 void G4BorisScheme:: 171 StepWithMidAndErrorEstimate(const G4double yIn 172 G4double yMi 173 ) const 174 { 175 G4double halfStep= 0.5*hstep; 176 G4double yOutAlt[G4FieldTrack::ncompSVEC]; 177 178 // In a single step 179 DoStep(restMass, charge, yIn, yOutAlt, hst 180 181 // Same, and also return mid-point evaluati 182 DoStep(restMass, charge, yIn, yMid, halfSt 183 DoStep(restMass, charge, yMid, yOut, halfSt 184 185 for( G4int i= 0; i<fnvar; i++ ) 186 { 187 yErr[i] = yOutAlt[i] - yOut[i]; 188 } 189 } 190