diff --git a/modesolverpy/mode_solver.py b/modesolverpy/mode_solver.py index 10af31c..d96ea04 100644 --- a/modesolverpy/mode_solver.py +++ b/modesolverpy/mode_solver.py @@ -317,6 +317,14 @@ def _write_mode_to_file(self, mode, filename): e_str = ",".join([str(v) for v in e]) fs.write(e_str + "\n") return mode + + def _write_mode_to_file_details(self, mode, filename): + efieldarray = [] + for e, y in zip(mode[::-1], self._structure.y[::-1]): + for a, x in zip(e, self._structure.x[::-1]): + efieldarray.append([x,y,a]) + np.savetxt(filename, efieldarray, delimiter=',',fmt='%.4e%+.4ej, %.4e%+.4ej, %.4e%+.4ej') + return mode def _plot_n_effs(self, filename_n_effs, filename_te_fractions, xlabel, ylabel, title): args = { @@ -545,7 +553,8 @@ def _solve(self, structure, wavelength): return r - def write_modes_to_file(self, filename="mode.dat", plot=True, analyse=True): + def write_modes_to_file(self, filename="mode.dat", plot=True, analyse=True, + details=False): """ Writes the mode fields to a file and optionally plots them. @@ -561,6 +570,8 @@ def write_modes_to_file(self, filename="mode.dat", plot=True, analyse=True): diameter (MFD) and marks it on the output, and it marks with a cross the maximum E-field value. Default is `True`. + details (bool): 'True' if you want to save the x, y values in + addition to the electric field amplitudes. Returns: dict: A dictionary containing the effective indices @@ -606,6 +617,9 @@ def write_modes_to_file(self, filename="mode.dat", plot=True, analyse=True): self.n_effs[i], wavelength=self._structure._wl, ) + if details: + self._write_mode_to_file_details( mode, filename) + return self.modes @@ -731,6 +745,7 @@ def write_modes_to_file( filename="mode.dat", plot=True, fields_to_write=("Ex", "Ey", "Ez", "Hx", "Hy", "Hz"), + details=False ): """ Writes the mode fields to a file and optionally plots them. @@ -747,6 +762,8 @@ def write_modes_to_file( defining what part of the mode should be saved and plotted. By default, all six components are written and plotted. + details (bool): 'True' if you want to save the x, y values in + addition to the electric field amplitudes. Returns: dict: A dictionary containing the effective indices @@ -795,5 +812,8 @@ def write_modes_to_file( area=area, wavelength=self._structure._wl, ) + if details: + self._write_mode_to_file_details( mode, filename) + return self.modes diff --git a/modesolverpy/structure_base.py b/modesolverpy/structure_base.py index fa8a630..0dfc3f1 100644 --- a/modesolverpy/structure_base.py +++ b/modesolverpy/structure_base.py @@ -133,7 +133,7 @@ def x(self): ''' if None not in (self.x_min, self.x_max, self.x_step) and \ self.x_min != self.x_max: - x = np.arange(self.x_min, self.x_max+self.x_step-self.y_step*0.1, self.x_step) + x = np.arange(self.x_min, self.x_max+self.x_step-self.x_step*0.1, self.x_step) else: x = np.array([]) return x @@ -369,7 +369,7 @@ def add_slab(self, height, n_background=1., position='top'): else: n_back = n_background - height_discretised = self.y_step*((height // self.y_step) + 1) + height_discretised = self.y_step*(height // self.y_step) y_min = self._next_start y_max = y_min + height_discretised