25 #include <gsl/gsl_errno.h>
38 for (
size_t i = 0; i < detector.
dimension(); ++i)
49 size_t result = total_size / n_handlers;
50 return total_size % n_handlers ? ++result : result;
53 size_t getStartIndex(
size_t n_handlers,
size_t current_handler,
size_t n_elements)
55 const size_t handler_size =
getIndexStep(n_elements,
static_cast<size_t>(n_handlers));
56 const size_t start_index = current_handler * handler_size;
57 if (start_index >= n_elements)
64 const size_t handler_size =
getIndexStep(n_elements,
static_cast<size_t>(n_handlers));
65 const size_t start_index = current_handler * handler_size;
66 if (start_index >= n_elements)
68 return std::min(handler_size, n_elements - start_index);
73 ASSERT(!computations.empty());
75 if (computations.size() == 1) {
76 auto& computation = computations.front();
78 if (computation->isCompleted())
80 std::string message = computation->errorMessage();
82 "terminated unexpectedly with following error: "
90 std::vector<std::unique_ptr<std::thread>> threads;
93 for (
auto& comp : computations)
94 threads.emplace_back(
new std::thread([&comp]() { comp->run(); }));
97 for (
auto& thread : threads)
101 std::vector<std::string> failure_messages;
102 for (
auto& comp : computations)
103 if (!comp->isCompleted())
104 failure_messages.push_back(comp->errorMessage());
106 if (failure_messages.empty())
109 "Error in runComputations: "
110 "At least one simulation thread has terminated unexpectedly.\n"
127 :
ICloneable(),
INode(), m_options(other.m_options), m_progress(other.m_progress),
128 m_sample_provider(other.m_sample_provider),
129 m_distribution_handler(other.m_distribution_handler), m_instrument(other.instrument())
148 if (percentage_done < 100)
149 std::cout << std::setprecision(2) <<
"\r... " << percentage_done <<
"%" << std::flush;
151 std::cout <<
"\r... 100%\n";
168 double total_transmission)
193 throw std::runtime_error(
194 "Error in Simulation::prepareSimulation(): non-default materials of"
195 " several different types are used in the sample provided");
196 gsl_set_error_handler_off();
214 const size_t batch_start =
getStartIndex(n_batches, current_batch, total_size);
220 for (
size_t index = 0; index < param_combinations; ++index) {
265 std::vector<const INode*>
result;
275 double sigma_factor,
const RealLimits& limits)
296 std::vector<std::unique_ptr<IComputation>> computations;
298 for (
size_t i_thread = 0; i_thread < n_threads;
300 const size_t thread_start = batch_start +
getStartIndex(n_threads, i_thread, batch_size);
302 if (thread_size == 0)
318 bool put_masked_areas_to_zero)
325 if (roi_data->hasSameDimensions(data)) {
327 if (put_masked_areas_to_zero) {
343 !put_masked_areas_to_zero);
346 throw std::runtime_error(
"FitObject::init_dataset() -> Error. Detector and exp data have "
#define ASSERT(condition)
Defines interface IBackground.
Defines interface IComputation.
Defines pure virtual base class ISampleBuilder.
Defines class MPISimulation.
Defines helper functions for MultiLayer objects.
Defines class MultiLayer.
Defines class ParameterPool.
Defines class Simulation.
Defines a few helper functions.
Declares utilities for unit converters.
void addParameterDistribution(const std::string ¶m_name, const IDistribution1D &distribution, size_t nbr_samples, double sigma_factor=0.0, const RealLimits &limits=RealLimits())
add a sampled parameter distribution
double setParameterValues(ParameterPool *p_parameter_pool, size_t index)
set the parameter values of the simulation object to a specific combination of values,...
void setParameterToMeans(ParameterPool *p_parameter_pool) const
Sets mean distribution values to the parameter pool.
size_t getTotalNumberOfSamples() const
get the total number of parameter value combinations (product of the individual sizes of each paramet...
virtual size_t size() const =0
retrieve the number of bins
Interface for a simulating the background signal.
virtual IBackground * clone() const =0
Interface for polymorphic classes that should not be copied, except by explicit cloning.
Abstract detector interface.
size_t dimension() const
Returns actual dimensionality of the detector (number of defined axes)
const IAxis & getAxis(size_t index) const
void iterate(std::function< void(const_iterator)> func, bool visit_masks=false) const
Interface for one-dimensional distributions.
Base class for tree-like structures containing parameterized objects.
ParameterPool * createParameterTree() const
Creates new parameter pool, with all local parameters and those of its children.
void registerChild(INode *node)
Interface providing two-dimensional resolution function.
Assembles beam, detector and their relative positions with respect to the sample.
void setAnalyzerProperties(const kvector_t direction, double efficiency, double total_transmission)
Sets the polarization analyzer characteristics of the detector.
void setBeamIntensity(double intensity)
double getBeamIntensity() const
void setDetectorResolutionFunction(const IResolutionFunction2D &p_resolution_function)
Sets detector resolution function.
void removeDetectorResolution()
Removes detector resolution function.
void setBeamPolarization(const kvector_t bloch_vector)
Sets the beam's polarization according to the given Bloch vector.
void runSimulation(Simulation *simulation)
Our sample model: a stack of layers one below the other.
std::vector< T > getRawDataVector() const
Returns copy of raw data vector.
size_t getRank() const
Returns number of dimensions.
const IAxis & getAxis(size_t serial_number) const
returns axis with given serial number
A parametric distribution function, for use with any model parameter.
void subscribe(ProgressHandler::Callback_t callback)
void setExpectedNTicks(size_t n)
Limits for a real fit parameter.
void setSample(const MultiLayer &multilayer)
void updateSample()
Generates new sample if sample builder defined.
const MultiLayer * sample() const
Returns current sample.
std::vector< const INode * > getChildren() const override
Returns a vector of children (const).
void setBuilder(const std::shared_ptr< ISampleBuilder > &sample_builder)
An iterator for SimulationArea.
size_t detectorIndex() const
unsigned getNumberOfBatches() const
unsigned getCurrentBatch() const
unsigned getNumberOfThreads() const
Wrapper around OutputData<double> that also provides unit conversions.
Pure virtual base class of OffSpecularSimulation, GISASSimulation and SpecularSimulation.
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
virtual void validateParametrization(const ParameterDistribution &) const
Checks the distribution validity for simulation.
void setBackground(const IBackground &bg)
const Instrument & instrument() const
virtual void updateIntensityMap()
void setTerminalProgressMonitor()
Initializes a progress monitor that prints to stdout.
void removeDetectorResolutionFunction()
void runMPISimulation()
Run a simulation in a MPI environment.
void setSample(const MultiLayer &sample)
The MultiLayer object will not be owned by the Simulation object.
virtual void initSimulationElementVector()=0
Initializes the vector of Simulation elements.
void setSampleBuilder(const std::shared_ptr< ISampleBuilder > &sample_builder)
void runSimulation()
Run a simulation, possibly averaged over parameter distributions.
void setAnalyzerProperties(const kvector_t direction, double efficiency, double total_transmission)
Sets the polarization analyzer characteristics of the detector.
std::vector< const INode * > getChildren() const
Returns a vector of children (const).
virtual void prepareSimulation()
Put into a clean state for running a simulation.
std::unique_ptr< IBackground > m_background
virtual void transferResultsToIntensityMap()
Creates the appropriate data structure (e.g.
SampleProvider m_sample_provider
const MultiLayer * sample() const
double getBeamIntensity() const
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.
DistributionHandler m_distribution_handler
virtual void addBackgroundIntensity(size_t start_ind, size_t n_elements)=0
void setBeamIntensity(double intensity)
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 ¶m_name, const IDistribution1D &distribution, size_t nbr_samples, double sigma_factor=0.0, const RealLimits &limits=RealLimits())
void runSingleSimulation(size_t batch_start, size_t batch_size, double weight=1.0)
Runs a single simulation with fixed parameter values.
ProgressHandler m_progress
void setBeamPolarization(const kvector_t bloch_vector)
Sets the beam polarization according to the given Bloch vector.
virtual size_t numberOfSimulationElements() const =0
Gets the number of elements this simulation needs to calculate.
void setDetectorResolutionFunction(const IResolutionFunction2D &resolution_function)
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_)
bool ContainsCompatibleMaterials(const MultiLayer &multilayer)
Returns true if the multilayer contains non-default materials of one type only.
std::string join(const std::vector< std::string > &joinable, const std::string &joint)
Returns string obtain by joining vector elements.
std::unique_ptr< IUnitConverter > createConverter(const Simulation &simulation)
std::unique_ptr< OutputData< double > > createOutputData(const IUnitConverter &converter, Axes::Units units)
Returns zero-valued output data array in specified units.
size_t getIndexStep(size_t total_size, size_t n_handlers)
void runComputations(std::vector< std::unique_ptr< IComputation >> computations)
size_t getStartIndex(size_t n_handlers, size_t current_handler, size_t n_elements)
size_t getNumberOfElements(size_t n_handlers, size_t current_handler, size_t n_elements)
bool detHasSameDimensions(const IDetector &detector, const OutputData< double > &data)