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 10.5 ] [ 10.5.p1 ] [ 10.6 ] [ 10.6.p1 ] [ 10.6.p2 ] [ 10.6.p3 ] [ 10.7 ] [ 10.7.p1 ] [ 10.7.p2 ] [ 10.7.p3 ] [ 10.7.p4 ] [ 11.0 ] [ 11.0.p1 ] [ 11.0.p2 ] [ 11.0.p3, ] [ 11.0.p4 ] [ 11.1 ] [ 11.1.1 ] [ 11.1.2 ] [ 11.1.3 ] [ 11.2 ] [ 11.2.1 ] [ 11.2.2 ] [ 11.3.0 ]

  1 //
  2 // ********************************************************************
  3 // * License and Disclaimer                                           *
  4 // *                                                                  *
  5 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
  6 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
  7 // * conditions of the Geant4 Software License,  included in the file *
  8 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
  9 // * include a list of copyright holders.                             *
 10 // *                                                                  *
 11 // * Neither the authors of this software system, nor their employing *
 12 // * institutes,nor the agencies providing financial support for this *
 13 // * work  make  any representation or  warranty, express or implied, *
 14 // * regarding  this  software system or assume any liability for its *
 15 // * use.  Please see the license in the file  LICENSE  and URL above *
 16 // * for the full disclaimer and the limitation of liability.         *
 17 // *                                                                  *
 18 // * This  code  implementation is the result of  the  scientific and *
 19 // * technical work of the GEANT4 collaboration.                      *
 20 // * By using,  copying,  modifying or  distributing the software (or *
 21 // * any work based  on the software)  you  agree  to acknowledge its *
 22 // * use  in  resulting  scientific  publications,  and indicate your *
 23 // * acceptance of all terms of the Geant4 Software license.          *
 24 // ********************************************************************
 25 //
 26 //  author: Le Tuan Anh, 20/10/2023
 27 /// \file main.cc
 28 /// \brief Main program of the dsbandrepair
 29 
 30 #include "G4UImanager.hh"
 31 #include "G4UIterminal.hh"
 32 #include "G4UItcsh.hh"
 33 #include "G4UIExecutive.hh"
 34 
 35 #include "G4RunManagerFactory.hh"
 36 
 37 #ifdef G4VIS_USE
 38 #include "G4VisExecutive.hh"
 39 #endif
 40 
 41 #include "G4Timer.hh"
 42 #include "G4ExceptionSeverity.hh"
 43 #include "G4DNAChemistryManager.hh"
 44 #include "G4VisExecutive.hh"
 45 #include "G4Filesystem.hh"
 46 
 47 #include "ActionInitialization.hh"
 48 #include "DetectorConstruction.hh"
 49 #include "PhysicsList.hh"
 50 
 51 #include "Analysis.hh"
 52 
 53 #ifdef USE_MPI
 54 #include "G4MPImanager.hh"
 55 #include "G4MPIsession.hh"
 56 #include "G4MPIextraWorker.hh"
 57 #endif
 58 #include <ctime>
 59 
 60 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
 61 
 62 G4String ExtractChemListNameFromMacroFile(G4String);
 63 
 64 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
 65 
 66 int main(int argc,char** argv)
 67 {
 68 #ifdef USE_MPI
 69         G4MPImanager* g4MPI = new G4MPImanager(argc, argv, 0);
 70         g4MPI->SetVerbose(1);
 71         G4MPIsession* session = g4MPI-> GetMPIsession();
 72         G4String prompt = "";
 73         prompt += "G4MPI";
 74         prompt += "(%s)[%/]:";
 75         session-> SetPrompt(prompt);
 76 #else 
 77         G4UIExecutive* ui = nullptr;
 78         if ( argc == 1 ) { ui = new G4UIExecutive(argc, argv); }
 79 #endif // USE_MPI
 80     if (argc < 2) {
 81         G4cerr<<"====>> Wrong input. To run Physgeo, type : ./dsbandrepair macrofile\n"
 82             <<"To run Chem_geo, type : ./dsbandrepair macrofile chem"<<G4endl;
 83         #ifdef USE_MPI
 84             delete g4MPI;
 85         #endif // USE_MPI
 86         return EXIT_SUCCESS;
 87     }
 88     G4String stgstr = "";
 89     G4String macrofileName = argv[1];
 90     if (argc > 2) {
 91         const G4String rmode = argv[2];
 92         if (rmode == "phys") gRunMode = RunningMode::Phys;
 93         else if (rmode == "chem") gRunMode = RunningMode::Chem;
 94         else {
 95             G4cout<<"Undefined Running Mode = "<<rmode<<" ;dsbansrepair will quit now. See you!\n";
 96             #ifdef USE_MPI
 97                 delete g4MPI;
 98             #endif // USE_MPI
 99             return  EXIT_SUCCESS;
100         }
101     } 
102     // Choose the Random engine
103     time_t timeStart;
104     time(&timeStart);
105     unsigned long seed = timeStart;
106 #ifdef USE_MPI
107     // Le Tuan Anh: add rankID to get different seeds for multi parallel processes
108     seed += g4MPI->GetRank(); 
109 #endif // USE_MPI
110     G4cout<<"Initial Seed for random engine: "<<seed<<G4endl;
111     CLHEP::HepRandom::setTheEngine(new CLHEP::MTwistEngine);
112     CLHEP::HepRandom::setTheSeed(seed);
113     G4RunManager* runManager{nullptr};
114     auto analysis = Analysis::GetAnalysis();
115     if (gRunMode == RunningMode::Phys) {
116         stgstr = "physical stage";
117         runManager = G4RunManagerFactory::CreateRunManager(G4RunManagerType::Default);
118 #ifdef G4MULTITHREADED
119         G4int threadNumber= 1;
120         runManager-> SetNumberOfThreads(threadNumber);
121 #endif  // G4MULTITHREADED
122 #ifdef USE_MPI
123         stgstr += " in #rank"+std::to_string(g4MPI->GetRank());
124         if (g4MPI->IsMaster()) analysis->CheckAndCreateNewFolderInPhysStage();
125 #else
126         analysis->CheckAndCreateNewFolderInPhysStage();
127 #endif
128     } else if (gRunMode == RunningMode::Chem) {
129         stgstr = "chemical stage";
130         runManager = G4RunManagerFactory::CreateRunManager(G4RunManagerType::Serial);
131         G4DNAChemistryManager::Instance()->SetChemistryActivation(true);
132         G4Scheduler::Instance();
133     } 
134     
135     DetectorConstruction* detector = new DetectorConstruction(1.,0,false);
136     runManager->SetUserInitialization(detector);
137     PhysicsList* physList = new PhysicsList;
138     ActionInitialization* actionIni = new ActionInitialization();
139 
140     if (gRunMode == RunningMode::Phys) {  
141         runManager->SetUserInitialization(physList);
142         runManager->SetUserInitialization(actionIni);    
143 #ifdef USE_MPI 
144         session-> SessionStart();
145         if (g4MPI->GetRank() == 0 ){
146             analysis->WritePhysGeo();
147         }
148 #else 
149         // Get the pointer to the User Interface manager
150         G4UImanager* UImanager = G4UImanager::GetUIpointer();
151         // Process macro or start UI session
152         if ( ! ui ) {
153             // batch mode
154             G4String command = "/control/execute ";
155             UImanager->ApplyCommand(command+macrofileName);
156         }
157         analysis->WritePhysGeo();
158 #endif // USE_MPI      
159     }
160 
161     
162     if (gRunMode == RunningMode::Chem) {
163         //get the pointer to the User Interface manager
164         G4UImanager* UI = G4UImanager::GetUIpointer();
165 #ifdef USE_MPI
166         session->SessionStart();
167         stgstr += " in #rank"+std::to_string(g4MPI->GetRank());    
168         if (g4MPI->IsMaster()) analysis->CheckAndCreateNewFolderInChemStage();
169 #else
170         G4String command = "/control/execute ";
171         UI->ApplyCommand(command+macrofileName);
172         analysis->CheckAndCreateNewFolderInChemStage();
173 #endif       
174         //------------------------------------------
175         // Prepare input file
176         //------------------------------------------
177         std::string inputFileorFolder = "chem_input";
178         if (argc == 4) inputFileorFolder = argv[3];
179         G4fs::path p{inputFileorFolder};
180         G4String outputFileName = "test";
181         std::vector<G4String> totalNumberofFilesVector, numberOfFilesTobeProcessedVector;
182         if (G4fs::is_directory(p)) {
183             for (const auto& entry : G4fs::directory_iterator(p)) {
184                 if (entry.path().extension() == ".dat") {
185                     totalNumberofFilesVector.push_back(entry.path().string());
186                 }
187             }
188             std::sort(totalNumberofFilesVector.begin(),totalNumberofFilesVector.end());
189 #ifdef USE_MPI
190             G4int numberofRanks = g4MPI->GetActiveSize();
191             size_t filesTobeProcessedSlave = (size_t)(
192                 std::floor(G4double(totalNumberofFilesVector.size())/G4double(numberofRanks)));
193             // note: should not use "std::ceil"
194             size_t filesTobeProcessedMaster = 
195                 totalNumberofFilesVector.size() - (numberofRanks-1)*filesTobeProcessedSlave;
196             if (g4MPI->IsMaster()) {
197                 for (size_t ii=0; ii< filesTobeProcessedMaster; ii++) {
198                     numberOfFilesTobeProcessedVector.push_back(totalNumberofFilesVector.at(ii));
199                 }
200             } else {
201                 for (size_t ii=0; ii< filesTobeProcessedSlave; ii++) {
202                     auto rankID = g4MPI->GetRank();
203                     size_t kk = filesTobeProcessedMaster + (rankID-1)*filesTobeProcessedSlave + ii;
204                     numberOfFilesTobeProcessedVector.push_back(totalNumberofFilesVector.at(kk));
205                 }
206             }
207             G4cout<<"-----> "<<numberOfFilesTobeProcessedVector.size()
208                     <<" files will be processed on rank #"<<g4MPI->GetRank()<<G4endl;
209 #else 
210             numberOfFilesTobeProcessedVector = totalNumberofFilesVector;
211 #endif
212             if (totalNumberofFilesVector.size() == 0) {
213                 G4cout<<"===>> There is no files found in "<<inputFileorFolder
214                 <<". You have to run Phys_geo first!!!"<<G4endl;
215                 #ifdef USE_MPI
216                 delete g4MPI;
217                 #endif // USE_MPI
218                 delete runManager;
219                 return EXIT_SUCCESS;
220             } else {
221                 G4cout<<"===>> Total files found in "<<inputFileorFolder
222                 <<" : "<<totalNumberofFilesVector.size()<<G4endl;
223             }
224         } else if (G4fs::is_regular_file(p)) {
225             numberOfFilesTobeProcessedVector.push_back(inputFileorFolder);
226             if (p.has_stem()) {
227                 outputFileName = p.stem().string();
228             } else outputFileName = inputFileorFolder;
229         } 
230         else G4cout<<"===>>dsbandrepair: "<<p.string()<<" is Not Directory or file !!!"<<G4endl;
231         G4String firstFileForInit="";
232         if (numberOfFilesTobeProcessedVector.size()>0) {
233             firstFileForInit=numberOfFilesTobeProcessedVector.at(0);
234             detector->ParseGeoFileForChemMode(firstFileForInit); // read to build voxel
235         }
236         //------------------------------------------
237         // Initialization classes
238         //------------------------------------------
239         
240         runManager->SetUserInitialization(physList);
241         runManager->SetUserInitialization(actionIni);
242         runManager->Initialize();
243         if (numberOfFilesTobeProcessedVector.size()>0) {
244             size_t nprocessedfiles{0}, ncounts{1};
245             if (numberOfFilesTobeProcessedVector.size()>=100) ncounts=10;
246             if (numberOfFilesTobeProcessedVector.size()>=10000) ncounts=100;
247             if (numberOfFilesTobeProcessedVector.size()>=1000000) ncounts=500;
248             for (auto const &fileInput : numberOfFilesTobeProcessedVector) {
249                 G4fs::path aP{std::string(fileInput)};
250                 if (aP.has_stem()) {
251                     outputFileName = aP.stem().string();
252                 } else outputFileName = fileInput;
253                 analysis->SetFileName(outputFileName);
254                 if (fileInput != firstFileForInit) detector->ParseGeoFileForChemMode(fileInput);
255                 detector->InsertMoleculeInWorld();
256                 UI->ApplyCommand("/run/beamOn 1");
257                 nprocessedfiles++;
258                 if (nprocessedfiles == 1 || 
259                     nprocessedfiles == numberOfFilesTobeProcessedVector.size() ||
260                     0 == (nprocessedfiles % ncounts)) {
261                     G4cout<<"=====> Processed file: "<<nprocessedfiles<<"-th/("
262                           <<numberOfFilesTobeProcessedVector.size()<<" files)"
263 #ifdef USE_MPI
264                           <<" in rank #"<<g4MPI->GetRank()
265 #endif
266                           <<"!!!"<<G4endl;
267                 }
268             }   
269         } else {
270             UI->ApplyCommand("/run/beamOn 1");
271         }
272     } 
273     
274 #ifdef USE_MPI
275     delete g4MPI;
276 #endif // USE_MPI
277     delete runManager;
278 
279     G4cout  <<"----------------------> Finish "<<stgstr<<"!!! Good bye :) <----------------------"<<G4endl;
280     return EXIT_SUCCESS;
281 }
282 
283 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
284