Skip to content

Commit 4a12abf

Browse files
committed
Handle gibbs performance issues
1 parent 601a678 commit 4a12abf

1 file changed

Lines changed: 17 additions & 4 deletions

File tree

backend/src/acidwatch_api/models/gibbs_minimization_model.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import asyncio
2+
13
from neqsim import jneqsim
24
from enum import StrEnum
35
from acidwatch_api.models.base import (
@@ -10,10 +12,10 @@
1012
# Model constants
1113
# Damping factor for composition convergence in Gibbs reactor
1214
DAMPING_COMPOSITION = 0.05 # Used for reactor.setDampingComposition()
13-
# Maximum number of iterations for Gibbs reactor convergence
1415
MAX_ITERATIONS = 5000 # Used for reactor.setMaxIterations()
15-
# Convergence tolerance for Gibbs reactor
16-
CONVERGENCE_TOLERANCE = 1e-3 # Used for reactor.setConvergenceTolerance()
16+
CONVERGENCE_TOLERANCE = 1e-2 # Used for reactor.setConvergenceTolerance()
17+
# Timeout for the (blocking) reactor.run() call.
18+
REACTOR_TIMEOUT_SECONDS = 60
1719

1820

1921
NOT_INITIALIZED_BY_DEFAULT = [
@@ -192,7 +194,18 @@ async def run(self) -> RunResult:
192194
reactor.setEnergyMode(
193195
jneqsim.process.equipment.reactor.GibbsReactor.EnergyMode.ISOTHERMAL
194196
)
195-
reactor.run()
197+
198+
try:
199+
await asyncio.wait_for(
200+
asyncio.to_thread(reactor.run),
201+
timeout=REACTOR_TIMEOUT_SECONDS,
202+
)
203+
except asyncio.TimeoutError:
204+
raise RuntimeError(
205+
f"Gibbs reactor did not converge within "
206+
f"{REACTOR_TIMEOUT_SECONDS}s using the '{eos.value}' "
207+
f"equation of state."
208+
)
196209

197210
assert inlet_stream.getFluid().getNumberOfPhases() == 1, (
198211
"Gibbs model cannot work with two phases as of now"

0 commit comments

Comments
 (0)