BornAgain  1.18.0
Simulate and fit neutron and x-ray scattering at grazing incidence
pyfmt Namespace Reference

Functions

std::string scriptPreamble ()
 
std::string getSampleFunctionName ()
 
std::string printBool (double value)
 
std::string printDouble (double input)
 
std::string printNm (double input)
 
std::string printNm2 (double input)
 
std::string printScientificDouble (double input)
 
std::string printDegrees (double input)
 
std::string printValue (double value, const std::string &units)
 
std::string printString (const std::string &value)
 
bool isSquare (double length1, double length2, double angle)
 
bool isHexagonal (double length1, double length2, double angle)
 
std::string printKvector (const kvector_t value)
 
std::string indent (size_t width=4u)
 
std::string printInt (int value)
 
std::string printRealLimits (const RealLimits &limits, const std::string &units)
 
std::string printRealLimitsArg (const RealLimits &limits, const std::string &units)
 

Detailed Description

Utility functions for writing Python code snippets.

Function Documentation

◆ scriptPreamble()

std::string pyfmt::scriptPreamble ( )

Definition at line 24 of file PyFmt.cpp.

25 {
26  const std::string result = "import numpy\n"
27  "import bornagain as ba\n"
28  "from bornagain import deg, angstrom, nm, nm2, kvector_t\n\n\n";
29 
30  return result;
31 }

Referenced by SimulationToPython::generateSimulationCode().

◆ getSampleFunctionName()

std::string pyfmt::getSampleFunctionName ( )

Definition at line 33 of file PyFmt.cpp.

34 {
35  return "get_sample";
36 }

Referenced by SampleToPython::defineGetSample().

◆ printBool()

std::string pyfmt::printBool ( double  value)

Definition at line 38 of file PyFmt.cpp.

39 {
40  return value ? "True" : "False";
41 }

Referenced by pyfmt2::representShape2D().

◆ printDouble()

std::string pyfmt::printDouble ( double  input)

◆ printNm()

std::string pyfmt::printNm ( double  input)

Definition at line 57 of file PyFmt.cpp.

58 {
59  std::ostringstream inter;
60  inter << std::setprecision(12);
61  inter << printDouble(input) << "*nm";
62  return inter.str();
63 }
std::string printDouble(double input)
Definition: PyFmt.cpp:43

References printDouble().

Referenced by SimulationToPython::defineGISASBeam(), SampleToPython::defineInterferenceFunctions(), SampleToPython::defineLattices(), SimulationToPython::defineOffSpecBeam(), printValue(), and SampleToPython::setPositionInformation().

Here is the call graph for this function:

◆ printNm2()

std::string pyfmt::printNm2 ( double  input)

Definition at line 65 of file PyFmt.cpp.

66 {
67  std::ostringstream inter;
68  inter << std::setprecision(12);
69  inter << printDouble(input) << "*nm2";
70  return inter.str();
71 }

References printDouble().

Referenced by SampleToPython::defineInterferenceFunctions().

Here is the call graph for this function:

◆ printScientificDouble()

std::string pyfmt::printScientificDouble ( double  input)

Definition at line 74 of file PyFmt.cpp.

75 {
76  std::ostringstream inter;
77  inter << std::scientific;
78  inter << input;
79 
80  std::string::size_type pos = inter.str().find('e');
81  if (pos == std::string::npos)
82  return inter.str();
83 
84  std::string part1 = inter.str().substr(0, pos);
85  std::string part2 = inter.str().substr(pos, std::string::npos);
86 
87  part1.erase(part1.find_last_not_of('0') + 1, std::string::npos);
88  if (part1.back() == '.')
89  part1 += "0";
90 
91  return part1 + part2;
92 }
std::string scientific(const T value, int n=10)
Returns scientific string representing given value of any numeric type.
Definition: StringUtils.h:54

References StringUtils::scientific().

Referenced by SimulationToPython::defineBackground(), SimulationToPython::defineBeamIntensity(), and SampleToPython::defineMultiLayers().

Here is the call graph for this function:

◆ printDegrees()

std::string pyfmt::printDegrees ( double  input)

Definition at line 94 of file PyFmt.cpp.

95 {
96  std::ostringstream inter;
97  inter << std::setprecision(11) << Units::rad2deg(input);
98  if (inter.str().find('e') == std::string::npos && inter.str().find('.') == std::string::npos)
99  inter << ".0";
100  inter << "*deg";
101  return inter.str();
102 }
double rad2deg(double angle)
Definition: Units.h:43

References Units::rad2deg().

Referenced by SimulationToPython::defineDetector(), SimulationToPython::defineGISASBeam(), SampleToPython::defineInterferenceFunctions(), SimulationToPython::defineOffSpecBeam(), anonymous_namespace{SimulationToPython.cpp}::printFunc(), printValue(), pyfmt2::representShape2D(), SampleToPython::setRotationInformation(), and pyfmt2::valueTimesUnit().

Here is the call graph for this function:

◆ printValue()

std::string pyfmt::printValue ( double  value,
const std::string &  units 
)

Definition at line 104 of file PyFmt.cpp.

105 {
106  if (units == "rad")
107  return printDegrees(value);
108  else if (units == "nm")
109  return printNm(value);
110  else if (units == "")
111  return printDouble(value);
112  else
113  throw std::runtime_error("pyfmt::printValue() -> Error. Unknown units '" + units + "'");
114 }
std::string printNm(double input)
Definition: PyFmt.cpp:57
std::string printDegrees(double input)
Definition: PyFmt.cpp:94

References printDegrees(), printDouble(), and printNm().

Referenced by printRealLimits(), PointwiseAxis::pyString(), and FixedBinAxis::pyString().

Here is the call graph for this function:

◆ printString()

std::string pyfmt::printString ( const std::string &  value)

Definition at line 116 of file PyFmt.cpp.

117 {
118  std::ostringstream result;
119  result << "\"" << value << "\"";
120  return result.str();
121 }

Referenced by FixedBinAxis::pyString().

◆ isSquare()

bool pyfmt::isSquare ( double  length1,
double  length2,
double  angle 
)

Definition at line 123 of file PyFmt.cpp.

124 {
125  return length1 == length2 && algo::almostEqual(angle, M_PI_2);
126 }
#define M_PI_2
Definition: MathConstants.h:40
bool almostEqual(double a, double b)
Returns true if two doubles agree within machine epsilon.
Definition: Algorithms.h:30

References algo::almostEqual(), and M_PI_2.

Here is the call graph for this function:

◆ isHexagonal()

bool pyfmt::isHexagonal ( double  length1,
double  length2,
double  angle 
)

Definition at line 128 of file PyFmt.cpp.

129 {
130  return length1 == length2 && algo::almostEqual(angle, M_TWOPI / 3.0);
131 }
#define M_TWOPI
Definition: MathConstants.h:49

References algo::almostEqual(), and M_TWOPI.

Here is the call graph for this function:

◆ printKvector()

std::string pyfmt::printKvector ( const kvector_t  value)

Definition at line 133 of file PyFmt.cpp.

134 {
135  std::ostringstream result;
136  result << "kvector_t(" << printDouble(value.x()) << ", " << printDouble(value.y()) << ", "
137  << printDouble(value.z()) << ")";
138  return result.str();
139 }
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

References printDouble(), BasicVector3D< T >::x(), BasicVector3D< T >::y(), and BasicVector3D< T >::z().

Referenced by SimulationToPython::defineDetector().

Here is the call graph for this function:

◆ indent()

◆ printInt()

std::string pyfmt::printInt ( int  value)

◆ printRealLimits()

std::string pyfmt::printRealLimits ( const RealLimits limits,
const std::string &  units 
)

Definition at line 23 of file PyFmtLimits.cpp.

24 {
25  std::ostringstream result;
26 
27  if (limits.isLimitless()) {
28  result << "RealLimits()";
29  }
30 
31  else if (limits.isPositive()) {
32  result << "RealLimits.positive()";
33  }
34 
35  else if (limits.isNonnegative()) {
36  result << "RealLimits.nonnegative()";
37  }
38 
39  else if (limits.isLowerLimited()) {
40  result << "RealLimits.lowerLimited(" << pyfmt::printValue(limits.lowerLimit(), units)
41  << ")";
42  }
43 
44  else if (limits.isUpperLimited()) {
45  result << "RealLimits.upperLimited(" << pyfmt::printValue(limits.upperLimit(), units)
46  << ")";
47  }
48 
49  else if (limits.isLimited()) {
50  result << "RealLimits.limited(" << pyfmt::printValue(limits.lowerLimit(), units) << ", "
51  << pyfmt::printValue(limits.upperLimit(), units) << ")";
52  }
53 
54  return result.str();
55 }
bool isLimited() const
Definition: RealLimits.cpp:197
bool isLowerLimited() const
Definition: RealLimits.cpp:187
bool isPositive() const
Definition: RealLimits.cpp:176
double upperLimit() const
Returns upper limit.
Definition: RealLimits.cpp:60
double lowerLimit() const
Returns lower limit.
Definition: RealLimits.cpp:38
bool isNonnegative() const
Definition: RealLimits.cpp:182
bool isLimitless() const
Definition: RealLimits.cpp:171
bool isUpperLimited() const
Definition: RealLimits.cpp:192
std::string printValue(double value, const std::string &units)
Definition: PyFmt.cpp:104

References RealLimits::isLimited(), RealLimits::isLimitless(), RealLimits::isLowerLimited(), RealLimits::isNonnegative(), RealLimits::isPositive(), RealLimits::isUpperLimited(), RealLimits::lowerLimit(), printValue(), and RealLimits::upperLimit().

Referenced by printRealLimitsArg().

Here is the call graph for this function:

◆ printRealLimitsArg()

std::string pyfmt::printRealLimitsArg ( const RealLimits limits,
const std::string &  units 
)

Prints RealLimits in the form of argument (in the context of ParameterDistribution and similar).

Default RealLimits will not be printed, any other will be printed as ", ba.RealLimits.limited(1*deg, 2*deg)"

Definition at line 61 of file PyFmtLimits.cpp.

62 {
63  return limits.isLimitless() ? "" : ", ba." + printRealLimits(limits, units);
64 }
std::string printRealLimits(const RealLimits &limits, const std::string &units)
Definition: PyFmtLimits.cpp:23

References RealLimits::isLimitless(), and printRealLimits().

Referenced by SimulationToPython::defineParameterDistributions(), pyfmt2::printParameterDistribution(), and RangedDistribution::pyString().

Here is the call graph for this function: