Skip to content

Commit d40a516

Browse files
fix single-time-step-per-year integration logic, with additional full unit test
1 parent b53d3fc commit d40a516

File tree

4 files changed

+51
-10
lines changed

4 files changed

+51
-10
lines changed

src/geophires_x/SurfacePlant.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,25 @@ def integrate_time_series_slice(
1616
time_steps_per_year: int,
1717
utilization_factor: float
1818
) -> np.float64:
19-
_slice = series[(0 + _i * time_steps_per_year):((_i + 1) * time_steps_per_year) + 1]
19+
slice_start_index = _i * time_steps_per_year
20+
slice_end_index = ((_i + 1) * time_steps_per_year) + 1
21+
_slice = list(series[slice_start_index:slice_end_index])
2022

2123
# Note that len(_slice) - 1 may be less than time_steps_per_year for the last slice.
2224

2325
if len(_slice) == 1:
24-
integral = _slice[0]
25-
else:
26-
dx_steps = len(_slice) - 1
26+
extrapolated_future_datapoint = _slice[0]
27+
if slice_start_index - 1 > 0:
28+
delta = series[slice_start_index] - series[slice_start_index - 1]
29+
extrapolated_future_datapoint = _slice[0] + delta
30+
_slice.append(extrapolated_future_datapoint)
31+
32+
dx_steps = len(_slice) - 1
2733

28-
integral = np.trapz(
29-
_slice,
30-
dx=1. / dx_steps * 365. * 24.
31-
)
34+
integral = np.trapz(
35+
_slice,
36+
dx=1. / dx_steps * 365. * 24.
37+
)
3238

3339
return integral * 1000. * utilization_factor
3440

src/geophires_x/WellBores.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ def RameyCalc(krock: float, rhorock: float, cprock: float, welldiam: float, tv,
196196
framey = np.zeros(alen)
197197
framey[1:] = -np.log(
198198
1.1 * (welldiam / 2.0) / np.sqrt(4. * alpharock * tv[1:] * 365.0 * 24.0 * 3600.0 * utilfactor)) - 0.29
199-
framey[0] = framey[1] # fource the first value to be the same as the second to get away from near surface effects
199+
framey[0] = framey[1] # force the first value to be the same as the second to get away from near surface effects
200200
rameyA = flowrate * cpwater * framey / 2 / math.pi / krock
201201
TempDrop = -((Trock - Tresoutput) - averagegradient * (depth - rameyA) + (
202202
Tresoutput - averagegradient * rameyA - Trock) * np.exp(-depth / rameyA))

tests/geophires_x_tests/test_surface_plant.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ def _integrate_slice(series: np.ndarray, _i: int) -> np.float64:
7373
TotalkWhProduced[i] = _integrate_slice(ElectricityProduced, i)
7474
NetkWhProduced[i] = _integrate_slice(NetElectricityProduced, i)
7575

76-
self.assertEqual(TotalkWhProduced[-1], ElectricityProduced[-1])
76+
self.assertAlmostEqual(3194711457.45603, NetkWhProduced[-1], places=3)
77+
self.assertAlmostEqual(TotalkWhProduced[-2], TotalkWhProduced[-1], delta=308_000)
7778

7879
def _workaround_module_initialization_order_dependency(self) -> Model:
7980
stash_cwd = Path.cwd()

tests/test_geophires_x.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,3 +676,37 @@ def _c_non_vert(r: GeophiresXResult) -> dict:
676676
self.assertIsNotNone(_c_non_vert(r_1)['value'])
677677

678678
self.assertEqual(_c_non_vert(r_1)['value'], _c_non_vert(_get_result(2))['value'])
679+
680+
def test_single_time_step_per_year(self):
681+
result_1 = GeophiresXClient().get_geophires_result(
682+
GeophiresInputParameters(
683+
from_file_path=self._get_test_file_path('geophires_x_tests/generic-egs-case.txt'),
684+
params={'Time steps per year': 1},
685+
)
686+
)
687+
688+
result_1_final_val = result_1.heat_electricity_extraction_generation_profile[-1][1]
689+
self.assertGreater(result_1_final_val, 0)
690+
691+
result_2 = GeophiresXClient().get_geophires_result(
692+
GeophiresInputParameters(
693+
from_file_path=self._get_test_file_path('geophires_x_tests/generic-egs-case.txt'),
694+
params={'Time steps per year': 2},
695+
)
696+
)
697+
698+
result_2_final_val = result_2.heat_electricity_extraction_generation_profile[-1][1]
699+
self.assertAlmostEqual(result_2_final_val, result_1_final_val, delta=1.5)
700+
701+
# TODO enable once https://github.com/NREL/GEOPHIRES-X/issues/352 is resolved
702+
# result_1_1 = GeophiresXClient().get_geophires_result(
703+
# GeophiresInputParameters(
704+
# from_file_path=self._get_test_file_path('geophires_x_tests/generic-egs-case.txt'),
705+
# params={
706+
# 'Time steps per year': 1,
707+
# 'Plant Lifetime': 1
708+
# },
709+
# )
710+
# )
711+
#
712+
# self.assertIsNotNone(result_1_1)

0 commit comments

Comments
 (0)