From fd627f524fb2347ef32daf384a26d7fc981016d7 Mon Sep 17 00:00:00 2001 From: Alec Hammond Date: Wed, 9 Sep 2020 14:13:14 -0400 Subject: [PATCH] Singleton DFT fix (#1333) * fix bug that caused segfault for singleton elements * exit earlier * revert initialization Co-authored-by: Alec Hammond --- python/meep.i | 5 ++++- src/array_slice.cpp | 11 ++++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/python/meep.i b/python/meep.i index 74e26fabf..da41a4fee 100644 --- a/python/meep.i +++ b/python/meep.i @@ -438,11 +438,14 @@ PyObject *_get_dft_array(meep::fields *f, dft_type dft, meep::component c, int n size_t dims[3]; std::complex *dft_arr = f->get_dft_array(dft, c, num_freq, &rank, dims); - if (rank==0 || dft_arr==NULL){ // this can happen e.g. if component c vanishes by symmetry + if (dft_arr==NULL){ // this can happen e.g. if component c vanishes by symmetry std::complex d[1] = {std::complex(0,0)}; return PyArray_SimpleNewFromData(0, 0, NPY_CDOUBLE, d); } + if (rank == 0) // singleton results + return PyArray_SimpleNewFromData(0, 0, NPY_CDOUBLE, dft_arr); + size_t length = 1; npy_intp *arr_dims = new npy_intp[rank]; for (int i = 0; i < rank; ++i) { diff --git a/src/array_slice.cpp b/src/array_slice.cpp index d747140a9..0c16521b3 100644 --- a/src/array_slice.cpp +++ b/src/array_slice.cpp @@ -662,6 +662,10 @@ double *collapse_array(double *array, int *rank, size_t dims[3], direction dirs[ reduced_dims[reduced_rank++] = dims[r]; } } + if (reduced_rank==0) { + *rank = 0; + return array; // return array as is for singleton use case + } if (reduced_rank == full_rank) return array; // nothing to collapse /*--------------------------------------------------------------*/ @@ -673,11 +677,8 @@ double *collapse_array(double *array, int *rank, size_t dims[3], direction dirs[ else if (full_rank == 3) { stride[0] = dims[1] * dims[2]; stride[1] = dims[2]; - if (reduced_stride[0] != 0) - reduced_stride[0] = reduced_dims[1]; - else if (reduced_stride[1] != 0) - reduced_stride[1] = reduced_dims[1]; - // else: two degenerate dimensions->reduced array is 1-diml, no strides needed + if (reduced_rank == 2) + reduced_stride[reduced_stride[0] != 0 ? 0 : 1] = reduced_dims[1]; } /*--------------------------------------------------------------*/