misc
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
# flake8: noqa
|
# flake8: noqa
|
||||||
from . import math
|
from . import math, operators
|
||||||
from .evaluator import Evaluator
|
from .evaluator import Evaluator
|
||||||
from .legacy import convert_sim_folder
|
from .legacy import convert_sim_folder
|
||||||
from .math import abs2, argclosest, normalized, span
|
from .math import abs2, argclosest, normalized, span
|
||||||
|
|||||||
@@ -310,7 +310,7 @@ def build_sim_grid(
|
|||||||
return z_targets, t, time_window, t_num, dt, w_c, w0, w, w_order, l
|
return z_targets, t, time_window, t_num, dt, w_c, w0, w, w_order, l
|
||||||
|
|
||||||
|
|
||||||
def linear_extrapolation(y: np.ndarray) -> np.ndarray:
|
def polynom_extrapolation(x: np.ndarray, y: np.ndarray, degree: float) -> np.ndarray:
|
||||||
"""extrapolates IN PLACE linearily on both side of the support
|
"""extrapolates IN PLACE linearily on both side of the support
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
@@ -323,13 +323,14 @@ def linear_extrapolation(y: np.ndarray) -> np.ndarray:
|
|||||||
last value we want to keep (extrapolate to the right of that)
|
last value we want to keep (extrapolate to the right of that)
|
||||||
"""
|
"""
|
||||||
out = y.copy()
|
out = y.copy()
|
||||||
order = np.argsort(y)
|
order = np.argsort(x)
|
||||||
left_ind, *_, right_ind = np.nonzero(out[order])[0]
|
left_ind, *_, right_ind = np.nonzero(out[order])[0]
|
||||||
_linear_extrapolation_in_place(out[order], left_ind, right_ind)
|
return _polynom_extrapolation_in_place(out[order], left_ind, right_ind, degree)[order.argsort]
|
||||||
return out
|
|
||||||
|
|
||||||
|
|
||||||
def _linear_extrapolation_in_place(y: np.ndarray, left_ind: int, right_ind: int):
|
def _polynom_extrapolation_in_place(y: np.ndarray, left_ind: int, right_ind: int, degree: float):
|
||||||
y[:left_ind] = -(1 + np.arange(left_ind))[::-1] * (y[left_ind + 1] - y[left_ind]) + y[left_ind]
|
r_left = (1 + np.arange(left_ind))[::-1] ** degree
|
||||||
y[right_ind:] = np.arange(len(y) - right_ind) * (y[right_ind] - y[right_ind - 1]) + y[right_ind]
|
r_right = np.arange(len(y) - right_ind) ** degree
|
||||||
|
y[:left_ind] = r_left * (y[left_ind] - y[left_ind + 1]) + y[left_ind]
|
||||||
|
y[right_ind:] = r_right * (y[right_ind] - y[right_ind - 1]) + y[right_ind]
|
||||||
return y
|
return y
|
||||||
|
|||||||
@@ -22,8 +22,10 @@ from .utils import load_material_dico
|
|||||||
class SpectrumDescriptor:
|
class SpectrumDescriptor:
|
||||||
name: str
|
name: str
|
||||||
value: np.ndarray = None
|
value: np.ndarray = None
|
||||||
|
_counter = 0
|
||||||
|
|
||||||
def __set__(self, instance, value):
|
def __set__(self, instance: CurrentState, value: np.ndarray):
|
||||||
|
self._counter += 1
|
||||||
instance.spec2 = math.abs2(value)
|
instance.spec2 = math.abs2(value)
|
||||||
instance.field = np.fft.ifft(value)
|
instance.field = np.fft.ifft(value)
|
||||||
instance.field2 = math.abs2(instance.field)
|
instance.field2 = math.abs2(instance.field)
|
||||||
@@ -243,11 +245,6 @@ class ConstantRefractiveIndex(AbstractRefractiveIndex):
|
|||||||
return self.n_eff_arr
|
return self.n_eff_arr
|
||||||
|
|
||||||
|
|
||||||
class PCFRefractiveIndex(AbstractRefractiveIndex):
|
|
||||||
def __int__(self, wl_for_disp: np.ndarray, pitch: float, pitch_ratio: float):
|
|
||||||
self.n_eff_const = fiber.n_eff_pcf(wl_for_disp, pitch, pitch_ratio)
|
|
||||||
|
|
||||||
|
|
||||||
class MarcatiliRefractiveIndex(AbstractRefractiveIndex):
|
class MarcatiliRefractiveIndex(AbstractRefractiveIndex):
|
||||||
gas_op: AbstractGas
|
gas_op: AbstractGas
|
||||||
core_radius: float
|
core_radius: float
|
||||||
@@ -282,26 +279,6 @@ class HasanRefractiveIndex(AbstractRefractiveIndex):
|
|||||||
capillary_resonance_strengths: list[float]
|
capillary_resonance_strengths: list[float]
|
||||||
wl_for_disp: np.ndarray
|
wl_for_disp: np.ndarray
|
||||||
|
|
||||||
# def __init__(
|
|
||||||
# self,
|
|
||||||
# gas_op: ConstantGas,
|
|
||||||
# core_radius: float,
|
|
||||||
# capillary_num: int,
|
|
||||||
# capillary_nested: int,
|
|
||||||
# capillary_thickness: float,
|
|
||||||
# capillary_radius: float,
|
|
||||||
# capillary_resonance_strengths: list[float],
|
|
||||||
# wl_for_disp: np.ndarray,
|
|
||||||
# ):
|
|
||||||
# self.gas_op = gas_op
|
|
||||||
# self.core_radius = core_radius
|
|
||||||
# self.capillary_num = capillary_num
|
|
||||||
# self.capillary_nested = capillary_nested
|
|
||||||
# self.capillary_thickness = capillary_thickness
|
|
||||||
# self.capillary_radius = capillary_radius
|
|
||||||
# self.capillary_resonance_strengths = capillary_resonance_strengths
|
|
||||||
# self.wl_for_disp = wl_for_disp
|
|
||||||
|
|
||||||
def __call__(self, state: CurrentState) -> np.ndarray:
|
def __call__(self, state: CurrentState) -> np.ndarray:
|
||||||
return fiber.n_eff_hasan(
|
return fiber.n_eff_hasan(
|
||||||
self.wl_for_disp,
|
self.wl_for_disp,
|
||||||
@@ -391,8 +368,8 @@ class ConstantDirectDispersion(AbstractDispersion):
|
|||||||
w_for_disp, w0, n_op(), w0_ind
|
w_for_disp, w0, n_op(), w0_ind
|
||||||
)[2:-2]
|
)[2:-2]
|
||||||
left_ind, *_, right_ind = np.nonzero(self.disp_arr[w_order])[0]
|
left_ind, *_, right_ind = np.nonzero(self.disp_arr[w_order])[0]
|
||||||
self.disp_arr[w_order] = math._linear_extrapolation_in_place(
|
self.disp_arr[w_order] = math._polynom_extrapolation_in_place(
|
||||||
self.disp_arr[w_order], left_ind, right_ind
|
self.disp_arr[w_order], left_ind, right_ind, 1
|
||||||
)
|
)
|
||||||
|
|
||||||
def __call__(self, state: CurrentState = None) -> np.ndarray:
|
def __call__(self, state: CurrentState = None) -> np.ndarray:
|
||||||
@@ -468,8 +445,8 @@ class ConstantLoss(AbstractLoss):
|
|||||||
|
|
||||||
|
|
||||||
class NoLoss(ConstantLoss):
|
class NoLoss(ConstantLoss):
|
||||||
def __init__(self, w: np.ndarray):
|
def __init__(self, t_num: int):
|
||||||
super().__init__(0, w)
|
super().__init__(0, t_num)
|
||||||
|
|
||||||
|
|
||||||
class CapillaryLoss(ConstantLoss):
|
class CapillaryLoss(ConstantLoss):
|
||||||
@@ -790,7 +767,7 @@ def conserved_quantity(
|
|||||||
return NoConservedQuantity()
|
return NoConservedQuantity()
|
||||||
logger = get_logger(__name__)
|
logger = get_logger(__name__)
|
||||||
raman = not isinstance(raman_op, NoRaman)
|
raman = not isinstance(raman_op, NoRaman)
|
||||||
loss = not isinstance(raman_op, NoLoss)
|
loss = not isinstance(loss_op, NoLoss)
|
||||||
if raman and loss:
|
if raman and loss:
|
||||||
logger.debug("Conserved quantity : photon number with loss")
|
logger.debug("Conserved quantity : photon number with loss")
|
||||||
return PhotonNumberLoss(w, gamma_op, loss_op)
|
return PhotonNumberLoss(w, gamma_op, loss_op)
|
||||||
|
|||||||
@@ -376,7 +376,7 @@ class Parameters:
|
|||||||
spec_0: np.ndarray = Parameter(type_checker(np.ndarray))
|
spec_0: np.ndarray = Parameter(type_checker(np.ndarray))
|
||||||
beta2: float = Parameter(type_checker(int, float))
|
beta2: float = Parameter(type_checker(int, float))
|
||||||
alpha_arr: np.ndarray = Parameter(type_checker(np.ndarray))
|
alpha_arr: np.ndarray = Parameter(type_checker(np.ndarray))
|
||||||
alpha: float = Parameter(non_negative(float, int), default=0)
|
alpha: float = Parameter(non_negative(float, int))
|
||||||
gamma_arr: np.ndarray = Parameter(type_checker(np.ndarray))
|
gamma_arr: np.ndarray = Parameter(type_checker(np.ndarray))
|
||||||
A_eff_arr: np.ndarray = Parameter(type_checker(np.ndarray))
|
A_eff_arr: np.ndarray = Parameter(type_checker(np.ndarray))
|
||||||
w: np.ndarray = Parameter(type_checker(np.ndarray))
|
w: np.ndarray = Parameter(type_checker(np.ndarray))
|
||||||
|
|||||||
@@ -207,9 +207,10 @@ class RK4IP:
|
|||||||
"{} steps, z = {:.4f}, h = {:.5g}".format(step, self.state.z, h_taken)
|
"{} steps, z = {:.4f}, h = {:.5g}".format(step, self.state.z, h_taken)
|
||||||
)
|
)
|
||||||
|
|
||||||
self.stored_spectra.append(self.state.spectrum)
|
current_spec = self.get_current_spectrum()
|
||||||
|
self.stored_spectra.append(current_spec)
|
||||||
|
|
||||||
yield step, len(self.stored_spectra) - 1, self.get_current_spectrum()
|
yield step, len(self.stored_spectra) - 1, current_spec
|
||||||
|
|
||||||
self.z_stored.append(self.state.z)
|
self.z_stored.append(self.state.z)
|
||||||
del self.z_targets[0]
|
del self.z_targets[0]
|
||||||
@@ -256,8 +257,8 @@ class RK4IP:
|
|||||||
k4 = h * self.params.nonlinear_operator(self.state.replace(expD * (A_I + k3)))
|
k4 = h * self.params.nonlinear_operator(self.state.replace(expD * (A_I + k3)))
|
||||||
new_state = self.state.replace(expD * (A_I + k1 / 6 + k2 / 3 + k3 / 3) + k4 / 6)
|
new_state = self.state.replace(expD * (A_I + k1 / 6 + k2 / 3 + k3 / 3) + k4 / 6)
|
||||||
|
|
||||||
|
self.cons_qty[step] = self.params.conserved_quantity(new_state)
|
||||||
if self.params.adapt_step_size:
|
if self.params.adapt_step_size:
|
||||||
self.cons_qty[step] = self.params.conserved_quantity(new_state)
|
|
||||||
curr_p_change = np.abs(self.cons_qty[step - 1] - self.cons_qty[step])
|
curr_p_change = np.abs(self.cons_qty[step - 1] - self.cons_qty[step])
|
||||||
cons_qty_change_ok = self.error_ok * self.cons_qty[step - 1]
|
cons_qty_change_ok = self.error_ok * self.cons_qty[step - 1]
|
||||||
|
|
||||||
|
|||||||
@@ -452,7 +452,7 @@ def update_params(new_path: Path, file: Path):
|
|||||||
if (p := params.get("prev_data_dir")) is not None:
|
if (p := params.get("prev_data_dir")) is not None:
|
||||||
p = Path(p)
|
p = Path(p)
|
||||||
params["prev_data_dir"] = str(p.parent / update_path_name(p.name))
|
params["prev_data_dir"] = str(p.parent / update_path_name(p.name))
|
||||||
params["output_path"] = new_path
|
params["output_path"] = str(new_path)
|
||||||
save_toml(new_path / PARAM_FN, params)
|
save_toml(new_path / PARAM_FN, params)
|
||||||
file.unlink()
|
file.unlink()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user