GGEMS  1.1
GPU GEant4-based Monte Carlo Simulations
GGEMSSystem.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 
33 
37 
38 GGEMSSystem::GGEMSSystem(std::string const& system_name)
39 : GGEMSNavigator(system_name)
40 {
41  GGcout("GGEMSSystem", "GGEMSSystem", 3) << "GGEMSSystem creating..." << GGendl;
42 
45 
49 
53 
54  is_scatter_ = false;
55 
56  GGcout("GGEMSSystem", "GGEMSSystem", 3) << "GGEMSSystem created!!!" << GGendl;
57 }
58 
62 
64 {
65  GGcout("GGEMSSystem", "~GGEMSSystem", 3) << "GGEMSSystem erasing..." << GGendl;
66 
67  GGcout("GGEMSSystem", "~GGEMSSystem", 3) << "GGEMSSystem erased!!!" << GGendl;
68 }
69 
73 
74 void GGEMSSystem::SetNumberOfModules(GGsize const& n_module_x, GGsize const& n_module_y)
75 {
76  number_of_modules_xy_.x_ = n_module_x;
77  number_of_modules_xy_.y_ = n_module_y;
78 }
79 
83 
84 void GGEMSSystem::SetNumberOfDetectionElementsInsideModule(GGsize const& n_detection_element_x, GGsize const& n_detection_element_y, GGsize const& n_detection_element_z)
85 {
89 }
90 
94 
95 void GGEMSSystem::SetSizeOfDetectionElements(GGfloat const& detection_element_x, GGfloat const& detection_element_y, GGfloat const& detection_element_z, std::string const& unit)
96 {
97  size_of_detection_elements_xyz_.x = DistanceUnit(detection_element_x, unit);
98  size_of_detection_elements_xyz_.y = DistanceUnit(detection_element_y, unit);
99  size_of_detection_elements_xyz_.z = DistanceUnit(detection_element_z, unit);
100 }
101 
105 
106 void GGEMSSystem::SetMaterialName(std::string const& material_name)
107 {
108  materials_->AddMaterial(material_name);
109 }
110 
114 
115 void GGEMSSystem::StoreScatter(bool const& is_scatter)
116 {
117  is_scatter_ = is_scatter;
118 }
119 
123 
125 {
126  GGcout("GGEMSSystem", "CheckParameters", 3) << "Checking the mandatory parameters..." << GGendl;
127 
129  std::ostringstream oss(std::ostringstream::out);
130  oss << "In system parameters, number of module in x and y axis (local axis) has to be > 0!!!";
131  GGEMSMisc::ThrowException("GGEMSSystem", "CheckParameters", oss.str());
132  }
133 
135  std::ostringstream oss(std::ostringstream::out);
136  oss << "In system parameters, number of detection elements in x, y and z axis (local axis) has to be > 0!!!";
137  GGEMSMisc::ThrowException("GGEMSSystem", "CheckParameters", oss.str());
138  }
139 
141  std::ostringstream oss(std::ostringstream::out);
142  oss << "In system parameters, size of detection elements (local axis) has to be > 0.0 mm!!!";
143  GGEMSMisc::ThrowException("GGEMSSystem", "CheckParameters", oss.str());
144  }
145 
146  if (materials_->GetNumberOfMaterials() == 0) {
147  std::ostringstream oss(std::ostringstream::out);
148  oss << "In system parameters, a material has to be defined!!!";
149  GGEMSMisc::ThrowException("GGEMSSystem", "CheckParameters", oss.str());
150  }
151 
153 }
154 
158 
160 {
161  GGcout("GGEMSSystem", "SaveResults", 2) << "Saving results in MHD format..." << GGendl;
162 
163  GGsize3 total_dim;
167 
169  GGint* output = new GGint[total_dim.x_*total_dim.y_*total_dim.z_];
170  std::memset(output, 0, total_dim.x_*total_dim.y_*total_dim.z_*sizeof(GGint));
171 
172  GGEMSMHDImage mhdImage;
174  mhdImage.SetDataType("MET_INT");
175  mhdImage.SetDimensions(total_dim);
177 
178  // Getting all the counts from solid from all OpenCL devices
179  for (GGsize i = 0; i < number_activated_devices_; ++i) {
180  for (GGsize jj = 0; jj < number_of_modules_xy_.y_; ++jj) {
181  for (GGsize ii = 0; ii < number_of_modules_xy_.x_; ++ii) {
182  cl::Buffer* histogram = solids_[ii + jj* number_of_modules_xy_.x_]->GetHistogram(i);
183 
185 
186  // Storing data on host
187  for (GGsize jjj = 0; jjj < number_of_detection_elements_inside_module_xyz_.y_; ++jjj) {
188  for (GGsize iii = 0; iii < number_of_detection_elements_inside_module_xyz_.x_; ++iii) {
190  histogram_device[iii + jjj*number_of_detection_elements_inside_module_xyz_.x_];
191  }
192  }
193 
194  opencl_manager.ReleaseDeviceBuffer(histogram, histogram_device, i);
195  }
196  }
197  }
198 
199  mhdImage.Write<GGint>(output);
200 
201  // Cleaning output buffer
202  std::memset(output, 0, total_dim.x_*total_dim.y_*total_dim.z_*sizeof(GGint));
203 
204  // If scatter output if necessary
205  if (is_scatter_) {
206  // From output file add '-scatter' extension
207  std::string scatter_output_filename = output_basename_;
208 
209  // Checking if there is .mhd suffix
210  GGsize found_mhd = output_basename_.find(".mhd");
211 
212  if (found_mhd == std::string::npos) { // "add '-scatter.mhd' at the end of file"
213  scatter_output_filename += "-scatter.mhd";
214  }
215  else { // If suffix found, add '-scatter' between end of filename and suffix
216  scatter_output_filename = scatter_output_filename.substr(0, found_mhd) + "-scatter.mhd";
217  }
218 
219  GGEMSMHDImage mhdImageScatter;
220  mhdImageScatter.SetOutputFileName(scatter_output_filename);
221  mhdImageScatter.SetDataType("MET_INT");
222  mhdImageScatter.SetDimensions(total_dim);
224 
225  // Getting all the counts from solid from all OpenCL devices
226  for (GGsize i = 0; i < number_activated_devices_; ++i) {
227  for (GGsize jj = 0; jj < number_of_modules_xy_.y_; ++jj) {
228  for (GGsize ii = 0; ii < number_of_modules_xy_.x_; ++ii) {
229  cl::Buffer* scatter_histogram = solids_[ii + jj* number_of_modules_xy_.x_]->GetScatterHistogram(i);
230 
231  GGint* scatter_histogram_device = opencl_manager.GetDeviceBuffer<GGint>(scatter_histogram, number_of_detection_elements_inside_module_xyz_.x_*number_of_detection_elements_inside_module_xyz_.y_*sizeof(GGint), i);
232 
233  // Storing data on host
234  for (GGsize jjj = 0; jjj < number_of_detection_elements_inside_module_xyz_.y_; ++jjj) {
235  for (GGsize iii = 0; iii < number_of_detection_elements_inside_module_xyz_.x_; ++iii) {
237  scatter_histogram_device[iii + jjj*number_of_detection_elements_inside_module_xyz_.x_];
238  }
239  }
240 
241  opencl_manager.ReleaseDeviceBuffer(scatter_histogram, scatter_histogram_device, i);
242  }
243  }
244  }
245 
246  mhdImageScatter.Write<GGint>(output);
247  }
248 
249  delete[] output;
250 }
GGEMSMHDImage
I/O class handling MHD file.
Definition: GGEMSMHDImage.hh:47
GGEMSNavigator::solids_
GGEMSSolid ** solids_
Definition: GGEMSNavigator.hh:255
GGsize3_t
Structure storing 3 GGsize values.
Definition: GGEMSTypes.hh:267
GGEMSSystem::SetNumberOfModules
void SetNumberOfModules(GGsize const &n_module_x, GGsize const &n_module_y)
set the number of module in X, Y of local axis of detector
Definition: GGEMSSystem.cc:74
GGEMSSystem::is_scatter_
bool is_scatter_
Definition: GGEMSSystem.hh:153
GGsize2_t::x_
GGsize x_
Definition: GGEMSTypes.hh:259
GGEMSMHDImage::SetDataType
void SetDataType(std::string const &data_type)
set the type of data
Definition: GGEMSMHDImage.cc:111
GGEMSSystem::StoreScatter
void StoreScatter(bool const &is_scatter)
set to true to activate scatter registration
Definition: GGEMSSystem.cc:115
GGsize2_t::y_
GGsize y_
Definition: GGEMSTypes.hh:260
GGEMSSystem::number_of_detection_elements_inside_module_xyz_
GGsize3 number_of_detection_elements_inside_module_xyz_
Definition: GGEMSSystem.hh:151
GGEMSMHDImage::SetElementSizes
void SetElementSizes(GGfloat3 const &element_sizes)
set the size of the elements
Definition: GGEMSMHDImage.cc:102
GGEMSNavigator::output_basename_
std::string output_basename_
Definition: GGEMSNavigator.hh:253
GGEMSNavigator::CheckParameters
virtual void CheckParameters(void) const
Definition: GGEMSNavigator.cc:175
GGEMSOpenCLManager::ReleaseDeviceBuffer
void ReleaseDeviceBuffer(cl::Buffer *const device_ptr, T *host_ptr, GGsize const &thread_index)
Get the device pointer on host to write on it. Mandatory after a GetDeviceBufferWrite ou GetDeviceBuf...
Definition: GGEMSOpenCLManager.hh:495
GGEMSSolid::GetScatterHistogram
cl::Buffer * GetScatterHistogram(GGsize const &thread_index) const
return the point on scatter histogram
Definition: GGEMSSolid.hh:204
GGEMSSystem::SetNumberOfDetectionElementsInsideModule
void SetNumberOfDetectionElementsInsideModule(GGsize const &n_detection_element_x, GGsize const &n_detection_element_y, GGsize const &n_detection_element_z)
set the number of detection elements in X and Y and Z
Definition: GGEMSSystem.cc:84
GGsize
#define GGsize
Definition: GGEMSTypes.hh:252
GGEMSSystem::CheckParameters
virtual void CheckParameters(void) const
Definition: GGEMSSystem.cc:124
GGEMSMHDImage::Write
void Write(cl::Buffer *image, GGsize const &thread_index) const
Write mhd header/raw file.
Definition: GGEMSMHDImage.cc:223
GGint
#define GGint
Definition: GGEMSTypes.hh:224
GGEMSMHDImage::SetOutputFileName
void SetOutputFileName(std::string const &basename)
set the output filename (*.mhd)
Definition: GGEMSMHDImage.cc:77
GGsize3_t::z_
GGsize z_
Definition: GGEMSTypes.hh:270
GGEMSNavigator::number_activated_devices_
GGsize number_activated_devices_
Definition: GGEMSNavigator.hh:264
GGEMSSystem::GGEMSSystem
GGEMSSystem(std::string const &system_name)
GGEMSSystem constructor.
Definition: GGEMSSystem.cc:38
GGEMSMaterials::GetNumberOfMaterials
GGsize GetNumberOfMaterials(void) const
Get the number of materials in the phantom.
Definition: GGEMSMaterials.hh:156
GGEMSSolid.hh
GGEMS class for solid. This class store geometry about phantom or detector.
GGEMSNavigator
Parent GGEMS class for navigator.
Definition: GGEMSNavigator.hh:55
GGEMSSolid::GetHistogram
cl::Buffer * GetHistogram(GGsize const &thread_index) const
return the point on histogram
Definition: GGEMSSolid.hh:196
GGcout
GGEMSStream GGcout
Definition: GGEMSPrint.cc:34
GGEMSNavigator::materials_
GGEMSMaterials * materials_
Definition: GGEMSNavigator.hh:257
GGEMSSystem::SetMaterialName
void SetMaterialName(std::string const &material_name)
set the name of the material
Definition: GGEMSSystem.cc:106
GGEMSMHDImage::SetDimensions
void SetDimensions(GGsize3 const &dimensions)
set the dimensions of the image
Definition: GGEMSMHDImage.cc:120
GGEMSSystem::SetSizeOfDetectionElements
void SetSizeOfDetectionElements(GGfloat const &size_of_detection_element_x, GGfloat const &size_of_detection_element_y, GGfloat const &size_of_detection_element_z, std::string const &unit="mm")
set the detection elements in each direction
Definition: GGEMSSystem.cc:95
GGsize3_t::x_
GGsize x_
Definition: GGEMSTypes.hh:268
GGEMSOpenCLManager::GetDeviceBuffer
T * GetDeviceBuffer(cl::Buffer *device_ptr, GGsize const &size, GGsize const &thread_index)
Get the device pointer on host to write on it. ReleaseDeviceBuffer must be used after this method!...
Definition: GGEMSOpenCLManager.hh:480
GGendl
#define GGendl
overload C++ std::endl
Definition: GGEMSPrint.hh:60
GGEMSOpenCLManager
Singleton class storing all informations about OpenCL and managing GPU/CPU devices,...
Definition: GGEMSOpenCLManager.hh:54
GGEMSSystem::SaveResults
void SaveResults(void)
save all results from solid
Definition: GGEMSSystem.cc:159
GGEMSSystem::size_of_detection_elements_xyz_
GGfloat3 size_of_detection_elements_xyz_
Definition: GGEMSSystem.hh:152
GGEMSSystem::~GGEMSSystem
virtual ~GGEMSSystem(void)
GGEMSSystem destructor.
Definition: GGEMSSystem.cc:63
GGEMSSystem.hh
Child GGEMS class managing detector system in GGEMS.
GGEMSMaterials::AddMaterial
void AddMaterial(std::string const &material_name)
Add a material associated to a phantom.
Definition: GGEMSMaterials.cc:102
GGEMSMHDImage.hh
I/O class handling MHD image file.
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
GGEMSSystem::number_of_modules_xy_
GGsize2 number_of_modules_xy_
Definition: GGEMSSystem.hh:150
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
GGsize3_t::y_
GGsize y_
Definition: GGEMSTypes.hh:269