Skip to content

Commit

Permalink
Tweaks to make test_traingle.py faster
Browse files Browse the repository at this point in the history
  • Loading branch information
leon-vv committed Nov 10, 2024
1 parent 569028a commit 06749c9
Showing 1 changed file with 28 additions and 40 deletions.
68 changes: 28 additions & 40 deletions tests/test_triangle.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ def flux_exact(a, b, target, points, normal):

def potential_exact_integrated(v0, v1, v2, target):
area = np.linalg.norm(np.cross(v1-v0, v2-v0))/2
return dblquad(potential_exact, 0, 1, 0, lambda x: 1-x, epsabs=1e-12, epsrel=1e-12, args=(target, (v0, v1, v2)))[0] * (2*area)
return dblquad(potential_exact, 0, 1, 0, lambda x: 1-x, epsabs=1e-10, epsrel=1e-10, args=(target, (v0, v1, v2)))[0] * (2*area)

def flux_exact_integrated(v0, v1, v2, target, normal):
area = np.linalg.norm(np.cross(v1-v0, v2-v0))/2
return dblquad(flux_exact, 0, 1, 0, lambda x: 1-x, epsabs=5e-14, epsrel=5e-14, args=(target, (v0, v1, v2), normal))[0] * (2*area)
return dblquad(flux_exact, 0, 1, 0, lambda x: 1-x, epsabs=1e-10, epsrel=1e-10, args=(target, (v0, v1, v2), normal))[0] * (2*area)


class TestTriangle(unittest.TestCase):
Expand Down Expand Up @@ -77,39 +77,41 @@ def test_potential_triangle_close(self):
v1 = np.array([1.0, 0.0, 0.0])
v2 = np.array([0.0, 1.0, 0.0])

for x in range(-20, 21):
range_ = [-20, -16, -4, -3, -2, -1, 0, 1, 2, 3, 4, 8, 20]

for x in range_:
target = np.array([x, 0.0, 0.5])
assert np.isclose(B.potential_triangle(v0, v1, v2, target), potential_exact_integrated(v0, v1, v2, target), atol=0.0, rtol=1e-9)

for x in range(-20, 21):
for x in range_:
target = np.array([x, 0.0, -0.5])
assert np.isclose(B.potential_triangle(v0, v1, v2, target), potential_exact_integrated(v0, v1, v2, target), atol=0.0, rtol=1e-9)

for y in range(-20, 21):
for y in range_:
target = np.array([0.0, y, 0.5])
assert np.isclose(B.potential_triangle(v0, v1, v2, target), potential_exact_integrated(v0, v1, v2, target), atol=0.0, rtol=1e-9)

for y in range(-20, 21):
for y in range_:
target = np.array([0.0, y, -0.5])
assert np.isclose(B.potential_triangle(v0, v1, v2, target), potential_exact_integrated(v0, v1, v2, target), atol=0.0, rtol=1e-9)

for z in range(-20, 21):
for z in range_:
target = np.array([1.0, 1.0, z])
assert np.isclose(B.potential_triangle(v0, v1, v2, target), potential_exact_integrated(v0, v1, v2, target), atol=0.0, rtol=1e-9)

for z in range(-20, 21):
for z in range_:
target = np.array([-1.0, -1.0, z])
assert np.isclose(B.potential_triangle(v0, v1, v2, target), potential_exact_integrated(v0, v1, v2, target), atol=0.0, rtol=1e-9)

for z in range(-20, 21):
for z in range_:
target = np.array([-0.5, 0.5, z])
assert np.isclose(B.potential_triangle(v0, v1, v2, target), potential_exact_integrated(v0, v1, v2, target), atol=0.0, rtol=1e-9)

for z in range(-20, 21):
for z in range_:
target = np.array([0.5, -0.5, z])
assert np.isclose(B.potential_triangle(v0, v1, v2, target), potential_exact_integrated(v0, v1, v2, target), atol=0.0, rtol=1e-9)

for k in range(-20, 21):
for k in range_:
target = np.array([k, k, 0.5])
assert np.isclose(B.potential_triangle(v0, v1, v2, target), potential_exact_integrated(v0, v1, v2, target), atol=0.0, rtol=1e-9)

Expand Down Expand Up @@ -145,7 +147,7 @@ def test(x0, a, b, c, z0):

correct = flux_exact_integrated(v0, v1, v2, target, [1., 0, 0])
approx = B.flux_triangle(v0, v1, v2, target, normal)
assert np.isclose(correct, approx, atol=0., rtol=1e-10), (x0, a,b,c, z0)
assert np.isclose(correct, approx, atol=0., rtol=1e-9), (x0, a,b,c, z0)

N = 10
for x0, a, b, c, z0 in zip(
Expand All @@ -156,23 +158,7 @@ def test(x0, a, b, c, z0):
rand(N, min=-1, max=1)):

test(x0, a, b, c, z0)

def test_flux_x_special_case(self):
def test(x0, a, b, c, z0):
v0, v1, v2 = np.array([
[x0, 0., 0.],
[x0 + a, 0., 0.],
[x0 + b, c, 0.]])

target = np.array([0., 0., z0])
normal = np.array([1., 0., 0.])

correct = flux_exact_integrated(v0, v1, v2, target, normal)
approx = B.flux_triangle(v0, v1, v2, target, normal)
assert np.isclose(correct, approx, atol=0., rtol=5e-12), (x0, a,b,c, z0)

test(0.011464125694671257, 39.75569724092025, -752.9302350849487, -16.93841253122889, 0.3783263712792002)


def test_pot_quadrants(self):
z0 = 2

Expand Down Expand Up @@ -245,7 +231,7 @@ def test_potential_singular(self):
assert np.isclose(approx, correct)

def test_self_potential(self):
for _ in range(10):
for _ in range(3):
v0, v1, v2 = rand(3,3)
target = v0

Expand All @@ -272,41 +258,43 @@ def test_flux_triangle_close(self):
np.array([0.0, 1.0, 0.0]),
np.array([0.0, 0.0, 1.0])
]


range_ = [-20, -16, -4, -3, -2, -1, 0, 1, 2, 3, 4, 16, 20]

for normal in normals:
for x in range(-20, 21):
for x in range_:
target = np.array([x, 0.0, 0.5])
assert np.isclose(B.flux_triangle(v0, v1, v2, target, normal), flux_exact_integrated(v0, v1, v2, target, normal), atol=0.0, rtol=1e-8)

for x in range(-20, 21):
for x in range_:
target = np.array([x, 0.0, -0.5])
assert np.isclose(B.flux_triangle(v0, v1, v2, target, normal), flux_exact_integrated(v0, v1, v2, target, normal), atol=0.0, rtol=1e-8)

for y in range(-20, 21):
for y in range_:
target = np.array([0.0, y, 0.5])
assert np.isclose(B.flux_triangle(v0, v1, v2, target, normal), flux_exact_integrated(v0, v1, v2, target, normal), atol=0.0, rtol=1e-8)

for y in range(-20, 21):
for y in range_:
target = np.array([0.0, y, -0.5])
assert np.isclose(B.flux_triangle(v0, v1, v2, target, normal), flux_exact_integrated(v0, v1, v2, target, normal), atol=0.0, rtol=1e-8)

for z in range(-20, 21):
for z in range_:
target = np.array([1.0, 1.0, z])
assert np.isclose(B.flux_triangle(v0, v1, v2, target, normal), flux_exact_integrated(v0, v1, v2, target, normal), atol=0.0, rtol=1e-8)

for z in range(-20, 21):
for z in range_:
target = np.array([-1.0, -1.0, z])
assert np.isclose(B.flux_triangle(v0, v1, v2, target, normal), flux_exact_integrated(v0, v1, v2, target, normal), atol=0.0, rtol=1e-8)

for z in range(-20, 21):
for z in range_:
target = np.array([-0.5, 0.5, z])
assert np.isclose(B.flux_triangle(v0, v1, v2, target, normal), flux_exact_integrated(v0, v1, v2, target, normal), atol=0.0, rtol=1e-8)

for z in range(-20, 21):
for z in range_:
target = np.array([0.5, -0.5, z])
assert np.isclose(B.flux_triangle(v0, v1, v2, target, normal), flux_exact_integrated(v0, v1, v2, target, normal), atol=0.0, rtol=1e-8)

for k in range(-20, 21):
for k in range_:
target = np.array([k, k, 0.5])
assert np.isclose(B.flux_triangle(v0, v1, v2, target, normal), flux_exact_integrated(v0, v1, v2, target, normal), atol=0.0, rtol=1e-8)

Expand Down

0 comments on commit 06749c9

Please sign in to comment.