Geant4 Cross Reference |
1 // 1 2 // ******************************************* 3 // * License and Disclaimer 4 // * 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 // 26 // G4AdjointCrossSurfChecker implementation 27 // 28 // ------------------------------------------- 29 // Class Name: G4AdjointCrossSurfChecker 30 // Author: L. Desorgher, 2007-2009 31 // Organisation: SpaceIT GmbH 32 // Contract: ESA contract 21435/08/NL/AT 33 // Customer: ESA/ESTEC 34 // ------------------------------------------- 35 36 #include "G4AdjointSimManager.hh" 37 38 #include "G4AdjointCrossSurfChecker.hh" 39 #include "G4AdjointPrimaryGeneratorAction.hh" 40 #include "G4AdjointSimMessenger.hh" 41 #include "G4AdjointStackingAction.hh" 42 #include "G4AdjointSteppingAction.hh" 43 #include "G4AdjointTrackingAction.hh" 44 #include "G4ParticleTable.hh" 45 #include "G4PhysicsLogVector.hh" 46 #include "G4Run.hh" 47 #include "G4RunManager.hh" 48 #include "G4WorkerRunManager.hh" 49 #include "G4UserEventAction.hh" 50 #include "G4UserRunAction.hh" 51 #include "G4UserStackingAction.hh" 52 #include "G4UserSteppingAction.hh" 53 #include "G4UserTrackingAction.hh" 54 #include "G4VUserPrimaryGeneratorAction.hh" 55 56 // ------------------------------------------- 57 // 58 G4ThreadLocal G4AdjointSimManager* G4AdjointSi 59 60 // ------------------------------------------- 61 // 62 G4AdjointSimManager::G4AdjointSimManager() 63 { 64 theAdjointPrimaryGeneratorAction = new G4Adj 65 theAdjointSteppingAction = new G4AdjointStep 66 theAdjointTrackingAction = new G4AdjointTrac 67 theAdjointStackingAction = new G4AdjointStac 68 theAdjointTrackingAction->SetListOfPrimaryFw 69 theAdjointPrimaryGeneratorAction->GetListOfP 70 71 theMessenger = new G4AdjointSimMessenger(thi 72 } 73 74 // ------------------------------------------- 75 // 76 G4AdjointSimManager::~G4AdjointSimManager() 77 { 78 delete theAdjointRunAction; 79 delete theAdjointPrimaryGeneratorAction; 80 delete theAdjointSteppingAction; 81 delete theAdjointEventAction; 82 delete theAdjointTrackingAction; 83 delete theAdjointStackingAction; 84 delete theMessenger; 85 } 86 87 // ------------------------------------------- 88 // 89 G4AdjointSimManager* G4AdjointSimManager::GetI 90 { 91 if (instance == nullptr) instance = new G4Ad 92 return instance; 93 } 94 95 // ------------------------------------------- 96 // 97 void G4AdjointSimManager::RunAdjointSimulation 98 { 99 100 if (welcome_message) { 101 G4cout << "******************************* 102 G4cout << "*** Geant4 Reverse/Adjoint Mont 103 G4cout << "*** Author: L.Desorgher 104 G4cout << "*** Company: SpaceIT GmbH, 105 G4cout << "*** Sponsored by: ESA/ESTEC con 106 G4cout << "******************************* 107 welcome_message = false; 108 } 109 110 // Switch to adjoint simulation mode 111 //------------------------------------------ 112 SwitchToAdjointSimulationMode(); 113 114 // Make the run 115 //------------ 116 nb_evt_of_last_run = nb_evt; 117 if (G4Threading::G4GetThreadId() < 0){ 118 G4RunManager::GetRunManager()->BeamOn( 119 G4int(nb_evt * theAdjointPrimaryGeneratorA 120 } 121 } 122 123 // ------------------------------------------- 124 // 125 void G4AdjointSimManager::SetRestOfAdjointActi 126 { if (G4Threading::G4GetThreadId() == -1) retu 127 G4RunManager* theRunManager = G4RunManager:: 128 129 if (!user_action_already_defined) DefineUser 130 131 // Replace the user action by the adjoint ac 132 //------------------------------------------ 133 134 theRunManager->G4RunManager::SetUserAction(t 135 theRunManager->G4RunManager::SetUserAction(t 136 theRunManager->G4RunManager::SetUserAction(t 137 } 138 139 // ------------------------------------------- 140 // 141 void G4AdjointSimManager::SwitchToAdjointSimul 142 { 143 // Replace the user defined actions by the a 144 //------------------------------------------ 145 SetAdjointActions(); 146 147 // Update the list of primaries 148 //----------------------------- 149 if (G4Threading::G4GetThreadId() != -1) the 150 adjoint_sim_mode = true; 151 ID_of_last_particle_that_reach_the_ext_sourc 152 153 } 154 155 // ------------------------------------------- 156 // 157 void G4AdjointSimManager::BackToFwdSimulationM 158 { 159 // Restore the user defined actions 160 //-------------------------------- 161 ResetUserActions(); 162 adjoint_sim_mode = false; 163 } 164 165 // ------------------------------------------- 166 // 167 void G4AdjointSimManager::SetAdjointActions() 168 { 169 auto theRunManager = G4RunManager::GetRunMan 170 171 172 if (!user_action_already_defined) DefineUser 173 174 175 // Replace the user action by the adjoint ac 176 //------------------------------------------ 177 theRunManager->G4RunManager::SetUserAction(t 178 if (G4RunManager::GetRunManager()->GetRunMan 179 G4RunManager::GetRunManager()->GetRunMan 180 theRunManager->G4RunManager::SetUserAction(t 181 theRunManager->G4RunManager::SetUserAction(t 182 if (use_user_StackingAction) 183 theAdjointStackingAction->SetUserFwdStacki 184 else 185 theAdjointStackingAction->SetUserFwdStacki 186 theRunManager->G4RunManager::SetUserAction(t 187 theRunManager->G4RunManager::SetUserAction(t 188 theRunManager->G4RunManager::SetUserAction(t 189 if (use_user_TrackingAction) 190 theAdjointTrackingAction->SetUserForwardTr 191 else 192 theAdjointTrackingAction->SetUserForwardTr 193 } 194 } 195 196 // ------------------------------------------- 197 // 198 void G4AdjointSimManager::SetAdjointPrimaryRun 199 { 200 G4RunManager* theRunManager = G4RunManager:: 201 202 if (!user_action_already_defined) DefineUser 203 204 // Replace the user action by the adjoint ac 205 //------------------------------------------ 206 207 theRunManager->G4RunManager::SetUserAction(t 208 if (G4Threading::G4GetThreadId() != -1) { 209 theRunManager->G4RunManager::SetUserAction(t 210 theRunManager->G4RunManager::SetUserAction(t 211 if (use_user_StackingAction) 212 theAdjointStackingAction->SetUserFwdStacki 213 else 214 theAdjointStackingAction->SetUserFwdStacki 215 } 216 } 217 218 // ------------------------------------------- 219 // 220 void G4AdjointSimManager::ResetUserActions() 221 { 222 G4RunManager* theRunManager = G4RunManager:: 223 224 // Restore the user defined actions 225 //------------------------------- 226 theRunManager->G4RunManager::SetUserAction(f 227 if (G4Threading::G4GetThreadId() != -1) { 228 theRunManager->G4RunManager::SetUserAction(f 229 theRunManager->G4RunManager::SetUserAction(f 230 theRunManager->G4RunManager::SetUserAction(f 231 theRunManager->G4RunManager::SetUserAction(f 232 theRunManager->G4RunManager::SetUserAction(f 233 } 234 } 235 236 // ------------------------------------------- 237 // 238 void G4AdjointSimManager::ResetRestOfUserActio 239 { 240 if (G4Threading::G4GetThreadId() == -1) retu 241 G4RunManager* theRunManager = G4RunManager:: 242 243 // Restore the user defined actions 244 //------------------------------- 245 246 theRunManager->G4RunManager::SetUserAction(f 247 theRunManager->G4RunManager::SetUserAction(f 248 theRunManager->G4RunManager::SetUserAction(f 249 } 250 251 // ------------------------------------------- 252 // 253 void G4AdjointSimManager::ResetUserPrimaryRunA 254 { 255 G4RunManager* theRunManager = G4RunManager:: 256 // Restore the user defined actions 257 //------------------------------- 258 theRunManager->G4RunManager::SetUserAction(f 259 if (G4Threading::G4GetThreadId() != -1) { 260 theRunManager->G4RunManager::SetUserAction(f 261 theRunManager->G4RunManager::SetUserAction(f 262 } 263 } 264 265 // ------------------------------------------- 266 // 267 void G4AdjointSimManager::DefineUserActions() 268 { 269 G4RunManager* theRunManager = G4RunManager:: 270 fUserRunAction = 271 const_cast<G4UserRunAction*>(theRunManager 272 if (G4Threading::G4GetThreadId() != -1) { 273 fUserTrackingAction = const_cast<G4UserTrack 274 fUserEventAction = const_cast<G4UserEventAct 275 fUserSteppingAction = const_cast<G4UserStepp 276 theAdjointSteppingAction->SetUserForwardStep 277 fUserPrimaryGeneratorAction = 278 const_cast<G4VUserPrimaryGeneratorAction*> 279 fUserStackingAction = const_cast<G4UserStack 280 user_action_already_defined = true; 281 } 282 } 283 284 // ------------------------------------------- 285 // 286 G4bool G4AdjointSimManager::GetAdjointTracking 287 { 288 return theAdjointTrackingAction->GetIsAdjoin 289 } 290 291 // ------------------------------------------- 292 // 293 void G4AdjointSimManager::SetAdjointTrackingMo 294 { 295 adjoint_tracking_mode = aBool; 296 297 if (adjoint_tracking_mode) { 298 SetRestOfAdjointActions(); 299 theAdjointStackingAction->SetAdjointMode(t 300 theAdjointStackingAction->SetKillTracks(fa 301 } 302 else { 303 ResetRestOfUserActions(); 304 theAdjointStackingAction->SetAdjointMode(f 305 if (GetDidAdjParticleReachTheExtSource()) 306 theAdjointStackingAction->SetKillTracks( 307 RegisterAtEndOfAdjointTrack(); 308 } 309 else 310 theAdjointStackingAction->SetKillTracks( 311 } 312 } 313 314 // ------------------------------------------- 315 // 316 G4bool G4AdjointSimManager::GetDidAdjParticleR 317 { 318 return (GetNbOfAdointTracksReachingTheExtern 319 } 320 321 // ------------------------------------------- 322 // 323 std::vector<G4ParticleDefinition*>* G4AdjointS 324 { 325 return theAdjointPrimaryGeneratorAction->Get 326 } 327 328 // ------------------------------------------- 329 // 330 std::size_t G4AdjointSimManager::GetNbOfPrimar 331 { 332 return theAdjointPrimaryGeneratorAction->Get 333 } 334 335 // ------------------------------------------- 336 // 337 G4ThreeVector G4AdjointSimManager::GetPosition 338 { 339 return theAdjointTrackingAction->GetPosition 340 } 341 342 // ------------------------------------------- 343 // 344 G4ThreeVector G4AdjointSimManager::GetDirectio 345 { 346 return theAdjointTrackingAction->GetDirectio 347 } 348 349 // ------------------------------------------- 350 // 351 G4double G4AdjointSimManager::GetEkinAtEndOfLa 352 { 353 return theAdjointTrackingAction->GetEkinAtEn 354 } 355 356 // ------------------------------------------- 357 // 358 G4double G4AdjointSimManager::GetEkinNucAtEndO 359 { 360 return theAdjointTrackingAction->GetEkinNucA 361 } 362 363 // ------------------------------------------- 364 // 365 G4double G4AdjointSimManager::GetWeightAtEndOf 366 { 367 return theAdjointTrackingAction->GetWeightAt 368 } 369 370 // ------------------------------------------- 371 // 372 G4double G4AdjointSimManager::GetCosthAtEndOfL 373 { 374 return theAdjointTrackingAction->GetCosthAtE 375 } 376 377 // ------------------------------------------- 378 // 379 const G4String& G4AdjointSimManager::GetFwdPar 380 { 381 return theAdjointTrackingAction->GetFwdParti 382 } 383 384 // ------------------------------------------- 385 // 386 G4int G4AdjointSimManager::GetFwdParticlePDGEn 387 { 388 return theAdjointTrackingAction->GetFwdParti 389 } 390 391 // ------------------------------------------- 392 // 393 G4int G4AdjointSimManager::GetFwdParticleIndex 394 { 395 return theAdjointTrackingAction->GetLastFwdP 396 } 397 398 // ------------------------------------------- 399 // 400 std::size_t G4AdjointSimManager::GetNbOfAdoint 401 { 402 return theAdjointTrackingAction->GetNbOfAdoi 403 } 404 405 // ------------------------------------------- 406 // 407 void G4AdjointSimManager::ClearEndOfAdjointTra 408 { 409 theAdjointTrackingAction->ClearEndOfAdjointT 410 } 411 412 // ------------------------------------------- 413 // 414 void G4AdjointSimManager::RegisterAtEndOfAdjoi 415 { 416 last_pos = theAdjointSteppingAction->GetLast 417 last_direction = theAdjointSteppingAction->G 418 last_direction /= last_direction.mag(); 419 last_cos_th = last_direction.z(); 420 G4ParticleDefinition* aPartDef = theAdjointS 421 422 last_fwd_part_name = aPartDef->GetParticleNa 423 424 last_fwd_part_name.erase(0, 4); 425 426 last_fwd_part_PDGEncoding = 427 G4ParticleTable::GetParticleTable()->FindP 428 429 std::vector<G4ParticleDefinition*>* aList = 430 theAdjointPrimaryGeneratorAction->GetListO 431 last_fwd_part_index = -1; 432 G4int i = 0; 433 while (i < (G4int)aList->size() && last_fwd_ 434 if ((*aList)[i]->GetParticleName() == last 435 ++i; 436 } 437 438 last_ekin = theAdjointSteppingAction->GetLas 439 last_ekin_nuc = last_ekin; 440 if (aPartDef->GetParticleType() == "adjoint_ 441 nb_nuc = static_cast<G4double>(aPartDef->G 442 last_ekin_nuc /= nb_nuc; 443 } 444 445 last_weight = theAdjointSteppingAction->GetL 446 447 last_pos_vec.push_back(last_pos); 448 last_direction_vec.push_back(last_direction) 449 last_ekin_vec.push_back(last_ekin); 450 last_ekin_nuc_vec.push_back(last_ekin_nuc); 451 last_cos_th_vec.push_back(last_cos_th); 452 last_weight_vec.push_back(last_weight); 453 last_fwd_part_PDGEncoding_vec.push_back(last 454 last_fwd_part_index_vec.push_back(last_fwd_p 455 ID_of_last_particle_that_reach_the_ext_sourc 456 ID_of_last_particle_that_reach_the_ext_sourc 457 ID_of_last_particle_that_reach_the_ext_sou 458 } 459 460 // ------------------------------------------- 461 // 462 G4bool G4AdjointSimManager::DefineSphericalExt 463 { 464 G4double area; 465 return G4AdjointCrossSurfChecker::GetInstanc 466 467 } 468 469 // ------------------------------------------- 470 // 471 G4bool G4AdjointSimManager::DefineSphericalExt 472 G4double radius, const G4String& volume_name 473 { 474 G4double area; 475 G4ThreeVector center; 476 return G4AdjointCrossSurfChecker::GetInstanc 477 ->AddaSphericalSurfaceWithCenterAtTheCente 478 479 } 480 481 // ------------------------------------------- 482 // 483 G4bool G4AdjointSimManager::DefineExtSourceOnT 484 { 485 G4double area; 486 return G4AdjointCrossSurfChecker::GetInstanc 487 488 } 489 490 // ------------------------------------------- 491 // 492 void G4AdjointSimManager::SetExtSourceEmax(G4d 493 { 494 theAdjointSteppingAction->SetExtSourceEMax(E 495 } 496 497 // ------------------------------------------- 498 // 499 G4bool G4AdjointSimManager::DefineSphericalAdj 500 { 501 G4double area; 502 G4bool aBool = G4AdjointCrossSurfChecker::Ge 503 504 theAdjointPrimaryGeneratorAction->SetSpheric 505 area_of_the_adjoint_source = area; 506 return aBool; 507 } 508 509 // ------------------------------------------- 510 // 511 G4bool G4AdjointSimManager::DefineSphericalAdj 512 G4double radius, const G4String& volume_name 513 { 514 G4double area; 515 G4ThreeVector center; 516 G4bool aBool = 517 G4AdjointCrossSurfChecker::GetInstance()-> 518 "AdjointSource", radius, volume_name, ce 519 theAdjointPrimaryGeneratorAction->SetSpheric 520 area_of_the_adjoint_source = area; 521 return aBool; 522 } 523 524 // ------------------------------------------- 525 // 526 G4bool G4AdjointSimManager::DefineAdjointSourc 527 { 528 G4double area; 529 G4bool aBool = G4AdjointCrossSurfChecker::Ge 530 "AdjointSource", volume_name, area); 531 area_of_the_adjoint_source = area; 532 if (aBool) { 533 theAdjointPrimaryGeneratorAction->SetAdjoi 534 } 535 return aBool; 536 } 537 538 // ------------------------------------------- 539 // 540 void G4AdjointSimManager::SetAdjointSourceEmin 541 { 542 theAdjointPrimaryGeneratorAction->SetEmin(Em 543 } 544 545 // ------------------------------------------- 546 // 547 void G4AdjointSimManager::SetAdjointSourceEmax 548 { 549 theAdjointPrimaryGeneratorAction->SetEmax(Em 550 } 551 552 // ------------------------------------------- 553 // 554 void G4AdjointSimManager::ConsiderParticleAsPr 555 { 556 theAdjointPrimaryGeneratorAction->ConsiderPa 557 } 558 559 // ------------------------------------------- 560 // 561 void G4AdjointSimManager::NeglectParticleAsPri 562 { 563 theAdjointPrimaryGeneratorAction->NeglectPar 564 } 565 566 // ------------------------------------------- 567 // 568 void G4AdjointSimManager::SetPrimaryIon(G4Part 569 G4Part 570 { 571 theAdjointPrimaryGeneratorAction->SetPrimary 572 } 573 574 // ------------------------------------------- 575 // 576 const G4String& G4AdjointSimManager::GetPrimar 577 { 578 return theAdjointPrimaryGeneratorAction->Get 579 } 580 581 // ------------------------------------------- 582 // 583 void G4AdjointSimManager::RegisterAdjointPrima 584 { 585 theAdjointPrimaryWeight = aWeight; 586 theAdjointSteppingAction->SetPrimWeight(aWei 587 } 588 589 // ------------------------------------------- 590 // 591 void G4AdjointSimManager::SetAdjointEventActio 592 { 593 theAdjointEventAction = anAction; 594 } 595 596 // ------------------------------------------- 597 // 598 void G4AdjointSimManager::SetAdjointSteppingAc 599 { 600 theAdjointSteppingAction->SetUserAdjointStep 601 } 602 603 // ------------------------------------------- 604 // 605 void G4AdjointSimManager::SetAdjointStackingAc 606 { 607 theAdjointStackingAction->SetUserAdjointStac 608 } 609 610 // ------------------------------------------- 611 // 612 void G4AdjointSimManager::SetAdjointRunAction( 613 { 614 theAdjointRunAction = anAction; 615 } 616 617 // ------------------------------------------- 618 // 619 void G4AdjointSimManager::SetNbOfPrimaryFwdGam 620 { 621 theAdjointPrimaryGeneratorAction->SetNbPrima 622 } 623 624 // ------------------------------------------- 625 // 626 void G4AdjointSimManager::SetNbAdjointPrimaryG 627 { 628 theAdjointPrimaryGeneratorAction->SetNbAdjoi 629 } 630 631 // ------------------------------------------- 632 // 633 void G4AdjointSimManager::SetNbAdjointPrimaryE 634 { 635 theAdjointPrimaryGeneratorAction->SetNbAdjoi 636 } 637 638 // ------------------------------------------- 639 // 640 void G4AdjointSimManager::BeginOfRunAction(con 641 { if (!adjoint_sim_mode){ 642 if(fUserRunAction != nullptr) fUserRunAct 643 } 644 else { 645 if (theAdjointRunAction != nullptr) theAd 646 } 647 648 //fUserRunAction->BeginOfRunAction(aRun); 649 } 650 651 // ------------------------------------------- 652 // 653 void G4AdjointSimManager::EndOfRunAction(const 654 { 655 if (!adjoint_sim_mode) { 656 if (fUserRunAction != nullptr) fUserRunAct 657 } 658 else if (theAdjointRunAction != nullptr) 659 theAdjointRunAction->EndOfRunAction(aRun); 660 661 BackToFwdSimulationMode(); 662 663 } 664 665 // ------------------------------------------- 666 // 667 G4ParticleDefinition* G4AdjointSimManager::Get 668 { 669 return theAdjointPrimaryGeneratorAction->Get 670 } 671 672 // ------------------------------------------- 673 // 674 void G4AdjointSimManager::ResetDidOneAdjPartRe 675 { 676 theAdjointSteppingAction->ResetDidOneAdjPart 677 } 678