diff --git a/example/plotField.jl b/example/plotField.jl index c3c2633..fdc853a 100644 --- a/example/plotField.jl +++ b/example/plotField.jl @@ -55,7 +55,7 @@ quiverRange = -coeffs.radius:0.005:coeffs.radius figure(figsize=(12,6)) for p=1:length(coeffs) # plot both patches subplot(1,2,p) - selectPatch(field,p) # switch patches + setPatch!(field,p) # switch patches # plot norm imshow(norm.(field[plotRange, plotRange, 0]), origin="lower", diff --git a/src/MPISphericalHarmonics.jl b/src/MPISphericalHarmonics.jl index cb6bc91..e0ae2ea 100644 --- a/src/MPISphericalHarmonics.jl +++ b/src/MPISphericalHarmonics.jl @@ -21,7 +21,7 @@ export findFFP, findFFP! ## SphericalHarmonicsDefinedField ## export SphericalHarmonicsDefinedField -export selectPatch, length +export setPatch!, getPatch, length Base.@kwdef mutable struct SphericalHarmonicsDefinedField <: AbstractMagneticField func::Array{Union{Function,SphericalHarmonicExpansions.StaticPolynomials.Polynomial},2} @@ -56,13 +56,20 @@ MPIMagneticFields.value_(field::SphericalHarmonicsDefinedField, r) = # patches length(field::SphericalHarmonicsDefinedField) = size(field.func, 2) # get number of patches -function selectPatch(field::SphericalHarmonicsDefinedField, patchNum::Int) - if 1 <= patchNum <= length(field) # test if patch exists - field.patch = patchNum - else - throw(DimensionMismatch("The field contains only $(length(field)) patches.")) - end +function setPatch!(field::SphericalHarmonicsDefinedField, patch::Int) # set patch + checkPatch(field, patch) # test if patch exists + field.patch = patch # set patch + + return patch end +getPatch(field::SphericalHarmonicsDefinedField) = field.patch # get current patch +# test if patch exists +function checkPatch(field::SphericalHarmonicsDefinedField, patch::Int) + if patch > length(field) || patch < 1 + throw(DimensionMismatch("Patch $patch requested, but the field contains only $(length(field)) patches.")) + end + return nothing +end end diff --git a/test/runtests.jl b/test/runtests.jl index 40d3b42..b797844 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -362,8 +362,9 @@ const tmpdir = @get_scratch!("tmp") @test isapprox(field[ffp...], zeros(3), atol = 1e-10) # Second patch - selectPatch(field, 2) - @test field.patch == 2 + setPatch!(field, 2) + @test field.patch == 2 # tests setPatch! + @test getPatch(field) == 2 # tests getPatch # test offset in (0, 0, 0) offset = ones(3) .* 0.01 @test isapprox(field[0, 0, 0], offset, atol = 1e-10) @@ -372,7 +373,7 @@ const tmpdir = @get_scratch!("tmp") @test isapprox(field[ffp...], zeros(3), atol = 1e-10) # Test wrong patch number - @test_throws DimensionMismatch selectPatch(field, 3) + @test_throws DimensionMismatch setPatch!(field, 3) end end