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

return numpy arrays from std::vectors #1458

Merged
merged 1 commit into from
Dec 29, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions python/meep.i
Original file line number Diff line number Diff line change
Expand Up @@ -1471,6 +1471,18 @@ void _get_gradient(PyObject *grad, PyObject *fields_a, PyObject *fields_f, PyObj
%template(IntVector) std::vector<int>;
%template(DoubleVector) std::vector<double>;

// use NumPy arrays for returning common std::vector types:
%typemap(out) std::vector<double> {
npy_intp vec_len = (npy_intp) $1.size();
$result = PyArray_SimpleNew(1, &vec_len, NPY_DOUBLE);
memcpy(PyArray_DATA((PyArrayObject*) $result), &$1[0], vec_len * sizeof(double));
}
%typemap(out) std::vector<int> {
npy_intp vec_len = (npy_intp) $1.size();
$result = PyArray_SimpleNew(1, &vec_len, NPY_INT);
memcpy(PyArray_DATA((PyArrayObject*) $result), &$1[0], vec_len * sizeof(int));
}

%include "vec.i"
%include "meep.hpp"
%include "meep/mympi.hpp"
Expand All @@ -1482,6 +1494,12 @@ void _get_gradient(PyObject *grad, PyObject *fields_a, PyObject *fields_f, PyObj
%include "std_complex.i"
%template(ComplexVector) std::vector<std::complex<double> >;

%typemap(out) std::vector<std::complex<double> > {
npy_intp vec_len = (npy_intp) $1.size();
$result = PyArray_SimpleNew(1, &vec_len, NPY_COMPLEX128);
memcpy(PyArray_DATA((PyArrayObject*) $result), &$1[0], vec_len * sizeof(double) * 2);
}

std::vector<struct meep::sourcedata> meep::dft_near2far::near_sourcedata(const meep::vec &x_0, double* farpt_list, size_t nfar_pts, std::complex<double>* dJ);

void meep::fields::add_srcdata(struct meep::sourcedata cur_data, meep::src_time *src, size_t n, std::complex<double>* amp_arr);
Expand Down