BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
ISimulation.h
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file Core/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 USER_API
16 #ifndef BORNAGAIN_CORE_SIMULATION_ISIMULATION_H
17 #define BORNAGAIN_CORE_SIMULATION_ISIMULATION_H
18 
26 
27 template <class T> class OutputData;
28 class IBackground;
29 class IComputation;
30 class ISampleBuilder;
31 class MultiLayer;
32 
33 //! Abstract base class of OffSpecularSimulation, GISASSimulation and SpecularSimulation.
34 //! Holds the common infrastructure to run a simulation: multithreading, batch processing,
35 //! weighting over parameter distributions, ...
36 //! @ingroup simulation
37 
38 class ISimulation : public ICloneable, public INode {
39 public:
40  ISimulation(const Beam& beam, const MultiLayer& sample, const IDetector& detector);
41  ISimulation();
42  virtual ~ISimulation();
43 
44  virtual ISimulation* clone() const = 0;
45 
46  //! Put into a clean state for running a simulation
47  virtual void prepareSimulation();
48 
49  //! Run a simulation, possibly averaged over parameter distributions
50  void runSimulation();
51 
52  //! Run a simulation in a MPI environment
53  void runMPISimulation();
54 
55  const Instrument& instrument() const { return m_instrument; }
57 
58  Beam& beam() { return m_instrument.beam(); }
59  const Beam& beam() const { return m_instrument.beam(); }
60 
62  const IDetector* getDetector() const { return m_instrument.getDetector(); }
64  const IDetector& detector() const { return m_instrument.detector(); }
65 
66  void setDetectorResolutionFunction(const IResolutionFunction2D& resolution_function);
67 
68  void setSample(const MultiLayer& sample);
69  const MultiLayer* sample() const;
70 
71  void setSampleBuilder(const std::shared_ptr<ISampleBuilder>& sample_builder);
72 
73  void setBackground(const IBackground& bg);
74  const IBackground* background() const { return m_background.get(); }
75 
76  //! Returns the total number of the intensity values in the simulation result
77  virtual size_t intensityMapSize() const = 0;
78 
79  //! Returns the results of the simulation in a format that supports unit conversion and export
80  //! to numpy arrays
81  virtual SimulationResult result() const = 0;
82 
83  void addParameterDistribution(const std::string& param_name,
84  const IDistribution1D& distribution, size_t nbr_samples,
85  double sigma_factor = 0.0,
86  const RealLimits& limits = RealLimits());
87  void addParameterDistribution(const ParameterDistribution& par_distr);
89 
91  const SimulationOptions& getOptions() const { return m_options; }
93 
96 
97  std::vector<const INode*> getChildren() const;
98 
100  bool put_masked_areas_to_zero = true);
101 
102  friend class MPISimulation;
103 
104 #ifndef SWIG
105  ISimulation(const Beam& beam, const IDetector& detector);
106  void setInstrument(const Instrument& instrument_);
107 #endif // SWIG
108 
109 protected:
110  ISimulation(const ISimulation& other);
111 
112  //! Creates the appropriate data structure (e.g. 2D intensity map) from the calculated
113  //! SimulationElement objects
115 
116  //! Initializes the vector of ISimulation elements
117  virtual void initSimulationElementVector() = 0;
118 
119  virtual void updateIntensityMap() {}
120 
121  //! Gets the number of elements this simulation needs to calculate
122  virtual size_t numberOfSimulationElements() const = 0;
123 
124  const SimulationOptions& options() const { return m_options; }
126 
127 private:
128  void initialize();
129 
130  void runSingleSimulation(size_t batch_start, size_t batch_size, double weight = 1.0);
131 
132  //! Generate a single threaded computation for a given range of simulation elements
133  //! @param start Index of the first element to include into computation
134  //! @param n_elements Number of elements to process
135  virtual std::unique_ptr<IComputation> generateSingleThreadedComputation(size_t start,
136  size_t n_elements) = 0;
137 
138  //! Checks the distribution validity for simulation.
139  virtual void validateParametrization(const ParameterDistribution&) const {}
140 
141  virtual void addBackgroundIntensity(size_t start_ind, size_t n_elements) = 0;
142 
143  //! Normalize the detector counts to beam intensity, to solid angle, and to exposure angle.
144  //! @param start_ind Index of the first element to operate on
145  //! @param n_elements Number of elements to process
146  virtual void normalize(size_t start_ind, size_t n_elements) = 0;
147 
148  virtual void addDataToCache(double weight) = 0;
149 
150  virtual void moveDataFromCache() = 0;
151 
152  // used in MPI calculations for transfer of partial results
153  virtual std::vector<double> rawResults() const = 0;
154  virtual void setRawResults(const std::vector<double>& raw_data) = 0;
155 
161  std::unique_ptr<IBackground> m_background;
162 };
163 
164 #endif // BORNAGAIN_CORE_SIMULATION_ISIMULATION_H
165 #endif // USER_API
Defines class DistributionHandler.
Defines interface IDetector2D.
Defines class Instrument.
Defines class ProgressHandler.
Defines class SampleProvider.
Defines class SimulationOptions.
Defines class SimulationResult.
An incident neutron or x-ray beam.
Definition: Beam.h:27
Provides the functionality to average over parameter distributions with weights.
Interface for a simulating the background signal.
Definition: IBackground.h:26
Interface for polymorphic classes that should not be copied, except by explicit cloning.
Definition: ICloneable.h:25
Interface for a single-threaded computation with given range of SimulationElements and ProgressHandle...
Definition: IComputation.h:39
Abstract detector interface.
Definition: IDetector.h:36
Interface for one-dimensional distributions.
Definition: Distributions.h:34
Base class for tree-like structures containing parameterized objects.
Definition: INode.h:49
Interface providing two-dimensional resolution function.
Interface to the class capable to build samples to simulate.
Abstract base class of OffSpecularSimulation, GISASSimulation and SpecularSimulation.
Definition: ISimulation.h:38
const Beam & beam() const
Definition: ISimulation.h:59
void setSample(const MultiLayer &sample)
The MultiLayer object will not be owned by the ISimulation object.
Instrument m_instrument
Definition: ISimulation.h:160
std::vector< const INode * > getChildren() const
Returns a vector of children.
virtual void updateIntensityMap()
Definition: ISimulation.h:119
virtual ~ISimulation()
virtual void validateParametrization(const ParameterDistribution &) const
Checks the distribution validity for simulation.
Definition: ISimulation.h:139
void runSingleSimulation(size_t batch_start, size_t batch_size, double weight=1.0)
Runs a single simulation with fixed parameter values.
SimulationOptions & getOptions()
Definition: ISimulation.h:92
const IDetector & detector() const
Definition: ISimulation.h:64
void initialize()
virtual size_t numberOfSimulationElements() const =0
Gets the number of elements this simulation needs to calculate.
virtual void setRawResults(const std::vector< double > &raw_data)=0
const DistributionHandler & getDistributionHandler() const
Definition: ISimulation.h:88
virtual void addDataToCache(double weight)=0
const IBackground * background() const
Definition: ISimulation.h:74
void setDetectorResolutionFunction(const IResolutionFunction2D &resolution_function)
const MultiLayer * sample() const
void addParameterDistribution(const std::string &param_name, const IDistribution1D &distribution, size_t nbr_samples, double sigma_factor=0.0, const RealLimits &limits=RealLimits())
virtual std::unique_ptr< IComputation > generateSingleThreadedComputation(size_t start, size_t n_elements)=0
Generate a single threaded computation for a given range of simulation elements.
SampleProvider m_sample_provider
Definition: ISimulation.h:158
Instrument & instrument()
Definition: ISimulation.h:56
SimulationResult convertData(const OutputData< double > &data, bool put_masked_areas_to_zero=true)
Convert user data to SimulationResult object for later drawing in various axes units.
virtual ISimulation * clone() const =0
void runMPISimulation()
Run a simulation in a MPI environment.
const Instrument & instrument() const
Definition: ISimulation.h:55
void runSimulation()
Run a simulation, possibly averaged over parameter distributions.
SimulationOptions m_options
Definition: ISimulation.h:156
void setSampleBuilder(const std::shared_ptr< ISampleBuilder > &sample_builder)
const IDetector * getDetector() const
Definition: ISimulation.h:62
ProgressHandler & progress()
Definition: ISimulation.h:125
void setInstrument(const Instrument &instrument_)
virtual void moveDataFromCache()=0
virtual void addBackgroundIntensity(size_t start_ind, size_t n_elements)=0
const SimulationOptions & getOptions() const
Definition: ISimulation.h:91
virtual void initSimulationElementVector()=0
Initializes the vector of ISimulation elements.
const SimulationOptions & options() const
Definition: ISimulation.h:124
virtual void prepareSimulation()
Put into a clean state for running a simulation.
virtual SimulationResult result() const =0
Returns the results of the simulation in a format that supports unit conversion and export to numpy a...
std::unique_ptr< IBackground > m_background
Definition: ISimulation.h:161
virtual std::vector< double > rawResults() const =0
void subscribe(ProgressHandler::Callback_t inform)
Definition: ISimulation.h:94
IDetector & detector()
Definition: ISimulation.h:63
void setBackground(const IBackground &bg)
DistributionHandler m_distribution_handler
Definition: ISimulation.h:159
IDetector * getDetector()
Definition: ISimulation.h:61
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.
ProgressHandler m_progress
Definition: ISimulation.h:157
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.
virtual void transferResultsToIntensityMap()
Creates the appropriate data structure (e.g.
Definition: ISimulation.h:114
void setOptions(const SimulationOptions &options)
Definition: ISimulation.h:90
Beam & beam()
Definition: ISimulation.h:58
Assembles beam, detector and their relative positions with respect to the sample.
Definition: Instrument.h:32
IDetector * getDetector()
Definition: Instrument.cpp:96
IDetector & detector()
Definition: Instrument.cpp:107
Beam & beam()
Definition: Instrument.h:43
Our sample model: a stack of layers one below the other.
Definition: MultiLayer.h:41
Templated class to store data of type double or CumulativeValue in multi-dimensional space.
Definition: OutputData.h:32
A parametric distribution function, for use with any model parameter.
Maintains information about progress of a computation.
void subscribe(ProgressHandler::Callback_t callback)
std::function< bool(size_t)> Callback_t
Limits for a real fit parameter.
Definition: RealLimits.h:24
Holds either a Sample, or a SampleBuilderNode (which holds an ISampleBuilder).
Collect the different options for simulation.
Wrapper around OutputData<double> that also provides unit conversions.