better config defaults

This commit is contained in:
Benoît Sierro
2023-03-21 10:42:27 +01:00
parent 7d2406f4f5
commit 9658839dc7
2 changed files with 19 additions and 18 deletions

View File

@@ -2,13 +2,13 @@ from __future__ import annotations
import sys import sys
from pathlib import Path from pathlib import Path
from typing import Any, NamedTuple from typing import NamedTuple
import numpy as np import numpy as np
import scgenerator as sc import scgenerator as sc
import tomli import tomli
import tomli_w import tomli_w
from pydantic import BaseModel, PrivateAttr, ValidationError, confloat from pydantic import BaseModel, PrivateAttr, ValidationError, confloat, conint
DEFAULT_CONFIG_FILE = "dispersion_config.toml" DEFAULT_CONFIG_FILE = "dispersion_config.toml"
@@ -23,12 +23,13 @@ class CurrentState(BaseModel):
class Config(BaseModel): class Config(BaseModel):
wl_min: confloat(ge=100, le=1000) wl_min: confloat(ge=100, le=1000) = 160
wl_max: confloat(ge=500, le=6000) wl_max: confloat(ge=500, le=6000) = 1600
wl_pump: confloat(ge=200, le=6000) wl_pump: confloat(ge=200, le=6000) = 800
rep_rate: confloat(gt=0) rep_rate: confloat(gt=0) = 8e3
gas: str num_resonances: conint(ge=6, le=20) = 6
safety_factor: float gas: str = "argon"
safety_factor: float = 10.0
current_state: CurrentState | None = None current_state: CurrentState | None = None
_file_name: Path = PrivateAttr() _file_name: Path = PrivateAttr()
@@ -41,7 +42,6 @@ class Config(BaseModel):
with open(config_file, "rb") as file: with open(config_file, "rb") as file:
d = tomli.load(file) d = tomli.load(file)
d = cls.default() | d
try: try:
out = cls(**d) out = cls(**d)
except ValidationError as e: except ValidationError as e:
@@ -51,12 +51,6 @@ class Config(BaseModel):
out._file_name = Path(config_file) out._file_name = Path(config_file)
return out return out
@classmethod
def default(cls) -> dict[str, Any]:
return dict(
wl_min=160, wl_max=1600, wl_pump=800, rep_rate=8e3, gas="argon", safety_factor=10
)
def save(self): def save(self):
tmp = self._file_name.parent / f"{self._file_name.name}.tmp" tmp = self._file_name.parent / f"{self._file_name.name}.tmp"
with open(tmp, "wb") as file: with open(tmp, "wb") as file:

View File

@@ -99,7 +99,14 @@ def app(config_file: os.PathLike | None = None):
tr = sc.fiber.tube_radius_from_gap(core_diameter / 2, gap, n_tubes) tr = sc.fiber.tube_radius_from_gap(core_diameter / 2, gap, n_tubes)
n_gas_2 = gas.sellmeier.n_gas_2(wl, None, pressure) n_gas_2 = gas.sellmeier.n_gas_2(wl, None, pressure)
n_eff_vinc = sc.fiber.n_eff_vincetti( n_eff_vinc = sc.fiber.n_eff_vincetti(
wl, 800e-9, n_gas_2, wall_thickness, tr, gap, n_tubes wl,
800e-9,
n_gas_2,
wall_thickness,
tr,
gap,
n_tubes,
n_terms=config.num_resonances,
) )
return b2(w, n_eff_vinc) return b2(w, n_eff_vinc)