From ab02061fb98063475cf9ff7c46bea9086839250c Mon Sep 17 00:00:00 2001 From: SebastienJoly Date: Thu, 10 Oct 2024 12:37:42 +0200 Subject: [PATCH 1/4] Change np.average to np.ma.average to properly handle cases with zero weights. --- pySC/core/beam.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pySC/core/beam.py b/pySC/core/beam.py index c7134ab..bffd73d 100644 --- a/pySC/core/beam.py +++ b/pySC/core/beam.py @@ -47,8 +47,8 @@ def bpm_reading(SC: SimulatedCommissioning, bpm_ords: ndarray = None, calculate_ bpm_orbits_4d[:, :, :, shot_num], bpm_sums_4d[:, :, :, shot_num] = _real_bpm_reading(SC, tracking_4d, bpm_inds) # mean_bpm_orbits_3d is 3D (dim, BPM, turn) - mean_bpm_orbits_3d = np.average(np.ma.array(bpm_orbits_4d, mask=np.isnan(bpm_orbits_4d)), - weights=np.ma.array(bpm_sums_4d, mask=np.isnan(bpm_sums_4d)), axis=3).filled(np.nan) + mean_bpm_orbits_3d = np.ma.average(np.ma.array(bpm_orbits_4d, mask=np.isnan(bpm_orbits_4d)), + weights=np.ma.array(bpm_sums_4d, mask=np.isnan(bpm_sums_4d)), axis=3).filled(np.nan) # averaging "charge" also when the beam did not reach the location mean_bpm_sums_3d = np.nansum(bpm_sums_4d, axis=3) / SC.INJ.nShots From b58d83b5888f8d6fce7542305a9daf8a171f7496 Mon Sep 17 00:00:00 2001 From: SebastienJoly Date: Wed, 30 Oct 2024 13:14:09 +0100 Subject: [PATCH 2/4] Fixed measured ORM to return to initial state despite corrector strength clipping. --- pySC/correction/loco.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pySC/correction/loco.py b/pySC/correction/loco.py index 52212a8..d24ee2b 100644 --- a/pySC/correction/loco.py +++ b/pySC/correction/loco.py @@ -2,7 +2,7 @@ import numpy as np import multiprocessing from pySC.lattice_properties.response_model import SCgetModelRM, SCgetModelDispersion -from pySC.core.constants import SETTING_ADD, TRACK_ORB +from pySC.core.constants import SETTING_ADD, SETTING_ABS, TRACK_ORB from pySC.core.beam import bpm_reading import matplotlib.pyplot as plt from scipy.optimize import least_squares @@ -71,9 +71,10 @@ def measure_closed_orbit_response_matrix(SC, bpm_ords, cm_ords, dkick=1e-5): cnt = 0 for n_dim in range(2): for cm_ord in cm_ords[n_dim]: + initial_cm_value = SC.get_cm_setpoints(cm_ord, skewness=bool(n_dim)) SC.set_cm_setpoints(cm_ord, dkick, skewness=bool(n_dim), method=SETTING_ADD) closed_orbits1 = bpm_reading(SC, bpm_ords=bpm_ords)[0] - SC.set_cm_setpoints(cm_ord, -dkick, skewness=bool(n_dim), method=SETTING_ADD) + SC.set_cm_setpoints(cm_ord, initial_cm_value, skewness=bool(n_dim), method=SETTING_ABS) response_matrix[:, cnt] = np.ravel((closed_orbits1 - closed_orbits0) / dkick) cnt = cnt + 1 return response_matrix From 0c12d7b9291c971f84bcfd50fe6fa41278a7366c Mon Sep 17 00:00:00 2001 From: SebastienJoly Date: Tue, 5 Nov 2024 11:49:19 +0100 Subject: [PATCH 3/4] Added deepcopy around variables to prevent next line to set their first element to zero. --- pySC/lattice_properties/response_model.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pySC/lattice_properties/response_model.py b/pySC/lattice_properties/response_model.py index 5c3dea5..1d6152e 100644 --- a/pySC/lattice_properties/response_model.py +++ b/pySC/lattice_properties/response_model.py @@ -160,8 +160,8 @@ def SCgetModelRING(SC: SimulatedCommissioning, includeAperture: bool =False) -> ring = SC.IDEALRING.deepcopy() for ord in range(len(SC.RING)): if hasattr(SC.RING[ord], 'SetPointA') and hasattr(SC.RING[ord], 'SetPointB'): - ring[ord].PolynomA = SC.RING[ord].SetPointA - ring[ord].PolynomB = SC.RING[ord].SetPointB + ring[ord].PolynomA = copy.deepcopy(SC.RING[ord].SetPointA) + ring[ord].PolynomB = copy.deepcopy(SC.RING[ord].SetPointB) ring[ord].PolynomA[0] = 0.0 ring[ord].PolynomB[0] = 0.0 if includeAperture: From 3e6006e1582acdcff1780ca4207f818f5245eb99 Mon Sep 17 00:00:00 2001 From: SebastienJoly Date: Thu, 7 Nov 2024 11:34:41 +0100 Subject: [PATCH 4/4] Disentangle changes from two different PR. --- pySC/core/beam.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pySC/core/beam.py b/pySC/core/beam.py index bffd73d..c7134ab 100644 --- a/pySC/core/beam.py +++ b/pySC/core/beam.py @@ -47,8 +47,8 @@ def bpm_reading(SC: SimulatedCommissioning, bpm_ords: ndarray = None, calculate_ bpm_orbits_4d[:, :, :, shot_num], bpm_sums_4d[:, :, :, shot_num] = _real_bpm_reading(SC, tracking_4d, bpm_inds) # mean_bpm_orbits_3d is 3D (dim, BPM, turn) - mean_bpm_orbits_3d = np.ma.average(np.ma.array(bpm_orbits_4d, mask=np.isnan(bpm_orbits_4d)), - weights=np.ma.array(bpm_sums_4d, mask=np.isnan(bpm_sums_4d)), axis=3).filled(np.nan) + mean_bpm_orbits_3d = np.average(np.ma.array(bpm_orbits_4d, mask=np.isnan(bpm_orbits_4d)), + weights=np.ma.array(bpm_sums_4d, mask=np.isnan(bpm_sums_4d)), axis=3).filled(np.nan) # averaging "charge" also when the beam did not reach the location mean_bpm_sums_3d = np.nansum(bpm_sums_4d, axis=3) / SC.INJ.nShots