# Kerr-Lens Mode-Locking: Numerical Simulation of the Spatio-Temporal Dynamics on All Time Scales
## Appendix A Detailed Algorithm and Pseudo-Code  
The previous sections have given an overview of the algorithm, methods, and physics involved in the simulation. This appendix provides the detailed algorithm in the form of pseudo-code. The MATLAB implementation is available through Github [25]. ```
procedureMain(\(N,M\)) Initialization: \(w[t]\gets Random\)\(\triangleright\) Initialize temporal beam waist \(w(t)\) from a random distribution. \(R[t]\gets Random\)\(\triangleright\) Initialize beam radius of curvature (phase) \(R(t)\) from a random distribution. \(\frac{1}{q[t]}\leftarrow\frac{1}{R[t]}-\frac{\Delta i}{\pi w[t]^{2}}\)\(\triangleright\) Initialize the complex beam parameter. \(E[t]\leftarrow Random\)\(\triangleright\) Initialize temporal field amplitude to noise. \(s(\omega)\leftarrow\frac{8}{1+\left(\frac{\omega}{C}\right)^{2}}\)\(\triangleright\) Spectral shape of the linear gain/loss (Lorentzian spectrum). \(\phi(\omega)\leftarrow\sigma_{D}^{2}\omega^{2}\)\(\triangleright\) Spectral phase per round trip due to dispersion (2 nd order GVD). for\(i\gets 1\) to \(N\)do\(\triangleright\) Main loop of \(N\) round trips. \(\mathcal{E}[\omega]\leftarrow\) FFT(\(E[t]\))\(\triangleright\) Transforming to frequency domain to calculate linear propagation. \(\hat{E}[\omega]\leftarrow s(\omega)e^{-i\phi(\omega)}\hat{E}[\omega]\)\(\triangleright\) Apply linear loss, spectral gain shaping and chromatic dispersion. \(E[t]\leftarrow\) IFFT(\(\hat{E}[\omega]\))\(\triangleright\) Transform field back to time domain \(P[t]\leftarrow|E[t]|^{2}\pi w^{2}[t]\)\(\triangleright\) Calculate the instantaneous power \(P(t)\)\(q[t],\phi_{NL}[t]\leftarrow\) ML-Spatial\([P[t],q[t]]\)\(\triangleright\) Procedure to calculate the new beam parameter \(q(t)\) and the nonlinear phase \(\phi_{NL}(t)\) for the next round trip. \(g[t]\leftarrow\) SaturateGain\([P[t],q[t]]\)\(\triangleright\) Procedure to calculate the gain saturation based on power \(P(t)\) and the temporal overlap with pump mode. \(s_{D}[t]\leftarrow\) DiffLoss\([q[t]]\)\(\triangleright\) Procedure to calculate the diffraction losses based on beam waist relative to the pump. \(E[t]\gets s_{D}[t]g[t]e^{i\phi_{NL}[t]}\hat{E}[t]\)\(\triangleright\) Apply saturated gain, diffraction losses and nonlinear phase to the field in time endfor endprocedure
```  
**Algorithm A1** Main loop with temporal Kerr-lens. ```
procedureML-Spatial(\(P(t),q(t)\))\(\triangleright\) Cavity propagation with time dependence \(q[t],\phi_{NL}^{(1)}[t]\leftarrow\) PropagateThickKerrLens[\(q[t],P[t]\)]\(\triangleright\) calculate \(q(t)\) and the SPM \(\phi_{NL}(t)\) at output of Kerr medium (1 st pass) \(M=M_{1}(L_{1}),\ q[t]\leftarrow\frac{(A_{d}[t]+B)}{(C_{d}[t]+D)}\)\(\triangleright\) linear propagation through left side of cavity. A,B,C,D are the elements of the propagation matrix \(w[t],q[t],\phi_{NL}^{(2)}[t]\leftarrow\) PropagateThickKerrLens[\(q[t],P[t]\)]\(\triangleright\) calculate \(q(t)\) at output of Kerr medium (2 nd pass) \(M=M_{2}(L_{2}),\ q[t]\leftarrow\frac{(A_{d}[t]+B)}{(C_{d}[t]+D)}\)\(\triangleright\) linear propagation through right side of cavity \(M_{A}(f_{A})\leftarrow \begin{pmatrix}1&0\\ -i\frac{\lambda}{2\pi w_{A}^{2}}&1\end{pmatrix} \)\(\triangleright\) matrix of artificial aperture (A) as a lens with imaginary focus \(M=M_{A}(f_{A}),\ q[t]\leftarrow\frac{(A_{d}[t]+B)}{(C_{d}[t]+D)}\)\(\triangleright\) Apply artificial aperture to prevent divergence of the beam waist return\(q[t],\ \phi_{NL}^{(1)}[t]+\phi_{NL}^{(2)}[t]\) endprocedureprocedurePropagateThickKerrLens(\(P(t),q(t)\))\(\triangleright\) the thick Kerr medium of length \(L_{c}\) is broken into several thin Kerr lenses separated by short free propagation \(l\) \(M_{prop}(z)\leftarrow \begin{pmatrix}1&nz\\ 0&1\end{pmatrix}