Skip to content

Commit e388f15

Browse files
committed
added write for MagneticFieldCoefficients
1 parent 8769db3 commit e388f15

File tree

2 files changed

+43
-8
lines changed

2 files changed

+43
-8
lines changed

src/MPISphericalHarmonics.jl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ using MPIMagneticFields
88
using SphericalHarmonicExpansions
99
using MPIFiles
1010

11+
import Base.write
12+
1113
export MagneticFieldCoefficients
1214
export selectPatch
1315

@@ -82,6 +84,26 @@ function MagneticFieldCoefficients(path::String)
8284
return coeffsMF
8385
end
8486

87+
# write coefficients to an HDF5-file
88+
function write(path::String, coeffs::MagneticFieldCoefficients)
89+
90+
# save SphericalHarmonicCoefficients
91+
write(path,coeffs.coeffs)
92+
93+
# add field informations
94+
radius = coeffs.radius
95+
center = coeffs.center
96+
ffp = coeffs.ffp
97+
98+
h5open(path,"cw") do file
99+
write(file, "/radius", radius)
100+
write(file, "/center", center)
101+
if ffp !== nothing
102+
write(file, "/ffp", ffp)
103+
end
104+
end
105+
end
106+
85107
"""
86108
magneticField(tDesign::SphericalTDesign, field::Union{AbstractArray{T,2},AbstractArray{T,3}},
87109
x::Variable, y::Variable, z::Variable;
@@ -195,6 +217,7 @@ function loadTDesignCoefficients(filename::String)
195217
return coeffs_MF, expansion, func
196218
end
197219

220+
## SphericalHarmonicsDefinedField ##
198221
export SphericalHarmonicsDefinedField
199222
Base.@kwdef mutable struct SphericalHarmonicsDefinedField <: AbstractMagneticField
200223
func::Array{Function, 2}
@@ -219,6 +242,9 @@ function SphericalHarmonicsDefinedField(filename::String)
219242
return SphericalHarmonicsDefinedField(func=func)
220243
end
221244

245+
# constructors for coefficients
246+
SphericalHarmonicsDefinedField(coeffs::Array{SphericalHarmonicCoefficients}) = SphericalHarmonicsDefinedField(func = fastfunc.(coeffs))
247+
SphericalHarmonicsDefinedField(coeffs_MF::MagneticFieldCoefficients) = SphericalHarmonicsDefinedField(coeffs_MF.coeffs)
222248

223249
MPIMagneticFields.fieldType(::SphericalHarmonicsDefinedField) = OtherField()
224250
MPIMagneticFields.definitionType(::SphericalHarmonicsDefinedField) = SphericalHarmonicsDataBasedFieldDefinition()

test/runtests.jl

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,19 +90,14 @@ using Aqua
9090
@test_throws DimensionMismatch MagneticFieldCoefficients(coeffs, tDes, zeros(3,2))
9191
end
9292

93-
@testset "Load data from file" begin
93+
@testset "Load/write data from/to file" begin
9494
ɛ = eps(Float64)
9595

9696
## measurement data (without coefficients)
9797
filename = "idealGradientField.h5"
9898
field = SphericalHarmonicsDefinedField(filename)
9999
@test isapprox(field[0.01,0.01,0.01], [-0.01,-0.01,0.02], atol=ε)
100100

101-
# Test field types
102-
@test fieldType(field) isa OtherField
103-
@test definitionType(field) isa SphericalHarmonicsDataBasedFieldDefinition
104-
@test timeDependencyType(field) isa TimeConstant
105-
106101
# get coefficients
107102
coeffsMF, = MPISphericalHarmonics.loadTDesignCoefficients(filename)
108103
@test isapprox(coeffsMF.radius, 0.042, atol=ε) # radius
@@ -115,6 +110,7 @@ using Aqua
115110
filename2 = "Coeffs.h5"
116111
filename3 = "Coeffs2.h5"
117112
filename4 = "Coeffs3.h5"
113+
filenameW = "CoeffsW.h5"
118114
write(filename2, coeffsMF.coeffs)
119115
# add radius and center
120116
cp(filename2, filename3)
@@ -144,17 +140,30 @@ using Aqua
144140
field = SphericalHarmonicsDefinedField(filename4)
145141
@test isapprox(field[-0.02,0.02,-0.03], [0.02,-0.02,-0.06], atol=ε)
146142

143+
# test write
144+
MPISphericalHarmonics.write(filenameW, coeffsTest)
145+
coeffsW = MagneticFieldCoefficients(filenameW)
146+
@test isapprox(coeffsW.radius, 0.042, atol=ε) # radius
147+
@test isapprox(coeffsW.center, zeros(3), atol=ε) # center
148+
@test coeffsW.ffp == zeros(3,1) # FFP
149+
147150
# remove test files
148151
rm(filename2)
149152
rm(filename3)
150153
rm(filename4)
154+
rm(filenameW)
151155
end
152156

153-
@testset "Multiple patches" begin
157+
@testset "SphericalHarmonicsDefinedField (multiple patches)" begin
154158
## Multi-patch setting: Second field with offset
155159
coeffsPatch = hcat(deepcopy(coeffs),deepcopy(coeffs)) # two patches
156160
for j=1:3 coeffsPatch[j,2][0,0] = 0.01 end # set offset
157-
field = SphericalHarmonicsDefinedField(func = fastfunc.(coeffsPatch))
161+
field = SphericalHarmonicsDefinedField(coeffsPatch) # test constructor on coefficients
162+
163+
# Test field types
164+
@test fieldType(field) isa OtherField
165+
@test definitionType(field) isa SphericalHarmonicsDataBasedFieldDefinition
166+
@test timeDependencyType(field) isa TimeConstant
158167

159168
## Test FFPs (for both patches)
160169
# First patch

0 commit comments

Comments
 (0)