Geant4 Cross Reference |
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 #ifdef USE_INFERENCE 27 # ifndef PAR04INFEERENCESETUP_HH 28 # define PAR04INFEERENCESETUP_HH 29 30 # include "CLHEP/Units/SystemOfUnits.h" // for mm 31 32 # include "G4ThreeVector.hh" // for G4ThreeVector 33 34 # include <G4String.hh> // for G4String 35 # include <G4SystemOfUnits.hh> // for mm 36 # include <G4Types.hh> // for G4int, G4double, G4bool, G4f... 37 # include <memory> // for unique_ptr 38 # include <vector> // for vector 39 class Par04DetectorConstruction; 40 class Par04InferenceInterface; 41 class Par04InferenceMessenger; 42 43 /** 44 * @brief Inference setup. 45 * 46 * Constructs the input vector of size b+c to run the inference, b represents 47 * the size of the latent space (or the encoded space in a Variational 48 * Autoencoder based model), c represents the size of the conditional vector. 49 *The b values of the input vector are randomly sampled from b-dimensional 50 *Gaussian distribution. The c values represent respectively the condition 51 *values of the particle energy, angle and detector geometry. These condition 52 *values are user-specific application. The energy rescaling is used to retrieve 53 *the original energy scale in MeV. Computes the cell position in the detector 54 *of each inferred energy value. 55 * 56 **/ 57 58 class Par04InferenceSetup 59 { 60 public: 61 Par04InferenceSetup(); 62 ~Par04InferenceSetup(); 63 64 /// Geometry setup 65 /// Check if inference should be performed for the particle 66 /// @param[in] aEnergy Particle's energy 67 G4bool IfTrigger(G4double aEnergy); 68 /// Set mesh size. 69 /// @param aSize (x,y,x) size for Carthesian coordinates, or (R, phi, z) for 70 /// cylindrical coordinates. 71 inline void SetMeshSize(const G4ThreeVector& aSize) { fMeshSize = aSize; }; 72 /// Get mesh size. 73 /// @return G4ThreeVector (x,y,x) size for Carthesian coordinates, or (R, phi, 74 /// z) for cylindrical coordinates. 75 inline G4ThreeVector GetMeshSize() const { return fMeshSize; }; 76 /// Set number of mesh cells. 77 /// @param aSize (x,y,x) size for Carthesian coordinates, or (R, phi, z) for 78 /// cylindrical coordinates. 79 inline void SetMeshNumber(const G4ThreeVector& aSize) { fMeshNumber = aSize; }; 80 /// Get number of mesh cells. 81 /// @return G4ThreeVector (x,y,x) size for Carthesian coordinates, or (R, phi, 82 /// z) for cylindrical coordinates. 83 inline G4ThreeVector GetMeshNumber() const { return fMeshNumber; }; 84 /// Set size of the condition vector 85 inline void SetSizeConditionVector(G4int aNumber) { fSizeConditionVector = aNumber; }; 86 /// Get size of the condition vector 87 inline G4int GetSizeConditionVector() const { return fSizeConditionVector; }; 88 /// Set size of the latent space vector 89 inline void SetSizeLatentVector(G4int aNumber) { fSizeLatentVector = aNumber; }; 90 /// Get size of the latent space vector 91 inline G4int GetSizeLatentVector() const { return fSizeLatentVector; }; 92 /// Set path and name of the model 93 inline void SetModelPathName(G4String aName) { fModelPathName = aName; }; 94 /// Get path and name of the model 95 inline G4String GetModelPathName() const { return fModelPathName; }; 96 /// Set profiling flag 97 inline void SetProfileFlag(G4int aNumber) { fProfileFlag = aNumber; }; 98 /// Get profiling flag 99 inline G4int GetProfileFlag() const { return fProfileFlag; }; 100 /// Set optimization flag 101 inline void SetOptimizationFlag(G4int aNumber) { fOptimizationFlag = aNumber; }; 102 /// Get optimization flag 103 inline G4int GetOptimizationFlag() const { return fOptimizationFlag; }; 104 /// Get name of the inference library 105 inline G4String GetInferenceLibrary() const { return fInferenceLibrary; }; 106 /// Set name of the inference library and create a pointer to chosen inference 107 /// interface 108 void SetInferenceLibrary(G4String aName); 109 /// Check settings of the inference library 110 void CheckInferenceLibrary(); 111 /// Set number of Mesh cells in cylindrical coordinates (r, phi, z) 112 inline void SetMeshNbOfCells(G4ThreeVector aNb) { fMeshNumber = aNb; }; 113 /// Set number of Mesh cells in cylindrical coordinates 114 /// @param[in] aIndex index of cylindrical axis (0,1,2) = (r, phi, z) 115 inline void SetMeshNbOfCells(G4int aIndex, G4double aNb) { fMeshNumber[aIndex] = aNb; }; 116 /// Get number of Mesh cells in cylindrical coordinates (r, phi, z) 117 inline G4ThreeVector GetMeshNbOfCells() const { return fMeshNumber; }; 118 /// Set size of Mesh cells in cylindrical coordinates (r, phi, z) 119 inline void SetMeshSizeOfCells(G4ThreeVector aNb) { fMeshSize = aNb; }; 120 /// Set size of Mesh cells in cylindrical coordinates 121 /// @param[in] aIndex index of cylindrical axis (0,1,2) = (r, phi, z) 122 inline void SetMeshSizeOfCells(G4int aIndex, G4double aNb) { fMeshSize[aIndex] = aNb; }; 123 /// Get size of Mesh cells in cylindrical coordinates (r, phi, z) 124 inline G4ThreeVector GetMeshSizeOfCells() const { return fMeshSize; }; 125 /// Setting execution providers flags 126 /// GPU 127 inline void SetCudaFlag(G4int aNumber) { fCudaFlag = aNumber; }; 128 inline G4int GetCudaFlag() const { return fCudaFlag; }; 129 /// Setting execution providers Options 130 /// Cuda 131 inline void SetCudaDeviceId(G4String aNumber) { fCudaDeviceId = aNumber; }; 132 inline G4String GetCudaDeviceId() const { return fCudaDeviceId; }; 133 inline void SetCudaGpuMemLimit(G4String aNumber) { fCudaGpuMemLimit = aNumber; }; 134 inline G4String GetCudaGpuMemLimit() const { return fCudaGpuMemLimit; }; 135 inline void SetCudaArenaExtendedStrategy(G4String aNumber) 136 { 137 fCudaArenaExtendedStrategy = aNumber; 138 }; 139 inline G4String GetCudaArenaExtendedStrategy() const { return fCudaArenaExtendedStrategy; }; 140 inline void SetCudaCudnnConvAlgoSearch(G4String aNumber) 141 { 142 fCudaCudnnConvAlgoSearch = aNumber; 143 }; 144 inline G4String GetCudaCudnnConvAlgoSearch() const { return fCudaCudnnConvAlgoSearch; }; 145 inline void SetCudaDoCopyInDefaultStream(G4String aNumber) 146 { 147 fCudaDoCopyInDefaultStream = aNumber; 148 }; 149 inline G4String GetCudaDoCopyInDefaultStream() const { return fCudaDoCopyInDefaultStream; }; 150 inline void SetCudaCudnnConvUseMaxWorkspace(G4String aNumber) 151 { 152 fCudaCudnnConvUseMaxWorkspace = aNumber; 153 }; 154 inline G4String GetCudaCudnnConvUseMaxWorkspace() const 155 { 156 return fCudaCudnnConvUseMaxWorkspace; 157 }; 158 159 /// Execute inference 160 /// @param[out] aDepositsEnergies of inferred energies deposited in the 161 /// detector 162 /// @param[in] aParticleEnergy Energy of initial particle 163 void GetEnergies(std::vector<G4double>& aEnergies, G4double aParticleEnergy, 164 G4float aInitialAngle); 165 166 /// Calculate positions 167 /// @param[out] aDepositsPositions Vector of positions corresponding to 168 /// energies deposited in the detector 169 /// @param[in] aParticlePosition Initial particle position which is centre of 170 /// transverse plane of the mesh 171 /// and beginning of the mesh in the longitudinal direction 172 /// @param[in] aParticleDirection Initial particle direction for the mesh 173 /// rotation 174 void GetPositions(std::vector<G4ThreeVector>& aDepositsPositions, 175 G4ThreeVector aParticlePosition, G4ThreeVector aParticleDirection); 176 177 private: 178 /// Cell's size: (x,y,x) for Carthesian, and (R, phi, z) for cylindrical 179 /// coordinates Can be changed with UI command `/example/mesh/size <x y z>/<r 180 /// phi z> <unit>`. For cylindrical coordinates phi is ignored and calculated 181 /// from fMeshNumber. 182 G4ThreeVector fMeshSize = G4ThreeVector(2.325 * CLHEP::mm, 1, 3.4 * CLHEP::mm); 183 /// Number of cells: (x,y,x) for Carthesian, and (R, phi, z) for cylindrical 184 /// coordinates. Can be changed with UI command `/example/mesh/number <Nx Ny 185 /// Nz>/<Nr Nphi Nz>` 186 G4ThreeVector fMeshNumber = G4ThreeVector(18, 50, 45); 187 /// Inference interface 188 std::unique_ptr<Par04InferenceInterface> fInferenceInterface; 189 /// Inference messenger 190 Par04InferenceMessenger* fInferenceMessenger; 191 /// Maximum particle energy value (in MeV) in the training range 192 float fMaxEnergy = 1024000.0; 193 /// Maximum particle angle (in degrees) in the training range 194 float fMaxAngle = 90.0; 195 /// Name of the inference library 196 G4String fInferenceLibrary = "ONNX"; 197 /// Size of the latent space vector 198 G4int fSizeLatentVector = 10; 199 /// Size of the condition vector 200 G4int fSizeConditionVector = 4; 201 /// Name of the inference library 202 G4String fModelPathName = "MLModels/Generator.onnx"; 203 /// ONNX specific 204 /// Profiling flag 205 G4bool fProfileFlag = false; 206 /// Optimization flag 207 G4bool fOptimizationFlag = false; 208 /// Optimization file 209 G4String fModelSavePath = "MLModels/Optimized-Generator.onnx"; 210 /// Profiling file 211 G4String fProfilingOutputSavePath = "opt.json"; 212 /// Intra-operation number of threads 213 G4int fIntraOpNumThreads = 1; 214 /// Flags for execution providers 215 /// GPU 216 G4bool fCudaFlag = false; 217 /// Execution Provider Options 218 /// Cuda options 219 G4String fCudaDeviceId = "0"; 220 G4String fCudaGpuMemLimit = "2147483648"; 221 G4String fCudaArenaExtendedStrategy = "kSameAsRequested"; 222 G4String fCudaCudnnConvAlgoSearch = "DEFAULT"; 223 G4String fCudaDoCopyInDefaultStream = "1"; 224 G4String fCudaCudnnConvUseMaxWorkspace = "1"; 225 std::vector<const char*> cuda_keys{ 226 "device_id", 227 "gpu_mem_limit", 228 "arena_extend_strategy", 229 "cudnn_conv_algo_search", 230 "do_copy_in_default_stream", 231 "cudnn_conv_use_max_workspace", 232 }; 233 std::vector<const char*> cuda_values{ 234 fCudaDeviceId.c_str(), 235 fCudaGpuMemLimit.c_str(), 236 fCudaArenaExtendedStrategy.c_str(), 237 fCudaCudnnConvAlgoSearch.c_str(), 238 fCudaDoCopyInDefaultStream.c_str(), 239 fCudaCudnnConvUseMaxWorkspace.c_str(), 240 }; 241 }; 242 243 # endif /* PAR04INFEERENCESETUP_HH */ 244 #endif 245