jomiri / pyfiberamp Goto Github PK
View Code? Open in Web Editor NEWFiber laser and amplifier modeling in Python
License: Other
Fiber laser and amplifier modeling in Python
License: Other
After running setup.py the following directories are missing in site-packages:
pyfiberamp/utils
pyfiberamp/spectroscopies
fiber_spectra
copying them manually from git repo solved the issue
When running a dynamic simulation, the warning about the C++ backend is given and defaults to the Python solver. I looked around for the pyfiberamp.dynamic.fiber_simulation_pybindings file that is imported for the DynamicSolverCPP, but could not find it. I am running Windows 10 on an i5-8250U.
Thanks for any help!
I have noticed that a decent number of people are visiting the PyFiberAmp project page but only a handful actually download the code. Therefore, I am planning to create a GUI program that also non-programmers can use.
If you happen to find this post and have any ideas or suggestions for the standalone application, please reply below or send me an email.
Hi. I was experiencing intermittent test failures in test_dynamic_simulation.py
with the simulation result sometimes being populated with NaN's. Replacing np.empty_like
with np.zeros_like
for the creation of P_hat_forward
and P_hat_backward
in dynamic/dynamic_solver_python.py
seems to fix the problem for me. Thanks.
Unfortunately, I have not been able to try the pre-compiled C++ bindings, even on Windows, because I have older CPUs that lack AVX2 instruction support. Since polishing and maintaining the (presumably handwritten) C++ source code sounds like a bit of a hassle for you, I wonder if you are interested in supporting another alternate backend with numba or pythran?
The Dynamic example 1 - Pulsed amplification
example takes 25 minutes to run on my machine with the pure python backend. However, by rewriting a few inner-loop functions in python and adding numba decorators or pythran type annotations and compiling to C++11 (see below), I have been able to get the example to run in 76 seconds (20x speedup) with numba and 54 seconds (28x speedup) with pythran.
Here are the modified inner-loop functions:
import numpy as np
from numba import njit
#pythran export dPdZ(float[][], float[][], float[][], float[][], float[][], float, int, int, bool)
@njit
def dPdZ(P_hat, N2, a_g_per_Nt, a_l, g_m_h_v_dv_per_Nt, dz, num_ion_populations, n_channels, add):
"""Modifies `P_hat` in-place."""
out = np.zeros_like(P_hat)
for i in range(num_ion_populations):
start = i * n_channels
for j in range(out.shape[0]):
for k in range(out.shape[1]):
out[j, k] += P_hat[j, k] * (a_g_per_Nt[start+j, k] * N2[i, k] - a_l[start+j, k]) + (g_m_h_v_dv_per_Nt[start+j, k] * N2[i, k])
if add:
P_hat[:, :] = P_hat + (dz * out)
else:
P_hat[:, :] = P_hat - (dz * out)
#pythran export dNdT(float[][], float[][], float[][], float[][], float, float, int, int)
@njit
def dNdT(N2, P, a_per_h_v_pi_r2, a_g_per_h_v_pi_r2_Nt, A, dt, num_ion_populations, n_channels):
"""Modifies `N2` in-place."""
out = np.empty_like(N2)
for i in range(num_ion_populations):
start = i * n_channels
tmp = np.zeros(out.shape[1], dtype=out.dtype)
for k in range(out.shape[1]):
for j in range(n_channels):
tmp[k] += P[j, k] * (a_per_h_v_pi_r2[start+j, k] - a_g_per_h_v_pi_r2_Nt[start+j, k] * N2[i, k])
out[i, k] = tmp[k] - A * N2[i, k]
N2[:, :] = N2 + (dt * out)
#pythran export shift_against_propagation_direction_to_from(float[][], float[][], int)
@njit
def shift_against_propagation_direction_to_from(P_hat_backward, P_hat_forward, n_forward):
"""Modifies `P_hat_backward` in-place."""
P_hat_backward[:n_forward, :-1] = P_hat_forward[:n_forward, 1:]
P_hat_backward[n_forward:, 1:] = P_hat_forward[n_forward:, :-1]
#pythran export shift_to_propagation_direction_to_from(float[][], float[][], int)
@njit
def shift_to_propagation_direction_to_from(P_hat_forward, P, n_forward):
"""Modifies `P_hat_foreward` in-place."""
P_hat_forward[:n_forward, 1:] = P[:n_forward, :-1]
P_hat_forward[n_forward:, :-1] = P[n_forward:, 1:]
#pythran export min_clamp(float[][], float)
@njit
def min_clamp(arr, min_value):
"""Modifies `arr` in-place."""
for i in range(arr.shape[0]):
for j in range(arr.shape[1]):
if arr[i, j] < min_value:
arr[i, j] = min_value
#pythran export apply_input(float[][], float[][], int, int)
@njit
def apply_input(P, P_in_out, idx_iteration, n_forward):
"""Modifies `P` in-place."""
P[:n_forward, 0] = P_in_out[:n_forward, idx_iteration]
P[n_forward:, -1] = P_in_out[n_forward:, idx_iteration]
#pythran export apply_output(float[][], float[][], int, int)
@njit
def apply_output(P_in_out, P_hat_forward, idx_iteration, n_forward):
"""Modifies `P_in_out` in-place."""
P_in_out[:n_forward, idx_iteration] = P_hat_forward[:n_forward, -1]
P_in_out[n_forward:, idx_iteration] = P_hat_forward[n_forward:, 0]
#pythran export apply_reflection(float[][], int[], int[], float[], int)
@njit
def apply_reflection(P, source_idx, target_idx, R, n_forward):
"""Modifies `P` in-place."""
for _source_idx, _target_idx, _R in zip(source_idx, target_idx, R):
if _source_idx < n_forward:
P[_target_idx, -1] += _R * P[_source_idx, -2]
else:
P[_target_idx, 0] += _R * P[_source_idx, 1]
#pythran export new_P(float[][], float[][], float[][])
@njit
def new_P(P, P_hat_forward, P_hat_backward):
"""Modifies `P` in-place."""
P[:, :] = P_hat_forward + 0.5 * (P - P_hat_backward)
Using pythran is as simple as commenting out the numba import and @njit
decorators, then compiling to a native extension module with:
pythran filename.py
I am guessing that these 9 python functions are much smaller and easier to maintain than handwritten C++. I can submit a pull request, if you are interested. Otherwise, this post might serve as a useful guide to other users.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.