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_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_already_unidir_linked(): """Should fail if component's port is already linked.""" # Environment creation a = IdealCoupler(name='a') b = IdealCoupler(name='b') c = IdealCoupler(name='c') layout = Layout() layout.add_unidir_link(a[1], b[0]) # Testing pytest.raises(LinkError, layout.add_unidir_links, (c[1], b[0])) pytest.raises(LinkError, layout.add_unidir_links, (b[0], c[2])) pytest.raises(LinkError, layout.add_unidir_links, (a[1], b[0]))
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 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 test_leafs(): """Should be specific leafs for specific tree.""" # Environment creation a = IdealCoupler(name='a') b = IdealCoupler(name='b') c = IdealCoupler(name='c') d = IdealCoupler(name='d') e = IdealCoupler(name='e') f = IdealCoupler(name='f') layout = Layout() layout.add_links((a[0], b[0]), (a[1], c[0]), (b[1], d[0]), (b[2], e[0])) # Testing assert layout.get_leafs_of_comps([a, b, c, d, e, f]) == [c, d, e] assert layout.leafs == [c, d, e]
def test_link_unidir_to_itself(): """Should fail if component's port tries to link to itself.""" # Environment creation a = IdealCoupler(name='a') layout = Layout() # Testing pytest.raises(LinkError, layout.add_unidir_links, (a[1], a[1])) pytest.raises(LinkError, layout.add_unidir_links, (a[1], a[0]))
def test_wrong_start_comp_ports(): """Should fail if the right warning is not shown.""" # Environment creation class DummyStartComp(AbstractStartComp): def __init__(self): super().__init__('', '', [cst.OPTI_ALL], True) def __call__(self, domain): return ([4], [Field(Domain(), 1)]) start = DummyStartComp() a = IdealCoupler(name='a') layout = Layout() layout.add_link(start[0], a[0]) # Testing pytest.warns(WrongPortWarning, layout.run, start)
def test_wrong_start_comp_output(): """Should fail if the right error is not raised.""" # Environment creation class DummyStartComp(AbstractStartComp): def __init__(self): super().__init__('', '', [cst.OPTI_ALL], True) def __call__(self, domain): return ([0, 0], [Field(Domain(), 1)]) start = DummyStartComp() a = IdealCoupler(name='a') layout = Layout() layout.add_link(start[0], a[0]) # Testing pytest.raises(PropagationError, layout.run, start)
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_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_non_existant_link_del(): """Should raise error if trying to delete a non-existing link.""" # Environment creation a = IdealCoupler(name='a') b = IdealCoupler(name='b') c = IdealCoupler(name='c') layout = Layout() layout.add_links((a[1], b[0]), (a[2], b[1])) layout.add_unidir_links((b[2], c[2]), (a[0], c[1])) layout.del_link(c[2], b[2]) # Valid (even if unidir in other dir.) # Testing pytest.raises(DelError, layout.del_links, (c[2], b[2])) pytest.raises(DelError, layout.del_links, (a[1], b[1])) pytest.raises(DelError, layout.del_links, (a[2], c[2]))
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(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_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_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 layout(starter, ATT, DISP, SPM, SS, RS, approx): domain = Domain() lt = Layout(domain) dtime = domain.dtime domega = domain.domega out_temporal_power = [] out_spectral_power = [] out_temporal_fwhm = [] out_spectral_fwhm = [] nlse_method = "ssfm_symmetric" flag = True till_nbr = 4 if approx else 5 for i in range(1, till_nbr): if (i == 4): flag = False fiber = Fiber(length=1.0, nlse_method=nlse_method, alpha=[0.046], nl_approx=flag, ATT=ATT, DISP=DISP, SPM=SPM, SS=SS, RS=RS, steps=1000, save=True, approx_type=i) lt.add_link(starter[0], fiber[0]) lt.run(starter) lt.reset() out_temporal_power.append(temporal_power(fiber[1][0].channels)) out_spectral_power.append(spectral_power(fiber[1][0].channels)) out_temporal_fwhm.append(fwhm(out_temporal_power[-1], dtime)) out_spectral_fwhm.append(fwhm(out_spectral_power[-1], domega)) in_temporal_power = temporal_power(starter[0][0].channels) 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)
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 test_degree(): """Should be of a specific degree for specific tree.""" # Environment creation a = IdealCoupler(name='a') b = IdealCoupler(name='b') c = IdealCoupler(name='c') d = IdealCoupler(name='d') e = IdealCoupler(name='e') f = IdealCoupler(name='f') layout = Layout() layout.add_links((a[0], b[0]), (a[1], c[0]), (b[1], d[0]), (b[2], e[0]), (c[1], a[2])) # Testing assert layout.get_degree(a) == 3 assert layout.get_degree(b) == 3 assert layout.get_degree(c) == 2 assert layout.get_degree(d) == 1 assert layout.get_degree(e) == 1 assert layout.get_degree(f) == 0
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 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.domain import Domain from optcom.effects.coupling import Coupling from optcom.layout import Layout from optcom.parameters.fiber.coupling_coeff import CouplingCoeff 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]]
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]