From eb0349627142d6c447ac3f08c2536ed9eda2adb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Sierro?= Date: Fri, 3 Sep 2021 09:57:30 +0200 Subject: [PATCH] better A_eff with add numerical_aperture param --- play.py | 55 +++++++-------------------- src/scgenerator/const.py | 2 +- src/scgenerator/physics/fiber.py | 58 ++++++++++++++++++++++------- src/scgenerator/physics/simulate.py | 12 +++--- src/scgenerator/utils/parameter.py | 11 ++++-- 5 files changed, 72 insertions(+), 66 deletions(-) diff --git a/play.py b/play.py index e59c373..9543fe2 100644 --- a/play.py +++ b/play.py @@ -1,47 +1,18 @@ -from typing import Any, Generator -import scgenerator as sc -import itertools - +import os import numpy as np +import scgenerator as sc +import matplotlib.pyplot as plt +from pathlib import Path -class DataPather: - def __init__(self, dl: list[dict[str, Any]]): - self.dict_list = dl - self.n = len(self.dict_list) - self.final_list = list(self.dico_iterator(self.n)) - - def dico_iterator(self, index: int) -> Generator[list[list[tuple[str, Any]]], None, None]: - d_tem_list = [el for d in self.dict_list[: index + 1] for el in d.items()] - dict_pos = np.cumsum([0] + [len(d) for d in self.dict_list[: index + 1]]) - ranges = [range(len(l)) for _, l in d_tem_list] - - for r in itertools.product(*ranges): - flat = [(d_tem_list[i][0], d_tem_list[i][1][j]) for i, j in enumerate(r)] - out = [flat[left:right] for left, right in zip(dict_pos[:-1], dict_pos[1:])] - yield out - - def all_vary_list(self, index): - for l in self.dico_iterator(index): - yield sc.utils.parameter.format_variable_list( - sc.utils.parameter.reduce_all_variable(l[:index]) - ), sc.utils.parameter.format_variable_list( - sc.utils.parameter.reduce_all_variable(l) - ), l[ - index - ] +def main(): + drr = os.getcwd() + os.chdir("/Users/benoitsierro/Nextcloud/PhD/Supercontinuum/PCF Simulations") + try: + sc.run_simulation("PM1550+PM2000D/Pos30000.toml") + finally: + os.chdir(drr) -configs, name = sc.utils.load_config_sequence( - "/Users/benoitsierro/Nextcloud/PhD/Supercontinuum/PCF Simulations/Test/NewStyle.toml" -) - -dp = DataPather([config["variable"] for config in configs]) -# pprint(list(dp.dico_iterator(1))) -for i in range(3): - for prev_path, this_path, this_vary in dp.all_vary_list(i): - print(prev_path) - print(this_path) - print(this_vary) - print() - print() +if __name__ == "__main__": + main() diff --git a/src/scgenerator/const.py b/src/scgenerator/const.py index 974c44c..c679236 100644 --- a/src/scgenerator/const.py +++ b/src/scgenerator/const.py @@ -1,4 +1,4 @@ -__version__ = "0.2.2rules" +__version__ = "0.2.3rules" from typing import Any diff --git a/src/scgenerator/physics/fiber.py b/src/scgenerator/physics/fiber.py index 6edcbbf..43b4155 100644 --- a/src/scgenerator/physics/fiber.py +++ b/src/scgenerator/physics/fiber.py @@ -1,7 +1,6 @@ -from typing import Any, Dict, Iterable, List, Literal, Optional, Tuple, Union, TypeVar +from typing import Any, Iterable, Literal, TypeVar import numpy as np -from numpy.ma import core from numpy.fft import fft, ifft from numpy.polynomial.chebyshev import Chebyshev, cheb2poly from scipy.interpolate import interp1d @@ -280,13 +279,42 @@ def A_eff_hasan(core_radius, capillary_num, capillary_spacing): return M_f * core_radius ** 2 * np.exp((capillary_spacing / 22e-6) ** 2.5) -def V_eff_marcuse(l: T, core_radius: float, numerical_aperture: float) -> T: - return 2 * pi * core_radius * numerical_aperture / l +def V_eff_step_index( + l: T, + core_radius: float, + numerical_aperture: float, + interpolation_range: tuple[float, float] = None, +) -> T: + """computes the V parameter of a step-index fiber + + Parameters + ---------- + l : T + wavelength + core_radius : float + radius of the core + numerical_aperture : float + as a decimal number + interpolation_range : tuple[float, float], optional + when provided, only computes V over this range, wavelengths outside this range will + yield V=inf, by default None + + Returns + ------- + T + V parameter + """ + pi2cn = 2 * pi * core_radius * numerical_aperture + if interpolation_range is not None and isinstance(l, np.ndarray): + low, high = interpolation_range + l = np.where((l >= low) & (l <= high), l, np.inf) + return pi2cn / l def V_parameter_koshiba(l: np.ndarray, pitch: float, pitch_ratio: float) -> float: """returns the V parameter according to Koshiba2004 + Parameters ---------- l : np.ndarray, shape (n,) @@ -322,19 +350,21 @@ def A_eff_from_V(core_radius: float, V_eff: T) -> T: Parameters ---------- - wl : T - wavelength core_radius : float in m - numerical_aperture : float - NA + V_eff : T + effective V parameter. Returns ------- T - A_eff as function of wl + A_eff """ - return core_radius * (0.65 + 1.619 / V_eff ** 1.5 + 2.879 / V_eff ** 6) + out = np.ones_like(V_eff) + out[V_eff > 0] = core_radius * ( + 0.65 + 1.619 / V_eff[V_eff > 0] ** 1.5 + 2.879 / V_eff[V_eff > 0] ** 6 + ) + return out def HCPCF_find_with_given_ZDW( @@ -547,15 +577,15 @@ def HCPCF_dispersion( def dynamic_HCPCF_dispersion( wl_for_disp: np.ndarray, - pressure_values: List[float], + pressure_values: list[float], core_radius: float, fiber_model: str, - model_params: Dict[str, Any], + model_params: dict[str, Any], temperature: float, ideal_gas: bool, w0: float, - interp_range: Tuple[float, float], - material_dico: Dict[str, Any], + interp_range: tuple[float, float], + material_dico: dict[str, Any], deg: int, ): """returns functions for beta2 coefficients and gamma instead of static values diff --git a/src/scgenerator/physics/simulate.py b/src/scgenerator/physics/simulate.py index 7488609..bee847a 100644 --- a/src/scgenerator/physics/simulate.py +++ b/src/scgenerator/physics/simulate.py @@ -4,7 +4,7 @@ import os import random from datetime import datetime from pathlib import Path -from typing import Any, Generator, Type +from typing import Any, Generator, Type, Union import numpy as np from send2trash import send2trash @@ -423,7 +423,7 @@ class Simulations: @classmethod def new( - cls, configuration: Configuration, task_id, method: Type["Simulations"] = None + cls, configuration: Configuration, task_id, method: Union[str, Type["Simulations"]] = None ) -> "Simulations": """Prefered method to create a new simulations object @@ -697,7 +697,7 @@ class RaySimulations(Simulations, priority=2): def run_simulation( config_file: os.PathLike, - method=None, + method: Union[str, Type[Simulations]] = None, ): config = Configuration(config_file) @@ -718,7 +718,7 @@ def run_simulation( def new_simulation( configuration: Configuration, - method: Type[Simulations] = None, + method: Union[str, Type[Simulations]] = None, ) -> Simulations: logger = get_logger(__name__) task_id = random.randint(1e9, 1e12) @@ -743,7 +743,7 @@ def __parallel_RK4IP_worker( def parallel_RK4IP( - config, + config: os.PathLike, ) -> Generator[ tuple[tuple[list[tuple[str, Any]], Parameters, int, int, np.ndarray], ...], None, None ]: @@ -769,7 +769,7 @@ def parallel_RK4IP( for q in pipes: q[1].send(0) logger.debug("msg sent") - computed_dict = {} + computed_dict: dict[int, np.ndarray] = {} for j in range(n): w_id, computed = data_queue.get() computed_dict[w_id] = computed diff --git a/src/scgenerator/utils/parameter.py b/src/scgenerator/utils/parameter.py index 05275ba..22fd485 100644 --- a/src/scgenerator/utils/parameter.py +++ b/src/scgenerator/utils/parameter.py @@ -331,6 +331,7 @@ class Parameters: effective_mode_diameter: float = Parameter(positive(float, int)) A_eff: float = Parameter(non_negative(float, int)) A_eff_file: str = Parameter(string) + numerical_aperture: float = Parameter(in_range_excl(0, 1)) pitch: float = Parameter(in_range_excl(0, 1e-3)) pitch_ratio: float = Parameter(in_range_excl(0, 1)) core_radius: float = Parameter(in_range_excl(0, 1e-3)) @@ -1371,15 +1372,19 @@ default_rules: list[Rule] = [ ["wavelength", "pitch", "pitch_ratio"], conditions=dict(model="pcf"), ), - Rule("V_eff", fiber.V_eff_marcuse, ["wavelength", "core_radius", "numerical_aperture"]), + Rule("V_eff", fiber.V_eff_step_index, ["wavelength", "core_radius", "numerical_aperture"]), Rule("V_eff_arr", fiber.V_parameter_koshiba, conditions=dict(model="pcf")), - Rule("V_eff_arr", fiber.V_eff_marcuse), + Rule( + "V_eff_arr", + fiber.V_eff_step_index, + ["l", "core_radius", "numerical_aperture", "interpolation_range"], + ), Rule("gamma", lambda gamma_arr: gamma_arr[0]), Rule("gamma_arr", fiber.gamma_parameter, ["n2", "w0", "A_eff_arr"]), # Fiber loss Rule("alpha_arr", fiber.compute_capillary_loss), Rule("alpha_arr", fiber.load_custom_loss), - Rule("alpha_arr", lambda alpha, t: np.ones_like(t) * alpha), + Rule("alpha_arr", lambda alpha, t: np.ones_like(t) * alpha, priorities=-1), # gas Rule("n_gas_2", materials.n_gas_2), ]