def calculate_relative_error(data_directory, method, step, A_true): """ Calculate relative local error using A_true. """ method_step = "-".join([method, str(step)]) filename = ".".join([method_step, "npy"]) filename = os.path.join(data_directory, filename) A_calc = np.load(filename) delta_power = temporal_power(A_calc) - temporal_power(A_true) mean_relative_error = np.mean(np.abs(delta_power)) mean_relative_error /= np.amax(temporal_power(A_true)) return mean_relative_error
def calculate_relative_error(data_directory, method, target_error, A_true): """ Calculate relative local error using reference field as A_true. """ method_error = "-".join([method, "%.0e" % (target_error)]) filename = ".".join([method_error, "npz"]) filename = os.path.join(data_directory, filename) try: npz_data = np.load(filename) A_calc = npz_data["field"] delta_power = temporal_power(A_calc) - temporal_power(A_true) mean_relative_error = np.mean(np.abs(delta_power)) mean_relative_error /= np.amax(temporal_power(A_true)) #~mean_relative_error /= np.mean(temporal_power(A_true)) #~mean_relative_error /= np.sum(temporal_power(A_true)) number_of_ffts = npz_data["ffts"] return (number_of_ffts, mean_relative_error) except IOError: print "Error opening file: %s" % filename
def calculate_relative_error(data_directory, method, target_error, A_true): """ Calculate relative local error using A_true. """ method_error = "-".join([method, "%.0e" % (target_error)]) filename = ".".join([method_error, "npz"]) filename = os.path.join(data_directory, filename) try: npz_data = np.load(filename) A_calc = npz_data["field"] delta_power = temporal_power(A_calc) - temporal_power(A_true) mean_relative_error = np.mean(np.abs(delta_power)) mean_relative_error /= np.amax(temporal_power(A_true)) #~mean_relative_error /= np.mean(temporal_power(A_true)) #~mean_relative_error /= np.sum(temporal_power(A_true)) number_of_ffts = npz_data["ffts"] return (number_of_ffts, mean_relative_error) except IOError: print("Error opening file: %s" % filename)
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. """ from pyofss import Domain, System, Gaussian, Fibre from pyofss import temporal_power, spectral_power, double_plot, labels system = System(Domain(bit_width=200.0, samples_per_bit=2048)) system.add(Gaussian(peak_power=1.0, width=1.0)) system.add(Fibre(length=5.0, beta=[0.0, 0.0, 0.0, 1.0], gamma=1.0, total_steps=100)) system.run() P_t = temporal_power(system.fields['fibre']) P_nu_normalised = spectral_power(system.fields['fibre'], True) double_plot(system.domain.t, P_t, system.domain.nu, P_nu_normalised, labels['t'], labels['P_t'], labels['nu'], labels['P_nu'], x_range=(95.0, 120.0), X_range=(192.6, 193.7), filename="4-15")
but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. """ from pyofss import Domain, System, Gaussian, Fibre from pyofss import temporal_power, multi_plot, labels system = System(Domain(bit_width=200.0, samples_per_bit=2048)) system.add(Gaussian("gaussian", peak_power=1.0, width=1.0)) system.run() P_ts = [temporal_power(system.fields['gaussian'])] fibres = [Fibre(length=5.0, beta=[0.0, 0.0, 0.0, 1.0], total_steps=100), Fibre(length=5.0, beta=[0.0, 0.0, 1.0, 1.0], total_steps=100)] for fibre in fibres: system = System(Domain(bit_width=200.0, samples_per_bit=2048)) system.add(Gaussian(peak_power=1.0, width=1.0)) system.add(fibre) system.run() P_ts.append(temporal_power(system.fields['fibre'])) z_labels = [r"$z = 0 \, km$", r"$z = 5 \, km$, $\beta_2 = \, 0 \, ps / (nm \cdot km)$", r"$z = 5 \, km$, $\beta_2 \neq \, 0 \, ps / (nm \cdot km)$"]
a high zoom level. """ from pyofss import Domain, System, Gaussian, Fibre from pyofss import temporal_power, multi_plot, labels domain = Domain(bit_width=200.0, samples_per_bit=2048) gaussian = Gaussian(peak_power=1.0, width=1.0) P_ts = [] methods = ['ss_simple', 'ss_symmetric', 'ss_sym_rk4', 'rk4ip'] for m in methods: sys = System(domain) sys.add(gaussian) sys.add( Fibre(length=5.0, method=m, total_steps=50, beta=[0.0, 0.0, 0.0, 1.0], gamma=1.0)) sys.run() P_ts.append(temporal_power(sys.field)) multi_plot(sys.domain.t, P_ts, methods, labels["t"], labels["P_t"], methods, x_range=(80.0, 140.0))
You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. """ from pyofss import Domain, System, Gaussian, Fibre from pyofss import temporal_power, spectral_power, double_plot, labels system = System(Domain(bit_width=200.0, samples_per_bit=2048)) system.add(Gaussian(peak_power=1.0, width=1.0)) system.add( Fibre(length=5.0, beta=[0.0, 0.0, 0.0, 1.0], gamma=1.0, total_steps=100)) system.run() print system.domain P_t = temporal_power(system.fields['fibre']) P_nu_normalised = spectral_power(system.fields['fibre'], True) P_lambda_normalised = P_nu_normalised t = system.domain.t nu = system.domain.nu Lambda = system.domain.Lambda double_plot(nu, P_nu_normalised, Lambda, P_lambda_normalised, labels['nu'], labels['P_nu'], labels['Lambda'], labels['P_lambda'],
phase = self.initial_phase phase -= 2.0 * pi * self.offset_nu * domain.t sechh = 1./np.cosh(t_normalised) sechh = np.where(sechh != 0, np.power(sechh, 1+1j*self.C), 0.) magnitude = sqrt(self.peak_power)*sechh if domain.channels > 1: self.field[self.channel] += magnitude * exp(1j * phase) else: self.field += magnitude * exp(1j * phase) return self.field if __name__ == "__main__": """ Plot a default Diss_soliton in temporal and spectral domain """ from pyofss import Domain, System, Diss_soliton from pyofss import temporal_power, spectral_power, inst_freq from pyofss import double_plot, labels sys = System(Domain(bit_width=500.0)) sys.add(Diss_soliton()) sys.run() double_plot(sys.domain.t, temporal_power(sys.field), sys.domain.nu, spectral_power(sys.field, True), labels["t"], labels["P_t"], labels["nu"], labels["P_nu"], inst_freq = inst_freq(sys.field, sys.domain.dt), y2_label=labels["inst_nu"])
SYS.run() stop = time.clock() NO_OCL_DURATION = (stop - start) / 1000.0 NO_OCL_OUT = SYS.fields["fibre"] sys = System(DOMAIN) sys.add(Gaussian("gaussian", peak_power=1.0, width=1.0)) sys.add(OpenclFibre(TS, dorf="float", length=LENGTH, total_steps=STEPS)) start = time.clock() sys.run() stop = time.clock() OCL_DURATION = (stop - start) / 1000.0 OCL_OUT = sys.fields["ocl_fibre"] NO_OCL_POWER = temporal_power(NO_OCL_OUT) OCL_POWER = temporal_power(OCL_OUT) DELTA_POWER = NO_OCL_POWER - OCL_POWER MEAN_RELATIVE_ERROR = np.mean(np.abs(DELTA_POWER)) MEAN_RELATIVE_ERROR /= np.amax(temporal_power(NO_OCL_OUT)) print("Run time without OpenCL: %e" % NO_OCL_DURATION) print("Run time with OpenCL: %e" % OCL_DURATION) print("Mean relative error: %e" % MEAN_RELATIVE_ERROR) # Expect both plots to appear identical: double_plot(SYS.domain.t, NO_OCL_POWER, SYS.domain.t, OCL_POWER, x_label=labels["t"], y_label=labels["P_t"], X_label=labels["t"], Y_label=labels["P_t"])
You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. """ from pyofss import Domain, System, Gaussian, Fibre from pyofss import temporal_power, chirp, multi_plot, labels P_ts = [] chirps = [] zs = [0.0, 2.0, 4.0] for z in zs: system = System(Domain(bit_width=200.0, samples_per_bit=2048)) system.add(Gaussian(peak_power=1.0, width=1.0)) system.add(Fibre(length=z, beta=[0.0, 0.0, -1.0, 0.0])) system.run() field = system.fields['fibre'] P_ts.append(temporal_power(field)) temp = [f if abs(f) >= 5e-12 else 0.0 for f in field] chirps.append(chirp(temp, system.domain.window_nu)) multi_plot(system.domain.t, P_ts, zs, labels["t"], labels["P_t"], [r"$z = {0:.0f} \, km$"], (90.0, 110.0), filename="3-1") multi_plot(system.domain.t, chirps, zs, labels["t"], labels["chirp"], [r"$z = {0:.0f} \, km$"], (90.0, 110.0), (-6.0, 6.0), filename="3-1_chirp")
return self.nonlinearity.exp_non(A, h, B) if __name__ == "__main__": """ Plot the result of a Gaussian pulse propagating through optical fibre. Simulates both (third-order) dispersion and nonlinearity. Use five different methods: ss_simple, ss_symmetric, ss_sym_rk4, ss_sym_rkf, and rk4ip. Expect all five methods to produce similar results; plot traces should all overlap. Separate traces should only be seen at a high zoom level. """ from pyofss import Domain, System, Gaussian, Fibre from pyofss import temporal_power, multi_plot, labels domain = Domain(bit_width=200.0, samples_per_bit=2048) gaussian = Gaussian(peak_power=1.0, width=1.0) P_ts = [] methods = ['ss_simple', 'ss_symmetric', 'ss_sym_rk4', 'rk4ip'] for m in methods: sys = System(domain) sys.add(gaussian) sys.add(Fibre(length=5.0, method=m, total_steps=50, beta=[0.0, 0.0, 0.0, 1.0], gamma=1.0)) sys.run() P_ts.append(temporal_power(sys.field)) multi_plot(sys.domain.t, P_ts, methods, labels["t"], labels["P_t"], methods, x_range=(80.0, 140.0))
system = System(Domain(bit_width=30.0, samples_per_bit=8192, channels=2)) system.add(Gaussian(width=1.0, peak_power=100.0, channel=0)) system.add(Gaussian(width=1.0, peak_power=1.0, channel=1, offset_nu=offset_nu)) system.add( Fibre('fibre', length=0.4, gamma=[1.0, 1.2], beta=[[0.0, 0.0, 1.0, 0.0], [0.0, 10.0, 1.0, 0.0]], centre_omega=(nu_to_omega(nu_0), nu_to_omega(nu_1)), sim_type='wdm', method='ARK4IP')) system.run() A_fs = system.fields['fibre'] P_t0 = temporal_power(A_fs[0]) P_t1 = temporal_power(A_fs[1]) double_plot(system.domain.t, P_t0, system.domain.t, P_t1, labels["t"], labels["P_t"], labels["t"], labels["P_t"], filename="7-6a") P_nu0 = spectral_power(A_fs[0], True) P_nu1 = spectral_power(A_fs[1], True)
from pyofss.domain import nu_to_omega from pyofss import Domain, System, Gaussian, Fibre from pyofss import temporal_power, double_plot, labels domain = Domain(bit_width=20.0, samples_per_bit=8192, channels=2) nu_0 = 193.1 nu_1 = 1.2 * nu_0 offset_nu = 0.2 * 193.1 offset = 2.5 / domain.bit_width system = System(domain) system.add(Gaussian(width=1.0, peak_power=1000.0, channel=0)) system.add(Gaussian(width=1.0, peak_power=0.1, channel=1, position=0.5 - offset, offset_nu=offset_nu)) system.add(Fibre('fibre', length=0.2, gamma=[0.1, 0.12], beta=[[0.0, 0.0, 1.0, 0.0], [0.0, 10.0, 1.0, 0.0]], centre_omega=(nu_to_omega(nu_0), nu_to_omega(nu_1)), sim_type='wdm', method='ARK4IP')) system.run() A_fs = system.fields['fibre'] P_t0 = temporal_power(A_fs[0]) P_t1 = temporal_power(A_fs[1]) double_plot(system.domain.t, P_t0, system.domain.t, P_t1, labels["t"], labels["P_t"], labels["t"], labels["P_t"], x_range=(6.0, 14.0), X_range=(5.0, 10.0), filename="7-8")
def __str__(self): """ :return: Information string :rtype: string Output information on Gaussian. """ output_string = [ 'position = {0:f}', 'width = {1:f} ps', 'fwhm = {2:f} ps', 'peak_power = {3:f} W', 'offset_nu = {4:f} THz', 'm = {5:d}', 'C = {6:f}', 'initial_phase = {7:f} rad', 'channel = {8:d}'] return "\n".join(output_string).format( self.position, self.width, self.calculate_fwhm(), self.peak_power, self.offset_nu, self.m, self.C, self.initial_phase, self.channel) if __name__ == "__main__": """ Plot a default Gaussian in temporal and spectral domain """ from pyofss import Domain, System, Gaussian from pyofss import temporal_power, spectral_power from pyofss import double_plot, labels sys = System(Domain(bit_width=500.0)) sys.add(Gaussian()) sys.run() double_plot(sys.domain.t, temporal_power(sys.field), sys.domain.nu, spectral_power(sys.field, True), labels["t"], labels["P_t"], labels["nu"], labels["P_nu"])
from pyofss import Domain, System, Gaussian, Fibre from pyofss import temporal_power, chirp, multi_plot, labels P_ts = [] chirps = [] zs = [0.0, 2.0, 4.0] for z in zs: system = System(Domain(bit_width=200.0, samples_per_bit=2048)) system.add(Gaussian(peak_power=1.0, width=1.0)) system.add(Fibre(length=z, beta=[0.0, 0.0, -1.0, 0.0])) system.run() field = system.fields['fibre'] P_ts.append(temporal_power(field)) temp = [f if abs(f) >= 5e-12 else 0.0 for f in field] chirps.append(chirp(temp, system.domain.window_nu)) multi_plot(system.domain.t, P_ts, zs, labels["t"], labels["P_t"], [r"$z = {0:.0f} \, km$"], (-10.0, 10.0), filename="3-1") multi_plot(system.domain.t, chirps, zs, labels["t"],
but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. """ from pyofss import Domain, System, Gaussian, Fibre from pyofss import temporal_power, multi_plot, labels system = System(Domain(bit_width=200.0, samples_per_bit=2048)) system.add(Gaussian("gaussian", peak_power=1.0, width=1.0)) system.run() P_ts = [temporal_power(system.fields['gaussian'])] fibres = [ Fibre(length=5.0, beta=[0.0, 0.0, 0.0, 1.0], total_steps=100), Fibre(length=5.0, beta=[0.0, 0.0, 1.0, 1.0], total_steps=100) ] for fibre in fibres: system = System(Domain(bit_width=200.0, samples_per_bit=2048)) system.add(Gaussian(peak_power=1.0, width=1.0)) system.add(fibre) system.run() P_ts.append(temporal_power(system.fields['fibre'])) z_labels = [ r"$z = 0 \, km$", r"$z = 5 \, km$, $\beta_2 = \, 0 \, ps / (nm \cdot km)$",
self.width, self.calculate_fwhm(), self.peak_power, self.offset_nu, self.C, self.initial_phase, self.channel, ) if __name__ == "__main__": """ Plot a default Sech in temporal and spectral domain """ from pyofss import Domain, System, Sech from pyofss import temporal_power, spectral_power from pyofss import double_plot, labels sys = System(Domain(bit_width=500.0)) sys.add(Sech()) sys.run() double_plot( sys.domain.t, temporal_power(sys.field), sys.domain.nu, spectral_power(sys.field, True), labels["t"], labels["P_t"], labels["nu"], labels["P_nu"], )