From 5bd67c0c0af93d9fc43555cb5b441f13721832fe Mon Sep 17 00:00:00 2001
From: Michiel Cottaar <michiel.cottaar@ndcn.ox.ac.uk>
Date: Fri, 26 Jan 2024 14:55:23 +0000
Subject: [PATCH] Fix concretisation of PulsedGradient

---
 src/concrete_blocks.jl            | 24 +++++++++++++++++++-----
 src/gradients/pulsed_gradients.jl |  2 +-
 2 files changed, 20 insertions(+), 6 deletions(-)

diff --git a/src/concrete_blocks.jl b/src/concrete_blocks.jl
index 9570a0f..b3ea308 100644
--- a/src/concrete_blocks.jl
+++ b/src/concrete_blocks.jl
@@ -1,7 +1,7 @@
 module ConcreteBlocks
-import JuMP: has_values, optimize!
+import JuMP: has_values, optimize!, value
 import ..BuildingBlocks: BuildingBlock, BuildingBlockPlaceholder, properties, duration
-import ..SequenceBuilders: SequenceBuilder, to_block, AbstractSequence
+import ..SequenceBuilders: SequenceBuilder, to_block, AbstractSequence, TR, get_blocks
 
 abstract type AbstractConcreteBlock <: BuildingBlock end
 
@@ -40,7 +40,17 @@ function ConcreteGradient(arr::Vector)
     )
 end
 
+function ConcreteGradient(times, gradients)
+    ConcreteGradient(
+        times,
+        [g[1] for g in gradients],
+        [g[2] for g in gradients],
+        [g[3] for g in gradients],
+    )
+end
+
 ConcreteGradient(::Nothing) = nothing
+ConcreteGradient(values::Tuple{<:Vector, <:Vector}) = ConcreteGradient(values...)
 ConcreteGradient(values::Tuple{<:Vector, <:Vector, <:Vector, <:Vector}) = ConcreteGradient(values...)
 
 
@@ -55,12 +65,13 @@ struct ConcreteBlock <: AbstractConcreteBlock
     pulse :: Union{ConcreteRFPulse, Nothing}
     gradient :: Union{ConcreteGradient, Nothing}
     readout_times :: Vector{Float64}
+    rotate_gradients :: Bool
 end
 
 ConcreteBlock(args...; kwargs...) = BuildingBlockPlaceholder{ConcreteBlock}(args...; kwargs...)
 
-function ConcreteBlock(builder::AbstractSequence, duration::Number; pulse=nothing, gradient=nothing, readout_times=Number[])
-    ConcreteBlock(builder, duration, ConcreteRFPulse(pulse), ConcreteGradient(gradient), Float64.(readout_times))
+function ConcreteBlock(builder::AbstractSequence, duration::Number; pulse=nothing, gradient=nothing, readout_times=Number[], rotate_gradients=false)
+    ConcreteBlock(builder, duration, ConcreteRFPulse(pulse), ConcreteGradient(gradient), Float64.(readout_times), rotate_gradients)
 end
 
 
@@ -112,11 +123,14 @@ struct Sequence <: AbstractSequence
     TR :: Number
 end
 
+TR(seq::Sequence) = seq.TR
+get_blocks(seq::Sequence) = seq.blocks
+
 function Sequence(builder::SequenceBuilder)
     if !has_values(builder)
         optimize!(builder.model)
     end
-    seq = Sequence(AbstractConcreteBlock[], value(TR))
+    seq = Sequence(AbstractConcreteBlock[], value(TR(builder)))
     for block in builder.blocks
         push!(seq.blocks, ConcreteBlock(seq, block))
     end
diff --git a/src/gradients/pulsed_gradients.jl b/src/gradients/pulsed_gradients.jl
index e0a955b..0c4bc43 100644
--- a/src/gradients/pulsed_gradients.jl
+++ b/src/gradients/pulsed_gradients.jl
@@ -132,7 +132,7 @@ function to_concrete_block(s::AbstractSequence, block::PulsedGradient)
     t_start = value(start_time(block))
     t_rise = value(rise_time(block))
     t_d = value(δ(block))
-    return ConcreteBlock(s, t_start + t_d + t_rise, gradients=[
+    return ConcreteBlock(s, t_start + t_d + t_rise, gradient=[
         (t_start, zeros(3)),
         (t_start + t_rise, qvec),
         (t_start + t_d, qvec),
-- 
GitLab