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 // 27 // Author: Mathieu Karamitros (kara@cenbg.in2p 28 // 29 // WARNING : This class is released as a proto 30 // It might strongly evolve or even disappear 31 // 32 // History: 33 // ----------- 34 // 10 Oct 2011 M.Karamitros created 35 // 36 // ------------------------------------------- 37 38 #include "G4DNAChemistryManager.hh" 39 40 #include "G4AutoLock.hh" 41 #include "G4DNAMolecularReactionTable.hh" 42 #include "G4DNAWaterExcitationStructure.hh" 43 #include "G4DNAWaterIonisationStructure.hh" 44 #include "G4Electron_aq.hh" 45 #include "G4GeometryManager.hh" 46 #include "G4H2O.hh" 47 #include "G4MolecularConfiguration.hh" 48 #include "G4Molecule.hh" 49 #include "G4MoleculeFinder.hh" 50 #include "G4MoleculeTable.hh" 51 #include "G4PhysChemIO.hh" 52 #include "G4Scheduler.hh" 53 #include "G4StateManager.hh" 54 #include "G4SystemOfUnits.hh" 55 #include "G4UIcmdWithABool.hh" 56 #include "G4UIcmdWithADoubleAndUnit.hh" 57 #include "G4UIcmdWithAnInteger.hh" 58 #include "G4UIcmdWithoutParameter.hh" 59 #include "G4VITTrackHolder.hh" 60 #include "G4VMoleculeCounter.hh" 61 #include "G4VUserChemistryList.hh" 62 #include "G4VUserPulseInfo.hh" 63 64 #include <memory> 65 66 G4DNAChemistryManager* G4DNAChemistryManager:: 67 68 G4ThreadLocal G4DNAChemistryManager::ThreadLoc 69 G4DNAChemistryManager::fpThreadData = null 70 71 G4Mutex chemManExistence; 72 73 //-------------------------------------------- 74 75 G4DNAChemistryManager::ThreadLocalData::Thread 76 { 77 fpPhysChemIO = nullptr; 78 fThreadInitialized = false; 79 } 80 81 //-------------------------------------------- 82 83 G4DNAChemistryManager::ThreadLocalData::~Threa 84 { 85 fpThreadData = nullptr; 86 } 87 88 //-------------------------------------------- 89 90 void G4DNAChemistryManager::SetPhysChemIO(std: 91 { 92 fpThreadData->fpPhysChemIO = std::move(pPh 93 } 94 95 //-------------------------------------------- 96 97 //-------------------------------------------- 98 /* 99 * The chemistry manager is shared between thr 100 * It is initialized both on the master thread 101 */ 102 //-------------------------------------------- 103 G4DNAChemistryManager::G4DNAChemistryManager() 104 : 105 fpChemDNADirectory(new G4UIdirectory( 106 , fpActivateChem(new G4UIcmdWithABool( 107 , fpRunChem(new G4UIcmdWithAnInteger(" 108 , fpSkipReactionsFromChemList(new G4UI 109 , fpScaleForNewTemperature(new G4UIcmd 110 , fpInitChem(new G4UIcmdWithoutParamet 111 , 112 fpExcitationLevel(nullptr) 113 , fpIonisationLevel(nullptr) 114 , fpUserChemistryList(nullptr) 115 { 116 fpRunChem->SetParameterName("Number of run 117 "(this works w 118 fpRunChem->SetDefaultValue(1); 119 fpScaleForNewTemperature->SetUnitCategory( 120 } 121 122 //-------------------------------------------- 123 124 G4DNAChemistryManager* G4DNAChemistryManager:: 125 { 126 if (fgInstance == nullptr) 127 { 128 G4AutoLock lock(&chemManExistence); 129 if (fgInstance == nullptr) // MT : dou 130 { 131 fgInstance = new G4DNAChemistryMan 132 } 133 lock.unlock(); 134 } 135 136 // make sure thread local data is initiali 137 if (fpThreadData == nullptr) 138 { 139 fpThreadData = new ThreadLocalData(); 140 } 141 142 assert(fpThreadData != nullptr); 143 144 return fgInstance; 145 } 146 147 //-------------------------------------------- 148 149 G4DNAChemistryManager* G4DNAChemistryManager:: 150 { 151 return fgInstance; 152 } 153 154 //-------------------------------------------- 155 156 G4DNAChemistryManager::~G4DNAChemistryManager( 157 { 158 Clear(); 159 fgInstance = nullptr; 160 } 161 162 //-------------------------------------------- 163 164 void G4DNAChemistryManager::Clear() 165 { 166 fpIonisationLevel.reset(); 167 fpExcitationLevel.reset(); 168 169 if (fpUserChemistryList) 170 { 171 Deregister(*fpUserChemistryList); 172 } 173 174 fpChemDNADirectory.reset(); 175 fpActivateChem.reset(); 176 fpRunChem.reset(); 177 178 fpSkipReactionsFromChemList.reset(); 179 fpInitChem.reset(); 180 181 if (fpThreadData != nullptr) 182 { 183 delete fpThreadData; 184 fpThreadData = nullptr; 185 } 186 187 G4DNAMolecularReactionTable::DeleteInstanc 188 G4MolecularConfiguration::DeleteManager(); 189 G4VMoleculeCounter::DeleteInstance(); 190 } 191 192 //-------------------------------------------- 193 194 void G4DNAChemistryManager::DeleteInstance() 195 { 196 G4AutoLock lock(&chemManExistence); 197 198 if (fgInstance != nullptr) 199 { 200 G4DNAChemistryManager* pDeleteMe = fgI 201 fgInstance = nullptr; 202 lock.unlock(); 203 delete pDeleteMe; 204 } 205 else 206 { 207 G4cerr << "G4DNAChemistryManager alrea 208 } 209 lock.unlock(); 210 } 211 212 //-------------------------------------------- 213 214 G4bool G4DNAChemistryManager::Notify(G4Applica 215 { 216 if (requestedState == G4State_Quit) 217 { 218 if (fVerbose != 0) 219 { 220 G4cout << "G4DNAChemistryManager:: 221 << G4endl; 222 } 223 Clear(); 224 } 225 else if (requestedState == G4State_GeomClo 226 { 227 fGeometryClosed = true; 228 } 229 else if (requestedState == G4State_Idle) 230 { 231 InitializeThreadSharedData(); 232 } 233 234 return true; 235 } 236 237 //-------------------------------------------- 238 239 void G4DNAChemistryManager::SetNewValue(G4UIco 240 { 241 if (pCommand == fpActivateChem.get()) 242 { 243 SetChemistryActivation(G4UIcmdWithABoo 244 } 245 else if (pCommand == fpRunChem.get()) 246 { 247 int nbExec = value.empty() ? 1 : G4UIc 248 for (int i = 0 ; i < nbExec ; ++i) 249 { 250 Run(); 251 } 252 } 253 else if (pCommand == fpSkipReactionsFromCh 254 { 255 fSkipReactions = true; 256 } 257 else if (pCommand == fpScaleForNewTemperat 258 { 259 SetGlobalTemperature(fpScaleForNewTemp 260 } 261 else if (pCommand == fpInitChem.get()) 262 { 263 Initialize(); 264 InitializeThread(); 265 } 266 } 267 268 //-------------------------------------------- 269 270 G4String G4DNAChemistryManager::GetCurrentValu 271 { 272 if (pCommand == fpActivateChem.get()) 273 { 274 return G4UIcmdWithABool::ConvertToStri 275 } 276 if (pCommand == fpScaleForNewTemperature.g 277 { 278 return fpScaleForNewTemperature->Conve 279 } 280 if (pCommand == fpSkipReactionsFromChemLis 281 { 282 return G4UIcmdWithABool::ConvertToStri 283 } 284 285 return ""; 286 } 287 288 //-------------------------------------------- 289 290 void G4DNAChemistryManager::InitializeThreadSh 291 { 292 if (!G4Threading::IsMasterThread()) 293 { 294 return; 295 } 296 297 G4MoleculeTable::Instance()->PrepareMolecu 298 G4MoleculeTable::Instance()->Finalize(); 299 } 300 301 //-------------------------------------------- 302 void G4DNAChemistryManager::Run() 303 { 304 if (!fActiveChemistry) 305 { 306 return; 307 } 308 309 InitializeThread(); 310 311 if (!fMasterInitialized) 312 { 313 G4ExceptionDescription description; 314 description << "Global components were 315 G4Exception("G4DNAChemistryManager::Ru 316 description); 317 } 318 319 if (!fpThreadData->fThreadInitialized) 320 { 321 G4ExceptionDescription description; 322 description << "Thread local component 323 G4Exception("G4DNAChemistryManager::Ru 324 description); 325 } 326 327 G4MoleculeTable::Instance()->Finalize(); 328 G4Scheduler::Instance()->Process(); 329 if (fResetCounterWhenRunEnds) 330 { 331 G4VMoleculeCounter::Instance()->ResetC 332 } 333 CloseFile(); 334 } 335 336 //-------------------------------------------- 337 338 void G4DNAChemistryManager::UseAsStandalone(G4 339 { 340 fUseInStandalone = flag; 341 } 342 343 //-------------------------------------------- 344 345 void G4DNAChemistryManager::SetGun(G4ITGun* pC 346 { 347 G4Scheduler::Instance()->SetGun(pChemGun); 348 } 349 350 //-------------------------------------------- 351 352 void G4DNAChemistryManager::Initialize() 353 { 354 //======================================== 355 // MT MODE 356 //======================================== 357 if (G4Threading::IsMultithreadedApplicatio 358 { 359 //==================================== 360 // ON WORKER THREAD 361 //==================================== 362 if (G4Threading::IsWorkerThread()) 363 { 364 InitializeThread(); // Will create 365 return; 366 } 367 //==================================== 368 // ON MASTER THREAD 369 //==================================== 370 371 InitializeMaster(); 372 InitializeThreadSharedData(); 373 return; 374 } 375 //======================================== 376 // IS NOT IN MT MODE 377 //======================================== 378 379 InitializeMaster(); 380 InitializeThreadSharedData(); 381 // In this case: InitializeThread is calle 382 return; 383 } 384 385 //-------------------------------------------- 386 387 void G4DNAChemistryManager::InitializeMaster() 388 { 389 if (fMasterInitialized) 390 { 391 return; 392 } 393 394 if (fVerbose != 0) 395 { 396 G4cout << "G4DNAChemistryManager::Init 397 } 398 399 400 if (fpUserChemistryList == nullptr) 401 { 402 G4ExceptionDescription description; 403 description << "No user chemistry list 404 G4Exception("G4DNAChemistryManager::In 405 FatalException, descriptio 406 }else 407 { 408 fpUserChemistryList->ConstructDissociati 409 if (!fSkipReactions) 410 { 411 fpUserChemistryList->ConstructReacti 412 } 413 else 414 { 415 G4DNAMolecularReactionTable::GetReac 416 } 417 } 418 419 G4Scheduler::Instance(); 420 // creates a concrete object of the schedu 421 fMasterInitialized = true; 422 } 423 424 //-------------------------------------------- 425 426 void G4DNAChemistryManager::HandleStandaloneIn 427 { 428 if (!fUseInStandalone || fPhysicsTableBuil 429 { 430 return; 431 } 432 433 if (fVerbose != 0) 434 { 435 G4cout << "G4DNAChemistryManager: Buil 436 "molecule definition only." 437 << G4endl; 438 } 439 440 fpUserChemistryList->BuildPhysicsTable(); 441 442 if (!fGeometryClosed) 443 { 444 if (fVerbose != 0) 445 { 446 G4cout << "G4DNAChemistryManager: 447 } 448 449 G4GeometryManager* pGeomManager = G4Ge 450 pGeomManager->OpenGeometry(); 451 pGeomManager->CloseGeometry(true, true 452 fGeometryClosed = true; 453 } 454 455 fPhysicsTableBuilt = true; 456 } 457 458 //-------------------------------------------- 459 460 void G4DNAChemistryManager::InitializeThread() 461 { 462 if (fpThreadData->fThreadInitialized && !f 463 { 464 return; 465 } 466 467 if (fpUserChemistryList == nullptr) 468 { 469 G4ExceptionDescription description; 470 description << "No user chemistry list 471 G4Exception("G4DNAChemistryManager::In 472 FatalException, descriptio 473 }else 474 { 475 HandleStandaloneInitialization();// To 476 fpUserChemistryList->ConstructTimeStep 477 } 478 479 if (fVerbose != 0) 480 { 481 G4cout << "G4DNAChemistryManager::Init 482 << G4endl; 483 } 484 485 G4Scheduler::Instance()->Initialize(); 486 487 fpThreadData->fThreadInitialized = true; 488 489 G4VMoleculeCounter::InitializeInstance(); 490 491 InitializeFile(); 492 } 493 494 //-------------------------------------------- 495 496 void G4DNAChemistryManager::InitializeFile() 497 { 498 if (fVerbose != 0) 499 { 500 G4cout << "G4DNAChemistryManager::Init 501 << G4endl; 502 } 503 504 if (fpThreadData->fpPhysChemIO) 505 { 506 fpThreadData->fpPhysChemIO->Initialize 507 } 508 } 509 510 //-------------------------------------------- 511 512 G4bool G4DNAChemistryManager::IsActivated() 513 { 514 return fgInstance != nullptr ? fgInstance- 515 } 516 517 //-------------------------------------------- 518 519 G4bool G4DNAChemistryManager::IsChemistryActiv 520 { 521 return fActiveChemistry; 522 } 523 524 //-------------------------------------------- 525 526 void G4DNAChemistryManager::SetChemistryActiva 527 { 528 fActiveChemistry = flag; 529 } 530 531 //-------------------------------------------- 532 533 void G4DNAChemistryManager::WriteInto(const G4 534 std::ios 535 { 536 if (fVerbose != 0) 537 { 538 G4cout << "G4DNAChemistryManager: Writ 539 << output.data() << G4endl; 540 } 541 542 if (!fpThreadData->fpPhysChemIO) 543 { 544 fpThreadData->fpPhysChemIO = std::make 545 } 546 547 fpThreadData->fpPhysChemIO->WriteInto(outp 548 549 } 550 551 //-------------------------------------------- 552 553 void G4DNAChemistryManager::AddEmptyLineInOutp 554 { 555 if (fpThreadData->fpPhysChemIO) 556 { 557 fpThreadData->fpPhysChemIO->AddEmptyLi 558 } 559 } 560 561 //-------------------------------------------- 562 563 void G4DNAChemistryManager::CloseFile() 564 { 565 if (fpThreadData->fpPhysChemIO) 566 { 567 fpThreadData->fpPhysChemIO->CloseFile( 568 } 569 } 570 571 //-------------------------------------------- 572 573 G4DNAWaterExcitationStructure* G4DNAChemistryM 574 { 575 if (!fpExcitationLevel) 576 { 577 fpExcitationLevel = std::make_unique<G 578 } 579 return fpExcitationLevel.get(); 580 } 581 582 //-------------------------------------------- 583 584 G4DNAWaterIonisationStructure* G4DNAChemistryM 585 { 586 if (!fpIonisationLevel) 587 { 588 fpIonisationLevel = std::make_unique<G 589 } 590 return fpIonisationLevel.get(); 591 } 592 593 //-------------------------------------------- 594 595 void G4DNAChemistryManager::CreateWaterMolecul 596 597 598 { 599 if (fpThreadData->fpPhysChemIO) 600 { 601 G4double energy = -1.; 602 603 switch (modification) 604 { 605 case eDissociativeAttachment: 606 energy = 0.; 607 break; 608 case eExcitedMolecule: 609 energy = GetExcitationLevel()->Exc 610 break; 611 case eIonizedMolecule: 612 energy = GetIonisationLevel()->Ion 613 break; 614 } 615 616 fpThreadData->fpPhysChemIO->CreateWate 617 618 619 620 } 621 622 if (fActiveChemistry) 623 { 624 auto pH2OMolecule = new G4Molecule(G4 625 626 switch (modification) 627 { 628 case eDissociativeAttachment: 629 pH2OMolecule->AddElectron(5, 1); 630 break; 631 case eExcitedMolecule: 632 pH2OMolecule->ExciteMolecule(4 - e 633 break; 634 case eIonizedMolecule: 635 pH2OMolecule->IonizeMolecule(4 - e 636 break; 637 } 638 639 G4double delayedTime = 0.; 640 if(pIncomingTrack->GetUserInformation( 641 { 642 auto pPulseInfo = dynamic_cast<G4V 643 (pIncomingTrack->GetUserInformat 644 if(pPulseInfo != nullptr) 645 { 646 delayedTime = pPulseInfo->GetD 647 } 648 } 649 650 G4Track* pH2OTrack = pH2OMolecule->Bui 651 652 653 pH2OTrack->SetParentID(pIncomingTrack- 654 pH2OTrack->SetTrackStatus(fStopButAliv 655 pH2OTrack->SetKineticEnergy(0.); 656 PushTrack(pH2OTrack); 657 } 658 } 659 660 //-------------------------------------------- 661 // pFinalPosition is optional 662 void G4DNAChemistryManager::CreateSolvatedElec 663 664 { 665 if (fpThreadData->fpPhysChemIO) 666 { 667 fpThreadData->fpPhysChemIO->CreateSolv 668 669 } 670 671 if (fActiveChemistry) 672 { 673 G4double delayedTime = 0.; 674 if(pIncomingTrack->GetUserInformation( 675 { 676 auto pPulseInfo = dynamic_cast<G4V 677 (pIncomingTrack->GetUserInformat 678 if(pPulseInfo != nullptr) 679 { 680 delayedTime = pPulseInfo->GetD 681 } 682 } 683 684 PushMolecule(std::make_unique<G4Molecu 685 picosecond + delayedTime, 686 pFinalPosition != nullptr 687 pIncomingTrack->GetTrackI 688 } 689 } 690 691 //-------------------------------------------- 692 693 void G4DNAChemistryManager::PushMolecule(std:: 694 doubl 695 const 696 int p 697 { 698 assert(fActiveChemistry 699 && "To inject chemical species, the 700 "Check chemistry activation befo 701 G4Track* pTrack = pMolecule->BuildTrack(ti 702 pTrack->SetTrackStatus(fAlive); 703 pTrack->SetParentID(parentID); 704 pMolecule.release(); 705 PushTrack(pTrack); 706 } 707 708 //-------------------------------------------- 709 710 void G4DNAChemistryManager::SetGlobalTemperatu 711 { 712 G4MolecularConfiguration::SetGlobalTempera 713 G4DNAMolecularReactionTable::Instance()->S 714 } 715 716 //-------------------------------------------- 717 // [[deprecated]] : chemistry list should neve 718 void G4DNAChemistryManager::SetChemistryList(G 719 { 720 fpUserChemistryList.reset(pChemistryList); 721 fOwnChemistryList = false; 722 SetChemistryActivation(true); 723 } 724 725 void G4DNAChemistryManager::SetChemistryList(G 726 { 727 fpUserChemistryList.reset(&chemistryList); 728 fOwnChemistryList = false; 729 SetChemistryActivation(true); 730 } 731 732 void G4DNAChemistryManager::SetChemistryList(s 733 { 734 fpUserChemistryList = std::move(pChemistry 735 fOwnChemistryList = true; 736 SetChemistryActivation(true); 737 } 738 739 //-------------------------------------------- 740 741 void G4DNAChemistryManager::Deregister(G4VUser 742 { 743 if (fpUserChemistryList.get() == &chemistr 744 { 745 if (!fpUserChemistryList->IsPhysicsCon 746 { 747 fpUserChemistryList.reset(); 748 } 749 750 fpUserChemistryList.release(); 751 } 752 } 753 754 //-------------------------------------------- 755 756 void G4DNAChemistryManager::PushTrack(G4Track* 757 { 758 G4ITTrackHolder::Instance()->Push(pTrack); 759 } 760 761 //-------------------------------------------- 762 763 void G4DNAChemistryManager::SetVerbose(G4int v 764 { 765 fVerbose = verbose; 766 } 767 768 //-------------------------------------------- 769 770 G4bool G4DNAChemistryManager::IsCounterResetWh 771 { 772 return fResetCounterWhenRunEnds; 773 } 774 775 //-------------------------------------------- 776 777 void G4DNAChemistryManager::ResetCounterWhenRu 778 { 779 fResetCounterWhenRunEnds = resetCounterWhe 780 } 781 782 //-------------------------------------------- 783 784 void G4DNAChemistryManager::ForceRebuildingPhy 785 { 786 fPhysicsTableBuilt = false; 787 } 788 789 //-------------------------------------------- 790 791 void G4DNAChemistryManager::ForceMasterReiniti 792 { 793 fMasterInitialized = false; 794 InitializeMaster(); 795 } 796 797 //-------------------------------------------- 798 799 void G4DNAChemistryManager::ForceThreadReiniti 800 { 801 fForceThreadReinitialization = true; 802 } 803 804 //-------------------------------------------- 805 806 void G4DNAChemistryManager::TagThreadForReinit 807 { 808 fpThreadData->fThreadInitialized = false; 809 } 810