Some more cleanups, especially tests

This commit is contained in:
Benoît Sierro
2023-07-24 14:28:41 +02:00
parent 41072ea1f2
commit be0a9b8c20
71 changed files with 142 additions and 1529 deletions

View File

@@ -1,18 +0,0 @@
name = "PM2000D"
mean_power = 0.23
field_file = "Pos30000New.npz"
repetition_rate = 40e6
wavelength = 1546e-9
dt = 1e-15
t_num = 8192
tolerated_error = 1e-6
quantum_noise = true
raman_type = "agrawal"
z_num = 128
length = 0.3
dispersion_file = "PM2000D_2 extrapolated 4 0.npz"
interpolation_degree = 12
A_eff_file = "PM2000D_A_eff_marcuse.npz"
n2 = 4.5e-20

Binary file not shown.

View File

@@ -1,70 +0,0 @@
"""
May 2023
Testing the new solver / operators structure
using parameters from the 2022 Optica paper
"""
import matplotlib.pyplot as plt
import numpy as np
from scipy.interpolate import interp1d
import scgenerator as sc
import scgenerator.math as math
import scgenerator.physics.units as units
import scgenerator.plotting as plot
import scgenerator.solver as sol
def main():
params = sc.Parameters(**sc.open_single_config("./tests/Optica_PM2000D/Optica_PM2000D.toml"))
# print(params.nonlinear_operator)
# print(params.compute("dispersion_op"))
# print(params.linear_operator)
# print(params.spec_0)
# print(params.compute("gamma_op"))
#
# plt.plot(params.w, params.linear_operator(0).imag)
# plt.show()
breakpoint()
res = sol.integrate(
params.spec_0, params.length, params.linear_operator, params.nonlinear_operator
)
new_z = np.linspace(0, params.length, 256)
specs2 = math.abs2(res.spectra)
specs2 = units.to_WL(specs2, params.l)
x = params.l
# x = units.THz.inv(w)
# new_x = np.linspace(100, 2200, 1024)
new_x = np.linspace(800e-9, 2000e-9, 1024)
solution = interp1d(res.z, specs2, axis=0)(new_z)
solution = interp1d(x, solution)(new_x)
solution = units.to_log2D(solution)
plt.imshow(
solution,
origin="lower",
aspect="auto",
extent=plot.get_extent(1e9 * new_x, new_z * 1e2),
vmin=-30,
)
plt.show()
fields = np.fft.irfft(res.spectra)
solution = math.abs2(fields)
solution = interp1d(res.z, solution, axis=0)(new_z)
solution.T[:] /= solution.max(axis=1)
plt.imshow(
solution,
origin="lower",
aspect="auto",
extent=plot.get_extent(params.t * 1e15, new_z * 1e2),
)
plt.show()
if __name__ == "__main__":
main()

View File

@@ -1,15 +0,0 @@
name = "Travers"
repetition_rate = 1e3
wavelength = 800e-9
tolerated_error = 1e-6
z_num = 128
t_num = 4096
dt = 50e-18
core_radius = 125e-6
gas_name = "helium"
pressure = 400e2
energy = 0.4e-3
width = 10e-15
length = 3
full_field = true
shape = "sech"

View File

@@ -1,69 +0,0 @@
"""
Testing the the solver / operator mechanism with
parameters from the 2019 Travers paper
"""
import matplotlib.pyplot as plt
import numpy as np
from scipy.interpolate import interp1d
import scgenerator as sc
import scgenerator.math as math
import scgenerator.physics.units as units
import scgenerator.plotting as plot
import scgenerator.solver as sol
def main():
params = sc.Parameters(**sc.open_single_config("./tests/Travers/Travers.toml"))
# print(params.nonlinear_operator)
# print(params.compute("dispersion_op"))
# print(params.linear_operator)
# print(params.spec_0)
# print(params.compute("gamma_op"))
#
# plt.plot(params.w, params.linear_operator(0).imag)
# plt.show()
breakpoint()
res = sol.integrate(
params.spec_0, params.length, params.linear_operator, params.nonlinear_operator
)
new_z = np.linspace(0, params.length, 256)
specs2 = math.abs2(res.spectra)
specs2 = units.to_WL(specs2, params.l)
x = params.l
# x = units.THz.inv(w)
# new_x = np.linspace(100, 2200, 1024)
new_x = np.linspace(100e-9, 1200e-9, 1024)
solution = interp1d(res.z, specs2, axis=0)(new_z)
solution = interp1d(x, solution)(new_x)
solution = units.to_log2D(solution)
plt.imshow(
solution,
origin="lower",
aspect="auto",
extent=plot.get_extent(1e9 * new_x, new_z * 1e2),
vmin=-30,
)
plt.show()
fields = np.fft.irfft(res.spectra)
solution = math.abs2(fields)
solution = interp1d(res.z, solution, axis=0)(new_z)
solution.T[:] /= solution.max(axis=1)
plt.imshow(
solution,
origin="lower",
aspect="auto",
extent=plot.get_extent(params.t * 1e15, new_z * 1e2),
)
plt.show()
if __name__ == "__main__":
main()

View File

@@ -1,31 +0,0 @@
import numpy as np
import pytest
from scgenerator.operators import SimulationState
def test_creation():
x = np.linspace(0, 1, 128, dtype=complex)
cs = SimulationState(1.0, 0, 0.1, x, 1.0)
assert cs.converter is np.fft.ifft
assert cs.stats == {}
assert np.allclose(cs.field2, np.abs(np.fft.ifft(x)) ** 2)
with pytest.raises(ValueError):
cs = SimulationState(1.0, 0, 0.0, x, 1.0, spectrum2=np.abs(x) ** 3)
cs = SimulationState(1.0, 0, 0.1, x, 1.0, spectrum2=x.copy(), field=x.copy(), field2=x.copy())
assert np.allclose(cs.spectrum2, cs.spectrum)
assert np.allclose(cs.spectrum, cs.field)
assert np.allclose(cs.field, cs.field2)
def test_copy():
x = np.linspace(0, 1, 128, dtype=complex)
start = SimulationState(1.0, 0, 0.1, x, 1.0)
end = start.copy()
assert start.spectrum is not end.spectrum
assert np.all(start.field2 == end.field2)

87
tests/test_integrator.py Normal file
View File

@@ -0,0 +1,87 @@
import matplotlib.pyplot as plt
import numpy as np
import pytest
import scgenerator as sc
import scgenerator.operators as op
def test_rk43_absorbtion_only():
n = 129
w_c = np.linspace(-5, 5, n)
spec0 = np.exp(-(w_c**2))
lin = op.envelope_linear_operator(
op.constant_quantity(np.zeros(n)),
op.constant_quantity(np.ones(n) * np.log(2)),
)
non_lin = op.no_op_freq(n)
res = sc.integrate(spec0, 1.0, lin, non_lin, targets=[1.0])
assert np.max(sc.abs2(res.spectra[-1])) == pytest.approx(0.5)
def test_rk43_soliton(plot=False):
"""
create a N=3 soliton and test that the spectrum at after one oscillation goes back to the same
maximum value
"""
n = 1024
l0 = 835e-9
w0 = sc.units.m(l0)
b2 = sc.fiber.D_to_beta2(sc.units.D_ps_nm_km(24), l0)
gamma = 0.08
t0_fwhm = 50e-15
p0 = 1.26e3
t0 = sc.pulse.width_to_t0(t0_fwhm, "sech")
soliton_num = 3
p0 = soliton_num**2 * np.abs(b2) / (gamma * t0**2)
disp_len = t0**2 / np.abs(b2)
end = disp_len * 0.5 * np.pi
targets = np.linspace(0, end, 64)
t = np.linspace(-200e-15, 200e-15, n)
w_c = np.pi * 2 * np.fft.fftfreq(n, t[1] - t[0])
field0 = sc.pulse.sech_pulse(t, t0, p0)
spec0 = np.fft.fft(field0)
no_op = op.no_op_freq(n)
lin = op.envelope_linear_operator(
op.constant_polynomial_dispersion([b2], w_c),
op.constant_quantity(np.zeros(n)),
)
non_lin = op.envelope_nonlinear_operator(
op.constant_quantity(np.ones(n) * gamma),
op.constant_quantity(np.zeros(n)),
op.envelope_spm(0),
no_op,
)
res = sc.integrate(spec0, end, lin, non_lin, targets=targets, atol=1e-10, rtol=1e-9)
if plot:
x, y, z = sc.plotting.transform_2D_propagation(
res.spectra,
sc.PlotRange(500, 1300, "nm"),
w_c + w0,
targets,
)
plt.imshow(z, extent=sc.plotting.get_extent(x, y), origin="lower", aspect="auto", vmin=-40)
plt.show()
plt.plot(sc.abs2(spec0))
plt.plot(sc.abs2(res.spectra[-1]))
plt.yscale("log")
plt.show()
assert sc.abs2(spec0).max() == pytest.approx(sc.abs2(res.spectra[-1]).max(), rel=0.01)
def benchmark():
for _ in range(50):
test_rk43_soliton()
if __name__ == "__main__":
test_rk43_soliton()
benchmark()

View File

@@ -0,0 +1,17 @@
from pathlib import Path
import numpy as np
from scgenerator.solver import SimulationResult
def test_load_save(tmp_path: Path):
sim = SimulationResult(
np.random.randint(0, 20, (5, 5)), dict(a=[], b=[1, 2, 3], z=list(range(32)))
)
sim.save(tmp_path / "mysim")
sim2 = SimulationResult.load(tmp_path / "mysim.zip")
assert np.all(sim2.spectra == sim.spectra)
assert np.all(sim2.z == sim.z)
for k, v in sim.stats.items():
assert sim2.stats[k] == v