from BeamStuff import BeamQ, LGMode, HGMode, propagate, updateMask, graphfields, transferFunctionOfFreeSpace, cart2pol, evaluate_masks_eta, save_slm_masks, pad_field, crop_field # type: ignore import numpy as np import matplotlib.pyplot as plt # physical parameters iterations = 30 # number of iterations wavelength = 1064e-9 pixelSize = 8e-6 planeSpacing = 100e-3 planeCount = 2 modeCount = 1 Nx, Ny = int(1920/2), int(1200) pad_factor = 2 # factor to pad the field Nx_p, Ny_p = Nx * pad_factor, Ny * pad_factor # padded size for the field symmetricMask = 0 # True for symmetric masks, False for asymmetric LG = 1 # True for LG modes, False for HG modes save_masks = 1 # True to save masks to file w0 = 2e-3 # waist of the Gaussian beam LG_mfd = 1e-3 # mode field diameter for LG modes HG_mfd = 2e-3 # mode field diameter for HG modes kspacefilter = 1 # k-space filter # build coordinate grid x = (np.arange(Nx) - Nx/2 + 0.5)*pixelSize y = (np.arange(Ny) - Ny/2 + 0.5)*pixelSize X, Y = np.meshgrid(x, y) TH, R = cart2pol(X, Y) # --- define input Gaussian and target mode --- beam0 = BeamQ.from_waist_and_z(w0, 0.0, wavelength) E_gauss = ( #Gaussian field np.sqrt(2/(np.pi*beam0.beam_width**2)) * np.exp(-(X**2+Y**2)/beam0.beam_width**2) * np.exp(-1j*(2*np.pi/wavelength)*(X**2+Y**2)/(2*beam0.radius_of_curvature)) ) if LG: lg = LGMode(10, 0, LG_mfd, X, Y) E_lg = lg.field else: hg = HGMode(10, 10, HG_mfd, X, Y) E_hg = hg.field # --- allocate fields & masks --- FIELDS = np.zeros((2, planeCount, modeCount, Ny, Nx), dtype=complex) MASKS = np.ones((planeCount, Ny, Nx), dtype=complex) # randomize the initial mask #MASKS[0, :, :] = np.exp(1j * np.random.rand(Ny, Nx) * 2 * np.pi) #MASKS[0, :, :] = np.exp(1j * np.angle(E_lg)) # use the target LG mode phase as initial mask # inject them FIELDS[0, 0, 0] = E_gauss FIELDS[0, 1, 0] = E_gauss if LG: FIELDS[1, 1, 0] = E_lg FIELDS[1, 0, 0] = E_lg else: FIELDS[1, 1, 0] = E_hg FIELDS[1, 0, 0] = E_hg # padded transfer function x_p = (np.arange(Nx_p) - Nx_p/2 + 0.5)*pixelSize y_p = (np.arange(Ny_p) - Ny_p/2 + 0.5)*pixelSize Xp, Yp = np.meshgrid(x_p, y_p) THp, Rp = cart2pol(Xp, Yp) H0_p = transferFunctionOfFreeSpace(Xp, Yp, planeSpacing, wavelength) H_p = H0_p * (Rp < (kspacefilter*np.max(Rp))) Hc_p = np.conj(H_p) # precompute transfer function H0 = transferFunctionOfFreeSpace(X, Y, planeSpacing, wavelength) maxR = np.max(R) H = H0*(R<(kspacefilter*maxR)) # --- GS loop --- for i in range(iterations): # FIELDS[0, 0, 0] = E_gauss # FIELDS[1, planeCount-1, 0] = E_lg # —— forward sweep —— for p in range(planeCount-1): # 1) update this plane’s mask with the fields MASKS = updateMask(FIELDS, MASKS, modeCount, p, symmetricMask) # 2) build the mask operator mask = np.exp(-1j * np.angle(np.squeeze(MASKS[p,:,:]))) # 3) get the forward field at plane p field_fwd = np.squeeze(FIELDS[0, p, 0, :, :]) # 4) apply mask field_fwd = field_fwd * mask # 5) pad the field for propagation field_fwd, px, py = pad_field(field_fwd, Nx_p, Ny_p) # 6) propagate on padded grid field_fwd = propagate(field_fwd, H_p) # 7) crop the field back to original size FIELDS[0, p+1, 0, :, :] = crop_field(field_fwd, px, py, Nx, Ny) # —— backward sweep —— Hc = np.conj(H) for p in range(planeCount-1, 0, -1): # 1) update this plane’s mask MASKS = updateMask(FIELDS, MASKS, modeCount, p, symmetricMask) # 2) build the backward mask operator mask = np.exp(1j * np.angle(np.squeeze(MASKS[p,:,:]))) # 3) get the backward field at plane p field_bwd = np.squeeze(FIELDS[1, p, 0, :, :]) # 4) apply mask field_bwd = field_bwd * mask # 5) pad the field for propagation field_bwd, px, py = pad_field(field_bwd, Nx_p, Ny_p) # 6) propagate on padded grid field_bwd = propagate(field_bwd, Hc_p) # 7) crop the field back to original size FIELDS[1, p-1, 0, :, :] = crop_field(field_bwd, px, py, Nx, Ny) # --- final plot & overlap --- graphfields(FIELDS, MASKS, planeCount, modeCount, fig_number=2) if LG: fwd_out, bwd_out, η = evaluate_masks_eta(H, E_gauss, MASKS, E_lg) else: fwd_out, bwd_out, η = evaluate_masks_eta(H, E_gauss, MASKS, E_hg) ILdB = -10 * np.log10(η) if η > 0.95: if save_masks: if LG: print("Saving masks to file...") save_slm_masks(run_name="GAUSS_to"+"_LG("+str(lg.p)+","+str(lg.l)+")", masks=MASKS, wavelength = wavelength, pixel_size = pixelSize, plane_spacing = planeSpacing, output_dir = ".", w0=w0, LG_mfd=LG_mfd) else: print("Saving masks to file...") save_slm_masks(run_name="GAUSS_to"+"_HG("+str(hg.m)+","+str(hg.n)+")", masks=MASKS, wavelength = wavelength, pixel_size = pixelSize, plane_spacing = planeSpacing, output_dir = ".", w0=w0, HG_mfd=HG_mfd) print(f"η = {η*100:.2f}% IL = {ILdB:.2f} dB") f_final = np.abs(FIELDS[0, planeCount-1, 0]) # final propagated field f_final /= np.max(f_final) # normalize amplitude if LG: f_targ = np.abs(E_lg) # target field else: f_targ = np.abs(E_hg) # target field f_targ /= np.max(f_targ) # normalize amplitude # pick the center row row = Ny//2 # extract the two lines line_prop = f_final[row, :] # propagated line_targ = f_targ[row, :] # target # compute amp & phase amp_prop = np.abs(line_prop) amp_targ = np.abs(line_targ) ph_prop = np.angle(FIELDS[0, planeCount-1, 0, row, :]) if LG: ph_targ = np.angle(E_lg[row, :]) else: ph_targ = np.angle(E_hg[row, :]) # plot plt.figure(num=3, figsize=(10,5)) # amplitude plt.subplot(1,2,1) plt.plot(x, amp_prop, label='Propagated') plt.plot(x, amp_targ, label='Target', linestyle='--') plt.xlabel('x (m)') plt.ylabel('Amplitude |E|') plt.title(f'Amplitude at y={y[row]:.3e} m') plt.legend() plt.grid(True) # phase plt.subplot(1,2,2) plt.plot(x, ph_prop, label='Propagated') plt.plot(x, ph_targ, label='Target', linestyle='--') plt.xlabel('x (m)') plt.ylabel('Phase (rad)') plt.title(f'Phase at y={y[row]:.3e} m') plt.legend() plt.grid(True) plt.tight_layout() plt.show()