Skip to content

Commit c051aba

Browse files
committed
further tests
1 parent 0ce99b4 commit c051aba

File tree

3 files changed

+91
-5
lines changed

3 files changed

+91
-5
lines changed

src/MPISphericalHarmonics.jl

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ function MagneticFieldCoefficients(L::Int)
4747
end
4848
return MagneticFieldCoefficients(reshape([SphericalHarmonicCoefficients(L),SphericalHarmonicCoefficients(L),SphericalHarmonicCoefficients(L)],3,1))
4949
end
50+
51+
# constructor using t-design
52+
MagneticFieldCoefficients(coeffs::Array{SphericalHarmonicCoefficients,2}, tDesign::SphericalTDesign, ffp=nothing) = MagneticFieldCoefficients(coeffs,ustrip(Unitful.m(tDesign.radius)), ustrip.(Unitful.m.(tDesign.center)), ffp)
53+
5054

5155
# read coefficients from an HDF5-file
5256
function MagneticFieldCoefficients(path::String)
@@ -69,8 +73,8 @@ function MagneticFieldCoefficients(path::String)
6973
else
7074
# convert file of SphericalHarmonicCoefficients into MagneticFieldCoefficients
7175
# -> set all additional informations to 0 or nothing
72-
# use radius = 0.042 as default value
73-
return MagneticFieldCoefficients(shcoeffs, 0.042)
76+
# use radius = 0.01 as default value
77+
return MagneticFieldCoefficients(shcoeffs, 0.01)
7478
end
7579
end
7680

@@ -163,7 +167,7 @@ function magneticField(coords::AbstractArray{T, 2}, field::Union{AbstractArray{T
163167
end
164168

165169
#TODO: This should be merged with and moved to MPIFiles
166-
function loadTDesign(filename::String)
170+
function loadTDesignCoefficients(filename::String)
167171
field, radius, N, t, center, correction = h5open(filename, "r") do file
168172
field = read(file,"/fields") # measured field (size: 3 x #points x #patches)
169173
radius = read(file,"/positions/tDesign/radius") # radius of the measured ball
@@ -203,7 +207,7 @@ function SphericalHarmonicsDefinedField(filename::String)
203207
func = fastfunc.(coeffs_MF.coeffs)
204208
else
205209
# load measured field
206-
coeffs_MF, expansion, func = loadTDesign(filename)
210+
coeffs_MF, expansion, func = loadTDesignCoefficients(filename)
207211
end
208212

209213
return SphericalHarmonicsDefinedField(func=func)

test/idealGradientField.h5

7.24 KB
Binary file not shown.

test/runtests.jl

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ using Aqua
1414

1515
## Calculate some field values
1616
# load a spherical t-design
17-
tDes = loadTDesign(6,26,42u"mm")
17+
tDes = loadTDesign(6,26,42.0u"mm")
1818
# ideal gradient field with FFP
1919
idealField = IdealFFP([-1,-1,2])
2020
# get field values
@@ -34,6 +34,88 @@ using Aqua
3434
@test isapprox(coeffs[j,1].c, ideal[j], atol = 1e-10)
3535
end
3636

37+
@testset "MagneticFieldCoefficients" begin
38+
39+
## Test Constructor
40+
# Errors
41+
@test_throws DomainError MagneticFieldCoefficients(-2)
42+
@test_throws DimensionMismatch MagneticFieldCoefficients(coeffs[1:2,:])
43+
44+
# standard constructors
45+
coeffsMF = MagneticFieldCoefficients(coeffs)
46+
@test coeffsMF.coeffs == coeffs
47+
@test coeffsMF.radius == 0.0
48+
@test coeffsMF.center == zeros(Float64,3)
49+
@test coeffsMF.ffp === nothing
50+
51+
# constructor with t-design
52+
coeffsMF = MagneticFieldCoefficients(coeffs, tDes, zeros(Float64,3,1))
53+
@test coeffsMF.radius == 0.042
54+
@test coeffsMF.center == zeros(Float64,3)
55+
@test coeffsMF.ffp == zeros(Float64,3,1)
56+
57+
# constructor with wrong FFP sizes
58+
@test_throws DimensionMismatch MagneticFieldCoefficients(coeffs, tDes, zeros(2,1))
59+
@test_throws DimensionMismatch MagneticFieldCoefficients(coeffs, tDes, zeros(3,2))
60+
end
61+
62+
@testset "Load data from file" begin
63+
ɛ = eps(Float64)
64+
65+
## measurement data (without coefficients)
66+
filename = "idealGradientField.h5"
67+
func = SphericalHarmonicsDefinedField(filename)
68+
@test isapprox(func[0.01,0.01,0.01], [-0.01,-0.01,0.02], atol=ε)
69+
70+
# get coefficients
71+
coeffsMF, = MPISphericalHarmonics.loadTDesignCoefficients(filename)
72+
@test isapprox(coeffsMF.radius, 0.042, atol=ε) # radius
73+
@test isapprox(coeffsMF.coeffs[1][1,1], -1.0, atol=1e-10) # gradient (x)
74+
@test isapprox(coeffsMF.coeffs[2][1,-1], -1.0, atol=1e-10) # gradient (y)
75+
@test isapprox(coeffsMF.coeffs[3][1,0], 2.0, atol=1e-10) # gradient (z)
76+
77+
## load coefficients from file
78+
# write coefficients in file
79+
filename2 = "idealGradientFieldwithCoeffs.h5"
80+
filename3 = "idealGradientFieldwithCoeffs2.h5"
81+
filename4 = "idealGradientFieldwithCoeffs3.h5"
82+
cp(filename, filename2)
83+
write(filename2, coeffsMF.coeffs)
84+
# add radius and center
85+
cp(filename2, filename3)
86+
h5open(filename3, "cw") do file
87+
write(file, "/radius", coeffsMF.radius)
88+
write(file, "/center", coeffsMF.center)
89+
end
90+
# add FFP
91+
cp(filename3, filename4)
92+
h5open(filename4, "cw") do file
93+
write(file, "/ffp", zeros(3,1))
94+
end
95+
96+
# only coefficients (no further informations given)
97+
coeffsTest = MagneticFieldCoefficients(filename2)
98+
@test isapprox(coeffsTest.radius, 0.01, atol=ε) # radius
99+
100+
# with given radius & center
101+
coeffsTest = MagneticFieldCoefficients(filename3)
102+
@test isapprox(coeffsTest.radius, 0.042, atol=ε) # radius
103+
@test isapprox(coeffsTest.center, zeros(3), atol=ε) # center
104+
@test coeffsTest.ffp === nothing # FFP
105+
106+
# with given FFP
107+
coeffsTest = MagneticFieldCoefficients(filename4)
108+
@test coeffsTest.ffp == zeros(3,1) # FFP
109+
110+
# remove test files
111+
rm(filename2)
112+
rm(filename3)
113+
rm(filename4)
114+
end
115+
116+
@testset "Multiple patches" begin
117+
#TODO
118+
end
37119
end
38120

39121
end

0 commit comments

Comments
 (0)