<jupyter_start><jupyter_text>pyMMF: Benchmark test for straights step index fibers<jupyter_code>import pyMMF
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
from matplotlib import rc
rc('figure', figsize=(18,9))
rc('text', usetex=True)
from IPython.display import display, Math<jupyter_output><empty_output><jupyter_text>Fiber parameters<jupyter_code>## Parameters
NA = 0.15
radius = 10. # in microns
areaSize = 3.5*radius # calculate the field on an area larger than the diameter of the fiber
npoints = 2**8 # resolution of the window
n1 = 1.45
wl = 0.6328 # wavelength in microns<jupyter_output><empty_output><jupyter_text>Instantiate the solver<jupyter_code># Create the fiber object
profile = pyMMF.IndexProfile(npoints = npoints, areaSize = areaSize)

# Initialize the index profile
profile.initStepIndex(n1=n1,a=radius,NA=NA)

# Instantiate the solver
solver = pyMMF.propagationModeSolver()

# Set the profile to the solver
solver.setIndexProfile(profile)

# Set the wavelength
solver.setWL(wl)

# Estimate the number of modes for a graded index fiber
Nmodes_estim = pyMMF.estimateNumModesSI(wl,radius,NA,pola=1)

print(f"Estimated number of modes using the V number = {Nmodes_estim}")<jupyter_output>2021-06-08 15:41:58,679 - pyMMF.core [DEBUG  ]  Debug mode ON.<jupyter_text>Semi-analytical solution<jupyter_code>modes_semianalytical = solver.solve(mode = 'SI', curvature = None)<jupyter_output>2021-06-08 15:41:58,689 - pyMMF.solv [INFO   ]  Finding the propagation constant of step index fiber by numerically solving the dispersion relation.
/opt/miniconda/envs/py38/lib/python3.8/site-packages/pyMMF/solvers/SI.py:102: RuntimeWarning: invalid value encountered in sqrt
  w=np.sqrt(v**2-u**2)
2021-06-08 15:41:59,009 - pyMMF.solv [INFO   ]  Found 59 modes is 0.32 seconds.
2021-06-08 15:41:59,009 - pyMMF.solv [INFO   ]  Finding analytical LP mode profiles associated to the propagation constants.
2021-06-08 15:42:05,458 - pyMMF.core [DEBUG  ]  Mode data stored in memory.<jupyter_text>Numerical calculations 2d Eigenvalue solver: See the [tutorial](https://www.wavefrontshaping.net/post/id/3) for more information.<jupyter_code>modes_eig = solver.solve(nmodesMax=Nmodes_estim+10,
                         boundary = 'close', 
                         mode = 'eig', 
                         curvature = None, 
                         propag_only=True)<jupyter_output>2021-06-08 15:42:05,465 - pyMMF.solv [INFO   ]  Solving the spatial eigenvalue problem for mode finding.
2021-06-08 15:42:05,468 - pyMMF.solv [INFO   ]  Use close boundary condition.
2021-06-08 15:44:53,865 - pyMMF.solv [INFO   ]  Solver found 59 modes is 168.40 seconds.
2021-06-08 15:44:53,867 - pyMMF.core [DEBUG  ]  Mode data stored in memory.<jupyter_text>Radial solver: See the [tutorial](https://www.wavefrontshaping.net/post/id/66)and the article [Learning and avoiding disorder in multimode fibers](https://arxiv.org/abs/2010.14813) for more information.<jupyter_code>modes_radial = solver.solve(mode = 'radial',  
                         propag_only=True,
                         N_beta_coarse = int(1e3))<jupyter_output>2021-06-08 15:44:53,895 - pyMMF.solv [INFO   ]  Found 5 radial mode(s) for m=0
2021-06-08 15:44:53,896 - pyMMF.solv [INFO   ]  Searching propagation constant for |l| = 1
2021-06-08 15:44:53,988 - pyMMF.solv [WARNING]  maximum recursion depth exceeded while calling a Python object
2021-06-08 15:44:53,988 - pyMMF.solv [WARNING]  Boundary condition could not be met.
2021-06-08 15:44:53,989 - pyMMF.solv [WARNING]  Retrying by changing r_max to 3.13a
2021-06-08 15:44:54,080 - pyMMF.solv [WARNING]  maximum recursion depth exceeded while calling a Python object
2021-06-08 15:44:54,081 - pyMMF.solv [WARNING]  Boundary condition could not be met.
2021-06-08 15:44:54,081 - pyMMF.solv [WARNING]  Retrying by changing r_max to 2.82a
2021-06-08 15:44:54,162 - pyMMF.solv [WARNING]  maximum recursion depth exceeded while calling a Python object
2021-06-08 15:44:54,163 - pyMMF.solv [WARNING]  Boundary condition could not be met.
2021-06-08 15:44:54,164 - pyMMF.solv [WARNING]  Retrying by changing r_max[...]<jupyter_text>Comparing results<jupyter_code># Sort the modes
modes = {}
idx = np.flip(np.argsort(modes_semianalytical.betas), axis=0)
modes['SA'] = {'betas':np.array(modes_semianalytical.betas)[idx],'profiles':[modes_semianalytical.profiles[i] for i in idx]}
idx = np.flip(np.argsort(modes_eig.betas), axis=0)
modes['eig'] = {'betas':np.array(modes_eig.betas)[idx],'profiles':[modes_eig.profiles[i] for i in idx]}
idx = np.flip(np.argsort(modes_eig.betas), axis=0)
modes['radial'] = {'betas':np.array(modes_radial.betas)[idx],'profiles':[modes_radial.profiles[i] for i in idx]}


def sort(a):
    return np.flip(np.sort(a),axis=0)




plt.figure(); 
plt.plot(sort(np.real(modes_eig.betas)),
         label='Numerical simulations (eigenvalue solver)',
         linewidth=2.)
plt.plot(sort(np.real(modes_semianalytical.betas)),
         'r--',
         label='Semi-analytical',
         linewidth=2.)
plt.plot(sort(np.real(modes_radial.betas)),
         'go',
         label='Numerical simulations (radial solver)',
         linewidth=2.)

plt.xticks(fontsize = 20)
plt.yticks(fontsize = 20)
plt.title(r'Semi-analytical VS numerical simulations' ,fontsize = 30)
plt.ylabel(r'Propagation constant $\beta$ (in $\mu$m$^{-1}$)', fontsize = 25)
plt.xlabel(r'Mode index', fontsize = 25)
plt.legend(fontsize = 22,loc='upper right')
plt.show()<jupyter_output><empty_output><jupyter_text>Comparing numerical solutions to LP modes<jupyter_code>imode = 15
plt.figure()
plt.subplot(131)
plt.imshow(np.abs(modes['SA']['profiles'][imode].reshape([npoints]*2)))
plt.gca().set_title("Ideal LP mode",fontsize=25)
plt.axis('off')
plt.subplot(132)
plt.imshow(np.abs(modes['eig']['profiles'][imode].reshape([npoints]*2)))
plt.gca().set_title("Numerical simulations",fontsize=25)
plt.axis('off')
plt.subplot(133)
plt.imshow(np.abs(modes['radial']['profiles'][imode].reshape([npoints]*2)))
plt.gca().set_title("Numerical simulations",fontsize=25)
plt.axis('off')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1342281879194631, 'fraction_numerical': 0.0738255033557047, 'mean_word_length': 4.699626865671642, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 3, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '575958', 'license': 'MIT', 'n_tokens_mistral': 2588, 'n_tokens_neox': 2202, 'n_words': 486, 'path': 'example/Benchmark_SI.ipynb', 'repo_name': 'Asteib/pyMMF', 'size': '6109'}