Skip to content

Commit ab33dd1

Browse files
committed
further tests
1 parent c051aba commit ab33dd1

File tree

2 files changed

+58
-7
lines changed

2 files changed

+58
-7
lines changed

src/MPISphericalHarmonics.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ using SphericalHarmonicExpansions
99
using MPIFiles
1010

1111
export MagneticFieldCoefficients
12+
export selectPatch
1213

1314
# Spherical harmonic coefficients describing a magnetic field
1415
mutable struct MagneticFieldCoefficients

test/runtests.jl

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using MPISphericalHarmonics
22
using MPIMagneticFields
33
using MPIFiles
4+
using SphericalHarmonicExpansions
45
using Test
56
using Aqua
67

@@ -34,6 +35,25 @@ using Aqua
3435
@test isapprox(coeffs[j,1].c, ideal[j], atol = 1e-10)
3536
end
3637

38+
@testset "magneticField" begin
39+
# further tests for function magneticField()
40+
coords = Float64.(ustrip.(Unitful.m.(hcat([p for p in tDes]...))))
41+
R = Float64(ustrip(Unitful.m(tDes.radius)))
42+
center = Float64.(ustrip.(Unitful.m.(tDes.center)))
43+
L = floor(Int,tDes.T/2)
44+
45+
# transposed positions
46+
coeffsTest, = MPISphericalHarmonics.magneticField(coords', fieldValues, R, center, L, x, y, z)
47+
for j=1:3
48+
@test isapprox(coeffs[j,1].c, coeffsTest[j,1].c, atol = 1e-10)
49+
end
50+
51+
# Errors
52+
@test_throws DimensionMismatch MPISphericalHarmonics.magneticField(coords, zeros(4,length(tDes)), R, center, L, x, y, z) # >3 field values in the first dimension
53+
@test_throws DimensionMismatch MPISphericalHarmonics.magneticField(coords, fieldValues[:,1:end-1], R, center, L, x, y, z) # number of field values != number of measured positions
54+
end
55+
56+
3757
@testset "MagneticFieldCoefficients" begin
3858

3959
## Test Constructor
@@ -48,6 +68,11 @@ using Aqua
4868
@test coeffsMF.center == zeros(Float64,3)
4969
@test coeffsMF.ffp === nothing
5070

71+
L = 1
72+
coeffsMF = MagneticFieldCoefficients(L)
73+
@test size(coeffsMF.coeffs) == (3,1)
74+
[@test coeffsMF.coeffs[j,1].L == L for j=1:3]
75+
5176
# constructor with t-design
5277
coeffsMF = MagneticFieldCoefficients(coeffs, tDes, zeros(Float64,3,1))
5378
@test coeffsMF.radius == 0.042
@@ -64,8 +89,13 @@ using Aqua
6489

6590
## measurement data (without coefficients)
6691
filename = "idealGradientField.h5"
67-
func = SphericalHarmonicsDefinedField(filename)
68-
@test isapprox(func[0.01,0.01,0.01], [-0.01,-0.01,0.02], atol=ε)
92+
field = SphericalHarmonicsDefinedField(filename)
93+
@test isapprox(field[0.01,0.01,0.01], [-0.01,-0.01,0.02], atol=ε)
94+
95+
# Test field types
96+
@test fieldType(field) isa OtherField
97+
@test definitionType(field) isa SphericalHarmonicsDataBasedFieldDefinition
98+
@test timeDependencyType(field) isa TimeConstant
6999

70100
# get coefficients
71101
coeffsMF, = MPISphericalHarmonics.loadTDesignCoefficients(filename)
@@ -76,10 +106,9 @@ using Aqua
76106

77107
## load coefficients from file
78108
# write coefficients in file
79-
filename2 = "idealGradientFieldwithCoeffs.h5"
80-
filename3 = "idealGradientFieldwithCoeffs2.h5"
81-
filename4 = "idealGradientFieldwithCoeffs3.h5"
82-
cp(filename, filename2)
109+
filename2 = "Coeffs.h5"
110+
filename3 = "Coeffs2.h5"
111+
filename4 = "Coeffs3.h5"
83112
write(filename2, coeffsMF.coeffs)
84113
# add radius and center
85114
cp(filename2, filename3)
@@ -106,6 +135,8 @@ using Aqua
106135
# with given FFP
107136
coeffsTest = MagneticFieldCoefficients(filename4)
108137
@test coeffsTest.ffp == zeros(3,1) # FFP
138+
field = SphericalHarmonicsDefinedField(filename4)
139+
@test isapprox(field[-0.02,0.02,-0.03], [0.02,-0.02,-0.06], atol=ε)
109140

110141
# remove test files
111142
rm(filename2)
@@ -114,7 +145,26 @@ using Aqua
114145
end
115146

116147
@testset "Multiple patches" begin
117-
#TODO
148+
## Multi-patch setting: Second field with offset
149+
coeffsPatch = hcat(deepcopy(coeffs),deepcopy(coeffs)) # two patches
150+
for j=1:3 coeffsPatch[j,2][0,0] = 0.01 end # set offset
151+
field = SphericalHarmonicsDefinedField(func = fastfunc.(coeffsPatch))
152+
153+
## Test FFPs (for both patches)
154+
# First patch
155+
@test field.patch == 1
156+
ffp = zeros(3)
157+
@test isapprox(field[ffp...], zeros(3), atol=1e-10)
158+
159+
# Second patch
160+
selectPatch(field,2)
161+
@test field.patch == 2
162+
# test offset in (0,0,0)
163+
offset = ones(3) .* 0.01
164+
@test isapprox(field[0,0,0], offset, atol=1e-10)
165+
# test FFP
166+
ffp = [0.01, 0.01, -0.005]
167+
@test isapprox(field[ffp...], zeros(3), atol=1e-10)
118168
end
119169
end
120170

0 commit comments

Comments
 (0)