Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer << 3 // * DISCLAIMER * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th << 5 // * The following disclaimer summarizes all the specific disclaimers * 6 // * the Geant4 Collaboration. It is provided << 6 // * of contributors to this software. The specific disclaimers,which * 7 // * conditions of the Geant4 Software License << 7 // * govern, are listed with their locations in: * 8 // * LICENSE and available at http://cern.ch/ << 8 // * http://cern.ch/geant4/license * 9 // * include a list of copyright holders. << 10 // * 9 // * * 11 // * Neither the authors of this software syst 10 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 11 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 12 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 13 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file << 14 // * use. * 16 // * for the full disclaimer and the limitatio << 17 // * 15 // * * 18 // * This code implementation is the result << 16 // * This code implementation is the intellectual property of the * 19 // * technical work of the GEANT4 collaboratio << 17 // * GEANT4 collaboration. * 20 // * By using, copying, modifying or distri << 18 // * By copying, distributing or modifying the Program (or any work * 21 // * any work based on the software) you ag << 19 // * based on the Program) you indicate your acceptance of this * 22 // * use in resulting scientific publicati << 20 // * statement, and all its terms. * 23 // * acceptance of all terms of the Geant4 Sof << 24 // ******************************************* 21 // ******************************************************************** 25 // 22 // 26 // G4MagInt_Driver implementation << 27 // 23 // 28 // V.Grichine, 07.10.1996 - Created << 24 // $Id: G4MagIntegratorDriver.cc,v 1.20 2001/11/21 16:43:16 gcosmo Exp $ 29 // W.Wander, 28.01.1998 - Added ability for lo << 25 // GEANT4 tag $Name: geant4-04-00 $ 30 // J.Apostolakis, 08.11.2001 - Respect minimum << 26 // 31 // ------------------------------------------- << 27 // 32 << 28 // 33 #include <iomanip> << 29 // Implementation for class G4MagInt_Driver 34 << 30 // Tracking in space dependent magnetic field 35 #include "globals.hh" << 31 // 36 #include "G4SystemOfUnits.hh" << 32 // History of major changes: 37 #include "G4GeometryTolerance.hh" << 33 // 7 Oct 96 V. Grichine First version >> 34 // 28 Jan 98 W. Wander: Added ability for low order integrators >> 35 // 30 Jan 98 J. Apostolakis: Made method parameters into instance variables >> 36 // 27 Jul 99 J. Apostolakis: Ensured that AccurateAdvance does not loop >> 37 // due to very small eps & step size (precision) >> 38 #include <math.h> >> 39 #include "G4ios.hh" 38 #include "G4MagIntegratorDriver.hh" 40 #include "G4MagIntegratorDriver.hh" 39 #include "G4FieldTrack.hh" 41 #include "G4FieldTrack.hh" >> 42 #include "geomdefs.hh" // for kCarTolerance 40 43 41 #ifdef G4DEBUG_FIELD << 44 // Stepsize can increase by no more than 5.0 42 #include "G4DriverReporter.hh" << 45 // and decrease by no more than 1/10. = 0.1 43 #endif << 46 // >> 47 const G4double G4MagInt_Driver::max_stepping_increase = 5.0; >> 48 const G4double G4MagInt_Driver::max_stepping_decrease = 0.1; 44 49 45 // ------------------------------------------- << 50 // The (default) maximum number of steps is Base divided by the order of Stepper >> 51 // >> 52 const G4int G4MagInt_Driver::fMaxStepBase = 500; // Was 5000 46 53 47 // Constructor 54 // Constructor 48 // 55 // 49 G4MagInt_Driver::G4MagInt_Driver( G4double 56 G4MagInt_Driver::G4MagInt_Driver( G4double hminimum, 50 G4MagIntegra << 57 G4MagIntegratorStepper *pItsStepper, 51 G4int << 58 G4int numComponents) 52 G4int << 59 : nvar(numComponents), 53 : fNoIntegrationVariables(numComponents), << 60 fVerboseLevel(0) 54 fNoVars( std::max( fNoIntegrationVariables << 55 fStatisticsVerboseLevel(statisticsVerbose) << 56 { 61 { 57 // In order to accomodate "Laboratory Time", << 62 RenewStepperAndAdjust( pItsStepper ); 58 // is required. For proper time of flight an << 63 hminimum_val= hminimum; 59 << 64 fMaxNoSteps = fMaxStepBase / pIntStepper->IntegratorOrder(); 60 RenewStepperAndAdjust( pStepper ); << 61 fMinimumStep = hminimum; << 62 << 63 fMaxNoSteps = fMaxStepBase / pIntStepper->In << 64 #ifdef G4DEBUG_FIELD << 65 fVerboseLevel=2; << 66 #endif << 67 << 68 if( (fVerboseLevel > 0) || (fStatisticsVerbo << 69 { << 70 G4cout << "MagIntDriver version: Accur-Adv << 71 << "invE_nS, QuickAdv-2sqrt with St << 72 #ifdef G4FLD_STATS << 73 << " enabled " << 74 #else << 75 << " disabled " << 76 #endif << 77 << G4endl; << 78 } << 79 } 65 } 80 66 81 // ------------------------------------------- << 82 << 83 // Destructor 67 // Destructor 84 // 68 // 85 G4MagInt_Driver::~G4MagInt_Driver() 69 G4MagInt_Driver::~G4MagInt_Driver() 86 { << 70 { 87 if( fStatisticsVerboseLevel > 1 ) << 88 { << 89 PrintStatisticsReport(); << 90 } << 91 } 71 } 92 72 93 // ------------------------------------------- << 73 // To add much printing for debugging purposes, uncomment this: >> 74 // #define G4DEBUG_FIELD 1 94 75 95 G4bool 76 G4bool 96 G4MagInt_Driver::AccurateAdvance(G4FieldTrack& 77 G4MagInt_Driver::AccurateAdvance(G4FieldTrack& y_current, 97 G4double << 78 G4double hstep, 98 G4double << 79 G4double eps ) 99 G4double << 80 // const G4double dydx[6], // We could may add this ?? 100 { << 81 101 // Runge-Kutta driver with adaptive stepsize << 82 // Runge-Kutta driver with adaptive stepsize control. Integrate starting 102 // values at y_current over hstep x2 with ac << 83 // values at y_current over hstep x2 with accuracy eps. 103 // On output ystart is replaced by values at << 84 // On output ystart is replaced by values at the end of the integration 104 // interval. RightHandSide is the right-hand << 85 // interval. 105 // The source is similar to odeint routine f << 86 // RightHandSide is the right-hand side of ODE system. >> 87 // The source is similar to odeint routine from NRC p.721-722 . 106 88 107 G4int nstp, i; << 89 { 108 G4double x, hnext, hdid, h; << 90 G4int nstp, i; >> 91 G4double x, hnext, hdid, h ; 109 92 >> 93 G4int no_warnings=0; 110 #ifdef G4DEBUG_FIELD 94 #ifdef G4DEBUG_FIELD 111 G4int no_warnings = 0; << 95 static G4int dbg=0; 112 static G4int dbg = 1; << 96 dbg=1; 113 static G4int nStpPr = 50; // For debug pri << 97 fVerboseLevel=2; 114 G4double ySubStepStart[G4FieldTrack::ncompSV << 115 G4FieldTrack yFldTrkStart(y_current); << 116 #endif 98 #endif 117 99 118 G4double y[G4FieldTrack::ncompSVEC] = {0., 0 << 100 // G4double yscal[ncompSVEC]; 119 G4double dydx[G4FieldTrack::ncompSVEC] = {0. << 101 G4double y[G4FieldTrack::ncompSVEC], dydx[G4FieldTrack::ncompSVEC]; 120 G4double ystart[G4FieldTrack::ncompSVEC] = { << 102 G4double ystart[G4FieldTrack::ncompSVEC], yEnd[G4FieldTrack::ncompSVEC]; 121 G4double yEnd[G4FieldTrack::ncompSVEC] = {0. << 122 G4double x1, x2; 103 G4double x1, x2; 123 G4bool succeeded = true; << 104 G4bool succeeded = true, lastStepSucceeded; 124 << 125 G4double startCurveLength; << 126 << 127 const G4int nvar = fNoVars; << 128 105 129 G4FieldTrack yStartFT(y_current); 106 G4FieldTrack yStartFT(y_current); 130 107 131 // Ensure that hstep > 0 << 108 // Assume that hstep > 0 132 // << 133 if( hstep <= 0.0 ) << 134 { << 135 if( hstep == 0.0 ) << 136 { << 137 std::ostringstream message; << 138 message << "Proposed step is zero; hstep << 139 G4Exception("G4MagInt_Driver::AccurateAd << 140 "GeomField1001", JustWarning << 141 return succeeded; << 142 } << 143 << 144 std::ostringstream message; << 145 message << "Invalid run condition." << G4e << 146 << "Proposed step is negative; hst << 147 << "Requested step cannot be negat << 148 G4Exception("G4MagInt_Driver::AccurateAdva << 149 "GeomField0003", EventMustBeAb << 150 return false; << 151 } << 152 109 >> 110 // ystart = y_current.PosVelVec(); 153 y_current.DumpToArray( ystart ); 111 y_current.DumpToArray( ystart ); 154 << 112 x1= y_current.GetCurveLength(); 155 startCurveLength= y_current.GetCurveLength() << 156 x1= startCurveLength; << 157 x2= x1 + hstep; 113 x2= x1 + hstep; 158 << 114 159 if ( (hinitial > 0.0) && (hinitial < hstep) << 115 // // Initial Step size "h" is half the interval 160 && (hinitial > perMillion * hstep) ) << 116 // h = 0.5 * hstep; 161 { << 117 // Initial Step size "h" is the full interval 162 h = hinitial; << 118 h = hstep; 163 } << 164 else // Initial Step size "h" defaults to << 165 { << 166 h = hstep; << 167 } << 168 << 169 x = x1; 119 x = x1; 170 120 171 for ( i=0; i<nvar; ++i) { y[i] = ystart[i]; << 121 G4int noFullIntegr=0, noSmallIntegr = 0 ; 172 << 122 static G4int noGoodSteps =0, noBadSteps = 0 ; // Bad = chord > curve-len 173 G4bool lastStep= false; << 174 nstp = 1; << 175 << 176 do << 177 { << 178 G4ThreeVector StartPos( y[0], y[1], y[2] ) << 179 << 180 #ifdef G4DEBUG_FIELD << 181 G4double xSubStepStart= x; << 182 for (i=0; i<nvar; ++i) { ySubStepStart[i] << 183 yFldTrkStart.LoadFromArray(y, fNoIntegrati << 184 yFldTrkStart.SetCurveLength(x); << 185 #endif << 186 << 187 pIntStepper->RightHandSide( y, dydx ); << 188 ++fNoTotalSteps; << 189 << 190 // Perform the Integration << 191 // << 192 if( h > fMinimumStep ) << 193 { << 194 OneGoodStep(y,dydx,x,h,eps,hdid,hnext) ; << 195 //-------------------------------------- << 196 #ifdef G4DEBUG_FIELD << 197 if (dbg>2) << 198 { << 199 // PrintStatus( ySubStepStart, xSubSt << 200 G4DriverReporter::PrintStatus( ySubSte << 201 } << 202 #endif << 203 } << 204 else << 205 { << 206 G4FieldTrack yFldTrk( G4ThreeVector(0,0, << 207 G4ThreeVector(0,0, << 208 G4double dchord_step, dyerr, dyerr_len; << 209 yFldTrk.LoadFromArray(y, fNoIntegrationV << 210 yFldTrk.SetCurveLength( x ); << 211 << 212 QuickAdvance( yFldTrk, dydx, h, dchord_s << 213 //-------------------------------------- << 214 << 215 yFldTrk.DumpToArray(y); << 216 << 217 #ifdef G4FLD_STATS << 218 ++fNoSmallSteps; << 219 if ( dyerr_len > fDyerr_max ) { fDyerr_ << 220 fDyerrPos_smTot += dyerr_len; << 221 fSumH_sm += h; // Length total for 'sma << 222 if (nstp==1) { ++fNoInitialSmallSteps; << 223 #endif << 224 #ifdef G4DEBUG_FIELD << 225 if (dbg>1) << 226 { << 227 if(fNoSmallSteps<2) { PrintStatus(ySub << 228 G4cout << "Another sub-min step, no " << 229 << " of " << fNoTotalSteps << " << 230 PrintStatus( ySubStepStart, x1, y, x, << 231 G4cout << " dyerr= " << dyerr_len << " << 232 << " epsilon= " << eps << " hst << 233 << " h= " << h << " hmin= " << << 234 } << 235 #endif << 236 if( h == 0.0 ) << 237 { << 238 G4Exception("G4MagInt_Driver::Accurate << 239 "GeomField0003", FatalExce << 240 "Integration Step became Z << 241 } << 242 dyerr = dyerr_len / h; << 243 hdid = h; << 244 x += hdid; << 245 123 246 // Compute suggested new step << 124 for(i=0;i<nvar;i++) y[i] = ystart[i] ; 247 hnext = ComputeNewStepSize( dyerr/eps, h << 248 } << 249 125 250 G4ThreeVector EndPos( y[0], y[1], y[2] ); << 126 G4bool lastStep= false; >> 127 nstp=1; >> 128 // G4double lastStepThreshold = G4std::min( eps * hstep, Hmin() ); >> 129 >> 130 do{ >> 131 G4ThreeVector StartPos( y[0], y[1], y[2] ); >> 132 >> 133 pIntStepper->RightHandSide( y, dydx ); >> 134 >> 135 if( x+h > x2 ) { >> 136 h = x2 - x ; // When stepsize overshoots, decrease it! >> 137 } >> 138 if( h < eps * hstep) { >> 139 lastStep = true; // Ensure that this must be the last step >> 140 // because otherwise numerical (im)precision >> 141 // could otherwise force lots of small last steps. >> 142 } >> 143 >> 144 // static G4int nStpPr=50; // For debug printing of integrations with many steps >> 145 >> 146 // Perform the Integration >> 147 // >> 148 >> 149 if( h > Hmin() ){ >> 150 OneGoodStep(y,dydx,x,h,eps,hdid,hnext) ; >> 151 //-------------------------------------- >> 152 lastStepSucceeded= (hdid == h); >> 153 }else{ >> 154 #if 0 >> 155 OneGoodStep(y,dydx,x,h,2*eps,hdid,hnext) ; >> 156 //-------------------------------------- >> 157 lastStepSucceeded= (hdid == h); >> 158 #else >> 159 G4FieldTrack yFldTrk( G4ThreeVector(0,0,0), >> 160 G4ThreeVector(0,0,0), 0., 0., 0., 0. ); >> 161 G4double dchord_step, dyerr, dyerr_len; // Must figure what to do with these >> 162 yFldTrk.LoadFromArray(y); >> 163 yFldTrk.SetCurveLength( x ); >> 164 // G4double s_start = yFldTrk.GetCurveLength(); >> 165 QuickAdvance( yFldTrk, dydx, h, dchord_step, dyerr_len ); >> 166 # ifdef G4DEBUG_FIELD >> 167 if(dbg>1) OneGoodStep(y,dydx,x,h,2*eps,hdid,hnext) ; >> 168 if(dbg>1) PrintStatus( ystart, x1, y, x, h, -nstp); >> 169 yFldTrk.DumpToArray(y); >> 170 if(dbg>1) PrintStatus( ystart, x1, y, x, h, nstp); // Only this >> 171 # endif >> 172 dyerr = dyerr_len / hstep; >> 173 hdid= h; >> 174 x += hdid; >> 175 // Compute suggested new step >> 176 hnext= ComputeNewStepSize( dyerr/eps, h); >> 177 lastStepSucceeded= (dyerr<= eps); >> 178 #endif >> 179 } >> 180 // #ifdef G4DEBUG_FIELD >> 181 if(lastStepSucceeded) noFullIntegr++ ; else noSmallIntegr++ ; >> 182 G4ThreeVector EndPos( y[0], y[1], y[2] ); 251 183 252 #ifdef G4DEBUG_FIELD 184 #ifdef G4DEBUG_FIELD 253 if( (dbg>0) && (dbg<=2) && (nstp>nStpPr)) << 185 if(nstp>nStpPr) { 254 { << 186 G4cout << "hdid=" << hdid << "hnext =" << hnext << " " << endl; 255 if( nstp==nStpPr ) { G4cout << "***** M << 187 PrintStatus( ystart, x1, y, x, h, nstp==nStpPr ? -nstp: nstp); 256 G4cout << "MagIntDrv: " ; << 188 } 257 G4cout << "hdid=" << std::setw(12) << h << 258 << "hnext=" << std::setw(12) << h << 259 << "hstep=" << std::setw(12) << h << 260 << G4endl; << 261 PrintStatus( ystart, x1, y, x, h, (nstp= << 262 } << 263 #endif << 264 << 265 // Check the endpoint << 266 G4double endPointDist= (EndPos-StartPos).m << 267 if ( endPointDist >= hdid*(1.+perMillion) << 268 { << 269 ++fNoBadSteps; << 270 << 271 // Issue a warning only for gross differ << 272 // we understand how small difference oc << 273 if ( endPointDist >= hdid*(1.+perThousan << 274 { << 275 #ifdef G4DEBUG_FIELD << 276 if (dbg) << 277 { << 278 WarnEndPointTooFar ( endPointDist, h << 279 G4cerr << " Total steps: bad " << << 280 << " current h= " << hdid << << 281 PrintStatus( ystart, x1, y, x, hstep << 282 } << 283 ++no_warnings; << 284 #endif 189 #endif 285 } << 286 } << 287 190 288 // Avoid numerous small last steps << 191 // Check the endpoint 289 if( (h < eps * hstep) || (h < fSmallestFra << 192 G4double endPointDist= (EndPos-StartPos).mag(); 290 { << 193 if( endPointDist >= hdid*(1.+perMillion) ){ 291 // No more integration -- the next step << 194 noBadSteps ++; 292 lastStep = true; << 195 // Issue a warning only for gross differences - 293 } << 196 // we understand how small difference occur. 294 else << 197 if( endPointDist >= hdid*(1.+perThousand) ){ 295 { << 198 #ifdef G4DEBUG_FIELD 296 // Check the proposed next stepsize << 199 WarnEndPointTooFar ( endPointDist, hdid, eps, dbg ); 297 if(std::fabs(hnext) <= Hmin()) << 200 G4cerr << " Total steps: bad" << noBadSteps << " good " << noGoodSteps << endl; 298 { << 201 // G4cerr << "Mid:EndPtFar> "; >> 202 PrintStatus( ystart, x1, y, x, hstep, no_warnings?nstp:-nstp); >> 203 // Potentially add as arguments: <dydx> - as Initial Force >> 204 #endif >> 205 no_warnings++; >> 206 } >> 207 } else { // ie (!dbg) >> 208 noGoodSteps ++; >> 209 } >> 210 // #endif >> 211 >> 212 // Check the proposed next stepsize >> 213 if(fabs(hnext) <= Hmin()) >> 214 { 299 #ifdef G4DEBUG_FIELD 215 #ifdef G4DEBUG_FIELD 300 // If simply a very small interval is 216 // If simply a very small interval is being integrated, do not warn 301 if( (x < x2 * (1-eps) ) && // T << 217 if( (x < x2 * (1-eps) ) && // The last step can be small: it's OK 302 (std::fabs(hstep) > Hmin()) ) // a << 218 (fabs(hstep) > Hmin()) // and if we are asked, it's OK 303 { << 219 // && (hnext < hstep * PerThousand ) 304 if(dbg>0) << 220 ){ 305 { << 221 // Issue WARNING 306 WarnSmallStepSize( hnext, hstep, h << 222 WarnSmallStepSize( hnext, hstep, h, x-x1, nstp ); 307 PrintStatus( ystart, x1, y, x, hst << 223 // G4cerr << "Mid:SmallStep> "; 308 } << 224 PrintStatus( ystart, x1, y, x, hstep, no_warnings?nstp:-nstp); 309 ++no_warnings; << 225 no_warnings++; 310 } << 226 } 311 #endif << 227 #endif >> 228 // else >> 229 // succeeded = false; // Meaningful only if we break out of the loop. >> 230 // >> 231 // lastStep = true; // Make this the last step ... Dubious now >> 232 312 // Make sure that the next step is at 233 // Make sure that the next step is at least Hmin. 313 h = Hmin(); 234 h = Hmin(); 314 } << 235 }else{ 315 else << 236 h = hnext ; 316 { << 237 } 317 h = hnext; << 238 318 } << 239 }while ( ((nstp++)<=fMaxNoSteps) >> 240 && (x < x2) // Have we reached the end ? >> 241 // --> a better test might be x-x2 > an_epsilon >> 242 && (!lastStep) >> 243 ); 319 244 320 // Ensure that the next step does not o << 245 succeeded= (x>=x2); // If it was a "forced" last step 321 if ( x+h > x2 ) << 322 { // When stepsize oversh << 323 h = x2 - x ; // Must cope with diffi << 324 } // issues if hstep << x << 325 246 326 if ( h == 0.0 ) << 247 for(i=0;i<nvar;i++) yEnd[i] = y[i] ; 327 { << 328 // Cannot progress - accept this as la << 329 lastStep = true; << 330 #ifdef G4DEBUG_FIELD << 331 if (dbg>2) << 332 { << 333 int prec= G4cout.precision(12); << 334 G4cout << "Warning: G4MagIntegratorD << 335 << G4endl << 336 << " Integration step 'h' be << 337 << h << " due to roundoff. " << 338 << " Calculated as difference << 339 << " Forcing termination of << 340 G4cout.precision(prec); << 341 } << 342 #endif << 343 } << 344 } << 345 } while ( ((++nstp)<=fMaxNoSteps) && (x < x2 << 346 // Loop checking, 07.10.2016, J. Apostolakis << 347 << 348 // Have we reached the end ? << 349 // --> a better test might be x-x2 > an_e << 350 << 351 succeeded = (x>=x2); // If it was a "forced << 352 << 353 for (i=0; i<nvar; ++i) { yEnd[i] = y[i]; } << 354 248 355 // Put back the values. 249 // Put back the values. 356 y_current.LoadFromArray( yEnd, fNoIntegratio << 250 y_current.LoadFromArray( yEnd ); 357 y_current.SetCurveLength( x ); 251 y_current.SetCurveLength( x ); 358 252 359 if(nstp > fMaxNoSteps) << 253 if(nstp > fMaxNoSteps){ 360 { << 254 no_warnings++; 361 succeeded = false; << 255 succeeded = false; 362 #ifdef G4DEBUG_FIELD << 256 #ifdef G4DEBUG_FIELD 363 ++no_warnings; << 257 WarnTooManyStep( x1, x2, x ); // Issue WARNING 364 if (dbg) << 258 PrintStatus( yEnd, x1, y, x, hstep, -nstp); 365 { << 366 WarnTooManyStep( x1, x2, x ); // Issue << 367 PrintStatus( yEnd, x1, y, x, hstep, -nst << 368 } << 369 #endif 259 #endif 370 } 260 } 371 261 372 #ifdef G4DEBUG_FIELD 262 #ifdef G4DEBUG_FIELD 373 if( dbg && no_warnings ) << 263 if( no_warnings ){ 374 { << 264 G4cerr << " Exiting status: " 375 G4cerr << "G4MagIntegratorDriver exit stat << 265 << " no-steps " << nstp 376 PrintStatus( yEnd, x1, y, x, hstep, nstp); << 266 <<endl; >> 267 PrintStatus( yEnd, x1, y, x, hstep, nstp); 377 } 268 } 378 #endif 269 #endif 379 270 380 return succeeded; 271 return succeeded; 381 } // end of AccurateAdvance ................. << 382 272 383 // ------------------------------------------- << 273 } // end of AccurateAdvance ........................... 384 274 385 void 275 void 386 G4MagInt_Driver::WarnSmallStepSize( G4double h 276 G4MagInt_Driver::WarnSmallStepSize( G4double hnext, G4double hstep, 387 G4double h << 277 G4double h, G4double xDone, 388 G4int nstp << 278 G4int nstp) 389 { 279 { 390 static G4ThreadLocal G4int noWarningsIssued << 280 static G4int noWarningsIssued =0; 391 const G4int maxNoWarnings = 10; // Number << 281 const G4int maxNoWarnings = 10; // Number of verbose warnings 392 std::ostringstream message; << 282 if( (noWarningsIssued < maxNoWarnings) || fVerboseLevel > 10 ){ 393 if( (noWarningsIssued < maxNoWarnings) || fV << 283 G4cerr<< " Warning (G4MagIntegratorDriver::AccurateAdvance): The stepsize for the " 394 { << 284 << " next iteration=" << hnext << " is too small " 395 message << "The stepsize for the next iter << 285 << "- in Step number " << nstp << "." << G4endl; 396 << ", is too small - in Step numbe << 286 G4cerr << " The minimum for the driver is " << Hmin() << G4endl ; 397 << "The minimum for the driver is << 287 G4cerr << " Requested integr. length was " << hstep << " ." << G4endl ; 398 << "Requested integr. length was " << 288 G4cerr << " The size of this sub-step was " << h << " ." << G4endl ; 399 << "The size of this sub-step was << 289 G4cerr << " The integrations has already gone " << xDone << G4endl ; 400 << "The integrations has already g << 290 }else{ 401 } << 291 G4cerr<< " G4MagInt_Driver: Too small 'next' step " << hnext 402 else << 292 << " step-no " << nstp ; // << G4setw(4) 403 { << 293 G4cerr << " this sub-step " << h 404 message << "Too small 'next' step " << hne << 294 << " req_tot_len " << hstep 405 << ", step-no: " << nstp << G4endl << 295 << " done " << xDone 406 << ", this sub-step: " << h << 296 << " min " << Hmin() 407 << ", req_tot_len: " << hstep << 297 << G4endl ; 408 << ", done: " << xDone << ", min: << 409 } 298 } 410 G4Exception("G4MagInt_Driver::WarnSmallStepS << 299 noWarningsIssued++; 411 JustWarning, message); << 412 ++noWarningsIssued; << 413 } 300 } 414 301 415 // ------------------------------------------- << 416 << 417 void 302 void 418 G4MagInt_Driver::WarnTooManyStep( G4double x1s 303 G4MagInt_Driver::WarnTooManyStep( G4double x1start, 419 G4double x2e << 304 G4double x2end, 420 G4double xCu << 305 G4double xCurrent) 421 { 306 { 422 std::ostringstream message; << 307 G4cerr << " Warning (G4MagIntegratorDriver): The number of steps " 423 message << "The number of steps used in the << 308 << "used in the Integration driver (Runge-Kutta) is too many. " 424 << " (Runge-Kutta) is too many." << << 309 << G4endl ; 425 << "Integration of the interval was << 310 G4cerr << "Integration of the interval was not completed - only a " 426 << "Only a " << (xCurrent-x1start)* << 311 << (xCurrent-x1start)*100/(x2end-x1start) 427 << " % fraction of it was done."; << 312 <<" % fraction of it was Done." << G4endl; 428 G4Exception("G4MagInt_Driver::WarnTooManySt << 429 JustWarning, message); << 430 } 313 } 431 314 432 // ------------------------------------------- << 433 << 434 void 315 void 435 G4MagInt_Driver::WarnEndPointTooFar (G4double 316 G4MagInt_Driver::WarnEndPointTooFar (G4double endPointDist, 436 G4double << 317 G4double h , 437 G4double << 318 G4double eps, 438 G4int << 319 G4int dbg) 439 { << 320 { 440 static G4ThreadLocal G4double maxRelError = << 321 static G4double maxRelError= 0.0, maxRelError_last_printed=0.0; 441 G4bool isNewMax, prNewMax; << 322 G4bool isNewMax, prNewMax; 442 << 323 443 isNewMax = endPointDist > (1.0 + maxRelError << 324 isNewMax = endPointDist > (1.0 + maxRelError) * h; 444 prNewMax = endPointDist > (1.0 + 1.05 * maxR << 325 prNewMax = endPointDist > (1.0 + 1.05 * maxRelError) * h; 445 if( isNewMax ) { maxRelError= endPointDist / << 326 if( isNewMax ) 446 << 327 maxRelError= endPointDist / h - 1.0; 447 if( (dbg != 0) && (h > G4GeometryTolerance:: << 328 if( prNewMax ) 448 && ( (dbg>1) || prNewMax || (endPoin << 329 maxRelError_last_printed = maxRelError; 449 { << 330 450 static G4ThreadLocal G4int noWarnings = 0; << 331 if( dbg 451 std::ostringstream message; << 332 && (h > kCarTolerance) 452 if( (noWarnings++ < 10) || (dbg>2) ) << 333 && ( (dbg>1) || prNewMax || (endPointDist >= h*(1.+eps) ) ) 453 { << 334 ){ 454 message << "The integration produced an << 335 static G4int noWarnings = 0; 455 << "is further from the start-po << 336 if( (noWarnings ++ < 10) || (dbg>2) ){ 456 << G4endl; << 337 G4cerr << " Warning (G4MagIntegratorDriver): " 457 } << 338 << " The integration produced an endpoint which " << G4endl 458 message << " Distance of endpoints = " << << 339 << " is further from the startpoint than the curve length." << G4endl; 459 << ", curve length = " << h << G4e << 340 460 << " Difference (curveLen-endpDis << 341 G4cerr << " Distance of endpoints = " << endPointDist 461 << ", relative = " << (h-endPointD << 342 << " curve length = " << h 462 << ", epsilon = " << eps; << 343 << " Difference (curveLen-endpDist)= " << (h - endPointDist) 463 G4Exception("G4MagInt_Driver::WarnEndPoint << 344 << " relative = " << (h-endPointDist) / h 464 JustWarning, message); << 345 << " epsilon = " << eps 465 } << 346 << G4endl; >> 347 }else{ >> 348 G4cerr << " Warning:" >> 349 << " dist_e= " << endPointDist >> 350 << " h_step = " << h >> 351 << " Diff (hs-ed)= " << (h - endPointDist) >> 352 << " rel = " << (h-endPointDist) / h >> 353 << " eps = " << eps >> 354 << " (from G4MagInt_Driver)" << G4endl; >> 355 } >> 356 } 466 } 357 } 467 << 468 // ------------------------------------------- 358 // --------------------------------------------------------- 469 359 470 void 360 void 471 G4MagInt_Driver::OneGoodStep( G4double y[ 361 G4MagInt_Driver::OneGoodStep( G4double y[], // InOut 472 const G4double dy << 362 const G4double dydx[], 473 G4double& x << 363 G4double& x, // InOut 474 G4double ht << 364 G4double htry, 475 G4double ep << 365 G4double eps_rel_max, 476 G4double& h << 366 G4double& hdid, // Out 477 G4double& h << 367 G4double& hnext ) // Out 478 368 479 // Driver for one Runge-Kutta Step with monito 369 // Driver for one Runge-Kutta Step with monitoring of local truncation error 480 // to ensure accuracy and adjust stepsize. Inp 370 // to ensure accuracy and adjust stepsize. Input are dependent variable 481 // array y[0,...,5] and its derivative dydx[0, 371 // array y[0,...,5] and its derivative dydx[0,...,5] at the 482 // starting value of the independent variable 372 // starting value of the independent variable x . Also input are stepsize 483 // to be attempted htry, and the required accu 373 // to be attempted htry, and the required accuracy eps. On output y and x 484 // are replaced by their new values, hdid is t 374 // are replaced by their new values, hdid is the stepsize that was actually 485 // accomplished, and hnext is the estimated ne 375 // accomplished, and hnext is the estimated next stepsize. 486 // This is similar to the function rkqs from t 376 // This is similar to the function rkqs from the book: 487 // Numerical Recipes in C: The Art of Scientif 377 // Numerical Recipes in C: The Art of Scientific Computing (NRC), Second 488 // Edition, by William H. Press, Saul A. Teuko 378 // Edition, by William H. Press, Saul A. Teukolsky, William T. 489 // Vetterling, and Brian P. Flannery (Cambridg 379 // Vetterling, and Brian P. Flannery (Cambridge University Press 1992), 490 // 16.2 Adaptive StepSize Control for Runge-Ku 380 // 16.2 Adaptive StepSize Control for Runge-Kutta, p. 719 491 381 492 { 382 { 493 G4double errmax_sq; << 383 G4double errpos_sq, errvel_sq, errmax_sq; 494 G4double h, htemp, xnew ; << 384 G4double errmax, h, htemp, xnew ; >> 385 G4int i; 495 386 496 G4double yerr[G4FieldTrack::ncompSVEC], ytem << 387 G4double yerr[G4FieldTrack::ncompSVEC], ytemp[G4FieldTrack::ncompSVEC]; 497 388 498 h = htry ; // Set stepsize to the initial tr << 389 h = htry ; // Set stepsize to the initial trial value 499 390 500 G4double inv_eps_vel_sq = 1.0 / (eps_rel_max << 391 // G4double inv_epspos_sq= 1.0 / eps * eps; 501 392 502 G4double errpos_sq = 0.0; // square of di << 393 for (;;) 503 G4double errvel_sq = 0.0; // square of mo << 394 { 504 G4double errspin_sq = 0.0; // square of sp << 395 pIntStepper-> Stepper(y,dydx,h,ytemp,yerr); 505 << 396 G4double eps_pos = eps_rel_max * G4std::max(h, Hmin()); 506 const G4int max_trials=100; << 397 // Evaluate accuracy 507 << 398 // 508 G4ThreeVector Spin(y[9],y[10],y[11]); << 399 errpos_sq = sqr(yerr[0]) + sqr(yerr[1]) + sqr(yerr[2]) ; 509 G4double spin_mag2 = Spin.mag2(); << 400 errpos_sq /= eps_pos*eps_pos; // Scale relative to required tolerance 510 G4bool hasSpin = (spin_mag2 > 0.0); << 401 511 << 402 // Accuracy for velocity 512 for (G4int iter=0; iter<max_trials; ++iter) << 403 errvel_sq = (sqr(yerr[3]) + sqr(yerr[4]) + sqr(yerr[5]) ) 513 { << 404 / (sqr(y[3]) + sqr(y[4]) + sqr(y[5]) ); 514 pIntStepper-> Stepper(y,dydx,h,ytemp,yerr) << 405 errvel_sq /= eps_rel_max*eps_rel_max; 515 // ******* << 406 516 G4double eps_pos = eps_rel_max * std::max( << 407 errmax_sq = G4std::max( errpos_sq, errvel_sq ); // Square of maximum error 517 G4double inv_eps_pos_sq = 1.0 / (eps_pos*e << 408 errmax = sqrt( errmax_sq ); 518 << 409 if(errmax_sq <= 1.0 ) break ; // Step succeeded. 519 // Evaluate accuracy << 410 520 // << 411 // Step failed; compute the size of retrial Step. 521 errpos_sq = sqr(yerr[0]) + sqr(yerr[1]) + << 412 htemp = GetSafety()*h*pow(errmax,GetPshrnk()) ; 522 errpos_sq *= inv_eps_pos_sq; // Scale rela << 413 523 << 414 if(htemp >= 0.1*h) h = htemp ; // Truncation error too large, 524 // Accuracy for momentum << 415 else h = 0.1*h ; // reduce stepsize, but no more 525 G4double magvel_sq= sqr(y[3]) + sqr(y[4]) << 416 // than a factor of 10 526 G4double sumerr_sq = sqr(yerr[3]) + sqr(y << 417 xnew = x + h ; 527 if( magvel_sq > 0.0 ) << 418 if(xnew == x) { 528 { << 419 G4cerr<<"G4MagIntegratorDriver::OneGoodStep: Stepsize underflow in Stepper "<<G4endl ; 529 errvel_sq = sumerr_sq / magvel_sq; << 420 G4cerr<<" Step's start x=" << x << " and end x= " << xnew 530 } << 421 << " are equal !! " << G4endl 531 else << 422 <<" Due to step-size= " << h 532 { << 423 << " . Note that input step was " << htry << G4endl; 533 std::ostringstream message; << 424 break; 534 message << "Found case of zero momentum << 425 } 535 << "- iteration= " << iter << " << 426 } 536 G4Exception("G4MagInt_Driver::OneGoodSt << 537 "GeomField1001", JustWarnin << 538 errvel_sq = sumerr_sq; << 539 } << 540 errvel_sq *= inv_eps_vel_sq; << 541 errmax_sq = std::max( errpos_sq, errvel_sq << 542 << 543 if( hasSpin ) << 544 { << 545 // Accuracy for spin << 546 errspin_sq = ( sqr(yerr[9]) + sqr(yerr[ << 547 / spin_mag2; // ( sqr(y[9 << 548 errspin_sq *= inv_eps_vel_sq; << 549 errmax_sq = std::max( errmax_sq, errspin << 550 } << 551 427 552 if ( errmax_sq <= 1.0 ) { break; } // Ste << 428 // Compute size of next Step >> 429 if(errmax > errcon) hnext = GetSafety()*h*pow(errmax,GetPgrow()) ; >> 430 else hnext = max_stepping_increase*h ; >> 431 // No more than a factor of 5 increase 553 432 554 // Step failed; compute the size of retria << 433 x += (hdid = h) ; 555 htemp = GetSafety() * h * std::pow( errmax << 556 434 557 if (htemp >= 0.1*h) { h = htemp; } // Tr << 435 for(i=0;i<nvar;i++) y[i] = ytemp[i] ; 558 else { h = 0.1*h; } // re << 559 // th << 560 xnew = x + h; << 561 if(xnew == x) << 562 { << 563 std::ostringstream message; << 564 message << "Stepsize underflow in Steppe << 565 << "- Step's start x=" << x << " << 566 << " are equal !! " << G4endl << 567 << " Due to step-size= " << h << 568 << ". Note that input step was " << 569 G4Exception("G4MagInt_Driver::OneGoodSte << 570 "GeomField1001", JustWarning << 571 break; << 572 } << 573 } << 574 436 575 // Compute size of next Step << 437 // delete[] ytemp ; 576 if (errmax_sq > errcon*errcon) << 438 // delete[] yerr ; 577 { << 439 return ; 578 hnext = GetSafety()*h*std::pow(errmax_sq, << 579 } << 580 else << 581 { << 582 hnext = max_stepping_increase*h ; // No mo << 583 } << 584 x += (hdid = h); << 585 440 586 for(G4int k=0; k<fNoIntegrationVariables; ++ << 441 } // end of OneGoodStep ............................. 587 442 588 return; << 589 } << 590 443 591 //-------------------------------------------- 444 //---------------------------------------------------------------------- 592 << 593 // QuickAdvance just tries one Step - it does 445 // QuickAdvance just tries one Step - it does not ensure accuracy 594 // 446 // 595 G4bool G4MagInt_Driver::QuickAdvance(G4FieldTr << 447 G4bool G4MagInt_Driver::QuickAdvance( 596 const G4double << 448 G4FieldTrack& y_posvel, // INOUT 597 G4double << 449 const G4double dydx[], 598 G4double& << 450 G4double hstep, // In 599 G4double& << 451 G4double& dchord_step, 600 G4double& << 452 G4double& dyerr ) 601 { << 453 { 602 G4Exception("G4MagInt_Driver::QuickAdvance() << 454 G4double yerr_vec[G4FieldTrack::ncompSVEC], yarrin[G4FieldTrack::ncompSVEC], yarrout[G4FieldTrack::ncompSVEC]; 603 FatalException, "Not yet impleme << 455 G4double s_start; 604 << 456 G4double dyerr_len, dyerr_vel, vel_mag; 605 // Use the parameters of this method, to ple << 457 606 // << 458 // Move data into array 607 dchord_step = dyerr_pos_sq = hstep * hstep * << 459 y_posvel.DumpToArray( yarrin ); // yarrin <== y_posvel 608 dyerr_mom_rel_sq = y_posvel.GetPosition().ma << 460 s_start = y_posvel.GetCurveLength(); 609 return true; << 461 610 } << 462 // Do an Integration Step 611 << 463 pIntStepper-> Stepper(yarrin, dydx, hstep, yarrout, yerr_vec) ; 612 //-------------------------------------------- << 464 613 << 465 // Estimate curve-chord distance 614 G4bool G4MagInt_Driver::QuickAdvance(G4FieldTr << 466 dchord_step= pIntStepper-> DistChord(); 615 const G4double << 467 616 G4double << 468 // Put back the values. 617 G4double& << 469 y_posvel.LoadFromArray( yarrout ); // yarrout ==> y_posvel 618 G4double& << 470 y_posvel.SetCurveLength( s_start + hstep ); 619 { << 471 620 G4double dyerr_pos_sq, dyerr_mom_rel_sq; << 472 // A single measure of the error 621 G4double yerr_vec[G4FieldTrack::ncompSVEC], << 473 // TO-DO : account for tangent vector, energy, spin, ... ? 622 yarrin[G4FieldTrack::ncompSVEC], ya << 474 dyerr_len= sqrt( sqr(yerr_vec[0])+sqr(yerr_vec[1])+sqr(yerr_vec[2])); 623 G4double s_start; << 475 dyerr_vel= sqrt( sqr(yerr_vec[3])+sqr(yerr_vec[4])+sqr(yerr_vec[5])); 624 G4double dyerr_mom_sq, vel_mag_sq, inv_vel_m << 476 vel_mag = sqrt( sqr(yarrout[3])+sqr(yarrout[4])+sqr(yarrout[5]) ); 625 << 477 626 // Move data into array << 478 if( (dyerr_len / hstep) > (dyerr_vel / vel_mag) ) { 627 y_posvel.DumpToArray( yarrin ); // yar << 479 dyerr = dyerr_len; 628 s_start = y_posvel.GetCurveLength(); << 480 }else{ 629 << 481 // Scale it to the position - for now 630 // Do an Integration Step << 482 dyerr = (dyerr_vel / vel_mag) * hstep; 631 pIntStepper-> Stepper(yarrin, dydx, hstep, y << 483 } 632 << 633 // Estimate curve-chord distance << 634 dchord_step= pIntStepper-> DistChord(); << 635 << 636 // Put back the values. yarrout ==> y_posve << 637 y_posvel.LoadFromArray( yarrout, fNoIntegrat << 638 y_posvel.SetCurveLength( s_start + hstep ); << 639 << 640 #ifdef G4DEBUG_FIELD << 641 if(fVerboseLevel>2) << 642 { << 643 G4cout << "G4MagIntDrv: Quick Advance" << << 644 PrintStatus( yarrin, s_start, yarrout, s_s << 645 } << 646 #endif << 647 << 648 // A single measure of the error << 649 // TO-DO : account for energy, spin, << 650 vel_mag_sq = ( sqr(yarrout[3])+sqr(yarrout << 651 inv_vel_mag_sq = 1.0 / vel_mag_sq; << 652 dyerr_pos_sq = ( sqr(yerr_vec[0])+sqr(yerr_v << 653 dyerr_mom_sq = ( sqr(yerr_vec[3])+sqr(yerr_v << 654 dyerr_mom_rel_sq = dyerr_mom_sq * inv_vel_ma << 655 << 656 // Calculate also the change in the momentum << 657 // G4double veloc_square = y_posvel.GetVeloc << 658 // ... << 659 << 660 #ifdef RETURN_A_NEW_STEP_LENGTH 484 #ifdef RETURN_A_NEW_STEP_LENGTH 661 // The following step cannot be done here be << 485 // The following step cannot be done here because "eps" is not known. 662 dyerr_len = std::sqrt( dyerr_len_sq ); << 486 dyerr_len /= eps; 663 dyerr_len_sq /= eps ; << 664 487 665 // Look at the velocity deviation ? << 488 // Look at the velocity deviation ? 666 // sqr(yerr_vec[3])+sqr(yerr_vec[4])+sqr(ye << 489 // sqr(yerr_vec[3])+sqr(yerr_vec[4])+sqr(yerr_vec[5])); 667 490 668 // Set suggested new step << 491 // Look at the change in the velocity (squared maybe ..) 669 hstep = ComputeNewStepSize( dyerr_len, hstep << 492 G4double veloc_square = y_posvel.GetVelocity().mag2(); 670 #endif << 671 493 672 if( dyerr_pos_sq > ( dyerr_mom_rel_sq * sqr( << 494 // Set suggested new step 673 { << 495 hstep= ComputeNewStepSize( dyerr_len, hstep); 674 dyerr = std::sqrt(dyerr_pos_sq); << 496 #endif 675 } << 676 else << 677 { << 678 // Scale it to the current step size - for << 679 dyerr = std::sqrt(dyerr_mom_rel_sq) * hste << 680 } << 681 497 682 return true; << 498 return true; 683 } 499 } 684 500 685 // ------------------------------------------- << 501 #ifdef QUICK_ADV_TWO 686 << 502 G4bool G4MagInt_Driver::QuickAdvance( 687 #ifdef QUICK_ADV_ARRAY_IN_AND_OUT << 503 G4double yarrin[], // IN 688 G4bool G4MagInt_Driver::QuickAdvance(G4double << 504 const G4double dydx[], 689 const G4double << 505 G4double hstep, // In 690 G4double << 506 G4double yarrout[], 691 G4double << 507 G4double& dchord_step, 692 G4double << 508 G4double& dyerr ) // in length 693 G4double << 694 { 509 { 695 G4Exception("G4MagInt_Driver::QuickAdvance() << 510 G4Exception("Not implemented in current version"); 696 FatalException, "Not yet impleme << 511 697 dyerr = dchord_step = hstep * yarrin[0] * dy << 512 dyerr = dchord_step = hstep * yarrin[0] * dydx[0]; 698 yarrout[0]= yarrin[0]; << 513 yarrout[0]= yarrin[0]; 699 } 514 } 700 #endif 515 #endif 701 516 702 // ------------------------------------------- 517 // -------------------------------------------------------------------------- 703 << 518 // This method computes new step sizes - but does not limit changes to 704 // This method computes new step sizes - but d << 519 // within certain factors 705 // within certain factors << 706 // 520 // 707 G4double G4MagInt_Driver:: << 521 708 ComputeNewStepSize_WithoutReductionLimit(G4dou << 522 G4double 709 G4double hstepCurrent) // << 523 G4MagInt_Driver::ComputeNewStepSize( >> 524 G4double errMaxNorm, // max error (normalised) >> 525 G4double hstepCurrent) // current step size 710 { 526 { 711 G4double hnew; 527 G4double hnew; 712 528 713 // Compute size of next Step for a failed st 529 // Compute size of next Step for a failed step 714 if(errMaxNorm > 1.0 ) << 530 if(errMaxNorm > 1.0 ) { 715 { << 531 716 // Step failed; compute the size of retria 532 // Step failed; compute the size of retrial Step. 717 hnew = GetSafety()*hstepCurrent*std::pow(e << 533 hnew = GetSafety()*hstepCurrent*pow(errMaxNorm,GetPshrnk()) ; 718 } << 534 }else{ 719 else if(errMaxNorm > 0.0 ) << 720 { << 721 // Compute size of next Step for a success 535 // Compute size of next Step for a successful step 722 hnew = GetSafety()*hstepCurrent*std::pow(e << 536 hnew = GetSafety()*hstepCurrent*pow(errMaxNorm,GetPgrow()) ; 723 } << 724 else << 725 { << 726 // if error estimate is zero (possible) or << 727 hnew = max_stepping_increase * hstepCurren << 728 } 537 } 729 538 730 return hnew; 539 return hnew; 731 } 540 } 732 541 733 // ------------------------------------------- << 542 // ----------------------------------------------------------------------------- 734 << 543 // This method computes new step sizes limiting changes within certain factors 735 G4double << 736 G4MagInt_Driver::ComputeNewStepSize( << 737 G4double errMaxNorm << 738 G4double hstepCurre << 739 { << 740 // Legacy behaviour: << 741 return ComputeNewStepSize_WithoutReductionL << 742 // 'Improved' behaviour - at least more con << 743 // return ComputeNewStepSize_WithinLimits( << 744 } << 745 << 746 // This method computes new step sizes limitin << 747 // 544 // 748 // It shares its logic with AccurateAdvance. << 545 // It shares its logic with AccurateAdvance. 749 // They are kept separate currently for optimi << 546 // They are kept separate currently for optimisation. 750 // << 547 751 G4double 548 G4double 752 G4MagInt_Driver::ComputeNewStepSize_WithinLimi 549 G4MagInt_Driver::ComputeNewStepSize_WithinLimits( 753 G4double errMaxNorm 550 G4double errMaxNorm, // max error (normalised) 754 G4double hstepCurre << 551 G4double hstepCurrent) // current step size 755 { 552 { 756 G4double hnew; 553 G4double hnew; 757 554 758 // Compute size of next Step for a failed st 555 // Compute size of next Step for a failed step 759 if (errMaxNorm > 1.0 ) << 556 if(errMaxNorm > 1.0 ) { 760 { << 557 761 // Step failed; compute the size of retria 558 // Step failed; compute the size of retrial Step. 762 hnew = GetSafety()*hstepCurrent*std::pow(e << 559 hnew = GetSafety()*hstepCurrent*pow(errMaxNorm,GetPshrnk()) ; 763 560 764 if (hnew < max_stepping_decrease*hstepCurr << 561 if(hnew < max_stepping_decrease*hstepCurrent) 765 { << 562 hnew = max_stepping_decrease*hstepCurrent ; 766 hnew = max_stepping_decrease*hstepCurren << 767 // reduce stepsize, b 563 // reduce stepsize, but no more 768 // than this factor ( 564 // than this factor (value= 1/10) 769 } << 565 }else{ 770 } << 771 else << 772 { << 773 // Compute size of next Step for a success 566 // Compute size of next Step for a successful step 774 if (errMaxNorm > errcon) << 567 if(errMaxNorm > errcon) hnew = GetSafety()*hstepCurrent*pow(errMaxNorm,GetPgrow()) ; 775 { hnew = GetSafety()*hstepCurrent*std::po << 568 else hnew = max_stepping_increase * hstepCurrent ; 776 else // No more than a factor of 5 increa << 569 // No more than a factor of 5 increase 777 { hnew = max_stepping_increase * hstepCur << 778 } 570 } >> 571 779 return hnew; 572 return hnew; 780 } 573 } 781 574 782 // ------------------------------------------- << 783 575 784 void G4MagInt_Driver::PrintStatus( const G4dou << 576 785 G4dou << 577 void G4MagInt_Driver::PrintStatus( const G4double* StartArr, 786 const G4dou << 578 G4double xstart, 787 G4dou << 579 const G4double* CurrentArr, 788 G4dou << 580 G4double xcurrent, 789 G4int << 581 G4double requestStep, >> 582 G4int subStepNo) 790 // Potentially add as arguments: 583 // Potentially add as arguments: 791 // <dydx> 584 // <dydx> - as Initial Force 792 // stepTaken 585 // stepTaken(hdid) - last step taken 793 // nextStep 586 // nextStep (hnext) - proposal for size 794 { 587 { 795 G4FieldTrack StartFT(G4ThreeVector(0,0,0), << 588 G4FieldTrack StartFT(G4ThreeVector(0,0,0), G4ThreeVector(0,0,0), 0., 0., 0., 0. ); 796 G4ThreeVector(0,0,0), 0., 0., << 797 G4FieldTrack CurrentFT (StartFT); 589 G4FieldTrack CurrentFT (StartFT); 798 590 799 StartFT.LoadFromArray( StartArr, fNoIntegra << 591 StartFT.LoadFromArray( StartArr); 800 StartFT.SetCurveLength( xstart); 592 StartFT.SetCurveLength( xstart); 801 CurrentFT.LoadFromArray( CurrentArr, fNoInt << 593 CurrentFT.LoadFromArray( CurrentArr); 802 CurrentFT.SetCurveLength( xcurrent ); 594 CurrentFT.SetCurveLength( xcurrent ); 803 595 804 PrintStatus(StartFT, CurrentFT, requestStep 596 PrintStatus(StartFT, CurrentFT, requestStep, subStepNo ); 805 } 597 } 806 598 807 // ------------------------------------------- << 599 #include "g4std/iomanip" 808 600 809 void G4MagInt_Driver::PrintStatus(const G4Fiel << 601 void G4MagInt_Driver::PrintStatus( 810 const G4Fiel << 602 const G4FieldTrack& StartFT, 811 G4doub << 603 const G4FieldTrack& CurrentFT, 812 G4int << 604 G4double requestStep, >> 605 // G4double safety, >> 606 G4int subStepNo) 813 { 607 { 814 G4int verboseLevel= fVerboseLevel; 608 G4int verboseLevel= fVerboseLevel; 815 const G4int noPrecision = 5; << 609 static G4int noPrecision= 5; 816 G4long oldPrec= G4cout.precision(noPrecisi << 610 G4int oldPrec= G4cout.precision(noPrecision); 817 // G4cout.setf(ios_base::fixed,ios_base::f 611 // G4cout.setf(ios_base::fixed,ios_base::floatfield); 818 612 819 const G4ThreeVector StartPosition= S << 613 const G4ThreeVector StartPosition= StartFT.GetPosition(); 820 const G4ThreeVector StartUnitVelocity= S << 614 const G4ThreeVector StartUnitVelocity= StartFT.GetMomentumDir(); 821 const G4ThreeVector CurrentPosition= C << 615 const G4ThreeVector CurrentPosition= CurrentFT.GetPosition(); 822 const G4ThreeVector CurrentUnitVelocity= C << 616 const G4ThreeVector CurrentUnitVelocity= CurrentFT.GetMomentumDir(); 823 << 617 824 G4double DotStartCurrentVeloc= StartUnitV << 618 G4double step_len= CurrentFT.GetCurveLength() 825 << 619 - StartFT.GetCurveLength(); 826 G4double step_len= CurrentFT.GetCurveLengt << 620 827 G4double subStepSize = step_len; << 621 if( (subStepNo <= 0) && (verboseLevel <= 3) ) 828 << 829 if( (subStepNo <= 1) || (verboseLevel > 3) << 830 { 622 { 831 subStepNo = - subStepNo; // To a 623 subStepNo = - subStepNo; // To allow printing banner 832 624 833 G4cout << std::setw( 6) << " " << std: << 625 G4cout << G4std::setw( 6) << " " 834 << " G4MagInt_Driver: Current Po << 626 << G4std::setw( 25) << " G4MagInt_Driver: Current Position and Direction" << " " 835 << G4endl; << 627 << G4endl; 836 G4cout << std::setw( 5) << "Step#" << " << 628 G4cout << G4std::setw( 5) << "Step#" << " " 837 << std::setw( 7) << "s-curve" << << 629 << G4std::setw( 7) << "s-curve" << " " 838 << std::setw( 9) << "X(mm)" << " << 630 << G4std::setw( 9) << "X(mm)" << " " 839 << std::setw( 9) << "Y(mm)" << " << 631 << G4std::setw( 9) << "Y(mm)" << " " 840 << std::setw( 9) << "Z(mm)" << " << 632 << G4std::setw( 9) << "Z(mm)" << " " 841 << std::setw( 8) << " N_x " << " << 633 << G4std::setw( 7) << " N_x " << " " 842 << std::setw( 8) << " N_y " << " << 634 << G4std::setw( 7) << " N_y " << " " 843 << std::setw( 8) << " N_z " << " << 635 << G4std::setw( 7) << " N_z " << " " 844 << std::setw( 8) << " N^2-1 " << << 636 << G4std::setw( 7) << "KinEner " << " " 845 << std::setw(10) << " N(0).N " < << 637 << G4std::setw( 9) << "StepLen" << " " // Add the Sub-step ?? 846 << std::setw( 7) << "KinEner " < << 638 << G4std::setw( 9) << "ReqStep" << " " 847 << std::setw(12) << "Track-l" << << 639 << G4endl; 848 << std::setw(12) << "Step-len" < << 849 << std::setw(12) << "Step-len" < << 850 << std::setw( 9) << "ReqStep" << << 851 << G4endl; << 852 } << 853 640 854 if( (subStepNo <= 0) ) << 641 PrintStat_Aux( StartFT, requestStep, 0., 0); 855 { << 642 //************* 856 PrintStat_Aux( StartFT, requestStep, 0. << 857 0, 0.0, << 858 } 643 } 859 644 860 if( verboseLevel <= 3 ) 645 if( verboseLevel <= 3 ) 861 { 646 { 862 G4cout.precision(noPrecision); << 647 G4cout.precision(noPrecision); 863 PrintStat_Aux( CurrentFT, requestStep, s << 648 PrintStat_Aux( CurrentFT, requestStep, step_len, subStepNo); 864 subStepNo, subStepSize, D << 649 //************* 865 } << 650 } >> 651 >> 652 else // if( verboseLevel > 3 ) >> 653 { >> 654 // Multi-line output >> 655 >> 656 // G4cout << "Current Position is " << CurrentPosition << G4endl >> 657 // << " and UnitVelocity is " << CurrentUnitVelocity << G4endl; >> 658 // G4cout << "Step taken was " << step_len >> 659 // << " out of PhysicalStep= " << requestStep << G4endl; >> 660 // G4cout << "Final safety is: " << safety << G4endl; 866 661 >> 662 // G4cout << "Chord length = " << (CurrentPosition-StartPosition).mag() << G4endl; >> 663 // G4cout << G4endl; >> 664 } 867 G4cout.precision(oldPrec); 665 G4cout.precision(oldPrec); 868 } 666 } 869 667 870 // ------------------------------------------- << 668 void G4MagInt_Driver::PrintStat_Aux( 871 << 669 const G4FieldTrack& aFieldTrack, 872 void G4MagInt_Driver::PrintStat_Aux(const G4Fi << 670 G4double requestStep, 873 G4do << 671 G4double step_len, 874 G4do << 672 G4int subStepNo) 875 G4in << 876 G4do << 877 G4do << 878 { 673 { 879 const G4ThreeVector Position = aFieldTrack << 674 const G4ThreeVector Position= aFieldTrack.GetPosition(); 880 const G4ThreeVector UnitVelocity = aFieldT << 675 const G4ThreeVector UnitVelocity= aFieldTrack.GetMomentumDir(); 881 676 882 if( subStepNo >= 0) 677 if( subStepNo >= 0) 883 { << 678 G4cout << G4std::setw( 5) << subStepNo << " "; 884 G4cout << std::setw( 5) << subStepNo << << 885 } << 886 else 679 else 887 { << 680 G4cout << G4std::setw( 5) << "Start" << " "; 888 G4cout << std::setw( 5) << "Start" << " << 681 G4cout << G4std::setw( 7) << aFieldTrack.GetCurveLength(); 889 } << 682 G4cout << G4std::setw( 9) << Position.x() << " " 890 G4double curveLen= aFieldTrack.GetCurveLen << 683 << G4std::setw( 9) << Position.y() << " " 891 G4cout << std::setw( 7) << curveLen; << 684 << G4std::setw( 9) << Position.z() << " " 892 G4cout << std::setw( 9) << Position.x() << << 685 << G4std::setw( 7) << UnitVelocity.x() << " " 893 << std::setw( 9) << Position.y() << << 686 << G4std::setw( 7) << UnitVelocity.y() << " " 894 << std::setw( 9) << Position.z() << << 687 << G4std::setw( 7) << UnitVelocity.z() << " "; 895 << std::setw( 8) << UnitVelocity.x( << 688 G4cout << G4std::setw( 7) << aFieldTrack.GetKineticEnergy(); 896 << std::setw( 8) << UnitVelocity.y( << 689 G4cout << G4std::setw( 9) << step_len << " "; 897 << std::setw( 8) << UnitVelocity.z( << 690 if( requestStep != -1.0 ) 898 G4long oldprec= G4cout.precision(3); << 691 G4cout << G4std::setw( 9) << requestStep << " "; 899 G4cout << std::setw( 8) << UnitVelocity.ma << 900 G4cout.precision(6); << 901 G4cout << std::setw(10) << dotVeloc_StartC << 902 G4cout.precision(oldprec); << 903 G4cout << std::setw( 7) << aFieldTrack.Get << 904 G4cout << std::setw(12) << step_len << " " << 905 << 906 static G4ThreadLocal G4double oldCurveLeng << 907 static G4ThreadLocal G4double oldSubStepLe << 908 static G4ThreadLocal G4int oldSubStepNo = << 909 << 910 G4double subStep_len = 0.0; << 911 if( curveLen > oldCurveLength ) << 912 { << 913 subStep_len= curveLen - oldCurveLength; << 914 } << 915 else if (subStepNo == oldSubStepNo) << 916 { << 917 subStep_len= oldSubStepLength; << 918 } << 919 oldCurveLength= curveLen; << 920 oldSubStepLength= subStep_len; << 921 << 922 G4cout << std::setw(12) << subStep_len << << 923 G4cout << std::setw(12) << subStepSize << << 924 if( requestStep != -1.0 ) << 925 { << 926 G4cout << std::setw( 9) << requestStep < << 927 } << 928 else 692 else 929 { << 693 G4cout << G4std::setw( 9) << " InitialStep " << " "; 930 G4cout << std::setw( 9) << " InitialSte << 694 // G4cout << G4std::setw(12) << safety << " "; 931 } << 932 G4cout << G4endl; 695 G4cout << G4endl; 933 } << 934 << 935 // ------------------------------------------- << 936 << 937 void G4MagInt_Driver::PrintStatisticsReport() << 938 { << 939 G4int noPrecBig = 6; << 940 G4long oldPrec = G4cout.precision(noPrecBig) << 941 << 942 G4cout << "G4MagInt_Driver Statistics of ste << 943 G4cout << "G4MagInt_Driver: Number of Steps: << 944 << " Total= " << fNoTotalSteps << 945 << " Bad= " << fNoBadSteps << 946 << " Small= " << fNoSmallSteps << 947 << " Non-initial small= " << (fNoSmal << 948 << G4endl; << 949 G4cout.precision(oldPrec); << 950 } << 951 << 952 // ------------------------------------------- << 953 << 954 void G4MagInt_Driver::SetSmallestFraction(G4do << 955 { << 956 if( (newFraction > 1.e-16) && (newFraction < << 957 { << 958 fSmallestFraction= newFraction; << 959 } << 960 else << 961 { << 962 std::ostringstream message; << 963 message << "Smallest Fraction not changed. << 964 << " Proposed value was " << newF << 965 << " Value must be between 1.e-8 << 966 G4Exception("G4MagInt_Driver::SetSmallestF << 967 "GeomField1001", JustWarning, << 968 } << 969 } << 970 << 971 void G4MagInt_Driver:: << 972 GetDerivatives(const G4FieldTrack& y_curr, G4d << 973 { << 974 G4double ytemp[G4FieldTrack::ncompSVEC]; << 975 y_curr.DumpToArray(ytemp); << 976 pIntStepper->RightHandSide(ytemp, dydx); << 977 // Avoid virtual call for GetStepper << 978 // Was: GetStepper()->ComputeRightHandSi << 979 } << 980 << 981 void G4MagInt_Driver::GetDerivatives(const G4F << 982 G4double << 983 G4double << 984 { << 985 G4double ytemp[G4FieldTrack::ncompSVEC]; << 986 track.DumpToArray(ytemp); << 987 pIntStepper->RightHandSide(ytemp, dydx, fi << 988 } << 989 << 990 G4EquationOfMotion* G4MagInt_Driver::GetEquati << 991 { << 992 return pIntStepper->GetEquationOfMotion(); << 993 } << 994 << 995 void G4MagInt_Driver::SetEquationOfMotion(G4Eq << 996 { << 997 pIntStepper->SetEquationOfMotion(equation) << 998 } << 999 << 1000 const G4MagIntegratorStepper* G4MagInt_Driver << 1001 { << 1002 return pIntStepper; << 1003 } << 1004 << 1005 G4MagIntegratorStepper* G4MagInt_Driver::GetS << 1006 { << 1007 return pIntStepper; << 1008 } << 1009 << 1010 void G4MagInt_Driver:: << 1011 RenewStepperAndAdjust(G4MagIntegratorStepper* << 1012 { << 1013 pIntStepper = pItsStepper; << 1014 ReSetParameters(); << 1015 } << 1016 << 1017 void G4MagInt_Driver::StreamInfo( std::ostrea << 1018 { << 1019 os << "State of G4MagInt_Driver: " << std << 1020 os << " Max number of Steps = " << fMaxN << 1021 << " (base # = " << fMaxStepBase << << 1022 os << " Safety factor = " << safet << 1023 os << " Power - shrink = " << pshrn << 1024 os << " Power - grow = " << pgrow << 1025 os << " threshold (errcon) = " << errco << 1026 << 1027 os << " fMinimumStep = " << fMini << 1028 os << " Smallest Fraction = " << fSmal << 1029 << 1030 os << " No Integrat Vars = " << fNoIn << 1031 os << " Min No Vars = " << fMinN << 1032 os << " Num-Vars = " << fNoVa << 1033 << 1034 os << " verbose level = " << fVerb << 1035 os << " Reintegrates = " << DoesR << 1036 } << 1037 << 1038 void PrintInfo( const G4MagInt_Driver & magDr << 1039 { << 1040 os << "State of G4MagInt_Driver: " << std << 1041 os << " Max number of Steps = " << magDr << 1042 // << " (base # = " << magDrv.fMaxSt << 1043 os << " Safety factor = " << magDr << 1044 os << " Power - shrink = " << magDr << 1045 os << " Power - grow = " << magDr << 1046 os << " threshold (errcon) = " << magDr << 1047 << 1048 os << " fMinimumStep = " << magDr << 1049 os << " Smallest Fraction = " << magDr << 1050 << 1051 /***** << 1052 os << " No Integrat Vars = " << magDr << 1053 os << " Min No Vars = " << magDr << 1054 os << " Num-Vars = " << magDr << 1055 *****/ << 1056 os << " verbose level = " << magDr << 1057 os << " Reintegrates = " << magDr << 1058 } 696 } 1059 697