Skip to content

Commit ecfdb71

Browse files
mikeingoldgithub-actions[bot]JoshuaLampert
authored
Add more analytic tests (#110)
* Add analytic solution for Circle * Add analytic solution for Ball 2D * Add analytic tests for Disk * Add semi-analytic solution for Ball 3D * Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * fix Ball 3d test * fix for general center * Update test/combinations.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * make center const * define offset * remove comment * Change usages of hypot to norm * Remove redundant using LinearAlgebra statements * Add analytic solution for Sphere 3D --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Joshua Lampert <[email protected]> Co-authored-by: Joshua Lampert <[email protected]>
1 parent aa0270a commit ecfdb71

File tree

2 files changed

+62
-36
lines changed

2 files changed

+62
-36
lines changed

test/combinations.jl

Lines changed: 61 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@
44

55
@testitem "Meshes.Ball 2D" setup=[Setup] begin
66
origin = Point(0, 0)
7-
ball = Ball(origin, 2.8)
7+
radius = 2.8
8+
ball = Ball(origin, radius)
89

9-
f(p) = 1.0
10+
function f(p::P) where {P <: Meshes.Point}
11+
r = ustrip(u"m", norm(to(p)))
12+
exp(-r^2)
13+
end
1014
fv(p) = fill(f(p), 3)
1115

1216
# Scalar integrand
13-
sol = Meshes.measure(ball)
17+
sol = - π * exp(-radius^2)) * u"m^2"
1418
@test integral(f, ball, GaussLegendre(100)) sol
1519
@test integral(f, ball, GaussKronrod()) sol
1620
@test integral(f, ball, HAdaptiveCubature()) sol
@@ -28,14 +32,23 @@
2832
end
2933

3034
@testitem "Meshes.Ball 3D" setup=[Setup] begin
31-
origin = Point(0, 0, 0)
32-
ball = Ball(origin, 2.8)
35+
using SpecialFunctions: erf
3336

34-
f(p) = 1.0
37+
center = Point(1, 2, 3)
38+
radius = 2.8u"m"
39+
ball = Ball(center, radius)
40+
41+
function f(p::P) where {P <: Meshes.Point}
42+
offset = p - center
43+
ur = norm(offset)
44+
r = ustrip(u"m", ur)
45+
exp(-r^2)
46+
end
3547
fv(p) = fill(f(p), 3)
3648

3749
# Scalar integrand
38-
sol = Meshes.measure(ball)
50+
r = ustrip(u"m", radius)
51+
sol =^(3 / 2) * erf(r) - 2π * exp(-r^2) * r) * u"m^3"
3952
@test integral(f, ball, GaussLegendre(100)) sol
4053
@test_throws "not supported" integral(f, ball, GaussKronrod())sol
4154
@test integral(f, ball, HAdaptiveCubature()) sol
@@ -201,16 +214,21 @@ end
201214
end
202215

203216
@testitem "Meshes.Circle" setup=[Setup] begin
204-
origin = Point(0, 0, 0)
205-
= Vec(0, 0, 1)
206-
xy_plane = Plane(origin, ẑ)
207-
circle = Circle(xy_plane, 2.5)
217+
center = Point(1, 2, 3)
218+
= Vec(1 / 2, 1 / 2, sqrt(2) / 2)
219+
plane = Plane(center, n̂)
220+
radius = 4.4
221+
circle = Circle(plane, radius)
208222

209-
f(p) = 1.0
223+
function f(p::P) where {P <: Meshes.Point}
224+
offset = p - center
225+
r = ustrip(u"m", norm(offset))
226+
exp(-r^2)
227+
end
210228
fv(p) = fill(f(p), 3)
211229

212230
# Scalar integrand
213-
sol = Meshes.measure(circle)
231+
sol = 2π * radius * exp(-radius^2) * u"m"
214232
@test integral(f, circle, GaussLegendre(100)) sol
215233
@test integral(f, circle, GaussKronrod()) sol
216234
@test integral(f, circle, HAdaptiveCubature()) sol
@@ -344,16 +362,21 @@ end
344362
end
345363

346364
@testitem "Meshes.Disk" setup=[Setup] begin
347-
origin = Point(0, 0, 0)
348-
= Vec(0, 0, 1)
349-
xy_plane = Plane(origin, ẑ)
350-
disk = Disk(xy_plane, 2.5)
365+
center = Point(1, 2, 3)
366+
= Vec(1 / 2, 1 / 2, sqrt(2) / 2)
367+
plane = Plane(center, n̂)
368+
radius = 2.5
369+
disk = Disk(plane, radius)
351370

352-
f(p) = 1.0
371+
function f(p::P) where {P <: Meshes.Point}
372+
offset = p - center
373+
r = ustrip(u"m", norm(offset))
374+
exp(-r^2)
375+
end
353376
fv(p) = fill(f(p), 3)
354377

355378
# Scalar integrand
356-
sol = Meshes.measure(disk)
379+
sol = - π * exp(-radius^2)) * u"m^2"
357380
@test integral(f, disk, GaussLegendre(100)) sol
358381
@test integral(f, disk, GaussKronrod()) sol
359382
@test integral(f, disk, HAdaptiveCubature()) sol
@@ -467,8 +490,7 @@ end
467490
line = Line(a, b)
468491

469492
function f(p::P) where {P <: Meshes.Point}
470-
ur = hypot(p.coords.x, p.coords.y, p.coords.z)
471-
r = ustrip(u"m", ur)
493+
r = ustrip(u"m", norm(to(p)))
472494
exp(-r^2)
473495
end
474496
fv(p) = fill(f(p), 3)
@@ -521,7 +543,6 @@ end
521543
# If the version is specified as minimal compat bound in the Project.toml, the downgrade test fails
522544
if pkgversion(Meshes) >= v"0.51.20"
523545
using CoordRefSystems: Polar
524-
using LinearAlgebra: norm
525546

526547
# Parameterize a circle centered on origin with specified radius
527548
radius = 4.4
@@ -570,8 +591,7 @@ end
570591
plane = Plane(p, v)
571592

572593
function f(p::P) where {P <: Meshes.Point}
573-
ur = hypot(p.coords.x, p.coords.y, p.coords.z)
574-
r = ustrip(u"m", ur)
594+
r = ustrip(u"m", norm(to(p)))
575595
exp(-r^2)
576596
end
577597
fv(p) = fill(f(p), 3)
@@ -599,8 +619,7 @@ end
599619
quadrangle = Quadrangle((-1.0, 0.0), (-1.0, 1.0), (1.0, 1.0), (1.0, 0.0))
600620

601621
function f(p::P) where {P <: Meshes.Point}
602-
ur = hypot(p.coords.x, p.coords.y)
603-
r = ustrip(u"m", ur)
622+
r = ustrip(u"m", norm(to(p)))
604623
exp(-r^2)
605624
end
606625
fv(p) = fill(f(p), 3)
@@ -629,8 +648,7 @@ end
629648
ray = Ray(a, v)
630649

631650
function f(p::P) where {P <: Meshes.Point}
632-
ur = hypot(p.coords.x, p.coords.y, p.coords.z)
633-
r = ustrip(u"m", ur)
651+
r = ustrip(u"m", norm(to(p)))
634652
exp(-r^2)
635653
end
636654
fv(p) = fill(f(p), 3)
@@ -725,8 +743,7 @@ end
725743
a, b = (7.1, 4.6) # arbitrary constants > 0
726744

727745
function f(p::P) where {P <: Meshes.Point}
728-
ur = hypot(p.coords.x, p.coords.y, p.coords.z)
729-
r = ustrip(u"m", ur)
746+
r = ustrip(u"m", norm(to(p)))
730747
exp(r * log(a) + (1 - r) * log(b))
731748
end
732749
fv(p) = fill(f(p), 3)
@@ -755,8 +772,7 @@ end
755772
sphere = Sphere(origin, radius)
756773

757774
function f(p::P) where {P <: Meshes.Point}
758-
ur = hypot(p.coords.x, p.coords.y)
759-
r = ustrip(u"m", ur)
775+
r = ustrip(u"m", norm(to(p)))
760776
exp(-r^2)
761777
end
762778
fv(p) = fill(f(p), 3)
@@ -780,14 +796,23 @@ end
780796
end
781797

782798
@testitem "Meshes.Sphere 3D" setup=[Setup] begin
783-
origin = Point(0, 0, 0)
784-
sphere = Sphere(origin, 4.4)
799+
using CoordRefSystems: Cartesian, Spherical
785800

786-
f(p) = 1.0
801+
center = Point(1, 2, 3)
802+
radius = 4.4u"m"
803+
sphere = Sphere(center, radius)
804+
805+
function f(p::P) where {P <: Meshes.Point}
806+
rθφ = convert(Spherical, Cartesian((p - center)...))
807+
r = ustrip(rθφ.r)
808+
θ = ustrip(rθφ.θ)
809+
φ = ustrip(rθφ.ϕ)
810+
sin(φ)^2 + cos(θ)^2
811+
end
787812
fv(p) = fill(f(p), 3)
788813

789814
# Scalar integrand
790-
sol = Meshes.measure(sphere)
815+
sol = (2π * radius^2) + (4π / 3 * radius^2)
791816
@test integral(f, sphere, GaussLegendre(100)) sol
792817
@test integral(f, sphere, GaussKronrod()) sol
793818
@test integral(f, sphere, HAdaptiveCubature()) sol

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ using TestItems
55
@run_package_tests filter=ti -> !(:extended in ti.tags) verbose=true
66

77
@testsnippet Setup begin
8+
using LinearAlgebra: norm
89
using Meshes
910
using Unitful
1011
end

0 commit comments

Comments
 (0)