Repository: desy-ml/cheetah (language: python)

Issue:
Tracking fails with `TransverseDeflectingCavity` if embedded in a segment
The following example currently fails to track:

```python
import cheetah
import torch

segment = cheetah.Segment(elements=[
    cheetah.TransverseDeflectingCavity(length=torch.tensor(1.0))
])
beam = cheetah.ParticleBeam.from_parameters()

segment.track(beam)
```

The code raises an `NotImplementedError` in `Element.track()`.


Discussion / hints:

Fix patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 18c1adc5..e7f7c395 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -21,7 +21,7 @@ This is a major release with significant upgrades under the hood of Cheetah. Des
 - Moving `Element`s and `Beam`s to a different `device` and changing their `dtype` like with any `torch.nn.Module` is now possible (see #209) (@jank324)
 - `Quadrupole` now supports tracking with Cheetah's matrix-based method or with Bmad's more accurate method (see #153) (@jp-ga, @jank324)
 - Port Bmad-X tracking methods to Cheetah for `Quadrupole`, `Drift`, and `Dipole` (see #153, #240) (@jp-ga, @jank324)
-- Add `TransverseDeflectingCavity` element (following the Bmad-X implementation) (see #240, #278) (@jp-ga, @cr-xu, @jank324)
+- Add `TransverseDeflectingCavity` element (following the Bmad-X implementation) (see #240, #278 #296) (@jp-ga, @cr-xu, @jank324)
 - `Dipole` and `RBend` now take a focusing moment `k1` (see #235, #247) (@hespe)
 - Implement a converter for lattice files imported from Elegant (see #222, #251, #273, #281) (@hespe, @jank324)
 
diff --git a/cheetah/accelerator/transverse_deflecting_cavity.py b/cheetah/accelerator/transverse_deflecting_cavity.py
index fd4ed2af..e4e9a3cb 100644
--- a/cheetah/accelerator/transverse_deflecting_cavity.py
+++ b/cheetah/accelerator/transverse_deflecting_cavity.py
@@ -99,7 +99,8 @@ def is_active(self) -> bool:
 
     @property
     def is_skippable(self) -> bool:
-        return not self.is_active
+        # TODO: Implement drrift-like `transfer_map` and set to `self.is_active`
+        return False
 
     def track(self, incoming: Beam) -> Beam:
         """


Test patch:
diff --git a/tests/test_transverse_deflecting_cavity.py b/tests/test_transverse_deflecting_cavity.py
index 231c883d..3555532b 100644
--- a/tests/test_transverse_deflecting_cavity.py
+++ b/tests/test_transverse_deflecting_cavity.py
@@ -133,3 +133,16 @@ def test_transverse_deflecting_cavity_all_parameters_vectorization():
     outgoing_beam = tdc.track(incoming_beam)
 
     assert outgoing_beam.particles.shape[:-2] == torch.Size([4, 3, 2, 2])
+
+
+def test_tracking_inactive_in_segment():
+    """
+    Test that tracking through a `Segment` that contains an inactive
+    `TransverseDeflectingCavity` does not throw an exception. This was an issue in #290.
+    """
+    segment = cheetah.Segment(
+        elements=[cheetah.TransverseDeflectingCavity(length=torch.tensor(1.0))]
+    )
+    beam = cheetah.ParticleBeam.from_parameters()
+
+    segment.track(beam)