BornAgain  1.19.79
Open-source research software to simulate and fit neutron and x-ray reflectometry and grazing-incidence small-angle scattering
ISimulation.h
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file Sim/Simulation/ISimulation.h
6 //! @brief Defines interface ISimulation.
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 
15 #ifndef BORNAGAIN_SIM_SIMULATION_ISIMULATION_H
16 #define BORNAGAIN_SIM_SIMULATION_ISIMULATION_H
17 
19 #include "Param/Node/INode.h"
20 #include <heinz/Vectors3D.h>
21 
22 class IBackground;
23 class IComputation;
24 class ICoordSystem;
25 class MultiLayer;
26 class ProgressHandler;
27 class Datafield;
28 class SimulationOptions;
29 class SimulationResult;
30 class reSample;
31 
32 //! Abstract base class, holds the infrastructure to run a simulation.
33 //!
34 //! Base class of SpecularSimulation, ISimulation2D and DepthProbeSimulation.
35 //!
36 //! Holds an instrument and sample model. Provides the common infrastructure
37 //! to run a simulation: multithreading, batch processing, averaging over
38 //! parameter distributions, etc.
39 //!
40 //! Simulations are run, and results returned, by the function ISimulation::simulate().
41 
42 class ISimulation : public INode {
43 public:
45  ~ISimulation() override;
46 
47  ISimulation(const ISimulation&) = delete;
48  ISimulation(ISimulation&&) = default;
49 
50  //! Run a simulation, and return the result.
52 
53  void setBackground(const IBackground& bg);
54 
56  const IDistribution1D& distribution, size_t nbr_samples,
57  double sigma_factor = 0.0,
58  const RealLimits& limits = RealLimits());
59  void addParameterDistribution(const ParameterDistribution& par_distr);
60 
61  const SimulationOptions& options() const;
63 
65 
66  //! Force polarized computation even in absence of sample magnetization or external fields
67  virtual bool force_polarized() const = 0;
68 
69 #ifndef SWIG
70  void subscribe(const std::function<bool(size_t)>& inform);
71 
73 
74  const MultiLayer* sample() const;
75 
76  const IBackground* background() const { return m_background.get(); }
77 
78  const std::vector<ParameterDistribution>& getDistributions() const;
79 
80  std::vector<const INode*> nodeChildren() const override;
81 
82  //! Returns the total number of the intensity values in the simulation result
83  virtual size_t intensityMapSize() const = 0;
84 
85  virtual ICoordSystem* createCoordSystem() const = 0;
86 
87 protected:
88  //! Put into a clean state for running a simulation
89  virtual void prepareSimulation() = 0;
90 
91  //! Sets m_result.
93 
94  //! Initializes the vector of ISimulation elements
95  virtual void initElementVector() = 0;
96 
97  virtual void updateIntensityMap() {}
98 
99  //! Gets the number of elements this simulation needs to calculate
100  virtual size_t numberOfElements() const = 0;
101 
103 
105 
106  virtual void initDistributionHandler() {}
107 
108 private:
109  void runSingleSimulation(const reSample& re_sample, size_t batch_start, size_t batch_size,
110  double weight = 1.0);
111 
112  //! Generate a single threaded computation for a given range of simulation elements
113  //! @param re_sample Preprocessed version of our sample
114  //! @param start Index of the first element to include into computation
115  //! @param n_elements Number of elements to process
116  virtual std::unique_ptr<IComputation> createComputation(const reSample& re_sample, size_t start,
117  size_t n_elements) = 0;
118 
119  //! Checks the distribution validity for simulation.
120  virtual void validateParametrization(const ParameterDistribution&) const {}
121 
122  virtual void addBackgroundIntensity(size_t start_ind, size_t n_elements) = 0;
123 
124  //! Normalize the detector counts to beam intensity, to solid angle, and to exposure angle.
125  //! @param start_ind Index of the first element to operate on
126  //! @param n_elements Number of elements to process
127  virtual void normalize(size_t start_ind, size_t n_elements) = 0;
128 
129  virtual void addDataToCache(double weight) = 0;
130 
131  virtual void moveDataFromCache() = 0;
132 
133  std::shared_ptr<MultiLayer> m_sample;
134 
135  std::shared_ptr<SimulationOptions> m_options;
136  std::shared_ptr<ProgressHandler> m_progress;
137  std::shared_ptr<IBackground> m_background;
138 #endif // SWIG
139 };
140 
141 #endif // BORNAGAIN_SIM_SIMULATION_ISIMULATION_H
Defines class DistributionHandler.
Defines interface INode.
Stores radiation power per bin.
Definition: Datafield.h:30
Provides the functionality to average over parameter distributions with weights.
Abstract base class for background noise, to be added to simulated scattering.
Definition: IBackground.h:27
Interface for a single-threaded computation with given range of DiffuseElements and ProgressHandler.
Definition: IComputation.h:37
Interface to provide axis translations to different units for simulation output.
Definition: ICoordSystem.h:40
Interface for one-dimensional distributions.
Definition: Distributions.h:33
Base class for tree-like structures containing parameterized objects.
Definition: INode.h:40
Abstract base class, holds the infrastructure to run a simulation.
Definition: ISimulation.h:42
void addParameterDistribution(ParameterDistribution::WhichParameter whichParameter, const IDistribution1D &distribution, size_t nbr_samples, double sigma_factor=0.0, const RealLimits &limits=RealLimits())
std::shared_ptr< MultiLayer > m_sample
Definition: ISimulation.h:133
virtual ICoordSystem * createCoordSystem() const =0
virtual size_t numberOfElements() const =0
Gets the number of elements this simulation needs to calculate.
virtual void prepareSimulation()=0
Put into a clean state for running a simulation.
virtual void initElementVector()=0
Initializes the vector of ISimulation elements.
virtual void updateIntensityMap()
Definition: ISimulation.h:97
ProgressHandler & progress()
virtual void validateParametrization(const ParameterDistribution &) const
Checks the distribution validity for simulation.
Definition: ISimulation.h:120
virtual void addDataToCache(double weight)=0
const IBackground * background() const
Definition: ISimulation.h:76
virtual SimulationResult pack_result()=0
Sets m_result.
void subscribe(const std::function< bool(size_t)> &inform)
const MultiLayer * sample() const
std::vector< const INode * > nodeChildren() const override
Returns all children.
ISimulation(ISimulation &&)=default
~ISimulation() override
const SimulationOptions & options() const
virtual void moveDataFromCache()=0
virtual void addBackgroundIntensity(size_t start_ind, size_t n_elements)=0
void runSingleSimulation(const reSample &re_sample, size_t batch_start, size_t batch_size, double weight=1.0)
Runs a single simulation with fixed parameter values. If desired, the simulation is run in several th...
const std::vector< ParameterDistribution > & getDistributions() const
virtual std::unique_ptr< IComputation > createComputation(const reSample &re_sample, size_t start, size_t n_elements)=0
Generate a single threaded computation for a given range of simulation elements.
std::shared_ptr< SimulationOptions > m_options
Definition: ISimulation.h:135
std::shared_ptr< ProgressHandler > m_progress
Definition: ISimulation.h:136
virtual bool force_polarized() const =0
Force polarized computation even in absence of sample magnetization or external fields.
ISimulation(const ISimulation &)=delete
ISimulation(const MultiLayer &sample)
SimulationResult simulate()
Run a simulation, and return the result.
std::shared_ptr< IBackground > m_background
Definition: ISimulation.h:137
void setBackground(const IBackground &bg)
virtual void initDistributionHandler()
Definition: ISimulation.h:106
std::string unitOfParameter(ParameterDistribution::WhichParameter which) const
DistributionHandler m_distribution_handler
Definition: ISimulation.h:104
virtual void normalize(size_t start_ind, size_t n_elements)=0
Normalize the detector counts to beam intensity, to solid angle, and to exposure angle.
virtual size_t intensityMapSize() const =0
Returns the total number of the intensity values in the simulation result.
void setTerminalProgressMonitor()
Initializes a progress monitor that prints to stdout.
Our sample model: a stack of layers one below the other.
Definition: MultiLayer.h:43
A parametric distribution function, for use with any model parameter.
Maintains information about progress of a computation.
Limits for a real fit parameter.
Definition: RealLimits.h:24
Collect the different options for simulation.SimulationOptions.
Wrapper around Datafield that also provides unit conversions.
Data structure that contains all the necessary data for scattering calculations.
Definition: ReSample.h:41