Skip to content

Commit bb12611

Browse files
Drop redundant T parameters per review feedback
Address @cncastillo's review on PR #782: - DiscreteSequence: drop T<:Real, drop inner constructor, merge t/Δt types - Magnetization: drop storage type params and parameterised Mag{T}(...) helper; T(2) → 2 in mul! and *; keep structural Mag(xy, z) outer for Functors walks - Spinor.Q: drop Base.one. shadowing (extend Base.one(::Spinor) properly); hoist φ_half; @. on final Spinor build - Update DiscreteSequence{T} dispatch sites to DiscreteSequence; T still bound from Phantom{T} / Mag{T} for the remaining T(γ) literals sincos broadcast intentionally not adopted: incompatible with Reactant tracing (no similar() method for array-of-tuples over TracedRNumber).
1 parent ab5c0bd commit bb12611

9 files changed

Lines changed: 34 additions & 48 deletions

File tree

KomaMRIBase/src/datatypes/simulation/DiscreteSequence.jl

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,12 @@ times. DiscreteSequence is the struct used for simulation.
1919
- `seqd`: (`::DiscreteSequence`) DiscreteSequence struct
2020
"""
2121
struct DiscreteSequence{
22-
T<:Real,
2322
GType<:AbstractVector,
2423
B1Type<:AbstractVector,
2524
ΔfType<:AbstractVector,
2625
ψType<:AbstractVector,
2726
ADCType<:AbstractVector{Bool},
2827
tType<:AbstractVector,
29-
ΔtType<:AbstractVector,
3028
}
3129
Gx::GType
3230
Gy::GType
@@ -36,16 +34,7 @@ struct DiscreteSequence{
3634
ψ::ψType
3735
ADC::ADCType
3836
t::tType
39-
Δt::ΔtType
40-
end
41-
42-
function DiscreteSequence(
43-
Gx::AbstractVector{T}, Gy, Gz, B1, Δf, ψ, ADC, t, Δt,
44-
) where {T<:Real}
45-
return DiscreteSequence{
46-
T, typeof(Gx), typeof(B1),
47-
typeof(Δf), typeof(ψ), typeof(ADC), typeof(t), typeof(Δt),
48-
}(Gx, Gy, Gz, B1, Δf, ψ, ADC, t, Δt)
37+
Δt::tType
4938
end
5039

5140
Base.length(seq::DiscreteSequence) = length(seq.Δt)

KomaMRICore/src/datatypes/Spinor.jl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ struct Spinor{V<:AbstractVector}
2929
end
3030
Spinor::Complex{T}, β::Complex{T}) where {T<:Real} = Spinor([α], [β])
3131
Spinor::T, β::T) where {T<:Real} = Spinor([complex(α)], [complex(β)])
32-
one(T::Spinor) = Spinor(1.,0.)
32+
Base.one(::Spinor) = Spinor(1.,0.)
3333
Base.getindex(s::Spinor, i) = Spinor(s.α[i], s.β[i])
3434
Base.view(s::Spinor, i::UnitRange) = @views Spinor(s.α[i], s.β[i])
3535
"""
@@ -158,10 +158,11 @@ IEEE Transactions on Medical Imaging, 10(1), 53-65. doi:10.1109/42.75611
158158
function Q(φ, nxy, nz)
159159
φ_half = φ ./ 2
160160
sin_φ_half = sin.(φ_half)
161-
neg_im = complex.(zero.(φ), -Base.one.(φ))
161+
cos_φ_half = cos.(φ_half)
162+
neg_im = complex.(zero.(φ), -one.(φ))
162163
return Spinor(
163-
cos.(φ_half) .+ neg_im .* nz .* sin_φ_half,
164-
neg_im .* nxy .* sin_φ_half,
164+
(@. cos_φ_half + neg_im * nz * sin_φ_half),
165+
(@. neg_im * nxy * sin_φ_half),
165166
)
166167
end
167168

KomaMRICore/src/simulation/SimMethods/Bloch/cpu/BlochCPU.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ that they can be re-used from block to block.
4747
"""
4848
function run_spin_precession!(
4949
p::Phantom{T},
50-
seq::DiscreteSequence{T},
50+
seq::DiscreteSequence,
5151
sig::AbstractArray{Complex{T}},
5252
M::Mag{T},
5353
sim_method::Bloch,
@@ -105,7 +105,7 @@ optimized for the CPU. Uses preallocation for all arrays to reduce memory usage.
105105
"""
106106
function run_spin_excitation!(
107107
p::Phantom{T},
108-
seq::DiscreteSequence{T},
108+
seq::DiscreteSequence,
109109
sig::AbstractArray{Complex{T}},
110110
M::Mag{T},
111111
sim_method::Bloch,

KomaMRICore/src/simulation/SimMethods/Bloch/gpu/BlochGPU.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ end
2727

2828
function run_spin_precession!(
2929
p::Phantom{T},
30-
seq::DiscreteSequence{T},
30+
seq::DiscreteSequence,
3131
sig::AbstractArray{Complex{T}},
3232
M::Mag{T},
3333
sim_method::SM,
@@ -64,7 +64,7 @@ end
6464

6565
function run_spin_excitation!(
6666
p::Phantom{T},
67-
seq::DiscreteSequence{T},
67+
seq::DiscreteSequence,
6868
sig::AbstractArray{Complex{T}},
6969
M::Mag{T},
7070
sim_method::SM,

KomaMRICore/src/simulation/SimMethods/BlochDict/BlochDict.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ precession.
3636
"""
3737
function run_spin_precession!(
3838
p::Phantom{T},
39-
seq::DiscreteSequence{T},
39+
seq::DiscreteSequence,
4040
sig::AbstractArray{Complex{T}},
4141
M::Mag{T},
4242
sim_method::BlochDict,

KomaMRICore/src/simulation/SimMethods/BlochMagnus/cpu/BlochMagnusCPU.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ end
5252
# Use Bloch implementation for precession
5353
function run_spin_precession!(
5454
p::Phantom{T},
55-
seq::DiscreteSequence{T},
55+
seq::DiscreteSequence,
5656
sig::AbstractArray{Complex{T}},
5757
M::Mag{T},
5858
sim_method::BlochMagnus,
@@ -66,7 +66,7 @@ end
6666
# This part changes a bit more
6767
function run_spin_excitation!(
6868
p::Phantom{T},
69-
seq::DiscreteSequence{T},
69+
seq::DiscreteSequence,
7070
sig::AbstractArray{Complex{T}},
7171
M::Mag{T},
7272
sim_method::BlochMagnus,

KomaMRICore/src/simulation/SimMethods/BlochSimple/BlochSimple.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ precession.
2323
"""
2424
function run_spin_precession!(
2525
p::Phantom{T},
26-
seq::DiscreteSequence{T},
26+
seq::DiscreteSequence,
2727
sig::AbstractArray{Complex{T}},
2828
M::Mag{T},
2929
sim_method::SimulationMethod,
@@ -72,7 +72,7 @@ It gives rise to a rotation of `M0` with an angle given by the efective magnetic
7272
"""
7373
function run_spin_excitation!(
7474
p::Phantom{T},
75-
seq::DiscreteSequence{T},
75+
seq::DiscreteSequence,
7676
sig::AbstractArray{Complex{T}},
7777
M::Mag{T},
7878
sim_method::SimulationMethod,

KomaMRICore/src/simulation/SimMethods/Magnetization.jl

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,20 @@ The Magnetization struct.
1010
# Returns
1111
- `mag`: (`::Mag`) Magnetization struct
1212
"""
13-
mutable struct Mag{T<:Real, XYType<:AbstractVector, ZType<:AbstractVector} <: SpinStateRepresentation{T}
14-
xy::XYType
15-
z::ZType
13+
mutable struct Mag{T<:Real} <: SpinStateRepresentation{T}
14+
xy::AbstractVector
15+
z::AbstractVector
1616
end
1717

18-
function Mag(xy::AbstractVector{Complex{T}}, z::AbstractVector{T}) where {T<:Real}
19-
return Mag{T,typeof(xy),typeof(z)}(xy, z)
20-
end
21-
function Mag{T}(xy::AbstractVector, z::AbstractVector) where {T<:Real}
22-
return Mag{T,typeof(xy),typeof(z)}(xy, z)
23-
end
18+
# Convenience constructor used by Functors/concrete callers; Reactant paths use Mag{T}(...) directly
19+
Mag(xy::AbstractVector{Complex{T}}, z::AbstractVector{T}) where {T<:Real} = Mag{T}(xy, z)
2420

2521
# Required indexing operations
2622
# M[i]
27-
Base.getindex(M::Mag, i::Integer) = Mag(M.xy[i,:], M.z[i,:])
23+
Base.getindex(M::Mag{T}, i::Integer) where {T} = Mag{T}(M.xy[i,:], M.z[i,:])
2824
# M[a:b]
29-
Base.getindex(M::Mag, i) = Mag(M.xy[i], M.z[i])
30-
Base.view(M::Mag, i) = @views Mag(M.xy[i], M.z[i])
25+
Base.getindex(M::Mag{T}, i) where {T} = Mag{T}(M.xy[i], M.z[i])
26+
Base.view(M::Mag{T}, i) where {T} = @views Mag{T}(M.xy[i], M.z[i])
3127

3228
# Definition of rotation Spinor×SpinStateRepresentation
3329
@doc raw"""
@@ -52,23 +48,23 @@ Parameter relations for the Shinnar-Le Roux selective excitation pulse design al
5248
(NMR imaging).
5349
IEEE Transactions on Medical Imaging, 10(1), 53-65. doi:10.1109/42.75611
5450
"""
55-
mul!(s::Spinor, M::Mag{T}) where {T<:Real} = begin
51+
mul!(s::Spinor, M::Mag{T}) where {T} = begin
5652
M_aux = Mag{T}(
57-
T(2) .*conj.(s.α).*s.β.*M.z.+conj.(s.α).^2 .* M.xy.-s.β.^2 .*conj.(M.xy),
58-
(abs.(s.α).^2 .-abs.(s.β).^2).*M.z.-T(2) .*real.(s.α.*s.β.*conj.(M.xy))
53+
2 .*conj.(s.α).*s.β.*M.z.+conj.(s.α).^2 .* M.xy.-s.β.^2 .*conj.(M.xy),
54+
(abs.(s.α).^2 .-abs.(s.β).^2).*M.z.-2 .*real.(s.α.*s.β.*conj.(M.xy))
5955
)
6056
M.xy .= M_aux.xy
6157
M.z .= M_aux.z
6258
end
63-
mul!(s::Spinor, M::Mag{T}, Maux_xy, Maux_z) where {T<:Real} = begin
64-
@. Maux_xy = T(2)*conj(s.α)*s.β*M.z+conj(s.α)^2*M.xy-s.β^2*conj(M.xy)
65-
@. Maux_z = (abs(s.α)^2 -abs(s.β)^2)*M.z-T(2) *real(s.α*s.β*conj(M.xy))
59+
mul!(s::Spinor, M::Mag, Maux_xy, Maux_z) = begin
60+
@. Maux_xy = 2*conj(s.α)*s.β*M.z+conj(s.α)^2*M.xy-s.β^2*conj(M.xy)
61+
@. Maux_z = (abs(s.α)^2 -abs(s.β)^2)*M.z-2 *real(s.α*s.β*conj(M.xy))
6662
@. M.xy = Maux_xy
6763
@. M.z = Maux_z
6864
end
69-
*(s::Spinor, M::Mag{T}) where {T<:Real} = begin
65+
*(s::Spinor, M::Mag{T}) where {T} = begin
7066
Mag{T}(
71-
T(2) .*conj.(s.α).*s.β.*M.z.+conj.(s.α).^2 .* M.xy.-s.β.^2 .*conj.(M.xy),
72-
(abs.(s.α).^2 .-abs.(s.β).^2).*M.z.-T(2) .*real.(s.α.*s.β.*conj.(M.xy))
67+
2 .*conj.(s.α).*s.β.*M.z.+conj.(s.α).^2 .* M.xy.-s.β.^2 .*conj.(M.xy),
68+
(abs.(s.α).^2 .-abs.(s.β).^2).*M.z.-2 .*real.(s.α.*s.β.*conj.(M.xy))
7369
)
7470
end

KomaMRICore/src/simulation/SimulatorCore.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ end
6262

6363
function run_spin_precession_parallel!(
6464
obj::Phantom{T},
65-
seq::DiscreteSequence{T},
65+
seq::DiscreteSequence,
6666
sig::AbstractArray{Complex{T}},
6767
Xt::SpinStateRepresentation{T},
6868
sim_method::SimulationMethod,
@@ -84,7 +84,7 @@ end
8484

8585
function run_spin_excitation_parallel!(
8686
obj::Phantom{T},
87-
seq::DiscreteSequence{T},
87+
seq::DiscreteSequence,
8888
sig::AbstractArray{Complex{T}},
8989
Xt::SpinStateRepresentation{T},
9090
sim_method::SimulationMethod,

0 commit comments

Comments
 (0)