From 2f9e4bb3f9849d898d79ee523f1827547b2fb3cf Mon Sep 17 00:00:00 2001
From: Michiel Cottaar <michiel.cottaar@ndcn.ox.ac.uk>
Date: Fri, 2 Feb 2024 17:24:08 +0000
Subject: [PATCH] Make nsamples a non-integer variable

---
 src/readouts/ADCs.jl | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)

diff --git a/src/readouts/ADCs.jl b/src/readouts/ADCs.jl
index cc10a95..3e265d7 100644
--- a/src/readouts/ADCs.jl
+++ b/src/readouts/ADCs.jl
@@ -1,7 +1,7 @@
 module ADCs
 import JuMP: @constraint
-import ...Variables: variables, dwell_time, duration, effective_time, get_free_variable, VariableType
-import ...BuildingBlocks: BuildingBlock, apply_simple_constraint!, set_simple_constraints!
+import ...Variables: variables, dwell_time, duration, effective_time, get_free_variable, VariableType, nsamples
+import ...BuildingBlocks: BuildingBlock, apply_simple_constraint!, set_simple_constraints!, fixed
 import ...BuildSequences: global_model
 
 """
@@ -10,23 +10,23 @@ import ...BuildSequences: global_model
 Adds a readout event with `nsamples` readouts.
 
 ## Parameters
-- `nsamples`: number of samples in the readout.
 - `center_halfway`: by default the `time_to_center` is assumed to be half of the `duration`. Set this to false to disable this assumption.
 
 ## Variables
+- `nsamples`: number of samples in the readout. This can be a non-integer value during optimisation
 - `dwell_time`: Time between each readout sample in ms.
 - `duration`: Total duration of the ADC event in ms.
 - `time_to_center`: time till the center of k-space.
 """
 struct ADC <: BuildingBlock
-    nsamples :: Integer
+    nsamples :: VariableType
     dwell_time :: VariableType
     time_to_center :: VariableType
 end
 
-function ADC(nsamples; dwell_time=nothing, time_to_center=nothing, center_halfway=true, kwargs...)
+function ADC(nsamples=nothing, dwell_time=nothing, time_to_center=nothing, center_halfway=true, kwargs...)
     res = ADC(
-        nsamples,
+        get_free_variable(nsamples),
         get_free_variable(dwell_time),
         get_free_variable(time_to_center),
     )
@@ -41,11 +41,19 @@ function ADC(nsamples; dwell_time=nothing, time_to_center=nothing, center_halfwa
     return res
 end
 
+nsamples(adc::ADC) = adc.nsamples
 dwell_time(adc::ADC) = adc.dwell_time
-duration(adc::ADC) = adc.nsamples * dwell_time(adc)
+duration(adc::ADC) = nsamples(adc) * dwell_time(adc)
 time_to_center(adc::ADC) = adc.time_to_center
 effective_time(adc::ADC) = time_to_center(adc)
 
-variables(::Type{<:ADC}) = [dwell_time, duration, time_to_center]
+variables(::Type{<:ADC}) = [nsamples, dwell_time, duration, time_to_center]
+
+function fixed(adc::ADC)
+    # round nsamples during fixing
+    nsamples = Int(round(adc.nsamples))
+    dwell_time = duration(adc) / nsamples
+    return ADC(nsamples, dwell_time, time_to_center(adc))
+end
 
 end
\ No newline at end of file
-- 
GitLab