Skip to content

Commit aba6149

Browse files
Set precision with local mpmath context instead of global (threadsafe).
1 parent ad0a11b commit aba6149

File tree

1 file changed

+33
-10
lines changed

1 file changed

+33
-10
lines changed

src/geophires_x/MPFReservoir.py

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import sys
22

3-
from mpmath import mp
3+
from mpmath import mp, MPContext, exp, sqrt, tanh
44
import numpy as np
5-
from mpmath import *
5+
66
import geophires_x.Model as Model
77
from .Parameter import intParameter
88
from .Reservoir import Reservoir
@@ -108,27 +108,50 @@ def Calculate(self, model: Model):
108108
# calculate non-dimensional temperature array
109109
Twnd = []
110110
try:
111-
stash_dps = mp.dps
111+
dps = mp.dps
112112
if self.gringarten_stehfest_precision.Provided:
113-
mp.dps = self.gringarten_stehfest_precision.value
113+
dps = self.gringarten_stehfest_precision.value
114114

115115
for t in range(1, len(model.reserv.timevector.value)):
116-
Twnd = Twnd + [float(invertlaplace(fp, td[t], method='stehfest'))]
117-
except Exception as e_:
118-
mp.dps = stash_dps
116+
# Twnd = Twnd + [float(invertlaplace(fp, td[t], method='stehfest'))]
117+
Twnd = Twnd + [float(_thread_safe_invertlaplace_stehfest(fp, td[t], dps))]
119118

119+
except Exception as e_:
120120
msg = (f'Error: GEOPHIRES could not execute numerical inverse laplace calculation for reservoir model 1 '
121-
f'({self.gringarten_stehfest_precision.Name} = {mpmath.dps}). '
121+
f'({self.gringarten_stehfest_precision.Name} = {dps}). '
122122
'Simulation will abort.')
123123
print(msg)
124124
raise RuntimeError(msg) from e_
125125

126-
mp.dps = stash_dps
127-
128126
Twnd = np.asarray(Twnd)
129127

130128
# calculate dimensional temperature, add initial rock temperature to beginning of array
131129
model.reserv.Tresoutput.value = model.reserv.Trock.value - (Twnd * (model.reserv.Trock.value - model.wellbores.Tinj.value))
132130
model.reserv.Tresoutput.value = np.append([model.reserv.Trock.value], model.reserv.Tresoutput.value)
133131

134132
model.logger.info(f'Complete {str(__class__)}: {sys._getframe().f_code.co_name}')
133+
134+
135+
# noinspection SpellCheckingInspection
136+
def _thread_safe_invertlaplace_stehfest(fp, t, dps):
137+
"""
138+
Calculates the inverse Laplace transform at a specific precision
139+
without modifying the global mpmath context.
140+
141+
Args:
142+
fp: The Laplace-space function.
143+
t: The time at which to evaluate the inverse transform.
144+
dps: The desired decimal places of precision for this calculation.
145+
146+
Returns:
147+
The result of the inverse Laplace transform.
148+
"""
149+
# Create a local, temporary context object.
150+
local_ctx = MPContext()
151+
152+
# Set the desired precision *only* on this local context.
153+
local_ctx.dps = dps
154+
155+
# Call invertlaplace from the local_ctx object. It will use its own
156+
# precision when instantiating the stehfest method internally.
157+
return local_ctx.invertlaplace(fp, t, method='stehfest')

0 commit comments

Comments
 (0)