24 std::string scriptPreamble()
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";
33 std::string getSampleFunctionName()
38 std::string printBool(
double value)
40 return value ?
"True" :
"False";
43 std::string printDouble(
double input)
45 std::ostringstream inter;
46 inter << std::setprecision(12);
47 if (std::abs(input) < std::numeric_limits<double>::epsilon()) {
52 if (inter.str().find(
'e') == std::string::npos && inter.str().find(
'.') == std::string::npos)
57 std::string printNm(
double input)
59 std::ostringstream inter;
60 inter << std::setprecision(12);
61 inter << printDouble(input) <<
"*nm";
65 std::string printNm2(
double input)
67 std::ostringstream inter;
68 inter << std::setprecision(12);
69 inter << printDouble(input) <<
"*nm2";
74 std::string printScientificDouble(
double input)
76 std::ostringstream inter;
80 std::string::size_type pos = inter.str().find(
'e');
81 if (pos == std::string::npos)
84 std::string part1 = inter.str().substr(0, pos);
85 std::string part2 = inter.str().substr(pos, std::string::npos);
87 part1.erase(part1.find_last_not_of(
'0') + 1, std::string::npos);
88 if (part1.back() ==
'.')
94 std::string printDegrees(
double input)
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)
104 std::string printValue(
double value,
const std::string& units)
107 return printDegrees(value);
108 else if (units ==
"nm")
109 return printNm(value);
110 else if (units ==
"")
111 return printDouble(value);
113 throw std::runtime_error(
"pyfmt::printValue() -> Error. Unknown units '" + units +
"'");
116 std::string printString(
const std::string& value)
118 std::ostringstream result;
119 result <<
"\"" << value <<
"\"";
123 bool isSquare(
double length1,
double length2,
double angle)
128 bool isHexagonal(
double length1,
double length2,
double angle)
133 std::string printKvector(
const kvector_t value)
135 std::ostringstream result;
136 result <<
"kvector_t(" << printDouble(value.
x()) <<
", " << printDouble(value.
y()) <<
", "
137 << printDouble(value.
z()) <<
")";
143 return std::string(width,
' ');
Defines and implements namespace algo with some algorithms.
Defines M_PI and some more mathematical constants.
Defines functions in namespace pyfmt.
Defines some unit conversion factors and other constants in namespace Units.
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.
std::string scientific(const T value, int n=10)
Returns scientific string representing given value of any numeric type.
bool almostEqual(double a, double b)
Returns true if two doubles agree within machine epsilon.
Utility functions for writing Python code snippets.
std::string indent(size_t width)
Returns a string of blanks with given width.