From 0ed7c6167e225d1a026272276b6a1db6b2f3a417 Mon Sep 17 00:00:00 2001
From: Gamelin Alexis <alexis.gamelin@synchrotron-soleil.fr>
Date: Thu, 29 Jun 2023 19:14:20 +0200
Subject: [PATCH] Add loss_factor computation for WakeFunction

Change the default binning for WakePotential to 80 (better value for most cases).
---
 mbtrack2/impedance/wakefield.py    | 30 ++++++++++++++++++++++++++++++
 mbtrack2/tracking/wakepotential.py |  2 +-
 2 files changed, 31 insertions(+), 1 deletion(-)

diff --git a/mbtrack2/impedance/wakefield.py b/mbtrack2/impedance/wakefield.py
index 5de4a8f..3171251 100644
--- a/mbtrack2/impedance/wakefield.py
+++ b/mbtrack2/impedance/wakefield.py
@@ -277,6 +277,8 @@ class WakeFunction(ComplexData):
         Compute a wake function from wake potential data.
     plot()
         Plot the wake function data.
+    loss_factor(sigma)
+        Compute the loss factor or the kick factor for a Gaussian bunch.
     """
 
     def __init__(self,
@@ -454,6 +456,34 @@ class WakeFunction(ComplexData):
         label = r"$W_{" + self.component_type + r"}$" + unit
         ax.set_ylabel(label)
         return ax
+    
+    def loss_factor(self, sigma):
+        """
+        Compute the loss factor or the kick factor for a Gaussian bunch. 
+        Formulas from Eq. (5.6), Eq. (5.12) and Eq. (5.17) of [1].
+        
+        Parameters
+        ----------
+        sigma : float
+            RMS bunch length in [s]
+        
+        Returns
+        -------
+        kloss: float
+            Loss factor in [V/C] or kick factor in [V/C/m] depanding on the 
+            impedance type.
+            
+        References
+        ----------
+        [1] : Zotter, Bruno W., and Semyon A. Kheifets. Impedances and wakes 
+        in high-energy particle accelerators. World Scientific, 1998.
+        
+        """
+        time = self.data.index
+        S = 1/(2*np.sqrt(np.pi)*sigma)*np.exp(-1*time**2/(4*sigma**2))
+        kloss = trapz(x = time, y = self.data["real"]*S)
+
+        return kloss
         
 class Impedance(ComplexData):
     """
diff --git a/mbtrack2/tracking/wakepotential.py b/mbtrack2/tracking/wakepotential.py
index 14ac151..43a1692 100644
--- a/mbtrack2/tracking/wakepotential.py
+++ b/mbtrack2/tracking/wakepotential.py
@@ -70,7 +70,7 @@ class WakePotential(Element):
         
     """
     
-    def __init__(self, ring, wakefield, n_bin=65):
+    def __init__(self, ring, wakefield, n_bin=80):
         self.wakefield = wakefield
         self.types = self.wakefield.wake_components
         self.n_types = len(self.wakefield.wake_components)
-- 
GitLab