Skip to content

Commit

Permalink
Merge pull request #164 from j-c-cook/issue_159_FixSegmentRatios
Browse files Browse the repository at this point in the history
Fix segment ratio function
  • Loading branch information
MassimoCimmino authored Nov 9, 2021
2 parents 50b54af + 2eda938 commit 806ade9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
* [Issue 99](https://github.com/MassimoCimmino/pygfunction/issues/99) - Fixed an issue where `MultipleUTube._continuity_condition()` and `MultipleUTube._general_solution()` returned complex valued coefficient matrices.
* [Issue 130](https://github.com/MassimoCimmino/pygfunction/issues/130) - Fix incorrect initialization of variables `_mix_out` and `_mixing_m_flow` in `Network`.
* [Issue 155](https://github.com/MassimoCimmino/pygfunction/issues/155) - Fix incorrect initialization of variables in `Network` and `_BasePipe`. Stored variables are now initialized as `numpy.nan` instead of `numpy.empty`.
* [Issue 159](https://github.com/MassimoCimmino/pygfunction/issues/159) - Fix `segment_ratios` function in the `utilities` module to always expect 0 < `end_length_ratio` < 0.5, and allows for `nSegments=1` or `nSegments=2`. If 1<=`nSegments`<3 then the user is warned that the `end_length_ratio` parameter is being over-ridden.

## Version 2.0.0 (2021-05-22)

Expand Down
30 changes: 20 additions & 10 deletions pygfunction/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,34 @@ def segment_ratios(nSegments, end_length_ratio=0.02):
def is_even(n):
"Returns True if n is even."
return not(n & 0x1)
assert nSegments >= 3 and isinstance(nSegments, int), \
assert nSegments >= 1 and isinstance(nSegments, int), \
"The number of segments `nSegments` should be greater or equal " \
"to 3 and of type int."
assert end_length_ratio > 0. and isinstance(end_length_ratio, (float, np.floating)), \
"to 1 and of type int."
assert nSegments <= 2 or 0. < end_length_ratio < 0.5 and \
isinstance(end_length_ratio, (float, np.floating)), \
"The end-length-ratio `end_length_ratio` should be greater than " \
"0. and of type float."

"0, less than 0.5 (0 < end_length_ratio < 0.5) and of type float."

# If nSegments == 1, the only segment covers the entire length
if nSegments == 1:
return np.array([1.0])
# If nSegments == 2, split the borehole in two even segments
elif nSegments == 2:
if not np.abs(end_length_ratio - 0.5) < 1e-6:
warnings.warn('nSegments = 2 has been provided. The '
'`end_length_ratio` will be over-ridden. Two '
'segment ratios of [0.5, 0.5] will be returned.')
return np.array([0.5, 0.5])
# If nSegments == 3, then the middle segment is simply the remainder of the
# length
if nSegments == 3:
assert end_length_ratio < 0.5, \
"For nSegments == 3, the end-length-ratio `end_length_ratio` " \
"should be less than 0.5."
elif nSegments == 3:
segment_ratios = np.array(
[end_length_ratio,
1 - 2 * end_length_ratio,
end_length_ratio])
return segment_ratios
else:
pass

# If end_length_ratio == 1 / nSegments, then the discretization is
# uniform
Expand Down Expand Up @@ -134,7 +144,7 @@ def is_even(n):
'A decreasing segment ratios discretization was found by '
'utilities.segment_ratios(). Better accuracy is expected for '
'increasing segment ratios. Consider decreasing the '
'end-length-ratio.')
'end-length-ratio such that end_length_ratio < 1 / nSegments.')

return segment_ratios

Expand Down

0 comments on commit 806ade9

Please sign in to comment.