-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bind PN BinaryTrajectories in Python
- Loading branch information
Showing
8 changed files
with
158 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,3 +25,5 @@ target_link_libraries( | |
PUBLIC | ||
DataStructures | ||
) | ||
|
||
add_subdirectory(Python) |
47 changes: 47 additions & 0 deletions
47
tests/Unit/Helpers/PointwiseFunctions/PostNewtonian/Python/Bindings.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Distributed under the MIT License. | ||
// See LICENSE.txt for details. | ||
|
||
#include <array> | ||
#include <pybind11/numpy.h> | ||
#include <pybind11/pybind11.h> | ||
#include <pybind11/stl.h> | ||
#include <string> | ||
|
||
#include "DataStructures/DataVector.hpp" | ||
#include "DataStructures/Tensor/Tensor.hpp" | ||
#include "Helpers/PointwiseFunctions/PostNewtonian/BinaryTrajectories.hpp" | ||
#include "Utilities/ErrorHandling/SegfaultHandler.hpp" | ||
|
||
namespace py = pybind11; | ||
|
||
PYBIND11_MODULE(_Pybindings, m) { // NOLINT | ||
enable_segfault_handler(); | ||
py::module::import("spectre.DataStructures"); | ||
py::module::import("spectre.DataStructures.Tensor"); | ||
py::class_<BinaryTrajectories>(m, "BinaryTrajectories") | ||
.def(py::init<double, const std::array<double, 3>&, bool>(), | ||
py::arg("initial_separation"), | ||
py::arg("center_of_mass_velocity") = | ||
std::array<double, 3>{{0.0, 0.0, 0.0}}, | ||
py::arg("newtonian") = false) | ||
.def("separation", &BinaryTrajectories::separation<double>, | ||
py::arg("time")) | ||
.def("separation", &BinaryTrajectories::separation<DataVector>, | ||
py::arg("time")) | ||
.def("orbital_frequency", &BinaryTrajectories::orbital_frequency<double>, | ||
py::arg("time")) | ||
.def("orbital_frequency", | ||
&BinaryTrajectories::orbital_frequency<DataVector>, py::arg("time")) | ||
.def("angular_velocity", &BinaryTrajectories::angular_velocity<double>, | ||
py::arg("time")) | ||
.def("angular_velocity", | ||
&BinaryTrajectories::angular_velocity<DataVector>, py::arg("time")) | ||
.def("positions", &BinaryTrajectories::positions<double>, py::arg("time")) | ||
.def("positions", &BinaryTrajectories::positions<DataVector>, | ||
py::arg("time")) | ||
.def("positions_no_expansion", | ||
&BinaryTrajectories::positions_no_expansion<double>, py::arg("time")) | ||
.def("positions_no_expansion", | ||
&BinaryTrajectories::positions_no_expansion<DataVector>, | ||
py::arg("time")); | ||
} |
21 changes: 21 additions & 0 deletions
21
tests/Unit/Helpers/PointwiseFunctions/PostNewtonian/Python/CMakeLists.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Distributed under the MIT License. | ||
# See LICENSE.txt for details. | ||
|
||
set(LIBRARY "PyPostNewtonianHelpers") | ||
|
||
spectre_python_add_module( | ||
PostNewtonian | ||
MODULE_PATH "testing" | ||
LIBRARY_NAME ${LIBRARY} | ||
SOURCES | ||
Bindings.cpp | ||
PYTHON_FILES | ||
__init__.py | ||
) | ||
|
||
spectre_python_link_libraries( | ||
${LIBRARY} | ||
PRIVATE | ||
PostNewtonianHelpers | ||
pybind11::module | ||
) |
4 changes: 4 additions & 0 deletions
4
tests/Unit/Helpers/PointwiseFunctions/PostNewtonian/Python/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Distributed under the MIT License. | ||
# See LICENSE.txt for details. | ||
|
||
from ._Pybindings import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
tests/Unit/Helpers/Tests/PointwiseFunctions/PostNewtonian/Test_BinaryTrajectories.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Distributed under the MIT License. | ||
# See LICENSE.txt for details. | ||
|
||
import unittest | ||
|
||
import numpy as np | ||
import numpy.testing as npt | ||
|
||
from spectre.testing.PostNewtonian import BinaryTrajectories | ||
|
||
|
||
class TestBinaryTrajectories(unittest.TestCase): | ||
def test_binary_trajectories(self): | ||
# This class is tested in C++. Only check here that Pybindings work. | ||
binary_trajectories = BinaryTrajectories(initial_separation=16) | ||
self.assertEqual(binary_trajectories.separation(0.0), 16.0) | ||
self.assertEqual(binary_trajectories.orbital_frequency(0.0), 0.015625) | ||
self.assertEqual(binary_trajectories.angular_velocity(0.0), 0.015625) | ||
npt.assert_allclose( | ||
binary_trajectories.positions(0.0), | ||
([8.0, 0.0, 0.0], [-8.0, 0.0, 0.0]), | ||
) | ||
# Test vectorized interface | ||
times = np.linspace(0.0, 10.0, 10) | ||
npt.assert_allclose( | ||
binary_trajectories.positions(times), | ||
np.transpose( | ||
[binary_trajectories.positions(t) for t in times], | ||
axes=(1, 2, 0), | ||
), | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main(verbosity=2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters