Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MassimoCimmino committed Nov 28, 2024
1 parent 2010311 commit 4364c06
Show file tree
Hide file tree
Showing 5 changed files with 302 additions and 56 deletions.
280 changes: 262 additions & 18 deletions tests/borefield_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
])
def test_borefield_init(field, request):
# Extract the bore field from the fixture
boreholes = request.getfixturevalue(field)
boreholes = request.getfixturevalue(field).to_boreholes()
# Borefield.from_boreholes
borefield_from_boreholes = gt.borefield.Borefield.from_boreholes(boreholes)
# Borefield.__init__
Expand Down Expand Up @@ -49,10 +49,8 @@ def test_borefield_init(field, request):
])
def test_borefield_eq(field, other_field, expected, request):
# Extract the bore field from the fixture
boreholes = request.getfixturevalue(field)
borefield = gt.borefield.Borefield.from_boreholes(boreholes)
other_boreholes = request.getfixturevalue(other_field)
other_field = gt.borefield.Borefield.from_boreholes(other_boreholes)
borefield = request.getfixturevalue(field)
other_field = request.getfixturevalue(other_field)
assert (borefield == other_field) == expected

# Test borefield comparison using __ne__
Expand All @@ -72,10 +70,8 @@ def test_borefield_eq(field, other_field, expected, request):
])
def test_borefield_ne(field, other_field, expected, request):
# Extract the bore field from the fixture
boreholes = request.getfixturevalue(field)
borefield = gt.borefield.Borefield.from_boreholes(boreholes)
other_boreholes = request.getfixturevalue(other_field)
other_field = gt.borefield.Borefield.from_boreholes(other_boreholes)
borefield = request.getfixturevalue(field)
other_field = request.getfixturevalue(other_field)
assert (borefield != other_field) == expected


Expand Down Expand Up @@ -136,12 +132,11 @@ def test_borefield_ne(field, other_field, expected, request):
])
def test_gfunctions_UBWT(field, method, opts, expected, request):
# Extract the bore field from the fixture
boreholes = request.getfixturevalue(field)
borefield = gt.borefield.Borefield.from_boreholes(boreholes)
borefield = request.getfixturevalue(field)
# Extract the g-function options from the fixture
options = request.getfixturevalue(opts)
# Mean borehole length [m]
H_mean = np.mean([b.H for b in boreholes])
H_mean = np.mean(borefield.H)
alpha = 1e-6 # Ground thermal diffusivity [m2/s]
# Bore field characteristic time [s]
ts = H_mean**2 / (9 * alpha)
Expand Down Expand Up @@ -207,12 +202,11 @@ def test_gfunctions_UBWT(field, method, opts, expected, request):
])
def test_gfunctions_UHTR(field, method, opts, expected, request):
# Extract the bore field from the fixture
boreholes = request.getfixturevalue(field)
borefield = gt.borefield.Borefield.from_boreholes(boreholes)
borefield = request.getfixturevalue(field)
# Extract the g-function options from the fixture
options = request.getfixturevalue(opts)
# Mean borehole length [m]
H_mean = np.mean([b.H for b in boreholes])
H_mean = np.mean(borefield.H)
alpha = 1e-6 # Ground thermal diffusivity [m2/s]
# Bore field characteristic time [s]
ts = H_mean**2 / (9 * alpha)
Expand Down Expand Up @@ -249,12 +243,11 @@ def test_gfunctions_UHTR(field, method, opts, expected, request):
])
def test_gfunctions_inclined_UBWT(two_boreholes_inclined, method, opts, expected, request):
# Extract the bore field from the fixture
boreholes = two_boreholes_inclined
borefield = gt.borefield.Borefield.from_boreholes(boreholes)
borefield = two_boreholes_inclined
# Extract the g-function options from the fixture
options = request.getfixturevalue(opts)
# Mean borehole length [m]
H_mean = np.mean([b.H for b in boreholes])
H_mean = np.mean(borefield.H)
alpha = 1e-6 # Ground thermal diffusivity [m2/s]
# Bore field characteristic time [s]
ts = H_mean**2 / (9 * alpha)
Expand All @@ -264,3 +257,254 @@ def test_gfunctions_inclined_UBWT(two_boreholes_inclined, method, opts, expected
gFunc = borefield.evaluate_g_function(
alpha, time, method=method, options=options, boundary_condition='UBWT')
assert np.allclose(gFunc, expected)


# =============================================================================
# Test borefield creation methods
# =============================================================================
# Test rectangle_field
@pytest.mark.parametrize("N_1, N_2, B_1, B_2", [
(1, 1, 5., 5.), # 1 by 1
(2, 1, 5., 5.), # 2 by 1
(1, 2, 5., 5.), # 1 by 2
(2, 2, 5., 7.5), # 2 by 2 (different x/y spacings)
(10, 9, 7.5, 5.), # 10 by 9 (different x/y spacings)
])
def test_rectangle_field(N_1, N_2, B_1, B_2):
H = 150. # Borehole length [m]
D = 4. # Borehole buried depth [m]
r_b = 0.075 # Borehole radius [m]
# Generate the bore field
borefield = gt.borefield.Borefield.rectangle_field(
N_1, N_2, B_1, B_2, H, D, r_b)
# Evaluate the borehole to borehole distances
x = borefield.x
y = borefield.y
dis = np.sqrt(
np.subtract.outer(x, x)**2 + np.subtract.outer(y, y)**2)[
~np.eye(len(borefield), dtype=bool)]
assert np.all(
[len(borefield) == N_1 * N_2,
np.allclose(H, borefield.H),
np.allclose(D, borefield.D),
np.allclose(r_b, borefield.r_b),
len(borefield) == 1 or np.isclose(np.min(dis), min(B_1, B_2)),
])


# Test staggered_rectangle_field
@pytest.mark.parametrize("N_1, N_2, B_1, B_2, include_last_element", [
(1, 1, 5., 5., True), # 1 by 1
(2, 1, 5., 5., True), # 2 by 1
(1, 2, 5., 5., True), # 1 by 2
(2, 2, 5., 7.5, True), # 2 by 2 (different x/y spacings)
(10, 9, 7.5, 5., True), # 10 by 9 (different x/y spacings),
(1, 1, 5., 5., False), # 1 by 1
(2, 1, 5., 5., False), # 2 by 1
(1, 2, 5., 5., False), # 1 by 2
(2, 2, 5., 7.5, False), # 2 by 2 (different x/y spacings)
(10, 9, 7.5, 5., False), # 10 by 9 (different x/y spacings)
])
def test_staggered_rectangular_field(N_1, N_2, B_1, B_2, include_last_element):
H = 150. # Borehole length [m]
D = 4. # Borehole buried depth [m]
r_b = 0.075 # Borehole radius [m]
# Generate the bore field
borefield = gt.borefield.Borefield.staggered_rectangle_field(
N_1, N_2, B_1, B_2, H, D, r_b,
include_last_borehole=include_last_element)
# Evaluate the borehole to borehole distances
x = borefield.x
y = borefield.y
dis = np.sqrt(
np.subtract.outer(x, x)**2 + np.subtract.outer(y, y)**2)[
~np.eye(len(borefield), dtype=bool)]

if include_last_element or N_1 == 1 or N_2 == 1:
expected_nBoreholes = N_1 * N_2
elif N_2 % 2 == 0:
expected_nBoreholes = N_2 * (2 * N_1 - 1) / 2
else:
expected_nBoreholes = (N_2 - 1) * (2 * N_1 - 1) / 2 + N_1

assert np.all(
[len(borefield) == expected_nBoreholes,
np.allclose(H, borefield.H),
np.allclose(D, borefield.D),
np.allclose(r_b, borefield.r_b),
len(borefield) == 1 or np.isclose(
np.min(dis), min(B_1, np.sqrt(B_2**2 + 0.25 * B_1**2))),
])


# Test dense_rectangle_field
@pytest.mark.parametrize("N_1, N_2, B, include_last_element", [
(1, 1, 5., True), # 1 by 1
(2, 1, 5., True), # 2 by 1
(1, 2, 5., True), # 1 by 2
(2, 2, 5., True), # 2 by 2
(10, 9, 7.5, True), # 10 by 9
(10, 10, 7.5, True), # 10 by 10
(1, 1, 5., False), # 1 by 1
(2, 1, 5., False), # 2 by 1
(1, 2, 5., False), # 1 by 2
(2, 2, 5., False), # 2 by 2
(10, 9, 7.5, False), # 10 by 9
(10, 10, 7.5, False), # 10 by 10
])
def test_dense_rectangle_field(N_1, N_2, B, include_last_element):
H = 150. # Borehole length [m]
D = 4. # Borehole buried depth [m]
r_b = 0.075 # Borehole radius [m]
# Generate the bore field
borefield = gt.borefield.Borefield.dense_rectangle_field(
N_1, N_2, B, H, D, r_b, include_last_borehole=include_last_element)
# Evaluate the borehole to borehole distances
x = borefield.x
y = borefield.y
dis = np.sqrt(
np.subtract.outer(x, x)**2 + np.subtract.outer(y, y)**2)[
~np.eye(len(borefield), dtype=bool)]

if include_last_element or N_1 == 1 or N_2 == 1:
expected_nBoreholes = N_1 * N_2
elif N_2 % 2 == 0:
expected_nBoreholes = N_2 * (2 * N_1 - 1) / 2
else:
expected_nBoreholes = (N_2 - 1) * (2 * N_1 - 1) / 2 + N_1

assert np.all(
[len(borefield) == expected_nBoreholes,
np.allclose(H, borefield.H),
np.allclose(D, borefield.D),
np.allclose(r_b, borefield.r_b),
len(borefield) == 1 or np.isclose(np.min(dis), B)
])


# Test L_shaped_field
@pytest.mark.parametrize("N_1, N_2, B_1, B_2", [
(1, 1, 5., 5.), # 1 by 1
(2, 1, 5., 5.), # 2 by 1
(1, 2, 5., 5.), # 1 by 2
(2, 2, 5., 7.5), # 2 by 2 (different x/y spacings)
(10, 9, 7.5, 5.), # 10 by 9 (different x/y spacings)
])
def test_L_shaped_field(N_1, N_2, B_1, B_2):
H = 150. # Borehole length [m]
D = 4. # Borehole buried depth [m]
r_b = 0.075 # Borehole radius [m]
# Generate the bore field
borefield = gt.borefield.Borefield.L_shaped_field(
N_1, N_2, B_1, B_2, H, D, r_b)
# Evaluate the borehole to borehole distances
x = borefield.x
y = borefield.y
dis = np.sqrt(
np.subtract.outer(x, x)**2 + np.subtract.outer(y, y)**2)[
~np.eye(len(borefield), dtype=bool)]
assert np.all(
[len(borefield) == N_1 + N_2 - 1,
np.allclose(H, borefield.H),
np.allclose(D, borefield.D),
np.allclose(r_b, borefield.r_b),
len(borefield) == 1 or np.isclose(np.min(dis), min(B_1, B_2)),
])


# Test U_shaped_field
@pytest.mark.parametrize("N_1, N_2, B_1, B_2", [
(1, 1, 5., 5.), # 1 by 1
(2, 1, 5., 5.), # 2 by 1
(1, 2, 5., 5.), # 1 by 2
(2, 2, 5., 7.5), # 2 by 2 (different x/y spacings)
(10, 9, 7.5, 5.), # 10 by 9 (different x/y spacings)
])
def test_U_shaped_field(N_1, N_2, B_1, B_2):
H = 150. # Borehole length [m]
D = 4. # Borehole buried depth [m]
r_b = 0.075 # Borehole radius [m]
# Generate the bore field
borefield = gt.borefield.Borefield.U_shaped_field(
N_1, N_2, B_1, B_2, H, D, r_b)
# Evaluate the borehole to borehole distances
x = borefield.x
y = borefield.y
dis = np.sqrt(
np.subtract.outer(x, x)**2 + np.subtract.outer(y, y)**2)[
~np.eye(len(borefield), dtype=bool)]
assert np.all(
[len(borefield) == N_1 + 2 * N_2 - 2 if N_1 > 1 else N_2,
np.allclose(H, borefield.H),
np.allclose(D, borefield.D),
np.allclose(r_b, borefield.r_b),
len(borefield) == 1 or np.isclose(np.min(dis), min(B_1, B_2)),
])


# Test box_shaped_field
@pytest.mark.parametrize("N_1, N_2, B_1, B_2", [
(1, 1, 5., 5.), # 1 by 1
(2, 1, 5., 5.), # 2 by 1
(1, 2, 5., 5.), # 1 by 2
(2, 2, 5., 7.5), # 2 by 2 (different x/y spacings)
(10, 9, 7.5, 5.), # 10 by 9 (different x/y spacings)
])
def test_box_shaped_field(N_1, N_2, B_1, B_2):
H = 150. # Borehole length [m]
D = 4. # Borehole buried depth [m]
r_b = 0.075 # Borehole radius [m]
# Generate the bore field
borefield = gt.borefield.Borefield.box_shaped_field(
N_1, N_2, B_1, B_2, H, D, r_b)
# Evaluate the borehole to borehole distances
x = borefield.x
y = borefield.y
dis = np.sqrt(
np.subtract.outer(x, x)**2 + np.subtract.outer(y, y)**2)[
~np.eye(len(borefield), dtype=bool)]
if N_1 == 1 and N_2 == 1:
nBoreholes_expected = 1
elif N_1 == 1:
nBoreholes_expected = N_2
elif N_2 == 1:
nBoreholes_expected = N_1
else:
nBoreholes_expected = 2 * (N_1 - 1) + 2 * (N_2 - 1)
assert np.all(
[len(borefield) == nBoreholes_expected,
np.allclose(H, borefield.H),
np.allclose(D, borefield.D),
np.allclose(r_b, borefield.r_b),
len(borefield) == 1 or np.isclose(np.min(dis), min(B_1, B_2)),
])


# Test circle_field
@pytest.mark.parametrize("N, R", [
(1, 5.), # 1 borehole
(2, 5.), # 2 boreholes
(3, 7.5), # 3 boreholes
(10, 9.), # 10 boreholes
])
def test_circle_field(N, R):
H = 150. # Borehole length [m]
D = 4. # Borehole buried depth [m]
r_b = 0.075 # Borehole radius [m]
# Generate the bore field
borefield = gt.borefield.Borefield.circle_field(N, R, H, D, r_b)
# Evaluate the borehole to borehole distances
x = borefield.x
y = borefield.y
dis = np.sqrt(
np.subtract.outer(x, x)**2 + np.subtract.outer(y, y)**2)[
~np.eye(len(borefield), dtype=bool)]
B_min = 2 * R * np.sin(np.pi / N)
assert np.all(
[len(borefield) == N,
np.allclose(H, borefield.H),
np.allclose(D, borefield.D),
np.allclose(r_b, borefield.r_b),
len(borefield) == 1 or np.isclose(np.min(dis), B_min),
len(borefield) == 1 or np.max(dis) <= (2 + 1e-6) * R,
])
Loading

0 comments on commit 4364c06

Please sign in to comment.