From 484a22ad777549cb2fb9dc1ef4fc02668d7ccb5c Mon Sep 17 00:00:00 2001
From: Alexis Gamelin <alexis.gamelin@synchrotron-soleil.fr>
Date: Mon, 22 Apr 2024 10:51:55 +0200
Subject: [PATCH] Fix Ncav bug

FB parameters to fix in future!
---
 mbtrack2/tracking/rf.py          | 45 +++++++++++++++++++-------------
 mbtrack2/tracking/synchrotron.py |  5 ++--
 2 files changed, 30 insertions(+), 20 deletions(-)

diff --git a/mbtrack2/tracking/rf.py b/mbtrack2/tracking/rf.py
index 0b66593..149b03e 100644
--- a/mbtrack2/tracking/rf.py
+++ b/mbtrack2/tracking/rf.py
@@ -1142,37 +1142,46 @@ class CavityResonator():
 
         return pos, voltage_rec
     
-    def to_pycolleff(self):
+    def to_pycolleff(self, Impedance=True):
         from pycolleff.longitudinal_equilibrium import ImpedanceSource
         
         cav = ImpedanceSource()
         cav.harm_rf = self.m
         cav.Q = self.QL
         RoverQ = self.RL/self.QL
-        cav.shunt_impedance = self.Ncav * RoverQ * cav.Q
+        cav.shunt_impedance = RoverQ * cav.Q
         cav.ang_freq_rf = self.ring.omega1
         cav.ang_freq = cav.harm_rf * cav.ang_freq_rf
         cav.detune_w = 2 * np.pi * self.detune
-        cav.calc_method = ImpedanceSource.Methods.ImpedanceDFT
         if self.Vg != 0:
             cav.active_passive = ImpedanceSource.ActivePassive.Active
-            # Define PID transfer function for control loop
-            def pid_transfer_func(w, wrf, gain=1, kp=1, ki=1, kd=0, delay=0):
-                # If you do not want to worry about the details of the voltage gap
-                # control system, you can use SIRIUS parameters in this function.
-                phase = wrf * delay
-                exp_delay = np.exp(-1j * delay * w)
-                exp_phase = np.exp(1j * phase)
-                eps = 1e-16
-                pid_ctrl = kp + ki / 1j / (w - wrf + eps) + kd * 1j * (w - wrf)
-                transfer = gain * pid_ctrl * exp_delay * exp_phase
-                return transfer
-            
-            cav.loop_ctrl_ang_freq = self.ring.omega1
-            cav.loop_ctrl_transfer = partial(pid_transfer_func, kp=1, ki=1, delay=0)
-            
+            if Impedance:
+                cav.calc_method = ImpedanceSource.Methods.ImpedanceDFT
+                # Define PID transfer function for control loop
+                def pid_transfer_func(w, wrf, gain=1, kp=1, ki=1, kd=0, delay=0):
+                    phase = wrf * delay
+                    exp_delay = np.exp(-1j * delay * w)
+                    exp_phase = np.exp(1j * phase)
+                    eps = 1e-16
+                    pid_ctrl = kp + ki / 1j / (w - wrf + eps) + kd * 1j * (w - wrf)
+                    transfer = gain * pid_ctrl * exp_delay * exp_phase
+                    return transfer
+                
+                delay = 1.9e-6
+                ki = 0.01
+                kp = 2.96e-6
+                kd = 0
+                cav.loop_ctrl_ang_freq = self.ring.omega1
+                cav.loop_ctrl_transfer = partial(pid_transfer_func, kp=kp, ki=ki, delay=delay)
+                # cav.loop_ctrl_transfer = partial(pid_transfer_func, kp=0, ki=1, delay=0)
+            else:
+                cav.calc_method = ImpedanceSource.Methods.Wake         
         else:
             cav.active_passive = ImpedanceSource.ActivePassive.Passive
+            if Impedance:
+                cav.calc_method = ImpedanceSource.Methods.ImpedanceDFT
+            else:
+                cav.calc_method = ImpedanceSource.Methods.Wake     
         return cav
         
 
diff --git a/mbtrack2/tracking/synchrotron.py b/mbtrack2/tracking/synchrotron.py
index 70b4f63..8a4dd02 100644
--- a/mbtrack2/tracking/synchrotron.py
+++ b/mbtrack2/tracking/synchrotron.py
@@ -585,7 +585,7 @@ class Synchrotron:
                                      TimeLag=TimeLag)
         return at_simple_ring
     
-    def to_pycolleff(self, I0, Vrf, bunch_number):
+    def to_pycolleff(self, I0, Vrf, bunch_number, delta=0):
         from pycolleff.colleff import Ring
         
         ring = Ring()
@@ -607,8 +607,9 @@ class Synchrotron:
         ring.damptx = self.tau[0]  # [s]
         ring.dampty = self.tau[1]  # [s]
         ring.dampte = self.tau[2]  # [s]
-        ring.en_lost_rad = self.U0  # [eV]
+        ring.en_lost_rad = self.U0 # [eV]
         ring.gap_voltage = Vrf  # [V]
+        ring.delta_HC = delta
         
         return ring
 
-- 
GitLab