BornAgain  1.18.0
Simulate and fit neutron and x-ray scattering at grazing incidence
Simulation.h
Go to the documentation of this file.
1 // ************************************************************************** //
2 //
3 // BornAgain: simulate and fit scattering at grazing incidence
4 //
5 //! @file Core/Simulation/Simulation.h
6 //! @brief Defines class Simulation.
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_CORE_SIMULATION_SIMULATION_H
16 #define BORNAGAIN_CORE_SIMULATION_SIMULATION_H
17 
25 
26 template <class T> class OutputData;
27 class IBackground;
28 class IComputation;
29 class ISampleBuilder;
30 class MultiLayer;
31 
32 //! Pure virtual base class of OffSpecularSimulation, GISASSimulation and SpecularSimulation.
33 //! Holds the common infrastructure to run a simulation: multithreading, batch processing,
34 //! weighting over parameter distributions, ...
35 //! @ingroup simulation
36 
37 class Simulation : public ICloneable, public INode
38 {
39 public:
40  Simulation();
41  virtual ~Simulation();
42 
43  virtual Simulation* clone() const = 0;
44 
45  //! Put into a clean state for running a simulation
46  virtual void prepareSimulation();
47 
48  //! Run a simulation, possibly averaged over parameter distributions
49  void runSimulation();
50 
51  //! Run a simulation in a MPI environment
52  void runMPISimulation();
53 
54  void setInstrument(const Instrument& instrument_);
55  const Instrument& instrument() const { return m_instrument; }
57 
58  void setBeamIntensity(double intensity);
59  double getBeamIntensity() const;
60 
61  void setBeamPolarization(const kvector_t bloch_vector);
62 
63  void setDetectorResolutionFunction(const IResolutionFunction2D& resolution_function);
65 
66  void setAnalyzerProperties(const kvector_t direction, double efficiency,
67  double total_transmission);
68 
69  void setSample(const MultiLayer& sample);
70  const MultiLayer* sample() const;
71 
72  void setSampleBuilder(const std::shared_ptr<ISampleBuilder>& sample_builder);
73 
74  void setBackground(const IBackground& bg);
75  const IBackground* background() const { return m_background.get(); }
76 
77  //! Returns the total number of the intensity values in the simulation result
78  virtual size_t intensityMapSize() const = 0;
79 
80  //! Returns the results of the simulation in a format that supports unit conversion and export
81  //! to numpy arrays
82  virtual SimulationResult result() const = 0;
83 
84  void addParameterDistribution(const std::string& param_name,
85  const IDistribution1D& distribution, size_t nbr_samples,
86  double sigma_factor = 0.0,
87  const RealLimits& limits = RealLimits());
88  void addParameterDistribution(const ParameterDistribution& par_distr);
90 
92  const SimulationOptions& getOptions() const { return m_options; }
94 
97 
98  std::vector<const INode*> getChildren() const;
99 
101  bool put_masked_areas_to_zero = true);
102 
103  friend class MPISimulation;
104 
105 protected:
106  Simulation(const Simulation& other);
107 
108  //! Creates the appropriate data structure (e.g. 2D intensity map) from the calculated
109  //! SimulationElement objects
111 
112  //! Initializes the vector of Simulation elements
113  virtual void initSimulationElementVector() = 0;
114 
115  virtual void updateIntensityMap() {}
116 
117  //! Gets the number of elements this simulation needs to calculate
118  virtual size_t numberOfSimulationElements() const = 0;
119 
120  const SimulationOptions& options() const { return m_options; }
122 
123 private:
124  void initialize();
125 
126  void runSingleSimulation(size_t batch_start, size_t batch_size, double weight = 1.0);
127 
128  //! Generate a single threaded computation for a given range of simulation elements
129  //! @param start Index of the first element to include into computation
130  //! @param n_elements Number of elements to process
131  virtual std::unique_ptr<IComputation> generateSingleThreadedComputation(size_t start,
132  size_t n_elements) = 0;
133 
134  //! Checks the distribution validity for simulation.
135  virtual void validateParametrization(const ParameterDistribution&) const {}
136 
137  virtual void addBackgroundIntensity(size_t start_ind, size_t n_elements) = 0;
138 
139  //! Normalize the detector counts to beam intensity, to solid angle, and to exposure angle.
140  //! @param start_ind Index of the first element to operate on
141  //! @param n_elements Number of elements to process
142  virtual void normalize(size_t start_ind, size_t n_elements) = 0;
143 
144  virtual void addDataToCache(double weight) = 0;
145 
146  virtual void moveDataFromCache() = 0;
147 
148  // used in MPI calculations for transfer of partial results
149  virtual std::vector<double> rawResults() const = 0;
150  virtual void setRawResults(const std::vector<double>& raw_data) = 0;
151 
157  std::unique_ptr<IBackground> m_background;
158 };
159 
160 #endif // BORNAGAIN_CORE_SIMULATION_SIMULATION_H
Defines class DistributionHandler.
Defines interface IDetector2D.
Defines class Instrument.
Defines class ProgressHandler.
Defines class SampleProvider.
Defines class SimulationOptions.
Defines class SimulationResult.
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:35
Interface for one-dimensional distributions.
Definition: Distributions.h:33
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.
Assembles beam, detector and their relative positions with respect to the sample.
Definition: Instrument.h:34
Our sample model: a stack of layers one below the other.
Definition: MultiLayer.h:42
Template class to store data of any type in multi-dimensional space.
Definition: OutputData.h:33
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:25
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.
Pure virtual base class of OffSpecularSimulation, GISASSimulation and SpecularSimulation.
Definition: Simulation.h:38
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.
virtual void moveDataFromCache()=0
SimulationOptions m_options
Definition: Simulation.h:152
virtual void validateParametrization(const ParameterDistribution &) const
Checks the distribution validity for simulation.
Definition: Simulation.h:135
const SimulationOptions & getOptions() const
Definition: Simulation.h:92
Instrument m_instrument
Definition: Simulation.h:156
void setBackground(const IBackground &bg)
Definition: Simulation.cpp:257
const Instrument & instrument() const
Definition: Simulation.h:55
virtual Simulation * clone() const =0
virtual void updateIntensityMap()
Definition: Simulation.h:115
void setTerminalProgressMonitor()
Initializes a progress monitor that prints to stdout.
Definition: Simulation.cpp:145
void removeDetectorResolutionFunction()
Definition: Simulation.cpp:161
void runMPISimulation()
Run a simulation in a MPI environment.
Definition: Simulation.cpp:229
ProgressHandler & progress()
Definition: Simulation.h:121
void setSample(const MultiLayer &sample)
The MultiLayer object will not be owned by the Simulation object.
Definition: Simulation.cpp:242
const IBackground * background() const
Definition: Simulation.h:75
virtual void initSimulationElementVector()=0
Initializes the vector of Simulation elements.
void setSampleBuilder(const std::shared_ptr< ISampleBuilder > &sample_builder)
Definition: Simulation.cpp:252
void runSimulation()
Run a simulation, possibly averaged over parameter distributions.
Definition: Simulation.cpp:200
void setAnalyzerProperties(const kvector_t direction, double efficiency, double total_transmission)
Sets the polarization analyzer characteristics of the detector.
Definition: Simulation.cpp:167
std::vector< const INode * > getChildren() const
Returns a vector of children (const).
Definition: Simulation.cpp:263
const DistributionHandler & getDistributionHandler() const
Definition: Simulation.h:89
void initialize()
Definition: Simulation.cpp:138
virtual void prepareSimulation()
Put into a clean state for running a simulation.
Definition: Simulation.cpp:189
void subscribe(ProgressHandler::Callback_t inform)
Definition: Simulation.h:95
std::unique_ptr< IBackground > m_background
Definition: Simulation.h:157
virtual ~Simulation()
virtual size_t intensityMapSize() const =0
Returns the total number of the intensity values in the simulation result.
virtual void transferResultsToIntensityMap()
Creates the appropriate data structure (e.g.
Definition: Simulation.h:110
SampleProvider m_sample_provider
Definition: Simulation.h:154
SimulationOptions & getOptions()
Definition: Simulation.h:93
const MultiLayer * sample() const
Definition: Simulation.cpp:247
Instrument & instrument()
Definition: Simulation.h:56
double getBeamIntensity() const
Definition: Simulation.cpp:178
virtual void addDataToCache(double weight)=0
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.
Definition: Simulation.cpp:317
DistributionHandler m_distribution_handler
Definition: Simulation.h:155
void setOptions(const SimulationOptions &options)
Definition: Simulation.h:91
virtual std::vector< double > rawResults() const =0
virtual void addBackgroundIntensity(size_t start_ind, size_t n_elements)=0
void setBeamIntensity(double intensity)
Definition: Simulation.cpp:173
virtual SimulationResult result() const =0
Returns the results of the simulation in a format that supports unit conversion and export to numpy a...
void addParameterDistribution(const std::string &param_name, const IDistribution1D &distribution, size_t nbr_samples, double sigma_factor=0.0, const RealLimits &limits=RealLimits())
Definition: Simulation.cpp:273
void runSingleSimulation(size_t batch_start, size_t batch_size, double weight=1.0)
Runs a single simulation with fixed parameter values.
Definition: Simulation.cpp:289
ProgressHandler m_progress
Definition: Simulation.h:153
const SimulationOptions & options() const
Definition: Simulation.h:120
virtual void setRawResults(const std::vector< double > &raw_data)=0
void setBeamPolarization(const kvector_t bloch_vector)
Sets the beam polarization according to the given Bloch vector.
Definition: Simulation.cpp:184
virtual size_t numberOfSimulationElements() const =0
Gets the number of elements this simulation needs to calculate.
void setDetectorResolutionFunction(const IResolutionFunction2D &resolution_function)
Definition: Simulation.cpp:156
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.
void setInstrument(const Instrument &instrument_)
Definition: Simulation.cpp:235