Skip to content

Commit 3c06d76

Browse files
Rename _default_method and absorb _gausslegendre (#147)
* Rename to _default_diff_method * Organize and add docstrings * Absorb _gausslegendre into only remaining GaussLegendre method * Fix typo * Update tests * Simplify to address inaccurate results * Bugfix (wrong N) * Lowercase n, make ts an ntuple * Rename variables for clarity * Reduce line length * Update old names * Try with Iterators * Typo fix * Use stored length * Fix number, wrong N * Pass-through N vs global access * Abandon use of N * Update utils.jl Co-authored-by: Joshua Lampert <[email protected]> * Update utils.jl Co-authored-by: Joshua Lampert <[email protected]> --------- Co-authored-by: Joshua Lampert <[email protected]>
1 parent ce99b3e commit 3c06d76

File tree

5 files changed

+51
-40
lines changed

5 files changed

+51
-40
lines changed

src/differentiation.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ function jacobian(
5252
geometry::G,
5353
ts::Union{AbstractVector{T}, Tuple{T, Vararg{T}}}
5454
) where {G <: Geometry, T <: AbstractFloat}
55-
return jacobian(geometry, ts, _default_method(G))
55+
return jacobian(geometry, ts, _default_diff_method(G))
5656
end
5757

5858
function jacobian(
@@ -107,7 +107,7 @@ possible and finite difference approximations otherwise.
107107
function differential(
108108
geometry::G,
109109
ts::Union{AbstractVector{T}, Tuple{T, Vararg{T}}},
110-
diff_method::DifferentiationMethod = _default_method(G)
110+
diff_method::DifferentiationMethod = _default_diff_method(G)
111111
) where {G <: Geometry, T <: AbstractFloat}
112112
J = Iterators.map(_KVector, jacobian(geometry, ts, diff_method))
113113
return LinearAlgebra.norm(foldl(, J))

src/integral.jl

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function _integral(
4242
geometry,
4343
rule::GaussKronrod;
4444
FP::Type{T} = Float64,
45-
diff_method::DM = _default_method(geometry)
45+
diff_method::DM = _default_diff_method(geometry)
4646
) where {DM <: DifferentiationMethod, T <: AbstractFloat}
4747
# Implementation depends on number of parametric dimensions over which to integrate
4848
N = Meshes.paramdim(geometry)
@@ -70,24 +70,28 @@ function _integral(
7070
geometry,
7171
rule::GaussLegendre;
7272
FP::Type{T} = Float64,
73-
diff_method::DM = _default_method(geometry)
73+
diff_method::DM = _default_diff_method(geometry)
7474
) where {DM <: DifferentiationMethod, T <: AbstractFloat}
7575
N = Meshes.paramdim(geometry)
7676

77-
# Get Gauss-Legendre nodes and weights for a region [-1,1]^N
78-
xs, ws = _gausslegendre(FP, rule.n)
79-
weights = Iterators.product(ntuple(Returns(ws), N)...)
80-
nodes = Iterators.product(ntuple(Returns(xs), N)...)
77+
# Get Gauss-Legendre nodes and weights of type FP for a region [-1,1]ᴺ
78+
xs, ws = FastGaussQuadrature.gausslegendre(rule.n)
79+
xsFP = Iterators.map(FP, xs)
80+
wsFP = Iterators.map(FP, ws)
81+
weight_grid = Iterators.product(ntuple(Returns(wsFP), N)...)
82+
node_grid = Iterators.product(ntuple(Returns(xsFP), N)...)
8183

8284
# Domain transformation: x [-1,1] ↦ t [0,1]
8385
t(x) = (1 // 2) * x + (1 // 2)
8486

8587
function integrand((weights, nodes))
86-
ts = t.(nodes)
88+
# Transforms nodes/xs, store in an NTuple
89+
ts = ntuple(i -> t(nodes[i]), length(nodes))
90+
# Integrand function
8791
prod(weights) * f(geometry(ts...)) * differential(geometry, ts, diff_method)
8892
end
8993

90-
return (1 // (2^N)) .* sum(integrand, zip(weights, nodes))
94+
return (1 // (2^N)) .* sum(integrand, zip(weight_grid, node_grid))
9195
end
9296

9397
# HAdaptiveCubature
@@ -96,7 +100,7 @@ function _integral(
96100
geometry,
97101
rule::HAdaptiveCubature;
98102
FP::Type{T} = Float64,
99-
diff_method::DM = _default_method(geometry)
103+
diff_method::DM = _default_diff_method(geometry)
100104
) where {DM <: DifferentiationMethod, T <: AbstractFloat}
101105
N = Meshes.paramdim(geometry)
102106

src/utils.jl

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,50 +2,62 @@
22
# Misc. Internal Tools
33
################################################################################
44

5-
# Calculate Gauss-Legendre nodes/weights and convert to type T
6-
function _gausslegendre(T, n)
7-
xs, ws = FastGaussQuadrature.gausslegendre(n)
8-
return T.(xs), T.(ws)
9-
end
10-
115
# Common error message structure
126
function _error_unsupported_combination(geometry, rule)
137
msg = "Integrating a $geometry using a $rule rule not supported."
148
throw(ArgumentError(msg))
159
end
1610

17-
# Return an NTuple{N, T} of zeros; same interface as Base.zeros() but faster
18-
_zeros(T::DataType, N::Int64) = ntuple(_ -> zero(T), N)
19-
_zeros(N::Int) = _zeros(Float64, N)
20-
21-
# Return an NTuple{N, T} of ones; same interface as Base.ones() but faster
22-
_ones(T::DataType, N::Int64) = ntuple(_ -> one(T), N)
23-
_ones(N::Int) = _ones(Float64, N)
24-
2511
################################################################################
2612
# DifferentiationMethod
2713
################################################################################
2814

2915
# Return the default DifferentiationMethod instance for a particular geometry type
30-
function _default_method(
16+
function _default_diff_method(
3117
g::Type{G}
3218
) where {G <: Geometry}
3319
return FiniteDifference()
3420
end
3521

3622
# Return the default DifferentiationMethod instance for a particular geometry instance
37-
_default_method(g::G) where {G <: Geometry} = _default_method(G)
23+
_default_diff_method(g::G) where {G <: Geometry} = _default_diff_method(G)
3824

3925
################################################################################
40-
# CliffordNumbers and Units
26+
# Numerical Tools
4127
################################################################################
4228

43-
# Meshes.Vec -> Unitful.Quantity{CliffordNumber.KVector}
29+
"""
30+
_KVector(v::Meshes.Vec) -> Unitful.Quantity{CliffordNumbers.KVector}
31+
32+
Convert a `Vec` to a Unitful KVector.
33+
"""
4434
function _KVector(v::Meshes.Vec{Dim, T}) where {Dim, T}
4535
ucoords = Iterators.map(Unitful.ustrip, v.coords)
4636
return CliffordNumbers.KVector{1, VGA(Dim)}(ucoords...) * _units(v)
4737
end
4838

49-
# Extract the length units used by the CRS of a Geometry
39+
"""
40+
_units(geometry)
41+
42+
Return the Unitful.jl units associated with a particular `geometry`.
43+
"""
5044
_units(::Geometry{M, CRS}) where {M, CRS} = Unitful.unit(CoordRefSystems.lentype(CRS))
5145
_units(::Meshes.Vec{Dim, T}) where {Dim, T} = Unitful.unit(T)
46+
47+
"""
48+
_zeros(T = Float64, N)
49+
50+
Return an `NTuple{N, T}` filled with zeros. This method avoids allocating an array, which
51+
happens when using `Base.zeros`.
52+
"""
53+
_zeros(T::DataType, N::Int64) = ntuple(_ -> zero(T), N)
54+
_zeros(N::Int) = _zeros(Float64, N)
55+
56+
"""
57+
_ones(T = Float64, N)
58+
59+
Return an `NTuple{N, T}` filled with ones. This method avoids allocating an array, which
60+
happens when using `Base.ones`.
61+
"""
62+
_ones(T::DataType, N::Int64) = ntuple(_ -> one(T), N)
63+
_ones(N::Int) = _ones(Float64, N)

test/combinations.jl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,7 @@ end
6666
end
6767

6868
@testitem "Meshes.BezierCurve" setup=[Setup] begin
69-
curve = BezierCurve(
70-
[Point(t * u"m", sin(t) * u"m", 0.0u"m") for t in range(-pi, pi, length = 361)]
71-
)
69+
curve = BezierCurve([Point(t, sin(t), 0) for t in range(-pi, pi, length = 361)])
7270

7371
function f(p::P) where {P <: Meshes.Point}
7472
ux = ustrip(p.coords.x)

test/utils.jl

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,12 @@
2020
end
2121

2222
@testitem "DifferentiationMethod" setup=[Setup] begin
23-
using MeshIntegrals: _default_method
23+
using MeshIntegrals: _default_diff_method
2424

25-
# Test geometries
25+
# _default_diff_method
2626
sphere = Sphere(Point(0, 0, 0), 1.0)
27-
triangle = Triangle(Point(0, 0, 0), Point(0, 1, 0), Point(1, 0, 0))
28-
29-
# _default_method
30-
@test _default_method(Meshes.Sphere) isa FiniteDifference
31-
@test _default_method(sphere) isa FiniteDifference
27+
@test _default_diff_method(Meshes.Sphere) isa FiniteDifference
28+
@test _default_diff_method(sphere) isa FiniteDifference
3229

3330
# FiniteDifference
3431
@test FiniteDifference().ε 1e-6

0 commit comments

Comments
 (0)