From b815e4dfb5283a151ef0b5b14ff36cd3286407d0 Mon Sep 17 00:00:00 2001
From: Gamelin Alexis <alexis.gamelin@synchrotron-soleil.fr>
Date: Wed, 2 Aug 2023 11:50:45 +0200
Subject: [PATCH] [Fix] Sign of long wake in LongRangeResistiveWall

Fix wrong sign of the longitudinal wake in LongRangeResistiveWall, thanks to Keon Hee Kim for pointing this out.
Fix ImpedanceModel.add method to raise error before making modifications.
---
 mbtrack2/impedance/impedance_model.py | 4 ++--
 mbtrack2/tracking/wakepotential.py    | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/mbtrack2/impedance/impedance_model.py b/mbtrack2/impedance/impedance_model.py
index bc3da3c..cf58638 100644
--- a/mbtrack2/impedance/impedance_model.py
+++ b/mbtrack2/impedance/impedance_model.py
@@ -102,8 +102,6 @@ class ImpedanceModel():
         None.
 
         """
-        self.wakefields.append(wakefield)
-        self.positions.append(positions)
         if name is None:
             name = wakefield.name
         if name is None:
@@ -112,6 +110,8 @@ class ImpedanceModel():
             self.names.append(name)
         else:
             raise ValueError("This name is already taken.")
+        self.wakefields.append(wakefield)
+        self.positions.append(positions)
 
     def add_global(self, wakefield, name=None):
         """
diff --git a/mbtrack2/tracking/wakepotential.py b/mbtrack2/tracking/wakepotential.py
index 43a1692..19bee81 100644
--- a/mbtrack2/tracking/wakepotential.py
+++ b/mbtrack2/tracking/wakepotential.py
@@ -793,7 +793,7 @@ class LongRangeResistiveWall(Element):
         for wake_type in self.types:
             kick = self.get_kick(rank, wake_type)
             if wake_type == "Wlong":
-                bunch["delta"] += kick / self.ring.E0
+                bunch["delta"] -= kick / self.ring.E0
             elif wake_type == "Wxdip":
                 bunch["xp"] += kick / self.ring.E0
             elif wake_type == "Wydip":
-- 
GitLab