BornAgain  1.18.0
Simulate and fit neutron and x-ray scattering at grazing incidence
SimulationToPython Class Reference
Collaboration diagram for SimulationToPython:

Public Types

enum  EMainType { RUN_SIMULATION , SAVE_DATA }
 

Public Member Functions

std::string generateSimulationCode (const Simulation &simulation, EMainType mainType)
 

Private Member Functions

std::string definePreamble () const
 
std::string defineGetSimulation (const Simulation *simulation) const
 
std::string defineGISASSimulation (const GISASSimulation *simulation) const
 
std::string defineOffSpecSimulation (const OffSpecSimulation *simulation) const
 
std::string defineSpecularSimulation (const SpecularSimulation *simulation) const
 
std::string defineDetector (const Simulation *simulation) const
 
std::string defineDetectorResolutionFunction (const Simulation *simulation) const
 
std::string defineDetectorPolarizationAnalysis (const Simulation *simulation) const
 
std::string defineGISASBeam (const GISASSimulation &simulation) const
 
std::string defineOffSpecBeam (const OffSpecSimulation &simulation) const
 
std::string defineSpecularScan (const SpecularSimulation &simulation) const
 
std::string defineBeamPolarization (const Beam &beam) const
 
std::string defineBeamIntensity (const Beam &beam) const
 
std::string defineParameterDistributions (const Simulation *simulation) const
 
std::string defineMasks (const Simulation *simulation) const
 
std::string defineSimulationOptions (const Simulation *simulation) const
 
std::string defineBackground (const Simulation *simulation) const
 
std::string defineMain (EMainType mainType=RUN_SIMULATION)
 

Detailed Description

Write a Python script that allows to run the current simulation.

Definition at line 30 of file SimulationToPython.h.

Member Enumeration Documentation

◆ EMainType

Enumerator
RUN_SIMULATION 

main function runs simulation

SAVE_DATA 

main function saves intensity data

Definition at line 33 of file SimulationToPython.h.

33  {
34  RUN_SIMULATION, //!< main function runs simulation
35  SAVE_DATA //!< main function saves intensity data
36  };
@ SAVE_DATA
main function saves intensity data
@ RUN_SIMULATION
main function runs simulation

Member Function Documentation

◆ generateSimulationCode()

std::string SimulationToPython::generateSimulationCode ( const Simulation simulation,
EMainType  mainType 
)

Returns a Python script that sets up a simulation and runs it if invoked as main program.

Definition at line 72 of file SimulationToPython.cpp.

74 {
75  if (simulation.sample() == nullptr)
76  throw std::runtime_error("SimulationToPython::generateSimulationCode() -> Error. "
77  "Simulation is not initialized.");
78 
79  SampleToPython sampleGenerator;
80 
81  return pyfmt::scriptPreamble() + sampleGenerator.generateSampleCode(*simulation.sample())
82  + defineGetSimulation(&simulation) + defineSimulate + defineMain(mainType);
83 }
Generates Python code snippet from domain (C++) objects representing sample construction.
std::string generateSampleCode(const MultiLayer &multilayer)
std::string defineGetSimulation(const Simulation *simulation) const
std::string defineMain(EMainType mainType=RUN_SIMULATION)
const MultiLayer * sample() const
Definition: Simulation.cpp:247
std::string scriptPreamble()
Definition: PyFmt.cpp:24

References defineGetSimulation(), defineMain(), anonymous_namespace{SimulationToPython.cpp}::defineSimulate, SampleToPython::generateSampleCode(), Simulation::sample(), and pyfmt::scriptPreamble().

Referenced by anonymous_namespace{ExportToPython.cpp}::simulationCode().

Here is the call graph for this function:

◆ definePreamble()

std::string SimulationToPython::definePreamble ( ) const
private

◆ defineGetSimulation()

std::string SimulationToPython::defineGetSimulation ( const Simulation simulation) const
private

Definition at line 85 of file SimulationToPython.cpp.

86 {
87  std::ostringstream result;
88  result << "def get_simulation():\n";
89 
90  if (auto gisas = dynamic_cast<const GISASSimulation*>(simulation))
91  result << defineGISASSimulation(gisas);
92  else if (auto offspec = dynamic_cast<const OffSpecSimulation*>(simulation))
93  result << defineOffSpecSimulation(offspec);
94  else if (auto spec = dynamic_cast<const SpecularSimulation*>(simulation))
95  result << defineSpecularSimulation(spec);
96  else
97  throw std::runtime_error("SimulationToPython::defineGetSimulation() -> Error. "
98  "Wrong simulation type");
99 
100  result << pyfmt::indent() << "return simulation\n\n\n";
101  return result.str();
102 }
Main class to run a Grazing-Incidence Small-Angle Scattering simulation.
Main class to run an off-specular simulation.
std::string defineOffSpecSimulation(const OffSpecSimulation *simulation) const
std::string defineSpecularSimulation(const SpecularSimulation *simulation) const
std::string defineGISASSimulation(const GISASSimulation *simulation) const
Main class to run a specular simulation.
std::string indent(size_t width)
Returns a string of blanks with given width.
Definition: PyFmt.cpp:141

References defineGISASSimulation(), defineOffSpecSimulation(), defineSpecularSimulation(), and pyfmt::indent().

Referenced by generateSimulationCode().

Here is the call graph for this function:

◆ defineGISASSimulation()

std::string SimulationToPython::defineGISASSimulation ( const GISASSimulation simulation) const
private

Definition at line 104 of file SimulationToPython.cpp.

105 {
106  std::ostringstream result;
107  result << pyfmt::indent() << "simulation = ba.GISASSimulation()\n";
108  result << defineDetector(simulation);
109  result << defineDetectorResolutionFunction(simulation);
110  result << defineDetectorPolarizationAnalysis(simulation);
111  result << defineGISASBeam(*simulation);
112  result << defineParameterDistributions(simulation);
113  result << defineMasks(simulation);
114  result << defineSimulationOptions(simulation);
115  result << defineBackground(simulation);
116  return result.str();
117 }
std::string defineDetectorPolarizationAnalysis(const Simulation *simulation) const
std::string defineSimulationOptions(const Simulation *simulation) const
std::string defineGISASBeam(const GISASSimulation &simulation) const
std::string defineDetectorResolutionFunction(const Simulation *simulation) const
std::string defineParameterDistributions(const Simulation *simulation) const
std::string defineBackground(const Simulation *simulation) const
std::string defineMasks(const Simulation *simulation) const
std::string defineDetector(const Simulation *simulation) const

References defineBackground(), defineDetector(), defineDetectorPolarizationAnalysis(), defineDetectorResolutionFunction(), defineGISASBeam(), defineMasks(), defineParameterDistributions(), defineSimulationOptions(), and pyfmt::indent().

Referenced by defineGetSimulation().

Here is the call graph for this function:

◆ defineOffSpecSimulation()

std::string SimulationToPython::defineOffSpecSimulation ( const OffSpecSimulation simulation) const
private

Definition at line 119 of file SimulationToPython.cpp.

120 {
121  std::ostringstream result;
122  result << pyfmt::indent() << "simulation = ba.OffSpecSimulation()\n";
123  result << defineDetector(simulation);
124  result << defineDetectorResolutionFunction(simulation);
125  result << defineDetectorPolarizationAnalysis(simulation);
126  result << defineOffSpecBeam(*simulation);
127  result << defineParameterDistributions(simulation);
128  result << defineMasks(simulation);
129  result << defineSimulationOptions(simulation);
130  result << defineBackground(simulation);
131  return result.str();
132 }
std::string defineOffSpecBeam(const OffSpecSimulation &simulation) const

References defineBackground(), defineDetector(), defineDetectorPolarizationAnalysis(), defineDetectorResolutionFunction(), defineMasks(), defineOffSpecBeam(), defineParameterDistributions(), defineSimulationOptions(), and pyfmt::indent().

Referenced by defineGetSimulation().

Here is the call graph for this function:

◆ defineSpecularSimulation()

std::string SimulationToPython::defineSpecularSimulation ( const SpecularSimulation simulation) const
private

Definition at line 134 of file SimulationToPython.cpp.

135 {
136  std::ostringstream result;
137  result << pyfmt::indent() << "simulation = ba.SpecularSimulation()\n";
138  result << defineDetectorPolarizationAnalysis(simulation);
139  result << defineSpecularScan(*simulation);
140  result << defineParameterDistributions(simulation);
141  result << defineSimulationOptions(simulation);
142  result << defineBackground(simulation);
143  return result.str();
144 }
std::string defineSpecularScan(const SpecularSimulation &simulation) const

References defineBackground(), defineDetectorPolarizationAnalysis(), defineParameterDistributions(), defineSimulationOptions(), defineSpecularScan(), and pyfmt::indent().

Referenced by defineGetSimulation().

Here is the call graph for this function:

◆ defineDetector()

std::string SimulationToPython::defineDetector ( const Simulation simulation) const
private

Definition at line 146 of file SimulationToPython.cpp.

147 {
148  const IDetector* const detector = simulation->instrument().getDetector();
149  if (detector->dimension() != 2)
150  throw Exceptions::RuntimeErrorException("SimulationToPython::defineDetector: "
151  "detector must be two-dimensional for GISAS");
152  std::ostringstream result;
153  result << std::setprecision(12);
154 
155  if (const auto* const det = dynamic_cast<const SphericalDetector*>(detector)) {
156  result << pyfmt::indent() << "simulation.setDetectorParameters(";
157  for (size_t index = 0; index < det->dimension(); ++index) {
158  if (index != 0)
159  result << ", ";
160  result << det->getAxis(index).size() << ", "
161  << pyfmt::printDegrees(det->getAxis(index).getMin()) << ", "
162  << pyfmt::printDegrees(det->getAxis(index).getMax());
163  }
164  result << ")\n";
165  } else if (const auto* const det = dynamic_cast<const RectangularDetector*>(detector)) {
166  result << pyfmt::indent() << "\n";
167  result << pyfmt::indent() << "detector = ba.RectangularDetector(" << det->getNbinsX()
168  << ", " << pyfmt::printDouble(det->getWidth()) << ", " << det->getNbinsY() << ", "
169  << pyfmt::printDouble(det->getHeight()) << ")\n";
170  if (det->getDetectorArrangment() == RectangularDetector::GENERIC) {
171  result << pyfmt::indent() << "detector.setPosition("
172  << pyfmt::printKvector(det->getNormalVector()) << ", "
173  << pyfmt::printDouble(det->getU0()) << ", " << pyfmt::printDouble(det->getV0());
174  if (!isDefaultDirection(det->getDirectionVector()))
175  result << ", " << pyfmt::printKvector(det->getDirectionVector());
176  result << ")\n";
177  } else if (det->getDetectorArrangment() == RectangularDetector::PERPENDICULAR_TO_SAMPLE) {
178  result << pyfmt::indent() << "detector.setPerpendicularToSampleX("
179  << pyfmt::printDouble(det->getDistance()) << ", "
180  << pyfmt::printDouble(det->getU0()) << ", " << pyfmt::printDouble(det->getV0())
181  << ")\n";
182  } else if (det->getDetectorArrangment()
184  result << pyfmt::indent() << "detector.setPerpendicularToDirectBeam("
185  << pyfmt::printDouble(det->getDistance()) << ", "
186  << pyfmt::printDouble(det->getU0()) << ", " << pyfmt::printDouble(det->getV0())
187  << ")\n";
188  } else if (det->getDetectorArrangment()
190  result << pyfmt::indent() << "detector.setPerpendicularToReflectedBeam("
191  << pyfmt::printDouble(det->getDistance()) << ", "
192  << pyfmt::printDouble(det->getU0()) << ", " << pyfmt::printDouble(det->getV0())
193  << ")\n";
194  } else if (det->getDetectorArrangment()
196  result << pyfmt::indent() << "detector.setPerpendicularToReflectedBeam("
197  << pyfmt::printDouble(det->getDistance()) << ")\n";
198  result << pyfmt::indent() << "detector.setDirectBeamPosition("
199  << pyfmt::printDouble(det->getDirectBeamU0()) << ", "
200  << pyfmt::printDouble(det->getDirectBeamV0()) << ")\n";
201  } else
203  "SimulationToPython::defineDetector() -> Error. Unknown alignment.");
204 
205  result << pyfmt::indent() << "simulation.setDetector(detector)\n";
206  } else
207  throw Exceptions::RuntimeErrorException("SimulationToPython::defineDetector() -> Error. "
208  "Unknown detector");
209  if (detector->regionOfInterest()) {
210  result << pyfmt::indent() << "simulation.setRegionOfInterest("
211  << printFunc(detector)(detector->regionOfInterest()->getXlow()) << ", "
212  << printFunc(detector)(detector->regionOfInterest()->getYlow()) << ", "
213  << printFunc(detector)(detector->regionOfInterest()->getXup()) << ", "
214  << printFunc(detector)(detector->regionOfInterest()->getYup()) << ")\n";
215  }
216  result << pyfmt::indent() << "\n";
217  return result.str();
218 }
Abstract detector interface.
Definition: IDetector.h:36
size_t dimension() const
Returns actual dimensionality of the detector (number of defined axes)
Definition: IDetector.cpp:44
virtual const RegionOfInterest * regionOfInterest() const =0
Returns region of interest if exists.
const IDetector * getDetector() const
Definition: Instrument.cpp:121
A flat rectangular detector with axes and resolution function.
double getYlow() const
double getYup() const
double getXlow() const
double getXup() const
const Instrument & instrument() const
Definition: Simulation.h:55
A spherical detector with axes and resolution function.
std::function< std::string(double)> printFunc(const IDetector *detector)
Returns a function that converts a coordinate to a Python code snippet with appropiate unit.
bool isDefaultDirection(const kvector_t direction)
returns true if it is (0, -1, 0) vector
std::string printDegrees(double input)
Definition: PyFmt.cpp:94
std::string printDouble(double input)
Definition: PyFmt.cpp:43
std::string printKvector(const kvector_t value)
Definition: PyFmt.cpp:133

References IDetector::dimension(), RectangularDetector::GENERIC, Instrument::getDetector(), RegionOfInterest::getXlow(), RegionOfInterest::getXup(), RegionOfInterest::getYlow(), RegionOfInterest::getYup(), pyfmt::indent(), Simulation::instrument(), anonymous_namespace{SimulationToPython.cpp}::isDefaultDirection(), RectangularDetector::PERPENDICULAR_TO_DIRECT_BEAM, RectangularDetector::PERPENDICULAR_TO_REFLECTED_BEAM, RectangularDetector::PERPENDICULAR_TO_REFLECTED_BEAM_DPOS, RectangularDetector::PERPENDICULAR_TO_SAMPLE, pyfmt::printDegrees(), pyfmt::printDouble(), anonymous_namespace{SimulationToPython.cpp}::printFunc(), pyfmt::printKvector(), and IDetector::regionOfInterest().

Referenced by defineGISASSimulation(), and defineOffSpecSimulation().

Here is the call graph for this function:

◆ defineDetectorResolutionFunction()

std::string SimulationToPython::defineDetectorResolutionFunction ( const Simulation simulation) const
private

Definition at line 220 of file SimulationToPython.cpp.

221 {
222  std::ostringstream result;
223  const IDetector* detector = simulation->instrument().getDetector();
224 
225  if (const IDetectorResolution* p_resfunc = detector->detectorResolution()) {
226  if (auto* p_convfunc = dynamic_cast<const ConvolutionDetectorResolution*>(p_resfunc)) {
227  if (auto* resfunc = dynamic_cast<const ResolutionFunction2DGaussian*>(
228  p_convfunc->getResolutionFunction2D())) {
229  result << pyfmt::indent() << "simulation.setDetectorResolutionFunction(";
230  result << "ba.ResolutionFunction2DGaussian(";
231  result << printFunc(detector)(resfunc->getSigmaX()) << ", ";
232  result << printFunc(detector)(resfunc->getSigmaY()) << "))\n";
233  } else
235  "SimulationToPython::defineDetectorResolutionFunction() -> Error. "
236  "Unknown detector resolution function");
237  } else
239  "SimulationToPython::defineDetectorResolutionFunction() -> Error. "
240  "Not a ConvolutionDetectorResolution function");
241  }
242  return result.str();
243 }
Convolutes the intensity in 1 or 2 dimensions with a resolution function.
Interface for detector resolution algorithms.
const IDetectorResolution * detectorResolution() const
Returns a pointer to detector resolution object.
Definition: IDetector.cpp:142
Simple gaussian two-dimensional resolution function.

References IDetector::detectorResolution(), Instrument::getDetector(), pyfmt::indent(), Simulation::instrument(), and anonymous_namespace{SimulationToPython.cpp}::printFunc().

Referenced by defineGISASSimulation(), and defineOffSpecSimulation().

Here is the call graph for this function:

◆ defineDetectorPolarizationAnalysis()

std::string SimulationToPython::defineDetectorPolarizationAnalysis ( const Simulation simulation) const
private

Definition at line 246 of file SimulationToPython.cpp.

247 {
248  std::ostringstream result;
249  const IDetector* detector = simulation->instrument().getDetector();
250  kvector_t analyzer_direction = detector->detectionProperties().analyzerDirection();
251  double analyzer_efficiency = detector->detectionProperties().analyzerEfficiency();
252  double analyzer_total_transmission =
254 
255  if (analyzer_direction.mag() > 0.0) {
256  std::string direction_name = "analyzer_direction";
257  result << pyfmt::indent() << direction_name << " = kvector_t("
258  << pyfmt::printDouble(analyzer_direction.x()) << ", "
259  << pyfmt::printDouble(analyzer_direction.y()) << ", "
260  << pyfmt::printDouble(analyzer_direction.z()) << ")\n";
261  result << pyfmt::indent() << "simulation.setAnalyzerProperties(" << direction_name << ", "
262  << pyfmt::printDouble(analyzer_efficiency) << ", "
263  << pyfmt::printDouble(analyzer_total_transmission) << ")\n";
264  }
265  return result.str();
266 }
double mag() const
Returns magnitude of the vector.
T z() const
Returns z-component in cartesian coordinate system.
Definition: BasicVector3D.h:68
T y() const
Returns y-component in cartesian coordinate system.
Definition: BasicVector3D.h:66
T x() const
Returns x-component in cartesian coordinate system.
Definition: BasicVector3D.h:64
double analyzerEfficiency() const
will always return positive value
double analyzerTotalTransmission() const
kvector_t analyzerDirection() const
Retrieve the analyzer characteristics.
const DetectionProperties & detectionProperties() const
Returns detection properties.
Definition: IDetector.h:93

References DetectionProperties::analyzerDirection(), DetectionProperties::analyzerEfficiency(), DetectionProperties::analyzerTotalTransmission(), IDetector::detectionProperties(), Instrument::getDetector(), pyfmt::indent(), Simulation::instrument(), BasicVector3D< T >::mag(), pyfmt::printDouble(), BasicVector3D< T >::x(), BasicVector3D< T >::y(), and BasicVector3D< T >::z().

Referenced by defineGISASSimulation(), defineOffSpecSimulation(), and defineSpecularSimulation().

Here is the call graph for this function:

◆ defineGISASBeam()

std::string SimulationToPython::defineGISASBeam ( const GISASSimulation simulation) const
private

Definition at line 268 of file SimulationToPython.cpp.

269 {
270  std::ostringstream result;
271  const Beam& beam = simulation.instrument().getBeam();
272 
273  result << pyfmt::indent() << "simulation.setBeamParameters("
274  << pyfmt::printNm(beam.getWavelength()) << ", " << pyfmt::printDegrees(beam.getAlpha())
275  << ", " << pyfmt::printDegrees(beam.getPhi()) << ")\n";
276 
277  result << defineBeamPolarization(beam);
278  result << defineBeamIntensity(beam);
279 
280  return result.str();
281 }
Beam defined by wavelength, direction and intensity.
Definition: Beam.h:27
double getWavelength() const
Definition: Beam.h:69
double getAlpha() const
Definition: Beam.h:70
double getPhi() const
Definition: Beam.h:71
Beam & getBeam()
Definition: Instrument.h:44
std::string defineBeamIntensity(const Beam &beam) const
std::string defineBeamPolarization(const Beam &beam) const
std::string printNm(double input)
Definition: PyFmt.cpp:57

References defineBeamIntensity(), defineBeamPolarization(), Beam::getAlpha(), Instrument::getBeam(), Beam::getPhi(), Beam::getWavelength(), pyfmt::indent(), Simulation::instrument(), pyfmt::printDegrees(), and pyfmt::printNm().

Referenced by defineGISASSimulation().

Here is the call graph for this function:

◆ defineOffSpecBeam()

std::string SimulationToPython::defineOffSpecBeam ( const OffSpecSimulation simulation) const
private

Definition at line 283 of file SimulationToPython.cpp.

284 {
285  std::ostringstream result;
286  const Beam& beam = simulation.instrument().getBeam();
287 
288  const std::string axis_def = pyfmt::indent() + "alpha_i_axis = ";
289  result << axis_def << simulation.beamAxis()->pyString("rad", axis_def.size()) << "\n";
290 
291  result << pyfmt::indent() << "simulation.setBeamParameters("
292  << pyfmt::printNm(beam.getWavelength()) << ", "
293  << "alpha_i_axis, " << pyfmt::printDegrees(beam.getPhi()) << ")\n";
294 
295  result << defineBeamPolarization(beam);
296  result << defineBeamIntensity(beam);
297  return result.str();
298 }
virtual std::string pyString(const std::string &units, size_t offset) const =0
const IAxis * beamAxis() const
Returns axis of the beam.

References OffSpecSimulation::beamAxis(), defineBeamIntensity(), defineBeamPolarization(), Instrument::getBeam(), Beam::getPhi(), Beam::getWavelength(), pyfmt::indent(), Simulation::instrument(), pyfmt::printDegrees(), pyfmt::printNm(), and IAxis::pyString().

Referenced by defineOffSpecSimulation().

Here is the call graph for this function:

◆ defineSpecularScan()

std::string SimulationToPython::defineSpecularScan ( const SpecularSimulation simulation) const
private

Definition at line 300 of file SimulationToPython.cpp.

301 {
302  std::ostringstream result;
303  const ISpecularScan* scan = simulation.dataHandler();
304  if (!scan)
305  throw std::runtime_error("Error SimulationToPython::defineSpecularScan: passed simulation "
306  "does not contain any scan");
307  result << *scan << "\n";
308 
309  result << pyfmt::indent() << "simulation.setScan(scan)\n";
310  result << defineBeamIntensity(simulation.instrument().getBeam());
311  result << "\n";
312  return result.str();
313 }
Pure virtual base class for all types of specular scans.
Definition: ISpecularScan.h:31
const ISpecularScan * dataHandler() const
Returns internal data handler.

References SpecularSimulation::dataHandler(), defineBeamIntensity(), Instrument::getBeam(), pyfmt::indent(), and Simulation::instrument().

Referenced by defineSpecularSimulation().

Here is the call graph for this function:

◆ defineBeamPolarization()

std::string SimulationToPython::defineBeamPolarization ( const Beam beam) const
private

Definition at line 315 of file SimulationToPython.cpp.

316 {
317  std::ostringstream result;
318  auto bloch_vector = beam.getBlochVector();
319  if (bloch_vector.mag() > 0.0) {
320  std::string beam_polarization = "beam_polarization";
321  result << pyfmt::indent() << beam_polarization << " = kvector_t("
322  << pyfmt::printDouble(bloch_vector.x()) << ", "
323  << pyfmt::printDouble(bloch_vector.y()) << ", "
324  << pyfmt::printDouble(bloch_vector.z()) << ")\n";
325  result << pyfmt::indent() << "simulation.setBeamPolarization(" << beam_polarization
326  << ")\n";
327  }
328  return result.str();
329 }
kvector_t getBlochVector() const
Definition: Beam.cpp:118

References Beam::getBlochVector(), pyfmt::indent(), and pyfmt::printDouble().

Referenced by defineGISASBeam(), and defineOffSpecBeam().

Here is the call graph for this function:

◆ defineBeamIntensity()

std::string SimulationToPython::defineBeamIntensity ( const Beam beam) const
private

Definition at line 331 of file SimulationToPython.cpp.

332 {
333  std::ostringstream result;
334  double beam_intensity = beam.getIntensity();
335  if (beam_intensity > 0.0)
336  result << pyfmt::indent() << "simulation.setBeamIntensity("
337  << pyfmt::printScientificDouble(beam_intensity) << ")\n";
338  return result.str();
339 }
double getIntensity() const
Returns the beam intensity in neutrons/sec.
Definition: Beam.h:45
std::string printScientificDouble(double input)
Definition: PyFmt.cpp:74

References Beam::getIntensity(), pyfmt::indent(), and pyfmt::printScientificDouble().

Referenced by defineGISASBeam(), defineOffSpecBeam(), and defineSpecularScan().

Here is the call graph for this function:

◆ defineParameterDistributions()

std::string SimulationToPython::defineParameterDistributions ( const Simulation simulation) const
private

Definition at line 341 of file SimulationToPython.cpp.

342 {
343  std::ostringstream result;
344  const std::vector<ParameterDistribution>& distributions =
346  if (distributions.empty())
347  return "";
348  for (size_t i = 0; i < distributions.size(); ++i) {
349  std::string main_par_name = distributions[i].getMainParameterName();
350 
351  std::string mainParUnits = ParameterUtils::poolParameterUnits(*simulation, main_par_name);
352 
353  size_t nbr_samples = distributions[i].getNbrSamples();
354  double sigma_factor = distributions[i].getSigmaFactor();
355 
356  std::string s_distr = "distr_" + std::to_string(i + 1);
357  result << pyfmt::indent() << s_distr << " = "
358  << pyfmt2::printDistribution(*distributions[i].getDistribution(), mainParUnits)
359  << "\n";
360 
361  result << pyfmt::indent() << "simulation.addParameterDistribution(\"" << main_par_name
362  << "\", " << s_distr << ", " << nbr_samples << ", "
363  << pyfmt::printDouble(sigma_factor)
364  << pyfmt::printRealLimitsArg(distributions[i].getLimits(), mainParUnits) << ")\n";
365  }
366  return result.str();
367 }
const Distributions_t & getDistributions() const
const DistributionHandler & getDistributionHandler() const
Definition: Simulation.h:89
std::string poolParameterUnits(const IParameterized &node, const std::string &parName)
Returns units of main parameter.
std::string printDistribution(const IDistribution1D &par_distr, const std::string &units)
Prints distribution with constructor parameters in given units.
Definition: PyFmt2.cpp:117
std::string printRealLimitsArg(const RealLimits &limits, const std::string &units)
Prints RealLimits in the form of argument (in the context of ParameterDistribution and similar).
Definition: PyFmtLimits.cpp:61

References Simulation::getDistributionHandler(), DistributionHandler::getDistributions(), pyfmt::indent(), ParameterUtils::poolParameterUnits(), pyfmt2::printDistribution(), pyfmt::printDouble(), and pyfmt::printRealLimitsArg().

Referenced by defineGISASSimulation(), defineOffSpecSimulation(), and defineSpecularSimulation().

Here is the call graph for this function:

◆ defineMasks()

std::string SimulationToPython::defineMasks ( const Simulation simulation) const
private

Definition at line 369 of file SimulationToPython.cpp.

370 {
371  std::ostringstream result;
372  result << std::setprecision(12);
373 
374  const IDetector* detector = simulation->instrument().getDetector();
375  const DetectorMask* detectorMask = detector->detectorMask();
376  if (detectorMask && detectorMask->numberOfMasks()) {
377  result << "\n";
378  for (size_t i_mask = 0; i_mask < detectorMask->numberOfMasks(); ++i_mask) {
379  bool mask_value(false);
380  const IShape2D* shape = detectorMask->getMaskShape(i_mask, mask_value);
381  result << pyfmt2::representShape2D(pyfmt::indent(), shape, mask_value,
382  printFunc(detector));
383  }
384  result << "\n";
385  }
386  return result.str();
387 }
Collection of detector masks.
Definition: DetectorMask.h:29
const IShape2D * getMaskShape(size_t mask_index, bool &mask_value) const
size_t numberOfMasks() const
virtual const DetectorMask * detectorMask() const =0
Returns detector masks container.
Basic class for all shapes in 2D.
Definition: IShape2D.h:27
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".
Definition: PyFmt2.cpp:38

References IDetector::detectorMask(), Instrument::getDetector(), DetectorMask::getMaskShape(), pyfmt::indent(), Simulation::instrument(), DetectorMask::numberOfMasks(), anonymous_namespace{SimulationToPython.cpp}::printFunc(), and pyfmt2::representShape2D().

Referenced by defineGISASSimulation(), and defineOffSpecSimulation().

Here is the call graph for this function:

◆ defineSimulationOptions()

std::string SimulationToPython::defineSimulationOptions ( const Simulation simulation) const
private

Definition at line 389 of file SimulationToPython.cpp.

390 {
391  std::ostringstream result;
392  result << std::setprecision(12);
393 
394  const SimulationOptions& options = simulation->getOptions();
395  if (options.getHardwareConcurrency() != options.getNumberOfThreads())
396  result << pyfmt::indent() << "simulation.getOptions().setNumberOfThreads("
397  << options.getNumberOfThreads() << ")\n";
398  if (options.isIntegrate())
399  result << pyfmt::indent() << "simulation.getOptions().setMonteCarloIntegration(True, "
400  << options.getMcPoints() << ")\n";
401  if (options.useAvgMaterials())
402  result << pyfmt::indent() << "simulation.getOptions().setUseAvgMaterials(True)\n";
403  if (options.includeSpecular())
404  result << pyfmt::indent() << "simulation.getOptions().setIncludeSpecular(True)\n";
405  return result.str();
406 }
Collect the different options for simulation.
size_t getMcPoints() const
bool includeSpecular() const
unsigned getHardwareConcurrency() const
unsigned getNumberOfThreads() const
bool isIntegrate() const
bool useAvgMaterials() const
const SimulationOptions & getOptions() const
Definition: Simulation.h:92

References SimulationOptions::getHardwareConcurrency(), SimulationOptions::getMcPoints(), SimulationOptions::getNumberOfThreads(), Simulation::getOptions(), SimulationOptions::includeSpecular(), pyfmt::indent(), SimulationOptions::isIntegrate(), and SimulationOptions::useAvgMaterials().

Referenced by defineGISASSimulation(), defineOffSpecSimulation(), and defineSpecularSimulation().

Here is the call graph for this function:

◆ defineBackground()

std::string SimulationToPython::defineBackground ( const Simulation simulation) const
private

Definition at line 408 of file SimulationToPython.cpp.

409 {
410  std::ostringstream result;
411 
412  auto p_bg = simulation->background();
413  if (auto p_constant_bg = dynamic_cast<const ConstantBackground*>(p_bg)) {
414  if (p_constant_bg->backgroundValue() > 0.0) {
415  result << pyfmt::indent() << "background = ba.ConstantBackground("
416  << pyfmt::printScientificDouble(p_constant_bg->backgroundValue()) << ")\n";
417  result << pyfmt::indent() << "simulation.setBackground(background)\n";
418  }
419  } else if (dynamic_cast<const PoissonNoiseBackground*>(p_bg)) {
420  result << pyfmt::indent() << "background = ba.PoissonNoiseBackground()\n";
421  result << pyfmt::indent() << "simulation.setBackground(background)\n";
422  }
423  return result.str();
424 }
Class representing a constant background signal.
Class representing Poisson noise on top of the scattered intensity.
const IBackground * background() const
Definition: Simulation.h:75

References Simulation::background(), pyfmt::indent(), and pyfmt::printScientificDouble().

Referenced by defineGISASSimulation(), defineOffSpecSimulation(), and defineSpecularSimulation().

Here is the call graph for this function:

◆ defineMain()

std::string SimulationToPython::defineMain ( SimulationToPython::EMainType  mainType = RUN_SIMULATION)
private

Definition at line 426 of file SimulationToPython.cpp.

427 {
428  std::string result;
429  if (mainType == RUN_SIMULATION) {
430  result = "if __name__ == '__main__': \n"
431  " result = run_simulation()\n"
432  " ba.plot_simulation_result(result)\n";
433  } else if (mainType == SAVE_DATA) {
434  result = "if __name__ == '__main__': \n"
435  " result = run_simulation()\n"
436  " import sys\n"
437  " if len(sys.argv)<2:\n"
438  " exit(\"File name is required\")\n"
439  " ba.IntensityDataIOFactory.writeSimulationResult(result, sys.argv[1])\n";
440  } else {
441  throw std::runtime_error("SimulationToPython::defineMain() -> Error. Unknown main type.");
442  }
443  return result;
444 }

References RUN_SIMULATION, and SAVE_DATA.

Referenced by generateSimulationCode().


The documentation for this class was generated from the following files: