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

use_centered_grid #1095

Merged
merged 3 commits into from
Jan 9, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions doc/docs/Python_User_Interface.md
Original file line number Diff line number Diff line change
Expand Up @@ -1051,9 +1051,9 @@ Given a frequency `omega` and a `Vector3` `pt`, returns the average eigenvalue o
Initialize the component `c` fields using the function `func` which has a single argument, a `Vector3` giving a position and returns a complex number for the value of the field at that point.

**`add_dft_fields(cs, freq_min, freq_max, nfreq, where=None, center=None, size=None)`**
**`add_dft_fields(cs, freq_min, freq_max, nfreq, where=None, center=None, size=None, yee_grid=False)`**
Given a list of field components `cs`, compute the Fourier transform of these fields for `nfreq` equally spaced frequencies covering the frequency range `freq_min` to `freq_max` over the `Volume` specified by `where` (default to the entire cell). The volume can also be specified via the `center` and `size` arguments.
Given a list of field components `cs`, compute the Fourier transform of these fields for `nfreq` equally spaced frequencies covering the frequency range `freq_min` to `freq_max` over the `Volume` specified by `where` (default to the entire cell). The volume can also be specified via the `center` and `size` arguments. The default routine interpolates the Fourier transformed fields at the center of each voxel within the specified volume. Alternatively, the exact Fourier transformed fields evaluated at each corresponding Yee grid point is available by setting `yee_grid` to `True`.

**`flux_in_box(dir, box=None, center=None, size=None)`**
Expand Down
10 changes: 10 additions & 0 deletions python/meep.i
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,16 @@ meep::volume_list *make_volume_list(const meep::volume &v, int c,
//--------------------------------------------------
// typemaps needed for add_dft_fields
//--------------------------------------------------

%typecheck(SWIG_TYPECHECK_POINTER) const volume where {
int py_material = PyObject_IsInstance($input, py_volume_object());
$1 = py_material;
}

%typecheck(SWIG_TYPECHECK_POINTER) meep::component *components {
$1 = PyList_Check($input);
}

%typemap(in) (meep::component *components, int num_components) {
if (!PyList_Check($input)) {
meep::abort("Expected a list");
Expand Down
10 changes: 5 additions & 5 deletions python/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1539,22 +1539,22 @@ def _evaluate_dft_objects(self):
if dft.swigobj is None:
dft.swigobj = dft.func(*dft.args)

def add_dft_fields(self, components, freq_min, freq_max, nfreq, where=None, center=None, size=None):
def add_dft_fields(self, components, freq_min, freq_max, nfreq, where=None, center=None, size=None, yee_grid=False):
center_v3 = Vector3(*center) if center is not None else None
size_v3 = Vector3(*size) if size is not None else None
dftf = DftFields(self._add_dft_fields, [components, where, center_v3, size_v3, freq_min, freq_max, nfreq])
use_centered_grid = not yee_grid
dftf = DftFields(self._add_dft_fields, [components, where, center_v3, size_v3, freq_min, freq_max, nfreq, use_centered_grid])
self.dft_objects.append(dftf)
return dftf

def _add_dft_fields(self, components, where, center, size, freq_min, freq_max, nfreq):
def _add_dft_fields(self, components, where, center, size, freq_min, freq_max, nfreq, use_centered_grid):
if self.fields is None:
self.init_sim()
try:
where = self._volume_from_kwargs(where, center, size)
except ValueError:
where = self.fields.total_volume()

return self.fields.add_dft_fields(components, where, freq_min, freq_max, nfreq)
return self.fields.add_dft_fields(components, where, freq_min, freq_max, nfreq, use_centered_grid)

def output_dft(self, dft_fields, fname):
if self.fields is None:
Expand Down
7 changes: 6 additions & 1 deletion python/tests/dft_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ def init(self):
sources=sources,
boundary_layers=pml_layers,
)

def test_use_centered_grid(self):
sim = self.init()
sim.init_sim()
dft_fields = sim.add_dft_fields([mp.Ez], self.fcen, self.fcen, 1, yee_grid=True)
sim.run(until=100)

def test_get_dft_array(self):
sim = self.init()
sim.init_sim()
Expand Down
9 changes: 9 additions & 0 deletions python/typemap_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@ static PyObject *py_vector3_object() {
return vector3_object;
}

static PyObject *py_volume_object() {
static PyObject *volume_object = NULL;
if (volume_object == NULL) {
PyObject *geom_mod = get_geom_mod();
volume_object = PyObject_GetAttrString(PyImport_ImportModule("meep"), "Volume");
}
return volume_object;
}

static PyObject *vec2py(const meep::vec &v, bool newobj = false) {

double x = 0, y = 0, z = 0;
Expand Down
7 changes: 5 additions & 2 deletions src/dft.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -672,13 +672,16 @@ void dft_fields::remove() {
}

dft_fields fields::add_dft_fields(component *components, int num_components, const volume where,
double freq_min, double freq_max, int Nfreq) {
double freq_min, double freq_max, int Nfreq, bool use_centered_grid) {
bool include_dV_and_interp_weights = false;
bool sqrt_dV_and_interp_weights = false; // default option from meep.hpp (expose to user?)
std::complex<double> extra_weight = 1.0; // default option from meep.hpp (expose to user?)
cdouble stored_weight = 1.0;
dft_chunk *chunks = 0;
for (int nc = 0; nc < num_components; nc++)
chunks = add_dft(components[nc], where, freq_min, freq_max, Nfreq,
include_dV_and_interp_weights, stored_weight, chunks);
include_dV_and_interp_weights, stored_weight, chunks,
sqrt_dV_and_interp_weights,extra_weight,use_centered_grid);

return dft_fields(chunks, freq_min, freq_max, Nfreq, where);
}
Expand Down
2 changes: 1 addition & 1 deletion src/meep.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1743,7 +1743,7 @@ class fields {
int Nfreq);

dft_fields add_dft_fields(component *components, int num_components, const volume where,
double freq_min, double freq_max, int Nfreq);
double freq_min, double freq_max, int Nfreq, bool use_centered_grid=true);

/********************************************************/
/* process_dft_component is an intermediate-level */
Expand Down