From 82dbcc296973cfd26a38b1411358da8240b8e72c Mon Sep 17 00:00:00 2001
From: Gamelin Alexis <gamelin@synchrotron-soleil.fr>
Date: Wed, 31 Mar 2021 18:10:37 +0200
Subject: [PATCH] Fix bug for cavity resonator

In track method of cavity resonator, there was an additional phasor decay of bin length.
To avoid that the good boundaries are set bins[0] instead of center[0].
For now the bins are also shared in Beam.mpi_share_distributions
---
 tracking/particles.py |  3 +++
 tracking/rf.py        | 19 ++++++++++++++-----
 2 files changed, 17 insertions(+), 5 deletions(-)

diff --git a/tracking/particles.py b/tracking/particles.py
index 68e2914..451ee4e 100644
--- a/tracking/particles.py
+++ b/tracking/particles.py
@@ -685,6 +685,9 @@ class Beam:
         bunch = self[self.mpi.bunch_num]
         bins, sorted_index, profile, center = bunch.binning(n_bin=75)
         
+        self.mpi.bins_all = np.empty((len(self), len(bins)), dtype=np.float64)
+        self.mpi.comm.Allgather([bins,  MPI.DOUBLE], [self.mpi.bins_all, MPI.DOUBLE])
+        
         self.mpi.center_all = np.empty((len(self), len(center)), dtype=np.float64)
         self.mpi.comm.Allgather([center,  MPI.DOUBLE], [self.mpi.center_all, MPI.DOUBLE])
         
diff --git a/tracking/rf.py b/tracking/rf.py
index 8f7998d..a12098a 100644
--- a/tracking/rf.py
+++ b/tracking/rf.py
@@ -190,7 +190,10 @@ class CavityResonator():
         self.nturn = 0
     
     def track(self, beam, ref_frame="beam"):
-        """version type mbtrack + formule analytique"""
+        """version type mbtrack + formule analytique
+        
+        phasor is given at t=0 synchronous particle of the bunch 0
+        """
         
         if self.tracking is False:
             self.init_tracking(beam)
@@ -201,6 +204,7 @@ class CavityResonator():
             
             if beam.mpi_switch:
                 # get shared bunch profile for current bunch
+                bins = beam.mpi.bins_all[j]
                 center = beam.mpi.center_all[j]
                 profile = beam.mpi.profile_all[j]
                 bin_length = center[1]-center[0]
@@ -218,10 +222,11 @@ class CavityResonator():
             
             # phasor is given at t=0 synchronous particle of the bunch
             # remove part of beam phasor decay to be at the start of the binning
-            self.phasor_decay(center[0], ref_frame=ref_frame)
+            self.phasor_decay(bins[0], ref_frame=ref_frame)
             
             if index != self.bunch_index:
                 self.phasor_evol(profile, bin_length, charge_per_mp, ref_frame=ref_frame)
+                print('here')
             else:
                 # modify beam phasor
                 for i, center0 in enumerate(center):
@@ -241,13 +246,16 @@ class CavityResonator():
                     self.beam_phasor -= 2*charge_per_mp*self.loss_factor*mp_per_bin
                     self.phasor_decay(bin_length, ref_frame=ref_frame)
                 
-            self.phasor_decay( (self.distance[index] * self.ring.T1) - center[-1], ref_frame=ref_frame)
+            self.phasor_decay(-1*bins[-1], ref_frame=ref_frame)
+            self.phasor_decay(self.distance[index] * self.ring.T1, ref_frame=ref_frame)
             
             if index == self.bunch_index:
                 # apply kick
                 bunch["delta"] += energy_change
                 bunch.energy_change = energy_change
                 
+            self.nturn += 1
+                
         
     def init_phasor(self, beam):
         """version type mbtrack + formule analytique"""
@@ -264,6 +272,7 @@ class CavityResonator():
                 
                 if beam.mpi_switch:
                     # get shared bunch profile for current bunch
+                    bins = beam.mpi.bins_all[j]
                     center = beam.mpi.center_all[j]
                     profile = beam.mpi.profile_all[j]
                     bin_length = center[1]-center[0]
@@ -276,9 +285,9 @@ class CavityResonator():
                 
                 # phasor is given at t=0 synchronous particle of the bunch
                 # remove part of beam phasor decay to be at the start of the binning
-                self.phasor_decay(center[0], ref_frame="rf")
+                self.phasor_decay(bins[0], ref_frame="rf")
                 self.phasor_evol(profile, bin_length, charge_per_mp, ref_frame="rf")
-                self.phasor_decay( (self.distance[index] * self.ring.T1) - center[-1], ref_frame="rf")
+                self.phasor_decay( (self.distance[index] * self.ring.T1) - bins[-1], ref_frame="rf")
             
     def phasor_decay(self, time, ref_frame="beam"):
         """
-- 
GitLab