Pickling objects
Pickling an object means saving it in a binary format (usually a PKL file). Unpickling means converting the PKL back to a Python object. It can be really useful when some calculations take a lot of time and you do not want to recompute them every time.
[1]:
from importlib import resources
import os
from pprint import pprint
from pathlib import Path
from lightwin.config.config_manager import process_config
from lightwin.core.accelerator.accelerator import Accelerator
from lightwin.ui.workflow_setup import run_simulation
from lightwin.util.pickling import MyCloudPickler
Remove previous pickles
[2]:
pickle_folder = Path("pickles")
if not pickle_folder.is_dir():
os.mkdir(pickle_folder)
else:
for file in Path("pickles").iterdir():
os.remove(file)
Manual pickling/unpickling
Perform first calculation
We start with an example configuration.
[3]:
toml_filepath = resources.files("lightwin.data.ads") / "lightwin.toml"
toml_keys = {
"files": "files",
"beam": "beam",
"beam_calculator": "generic_envelope1d",
"wtf": "generic_wtf",
"design_space": "fit_phi_s_design_space",
"plots": "plots_minimal",
}
override = {
"plots": {
"kwargs": {"lw": 5},
"emittance": False,
"energy": False,
"envelopes": False,
"phase": False,
"twiss": False,
},
}
config = process_config(toml_filepath, toml_keys, override=override)
fault_scenarios = run_simulation(config)
[INFO ] [log_manager.py ] Starting log for LightWin - Version: 0.16.3.dev1+g4c96aaf, Commit: 4c96aaf2a12e1dbfd514865321331c13ad57cc82
[INFO ] [files_specs.py ] Setting project_path = PosixPath('/home/docs/checkouts/readthedocs.org/user_builds/lightwin/envs/latest/lib/python3.12/site-packages/lightwin/data/ads/lw_results')
Setting log_file = 'lightwin.log'
[INFO ] [factory.py ] Creating new BeamCalculatorsFactory instance.
[INFO ] [factory.py ] Creating new BeamCalculator: 0_Envelope1D
[INFO ] [accelerator.py ] Created a ListOfElements ecompassing all linac. Created with:
dat_file = PosixPath('/home/docs/checkouts/readthedocs.org/user_builds/lightwin/envs/latest/lib/python3.12/site-packages/lightwin/data/ads/ads.dat')
w_kin_in = 20.00 MeV
phi_abs_in = 0.00 rad
[INFO ] [factory.py ] Created 000000_Reference Accelerator.
[INFO ] [accelerator.py ] Created a ListOfElements ecompassing all linac. Created with:
dat_file = PosixPath('/home/docs/checkouts/readthedocs.org/user_builds/lightwin/envs/latest/lib/python3.12/site-packages/lightwin/data/ads/ads.dat')
w_kin_in = 20.00 MeV
phi_abs_in = 0.00 rad
[INFO ] [factory.py ] Created 000001_Solution Accelerator.
[INFO ] [beam_calculator.py ] Elapsed time in beam calculation: 0:00:00.574604
[INFO ] [beam_calculator.py ] Elapsed time in beam calculation: 0:00:00.556965
[WARNING ] [objective.py ] key = 'to_deg' is recommended to avoid undetermined behavior but was not found.
Objective(name=$M_{z\delta}$, weight=1.0, get_key=twiss, get_kwargs={'elt': Drift # 80 | DRIFT 150 100 0 0 0, 'pos': 'out', 'to_numpy': True, 'phase_space_name': 'zdelta'}, ideal_value=0.0, descriptor=Minimize mismatch factor in the [z-delta] plane.)
[INFO ] [fault_scenario.py ] Created a ListOfElements ecompassing a linac subset.
Encompasses: FM9 to DR36
w_kin_in = 23.50 MeV
phi_abs_in = 133.38 rad
[INFO ] [fault.py ] Starting resolution of optimization problem defined by:
====================================================================================================
Variable | Element | x_0 | Lower lim | Upper lim
----------------------------------------------------------------------------------------------------
$\phi_s$ [deg] | FM10 | -41.965 | -90.000 | -25.179
$\phi_s$ [deg] | FM12 | -41.305 | -90.000 | -24.783
$\phi_s$ [deg] | FM9 | -41.751 | -90.000 | -25.050
$k_e$ [1] | FM10 | 1.649 | 1.319 | 4.756
$k_e$ [1] | FM12 | 1.698 | 1.359 | 4.756
$k_e$ [1] | FM9 | 1.649 | 1.319 | 4.756
====================================================================================================
====================================================================================================
Constraint | Element | x_0 | Lower lim | Upper lim
----------------------------------------------------------------------------------------------------
====================================================================================================
====================================================================================================
Objective | wgt. | ideal value
----------------------------------------------------------------------------------------------------
w_kin @elt DR36 (out) | 1.0 | +2.57715511997316e+01
phi_abs @elt DR36 (out) | 1.0 | +1.77990507535786e+02
twiss @elt DR36 (out) | 1.0 | +0.00000000000000e+00
====================================================================================================
[INFO ] [fault.py ] Finished! Solving this problem took 0:00:51.189794. Results are:
====================================================================================================
Objective | wgt. | ideal value | final residuals
----------------------------------------------------------------------------------------------------
w_kin @elt DR36 (out) | 1.0 | +2.57715511997316e+01 | +1.80561602717333e-03
phi_abs @elt DR36 (out) | 1.0 | +1.77990507535786e+02 | +3.66552096289752e-03
twiss @elt DR36 (out) | 1.0 | +0.00000000000000e+00 | +5.44064902029013e-02
====================================================================================================
Additional info: DownhillSimplex
Maximum number of function evaluations has been exceeded.
[INFO ] [fault_scenario.py ] Retuned cavities:
====================================================================================================
name status k_e phi_0_abs phi_0_rel v_cav_mv phi_s
0 FM9 compensate (in progress) 1.319017 289.339820 205.351492 0.576403 -25.051825
1 FM10 compensate (in progress) 2.068441 54.867062 191.154235 0.925300 -43.041931
2 FM11 failed 0.000000 289.337404 188.548608 NaN NaN
3 FM12 compensate (in progress) 2.769461 109.118120 206.454050 1.291172 -33.318239
====================================================================================================
[INFO ] [fault_scenario.py ] Solving all the optimization problems took 0:00:51.797686
[INFO ] [list_of_simulation_output_evaluators.py]
====================================================================================================
Fit quality:(FIXME: settings in FaultScenario, not config_manager)
----------------------------------------------------------------------------------------------------
end comp zone (DR36) end linac RMS [usual units]
$W_{kin}$ [MeV] 0.007% -0.004% 0.000
Beam phase [rad] 0.002% -0.000% 0.000
Norm. $\sigma_\phi$ @ $1\sigma$ [rad] 3.724% 1.380% 0.000
Norm. $\sigma_\phi$ @ $1\sigma$ [MeV] -4.499% -2.446% 0.000
Norm. $\epsilon_{\phi W}$ [$\pi$.rad.MeV] -0.001% -0.001% 0.000
$M_{z\delta}$ 0.054 0.046 nan
====================================================================================================
[WARNING ] [optimization.py ] When several fault scenarios are plotted after each other, they all keep the same objective position marker. This is not intended behavior.
Pickle the Accelerator
[4]:
pickler = MyCloudPickler()
fault_scenario = fault_scenarios[0]
reference = fault_scenario.ref_acc
ref_pickle_path = reference.pickle(pickler, path=pickle_folder / "reference-accelerator.pkl")
fixed = fault_scenario.fix_acc
fix_pickle_path = fixed.pickle(pickler, path=pickle_folder / "fixed-accelerator.pkl")
del fault_scenario, reference, fixed
[INFO ] [pickling.py ] Pickled 000000_Reference to /home/docs/checkouts/readthedocs.org/user_builds/lightwin/checkouts/latest/docs/manual/notebooks/pickles/reference-accelerator.pkl.
[INFO ] [pickling.py ] Pickled 000001_Solution to /home/docs/checkouts/readthedocs.org/user_builds/lightwin/checkouts/latest/docs/manual/notebooks/pickles/fixed-accelerator.pkl.
Warning
SimulationOutput instances created by Cython do not seem to be pickable.
Unpickle the Accelerator
[5]:
reference = Accelerator.from_pickle(pickler, ref_pickle_path)
fixed = Accelerator.from_pickle(pickler, fix_pickle_path)
We can quickly check that we recovered the results.
[6]:
ax = None
for accelerator in (reference, fixed):
ax = accelerator.plot("v_cav_mv", marker="o", ax=ax)
[WARNING ] [simulation_output.py] key = 'elt_idx' is structure-dependent and does not vary from simulation to simulation. You may be better of calling `Accelerator.get` or `ListOfElements.get`.
[WARNING ] [simulation_output.py] key = 'elt_idx' is structure-dependent and does not vary from simulation to simulation. You may be better of calling `Accelerator.get` or `ListOfElements.get`.
Configure pickling/unpickling from the TOML
The pickle_paths entry in the TOML should associate every accelerator name with a PKL filepath.
[files]
dat_file = "ads.dat"
project_folder = "lw_results/"
[files.pickle_paths]
Reference = "reference.pkl"
# Scenario 1: pre-computed solution (skips optimization)
[files.pickle_paths.000001]
Solution = "solution-000001.pkl"
# Scenario 2: alternatives with custom names (optimization still runs, the pickled Accelerators will be appended)
[files.pickle_paths.000002]
"Conservative approach" = "design-conservative.pkl"
"Aggressive tuning" = "design-aggressive.pkl"
# Scenario 3: solution + alternatives
[files.pickle_paths.000003]
Solution = "solution-000003.pkl"
"Tweaked design" = "tweaked.pkl"
"Experimental config" = "experimental.pkl"
If the path does not exist, the simulation is performed normally and the associated
Acceleratorobject is pickled.If the path exists, the
Acceleratoris unpickled and associated calculations are skipped.
Warning
After every update/source code editing, you should remove your pickles in order to avoid hard-to-track down bugs.
First simple scenario
Here, we define a pickle path for both the Reference Accelerator and the Solution of the first (and unique) FaultScenario.
Note
“Reference” and “Solution” are reserved keywords for Reference and Solution Accelerators.
[7]:
pickle_paths = {
"Reference": pickle_folder / "reference.pkl",
"000001": {
"Solution": pickle_folder / "solution-000001-3cavs.pkl"
},
}
override["files"] = {"pickle_paths": pickle_paths}
config = process_config(toml_filepath, toml_keys, override=override)
[INFO ] [table_spec.py ] .toml table [files] loaded!
[INFO ] [table_spec.py ] .toml table [beam] loaded!
[INFO ] [table_spec.py ] .toml table [generic_envelope1d] loaded!
[INFO ] [table_spec.py ] .toml table [plots_minimal] loaded!
[INFO ] [table_spec.py ] .toml table [fit_phi_s_design_space] loaded!
[INFO ] [table_spec.py ] .toml table [generic_wtf] loaded!
[INFO ] [log_manager.py ] Starting log for LightWin - Version: 0.16.3.dev1+g4c96aaf, Commit: 4c96aaf2a12e1dbfd514865321331c13ad57cc82
[INFO ] [files_specs.py ] Setting project_path = PosixPath('/home/docs/checkouts/readthedocs.org/user_builds/lightwin/envs/latest/lib/python3.12/site-packages/lightwin/data/ads/lw_results')
Setting log_file = 'lightwin.log'
Both files “reference.pkl” and “solution-000001-3cavs.pkl” do not exist. So a normal simulation will be run, and corresponding Accelerators will be pickled to this paths.
[8]:
fault_scenarios = run_simulation(config)
[INFO ] [factory.py ] Re-using previous BeamCalculatorsFactory instance.
[INFO ] [factory.py ] Re-using existing BeamCalculator: 0_Envelope1D
[INFO ] [accelerator.py ] Created a ListOfElements ecompassing all linac. Created with:
dat_file = PosixPath('/home/docs/checkouts/readthedocs.org/user_builds/lightwin/envs/latest/lib/python3.12/site-packages/lightwin/data/ads/ads.dat')
w_kin_in = 20.00 MeV
phi_abs_in = 0.00 rad
[INFO ] [factory.py ] Created 000000_Reference Accelerator (will be pickled to '/home/docs/checkouts/readthedocs.org/user_builds/lightwin/checkouts/latest/docs/manual/notebooks/pickles/reference.pkl').
[INFO ] [accelerator.py ] Created a ListOfElements ecompassing all linac. Created with:
dat_file = PosixPath('/home/docs/checkouts/readthedocs.org/user_builds/lightwin/envs/latest/lib/python3.12/site-packages/lightwin/data/ads/ads.dat')
w_kin_in = 20.00 MeV
phi_abs_in = 0.00 rad
[INFO ] [factory.py ] Created 000001_Solution Accelerator (will be pickled to '/home/docs/checkouts/readthedocs.org/user_builds/lightwin/checkouts/latest/docs/manual/notebooks/pickles/solution-000001-3cavs.pkl').
[WARNING ] [factory.py ] Behavior of additional Accelerator is not well defined. In particular if there are several FaultScenario.
[INFO ] [pickling.py ] Pickled 000000_Reference to /home/docs/checkouts/readthedocs.org/user_builds/lightwin/checkouts/latest/docs/manual/notebooks/pickles/reference.pkl.
[INFO ] [beam_calculator.py ] Elapsed time in beam calculation: 0:00:00.601474
[INFO ] [pickling.py ] Pickled 000001_Solution to /home/docs/checkouts/readthedocs.org/user_builds/lightwin/checkouts/latest/docs/manual/notebooks/pickles/solution-000001-3cavs.pkl.
[INFO ] [beam_calculator.py ] Elapsed time in beam calculation: 0:00:00.586789
[WARNING ] [objective.py ] key = 'to_deg' is recommended to avoid undetermined behavior but was not found.
Objective(name=$W_{kin}$ [MeV], weight=1.0, get_key=w_kin, get_kwargs={'elt': Drift # 80 | DRIFT 150 100 0 0 0, 'pos': 'out', 'to_numpy': False}, ideal_value=25.771551199731583, descriptor=Minimize diff. of w_kin between ref and fix at the end of the compensation zone.)
[WARNING ] [objective.py ] key = 'to_deg' is recommended to avoid undetermined behavior but was not found.
Objective(name=$M_{z\delta}$, weight=1.0, get_key=twiss, get_kwargs={'elt': Drift # 80 | DRIFT 150 100 0 0 0, 'pos': 'out', 'to_numpy': True, 'phase_space_name': 'zdelta'}, ideal_value=0.0, descriptor=Minimize mismatch factor in the [z-delta] plane.)
[INFO ] [fault_scenario.py ] Created a ListOfElements ecompassing a linac subset.
Encompasses: FM9 to DR36
w_kin_in = 23.50 MeV
phi_abs_in = 133.38 rad
[INFO ] [fault.py ] Starting resolution of optimization problem defined by:
====================================================================================================
Variable | Element | x_0 | Lower lim | Upper lim
----------------------------------------------------------------------------------------------------
$\phi_s$ [deg] | FM10 | -41.965 | -90.000 | -25.179
$\phi_s$ [deg] | FM12 | -41.305 | -90.000 | -24.783
$\phi_s$ [deg] | FM9 | -41.751 | -90.000 | -25.050
$k_e$ [1] | FM10 | 1.649 | 1.319 | 4.756
$k_e$ [1] | FM12 | 1.698 | 1.359 | 4.756
$k_e$ [1] | FM9 | 1.649 | 1.319 | 4.756
====================================================================================================
====================================================================================================
Constraint | Element | x_0 | Lower lim | Upper lim
----------------------------------------------------------------------------------------------------
====================================================================================================
====================================================================================================
Objective | wgt. | ideal value
----------------------------------------------------------------------------------------------------
w_kin @elt DR36 (out) | 1.0 | +2.57715511997316e+01
phi_abs @elt DR36 (out) | 1.0 | +1.77990507535786e+02
twiss @elt DR36 (out) | 1.0 | +0.00000000000000e+00
====================================================================================================
[INFO ] [fault.py ] Finished! Solving this problem took 0:00:51.959439. Results are:
====================================================================================================
Objective | wgt. | ideal value | final residuals
----------------------------------------------------------------------------------------------------
w_kin @elt DR36 (out) | 1.0 | +2.57715511997316e+01 | +1.80561602717333e-03
phi_abs @elt DR36 (out) | 1.0 | +1.77990507535786e+02 | +3.66552096289752e-03
twiss @elt DR36 (out) | 1.0 | +0.00000000000000e+00 | +5.44064902029013e-02
====================================================================================================
Additional info: DownhillSimplex
Maximum number of function evaluations has been exceeded.
[INFO ] [fault_scenario.py ] Retuned cavities:
====================================================================================================
name status k_e phi_0_abs phi_0_rel v_cav_mv phi_s
0 FM9 compensate (in progress) 1.319017 289.339820 205.351492 0.576403 -25.051825
1 FM10 compensate (in progress) 2.068441 54.867062 191.154235 0.925300 -43.041931
2 FM11 failed 0.000000 289.337404 188.548608 NaN NaN
3 FM12 compensate (in progress) 2.769461 109.118120 206.454050 1.291172 -33.318239
====================================================================================================
[INFO ] [fault_scenario.py ] Solving all the optimization problems took 0:00:52.571737
[INFO ] [list_of_simulation_output_evaluators.py]
====================================================================================================
Fit quality:(FIXME: settings in FaultScenario, not config_manager)
----------------------------------------------------------------------------------------------------
end comp zone (DR36) end linac RMS [usual units]
$W_{kin}$ [MeV] 0.007% -0.004% 0.000
Beam phase [rad] 0.002% -0.000% 0.000
Norm. $\sigma_\phi$ @ $1\sigma$ [rad] 3.724% 1.380% 0.000
Norm. $\sigma_\phi$ @ $1\sigma$ [MeV] -4.499% -2.446% 0.000
Norm. $\epsilon_{\phi W}$ [$\pi$.rad.MeV] -0.001% -0.001% 0.000
$M_{z\delta}$ 0.054 0.046 nan
====================================================================================================
[INFO ] [pickling.py ] Pickled 000001_Solution to /home/docs/checkouts/readthedocs.org/user_builds/lightwin/checkouts/latest/docs/manual/notebooks/pickles/solution-000001-3cavs.pkl.
Now that “reference.pkl” and “solution-000001-3cavs.pkl” do exist, Accelerators are unpickled and simulations are skipped.
[9]:
fault_scenarios = run_simulation(config)
[INFO ] [factory.py ] Re-using previous BeamCalculatorsFactory instance.
[INFO ] [factory.py ] Re-using existing BeamCalculator: 0_Envelope1D
[INFO ] [accelerator.py ] Created a ListOfElements ecompassing all linac. Created with:
dat_file = PosixPath('/home/docs/checkouts/readthedocs.org/user_builds/lightwin/envs/latest/lib/python3.12/site-packages/lightwin/data/ads/ads.dat')
w_kin_in = 20.00 MeV
phi_abs_in = 0.00 rad
[INFO ] [factory.py ] Created 000000_Reference Accelerator.
[INFO ] [factory.py ] Loading Solution from pickle: /home/docs/checkouts/readthedocs.org/user_builds/lightwin/checkouts/latest/docs/manual/notebooks/pickles/solution-000001-3cavs.pkl
[INFO ] [factory.py ] Created 000001_Solution Accelerator by unpickling '/home/docs/checkouts/readthedocs.org/user_builds/lightwin/checkouts/latest/docs/manual/notebooks/pickles/solution-000001-3cavs.pkl'.
[WARNING ] [factory.py ] Behavior of additional Accelerator is not well defined. In particular if there are several FaultScenario.
[INFO ] [beam_calculator.py ] Elapsed time in beam calculation: 0:00:00.571643
[WARNING ] [objective.py ] key = 'to_deg' is recommended to avoid undetermined behavior but was not found.
Objective(name=$W_{kin}$ [MeV], weight=1.0, get_key=w_kin, get_kwargs={'elt': Drift # 80 | DRIFT 150 100 0 0 0, 'pos': 'out', 'to_numpy': False}, ideal_value=25.771551199731583, descriptor=Minimize diff. of w_kin between ref and fix at the end of the compensation zone.)
[WARNING ] [objective.py ] key = 'to_deg' is recommended to avoid undetermined behavior but was not found.
Objective(name=$M_{z\delta}$, weight=1.0, get_key=twiss, get_kwargs={'elt': Drift # 80 | DRIFT 150 100 0 0 0, 'pos': 'out', 'to_numpy': True, 'phase_space_name': 'zdelta'}, ideal_value=0.0, descriptor=Minimize mismatch factor in the [z-delta] plane.)
[INFO ] [fault_scenario.py ] Created a ListOfElements ecompassing a linac subset.
Encompasses: FM9 to DR36
w_kin_in = 23.50 MeV
phi_abs_in = 133.38 rad
[INFO ] [fault.py ] Starting resolution of optimization problem defined by:
====================================================================================================
Variable | Element | x_0 | Lower lim | Upper lim
----------------------------------------------------------------------------------------------------
$\phi_s$ [deg] | FM10 | -41.965 | -90.000 | -25.179
$\phi_s$ [deg] | FM12 | -41.305 | -90.000 | -24.783
$\phi_s$ [deg] | FM9 | -41.751 | -90.000 | -25.050
$k_e$ [1] | FM10 | 1.649 | 1.319 | 4.756
$k_e$ [1] | FM12 | 1.698 | 1.359 | 4.756
$k_e$ [1] | FM9 | 1.649 | 1.319 | 4.756
====================================================================================================
====================================================================================================
Constraint | Element | x_0 | Lower lim | Upper lim
----------------------------------------------------------------------------------------------------
====================================================================================================
====================================================================================================
Objective | wgt. | ideal value
----------------------------------------------------------------------------------------------------
w_kin @elt DR36 (out) | 1.0 | +2.57715511997316e+01
phi_abs @elt DR36 (out) | 1.0 | +1.77990507535786e+02
twiss @elt DR36 (out) | 1.0 | +0.00000000000000e+00
====================================================================================================
[ERROR ] [cavity_settings.py ] Synch phase not found
[ERROR ] [cavity_settings.py ] Synch phase not found
[ERROR ] [cavity_settings.py ] Synch phase not found
[WARNING ] [algorithm.py ] Consistency check FAILED for PredefinedSolution:
w_kin @elt DR36 (out) | 1.0 | +2.57715511997316e+01: stored=0.00180562, fresh=nan, rel_diff=nan
phi_abs @elt DR36 (out) | 1.0 | +1.77990507535786e+02: stored=0.00366552, fresh=nan, rel_diff=nan
twiss @elt DR36 (out) | 1.0 | +0.00000000000000e+00: stored=0.0544065, fresh=nan, rel_diff=nan
[INFO ] [fault.py ] Skipped! Solving this problem took 0:00:00.144954. Results are:
====================================================================================================
Objective | wgt. | ideal value | final residuals
----------------------------------------------------------------------------------------------------
w_kin @elt DR36 (out) | 1.0 | +2.57715511997316e+01 | +1.80561602717333e-03
phi_abs @elt DR36 (out) | 1.0 | +1.77990507535786e+02 | +3.66552096289752e-03
twiss @elt DR36 (out) | 1.0 | +0.00000000000000e+00 | +5.44064902028991e-02
====================================================================================================
Additional info: PredefinedSolution
Predefined solution, no optimization performed.
[INFO ] [fault_scenario.py ] Retuned cavities:
====================================================================================================
name status k_e phi_0_abs phi_0_rel v_cav_mv phi_s
0 FM9 compensate (in progress) 1.319017 289.339820 205.351492 0.576403 -25.051825
1 FM10 compensate (in progress) 2.068441 54.867062 191.154235 0.925300 -43.041931
2 FM11 failed 0.000000 289.337404 188.548608 NaN NaN
3 FM12 compensate (in progress) 2.769461 109.118120 206.454050 1.291172 -33.318239
====================================================================================================
[INFO ] [fault_scenario.py ] Solving all the optimization problems took 0:00:00.154509
[INFO ] [list_of_simulation_output_evaluators.py]
====================================================================================================
Fit quality:(FIXME: settings in FaultScenario, not config_manager)
----------------------------------------------------------------------------------------------------
end comp zone (DR36) end linac RMS [usual units]
$W_{kin}$ [MeV] 0.007% -0.004% 0.000
Beam phase [rad] 0.002% -0.000% 0.000
Norm. $\sigma_\phi$ @ $1\sigma$ [rad] 3.724% 1.380% 0.000
Norm. $\sigma_\phi$ @ $1\sigma$ [MeV] -4.499% -2.446% 0.000
Norm. $\epsilon_{\phi W}$ [$\pi$.rad.MeV] -0.001% -0.001% 0.000
$M_{z\delta}$ 0.054 0.046 nan
====================================================================================================
/home/docs/checkouts/readthedocs.org/user_builds/lightwin/envs/latest/lib/python3.12/site-packages/lightwin/beam_calculation/envelope_1d/transfer_matrices.py:276: RuntimeWarning: invalid value encountered in scalar divide
scaled_e_middle /= gamma_middle * beta_m**2
/home/docs/checkouts/readthedocs.org/user_builds/lightwin/envs/latest/lib/python3.12/site-packages/numpy/linalg/_linalg.py:2406: RuntimeWarning: invalid value encountered in det
r = _umath_linalg.det(a, signature=signature)
Comparing several scenarios
[10]:
pickle_paths = {
"Reference": pickle_folder / "reference.pkl",
"000001": {
"Solution": pickle_folder / "solution-000001-4cavs.pkl",
"Alternative (3 compensating)": pickle_folder / "solution-000001-3cavs.pkl"
}
}
override["files"] = {"pickle_paths": pickle_paths}
override["wtf"] = {"k": 4}
config = process_config(toml_filepath, toml_keys, override=override)
[INFO ] [table_spec.py ] .toml table [files] loaded!
[INFO ] [table_spec.py ] .toml table [beam] loaded!
[INFO ] [table_spec.py ] .toml table [generic_envelope1d] loaded!
[INFO ] [table_spec.py ] .toml table [plots_minimal] loaded!
[INFO ] [table_spec.py ] .toml table [fit_phi_s_design_space] loaded!
[INFO ] [table_spec.py ] .toml table [generic_wtf] loaded!
[INFO ] [log_manager.py ] Starting log for LightWin - Version: 0.16.3.dev1+g4c96aaf, Commit: 4c96aaf2a12e1dbfd514865321331c13ad57cc82
[INFO ] [files_specs.py ] Setting project_path = PosixPath('/home/docs/checkouts/readthedocs.org/user_builds/lightwin/envs/latest/lib/python3.12/site-packages/lightwin/data/ads/lw_results')
Setting log_file = 'lightwin.log'
[11]:
fault_scenarios = run_simulation(config)
[INFO ] [factory.py ] Re-using previous BeamCalculatorsFactory instance.
[INFO ] [factory.py ] Re-using existing BeamCalculator: 0_Envelope1D
[INFO ] [factory.py ] Loading Reference from pickle: /home/docs/checkouts/readthedocs.org/user_builds/lightwin/checkouts/latest/docs/manual/notebooks/pickles/reference.pkl
[INFO ] [factory.py ] Created 000000_Reference Accelerator by unpickling '/home/docs/checkouts/readthedocs.org/user_builds/lightwin/checkouts/latest/docs/manual/notebooks/pickles/reference.pkl'.
[INFO ] [accelerator.py ] Created a ListOfElements ecompassing all linac. Created with:
dat_file = PosixPath('/home/docs/checkouts/readthedocs.org/user_builds/lightwin/envs/latest/lib/python3.12/site-packages/lightwin/data/ads/ads.dat')
w_kin_in = 20.00 MeV
phi_abs_in = 0.00 rad
[INFO ] [factory.py ] Created 000001_Solution Accelerator (will be pickled to '/home/docs/checkouts/readthedocs.org/user_builds/lightwin/checkouts/latest/docs/manual/notebooks/pickles/solution-000001-4cavs.pkl').
[INFO ] [factory.py ] Loading Alternative (3 compensating) from pickle: /home/docs/checkouts/readthedocs.org/user_builds/lightwin/checkouts/latest/docs/manual/notebooks/pickles/solution-000001-3cavs.pkl
[INFO ] [factory.py ] Loading additional accelerator '000001_Alternative (3 compensating)' from pickle.
[WARNING ] [factory.py ] Behavior of additional Accelerator is not well defined. In particular if there are several FaultScenario.
[INFO ] [beam_calculator.py ] Skipped calculation of unpickled Accelerator: 000000_Reference
[INFO ] [beam_calculator.py ] Elapsed time in beam calculation: 0:00:00.000274
[INFO ] [pickling.py ] Pickled 000001_Solution to /home/docs/checkouts/readthedocs.org/user_builds/lightwin/checkouts/latest/docs/manual/notebooks/pickles/solution-000001-4cavs.pkl.
[INFO ] [beam_calculator.py ] Elapsed time in beam calculation: 0:00:00.584973
[WARNING ] [objective.py ] key = 'to_deg' is recommended to avoid undetermined behavior but was not found.
Objective(name=$W_{kin}$ [MeV], weight=1.0, get_key=w_kin, get_kwargs={'elt': Drift # 91 | DRIFT 150 100 0 0 0, 'pos': 'out', 'to_numpy': False}, ideal_value=27.089335316883677, descriptor=Minimize diff. of w_kin between ref and fix at the end of the compensation zone.)
[WARNING ] [objective.py ] key = 'to_deg' is recommended to avoid undetermined behavior but was not found.
Objective(name=$M_{z\delta}$, weight=1.0, get_key=twiss, get_kwargs={'elt': Drift # 91 | DRIFT 150 100 0 0 0, 'pos': 'out', 'to_numpy': True, 'phase_space_name': 'zdelta'}, ideal_value=0.0, descriptor=Minimize mismatch factor in the [z-delta] plane.)
[INFO ] [fault_scenario.py ] Created a ListOfElements ecompassing a linac subset.
Encompasses: FM9 to DR42
w_kin_in = 23.50 MeV
phi_abs_in = 133.38 rad
[INFO ] [fault.py ] Starting resolution of optimization problem defined by:
====================================================================================================
Variable | Element | x_0 | Lower lim | Upper lim
----------------------------------------------------------------------------------------------------
$\phi_s$ [deg] | FM10 | -41.965 | -90.000 | -25.179
$\phi_s$ [deg] | FM12 | -41.305 | -90.000 | -24.783
$\phi_s$ [deg] | FM9 | -41.751 | -90.000 | -25.050
$\phi_s$ [deg] | FM13 | -40.409 | -90.000 | -24.245
$k_e$ [1] | FM10 | 1.649 | 1.319 | 4.933
$k_e$ [1] | FM12 | 1.698 | 1.359 | 4.933
$k_e$ [1] | FM9 | 1.649 | 1.319 | 4.933
$k_e$ [1] | FM13 | 1.762 | 1.409 | 4.933
====================================================================================================
====================================================================================================
Constraint | Element | x_0 | Lower lim | Upper lim
----------------------------------------------------------------------------------------------------
====================================================================================================
====================================================================================================
Objective | wgt. | ideal value
----------------------------------------------------------------------------------------------------
w_kin @elt DR42 (out) | 1.0 | +2.70893353168837e+01
phi_abs @elt DR42 (out) | 1.0 | +2.05483843906372e+02
twiss @elt DR42 (out) | 1.0 | +0.00000000000000e+00
====================================================================================================
[INFO ] [fault.py ] Finished! Solving this problem took 0:00:30.373494. Results are:
====================================================================================================
Objective | wgt. | ideal value | final residuals
----------------------------------------------------------------------------------------------------
w_kin @elt DR42 (out) | 1.0 | +2.70893353168837e+01 | +7.92761390755459e-07
phi_abs @elt DR42 (out) | 1.0 | +2.05483843906372e+02 | +8.27351698262646e-06
twiss @elt DR42 (out) | 1.0 | +0.00000000000000e+00 | +1.87293345763617e-05
====================================================================================================
Additional info: DownhillSimplex
Optimization terminated successfully.
[INFO ] [fault_scenario.py ] Retuned cavities:
====================================================================================================
name status k_e phi_0_abs phi_0_rel v_cav_mv phi_s
0 FM9 compensate (in progress) 1.797627 285.942071 201.953744 0.786849 -28.379899
1 FM10 compensate (in progress) 1.839455 62.110986 195.744103 0.830846 -39.929383
2 FM11 failed 0.000000 289.337404 173.958346 NaN NaN
3 FM12 compensate (in progress) 2.155776 116.943483 196.442884 1.003247 -44.046513
4 FM13 compensate (in progress) 2.018030 89.361529 220.132387 0.976918 -26.225177
====================================================================================================
[INFO ] [fault_scenario.py ] Solving all the optimization problems took 0:00:30.998726
[INFO ] [list_of_simulation_output_evaluators.py]
====================================================================================================
Fit quality:(FIXME: settings in FaultScenario, not config_manager)
----------------------------------------------------------------------------------------------------
end comp zone (DR42) end linac RMS [usual units]
$W_{kin}$ [MeV] -0.000% 0.000% 0.000
Beam phase [rad] 0.000% -0.000% 0.000
Norm. $\sigma_\phi$ @ $1\sigma$ [rad] 0.001% 0.003% 0.000
Norm. $\sigma_\phi$ @ $1\sigma$ [MeV] 0.000% -0.002% 0.000
Norm. $\epsilon_{\phi W}$ [$\pi$.rad.MeV] -0.000% -0.000% 0.000
$M_{z\delta}$ 0.000 0.000 nan
====================================================================================================
[INFO ] [pickling.py ] Pickled 000001_Solution to /home/docs/checkouts/readthedocs.org/user_builds/lightwin/checkouts/latest/docs/manual/notebooks/pickles/solution-000001-4cavs.pkl.
Clean pickles folder for the next executions
[12]:
for file in pickle_folder.iterdir():
os.remove(file)