Skip to content

Commit

Permalink
Add pybinding for writing strahlkorper points
Browse files Browse the repository at this point in the history
  • Loading branch information
knelli2 committed Sep 12, 2024
1 parent de89148 commit 03d5393
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/NumericalAlgorithms/SphericalHarmonics/Python/Strahlkorper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
#include "DataStructures/DataVector.hpp"
#include "DataStructures/ModalVector.hpp"
#include "DataStructures/Tensor/Tensor.hpp"
#include "NumericalAlgorithms/SphericalHarmonics/AngularOrdering.hpp"
#include "NumericalAlgorithms/SphericalHarmonics/IO/FillYlmLegendAndData.hpp"
#include "NumericalAlgorithms/SphericalHarmonics/IO/ReadSurfaceYlm.hpp"
#include "NumericalAlgorithms/SphericalHarmonics/IO/StrahlkorperCoordsToTextFile.hpp"
#include "NumericalAlgorithms/SphericalHarmonics/Strahlkorper.hpp"

namespace py = pybind11;
Expand Down Expand Up @@ -79,5 +81,20 @@ void bind_strahlkorper(pybind11::module& m) { // NOLINT
&ylm::read_surface_ylm_single_time<Frame::Inertial>,
py::arg("file_name"), py::arg("surface_subfile_name"), py::arg("time"),
py::arg("relative_epsilon"), py::arg("check_frame"));
py::enum_<ylm::AngularOrdering>(m, "AngularOrdering")
.value("Strahlkorper", ylm::AngularOrdering::Strahlkorper)
.value("Cce", ylm::AngularOrdering::Cce);
m.def(
"write_sphere_of_points_to_text_file",
[](const double radius, const size_t l_max,
const std::array<double, 3>& center,
const std::string& output_file_name,
const ylm::AngularOrdering ordering, const bool overwrite_file) {
ylm::write_strahlkorper_coords_to_text_file(
radius, l_max, center, output_file_name, ordering, overwrite_file);
},
py::arg("radius"), py::arg("l_max"), py::arg("center"),
py::arg("output_file_name"), py::arg("ordering"),
py::arg("overwrite_file") = false);
}
} // namespace ylm::py_bindings
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
import spectre.Informer as spectre_informer
import spectre.IO.H5 as spectre_h5
from spectre.SphericalHarmonics import (
AngularOrdering,
Strahlkorper,
cartesian_coords,
read_surface_ylm,
read_surface_ylm_single_time,
write_sphere_of_points_to_text_file,
ylm_legend_and_data,
)

Expand All @@ -26,6 +28,9 @@ def setUp(self):
"NumericalAlgorithms/Strahlkorper/Python",
)
self.filename = os.path.join(self.test_dir, "Strahlkorper.h5")
self.text_filename = os.path.join(
self.test_dir, "PyStrahlkorperCoords.txt"
)
shutil.rmtree(self.test_dir, ignore_errors=True)
os.makedirs(self.test_dir, exist_ok=True)

Expand Down Expand Up @@ -80,6 +85,44 @@ def test_strahlkorper(self):
strahlkorper,
)

l_max = 4
print("hello")
# First write with wrong l_max
write_sphere_of_points_to_text_file(
radius=1.2,
l_max=l_max - 1,
center=[-0.1, -0.2, -0.3],
output_file_name=self.text_filename,
ordering=AngularOrdering.Cce,
)
# Test that if overwrite_file = False (the default) an exception is
# raised
self.assertRaises(
RuntimeError,
write_sphere_of_points_to_text_file,
radius=1.2,
l_max=l_max - 1,
center=[-0.1, -0.2, -0.3],
output_file_name=self.text_filename,
ordering=AngularOrdering.Cce,
)
# Finally write the correct l_max so we can check that overwrite_file
# works
write_sphere_of_points_to_text_file(
radius=1.2,
l_max=l_max,
center=[-0.1, -0.2, -0.3],
output_file_name=self.text_filename,
ordering=AngularOrdering.Cce,
overwrite_file=True,
)

with open(self.text_filename, "r") as text_file:
# Physical size of ylm::Spherepack
num_points = (l_max + 1) * (2 * l_max + 1)
all_lines = text_file.readlines()
self.assertEqual(num_points, len(all_lines))


if __name__ == "__main__":
unittest.main(verbosity=2)

0 comments on commit 03d5393

Please sign in to comment.