Geant4 Cross Reference |
>> 1 // This code implementation is the intellectual property of >> 2 // the GEANT4 collaboration. 1 // 3 // 2 // ******************************************* << 4 // By copying, distributing or modifying the Program (or any work 3 // * License and Disclaimer << 5 // based on the Program) you indicate your acceptance of this statement, 4 // * << 6 // and all its terms. 5 // * The Geant4 software is copyright of th << 6 // * the Geant4 Collaboration. It is provided << 7 // * conditions of the Geant4 Software License << 8 // * LICENSE and available at http://cern.ch/ << 9 // * include a list of copyright holders. << 10 // * << 11 // * Neither the authors of this software syst << 12 // * institutes,nor the agencies providing fin << 13 // * work make any representation or warran << 14 // * regarding this software system or assum << 15 // * use. Please see the license in the file << 16 // * for the full disclaimer and the limitatio << 17 // * << 18 // * This code implementation is the result << 19 // * technical work of the GEANT4 collaboratio << 20 // * By using, copying, modifying or distri << 21 // * any work based on the software) you ag << 22 // * use in resulting scientific publicati << 23 // * acceptance of all terms of the Geant4 Sof << 24 // ******************************************* << 25 // 7 // 26 // G4ChordFinder implementation << 8 // $Id: G4ChordFinder.cc,v 1.9 1999/12/15 14:49:48 gunter Exp $ >> 9 // GEANT4 tag $Name: geant4-01-01 $ 27 // 10 // 28 // Author: J.Apostolakis - Design and implemen << 11 // 29 // ------------------------------------------- << 12 // 25.02.97 John Apostolakis, design and implimentation 30 << 13 // 05.03.97 V. Grichine , style modification 31 #include <iomanip> << 32 14 33 #include "G4ChordFinder.hh" 15 #include "G4ChordFinder.hh" 34 #include "G4SystemOfUnits.hh" << 16 #include "G4MagIntegratorDriver.hh" 35 #include "G4MagneticField.hh" << 36 #include "G4Mag_UsualEqRhs.hh" 17 #include "G4Mag_UsualEqRhs.hh" >> 18 #include "G4ClassicalRK4.hh" >> 19 // #include "G4Field.hh" >> 20 // #include "G4MagIntegratorStepper.hh" 37 #include "G4MagIntegratorDriver.hh" 21 #include "G4MagIntegratorDriver.hh" 38 // #include "G4ClassicalRK4.hh" << 39 // #include "G4CashKarpRKF45.hh" << 40 // #include "G4NystromRK4.hh" << 41 // #include "G4BogackiShampine23.hh" << 42 // #include "G4BogackiShampine45.hh" << 43 << 44 #include "G4DormandPrince745.hh" << 45 << 46 // New templated stepper(s) -- avoid virtual c << 47 #include "G4TDormandPrince45.hh" << 48 << 49 // FSAL type driver / steppers ----- << 50 #include "G4FSALIntegrationDriver.hh" << 51 #include "G4VFSALIntegrationStepper.hh" << 52 #include "G4RK547FEq1.hh" << 53 // #include "G4RK547FEq2.hh" << 54 // #include "G4RK547FEq3.hh" << 55 // #include "G4FSALBogackiShampine45.hh" << 56 // #include "G4FSALDormandPrince745.hh" << 57 << 58 // Templated type drivers ----- << 59 #include "G4IntegrationDriver.hh" << 60 #include "G4InterpolationDriver.hh" << 61 << 62 #include "G4HelixHeum.hh" << 63 #include "G4BFieldIntegrationDriver.hh" << 64 22 65 #include "G4QSSDriverCreator.hh" << 23 // For the moment fDeltaChord is a constant! 66 24 67 #include "G4CachedMagneticField.hh" << 25 const G4double G4ChordFinder::fDefaultDeltaChord = 3. * mm; 68 26 69 #include <cassert> << 70 #include <memory> << 71 << 72 G4bool G4ChordFinder::gVerboseCtor = false; << 73 // ........................................... 27 // .......................................................................... 74 28 75 G4ChordFinder::G4ChordFinder(G4VIntegrationDri << 29 G4ChordFinder::G4ChordFinder( G4MagneticField* theMagField, 76 : fDefaultDeltaChord(0.25 * mm), fIntgrDrive << 30 G4double stepMinimum, >> 31 G4MagIntegratorStepper* pItsStepper ) // A default one >> 32 : fDeltaChord( fDefaultDeltaChord ) 77 { 33 { 78 // Simple constructor -- it does not create << 34 // Construct the Chord Finder 79 if( gVerboseCtor ) << 35 // by creating in inverse order the Driver, the Stepper and EqRhs ... >> 36 // G4Mag_EqRhs * >> 37 fEquation = new G4Mag_UsualEqRhs(theMagField); // Should move q, p to >> 38 //G4FieldTrack ?? >> 39 // --->> Charge Q = 0 >> 40 // --->> Momentum P = 1 NOMINAL VALUES !!!!!!!!!!!!!!!!!! >> 41 >> 42 if( pItsStepper == 0 ) >> 43 { >> 44 pItsStepper = fDriversStepper = new G4ClassicalRK4(fEquation); >> 45 fAllocatedStepper= true; >> 46 } >> 47 else 80 { 48 { 81 G4cout << "G4ChordFinder: Simple construct << 49 fAllocatedStepper= false; 82 } 50 } 83 << 51 fIntgrDriver = new G4MagInt_Driver(stepMinimum, 84 fDeltaChord = fDefaultDeltaChord; // P << 52 pItsStepper, >> 53 pItsStepper->GetNumberOfVariables() ); 85 } 54 } 86 55 87 // ........................................... << 56 // ...................................................................... 88 57 89 G4ChordFinder::G4ChordFinder( G4MagneticField* << 58 G4ChordFinder::~G4ChordFinder() 90 G4double << 91 G4MagIntegratorS << 92 G4int << 93 : fDefaultDeltaChord(0.25 * mm) << 94 { 59 { 95 // Construct the Chord Finder << 60 delete fEquation; // fIntgrDriver->pIntStepper->theEquation_Rhs; 96 // by creating in inverse order the Driver, << 61 if( fAllocatedStepper) 97 constexpr G4int nVar6 = 6; // Components i << 62 { 98 << 63 delete fDriversStepper; 99 fDeltaChord = fDefaultDeltaChord; // P << 64 } // fIntgrDriver->pIntStepper;} >> 65 delete fIntgrDriver; >> 66 } 100 67 101 G4cout << " G4ChordFinder: stepperDriverId: << 68 // ...................................................................... 102 69 103 G4bool useFSALstepper = (stepperDriverId << 70 G4double 104 G4bool useTemplatedStepper= (stepperDriverId << 71 G4ChordFinder::AdvanceChordLimited( G4FieldTrack& yCurrent, 105 G4bool useRegularStepper = (stepperDriverId << 72 const G4double stepMax, 106 G4bool useBfieldDriver = (stepperDriverId << 73 const G4double epsStep ) 107 G4bool useG4QSSDriver = (stepperDriverId << 74 { 108 << 75 G4double stepPossible; 109 if( stepperDriverId == kQss3DriverType) << 76 G4double dyErr; 110 { << 77 G4FieldTrack yEnd( yCurrent); 111 stepperDriverId = kQss2DriverType; << 78 G4double startCurveLen= yCurrent.GetCurveLength(); 112 G4cout << " G4ChordFinder: QSS 3 is curren << 79 G4bool dbg= false; 113 } << 80 >> 81 #ifdef G4VERBOSE >> 82 if( dbg ) >> 83 G4cerr << "Entered FindNextChord Limited with:\n yCurrent: " << yCurrent >> 84 << " and initial Step=stepMax=" << stepMax << " mm. " << G4endl; >> 85 #endif 114 86 115 using EquationType = G4Mag_UsualEqRhs; << 87 stepPossible= FindNextChord(yCurrent, stepMax, yEnd, dyErr, epsStep); 116 << 88 G4bool good_advance; 117 using TemplatedStepperType = << 89 if ( dyErr < epsStep * stepPossible ) 118 G4TDormandPrince45<EquationType,nVar6 << 90 { 119 const char* TemplatedStepperName = << 91 // Accept this accuracy. 120 "G4TDormandPrince745 (templated Dormand- << 92 yCurrent = yEnd; 121 << 93 good_advance = true; 122 using RegularStepperType = << 94 } 123 G4DormandPrince745; // 5th order embe << 95 else 124 // G4ClassicalRK4; // The old << 125 // G4CashKarpRKF45; // First em << 126 // G4BogackiShampine45; // High eff << 127 // G4NystromRK4; // Nystrom << 128 // G4RK547FEq1; // or 2 or 3 << 129 const char* RegularStepperName = << 130 "G4DormandPrince745 (aka DOPRI5): 5th/4t << 131 // "BogackiShampine 45 (Embedded 5th/4th << 132 // "Nystrom stepper 4th order"; << 133 << 134 using NewFsalStepperType = G4DormandPrince74 << 135 << 136 const char* NewFSALStepperName = << 137 "G4RK574FEq1> FSAL 4th/5th order 7-stage << 138 << 139 #ifdef G4DEBUG_FIELD << 140 static G4bool verboseDebug = true; << 141 if( verboseDebug ) << 142 { 96 { 143 G4cout << "G4ChordFinder 2nd Constructor << 97 // Advance more accurately to "end of chord" 144 G4cout << " Arguments: " << G4endl << 98 good_advance = fIntgrDriver->AccurateAdvance(yCurrent, stepPossible, epsStep); 145 << " - min step = " << stepMinimum << 99 #ifdef G4VERBOSE 146 << " - stepper ptr provided : " << 100 if (dbg) G4cerr << "Accurate advance to end of chord attemped" 147 << ( pItsStepper==nullptr ? " no << 101 << "with result " << good_advance << G4endl ; 148 if( pItsStepper==nullptr ) << 102 #endif 149 G4cout << " - stepper/driver Id = " << << 103 if ( ! good_advance ){ 150 << " useFSAL = " << useFSALste << 104 // In this case the driver could not do the full distance 151 << " , useTemplated = " << use << 105 stepPossible= yCurrent.GetCurveLength()-startCurveLen; 152 << " , useRegular = " << useRe << 106 } 153 << " , useFSAL = " << useFSALs << 154 << G4endl; << 155 } 107 } >> 108 >> 109 #ifdef G4VERBOSE >> 110 if( dbg ) G4cerr << "Exiting FindNextChord Limited with:\n yCurrent: " >> 111 << yCurrent<< G4endl; 156 #endif 112 #endif 157 113 158 // useHigherStepper = forceHigherEffiencySte << 114 return stepPossible; >> 115 } 159 116 160 auto pEquation = new G4Mag_UsualEqRhs(theMa << 117 // .............................................................................. 161 fEquation = pEquation; << 162 118 163 // G4MagIntegratorStepper* regularStepper = << 119 G4double 164 // G4VFSALIntegrationStepper* fsalStepper = << 120 G4ChordFinder::FindNextChord( const G4FieldTrack yStart, 165 // G4MagIntegratorStepper* oldFSALStepper = << 121 const G4double stepMax, >> 122 G4FieldTrack& yEnd, // Endpoint >> 123 G4double& dyErr, // Error of endpoint >> 124 G4double epsStep ) >> 125 >> 126 // Returns Length of Step taken >> 127 { >> 128 // G4int stepRKnumber=0; >> 129 G4FieldTrack yCurrent= yStart; >> 130 G4double stepTrial= stepMax; >> 131 G4double dydx[G4FieldTrack::ncompSVEC]; >> 132 >> 133 // 1.) Try to "leap" to end of interval >> 134 // 2.) Evaluate if resulting chord gives d_chord that is good enough. >> 135 // 2a.) If d_chord is not good enough, find one that is. >> 136 >> 137 G4bool validEndPoint= false, dbg= false; >> 138 G4double dChordStep; 166 139 167 G4bool errorInStepperCreation = false; << 140 fIntgrDriver-> GetDerivatives( yCurrent, dydx ) ; 168 141 169 std::ostringstream message; // In case of f << 142 do >> 143 { >> 144 yCurrent = yStart; // Always start from initial point 170 145 171 if( pItsStepper != nullptr ) << 146 fIntgrDriver->QuickAdvance( yCurrent, dydx, stepTrial, dChordStep, dyErr); 172 { << 173 if( gVerboseCtor ) << 174 { << 175 G4cout << " G4ChordFinder: Creating G4I << 176 << " stepMinimum = " << stepMini << 177 << " numVar= " << pItsStepper->G << 178 } << 179 147 180 // Stepper type is not known - so must us << 148 #ifdef G4VERBOSE 181 if(pItsStepper->isQSS()) << 149 if( dbg ) { 182 { << 150 G4cerr << "Returned from QuickAdvance with: yCur=" << yCurrent << G4endl; 183 // fIntgrDriver = pItsStepper->build_ << 151 G4cerr << " dChordStep= "<< dChordStep <<" dyErr=" << dyErr << G4endl; 184 G4Exception("G4ChordFinder::G4ChordFi << 185 "GeomField1001", FatalEx << 186 "Cannot provide QSS ste << 187 } << 188 else << 189 { << 190 fIntgrDriver = new G4IntegrationDrive << 191 pItsStepper, << 192 // Stepper type is not known - so mus << 193 // Non-interpolating driver used by d << 194 // WAS: fIntgrDriver = pItsStepper-> << 195 } << 196 // -- Older: << 197 // G4cout << " G4ChordFinder: Creating G4 << 198 // Type is not known - so must use old cl << 199 // fIntgrDriver = new G4MagInt_Driver( st << 200 // pItsSt << 201 } << 202 else if ( useTemplatedStepper ) << 203 { << 204 if( gVerboseCtor ) << 205 { << 206 G4cout << " G4ChordFinder: Creating Te << 207 << TemplatedStepperName << G4en << 208 } 152 } 209 // RegularStepperType* regularStepper = n << 153 #endif 210 auto templatedStepper = new TemplatedStep << 211 // *** *************** << 212 // << 213 // Alternative - for G4NystromRK4: << 214 // = new G4NystromRK4(pEquation, 0.1*mm ) << 215 fRegularStepperOwned = templatedStepper; << 216 if( templatedStepper == nullptr ) << 217 { << 218 message << "Templated Stepper instanti << 219 message << "G4ChordFinder: Attempted t << 220 << TemplatedStepperName << " t << 221 errorInStepperCreation = true; << 222 } << 223 else << 224 { << 225 fIntgrDriver = new G4IntegrationDriver << 226 stepMinimum, templatedStepper, nVar << 227 if( gVerboseCtor ) << 228 { << 229 G4cout << " G4ChordFinder: Using G4 << 230 } << 231 } << 232 << 233 } << 234 else if ( useRegularStepper ) // Plain st << 235 { << 236 auto regularStepper = new RegularStepperT << 237 // *** *************** << 238 fRegularStepperOwned = regularStepper; << 239 << 240 if( gVerboseCtor ) << 241 { << 242 G4cout << " G4ChordFinder: Creating Dr << 243 } << 244 << 245 if( regularStepper == nullptr ) << 246 { << 247 message << "Regular Stepper instantiat << 248 message << "G4ChordFinder: Attempted t << 249 << RegularStepperName << " typ << 250 errorInStepperCreation = true; << 251 } << 252 else << 253 { << 254 auto dp5= dynamic_cast<G4DormandPrince << 255 if( dp5 != nullptr ) << 256 { << 257 fIntgrDriver = new G4InterpolationD << 258 stepMinimum, << 259 if( gVerboseCtor ) << 260 { << 261 G4cout << " Using InterpolationD << 262 } << 263 } << 264 else << 265 { << 266 fIntgrDriver = new G4IntegrationDri << 267 stepMinimum, << 268 if( gVerboseCtor ) << 269 { << 270 G4cout << " Using IntegrationDri << 271 } << 272 } << 273 } << 274 } << 275 else if ( useBfieldDriver ) << 276 { << 277 auto regularStepper = new G4DormandPrince << 278 // *** *************** << 279 // << 280 fRegularStepperOwned = regularStepper; << 281 << 282 { << 283 using SmallStepDriver = G4Interpolatio << 284 using LargeStepDriver = G4IntegrationD << 285 << 286 fLongStepper = std::make_unique<G4Heli << 287 << 288 fIntgrDriver = new G4BFieldIntegration << 289 std::make_unique<SmallStepDriver>(st << 290 regularStepper, regularStepper-> << 291 std::make_unique<LargeStepDriver>(st << 292 fLongStepper.get(), regularStepp << 293 << 294 if( fIntgrDriver == nullptr) << 295 { << 296 message << "Using G4BFieldIntegrati << 297 << RegularStepperName << " << 298 message << "Driver instantiation FA << 299 G4Exception("G4ChordFinder::G4Chord << 300 "GeomField1001", JustWa << 301 } << 302 } << 303 } << 304 else if( useG4QSSDriver ) << 305 { << 306 if( stepperDriverId == kQss2DriverType ) << 307 { << 308 auto qssStepper2 = G4QSSDriverCreator:: << 309 if( gVerboseCtor ) << 310 { << 311 G4cout << "-- Created QSS-2 stepper" << 312 } << 313 fIntgrDriver = G4QSSDriverCreator::Crea << 314 } << 315 else << 316 { << 317 auto qssStepper3 = G4QSSDriverCreator:: << 318 if( gVerboseCtor ) << 319 { << 320 G4cout << "-- Created QSS-3 stepper" << 321 } << 322 fIntgrDriver = G4QSSDriverCreator::Crea << 323 } << 324 if( gVerboseCtor ) << 325 { << 326 G4cout << "-- G4ChordFinder: Using QSS << 327 } << 328 } << 329 else << 330 { << 331 auto fsalStepper= new NewFsalStepperType << 332 // *** ****************** << 333 fNewFSALStepperOwned = fsalStepper; << 334 << 335 if( fsalStepper == nullptr ) << 336 { << 337 message << "Stepper instantiation FAIL << 338 message << "Attempted to instantiate " << 339 << NewFSALStepperName << " typ << 340 G4Exception("G4ChordFinder::G4ChordFin << 341 "GeomField1001", JustWarni << 342 errorInStepperCreation = true; << 343 } << 344 else << 345 { << 346 fIntgrDriver = new << 347 G4FSALIntegrationDriver<NewFsalStep << 348 fsal << 349 // ==== Create the driver which k << 350 << 351 if( fIntgrDriver == nullptr ) << 352 { << 353 message << "Using G4FSALIntegration << 354 << NewFSALStepperName << G4 << 355 message << "Integration Driver inst << 356 G4Exception("G4ChordFinder::G4Chord << 357 "GeomField1001", JustWa << 358 } << 359 } << 360 } << 361 154 362 // -- Main work is now done << 155 // We check whether the criterion is met here. 363 << 156 validEndPoint = AcceptableMissDist(dChordStep); 364 // Now check that no error occured, and r << 157 // && (dyErr < eps) ; 365 << 158 366 // To test failure to create driver << 159 if( ! validEndPoint ) { 367 // delete fIntgrDriver; << 160 // This is needed to decide new step size until QuickAdvance does it 368 // fIntgrDriver = nullptr; << 161 stepTrial = NewStep(stepTrial, dChordStep ); 369 << 162 370 // Detect and report Error conditions << 163 // Get the driver to calculate the new step size, if it is needed 371 // << 164 // stepTrial= fIntgrDriver->ComputeNewStepSize( dyErr/epsStep, stepTrial); 372 if( errorInStepperCreation || (fIntgrDriver << 165 #ifdef G4VERBOSE 373 { << 166 if( dbg ) 374 std::ostringstream errmsg; << 167 G4cerr << "Dchord too big. Trying new hstep=" << stepTrial << G4endl; 375 << 168 #endif 376 if( errorInStepperCreation ) << 377 { << 378 errmsg << "ERROR> Failure to create S << 379 << " ------------------- << 380 } << 381 if (fIntgrDriver == nullptr ) << 382 { << 383 errmsg << "ERROR> Failure to create I << 384 << G4endl << 385 << " ------------------- << 386 << G4endl; << 387 } 169 } 388 const std::string BoolName[2]= { "False", << 170 389 errmsg << " Configuration: (constructor << 390 << " provided Stepper = " << pI << 391 << " stepper/driver Id = " << step << 392 << " useTemplated = " << BoolNam << 393 << " useRegular = " << BoolName[ << 394 << " useFSAL = " << BoolName[use << 395 << " using combo BField Driver = << 396 BoolName[ ! (useFSALstepper << 397 || useRegularSt << 398 << G4endl; << 399 errmsg << message.str(); << 400 errmsg << "Aborting."; << 401 G4Exception("G4ChordFinder::G4ChordFinder << 402 "GeomField0003", FatalExcepti << 403 } 171 } >> 172 while( ! validEndPoint ); // End of do-while RKD 404 173 405 assert( ( pItsStepper != nullptr ) << 174 yEnd= yCurrent; 406 || ( fRegularStepperOwned != nullptr << 175 return stepTrial; 407 || ( fNewFSALStepperOwned != nullptr << 408 || useG4QSSDriver << 409 ); << 410 assert( fIntgrDriver != nullptr ); << 411 } << 412 << 413 // ........................................... << 414 << 415 G4ChordFinder::~G4ChordFinder() << 416 { << 417 delete fEquation; << 418 delete fRegularStepperOwned; << 419 delete fNewFSALStepperOwned; << 420 delete fCachedField; << 421 delete fIntgrDriver; << 422 } 176 } 423 177 424 // ........................................... 178 // ........................................................................... 425 179 426 G4FieldTrack << 180 G4double G4ChordFinder::NewStep( 427 G4ChordFinder::ApproxCurvePointS( const G4Fiel << 181 const G4double stepTrialOld, 428 const G4Fiel << 182 const G4double dChordStep ) // Current dchord achieved. 429 const G4Fiel << 183 430 const G4Thre << 431 const G4Thre << 432 const G4Thre << 433 G4bool << 434 { 184 { 435 // ApproxCurvePointS is 2nd implementation o << 185 G4double stepTrial; 436 // Use Brent Algorithm (or InvParabolic) whe << 437 // Given a starting curve point A (CurveA_Po << 438 // (CurveB_PointVelocity), a point E which i << 439 // and a point F which is on the curve (fir << 440 // point S on the curve closer to point E. << 441 // While advancing towards S utilise 'eps_st << 442 // relative accuracy of each Step. << 443 << 444 G4FieldTrack EndPoint(CurveA_PointVelocity); << 445 if(!first) { EndPoint = ApproxCurveV; } << 446 << 447 G4ThreeVector Point_A,Point_B; << 448 Point_A=CurveA_PointVelocity.GetPosition(); << 449 Point_B=CurveB_PointVelocity.GetPosition(); << 450 << 451 G4double xa,xb,xc,ya,yb,yc; << 452 << 453 // InverseParabolic. AF Intersects (First Pa << 454 186 455 if(first) << 187 if ( dChordStep > 1000. * fDeltaChord ){ 456 { << 188 stepTrial= stepTrialOld * 0.03; 457 xa=0.; << 189 }else{ 458 ya=(PointG-Point_A).mag(); << 190 if ( dChordStep > 100. * fDeltaChord ){ 459 xb=(Point_A-CurrentF_Point).mag(); << 191 stepTrial= stepTrialOld * 0.1; 460 yb=-(PointG-CurrentF_Point).mag(); << 192 }else{ 461 xc=(Point_A-Point_B).mag(); << 193 // Keep halving the length until dChordStep OK 462 yc=-(CurrentE_Point-Point_B).mag(); << 194 stepTrial= stepTrialOld * 0.5; 463 } << 195 } 464 else << 465 { << 466 xa=0.; << 467 ya=(Point_A-CurrentE_Point).mag(); << 468 xb=(Point_A-CurrentF_Point).mag(); << 469 yb=(PointG-CurrentF_Point).mag(); << 470 xc=(Point_A-Point_B).mag(); << 471 yc=-(Point_B-PointG).mag(); << 472 if(xb==0.) << 473 { << 474 EndPoint = ApproxCurvePointV(CurveA_Poin << 475 CurrentE_Po << 476 return EndPoint; << 477 } << 478 } 196 } 479 197 480 const G4double tolerance = 1.e-12; << 198 // A more sophisticated chord-finder could figure out a better 481 if(std::abs(ya)<=tolerance||std::abs(yc)<=to << 199 // stepTrial, from dChordStep and the required d_geometry 482 { << 200 // eg 483 ; // What to do for the moment: return the << 201 // Calculate R, r_helix (eg at orig point) 484 // then PropagatorInField will take care << 202 // if( stepTrial < 2 pi R ) 485 } << 203 // stepTrial = R arc_cos( 1 - fDeltaChord / r_helix ) 486 else << 204 // else 487 { << 205 // ?? 488 G4double test_step = InvParabolic(xa,ya,xb << 489 G4double curve; << 490 if(first) << 491 { << 492 curve=std::abs(EndPoint.GetCurveLength() << 493 -ApproxCurveV.GetCurveLeng << 494 } << 495 else << 496 { << 497 test_step = test_step - xb; << 498 curve=std::abs(EndPoint.GetCurveLength() << 499 -CurveB_PointVelocity.GetC << 500 xb = (CurrentF_Point-Point_B).mag(); << 501 } << 502 << 503 if(test_step<=0) { test_step=0.1*xb; } << 504 if(test_step>=xb) { test_step=0.5*xb; } << 505 if(test_step>=curve){ test_step=0.5*curve; << 506 << 507 if(curve*(1.+eps_step)<xb) // Similar to R << 508 { // G4VIntersect << 509 test_step=0.5*curve; << 510 } << 511 206 512 fIntgrDriver->AccurateAdvance(EndPoint,tes << 207 return stepTrial; 513 << 514 #ifdef G4DEBUG_FIELD << 515 // Printing Brent and Linear Approximation << 516 // << 517 G4cout << "G4ChordFinder::ApproxCurvePoint << 518 << test_step << " EndPoint = " << << 519 << 520 // Test Track << 521 // << 522 G4FieldTrack TestTrack( CurveA_PointVeloci << 523 TestTrack = ApproxCurvePointV( CurveA_Poin << 524 CurveB_Poin << 525 CurrentE_Po << 526 G4cout.precision(14); << 527 G4cout << "G4ChordFinder::BrentApprox = " << 528 G4cout << "G4ChordFinder::LinearApprox= " << 529 #endif << 530 } << 531 return EndPoint; << 532 } 208 } 533 209 534 << 210 // 535 // ........................................... << 211 // Given a starting curve point A (CurveA_PointVelocity), a later 536 << 212 // curve point B (CurveB_PointVelocity) and a point E which is (generally) 537 G4FieldTrack G4ChordFinder:: << 213 // not on the curve, find and return a point F which is on the curve and 538 ApproxCurvePointV( const G4FieldTrack& CurveA_ << 214 // which is close to E. While advancing towards F utilise eps_step 539 const G4FieldTrack& CurveB_ << 215 // as a measure of the relative accuracy of each Step. 540 const G4ThreeVector& Curren << 216 541 G4double eps_step) << 217 G4FieldTrack G4ChordFinder::ApproxCurvePointV( >> 218 const G4FieldTrack& CurveA_PointVelocity, >> 219 const G4FieldTrack& CurveB_PointVelocity, >> 220 const G4ThreeVector& CurrentE_Point, >> 221 const G4double eps_step) 542 { 222 { 543 // If r=|AE|/|AB|, and s=true path lenght (A << 223 // 1st implementation: 544 // return the point that is r*s along the cu << 224 // if r=|AE|/|AB|, and s=true path lenght (AB) 545 << 225 // return the point that is r*s along the curve! 546 G4FieldTrack Current_PointVelocity = Curve << 547 226 548 G4ThreeVector CurveA_Point= CurveA_PointVel << 227 G4FieldTrack Current_PointVelocity= CurveA_PointVelocity; 549 G4ThreeVector CurveB_Point= CurveB_PointVel << 228 >> 229 G4ThreeVector CurveA_Point= CurveA_PointVelocity.Position(); >> 230 G4ThreeVector CurveB_Point= CurveB_PointVelocity.Position(); 550 231 551 G4ThreeVector ChordAB_Vector= CurveB_Point 232 G4ThreeVector ChordAB_Vector= CurveB_Point - CurveA_Point; 552 G4ThreeVector ChordAE_Vector= CurrentE_Poin 233 G4ThreeVector ChordAE_Vector= CurrentE_Point - CurveA_Point; 553 234 554 G4double ABdist= ChordAB_Vector.mag(); 235 G4double ABdist= ChordAB_Vector.mag(); 555 G4double curve_length; // A curve length 236 G4double curve_length; // A curve length of AB 556 G4double AE_fraction; 237 G4double AE_fraction; 557 238 558 curve_length= CurveB_PointVelocity.GetCurveL << 239 curve_length= 559 - CurveA_PointVelocity.GetCurveL << 240 CurveB_PointVelocity.CurveS() - CurveA_PointVelocity.CurveS(); 560 << 241 561 G4double integrationInaccuracyLimit= std::ma << 242 // const 562 if( curve_length < ABdist * (1. - integratio << 243 G4double integrationInaccuracyLimit= G4std::max( perMillion, 0.5*eps_step ); 563 { << 244 if( curve_length < ABdist * (1. - integrationInaccuracyLimit) ){ 564 #ifdef G4DEBUG_FIELD << 245 // #ifdef G4DEBUG 565 G4cerr << " Warning in G4ChordFinder::Appr << 246 G4cerr << " Warning in G4ChordFinder::ApproxCurvePoint: " << G4endl << 566 << G4endl << 247 " The two points are further apart than the curve length " << G4endl << 567 << " The two points are further apa << 248 " Dist = " << ABdist << 568 << G4endl << 249 " curve length = " << curve_length 569 << " Dist = " << ABdist << 250 << " relativeDiff = " << (curve_length-ABdist)/ABdist 570 << " curve length = " << curve_leng << 251 << G4endl; 571 << " relativeDiff = " << (curve_len << 252 // #endif 572 << G4endl; << 253 if( curve_length < ABdist * (1. - 10*eps_step) ) { 573 if( curve_length < ABdist * (1. - 10*eps_s << 254 // #ifdef G4DEBUG 574 { << 255 G4cerr << " ERROR: the size of the above difference exceeds allowed limits. Aborting." 575 std::ostringstream message; << 256 << G4endl; 576 message << "Unphysical curve length." << << 257 // #endif 577 << "The size of the above differ << 258 G4Exception("G4ChordFinder::ApproxCurvePoint> Unphysical curve length."); 578 << G4endl << 579 << "Aborting."; << 580 G4Exception("G4ChordFinder::ApproxCurveP << 581 FatalException, message); << 582 } 259 } 583 #endif << 260 // Take default corrective action: 584 // Take default corrective action: adjust << 261 // --> adjust the maximum curve length. 585 // NOTE: this case only happens for relati << 262 // NOTE: this case only happens for relatively straight paths. 586 // curve_length = ABdist; << 263 curve_length = ABdist; 587 } 264 } 588 265 589 G4double new_st_length; << 266 G4double new_st_length; 590 267 591 if ( ABdist > 0.0 ) << 268 if ( ABdist > 0.0 ){ 592 { << 593 AE_fraction = ChordAE_Vector.mag() / ABdi 269 AE_fraction = ChordAE_Vector.mag() / ABdist; 594 } << 270 }else{ 595 else << 271 G4cerr << " Error in G4ChordFinder::ApproxCurvePoint: A and B are the same point\n" << 596 { << 272 " Chord AB length = " << ChordAE_Vector.mag() << G4endl << G4endl; 597 AE_fraction = 0.5; 273 AE_fraction = 0.5; // Guess .. ?; 598 #ifdef G4DEBUG_FIELD << 599 G4cout << "Warning in G4ChordFinder::Appr << 600 << " A and B are the same point!" << 601 << " Chord AB length = " << ChordA << 602 << G4endl; << 603 #endif << 604 } 274 } 605 275 606 if( (AE_fraction> 1.0 + perMillion) || (AE_f << 276 if( (AE_fraction> 1.0 + perMillion) || (AE_fraction< 0.) ){ 607 { << 277 G4cerr << " G4ChordFinder::ApproxCurvePointV: Warning: Anomalous condition:AE > AB or AE/AB <= 0 " << G4endl << 608 #ifdef G4DEBUG_FIELD << 278 " AE_fraction = " << AE_fraction << G4endl << 609 G4cerr << " G4ChordFinder::ApproxCurvePoin << 279 " Chord AE length = " << ChordAE_Vector.mag() << G4endl << 610 << " Anomalous condition:AE > AB or << 280 " Chord AB length = " << ABdist << G4endl << G4endl; 611 << " AE_fraction = " << AE_fract << 281 G4cerr << " OK if this condition occurs after a recalculation of 'B'" << G4endl 612 << " Chord AE length = " << Chord << 282 << " Otherwise it is an error. " << G4endl ; 613 << " Chord AB length = " << ABdis << 614 G4cerr << " OK if this condition occurs af << 615 << G4endl << " Otherwise it is an e << 616 #endif << 617 // This course can now result if B has be 283 // This course can now result if B has been re-evaluated, 618 // without E being recomputed (1 July 99) << 284 // without E being recomputed (1 July 99) 619 // In this case this is not a "real error << 285 // In this case this is not a "real error" - but it undesired 620 // and we cope with it by a default corre << 286 // and we cope with it by a default corrective action ... 621 // << 622 AE_fraction = 0.5; 287 AE_fraction = 0.5; // Default value 623 } 288 } 624 289 625 new_st_length = AE_fraction * curve_length; << 290 new_st_length= AE_fraction * curve_length; 626 291 627 if ( AE_fraction > 0.0 ) << 292 G4bool good_advance; 628 { << 293 if ( AE_fraction > 0.0 ) { 629 fIntgrDriver->AccurateAdvance(Current_Poi << 294 good_advance = 630 new_st_leng << 295 fIntgrDriver->AccurateAdvance(Current_PointVelocity, 631 // << 296 new_st_length, >> 297 eps_step ); // Relative accuracy 632 // In this case it does not matter if it 298 // In this case it does not matter if it cannot advance the full distance 633 } 299 } 634 300 635 // If there was a memory of the step_length << 301 // If there was a memory of the step_length actually require at the start 636 // of the integration Step, this could be re 302 // of the integration Step, this could be re-used ... 637 303 638 G4cout.precision(14); << 639 << 640 return Current_PointVelocity; 304 return Current_PointVelocity; 641 } 305 } 642 306 643 // ........................................... << 644 307 645 std::ostream& operator<<( std::ostream& os, co << 646 { << 647 // Dumping the state of G4ChordFinder << 648 os << "State of G4ChordFinder : " << std::e << 649 os << " delta_chord = " << cf.fDeltaCh << 650 os << " Default d_c = " << cf.fDefault << 651 << 652 os << " stats-verbose = " << cf.fStatsVe << 653 << 654 return os; << 655 } << 656 308