Skip to content

Commit 553fd51

Browse files
Adapt method signature and add example (#5)
1 parent cd118b5 commit 553fd51

File tree

2 files changed

+30
-28
lines changed

2 files changed

+30
-28
lines changed

example/plotField.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
using PyPlot
2+
using LinearAlgebra
3+
4+
using MPISphericalHarmonics
5+
6+
field = SphericalHarmonicsDefinedField(".\\MPISphericalHarmonics.jl\\FFP000COMSOLOptimized_tDesgn_t=12_r=45.h5")
7+
imshow(norm.(field[-0.02:0.001:0.02, -0.02:0.001:0.02, 0]))

src/MPISphericalHarmonics.jl

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,11 @@ function magneticField(tDesign::SphericalTDesign, field::Union{AbstractArray{T,2
115115
x, y, z,
116116
calcSolid)
117117
end
118-
119-
120118

121-
function magneticField(coords::AbstractArray{T,2}, field::Union{AbstractArray{T,2},AbstractArray{T,3}},
122-
R::T, center::Vector{T}, L::Int,
123-
x::Variable, y::Variable, z::Variable,
124-
calcSolid::Bool=true) where T <: Real
119+
function magneticField(coords::AbstractArray{T, 2}, field::Union{AbstractArray{T, 2}, AbstractArray{T, 3}},
120+
R::T, center::Vector{T}, L::Int,
121+
x::Variable, y::Variable, z::Variable,
122+
calcSolid::Bool=true) where T <: Real
125123

126124
# transpose coords if its dimensions do not fit
127125
if size(coords,1) != 3
@@ -145,20 +143,19 @@ function magneticField(coords::AbstractArray{T,2}, field::Union{AbstractArray{T,
145143
for c in axes(field,3)
146144
# calculation of the coefficients
147145
for j in axes(field,1)
146+
coeffs[j,c] = SphericalHarmonicExpansions.sphericalQuadrature(field[j,:,c],coords',L);
147+
coeffs[j,c].R = R
148148

149-
coeffs[j,c] = SphericalHarmonicExpansions.sphericalQuadrature(field[j,:,c],coords',L);
150-
coeffs[j,c].R = R
149+
normalize!(coeffs[j,c],R)
151150

152-
normalize!(coeffs[j,c],R)
153-
154-
# convert spherical into solid coefficients
155-
if calcSolid
156-
solid!(coeffs[j,c])
157-
end
151+
# convert spherical into solid coefficients
152+
if calcSolid
153+
solid!(coeffs[j,c])
154+
end
158155

159-
# calculation of the expansion
160-
expansion[j,c] = sphericalHarmonicsExpansion(coeffs[j,c],x,y,z) + 0*x;
161-
func[j,c] = @fastfunc expansion[j,c]+0*x+0*y+0*z
156+
# calculation of the expansion
157+
expansion[j,c] = sphericalHarmonicsExpansion(coeffs[j,c],x,y,z) + 0*x;
158+
func[j,c] = @fastfunc expansion[j,c]+0*x+0*y+0*z
162159
end
163160
end
164161

@@ -190,25 +187,23 @@ function loadTDesign(filename::String)
190187
return coeffs_MF, expansion, func
191188
end
192189

193-
194-
Base.@kwdef mutable struct SphericalHarmonicDefinedField <: AbstractMagneticField
190+
export SphericalHarmonicsDefinedField
191+
Base.@kwdef mutable struct SphericalHarmonicsDefinedField <: AbstractMagneticField
195192
func::Array{Function, 2}
196193
patch::Integer = 1
197194
end
198195

199-
function SphericalHarmonicDefinedField(filename::String)
196+
function SphericalHarmonicsDefinedField(filename::String)
200197
coeffs_MF, expansion, func = loadTDesign(filename)
201-
return SphericalHarmonicDefinedField(func=func)
198+
return SphericalHarmonicsDefinedField(func=func)
202199
end
203200

204-
MPIMagneticFields.fieldType(::SphericalHarmonicDefinedField) = OtherField()
205-
MPIMagneticFields.definitionType(::SphericalHarmonicDefinedField) = SphericalHarmonicsDataBasedFieldDefinition()
206-
MPIMagneticFields.timeDependencyType(::SphericalHarmonicDefinedField) = TimeConstant()
201+
MPIMagneticFields.fieldType(::SphericalHarmonicsDefinedField) = OtherField()
202+
MPIMagneticFields.definitionType(::SphericalHarmonicsDefinedField) = SphericalHarmonicsDataBasedFieldDefinition()
203+
MPIMagneticFields.timeDependencyType(::SphericalHarmonicsDefinedField) = TimeConstant()
207204

208-
# TODO: Maybe use StaticArrays here
209-
MPIMagneticFields.value(field::SphericalHarmonicDefinedField, r::Vector{T}) where T <: Number = [field.func[i, field.patch].(r...) for i=1:3]
210-
MPIMagneticFields.value(field::SphericalHarmonicDefinedField, r) = [MPIMagneticFields.value(field, [x, y, z]) for x in r[1], y in r[2], z in r[3]]
205+
MPIMagneticFields.value(field::SphericalHarmonicsDefinedField, r::PT) where {T <: Number, PT <: AbstractVector{T}} = [field.func[i, field.patch].(r...) for i=1:3]
211206

212-
selectPatch(field::SphericalHarmonicDefinedField, patchNum) = field.patch = patchNum
207+
selectPatch(field::SphericalHarmonicsDefinedField, patchNum) = field.patch = patchNum
213208

214209
end

0 commit comments

Comments
 (0)