46 std::function<std::string(
double)> printFunc(
const IDetector* detector)
52 throw std::runtime_error(
"SimulationToPython::defineMasks() -> Error. Unknown detector units.");
56 bool isDefaultDirection(
const kvector_t direction)
64 std::ostringstream result;
65 result <<
indent() <<
"footprint = ba." << foot.
name();
72 std::ostringstream result;
74 <<
indent() <<
"resolution = "
82 std::ostringstream result;
84 <<
indent() <<
"# Define specular scan:\n"
91 result <<
indent() <<
"scan.setFootprintFactor(footprint)\n";
94 result << defineScanResolution(*r) <<
"\n";
95 result <<
indent() <<
"scan.setAngleResolution(resolution)\n";
98 result << defineScanResolution(*r) <<
"\n";
99 result <<
indent() <<
"scan.setWavelengthResolution(resolution)\n";
104 std::string defineQSpecScan(
const QSpecScan& scan)
106 std::ostringstream result;
107 const std::string axis_def =
indent() +
"axis = ";
111 result <<
indent() <<
"scan = ba.QSpecScan(axis)";
114 result << defineScanResolution(*scan.
resolution()) <<
"\n";
115 result <<
indent() <<
"scan.setQResolution(resolution)";
123 return defineAngularSpecScan(*s);
124 if (
const auto* s =
dynamic_cast<const QSpecScan*
>(scan); s)
125 return defineQSpecScan(*s);
129 std::string defineDetector(
const ISimulation* simulation)
133 throw std::runtime_error(
"defineDetector: "
134 "detector must be two-dimensional for GISAS");
135 std::ostringstream result;
136 result << std::setprecision(12);
139 ASSERT(det->dimension() == 2);
140 result <<
indent() <<
"detector = ba.SphericalDetector(";
154 result <<
indent() <<
"detector = ba.RectangularDetector(" << det->getNbinsX() <<
", "
158 result <<
indent() <<
"detector.setPosition("
161 if (!isDefaultDirection(det->getDirectionVector()))
165 result <<
indent() <<
"detector.setPerpendicularToSampleX("
169 }
else if (det->getDetectorArrangment()
171 result <<
indent() <<
"detector.setPerpendicularToDirectBeam("
175 }
else if (det->getDetectorArrangment()
177 result <<
indent() <<
"detector.setPerpendicularToReflectedBeam("
181 }
else if (det->getDetectorArrangment()
183 result <<
indent() <<
"detector.setPerpendicularToReflectedBeam("
185 result <<
indent() <<
"detector.setDirectBeamPosition("
189 throw std::runtime_error(
"defineDetector() -> Error. Unknown alignment.");
191 throw std::runtime_error(
"defineDetector() -> Error. Unknown detector");
193 result <<
indent() <<
"detector.setRegionOfInterest("
202 std::string defineDetectorResolutionFunction(
const ISimulation* simulation)
204 std::ostringstream result;
210 convfunc->getResolutionFunction2D())) {
211 result <<
indent() <<
"simulation.setDetectorResolutionFunction(";
212 result <<
"ba.ResolutionFunction2DGaussian(";
213 result << printFunc(detector)(resfunc->getSigmaX()) <<
", ";
214 result << printFunc(detector)(resfunc->getSigmaY()) <<
"))\n";
216 throw std::runtime_error(
"defineDetectorResolutionFunction() -> Error. "
217 "Unknown detector resolution function");
219 throw std::runtime_error(
"defineDetectorResolutionFunction() -> Error. "
220 "Not a ConvolutionDetectorResolution function");
225 std::string defineDetectorPolarizationAnalysis(
const ISimulation* simulation)
227 std::ostringstream result;
231 double analyzer_total_transmission =
234 if (analyzer_direction.
mag() > 0.0) {
235 std::string direction_name =
"analyzer_direction";
236 result <<
indent() << direction_name <<
" = kvector_t("
240 result <<
indent() <<
"simulation.detector().setAnalyzerProperties(" << direction_name
247 std::string defineBeamPolarization(
const Beam& beam)
249 std::ostringstream result;
251 if (bloch_vector.mag() > 0.0) {
252 std::string beam_polarization =
"beam_polarization";
253 result <<
indent() << beam_polarization <<
" = kvector_t("
257 result <<
indent() <<
"beam.setPolarization(" << beam_polarization <<
")\n";
262 std::string defineBeamIntensity(
const Beam& beam)
264 std::ostringstream result;
265 double beam_intensity = beam.
intensity();
266 if (beam_intensity > 0.0)
267 result <<
indent() <<
"simulation.beam().setIntensity("
274 std::ostringstream result;
275 const Beam& beam = simulation.
beam();
282 result << defineBeamPolarization(beam);
289 std::ostringstream result;
290 const Beam& beam = simulation.
beam();
292 const std::string axidef =
indent() +
"alpha_i_axis = ";
299 result << defineBeamPolarization(beam);
300 result << defineBeamIntensity(beam);
306 std::ostringstream result;
309 throw std::runtime_error(
"Error defineSpecularScan: passed simulation "
310 "does not contain any scan");
311 result << defineScan(scan) <<
"\n";
312 result <<
indent() <<
"simulation.setScan(scan)\n";
313 result << defineBeamIntensity(simulation.
beam()) <<
"\n";
317 std::string defineParameterDistributions(
const ISimulation* simulation)
319 std::ostringstream result;
320 const std::vector<ParameterDistribution>& distributions =
322 if (distributions.empty())
324 for (
size_t i = 0; i < distributions.size(); ++i) {
325 std::string main_par_name = distributions[i].getMainParameterName();
329 size_t nbr_samples = distributions[i].getNbrSamples();
330 double sigma_factor = distributions[i].getSigmaFactor();
332 std::string distr =
"distr_" + std::to_string(i + 1);
333 result <<
indent() << distr <<
" = "
337 result <<
indent() <<
"simulation.addParameterDistribution(\"" << main_par_name <<
"\", "
344 std::string defineMasks(
const ISimulation* simulation)
346 std::ostringstream result;
347 result << std::setprecision(12);
353 for (
size_t i_mask = 0; i_mask < detectorMask->
numberOfMasks(); ++i_mask) {
354 bool mask_value(
false);
363 std::string defineSimulationOptions(
const ISimulation* simulation)
365 std::ostringstream result;
366 result << std::setprecision(12);
370 result <<
indent() <<
"simulation.getOptions().setNumberOfThreads("
373 result <<
indent() <<
"simulation.getOptions().setMonteCarloIntegration(True, "
376 result <<
indent() <<
"simulation.getOptions().setUseAvgMaterials(True)\n";
378 result <<
indent() <<
"simulation.getOptions().setIncludeSpecular(True)\n";
382 std::string defineBackground(
const ISimulation* simulation)
384 std::ostringstream result;
388 if (constant_bg->backgroundValue() > 0.0) {
389 result <<
indent() <<
"background = ba.ConstantBackground("
391 result <<
indent() <<
"simulation.setBackground(background)\n";
394 result <<
indent() <<
"background = ba.PoissonNoiseBackground()\n";
395 result <<
indent() <<
"simulation.setBackground(background)\n";
402 std::ostringstream result;
403 result << defineGISASBeam(*simulation);
404 result << defineDetector(simulation);
405 result <<
indent() <<
"simulation = ba.GISASSimulation(beam, sample, detector)\n";
406 result << defineDetectorResolutionFunction(simulation);
407 result << defineDetectorPolarizationAnalysis(simulation);
408 result << defineParameterDistributions(simulation);
409 result << defineMasks(simulation);
410 result << defineSimulationOptions(simulation);
411 result << defineBackground(simulation);
417 std::ostringstream result;
418 result <<
indent() <<
"simulation = ba.OffSpecularSimulation()\n";
419 result << defineDetector(simulation);
420 result << defineDetectorResolutionFunction(simulation);
421 result << defineDetectorPolarizationAnalysis(simulation);
422 result << defineOffSpecularBeam(*simulation);
423 result << defineParameterDistributions(simulation);
424 result << defineMasks(simulation);
425 result << defineSimulationOptions(simulation);
426 result << defineBackground(simulation);
427 result <<
" simulation.setSample(sample)\n";
433 std::ostringstream result;
434 result <<
indent() <<
"simulation = ba.SpecularSimulation()\n";
435 result << defineDetectorPolarizationAnalysis(simulation);
436 result << defineSpecularScan(*simulation);
437 result << defineParameterDistributions(simulation);
438 result << defineSimulationOptions(simulation);
439 result << defineBackground(simulation);
440 result <<
" simulation.setSample(sample)\n";
444 std::string defineSimulate(
const ISimulation* simulation)
446 std::ostringstream result;
447 result <<
"def get_simulation(sample):\n";
449 result << defineGISASSimulation(gisas);
451 result << defineOffSpecularSimulation(offspec);
453 result << defineSpecularSimulation(spec);
456 result <<
" return simulation\n\n\n";
461 std::string simulationCode(
const ISimulation& simulation)
463 if (simulation.
sample() ==
nullptr)
464 throw std::runtime_error(
"Cannot export: Simulation has no sample");
478 return simulationCode(simulation)
479 +
"if __name__ == '__main__':\n"
481 " sample = get_sample()\n"
482 " simulation = get_simulation(sample)\n"
483 " ba_plot.run_and_plot(simulation)\n";
487 const std::string& fname)
489 return simulationCode(simulation)
490 +
"if __name__ == '__main__':\n"
491 " sample = get_sample()\n"
492 " simulation = get_simulation(sample)\n"
493 " simulation.runSimulation()\n"
494 " ba.IntensityDataIOFactory.writeSimulationResult(simulation.result(), \""
Defines and implements namespace algo with some algorithms.
Declares AngularSpecScan class.
#define ASSERT(condition)
Defines class ConstantBackground.
Defines class ConvolutionDetectorResolution.
Defines namespace DetectorUtils.
Defines class GISASSimulation.
Defines namespace node_progeny.
Defines class OffSpecularSimulation.
Defines namespace ParameterUtils.
Defines class PoissonNoiseBackground.
Defines namespace pyfmt2.
Defines functions in namespace pyfmt.
Defines functions in namespace pyfmt.
Declares QSpecScan class.
Defines classes representing ranged one-dimensional distributions.
Defines class RectangularDetector.
Defines class RegionOfInterest.
Defines class ResolutionFunction2DGaussian.
Defines class SampleToPython.
Defines scan resolution class.
Defines class SimulationToPython.
Defines class SpecularSimulation.
Defines class SphericalDetector.
Scan type with inclination angles as coordinate values and a unique wavelength.
virtual const IFootprintFactor * footprintFactor() const override
Returns IFootprintFactor object pointer.
const ScanResolution * wavelengthResolution() const
const ScanResolution * angleResolution() const
virtual const IAxis * coordinateAxis() const override
Returns coordinate axis assigned to the data holder.
double wavelength() const
double mag() const
Returns magnitude of the vector.
T z() const
Returns z-component in cartesian coordinate system.
T y() const
Returns y-component in cartesian coordinate system.
T x() const
Returns x-component in cartesian coordinate system.
An incident neutron or x-ray beam.
Direction direction() const
double intensity() const
Returns the beam intensity in neutrons/sec.
double wavelength() const
kvector_t getBlochVector() const
Class representing a constant background signal.
Convolutes the intensity in 1 or 2 dimensions with a resolution function.
double analyzerEfficiency() const
will always return positive value
double analyzerTotalTransmission() const
kvector_t analyzerDirection() const
Retrieve the analyzer characteristics.
Collection of detector masks.
const IShape2D * getMaskShape(size_t mask_index, bool &mask_value) const
size_t numberOfMasks() const
const Distributions_t & getDistributions() const
Main class to run a Grazing-Incidence Small-Angle Scattering simulation.
Interface for detector resolution algorithms.
Abstract detector interface.
virtual const DetectorMask * detectorMask() const =0
Returns detector masks container.
size_t dimension() const
Returns actual dimensionality of the detector (number of defined axes)
const IDetectorResolution * detectorResolution() const
Returns a pointer to detector resolution object.
virtual const RegionOfInterest * regionOfInterest() const =0
Returns region of interest if exists.
virtual Axes::Units defaultAxesUnits() const
Return default axes units.
const DetectionProperties & detectionProperties() const
Returns detection properties.
Basic class for all shapes in 2D.
Abstract base class of OffSpecularSimulation, GISASSimulation and SpecularSimulation.
const DistributionHandler & getDistributionHandler() const
const IBackground * background() const
const MultiLayer * sample() const
const SimulationOptions & getOptions() const
IDetector * getDetector()
Abstract base class for all types of specular scans.
Main class to run an off-specular simulation.
const IAxis * beamAxis() const
Returns axis of the beam.
Class representing Poisson noise on top of the scattered intensity.
Scan type with z-components of scattering vector as coordinate values.
virtual const IAxis * coordinateAxis() const override
Returns coordinate axis assigned to the data holder.
const ScanResolution * resolution() const
A flat rectangular detector with axes and resolution function.
@ PERPENDICULAR_TO_DIRECT_BEAM
@ PERPENDICULAR_TO_SAMPLE
@ PERPENDICULAR_TO_REFLECTED_BEAM_DPOS
@ PERPENDICULAR_TO_REFLECTED_BEAM
Simple gaussian two-dimensional resolution function.
Generates Python code snippet from domain (C++) objects representing sample construction.
std::string sampleCode(const MultiLayer &multilayer)
Container for reflectivity resolution data.
virtual double delta() const =0
virtual std::string name() const =0
const IRangedDistribution * distribution() const
Collect the different options for simulation.
size_t getMcPoints() const
bool includeSpecular() const
unsigned getHardwareConcurrency() const
unsigned getNumberOfThreads() const
bool useAvgMaterials() const
std::string simulationSaveCode(const ISimulation &simulation, const std::string &fname)
Returns a Python script that runs a simulation and saves the result to a file.
std::string simulationPlotCode(const ISimulation &simulation)
Returns a Python script that runs a simulation and plots the result.
Main class to run a specular simulation.
const ISpecularScan * dataHandler() const
Returns internal data handler.
A detector with coordinate axes along angles phi and alpha.
bool isQuadratic(const IDetector2D &det)
std::string poolParameterUnits(const IParametricComponent &node, const std::string &parName)
Returns units of main parameter.
bool almostEqual(double a, double b)
Returns true if two doubles agree within machine epsilon.
std::string printAxis(const IAxis *axis, const std::string &unit)
Prints an axis.
std::string printDistribution(const IDistribution1D &par_distr, const std::string &units)
Prints distribution with constructor parameters in given units.
std::string representShape2D(const std::string &indent, const IShape2D *ishape, bool mask_value, std::function< std::string(double)> printValueFunc)
Returns fixed Python code snippet that defines the function "runSimulation".
std::string printRangedDistribution(const IRangedDistribution &distr)
std::string printNm(double input)
std::string printDegrees(double input)
std::string printDouble(double input)
std::string printKvector(const kvector_t value)
std::string printScientificDouble(double input)
std::string printImportedSymbols(const std::string &code)
std::string indent(size_t width)
Returns a string of blanks with given width.
std::string printRealLimitsArg(const RealLimits &limits, const std::string &units)
Prints RealLimits in the form of argument (in the context of ParameterDistribution and similar).