Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer 3 // * License and Disclaimer * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/ 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. 9 // * include a list of copyright holders. * 10 // * 10 // * * 11 // * Neither the authors of this software syst 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitatio 16 // * for the full disclaimer and the limitation of liability. * 17 // * 17 // * * 18 // * This code implementation is the result 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboratio 19 // * technical work of the GEANT4 collaboration. * 20 // * By using, copying, modifying or distri 20 // * By using, copying, modifying or distributing the software (or * 21 // * any work based on the software) you ag 21 // * any work based on the software) you agree to acknowledge its * 22 // * use in resulting scientific publicati 22 // * use in resulting scientific publications, and indicate your * 23 // * acceptance of all terms of the Geant4 Sof 23 // * acceptance of all terms of the Geant4 Software license. * 24 // ******************************************* 24 // ******************************************************************** 25 // 25 // 26 // G4GeneralParticleSourceData class implement << 27 // 26 // 28 // Author: Andrew Green, 20.03.2014 << 27 // ------------------------------------------------------------------- 29 // ------------------------------------------- << 28 // >> 29 // GEANT4 Class file >> 30 // >> 31 // >> 32 // File name: G4GeneralParticleSourceData.cc >> 33 // >> 34 // Author: Andrew Green >> 35 // >> 36 // Creation date: 20 Mar 2014 >> 37 // >> 38 // Modifications: >> 39 // 24/03/2014 >> 40 // Fixed a bug whereby there was a data race for ownership of the "currentSource" >> 41 // member data. This has been resolved by returning a pointer to the G4SPS from >> 42 // the source vector. Tested up to 4 threads, and works fine; may need some further >> 43 // locking in the G4SPS code. >> 44 // >> 45 // >> 46 // Class Description: >> 47 // This class uses the singleton pattern to create a single copy of the data >> 48 // needed for the G4GPS class. As yet, only the largest parts have been split >> 49 // off. >> 50 // >> 51 // 30 52 31 #include "G4GeneralParticleSourceData.hh" 53 #include "G4GeneralParticleSourceData.hh" 32 #include "G4Threading.hh" 54 #include "G4Threading.hh" 33 #include "G4AutoLock.hh" 55 #include "G4AutoLock.hh" 34 56 >> 57 >> 58 >> 59 35 namespace 60 namespace 36 { 61 { 37 G4Mutex singMutex = G4MUTEX_INITIALIZER; // << 62 G4Mutex singMutex = G4MUTEX_INITIALIZER; //Protects singleton access 38 } 63 } 39 64 40 G4GeneralParticleSourceData::G4GeneralParticle << 65 //G4GeneralParticleSourceData* G4GeneralParticleSourceData::theInstance = 0; >> 66 >> 67 G4GeneralParticleSourceData::G4GeneralParticleSourceData() : >> 68 multiple_vertex(false) ,flat_sampling(false), >> 69 normalised(false),currentSourceIdx(0) 41 { 70 { 42 G4MUTEXINIT(mutex); << 71 G4MUTEXINIT(mutex); 43 72 44 sourceVector.clear(); << 73 sourceVector.clear(); 45 sourceIntensity.clear(); << 74 sourceIntensity.clear(); 46 sourceProbability.clear(); << 75 sourceProbability.clear(); 47 76 48 currentSource = new G4SingleParticleSource() << 77 currentSource = new G4SingleParticleSource(); 49 sourceVector.push_back(currentSource); << 78 sourceVector.push_back(currentSource); 50 sourceIntensity.push_back(1.); << 79 sourceIntensity.push_back(1.); >> 80 51 } 81 } 52 82 53 G4GeneralParticleSourceData::~G4GeneralParticl 83 G4GeneralParticleSourceData::~G4GeneralParticleSourceData() 54 { 84 { 55 G4MUTEXDESTROY(mutex); 85 G4MUTEXDESTROY(mutex); 56 for (const auto it : sourceVector) << 86 for ( std::vector<G4SingleParticleSource*>::const_iterator it = sourceVector.begin() ; 57 { << 87 it != sourceVector.end() ; ++it ) { delete *it; } 58 delete it; << 59 } << 60 sourceVector.clear(); 88 sourceVector.clear(); 61 } 89 } 62 90 >> 91 63 G4GeneralParticleSourceData* G4GeneralParticle 92 G4GeneralParticleSourceData* G4GeneralParticleSourceData::Instance() 64 { 93 { 65 G4AutoLock lock(&singMutex); << 94 G4AutoLock lock(&singMutex); 66 static G4GeneralParticleSourceData instance; << 95 static G4GeneralParticleSourceData instance; 67 return &instance; << 96 return &instance; 68 } 97 } 69 98 70 void G4GeneralParticleSourceData::IntensityNor 99 void G4GeneralParticleSourceData::IntensityNormalise() 71 { 100 { 72 G4double total = 0.; << 101 G4double total = 0.; 73 std::size_t i = 0 ; << 102 size_t i = 0 ; 74 for (i = 0; i < sourceIntensity.size(); ++i) << 103 for (i = 0; i < sourceIntensity.size(); i++) 75 { << 104 { 76 total += sourceIntensity[i] ; << 105 total += sourceIntensity[i] ; 77 } << 106 } 78 sourceProbability.clear(); << 107 sourceProbability.clear(); 79 std::vector <G4double> sourceNormalizedInten << 108 std::vector <G4double> sourceNormalizedIntensity; 80 sourceNormalizedIntensity.clear(); << 109 sourceNormalizedIntensity.clear(); 81 110 82 sourceNormalizedIntensity.push_back(sourceIn << 111 sourceNormalizedIntensity.push_back(sourceIntensity[0]/total); 83 sourceProbability.push_back(sourceNormalized << 112 sourceProbability.push_back(sourceNormalizedIntensity[0]); 84 113 85 for (i = 1 ; i < sourceIntensity.size(); ++ << 114 for ( i = 1 ; i < sourceIntensity.size(); i++) 86 { << 87 sourceNormalizedIntensity.push_back(source << 88 sourceProbability.push_back(sourceNormaliz << 89 } << 90 << 91 // set source weights here based on sampling << 92 // and intensities << 93 // << 94 for (i = 0 ; i < sourceIntensity.size(); ++ << 95 { << 96 if (!flat_sampling) << 97 { 115 { 98 GetCurrentSource((G4int)i)->GetBiasRndm( << 116 sourceNormalizedIntensity.push_back(sourceIntensity[i]/total); >> 117 sourceProbability.push_back(sourceNormalizedIntensity[i] + sourceProbability[i-1]); 99 } 118 } 100 else << 119 >> 120 // set source weights here based on sampling scheme (analog/flat) and intensities >> 121 for ( i = 0 ; i < sourceIntensity.size(); i++) 101 { 122 { 102 GetCurrentSource((G4int)i)->GetBiasRndm( << 123 if (!flat_sampling) 103 ->SetIntensityWeight(sourceNormalizedInt << 124 { >> 125 this->GetCurrentSource(i)->GetBiasRndm()->SetIntensityWeight(1.); >> 126 } >> 127 else >> 128 { >> 129 this->GetCurrentSource(i)->GetBiasRndm()->SetIntensityWeight(sourceNormalizedIntensity[i]*sourceIntensity.size()); >> 130 } 104 } 131 } 105 } << 132 106 normalised = true; << 133 normalised = true; 107 } 134 } 108 135 109 void G4GeneralParticleSourceData::SetCurrentSo 136 void G4GeneralParticleSourceData::SetCurrentSourceIntensity(G4double intensity) 110 { 137 { 111 sourceIntensity.at(currentSourceIdx) = inten << 138 sourceIntensity.at(currentSourceIdx) = intensity; 112 normalised = false; << 139 normalised = false; 113 } 140 } 114 141 115 void G4GeneralParticleSourceData::AddASource(G 142 void G4GeneralParticleSourceData::AddASource(G4double intensity) 116 { 143 { 117 currentSource = new G4SingleParticleSource() << 144 currentSource = new G4SingleParticleSource(); 118 sourceVector.push_back(currentSource); << 145 sourceVector.push_back(currentSource); 119 sourceIntensity.push_back(intensity); << 146 sourceIntensity.push_back(intensity); 120 currentSourceIdx = G4int(sourceVector.size() << 147 currentSourceIdx = sourceVector.size() - 1; 121 normalised = false; << 148 normalised = false; 122 } 149 } 123 150 124 void G4GeneralParticleSourceData::DeleteASourc 151 void G4GeneralParticleSourceData::DeleteASource(G4int idx) 125 { 152 { 126 delete sourceVector[idx]; << 153 delete sourceVector[idx]; 127 sourceVector.erase(sourceVector.begin() + id << 154 sourceVector.erase(sourceVector.begin() + idx); 128 sourceIntensity.erase(sourceIntensity.begin( << 155 sourceIntensity.erase(sourceIntensity.begin()+idx); 129 normalised = false ; << 156 normalised = false ; 130 if (currentSourceIdx == idx ) << 157 if (currentSourceIdx == idx ) 131 { << 132 if ( GetIntensityVectorSize() > 0 ) << 133 { << 134 currentSource = GetCurrentSource(0); << 135 currentSourceIdx = 0; << 136 } << 137 else << 138 { 158 { 139 currentSource = nullptr; << 159 if ( this->GetIntensityVectorSize() > 0 ) 140 currentSourceIdx = -1; << 160 { >> 161 currentSource = this->GetCurrentSource(0); >> 162 currentSourceIdx = 0; >> 163 } >> 164 else >> 165 { >> 166 currentSource = NULL; >> 167 currentSourceIdx = -1; >> 168 } 141 } 169 } 142 } << 170 143 } 171 } 144 172 145 void G4GeneralParticleSourceData::ClearSources 173 void G4GeneralParticleSourceData::ClearSources() 146 { 174 { 147 currentSourceIdx = -1; << 175 currentSourceIdx = -1; 148 currentSource = nullptr; << 176 currentSource = NULL; 149 for (const auto it : sourceVector) << 177 for ( std::vector<G4SingleParticleSource*>::iterator it = sourceVector.begin(); 150 { << 178 it != sourceVector.end() ; ++it ) { delete *it; } 151 delete it; << 179 sourceVector.clear(); 152 } << 180 sourceIntensity.clear(); 153 sourceVector.clear(); << 181 normalised = false; 154 sourceIntensity.clear(); << 155 normalised = false; << 156 } 182 } 157 183 158 void G4GeneralParticleSourceData::SetVerbosity << 184 void G4GeneralParticleSourceData::SetVerbosityAllSources(G4int vl ) 159 { << 185 { 160 for (const auto it : sourceVector) << 186 for ( std::vector<G4SingleParticleSource*>::iterator it = sourceVector.begin(); 161 { << 187 it != sourceVector.end() ; ++it ) { 162 it->SetVerbosity(vl); << 188 (*it)->SetVerbosity(vl); 163 } << 189 >> 190 } >> 191 164 } 192 } 165 193 166 G4SingleParticleSource* G4GeneralParticleSourc 194 G4SingleParticleSource* G4GeneralParticleSourceData::GetCurrentSource(G4int idx) 167 { 195 { 168 currentSource = sourceVector[idx]; << 196 currentSource = sourceVector[idx]; 169 currentSourceIdx = idx; << 197 currentSourceIdx = idx; 170 return sourceVector[idx]; << 198 return sourceVector[idx]; 171 } 199 } 172 200 173 void G4GeneralParticleSourceData::Lock() 201 void G4GeneralParticleSourceData::Lock() 174 { 202 { 175 G4MUTEXLOCK(&mutex); << 203 G4MUTEXLOCK(&mutex); 176 } 204 } 177 205 178 void G4GeneralParticleSourceData::Unlock() 206 void G4GeneralParticleSourceData::Unlock() 179 { 207 { 180 G4MUTEXUNLOCK(&mutex); << 208 G4MUTEXUNLOCK(&mutex); 181 } 209 } 182 210