Upload files to "/"
This commit is contained in:
+141
-4
@@ -1,5 +1,21 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
# BD Fresnel - physics and parameter model
|
||||
#
|
||||
# Copyright (C) 2019-2026 Manuel R. Ferdinandus
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
"""
|
||||
bd_fresnel_core.py
|
||||
===================
|
||||
@@ -78,6 +94,8 @@ NOTES ON THE MATLAB -> PYTHON TRANSLATION
|
||||
numpy.heaviside(x, 0.5).
|
||||
"""
|
||||
|
||||
__version__ = "2.5"
|
||||
|
||||
import time
|
||||
import datetime
|
||||
|
||||
@@ -155,6 +173,37 @@ MECHANISM_NAMES = [
|
||||
"Exponential rise/decay (both)",
|
||||
]
|
||||
|
||||
# The empty slot. Its "(both)" suffix is inherited from the original
|
||||
# app's list and carries no meaning -- it contributes nothing -- so it
|
||||
# stays selectable in every mode.
|
||||
NO_MECHANISM = "None (both)"
|
||||
|
||||
# Measurement modes. Beam deflection is the program's original
|
||||
# behaviour. Pump probe centres the probe on the pump (x_0 = 0), where
|
||||
# the deflection signal vanishes by symmetry, and so fits transmission
|
||||
# alone. Only mechanisms with an absorptive part can change the
|
||||
# transmission, so the purely refractive ones are excluded.
|
||||
MODE_BEAM_DEFLECTION = "beam_deflection"
|
||||
MODE_PUMP_PROBE = "pump_probe"
|
||||
MODES = (MODE_BEAM_DEFLECTION, MODE_PUMP_PROBE)
|
||||
|
||||
|
||||
def mechanism_allowed(name: str, mode: str) -> bool:
|
||||
"""Whether a mechanism may occupy a slot in the given mode. Pump
|
||||
probe admits the absorptive (NLA) and mixed (both) mechanisms, plus
|
||||
the empty slot, and excludes the purely refractive (NLR) ones. A
|
||||
mixed mechanism contributes its absorptive part to the transmission;
|
||||
its refractive part produces no deflection with the probe centred."""
|
||||
if mode == MODE_PUMP_PROBE:
|
||||
return name == NO_MECHANISM or name.endswith(("(NLA)", "(both)"))
|
||||
return name in MECHANISM_NAMES
|
||||
|
||||
|
||||
def mechanism_names_for_mode(mode: str) -> List[str]:
|
||||
"""The mechanism pull-down's choices in the given mode, in the
|
||||
original list order."""
|
||||
return [n for n in MECHANISM_NAMES if mechanism_allowed(n, mode)]
|
||||
|
||||
# Per-mechanism (parameter name, unit label) pairs, in order, matching
|
||||
# the param_names/param_units cell arrays in set_params_GUI.m exactly.
|
||||
MECHANISM_PARAM_SPECS = {
|
||||
@@ -281,6 +330,15 @@ class BDFresnelParams:
|
||||
data_imported: bool = False
|
||||
data_fit: bool = False
|
||||
|
||||
# --- measurement mode (see set_mode) ---
|
||||
mode: str = MODE_BEAM_DEFLECTION
|
||||
# Beam-deflection settings put aside while in pump-probe mode, and
|
||||
# restored on switching back: the x_0 that pump probe forces to 0,
|
||||
# and the mechanism slots as they were before the non-absorptive
|
||||
# ones were cleared. Unused (and the list empty) in beam deflection.
|
||||
x_0_beam_deflection: float = 0.5
|
||||
mech_beam_deflection: List[Mechanism] = field(default_factory=list)
|
||||
|
||||
# --- file paths ---
|
||||
filename: str = ""
|
||||
pathname: str = ""
|
||||
@@ -1127,21 +1185,100 @@ def inputs_valid(p: BDFresnelParams) -> bool:
|
||||
# save_settings.m / persistence (JSON in place of MATLAB's .mat file)
|
||||
# =============================================================================
|
||||
|
||||
def _mech_to_dicts(mechs):
|
||||
return [dict(name=m.name, param=list(m.param)) for m in mechs]
|
||||
|
||||
|
||||
def _mech_from_dicts(items):
|
||||
return [Mechanism(name=m["name"], param=list(m["param"])) for m in items]
|
||||
|
||||
|
||||
# Fields holding a list of Mechanism objects, which json cannot
|
||||
# serialize directly.
|
||||
_MECHANISM_LIST_FIELDS = ("mech", "mech_beam_deflection")
|
||||
|
||||
|
||||
def params_to_dict(p: BDFresnelParams) -> dict:
|
||||
d = dict(p.__dict__)
|
||||
d["mech"] = [dict(name=m.name, param=list(m.param)) for m in p.mech]
|
||||
for name in _MECHANISM_LIST_FIELDS:
|
||||
d[name] = _mech_to_dicts(getattr(p, name))
|
||||
return d
|
||||
|
||||
|
||||
def params_from_dict(d: dict) -> BDFresnelParams:
|
||||
d = dict(d)
|
||||
mech_list = d.pop("mech", None)
|
||||
mech_lists = {name: d.pop(name, None) for name in _MECHANISM_LIST_FIELDS}
|
||||
p = BDFresnelParams(**{k: v for k, v in d.items() if k in BDFresnelParams.__dataclass_fields__})
|
||||
if mech_list:
|
||||
p.mech = [Mechanism(name=m["name"], param=list(m["param"])) for m in mech_list]
|
||||
for name, items in mech_lists.items():
|
||||
if items:
|
||||
setattr(p, name, _mech_from_dicts(items))
|
||||
apply_mode_constraints(p)
|
||||
return p
|
||||
|
||||
|
||||
def _copy_mech(m: Mechanism) -> Mechanism:
|
||||
return Mechanism(name=m.name, param=list(m.param))
|
||||
|
||||
|
||||
def apply_mode_constraints(p: BDFresnelParams) -> None:
|
||||
"""Brings p into line with its mode without touching the set-aside
|
||||
beam-deflection state: an unknown mode falls back to beam
|
||||
deflection, and pump probe forces x_0 to 0 and empties any slot
|
||||
holding a mechanism the mode does not allow. Applied to every set
|
||||
of parameters loaded from disk, so a hand-edited or damaged
|
||||
settings file cannot put the program in an inconsistent state."""
|
||||
if p.mode not in MODES:
|
||||
p.mode = MODE_BEAM_DEFLECTION
|
||||
if p.mode == MODE_PUMP_PROBE:
|
||||
p.x_0 = 0.0
|
||||
for i, m in enumerate(p.mech):
|
||||
if not mechanism_allowed(m.name, p.mode):
|
||||
p.mech[i] = Mechanism()
|
||||
|
||||
|
||||
def set_mode(p: BDFresnelParams, mode: str):
|
||||
"""Switches p to `mode`, returning (cleared, restored): the slot
|
||||
indices emptied on entering pump probe, and the ones refilled on
|
||||
leaving it. Both are empty when the mode does not change.
|
||||
|
||||
Entering pump probe sets aside the current x_0 and mechanism slots,
|
||||
then sets x_0 = 0 and empties every slot the mode does not allow
|
||||
(the purely refractive ones); the rest stay where they are,
|
||||
parameters and all.
|
||||
|
||||
Leaving it restores x_0, and puts each cleared mechanism back in
|
||||
its slot -- but only if that slot is still empty. A slot the user
|
||||
has since filled keeps its new mechanism, since that is the more
|
||||
recent choice. Mechanisms edited while in pump probe likewise keep
|
||||
their edits.
|
||||
"""
|
||||
if mode not in MODES:
|
||||
raise ValueError(f"unknown mode {mode!r}; expected one of {MODES}")
|
||||
if mode == p.mode:
|
||||
return [], []
|
||||
|
||||
cleared, restored = [], []
|
||||
if mode == MODE_PUMP_PROBE:
|
||||
p.x_0_beam_deflection = p.x_0
|
||||
p.mech_beam_deflection = [_copy_mech(m) for m in p.mech]
|
||||
p.mode = mode
|
||||
p.x_0 = 0.0
|
||||
for i, m in enumerate(p.mech):
|
||||
if not mechanism_allowed(m.name, mode):
|
||||
p.mech[i] = Mechanism()
|
||||
cleared.append(i)
|
||||
else:
|
||||
p.mode = mode
|
||||
p.x_0 = p.x_0_beam_deflection
|
||||
for i, saved in enumerate(p.mech_beam_deflection[:len(p.mech)]):
|
||||
was_cleared = not mechanism_allowed(saved.name, MODE_PUMP_PROBE)
|
||||
if was_cleared and p.mech[i].name == NO_MECHANISM:
|
||||
p.mech[i] = _copy_mech(saved)
|
||||
restored.append(i)
|
||||
p.mech_beam_deflection = []
|
||||
return cleared, restored
|
||||
|
||||
|
||||
def save_settings(p: BDFresnelParams, filepath: str) -> None:
|
||||
"""JSON equivalent of save_settings.m (which saved handles.params
|
||||
to last_settings.mat)."""
|
||||
|
||||
Reference in New Issue
Block a user