BornAgain  1.18.0
Simulate and fit neutron and x-ray scattering at grazing incidence
SimulationOptions.cpp
Go to the documentation of this file.
1 // ************************************************************************** //
2 //
3 // BornAgain: simulate and fit scattering at grazing incidence
4 //
5 //! @file Sample/RT/SimulationOptions.cpp
6 //! @brief Implements class SimulationOptions
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************** //
14 
16 #include <stdexcept>
17 #include <thread>
18 
19 SimulationOptions::SimulationOptions()
20  : m_mc_integration(false), m_include_specular(false), m_use_avg_materials(false), m_mc_points(1)
21 {
22  m_thread_info.n_threads = getHardwareConcurrency();
23 }
24 
25 bool SimulationOptions::isIntegrate() const
26 {
27  return m_mc_integration && m_mc_points > 1;
28 }
29 
30 void SimulationOptions::setMonteCarloIntegration(bool flag, size_t mc_points)
31 {
32  m_mc_integration = flag;
33  m_mc_points = mc_points;
34 }
35 
37 {
38  if (nthreads == 0)
39  m_thread_info.n_threads = getHardwareConcurrency();
40  else if (nthreads > 0)
41  m_thread_info.n_threads = nthreads;
42  else
43  m_thread_info.n_threads = 1;
44 }
45 
46 unsigned SimulationOptions::getNumberOfThreads() const
47 {
48  if (m_thread_info.n_threads < 1)
49  throw std::runtime_error("Error in SimulationOptions::getNumberOfThreads: Number of "
50  "threads must be positive");
51  return m_thread_info.n_threads;
52 }
53 
55 {
56  if (nbatches < 1)
57  throw std::runtime_error("Error in SimulationOptions::setNumberOfBatches: Number of "
58  "batches must be positive");
59  m_thread_info.n_batches = nbatches;
60 }
61 
62 unsigned SimulationOptions::getNumberOfBatches() const
63 {
64  if (m_thread_info.n_batches < 1)
65  throw std::runtime_error("Error in SimulationOptions::getNumberOfBatches: Number of "
66  "batches must be positive");
67  return m_thread_info.n_batches;
68 }
69 
70 unsigned SimulationOptions::getCurrentBatch() const
71 {
72  if (m_thread_info.current_batch >= getNumberOfBatches())
73  throw std::runtime_error(
74  "Error in SimulationOptions::getCurrentBatch: current batch is out of range");
75  return m_thread_info.current_batch;
76 }
77 
78 unsigned SimulationOptions::getHardwareConcurrency() const
79 {
80  return std::thread::hardware_concurrency();
81 }
Defines class SimulationOptions.
void setNumberOfBatches(int nbatches)
Sets number of batches to split.
void setNumberOfThreads(int nthreads)
Sets number of threads to use during the simulation (0 - take the default value from the hardware)
void setMonteCarloIntegration(bool flag=true, size_t mc_points=50)
Enables/disables MonetCarlo integration.