22 Simulation2D::Simulation2D() =
default;
24 Simulation2D::~Simulation2D() =
default;
29 m_detector_context = instrument().detector2D().createContext();
39 instrument().detector2D().
addMask(shape, mask_value);
44 instrument().detector2D().
maskAll();
53 :
Simulation(other), m_sim_elements(other.m_sim_elements), m_cache(other.m_cache)
59 if (!m_detector_context)
60 throw std::runtime_error(
"Error in numberOfSimulationElements(): no detector context");
61 return m_detector_context->numberOfSimulationElements();
65 double y_min,
double y_max)
80 ASSERT(start < m_sim_elements.size() && start + n_elements <= m_sim_elements.size());
81 const auto& begin = m_sim_elements.begin() +
static_cast<long>(start);
82 return std::make_unique<DWBAComputation>(*sample(), options(), progress(), begin,
83 begin +
static_cast<long>(n_elements));
88 const double wavelength = beam.getWavelength();
89 const double alpha_i = -beam.getAlpha();
90 const double phi_i = beam.getPhi();
93 const IDetector2D& detector = instrument().detector2D();
97 const size_t N = m_detector_context->numberOfSimulationElements();
99 std::vector<SimulationElement>
result;
101 for (
size_t element_index = 0; element_index < N; ++element_index) {
103 m_detector_context->createPixel(element_index));
106 if (m_detector_context->detectorIndex(element_index) == spec_index)
108 result.emplace_back(std::move(element));
115 const double beam_intensity = getBeamIntensity();
116 if (beam_intensity == 0.0)
118 for (
size_t i = start_ind, stop_point = start_ind + n_elements; i < stop_point; ++i) {
120 double sin_alpha_i = std::abs(std::sin(element.getAlphaI()));
121 if (sin_alpha_i == 0.0)
123 const double solid_angle = element.getSolidAngle();
124 element.setIntensity(element.getIntensity() * beam_intensity * solid_angle / sin_alpha_i);
128 void Simulation2D::addBackgroundIntensity(
size_t start_ind,
size_t n_elements)
132 for (
size_t i = start_ind, stop_point = start_ind + n_elements; i < stop_point; ++i) {
134 element.setIntensity(background()->addBackground(element.getIntensity()));
138 void Simulation2D::addDataToCache(
double weight)
140 if (m_sim_elements.size() != m_cache.size())
141 throw std::runtime_error(
"Error in Simulation2D::addDataToCache(double): cache size"
142 " not the same as element size");
143 for (
unsigned i = 0; i < m_sim_elements.size(); i++)
144 m_cache[i] += m_sim_elements[i].getIntensity() * weight;
147 void Simulation2D::moveDataFromCache()
149 ASSERT(!m_cache.empty());
150 if (!m_cache.empty()) {
151 for (
unsigned i = 0; i < m_sim_elements.size(); i++)
152 m_sim_elements[i].setIntensity(m_cache[i]);
157 std::vector<double> Simulation2D::rawResults()
const
159 std::vector<double>
result;
160 result.resize(m_sim_elements.size());
161 for (
unsigned i = 0; i < m_sim_elements.size(); ++i)
162 result[i] = m_sim_elements[i].getIntensity();
166 void Simulation2D::setRawResults(
const std::vector<double>& raw_data)
169 if (raw_data.size() != m_sim_elements.size())
170 throw std::runtime_error(
"Simulation2D::setRawResults: size of vector passed as "
171 "argument doesn't match number of elements in this simulation");
172 for (
unsigned i = 0; i < raw_data.size(); i++)
173 m_sim_elements[i].setIntensity(raw_data[i]);
Defines class DWBAComputation.
Define DetectorContext class.
Defines class Histogram2D.
Defines interface IBackground.
Defines class Simulation2D.
Defines class SimulationElement.
Beam defined by wavelength, direction and intensity.
Eigen::Matrix2cd getPolarization() const
Returns the polarization density matrix (in spin basis along z-axis)
Eigen::Matrix2cd analyzerOperator() const
Return the polarization density matrix (in spin basis along z-axis)
Abstract 2D detector interface.
void setRegionOfInterest(double xlow, double ylow, double xup, double yup)
Sets rectangular region of interest with lower left and upper right corners defined.
virtual size_t indexOfSpecular(const Beam &beam) const =0
Returns index of pixel that contains the specular wavevector.
void addMask(const IShape2D &shape, bool mask_value=true)
Adds mask of given shape to the stack of detector masks.
void setDetectorParameters(size_t n_x, double x_min, double x_max, size_t n_y, double y_min, double y_max)
Sets detector parameters using angle ranges.
void removeMasks()
Removes all masks from the detector.
void maskAll()
Put the mask for all detector channels (i.e. exclude whole detector from the analysis)
const DetectionProperties & detectionProperties() const
Returns detection properties.
Basic class for all shapes in 2D.
void setDetector(const IDetector &detector)
Sets the detector (axes can be overwritten later)
Pure virtual base class of OffSpecularSimulation and GISASSimulation.
size_t numberOfSimulationElements() const override
Gets the number of elements this simulation needs to calculate.
void setDetector(const IDetector2D &detector)
Sets the detector (axes can be overwritten later)
std::unique_ptr< IComputation > generateSingleThreadedComputation(size_t start, size_t n_elements) override
Generate a single threaded computation for a given range of simulation elements.
void normalize(size_t start_ind, size_t n_elements) override
Normalize the detector counts to beam intensity, to solid angle, and to exposure angle.
void addMask(const IShape2D &shape, bool mask_value=true)
Adds mask of given shape to the stack of detector masks.
void setDetectorParameters(size_t n_phi, double phi_min, double phi_max, size_t n_alpha, double alpha_min, double alpha_max)
Sets spherical detector parameters using angle ranges.
std::vector< SimulationElement > generateSimulationElements(const Beam &beam)
Generate simulation elements for given beam.
void maskAll()
Put the mask for all detector channels (i.e. exclude whole detector from the analysis)
void removeMasks()
removes all masks from the detector
void prepareSimulation() override
Put into a clean state for running a simulation.
void setRegionOfInterest(double xlow, double ylow, double xup, double yup)
Sets rectangular region of interest with lower left and upper right corners defined.
Data stucture containing both input and output of a single detector cell.
void setSpecular(bool is_specular)
Set specularity indication on/off.
void setPolarization(const Eigen::Matrix2cd &polarization)
Sets the polarization density matrix (in spin basis along z-axis)
void setAnalyzerOperator(const Eigen::Matrix2cd &polarization_operator)
Sets the polarization analyzer operator (in spin basis along z-axis)
Pure virtual base class of OffSpecularSimulation, GISASSimulation and SpecularSimulation.
virtual void initSimulationElementVector()=0
Initializes the vector of Simulation elements.
virtual void prepareSimulation()
Put into a clean state for running a simulation.
virtual void transferResultsToIntensityMap()
Creates the appropriate data structure (e.g.
virtual SimulationResult result() const =0
Returns the results of the simulation in a format that supports unit conversion and export to numpy a...