Skip to content

Commit

Permalink
check_duplicates becomes find_duplicates, remove_duplicates now recie…
Browse files Browse the repository at this point in the history
…ves one arg and calls find_duplicates from within, remove_duplicates doc string updated, optional display parameter added, example_custom_bore_field.rst updates,
  • Loading branch information
j_c_cook committed Apr 7, 2021
1 parent 0ecfafd commit 84131ff
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 19 deletions.
4 changes: 4 additions & 0 deletions doc/source/example_custom_bore_field.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ This example demonstrates the use of the :doc:`borehole <boreholes>` module
to define the positions of the boreholes within a bore field from a list of
borehole positions.

Two borehole positions (1 and 2) are intentionally added as duplicates and
are removed by calling the :func:`pygfunction.boreholes.remove_duplicates`
function.

The following script generates a bore field with 5 boreholes. The field is
then plotted on a figure.

Expand Down
46 changes: 37 additions & 9 deletions pygfunction/boreholes.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,22 @@ def position(self):
return pos


def check_duplicates(boreField):
def find_duplicates(boreField, disp=False):
"""
The distance method :func:`Borehole.distance` is utilized to find all duplicate boreholes in a boreField.
This function considers a duplicate to be any pair of points that fall within each others radius.
The distance method :func:`Borehole.distance` is utilized to find all
duplicate boreholes in a boreField.
This function considers a duplicate to be any pair of points that fall
within each others radius. The lower index (i) is always stored in the
0 position of the tuple, while the higher index (j) is stored in the 1
position.
Parameters
----------
boreField : list
A list of :class:`Borehole` objects
disp : bool, optional
Set to true to print progression messages.
Default is False.
Returns
-------
Expand All @@ -120,33 +127,48 @@ def check_duplicates(boreField):
duplicate_pairs = [] # define an empty list to be appended to
for i in range(len(boreField)):
borehole_1 = boreField[i]
for j in range(i, len(boreField)): # only loop over the unique interactions
for j in range(i, len(boreField)): # only loop unique interactions
borehole_2 = boreField[j]
if i == j: # the distance of borehole itself will return the borehole radius from Borehole.distance()
if i == j: # skip the borehole itself
continue
else:
dist = borehole_1.distance(borehole_2)
if abs(dist - borehole_1.r_b) < borehole_1.r_b:
duplicate_pairs.append((i, j))
if disp:
# pad with '-' align in center
output = f"{ '*gt.boreholes.find_duplicates()*' :-^50}"
# keep a space between the function name
print(output.replace('*', ' '))
print('The duplicate pairs of boreholes found: {}'\
.format(duplicate_pairs))
return duplicate_pairs


def remove_duplicates(boreField, duplicate_pairs):
def remove_duplicates(boreField, disp=False):
"""
Removes all of the duplicates found from the duplicate pairs returned in :func:`check_duplicates`.
Removes all of the duplicates found from the duplicate pairs returned in
:func:`check_duplicates`.
For each pair of duplicates, the first borehole (with the lower index) is
kept and the other (with the higher index) is removed.
Parameters
----------
boreField : list
A list of :class:`Borehole` objects
duplicate_pairs : list
A list of borehole duplicates in tuples from :func:`check_duplicates`
disp : bool, optional
Set to true to print progression messages.
Default is False.
Returns
-------
new_boreField : list
A boreField without duplicates
"""
# get a list of tuple
duplicate_pairs = find_duplicates(boreField, disp=disp)

new_boreField = []

# values not to be included
Expand All @@ -159,6 +181,12 @@ def remove_duplicates(boreField, duplicate_pairs):
continue
else:
new_boreField.append(boreField[i])
if disp:
# pad with '-' align in center
print(f"{'*gt.boreholes.remove_duplicates()*' :-^50}".\
replace('*', ' ')) # keep a space between the function name
n_duplicates = len(boreField) - len(new_boreField)
print('The number of duplicates removed: {}'.format(n_duplicates))

return new_boreField

Expand Down
17 changes: 7 additions & 10 deletions pygfunction/examples/custom_bore_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ def main():
r_b = 0.075 # Borehole radius (m)

# Borehole positions
# Note: Two duplicate boreholes have been added to this list of positions.
# Position 1 has a borehole that is directly on top of another bore
# Position 2 has a borehole with radius inside of another bore
# The duplicates will be removed with the remove_duplicates function
pos = [(0.0, 0.0),
(0.0, 0.0), # Note: a duplicate borehole has been intentionally added for example purposes
(0.03, 0.0), # Note: a borehole within a borehole radius of another borehole has been added for example
(0.0, 0.0), # Duplicate (for example purposes)
(0.03, 0.0), # Duplicate (for example purposes)
(5.0, 0.),
(3.5, 4.0),
(1.0, 7.0),
Expand All @@ -37,14 +41,7 @@ def main():
# Find and remove duplicates from borehole field
# -------------------------------------------------------------------------

print('The number of boreholes defined: {}'.format(len(field)))
duplicate_pairs = gt.boreholes.check_duplicates(field)
print('Duplicate pairs found in the field: {}'.format(len(duplicate_pairs)))
print(duplicate_pairs)
field = gt.boreholes.remove_duplicates(field, duplicate_pairs)
print('Duplicate pairs found in the new field:')
print(gt.boreholes.check_duplicates(field))
print('The number of unique boreholes left: {}'.format(len(field)))
field = gt.boreholes.remove_duplicates(field, disp=True)

# -------------------------------------------------------------------------
# Draw bore field
Expand Down

0 comments on commit 84131ff

Please sign in to comment.