def layout(nbr_channels_seed, peak_powers_seed, center_lambdas_seed, nbr_channels_pump, peak_powers_pump, center_lambdas_pump, REFL_SEED, REFL_PUMP, NOISE): nbr_ch_seed = nbr_channels_seed // 2 gssn = Gaussian(channels=nbr_ch_seed, peak_power=peak_powers_seed[:nbr_ch_seed], center_lambda=center_lambdas_seed[:nbr_ch_seed]) gssn_ = Gaussian(channels=(nbr_channels_seed - nbr_ch_seed), peak_power=peak_powers_seed[nbr_ch_seed:], center_lambda=center_lambdas_seed[nbr_ch_seed:]) nbr_ch_pump = nbr_channels_pump // 2 pump = CW(channels=nbr_ch_pump, peak_power=peak_powers_pump[:nbr_ch_pump], center_lambda=center_lambdas_pump[:nbr_ch_pump]) pump_ = CW(channels=(nbr_channels_pump - nbr_ch_pump), peak_power=peak_powers_pump[nbr_ch_pump:], center_lambda=center_lambdas_pump[nbr_ch_pump:]) fiber = FiberYb(length=0.01, steps=10, REFL_SEED=REFL_SEED, REFL_PUMP=REFL_PUMP, BISEED=True, BIPUMP=True, save_all=True, alpha=[0.05], max_nbr_iter=2, NOISE=NOISE) lt = Layout(Domain(samples_per_bit=64, noise_samples=10)) lt.add_unidir_links((gssn[0], fiber[0]), (pump[0], fiber[2]), (gssn_[0], fiber[1]), (pump_[0], fiber[3])) lt.run_all() return fiber.storages[0]
def test_load_field(): """Should fail if the saved fields are not the same as the loaded fields. """ lt = Layout() gssn_1 = Gaussian(channels=1, width=[5.0]) field_saver_1 = SaveField() gssn_2 = Gaussian(channels=1, width=[10.0]) field_saver_2 = SaveField() lt.add_links((gssn_1[0], field_saver_1[0]), (gssn_2[0], field_saver_2[0])) lt.run(gssn_1, gssn_2) fields = field_saver_1.fields + field_saver_2.fields lt_ = Layout() load_field = LoadField(fields=fields) lt.run(load_field) fields = load_field[0].fields assert (fields[0] == gssn_1[0].fields[0]) assert (fields[1] == gssn_2[0].fields[0]) assert_array_equal(fields[0].channels, gssn_1[0].fields[0].channels) assert_array_equal(fields[1].channels, gssn_2[0].fields[0].channels) assert_array_equal(fields[0].noise, gssn_1[0].fields[0].noise) assert_array_equal(fields[1].noise, gssn_2[0].fields[0].noise) assert_array_equal(fields[0].delays, gssn_1[0].fields[0].delays) assert_array_equal(fields[1].delays, gssn_2[0].fields[0].delays)
def test_output_combiner_no_combine(nbr_channels, ratios): """Should fail if the output temporal power division does not correpsond to the dividing ratios. """ # Environment creation gssns = [] nbr_arms = len(ratios) base_power = 10.0 for i in range(nbr_arms): gssns.append( Gaussian(channels=nbr_channels, save=True, peak_power=[(i + 1) * base_power for i in range(nbr_channels)])) combiner = IdealCombiner(arms=nbr_arms, ratios=ratios, save=True, combine=False) lt = Layout() for i in range(nbr_arms): lt.add_link(gssns[i][0], combiner[i]) lt.run_all() # Testing init_fields = [] for i in range(0, nbr_arms): init_fields.extend(gssns[i][0].fields) output_fields = combiner[nbr_arms].fields assert len(output_fields) == nbr_arms assert len(output_fields) == len(init_fields) for i in range(len(output_fields)): for j in range(len(output_fields[i])): # Taking into account rounding errors power_1 = ratios[i] * temporal_power(init_fields[i][j]) power_2 = temporal_power(output_fields[i][j]) assert_array_almost_equal(power_1, power_2, 10)
def test_constraint_port_valid(): r"""Should fail if the propagating field can enter the dummy_comp. Notes ----- Test case:: Gaussian_1 ______ Combiner """ # Environment creations class DummyPassComp(AbstractPassComp): def __init__(self): super().__init__('', '', [cst.ELEC_IN, cst.ELEC_OUT], True) def __call__(self, domain, ports, fields): return ([1 for i in range(len(fields))], fields) lt = Layout() gssn = Gaussian() dummy = DummyPassComp() lt.add_link(gssn[0], dummy[0]) lt.run(gssn) # Testing pytest.warns(PortValidWarning, lt.run, gssn)
def test_output_divider(nbr_channels, ratios): """Should fail if the output temporal power division does not correpsond to the dividing ratios. """ # Environment creation base_power = 10.0 gssn = Gaussian(channels=nbr_channels, save=True, peak_power=[(i + 1) * base_power for i in range(nbr_channels)]) nbr_arms = len(ratios) divider = IdealDivider(arms=nbr_arms, ratios=ratios, save=True) lt = Layout() lt.add_link(gssn[0], divider[0]) lt.run_all() # Testing init_power = temporal_power(gssn[0][0].channels) for i in range(nbr_arms): assert len(divider[i + 1]) == 1 arm_power = temporal_power(divider[i + 1][0].channels) assert len(arm_power) == len(init_power) for j in range(len(arm_power)): # Taking into account rounding errors assert_array_almost_equal((ratios[i] * init_power[j]), arm_power[j], 10)
def test_constraint_coprop(): r"""Should fail if the copropagating fields are not waiting for each others. Notes ----- Test case:: [0] _________ [1] __________\ [2] ___________\__ Combiner ___ Dummy Comp __ check output [3] ___________/ ... [n-1] _________/ """ lt = Layout() nbr_sig = 5 combiner = IdealCombiner(arms=nbr_sig, combine=False) for i in range(nbr_sig): lt.add_link(Gaussian()[0], combiner[i]) dummy_comp = IdealAmplifier(save=True) lt.add_link(combiner[nbr_sig], dummy_comp[0]) lt.run_all() assert (len(dummy_comp[1]) == nbr_sig)
def layout(split_noise_option, channels, peak_powers, center_lambdas): gssn = Gaussian(channels=channels, peak_power=peak_powers, center_lambda=center_lambdas, save=True) pump = CW(channels=3, peak_power=[0.01], center_lambda=[976.]) fiber = FiberYb(length=0.01, split_noise_option=split_noise_option, steps=10, REFL_SEED=True, REFL_PUMP=True, BISEED=False, BIPUMP=False, save=True, PROP_REFL=True, PROP_PUMP=True, alpha=[0.05], max_nbr_iter=2, NOISE=True) lt = Layout(Domain(samples_per_bit=64, noise_samples=10)) lt.add_unidir_links((gssn[0], fiber[0]), (pump[0], fiber[2])) lt.run_all() noise_power_input = fiber[0].fields[0].noise noise_power_seed = fiber[1].fields[0].noise noise_power_refl_seed = fiber[0].fields[1].noise noise_power_pump = fiber[1].fields[1].noise noise_power_refl_pump = fiber[0].fields[2].noise return (noise_power_input, noise_power_seed, noise_power_refl_seed, noise_power_pump, noise_power_refl_pump)
def layout(channels, peak_powers, widths, center_lambdas): domain = Domain(bit_width=100.0, samples_per_bit=2048) lt = Layout(domain) dtime = domain.dtime out_temporal_power = [] out_spectral_power = [] gssn = Gaussian(channels=channels, peak_power=peak_powers, width=widths, center_lambda=center_lambdas) for i in range(2): if (i): cfg.RK4IP_OPTI_GNLSE = True else: cfg.RK4IP_OPTI_GNLSE = False fiber = Fiber(length=0.2, nlse_method='rk4ip', alpha=[0.5], beta_order=3, nl_approx=False, ATT=True, DISP=True, SPM=True, XPM=True, SS=True, RS=True, steps=1000, save=True) lt.add_link(gssn[0], fiber[0]) lt.run(gssn) lt.reset() out_temporal_power.append(temporal_power(fiber[1][0].channels)) out_spectral_power.append(spectral_power(fiber[1][0].channels)) return (out_temporal_power, out_spectral_power)
def amplitude_transfer_function(nu: np.ndarray, center_nu: float, nu_bw: float, offset_nu: float = .0, order: int = 1): """The transfer function of the flat top window. Parameters ---------- nu : The frequency components. :math:`[ps^{-1}]` center_nu : The center frequency. :math:`[ps^{-1}]` nu_bw : The spectral bandwith. :math:`[ps^{-1}]` nu_offset : The offset frequency. :math:`[ps^{-1}]` """ nu_bw = Gaussian.fwhm_to_width(nu_bw, order) delta_nu = nu - offset_nu - center_nu window = np.zeros(delta_nu.shape, dtype=complex) arg = np.power(delta_nu / nu_bw, 2 * order) window = np.exp(-0.5 * arg) return window
def test_wrong_start_comp_for_run(): """Should fail if the layout is start with a wrong component.""" # Environment creations start = Gaussian() a = IdealCoupler() layout = Layout() layout.add_link(a[0], start[0]) # Testing pytest.raises(StartSimError, layout.run, a)
def test_dispersion(fiber_layout): """Should fail if the output fwhm not more than initial pulse.""" # Make sure to have a positive GVD to pass the test gssn = Gaussian(channels=1, peak_power=[1.0], center_lambda=[1030.]) outputs = fiber_layout(gssn, False, True, False, False, False, False) in_temporal_fwhm = outputs[2] out_temporal_fwhm = outputs[6] for i in range(len(out_temporal_fwhm)): assert in_temporal_fwhm[0] < out_temporal_fwhm[i][0]
def test_spm(fiber_layout): """Should fail if the fwhm of the output spectral powers are more than the initial pulse.""" # Make sure to have a positive GVD to pass the test gssn = Gaussian(channels=1, peak_power=[1.0], center_lambda=[1030.]) outputs = fiber_layout(gssn, False, False, True, False, False, False) in_spectral_fwhm = outputs[3] out_spectral_fwhm = outputs[7] for i in range(len(out_spectral_fwhm)): assert in_spectral_fwhm[0] < out_spectral_fwhm[i][0]
def test_save_field_to_file(): """Should fail if the saved fields are not the same as the loaded fields. """ file_name = 'temp_file_for_save_field_to_file_test.pk1' if (os.path.isfile(file_name)): print("Can not perfom test because a file named '{}' already exist." .format(file_name)) assert False else: lt = Layout() gssn_1 = Gaussian(channels=1, width=[5.0], save=True) field_saver_1 = SaveFieldToFile(file_name=file_name, add_fields=False) gssn_2 = Gaussian(channels=1, width=[10.0], save=True) field_saver_2 = SaveFieldToFile(file_name=file_name, add_fields=True) lt.add_links((gssn_1[0], field_saver_1[0]), (gssn_2[0], field_saver_2[0])) lt.run(gssn_1, gssn_2) lt_ = Layout() load_field = LoadFieldFromFile(file_name=file_name) lt_.run(load_field) fields = load_field[0].fields # Removing created file os.remove(file_name) # Tests assert (fields[0] == gssn_1[0].fields[0]) assert (fields[1] == gssn_2[0].fields[0]) assert_array_equal(fields[0].channels, gssn_1[0].fields[0].channels) assert_array_equal(fields[1].channels, gssn_2[0].fields[0].channels) assert_array_equal(fields[0].noise, gssn_1[0].fields[0].noise) assert_array_equal(fields[1].noise, gssn_2[0].fields[0].noise) assert_array_equal(fields[0].delays, gssn_1[0].fields[0].delays) assert_array_equal(fields[1].delays, gssn_2[0].fields[0].delays)
def test_wrong_pass_comp_ports(): """Should fail if the right error is not raised.""" # Environment creations class DummyPassComp(AbstractPassComp): def __init__(self): super().__init__('', '', [cst.OPTI_ALL, cst.OPTI_ALL], True) def __call__(self, domain, ports, fields): return ([5 for i in range(len(fields))], fields) start = Gaussian() pass_comp = DummyPassComp() a = IdealCoupler() layout = Layout() layout.add_links((start[0], pass_comp[0]), (pass_comp[1], a[0])) # Testing pytest.warns(WrongPortWarning, layout.run, start)
def test_no_divide_output(): """Should fail if the incoming fields are not the same as the output fields at each arm. """ nbr_channels = 10 arms = 5 gssn = Gaussian(channels=nbr_channels, save=True) divider = IdealDivider(arms=arms, divide=False, save=True) lt = Layout() lt.add_link(gssn[0], divider[0]) lt.run_all() # Testing init_power = temporal_power(gssn[0][0].channels) for i in range(arms): assert len(divider[i + 1]) == 1 arm_power = temporal_power(divider[i + 1][0].channels) assert len(arm_power) == len(init_power) for j in range(len(arm_power)): assert_array_equal(arm_power[j], init_power[j])
def test_combine_output_diff_omega_and_rep_freq(nbr_channels, ratios): """Should fail if the different omega and repetition frequencies are added to each other. """ # Environment creation back_up_flag_omega = cfg.get_field_op_matching_omega() back_up_flag_rep_freq = cfg.get_field_op_matching_rep_freq() cfg.set_field_op_matching_omega(True) cfg.set_field_op_matching_rep_freq(True) gssns = [] nbr_arms = len(ratios) base_power = 10.0 for i in range(nbr_arms): gssns.append( Gaussian(channels=nbr_channels, save=True, peak_power=[(j + 1) * base_power for j in range(nbr_channels)], center_lambda=[(1500. + j) * (i + 1) for j in range(nbr_channels)], rep_freq=[(1e-2 + (j * 1e-4)) * (i + 1) for j in range(nbr_channels)])) combiner = IdealCombiner(arms=nbr_arms, ratios=ratios, save=True, combine=True) lt = Layout() for i in range(nbr_arms): lt.add_link(gssns[i][0], combiner[i]) lt.run_all() lt.reset() # Testing init_fields = [] for i in range(0, nbr_arms): init_fields.extend(gssns[i][0].fields) output_fields = combiner[nbr_arms].fields assert len(output_fields) == 1 assert len(output_fields[0]) == (nbr_channels * nbr_arms) # Reset cfg.set_field_op_matching_omega(back_up_flag_omega) cfg.set_field_op_matching_rep_freq(back_up_flag_rep_freq)
def layout(nbr_channels_seed, peak_powers_seed, center_lambdas_seed, nbr_channels_pump, peak_powers_pump, center_lambdas_pump, REFL_SEED, REFL_PUMP, NOISE): gssn = Gaussian(channels=nbr_channels_seed, peak_power=peak_powers_seed, center_lambda=center_lambdas_seed) pump = CW(channels=nbr_channels_pump, peak_power=peak_powers_pump, center_lambda=center_lambdas_pump) fiber = FiberYb(length=0.01, steps=10, REFL_SEED=REFL_SEED, REFL_PUMP=REFL_PUMP, BISEED=False, BIPUMP=False, save_all=True, alpha=[0.05], max_nbr_iter=2, NOISE=NOISE) lt = Layout(Domain(samples_per_bit=64, noise_samples=10)) lt.add_unidir_links((gssn[0], fiber[0]), (pump[0], fiber[2])) lt.run_all() return fiber.storages[0]
def test_constraint_waiting(): r"""Should fail if the component is not waiting for other fields. Notes ----- Test case:: [0] _________ [1] __________\ [2] ___________\__ Combiner __ check output [3] ___________/ ... [n-1] _________/ """ lt = Layout() nbr_sig = 5 combiner = IdealCombiner(arms=nbr_sig, combine=False, save=True) for i in range(nbr_sig): lt.add_link(Gaussian()[0], combiner[i]) lt.run_all() assert (len(combiner[nbr_sig]) == nbr_sig)
from optcom.field import Field plot_groups: List[int] = [] line_labels: List[Optional[str]] = [] plot_titles: List[str] = [] x_datas: List[np.ndarray] = [] y_datas: List[np.ndarray] = [] ode_methods: List[str] = ["euler", "rk1", "rk2", "rk3", "rk4"] # ---------------- NLSE solvers test ------------------------------- lt: Layout = Layout() Lambda: float = 1030.0 pulse: Gaussian = Gaussian(channels=1, peak_power=[1.0, 1.0], center_lambda=[Lambda]) steps: int = int(1e1) beta_01: float = 1e5 beta_02: float = 1e5 beta: List[Union[List[float], Callable, None]] =\ [[beta_01,10.0,-0.0],[beta_02,10.0,-0.0]] v_nbr_value = 2.0 v_nbr: List[Union[float, Callable, None]] = [v_nbr_value] core_radius: List[float] = [5.0] c2c_spacing: List[List[float]] = [[15.0]] n_clad: float = 1.02 omega: float = Domain.lambda_to_omega(Lambda) kappa_: Union[float, Callable] kappa_ = CouplingCoeff.calc_kappa(omega, v_nbr_value, core_radius[0],
nlse2 = NLSE(alpha=[0.0], beta=[0.0, 0.0]) nlse3 = NLSE(alpha=[0.0], beta=[0.0, 0.0]) nlse4 = NLSE(alpha=[0.0], beta=[0.0, 0.0]) nlse5 = NLSE(alpha=[0.0], beta=[0.0, 0.0]) eqs = [nlse1, nlse2, nlse3, nlse4, nlse5] method = [ 'ssfm', 'ssfm_super_sym', 'ssfm_symmetric', 'ssfm_reduced', 'ssfm' ] length = 1.0 steps = [10, 9, 8, 7, 6, 5] step_method = [FIXED, ADAPTATIVE, FIXED] solver_sequence = [0, 0, 2, 2, 1] solver_order = ALTERNATING stepper_method = [FORWARD] stepper = Stepper(eqs=eqs, method=method, length=length, steps=steps, step_method=step_method, solver_sequence=solver_sequence, solver_order=solver_order, stepper_method=stepper_method) dummy_domain = Domain() ggsn = Gaussian(channels=4) dummy_ports, dummy_field = ggsn(dummy_domain) res = stepper(dummy_domain, dummy_field)
in_spectral_power = spectral_power(starter[0][0].channels) in_temporal_fwhm = fwhm(in_temporal_power, dtime) in_spectral_fwhm = fwhm(in_spectral_power, domega) return (in_temporal_power, in_spectral_power, in_temporal_fwhm, in_spectral_fwhm, out_temporal_power, out_spectral_power, out_temporal_fwhm, out_spectral_fwhm) return layout # ---------------------------------------------------------------------- # Tests ---------------------------------------------------------------- # ---------------------------------------------------------------------- gssn_1_ch = Gaussian(channels=1, peak_power=[1.0]) gssn_2_ch = Gaussian(channels=2, peak_power=[1.0, 0.5]) @pytest.mark.equations @pytest.mark.parametrize("starter, ATT, DISP, SPM, SS, RS, approx", [(gssn_1_ch, True, False, False, False, False, False), (gssn_1_ch, True, True, False, False, False, False), (gssn_1_ch, True, True, True, False, False, False), (gssn_1_ch, True, True, True, True, False, True), (gssn_1_ch, True, True, True, False, True, True), (gssn_1_ch, True, True, True, True, True, True), (gssn_2_ch, True, False, False, False, False, False), (gssn_2_ch, True, True, False, False, False, False), (gssn_2_ch, True, True, True, False, False, False), (gssn_2_ch, True, True, True, True, False, True),
def test_interplay_dispersion_and_spm(): """Should fail if (i) fwhm of temporal output powers for small N square number is greater than fwhm of initial pulse or (ii) fwhm of temporal output powers for big N square number is greater than initial fwhm in case of positive GVD and smaller than initial fwhm in case of negative GVD. Notes ----- Test case:: gssn ___ fiber """ # Environment creation domain = Domain() lt = Layout(domain) dtime = domain.dtime fwhms_pos_gvd = [] fwhms_neg_gvd = [] time_pos_gvd = [] time_neg_gvd = [] nlse_method = "ssfm_symmetric" # Make sure to have a positive GVD to pass the test gssn = Gaussian(channels=1, peak_power=[1.0], center_lambda=[1030.]) flag = True for i in range(1, 5): if (i == 4): flag = False fiber = Fiber(length=1.0, nlse_method=nlse_method, alpha=[0.46], gamma=2.0, nl_approx=flag, ATT=False, DISP=True, SPM=True, SS=False, RS=False, steps=1000, save=True, approx_type=i, beta=[1e5, 1e3, 20.0]) lt.add_link(gssn[0], fiber[0]) lt.run(gssn) lt.reset() time_pos_gvd.append(fiber[1][0].time) fwhms_pos_gvd.append(fwhm(temporal_power(fiber[1][0].channels), dtime)) flag = True for i in range(1, 5): if (i == 4): flag = False fiber = Fiber(length=1.0, nlse_method=nlse_method, alpha=[0.46], gamma=2.0, nl_approx=flag, ATT=False, DISP=True, SPM=True, SS=False, RS=False, steps=1000, save=True, approx_type=i, beta=[1e5, 1e3, -20.0]) lt.add_link(gssn[0], fiber[0]) lt.run(gssn) lt.reset() time_neg_gvd.append(fiber[1][0].time) fwhms_neg_gvd.append(fwhm(temporal_power(fiber[1][0].channels), dtime)) fwhm_temporal_gssn = fwhm(temporal_power(gssn[0][0].channels), dtime) time_gssn = gssn[0][0].time # Testing for i in range(len(fwhms_neg_gvd)): assert_array_less(time_gssn, time_pos_gvd[i]) assert_array_less(time_gssn, time_neg_gvd[i]) assert fwhm_temporal_gssn[0] < fwhms_pos_gvd[i][0] assert fwhms_neg_gvd[i][0] < fwhm_temporal_gssn[0]