Skip to content

Commit

Permalink
rename selectPatch() to setPatch!()
Browse files Browse the repository at this point in the history
  • Loading branch information
mboberg committed Jul 9, 2024
1 parent feb5e0c commit 04ae1a1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
2 changes: 1 addition & 1 deletion example/plotField.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
21 changes: 14 additions & 7 deletions src/MPISphericalHarmonics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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
7 changes: 4 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

Expand Down

0 comments on commit 04ae1a1

Please sign in to comment.