From ecffd9d8d9fc79cfe709be37539cba375cec884b Mon Sep 17 00:00:00 2001 From: Edoardo Vergerio Date: Mon, 7 Sep 2026 13:42:16 +0200 Subject: [PATCH] scripts for testing usage of the mpid class. Shaping the inverse design algorithm into a class has proven useful to streamline the design process. Being more easy to handle, one can now test various devices with very little setup effort. --- testing.py => testing_converter.py | 1 + testing_mode_sorter.py | 86 ++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) rename testing.py => testing_converter.py (99%) create mode 100644 testing_mode_sorter.py diff --git a/testing.py b/testing_converter.py similarity index 99% rename from testing.py rename to testing_converter.py index 750c9f6..95b2c5f 100644 --- a/testing.py +++ b/testing_converter.py @@ -24,6 +24,7 @@ space_grid = pid.SpaceGrid( unit_cell_pitch = unit_cell_pitch, prop_distances=d, materials_refr_index=mat_refr_index, + xy_symmetry=True, ) #define the source diff --git a/testing_mode_sorter.py b/testing_mode_sorter.py new file mode 100644 index 0000000..1672f7e --- /dev/null +++ b/testing_mode_sorter.py @@ -0,0 +1,86 @@ +import mpid as pid +import numpy as np +import os +if not os.path.exists("results"): + os.makedirs("results") + +#Space grid definition +N = 1025 +unit_cell_pitch = 700e-9 + +n0 = 1.45 +n1 = 1.0 +n2 = 1.45 +mat_refr_index = np.array([n0, n1, n2]) + +d0 = 476e-6 #propagation distance: source - MS1 [m] +d1 = 510e-6 #propagation distance: MS1 - MS2 [m] +d2 = d0 #propagation distance: MS2 - target [m] +d = np.array([d0, d1, d2]) + +ms_max_available_diameter = 122e-6 #[um] +output_fiber_core_diameter = 19e-6 #[um] +opt_max_eval = 140 + +space_grid = pid.SpaceGrid( + side_pixels=N, + unit_cell_pitch = unit_cell_pitch, + prop_distances=d, + materials_refr_index=mat_refr_index, +) + +wavelength = 1.55e-6 + +#define the sources +source1 = pid.Source(input_field='fiber_modes/LP11_field_distribution_lda_1550_N_1025.npy', + wavelength=wavelength, + space_grid=space_grid, + field_norm=True) +source2 = pid.Source(input_field='fiber_modes/LP21_field_distribution_lda_1550_N_1025.npy', + wavelength=wavelength, + space_grid=space_grid, + field_norm=True) + + +target = pid.Target(input_field='fiber_modes/LP01_field_distribution_lda_1550_N_1025.npy', + center=(20e-6,0), + associated_source=source1, + wavelength=wavelength, + space_grid=space_grid, + field_norm=True) +target2 = pid.Target(input_field='fiber_modes/LP01_field_distribution_lda_1550_N_1025.npy', + center=(-20e-6,0), + associated_source=source2, + wavelength=wavelength, + space_grid=space_grid, + field_norm=True) + +#make a plot of sources and targets + +target.plot2D(plot_zoom_x=100, plot_zoom_y=100, choose_quantity="amplitude", save_name="results/target_plot") +target2.plot2D(plot_zoom_x=100, plot_zoom_y=100, choose_quantity="amplitude", save_name="results/target2_plot") +source1.plot2D(plot_zoom_x=100, plot_zoom_y=100, choose_quantity="amplitude", save_name="results/source1_plot") +source2.plot2D(plot_zoom_x=100, plot_zoom_y=100, choose_quantity="amplitude", save_name="results/source2_plot") + +sim = pid.Simulation( + sources=[source1, source2], + targets=[target, target2], + central_wavelength=wavelength, + space_grid=space_grid, + n_cpu=1 +) + +opt_param_min = 0 +opt_param_max = 1 + +sim.set_optimization(opt_param_min=opt_param_min, + opt_param_max=opt_param_max, + ms_max_avail_diameter=ms_max_available_diameter, + phase_mask_init_size=output_fiber_core_diameter, + opt_max_eval=opt_max_eval,) + +sim.run() + +best_opt_val, distances = sim.get_best_opt_and_distances() +print("Best optimization value:", best_opt_val) +print("Distances:", distances) \ No newline at end of file