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; }
56  Instrument& instrument() { 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);
64  void removeDetectorResolutionFunction();
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);
89  const DistributionHandler& getDistributionHandler() const { return m_distribution_handler; }
90 
91  void setOptions(const SimulationOptions& options) { m_options = options; }
92  const SimulationOptions& getOptions() const { return m_options; }
93  SimulationOptions& getOptions() { return m_options; }
94 
95  void subscribe(ProgressHandler::Callback_t inform) { m_progress.subscribe(inform); }
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; }
121  ProgressHandler& progress() { return m_progress; }
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 
152  SimulationOptions m_options;
153  ProgressHandler m_progress;
154  SampleProvider m_sample_provider;
155  DistributionHandler m_distribution_handler;
156  Instrument m_instrument;
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.
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
void setTerminalProgressMonitor()
Initializes a progress monitor that prints to stdout.
Definition: Simulation.cpp:145
void runMPISimulation()
Run a simulation in a MPI environment.
Definition: Simulation.cpp:229
void setSample(const MultiLayer &sample)
The MultiLayer object will not be owned by the Simulation object.
Definition: Simulation.cpp:242
virtual void initSimulationElementVector()=0
Initializes the vector of Simulation elements.
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
virtual void prepareSimulation()
Put into a clean state for running a simulation.
Definition: Simulation.cpp:189
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
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
virtual SimulationResult result() const =0
Returns the results of the simulation in a format that supports unit conversion and export to numpy a...
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.