GGEMS  1.1
GPU GEant4-based Monte Carlo Simulations
GGEMSSource.cc
Go to the documentation of this file.
1 // ************************************************************************
2 // * This file is part of GGEMS. *
3 // * *
4 // * GGEMS is free software: you can redistribute it and/or modify *
5 // * it under the terms of the GNU General Public License as published by *
6 // * the Free Software Foundation, either version 3 of the License, or *
7 // * (at your option) any later version. *
8 // * *
9 // * GGEMS is distributed in the hope that it will be useful, *
10 // * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 // * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 // * GNU General Public License for more details. *
13 // * *
14 // * You should have received a copy of the GNU General Public License *
15 // * along with GGEMS. If not, see <https://www.gnu.org/licenses/>. *
16 // * *
17 // ************************************************************************
18 
36 
40 
41 GGEMSSource::GGEMSSource(std::string const& source_name)
42 : source_name_(source_name),
43  number_of_particles_(0),
44  number_of_particles_by_device_(nullptr),
45  number_of_particles_in_batch_(nullptr),
46  number_of_batchs_(nullptr),
47  particle_type_(99),
48  tracking_kernel_option_("")
49 {
50  GGcout("GGEMSSource", "GGEMSSource", 3) << "GGEMSSource creating..." << GGendl;
51 
52  // Allocation of geometry transformation
54 
55  // Get the number of activated device
58 
59  // Storing a kernel for each device
61 
62  // Store the source in source manager
64 
65  GGcout("GGEMSSource", "GGEMSSource", 3) << "GGEMSSource created!!!" << GGendl;
66 }
67 
71 
73 {
74  GGcout("GGEMSSource", "~GGEMSSource", 3) << "GGEMSSource erasing..." << GGendl;
75 
78  geometry_transformation_ = nullptr;
79  }
80 
81  if (number_of_batchs_) {
82  delete number_of_batchs_;
83  number_of_batchs_ = nullptr;
84  }
85 
89  }
90 
92  delete[] kernel_get_primaries_;
93  kernel_get_primaries_ = nullptr;
94  }
95 
97  for (GGsize i = 0; i < number_activated_devices_; ++i) {
99  number_of_particles_in_batch_[i] = nullptr;
100  }
103  }
104 
105  GGcout("GGEMSSource", "~GGEMSSource", 3) << "GGEMSSource erased!!!" << GGendl;
106 }
107 
111 
113 {
114  tracking_kernel_option_ = " -DGGEMS_TRACKING";
115 }
116 
120 
121 void GGEMSSource::SetPosition(GGfloat const& pos_x, GGfloat const& pos_y, GGfloat const& pos_z, std::string const& unit)
122 {
123  GGfloat3 translation;
124  translation.x = DistanceUnit(pos_x, unit);
125  translation.y = DistanceUnit(pos_y, unit);
126  translation.z = DistanceUnit(pos_z, unit);
128 }
129 
133 
134 void GGEMSSource::SetNumberOfParticles(GGsize const& number_of_particles)
135 {
136  number_of_particles_ = number_of_particles;
137 }
138 
142 
143 void GGEMSSource::SetSourceParticleType(std::string const& particle_type)
144 {
145  if (particle_type == "gamma") {
147  }
148  else if (particle_type == "e-") {
150  }
151  else if (particle_type == "e+") {
153  }
154  else
155  {
156  GGEMSMisc::ThrowException("GGEMSSourceManager", "SetParticleType", "Unknown particle!!!");
157  }
158 }
159 
163 
164 void GGEMSSource::SetRotation(GGfloat const& rx, GGfloat const& ry, GGfloat const& rz, std::string const& unit)
165 {
166  GGfloat3 rotation;
167  rotation.x = AngleUnit(rx, unit);
168  rotation.y = AngleUnit(ry, unit);
169  rotation.z = AngleUnit(rz, unit);
171 }
172 
176 
178 {
179  GGcout("GGEMSSource", "CheckParameters", 3) << "Checking the mandatory parameters..." << GGendl;
180 
181  // Checking the type of particles
182  if (particle_type_ == 99) {
183  std::ostringstream oss(std::ostringstream::out);
184  oss << "You have to set a particle type for the source:" << std::endl;
185  oss << " - Photon" << std::endl;
186  oss << " - Electron" << std::endl;
187  oss << " - Positron" << std::endl;
188  GGEMSMisc::ThrowException("GGEMSSource", "CheckParameters", oss.str());
189  }
190 
191  // Checking name of the source
192  if (source_name_.empty()) {
193  std::ostringstream oss(std::ostringstream::out);
194  oss << "You have to set a name for the source!!!";
195  GGEMSMisc::ThrowException("GGEMSSource", "CheckParameters", oss.str());
196  }
197 
198  // Checking the number of particles
199  if (number_of_particles_ == 0) {
200  std::ostringstream oss(std::ostringstream::out);
201  oss << "You have to set a number of particles > 0!!!";
202  GGEMSMisc::ThrowException("GGEMSSource", "CheckParameters", oss.str());
203  }
204 
205  // Checking the position of particles
206  GGfloat3 const kPosition = geometry_transformation_->GetPosition();
207  if (kPosition.s[0] == std::numeric_limits<float>::min() || kPosition.s[1] == std::numeric_limits<float>::min() || kPosition.s[2] == std::numeric_limits<float>::min()) {
208  std::ostringstream oss(std::ostringstream::out);
209  oss << "You have to set a position for the source!!!";
210  GGEMSMisc::ThrowException("GGEMSSource", "CheckParameters", oss.str());
211  }
212 
213  // Checking the rotation of particles
214  GGfloat3 const kRotation = geometry_transformation_->GetRotation();
215  if (kRotation.s[0] == std::numeric_limits<float>::min() || kRotation.s[1] == std::numeric_limits<float>::min() || kRotation.s[2] == std::numeric_limits<float>::min()) {
216  std::ostringstream oss(std::ostringstream::out);
217  oss << "You have to set a rotation for the source!!!";
218  GGEMSMisc::ThrowException("GGEMSSource", "CheckParameters", oss.str());
219  }
220 }
221 
225 
227 {
228  GGcout("GGEMSSource", "OrganizeParticlesInBatch", 3) << "Organizing the number of particles in batch..." << GGendl;
229 
230  // Getting OpenCL singleton
232 
233  // Computing number of particles to simulate for each device
235  if (opencl_manager.GetNumberDeviceLoads() == 0) {
236  for (GGsize i = 0; i < number_activated_devices_; ++i) {
238  }
239 
240  // Adding the remaing particles
241  for (GGsize i = 0; i < number_of_particles_ % number_activated_devices_; ++i) {
243  }
244  }
245  else {
246  GGsize tmp_number_of_particles = 0;
247  for (GGsize i = 0; i < number_activated_devices_; ++i) {
248  number_of_particles_by_device_[i] = static_cast<GGsize>(static_cast<GGfloat>(number_of_particles_) * opencl_manager.GetDeviceBalancing(i));
249  tmp_number_of_particles += number_of_particles_by_device_[i];
250  }
251 
252  // Checking number of particle
253  if (tmp_number_of_particles != number_of_particles_) {
254  number_of_particles_by_device_[0] += static_cast<GGsize>(abs(static_cast<GGlong>(number_of_particles_ - tmp_number_of_particles)));
255  }
256  }
257 
258  // Computing number of batch for each device
261  for (GGsize i = 0; i < number_activated_devices_; ++i) {
262  number_of_batchs_[i] = static_cast<GGsize>(std::ceil(static_cast<GGfloat>(number_of_particles_by_device_[i]) / MAXIMUM_PARTICLES));
263 
265 
266  // Computing the number of simulated particles in batch
267  if (number_of_batchs_[i] == 1) {
269  }
270  else {
271  for (GGsize j = 0; j < number_of_batchs_[i]; ++j) {
273  }
274 
275  // Adding the remaing particles
276  for (GGsize j = 0; j < number_of_particles_by_device_[i] % number_of_batchs_[i]; ++j) {
278  }
279  }
280  }
281 }
282 
286 
287 void GGEMSSource::Initialize(bool const& is_tracking)
288 {
289  GGcout("GGEMSSource", "Initialize", 3) << "Initializing the a GGEMS source..." << GGendl;
290 
291  // Checking the parameters of Source
292  CheckParameters();
293 
294  // Organize the particles in batch
296 
297  // Enable tracking
298  if (is_tracking) EnableTracking();
299 
300  GGcout("GGEMSSource", "Initialize", 0) << "Particles arranged in batch OK" << GGendl;
301 }
GGEMSOpenCLManager::GetDeviceBalancing
GGfloat GetDeviceBalancing(GGsize const &thread_index) const
return the device load for a device
Definition: GGEMSOpenCLManager.hh:261
GGEMSSourceManager::GetInstance
static GGEMSSourceManager & GetInstance(void)
Create at first time the Singleton.
Definition: GGEMSSourceManager.hh:67
GGEMSConstants.hh
Different namespaces storing constants useful for GGEMS.
PHOTON
__constant GGchar PHOTON
Definition: GGEMSParticleConstants.hh:43
GGEMSGeometryTransformation
This class handles everything about geometry transformation.
Definition: GGEMSGeometryTransformation.hh:42
GGEMSSource::number_of_batchs_
GGsize * number_of_batchs_
Definition: GGEMSSource.hh:197
GGEMSSource::SetSourceParticleType
void SetSourceParticleType(std::string const &particle_type)
Set the type of the particle: electron, positron or photon.
Definition: GGEMSSource.cc:143
GGEMSSourceManager::Store
void Store(GGEMSSource *source)
storing the source pointer to source manager
Definition: GGEMSSourceManager.cc:90
GGEMSSource::particle_type_
GGchar particle_type_
Definition: GGEMSSource.hh:199
GGEMSGeometryTransformation::GetRotation
GGfloat3 GetRotation(void) const
Return the current rotation.
Definition: GGEMSGeometryTransformation.hh:163
GGEMSOpenCLManager::GetNumberOfActivatedDevice
GGsize GetNumberOfActivatedDevice(void) const
get the number of activated devices
Definition: GGEMSOpenCLManager.hh:167
GGEMSSource::number_activated_devices_
GGsize number_activated_devices_
Definition: GGEMSSource.hh:204
GGEMSSource::OrganizeParticlesInBatch
void OrganizeParticlesInBatch(void)
Organize the particles in batch.
Definition: GGEMSSource.cc:226
GGEMSSource::tracking_kernel_option_
std::string tracking_kernel_option_
Definition: GGEMSSource.hh:200
AngleUnit
T AngleUnit(T const &value, std::string const &unit)
Choose best angle unit.
Definition: GGEMSSystemOfUnits.hh:360
GGEMSSource::EnableTracking
void EnableTracking(void)
Enabling tracking infos during simulation.
Definition: GGEMSSource.cc:112
GGEMSGeometryTransformation::SetTranslation
void SetTranslation(GGfloat const &tx, GGfloat const &ty, GGfloat const &tz)
Set the translation in X, Y and Z.
Definition: GGEMSGeometryTransformation.cc:143
GGEMSSource::SetPosition
void SetPosition(GGfloat const &pos_x, GGfloat const &pos_y, GGfloat const &pos_z, std::string const &unit="mm")
Set the position of the source in the global coordinates.
Definition: GGEMSSource.cc:121
GGlong
#define GGlong
Definition: GGEMSTypes.hh:238
GGEMSSource::kernel_get_primaries_
cl::Kernel ** kernel_get_primaries_
Definition: GGEMSSource.hh:203
GGEMSSource::~GGEMSSource
virtual ~GGEMSSource(void)
GGEMSSource destructor.
Definition: GGEMSSource.cc:72
GGEMSSource::SetNumberOfParticles
void SetNumberOfParticles(GGsize const &number_of_particles)
Set the number of particles to simulate during the simulation.
Definition: GGEMSSource.cc:134
GGsize
#define GGsize
Definition: GGEMSTypes.hh:252
GGEMSGeometryTransformation::SetRotation
void SetRotation(GGfloat const &rx, GGfloat const &ry, GGfloat const &rz)
Set the Rotation in X, Y and Z around global axis.
Definition: GGEMSGeometryTransformation.hh:103
GGEMSPrimaryParticles.hh
Structure storing the primary particle buffers for both OpenCL and GGEMS.
GGEMSRandom.hh
Structure storing the random buffers for both OpenCL and GGEMS.
GGEMSSource::SetRotation
void SetRotation(GGfloat const &rx, GGfloat const &ry, GGfloat const &rz, std::string const &unit="deg")
Set the rotation of the source around global axis.
Definition: GGEMSSource.cc:164
GGEMSSource::source_name_
std::string source_name_
Definition: GGEMSSource.hh:192
GGfloat3
#define GGfloat3
Definition: GGEMSTypes.hh:275
POSITRON
__constant GGchar POSITRON
Definition: GGEMSParticleConstants.hh:45
GGEMSSource::CheckParameters
virtual void CheckParameters(void) const
Check mandatory parameters for a source.
Definition: GGEMSSource.cc:177
GGcout
GGEMSStream GGcout
Definition: GGEMSPrint.cc:34
GGEMSSourceManager.hh
GGEMS class handling the source(s)
GGEMSGeometryTransformation::GetPosition
GGfloat3 GetPosition(void) const
Return the current position.
Definition: GGEMSGeometryTransformation.hh:156
GGendl
#define GGendl
overload C++ std::endl
Definition: GGEMSPrint.hh:60
GGEMSSource::number_of_particles_in_batch_
GGsize ** number_of_particles_in_batch_
Definition: GGEMSSource.hh:196
GGEMSOpenCLManager
Singleton class storing all informations about OpenCL and managing GPU/CPU devices,...
Definition: GGEMSOpenCLManager.hh:54
GGEMSSource::geometry_transformation_
GGEMSGeometryTransformation * geometry_transformation_
Definition: GGEMSSource.hh:201
GGEMSSource::number_of_particles_by_device_
GGsize * number_of_particles_by_device_
Definition: GGEMSSource.hh:194
GGEMSSource::GGEMSSource
GGEMSSource(std::string const &source_name)
GGEMSSource constructor.
Definition: GGEMSSource.cc:41
GGEMSSource::Initialize
virtual void Initialize(bool const &is_tracking=false)
Initialize a GGEMS source.
Definition: GGEMSSource.cc:287
GGEMSOpenCLManager::GetNumberDeviceLoads
GGsize GetNumberDeviceLoads(void) const
return the size of device load vector
Definition: GGEMSOpenCLManager.hh:268
GGEMSGeometryTransformation.hh
Class managing the geometry transformation.
GGEMSSource::number_of_particles_
GGsize number_of_particles_
Definition: GGEMSSource.hh:193
GGEMSMisc::ThrowException
void ThrowException(std::string const &class_name, std::string const &method_name, std::string const &message)
Throw a C++ exception.
Definition: GGEMSTools.cc:61
DistanceUnit
T DistanceUnit(T const &value, std::string const &unit)
Choose best distance unit.
Definition: GGEMSSystemOfUnits.hh:243
GGfloat
#define GGfloat
Definition: GGEMSTypes.hh:273
GGEMSOpenCLManager::GetInstance
static GGEMSOpenCLManager & GetInstance(void)
Create at first time the Singleton.
Definition: GGEMSOpenCLManager.hh:72
ELECTRON
__constant GGchar ELECTRON
Definition: GGEMSParticleConstants.hh:44