From ab0bfbc2329ffb79c58b59858e69acc855bbfa74 Mon Sep 17 00:00:00 2001
From: Michiel Cottaar <MichielCottaar@protonmail.com>
Date: Wed, 31 Jul 2024 11:24:15 +0100
Subject: [PATCH] Set secondary cost to drive pulse phase and frequency to zero

---
 src/components/pulses/constant_pulses.jl | 3 ++-
 src/components/pulses/instant_pulses.jl  | 1 +
 src/components/pulses/sinc_pulses.jl     | 1 +
 3 files changed, 4 insertions(+), 1 deletion(-)

diff --git a/src/components/pulses/constant_pulses.jl b/src/components/pulses/constant_pulses.jl
index 4af0975..cf6f2f5 100644
--- a/src/components/pulses/constant_pulses.jl
+++ b/src/components/pulses/constant_pulses.jl
@@ -1,7 +1,7 @@
 module ConstantPulses
 import JuMP: @constraint
 import ...AbstractTypes: RFPulseComponent, split_timestep
-import ....Variables: VariableType, set_simple_constraints!, make_generic, get_free_variable, adjust_internal, variables, @defvar, apply_simple_constraint!
+import ....Variables: VariableType, set_simple_constraints!, make_generic, get_free_variable, adjust_internal, variables, @defvar, apply_simple_constraint!, add_cost_function!
 import ..GenericPulses: GenericPulse
 
 """
@@ -34,6 +34,7 @@ function ConstantPulse(; amplitude=nothing, duration=nothing, phase=nothing, fre
     )
     apply_simple_constraint!(res.amplitude, :>=, 0)
     set_simple_constraints!(res, kwargs)
+    add_cost_function!(res.frequency^2 + res.phase^2)
     return res
 end
 
diff --git a/src/components/pulses/instant_pulses.jl b/src/components/pulses/instant_pulses.jl
index 2529668..546cab9 100644
--- a/src/components/pulses/instant_pulses.jl
+++ b/src/components/pulses/instant_pulses.jl
@@ -28,6 +28,7 @@ function InstantPulse(; flip_angle=nothing, phase=nothing, group=nothing)
         group
     )
     apply_simple_constraint!(res.flip_angle, :>=, 0)
+    add_cost_function!(res.phase^2)
     return res
 end
 
diff --git a/src/components/pulses/sinc_pulses.jl b/src/components/pulses/sinc_pulses.jl
index b445ef1..1ff06a8 100644
--- a/src/components/pulses/sinc_pulses.jl
+++ b/src/components/pulses/sinc_pulses.jl
@@ -55,6 +55,7 @@ function SincPulse(;
         apply_simple_constraint!(res.lobe_duration, :>=, 0)
     end
     set_simple_constraints!(res, kwargs)
+    add_cost_function!(res.frequency^2 + res.phase^2)
     return res
 end
 
-- 
GitLab