Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for ComplexDataVector to Pypp tests #1398

Merged
merged 1 commit into from
Feb 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
62 changes: 62 additions & 0 deletions tests/Unit/Pypp/PyppFundamentals.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,32 @@ struct ToPyObject<DataVector, std::nullptr_t> {
}
};

template <>
struct ToPyObject<ComplexDataVector, std::nullptr_t> {
static PyObject* convert(const ComplexDataVector& t) {
PyObject* npy_array = PyArray_SimpleNew( // NOLINT
1, (std::array<long, 1>{{static_cast<long>(t.size())}}.data()),
NPY_COMPLEX128);

if (npy_array == nullptr) {
throw std::runtime_error{"Failed to convert argument."};
}
for (size_t i = 0; i < t.size(); ++i) {
// clang-tidy: Do not use pointer arithmetic
// clang-tidy: Do not use reinterpret cast
const auto data =
static_cast<std::complex<double>*>(PyArray_GETPTR1( // NOLINT
reinterpret_cast<PyArrayObject*>(npy_array), // NOLINT
static_cast<long>(i)));
if (data == nullptr) {
throw std::runtime_error{"Failed to access argument of PyArray."};
}
*data = t[i];
}
return npy_array;
}
};

template <>
struct FromPyObject<long, std::nullptr_t> {
static long convert(PyObject* t) {
Expand Down Expand Up @@ -397,6 +423,42 @@ struct FromPyObject<DataVector, std::nullptr_t> {
}
};

template <>
struct FromPyObject<ComplexDataVector, std::nullptr_t> {
static ComplexDataVector convert(PyObject* p) {
if (p == nullptr) {
throw std::runtime_error{"Received null PyObject."};
}
// clang-tidy: c-style casts. (Expanded from macro)
if (not PyArray_CheckExact(p)) { // NOLINT
throw std::runtime_error{
"Cannot convert non-array type to ComplexDataVector."};
}
// clang-tidy: reinterpret_cast
const auto npy_array = reinterpret_cast<PyArrayObject*>(p); // NOLINT
if (PyArray_TYPE(npy_array) != NPY_COMPLEX128) {
throw std::runtime_error{
"Cannot convert array of non-complex type to ComplexDataVector."};
}
if (PyArray_NDIM(npy_array) != 1) {
throw std::runtime_error{
"Cannot convert array of ndim != 1 to ComplexDataVector."};
}
// clang-tidy: c-style casts, pointer arithmetic. (Expanded from macro)
ComplexDataVector t(static_cast<size_t>(PyArray_Size(p))); // NOLINT
for (size_t i = 0; i < t.size(); ++i) {
// clang-tidy: pointer arithmetic. (Expanded from macro)
const auto value = static_cast<const std::complex<double>*>(
PyArray_GETPTR1(npy_array, static_cast<long>(i))); // NOLINT
if (value == nullptr) {
throw std::runtime_error{"Failed to get argument from PyArray."};
}
t[i] = *value;
}
return t;
}
};

// This function is needed because one cannot cast an array of size_t's (used by
// SpECTRE) to an array of longs (used by NumPy).
template <size_t Size>
Expand Down
21 changes: 21 additions & 0 deletions tests/Unit/Pypp/Test_Pypp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@

#include <array>
#include <cmath>
#include <complex>
#include <cstddef>
#include <random>
#include <string>
#include <tuple>
#include <type_traits>
#include <vector>

#include "DataStructures/ComplexDataVector.hpp"
#include "DataStructures/DataVector.hpp"
#include "DataStructures/Tensor/Tensor.hpp"
#include "Utilities/Gsl.hpp"
Expand Down Expand Up @@ -137,6 +139,25 @@ SPECTRE_TEST_CASE("Unit.Pypp.DataVector", "[Pypp][Unit]") {
CHECK_THROWS(pypp::call<DataVector>("PyppPyTests", "ndarray_of_floats"));
}

SPECTRE_TEST_CASE("Unit.Pypp.ComplexDataVector", "[Pypp][Unit]") {
pypp::SetupLocalPythonEnvironment local_python_env{"Pypp/"};
std::complex<double> test_value_0{1.3, 2.2};
std::complex<double> test_value_1{4.0, 3.1};
std::complex<double> test_value_2{4.2, 5.7};
std::complex<double> test_value_3{6.8, 7.3};
const auto ret = pypp::call<ComplexDataVector>(
"numpy", "multiply", ComplexDataVector{test_value_0, test_value_1},
ComplexDataVector{test_value_2, test_value_3});
CHECK_ITERABLE_APPROX(ret[0], test_value_0 * test_value_2);
CHECK_ITERABLE_APPROX(ret[1], test_value_1 * test_value_3);
CHECK_THROWS(pypp::call<std::string>(
"numpy", "multiply", ComplexDataVector{test_value_0, test_value_1},
ComplexDataVector{test_value_2, test_value_3}));
CHECK_THROWS(pypp::call<ComplexDataVector>("PyppPyTests", "two_dim_ndarray"));
CHECK_THROWS(
pypp::call<ComplexDataVector>("PyppPyTests", "ndarray_of_floats"));
}

SPECTRE_TEST_CASE("Unit.Pypp.Tensor.Double", "[Pypp][Unit]") {
pypp::SetupLocalPythonEnvironment local_python_env{"Pypp/"};

Expand Down