diff --git a/python/examples/mode-decomposition.py b/python/examples/mode-decomposition.py index f3a8912fc..1dd09d689 100644 --- a/python/examples/mode-decomposition.py +++ b/python/examples/mode-decomposition.py @@ -76,20 +76,16 @@ def main(args): sources=sources, symmetries=symmetries) - xm = -0.5*sx + dpml + 0.5*Lw; # x-coordinate of monitor - mflux = sim.add_flux(fcen, 0, 1, mp.FluxRegion(center=mp.Vector3(xm,0), size=mp.Vector3(0,sy-2*dpml))); + xm = -0.5*sx + dpml + 0.5*Lw # x-coordinate of monitor + mflux = sim.add_eigenmode(fcen, 0, 1, mp.FluxRegion(center=mp.Vector3(xm,0), size=mp.Vector3(0,sy-2*dpml))) sim.run(until_after_sources=mp.stop_when_fields_decayed(50, mp.Ez, mp.Vector3(xm,0), 1e-10)) - bands = np.array([1],dtype=np.int32) # indices of modes for which to compute expansion coefficients - num_bands = 1; - alpha = np.zeros(2*num_bands,dtype=np.complex128) # preallocate array to store coefficients - vgrp = np.zeros(num_bands,dtype=np.float64) # also store mode group velocities - mvol = mp.volume(mp.vec(xm,-0.5*sy+dpml),mp.vec(xm,+0.5*sy-dpml)) - sim.fields.get_eigenmode_coefficients(mflux, mp.X, mvol, bands, alpha, vgrp) + bands = [1] # indices of modes for which to compute expansion coefficients + alpha = sim.get_eigenmode_coefficients(mflux, bands) - alpha0Plus = alpha[2*0 + 0]; # coefficient of forward-traveling fundamental mode - alpha0Minus = alpha[2*0 + 1]; # coefficient of backward-traveling fundamental mode + alpha0Plus = alpha[2*0 + 0] # coefficient of forward-traveling fundamental mode + alpha0Minus = alpha[2*0 + 1] # coefficient of backward-traveling fundamental mode print("refl:, {}, {:.8f}".format(Lt, abs(alpha0Minus)**2)) diff --git a/python/meep.i b/python/meep.i index d3e6f59cf..8f3896c03 100644 --- a/python/meep.i +++ b/python/meep.i @@ -623,13 +623,6 @@ meep::volume_list *make_volume_list(const meep::volume &v, int c, $1 = (std::complex *)array_data($input); } -%typecheck(SWIG_TYPECHECK_POINTER, fragment="NumPy_Fragments") double* vgrp { - $1 = is_array($input); -} - -%typemap(in, fragment="NumPy_Macros") double* vgrp { - $1 = (double *)array_data($input); -} //-------------------------------------------------- // end typemaps for get_eigenmode_coefficients //-------------------------------------------------- diff --git a/python/simulation.py b/python/simulation.py index 3e128938e..a72787937 100644 --- a/python/simulation.py +++ b/python/simulation.py @@ -760,6 +760,8 @@ def add_flux(self, fcen, df, nfreq, *fluxes): return self._add_fluxish_stuff(self.fields.add_dft_flux, fcen, df, nfreq, fluxes) + add_eigenmode = add_flux + def display_fluxes(self, *fluxes): display_csv(self, 'flux', zip(get_flux_freqs(fluxes[0]), *[get_fluxes(f) for f in fluxes])) @@ -929,6 +931,16 @@ def get_dft_array(self, dft_obj, component, num_freq): else: raise ValueError("Invalid type of dft object: {}".format(dft_obj)) + def get_eigenmode_coefficients(self, flux, bands): + if self.fields is None: + raise ValueError("Fields must be initialized before calling get_eigenmode_coefficients") + + num_bands = len(bands) + coeffs = np.zeros(2 * num_bands, dtype=np.complex128) + self.fields.get_eigenmode_coefficients(flux, np.array(bands, dtype=np.intc), coeffs, None) + + return coeffs + def output_field_function(self, name, cs, func, real_only=False, h5file=None): if self.fields is None: raise RuntimeError("Fields must be initialized before calling output_field_function")