Skip to content

Commit f2112b4

Browse files
committed
__add__ function for Borehole class
1 parent 2ef74cb commit f2112b4

File tree

4 files changed

+22
-0
lines changed

4 files changed

+22
-0
lines changed

pygfunction/borefield.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ def __ne__(
110110
def __add__(self,
111111
other_field: Union[Borehole, List[Borehole], Self]) -> Self:
112112
"""Add two borefields together"""
113+
from .boreholes import Borehole
114+
113115
if isinstance(other_field, Borehole):
114116
return Borefield.from_boreholes(self.to_boreholes() + [other_field])
115117
if isinstance(other_field, list):

pygfunction/boreholes.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@
33

44
import numpy as np
55
from scipy.spatial.distance import pdist
6+
from typing import TYPE_CHECKING
67

78
from .utilities import _initialize_figure, _format_axes, _format_axes_3d
89

10+
if TYPE_CHECKING:
11+
from .borefield import Borefield
12+
913

1014
class Borehole(object):
1115
"""
@@ -242,6 +246,15 @@ def _segment_midpoints(self, nSegments, segment_ratios=None):
242246
+ segment_ratios * self.H / 2
243247
return z
244248

249+
def __add__(self, other):
250+
"""
251+
Adds two boreholes together to form a borefield
252+
"""
253+
from .borefield import Borefield
254+
if isinstance(other, self):
255+
return Borefield.from_boreholes([self, other])
256+
return other.__add__(self)
257+
245258

246259
class _EquivalentBorehole(object):
247260
"""

tests/borefield_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ def test_borefield_add():
8080
borefield = gt.borefield.Borefield.rectangle_field(2, 1, 6, 6, 100, 1, 0.075)
8181
borefield_2 = gt.borefield.Borefield.from_boreholes([borehole, gt.boreholes.Borehole(110, 1, 0.075, 20, 15)])
8282
assert borefield + borehole == gt.borefield.Borefield.from_boreholes(borefield.to_boreholes() + [borehole])
83+
assert borehole + borefield == gt.borefield.Borefield.from_boreholes(borefield.to_boreholes() + [borehole])
8384
assert borefield + [borehole] == gt.borefield.Borefield.from_boreholes(borefield.to_boreholes() + [borehole])
8485
assert borefield + borefield_2 == gt.borefield.Borefield.from_boreholes(borefield.to_boreholes()+borefield_2.to_boreholes())
8586

tests/boreholes_test.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,3 +307,9 @@ def test_circle_field(N, R):
307307
len(field) == 1 or np.isclose(np.min(dis), B_min),
308308
len(field) == 1 or np.max(dis) <= (2 + 1e-6) * R,
309309
])
310+
311+
312+
def test_add_boreholes():
313+
borehole1 = gt.boreholes.Borehole(100, 1, 0.075, 0, 0)
314+
borehole2 = gt.boreholes.Borehole(110, 1, 0.075, 0, 0)
315+
assert gt.borefield.Borefield.from_boreholes([borehole1, borehole2]) == borehole1 + borehole2

0 commit comments

Comments
 (0)