BornAgain  1.18.0
Simulate and fit neutron and x-ray scattering at grazing incidence
PyFmt.cpp
Go to the documentation of this file.
1 // ************************************************************************** //
2 //
3 // BornAgain: simulate and fit scattering at grazing incidence
4 //
5 //! @file Base/Utils/PyFmt.cpp
6 //! @brief Implements functions from namespace pyfmt.
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************** //
14 
15 #include "Base/Utils/PyFmt.h"
17 #include "Base/Const/Units.h" // printDegrees
18 #include "Base/Utils/Algorithms.h"
19 #include <iomanip>
20 
21 namespace pyfmt
22 {
23 
24 std::string scriptPreamble()
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 }
32 
33 std::string getSampleFunctionName()
34 {
35  return "get_sample";
36 }
37 
38 std::string printBool(double value)
39 {
40  return value ? "True" : "False";
41 }
42 
43 std::string printDouble(double input)
44 {
45  std::ostringstream inter;
46  inter << std::setprecision(12);
47  if (std::abs(input) < std::numeric_limits<double>::epsilon()) {
48  inter << "0.0";
49  return inter.str();
50  }
51  inter << input;
52  if (inter.str().find('e') == std::string::npos && inter.str().find('.') == std::string::npos)
53  inter << ".0";
54  return inter.str();
55 }
56 
57 std::string printNm(double input)
58 {
59  std::ostringstream inter;
60  inter << std::setprecision(12);
61  inter << printDouble(input) << "*nm";
62  return inter.str();
63 }
64 
65 std::string printNm2(double input)
66 {
67  std::ostringstream inter;
68  inter << std::setprecision(12);
69  inter << printDouble(input) << "*nm2";
70  return inter.str();
71 }
72 
73 // 1.000000e+07 -> 1.0e+07
74 std::string printScientificDouble(double input)
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 }
93 
94 std::string printDegrees(double input)
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 }
103 
104 std::string printValue(double value, const std::string& units)
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 }
115 
116 std::string printString(const std::string& value)
117 {
118  std::ostringstream result;
119  result << "\"" << value << "\"";
120  return result.str();
121 }
122 
123 bool isSquare(double length1, double length2, double angle)
124 {
125  return length1 == length2 && algo::almostEqual(angle, M_PI_2);
126 }
127 
128 bool isHexagonal(double length1, double length2, double angle)
129 {
130  return length1 == length2 && algo::almostEqual(angle, M_TWOPI / 3.0);
131 }
132 
133 std::string printKvector(const kvector_t value)
134 {
135  std::ostringstream result;
136  result << "kvector_t(" << printDouble(value.x()) << ", " << printDouble(value.y()) << ", "
137  << printDouble(value.z()) << ")";
138  return result.str();
139 }
140 
141 std::string indent(size_t width)
142 {
143  return std::string(width, ' ');
144 }
145 
146 } // namespace pyfmt
Defines and implements namespace algo with some algorithms.
Defines M_PI and some more mathematical constants.
#define M_TWOPI
Definition: MathConstants.h:49
#define M_PI_2
Definition: MathConstants.h:40
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.
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
std::string scientific(const T value, int n=10)
Returns scientific string representing given value of any numeric type.
Definition: StringUtils.h:54
double rad2deg(double angle)
Definition: Units.h:43
bool almostEqual(double a, double b)
Returns true if two doubles agree within machine epsilon.
Definition: Algorithms.h:30
Utility functions for writing Python code snippets.
Definition: PyFmt.cpp:22
std::string printNm(double input)
Definition: PyFmt.cpp:57
std::string getSampleFunctionName()
Definition: PyFmt.cpp:33
std::string printNm2(double input)
Definition: PyFmt.cpp:65
std::string printDegrees(double input)
Definition: PyFmt.cpp:94
bool isSquare(double length1, double length2, double angle)
Definition: PyFmt.cpp:123
std::string printDouble(double input)
Definition: PyFmt.cpp:43
std::string printKvector(const kvector_t value)
Definition: PyFmt.cpp:133
std::string printScientificDouble(double input)
Definition: PyFmt.cpp:74
std::string printBool(double value)
Definition: PyFmt.cpp:38
std::string printString(const std::string &value)
Definition: PyFmt.cpp:116
std::string indent(size_t width)
Returns a string of blanks with given width.
Definition: PyFmt.cpp:141
bool isHexagonal(double length1, double length2, double angle)
Definition: PyFmt.cpp:128
std::string printValue(double value, const std::string &units)
Definition: PyFmt.cpp:104
std::string scriptPreamble()
Definition: PyFmt.cpp:24