Skip to content

Commit

Permalink
added write for MagneticFieldCoefficients
Browse files Browse the repository at this point in the history
  • Loading branch information
mboberg committed Apr 18, 2023
1 parent 8769db3 commit e388f15
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
26 changes: 26 additions & 0 deletions src/MPISphericalHarmonics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ using MPIMagneticFields
using SphericalHarmonicExpansions
using MPIFiles

import Base.write

export MagneticFieldCoefficients
export selectPatch

Expand Down Expand Up @@ -82,6 +84,26 @@ function MagneticFieldCoefficients(path::String)
return coeffsMF
end

# write coefficients to an HDF5-file
function write(path::String, coeffs::MagneticFieldCoefficients)

# save SphericalHarmonicCoefficients
write(path,coeffs.coeffs)

# add field informations
radius = coeffs.radius
center = coeffs.center
ffp = coeffs.ffp

h5open(path,"cw") do file
write(file, "/radius", radius)
write(file, "/center", center)
if ffp !== nothing
write(file, "/ffp", ffp)
end
end
end

"""
magneticField(tDesign::SphericalTDesign, field::Union{AbstractArray{T,2},AbstractArray{T,3}},
x::Variable, y::Variable, z::Variable;
Expand Down Expand Up @@ -195,6 +217,7 @@ function loadTDesignCoefficients(filename::String)
return coeffs_MF, expansion, func
end

## SphericalHarmonicsDefinedField ##
export SphericalHarmonicsDefinedField
Base.@kwdef mutable struct SphericalHarmonicsDefinedField <: AbstractMagneticField
func::Array{Function, 2}
Expand All @@ -219,6 +242,9 @@ function SphericalHarmonicsDefinedField(filename::String)
return SphericalHarmonicsDefinedField(func=func)
end

# constructors for coefficients
SphericalHarmonicsDefinedField(coeffs::Array{SphericalHarmonicCoefficients}) = SphericalHarmonicsDefinedField(func = fastfunc.(coeffs))
SphericalHarmonicsDefinedField(coeffs_MF::MagneticFieldCoefficients) = SphericalHarmonicsDefinedField(coeffs_MF.coeffs)

MPIMagneticFields.fieldType(::SphericalHarmonicsDefinedField) = OtherField()
MPIMagneticFields.definitionType(::SphericalHarmonicsDefinedField) = SphericalHarmonicsDataBasedFieldDefinition()
Expand Down
25 changes: 17 additions & 8 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,14 @@ using Aqua
@test_throws DimensionMismatch MagneticFieldCoefficients(coeffs, tDes, zeros(3,2))
end

@testset "Load data from file" begin
@testset "Load/write data from/to file" begin
ɛ = eps(Float64)

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

# Test field types
@test fieldType(field) isa OtherField
@test definitionType(field) isa SphericalHarmonicsDataBasedFieldDefinition
@test timeDependencyType(field) isa TimeConstant

# get coefficients
coeffsMF, = MPISphericalHarmonics.loadTDesignCoefficients(filename)
@test isapprox(coeffsMF.radius, 0.042, atol=ε) # radius
Expand All @@ -115,6 +110,7 @@ using Aqua
filename2 = "Coeffs.h5"
filename3 = "Coeffs2.h5"
filename4 = "Coeffs3.h5"
filenameW = "CoeffsW.h5"
write(filename2, coeffsMF.coeffs)
# add radius and center
cp(filename2, filename3)
Expand Down Expand Up @@ -144,17 +140,30 @@ using Aqua
field = SphericalHarmonicsDefinedField(filename4)
@test isapprox(field[-0.02,0.02,-0.03], [0.02,-0.02,-0.06], atol=ε)

# test write
MPISphericalHarmonics.write(filenameW, coeffsTest)
coeffsW = MagneticFieldCoefficients(filenameW)
@test isapprox(coeffsW.radius, 0.042, atol=ε) # radius
@test isapprox(coeffsW.center, zeros(3), atol=ε) # center
@test coeffsW.ffp == zeros(3,1) # FFP

# remove test files
rm(filename2)
rm(filename3)
rm(filename4)
rm(filenameW)
end

@testset "Multiple patches" begin
@testset "SphericalHarmonicsDefinedField (multiple patches)" begin
## Multi-patch setting: Second field with offset
coeffsPatch = hcat(deepcopy(coeffs),deepcopy(coeffs)) # two patches
for j=1:3 coeffsPatch[j,2][0,0] = 0.01 end # set offset
field = SphericalHarmonicsDefinedField(func = fastfunc.(coeffsPatch))
field = SphericalHarmonicsDefinedField(coeffsPatch) # test constructor on coefficients

# Test field types
@test fieldType(field) isa OtherField
@test definitionType(field) isa SphericalHarmonicsDataBasedFieldDefinition
@test timeDependencyType(field) isa TimeConstant

## Test FFPs (for both patches)
# First patch
Expand Down

0 comments on commit e388f15

Please sign in to comment.