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

Utility functions for writing Python code snippets. More...

Functions

std::string indent (size_t width=4u)
 Returns a string of blanks with given width. More...
 
bool isHexagonal (double length1, double length2, double angle)
 
bool isSquare (double length1, double length2, double angle)
 
std::string printBool (double value)
 
std::string printDegrees (double input)
 
std::string printDouble (double input)
 
std::string printImportedSymbols (const std::string &code)
 
std::string printInt (int value)
 
std::string printKvector (const kvector_t value)
 
std::string printLightDouble (double input)
 prints double as an integer, if possible within standard accuracy More...
 
std::string printNm (double input)
 
std::string printNm2 (double input)
 
std::string printRealLimits (const RealLimits &limits, const std::string &units)
 
std::string printRealLimitsArg (const RealLimits &limits, const std::string &units)
 Prints RealLimits in the form of argument (in the context of ParameterDistribution and similar). More...
 
std::string printScientificDouble (double input)
 
std::string printString (const std::string &value)
 
std::string printValue (double value, const std::string &units)
 

Detailed Description

Utility functions for writing Python code snippets.

Function Documentation

◆ indent()

◆ isHexagonal()

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

Definition at line 142 of file PyFmt.cpp.

143 {
144  return length1 == length2 && algo::almostEqual(angle, M_TWOPI / 3.0);
145 }
#define M_TWOPI
Definition: Constants.h:54
bool almostEqual(double a, double b)
Returns true if two doubles agree within machine epsilon.
Definition: Algorithms.h:34

References algo::almostEqual(), and M_TWOPI.

Here is the call graph for this function:

◆ isSquare()

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

Definition at line 137 of file PyFmt.cpp.

138 {
139  return length1 == length2 && algo::almostEqual(angle, M_PI_2);
140 }
#define M_PI_2
Definition: Constants.h:45

References algo::almostEqual(), and M_PI_2.

Here is the call graph for this function:

◆ printBool()

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

Definition at line 36 of file PyFmt.cpp.

37 {
38  return value ? "True" : "False";
39 }

Referenced by pyfmt2::representShape2D().

◆ printDegrees()

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

Definition at line 111 of file PyFmt.cpp.

112 {
113  std::ostringstream inter;
114  inter << printLightDouble(Units::rad2deg(input)) << "*deg";
115  return inter.str();
116 }
double rad2deg(double angle)
Definition: Units.h:55
std::string printLightDouble(double input)
prints double as an integer, if possible within standard accuracy
Definition: PyFmt.cpp:56

References printLightDouble(), and Units::rad2deg().

Referenced by SampleToPython::defineInterferenceFunctions(), SampleToPython::defineLattices2D(), printValue(), and pyfmt2::representShape2D().

Here is the call graph for this function:

◆ printDouble()

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

Definition at line 41 of file PyFmt.cpp.

42 {
43  std::ostringstream inter;
44  inter << std::setprecision(12);
45  if (std::abs(input) < std::numeric_limits<double>::epsilon()) {
46  inter << "0.0";
47  return inter.str();
48  }
49  inter << input;
50  if (inter.str().find('e') == std::string::npos && inter.str().find('.') == std::string::npos)
51  inter << ".0";
52  return inter.str();
53 }

Referenced by SampleToPython::defineInterferenceFunctions(), SampleToPython::defineMaterials(), SampleToPython::defineParticleLayouts(), printKvector(), pyfmt2::printParameterDistribution(), pyfmt2::printRangedDistribution(), and printValue().

◆ printImportedSymbols()

std::string pyfmt::printImportedSymbols ( const std::string &  code)

Definition at line 24 of file PyFmt.cpp.

25 {
26  std::vector<std::string> to_declare;
27  for (const std::string& key : {"angstrom", "deg", "nm", "nm2", "micrometer"})
28  if (code.find("*" + key) != std::string::npos)
29  to_declare.push_back(key);
30  for (const std::string& key : {"kvector_t"})
31  if (code.find(key) != std::string::npos)
32  to_declare.push_back(key);
33  return "from bornagain import " + StringUtils::join(to_declare, ", ") + "\n";
34 }
std::string join(const std::vector< std::string > &joinable, const std::string &joint)
Returns string obtain by joining vector elements.
Definition: StringUtils.cpp:71

References StringUtils::join().

Here is the call graph for this function:

◆ printInt()

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

◆ printKvector()

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

Definition at line 147 of file PyFmt.cpp.

148 {
149  std::ostringstream result;
150  result << "kvector_t(" << printDouble(value.x()) << ", " << printDouble(value.y()) << ", "
151  << printDouble(value.z()) << ")";
152  return result.str();
153 }
T z() const
Returns z-component in cartesian coordinate system.
Definition: BasicVector3D.h:67
T y() const
Returns y-component in cartesian coordinate system.
Definition: BasicVector3D.h:65
T x() const
Returns x-component in cartesian coordinate system.
Definition: BasicVector3D.h:63
std::string printDouble(double input)
Definition: PyFmt.cpp:41

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

Here is the call graph for this function:

◆ printLightDouble()

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

prints double as an integer, if possible within standard accuracy

Definition at line 56 of file PyFmt.cpp.

57 {
58  std::ostringstream inter;
59  int ival = std::lround(input);
60  if (std::abs(input - ival) < 1e-11)
61  inter << ival;
62  else {
63  inter << std::setprecision(12);
64  if (std::abs(input) < std::numeric_limits<double>::epsilon())
65  return "0.0";
66  inter << input;
67  if (inter.str().find('e') == std::string::npos
68  && inter.str().find('.') == std::string::npos)
69  inter << ".0";
70  }
71  return inter.str();
72 }

Referenced by printDegrees(), printNm(), and printNm2().

◆ printNm()

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

Definition at line 74 of file PyFmt.cpp.

75 {
76  std::ostringstream inter;
77  inter << std::setprecision(12);
78  inter << printLightDouble(input) << "*nm";
79  return inter.str();
80 }

References printLightDouble().

Referenced by SampleToPython::defineInterferenceFunctions(), SampleToPython::defineLattices2D(), SampleToPython::defineLattices3D(), SampleToPython::defineLayers(), and printValue().

Here is the call graph for this function:

◆ printNm2()

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

Definition at line 82 of file PyFmt.cpp.

83 {
84  std::ostringstream inter;
85  inter << std::setprecision(12);
86  inter << printLightDouble(input) << "*nm2";
87  return inter.str();
88 }

References printLightDouble().

Referenced by SampleToPython::defineInterferenceFunctions().

Here is the call graph for this function:

◆ printRealLimits()

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

Definition at line 22 of file PyFmtLimits.cpp.

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

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 60 of file PyFmtLimits.cpp.

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

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

Referenced by pyfmt2::printParameterDistribution(), and pyfmt2::printRangedDistribution().

Here is the call graph for this function:

◆ printScientificDouble()

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

Definition at line 91 of file PyFmt.cpp.

92 {
93  std::ostringstream inter;
94  inter << std::scientific;
95  inter << input;
96 
97  std::string::size_type pos = inter.str().find('e');
98  if (pos == std::string::npos)
99  return inter.str();
100 
101  std::string part1 = inter.str().substr(0, pos);
102  std::string part2 = inter.str().substr(pos, std::string::npos);
103 
104  part1.erase(part1.find_last_not_of('0') + 1, std::string::npos);
105  if (part1.back() == '.')
106  part1 += "0";
107 
108  return part1 + part2;
109 }
std::string scientific(const T value, int n=10)
Returns scientific string representing given value of any numeric type.
Definition: StringUtils.h:61

References StringUtils::scientific().

Referenced by SampleToPython::defineMultiLayers(), and PlotEventInfo::valueToString().

Here is the call graph for this function:

◆ printString()

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

Definition at line 130 of file PyFmt.cpp.

131 {
132  std::ostringstream result;
133  result << "\"" << value << "\"";
134  return result.str();
135 }

Referenced by pyfmt2::printAxis().

◆ printValue()

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

Definition at line 118 of file PyFmt.cpp.

119 {
120  if (units == "rad")
121  return printDegrees(value);
122  else if (units == "nm")
123  return printNm(value);
124  else if (units == "")
125  return printDouble(value);
126  else
127  throw std::runtime_error("pyfmt::printValue() -> Error. Unknown units '" + units + "'");
128 }
std::string printNm(double input)
Definition: PyFmt.cpp:74
std::string printDegrees(double input)
Definition: PyFmt.cpp:111

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

Referenced by pyfmt2::printAxis(), printRealLimits(), and pyfmt2::valueTimesUnit().

Here is the call graph for this function: