Skip to content

Commit

Permalink
[1D] Fix using non-default tolerances after automatic grid expansion
Browse files Browse the repository at this point in the history
This is analogous to the issue with multicomponent transport / Soret
diffusion that was reported in #615 and fixed in #631.
  • Loading branch information
speth authored and ischoegl committed Sep 23, 2021
1 parent 2c253df commit ee8c7b0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
19 changes: 14 additions & 5 deletions interfaces/cython/cantera/onedim.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1137,6 +1137,7 @@ cdef class Sim1D:
if isinstance(dom, _FlowBase):
dom.set_transport(self.gas)

# Do initial solution steps with default tolerances
have_user_tolerances = any(dom.have_user_tolerances for dom in self.domains)
if have_user_tolerances:
# Save the user-specified tolerances
Expand All @@ -1145,6 +1146,14 @@ cdef class Sim1D:
atol_ts_final = [dom.transient_abstol() for dom in self.domains]
rtol_ts_final = [dom.transient_reltol() for dom in self.domains]

def restore_tolerances():
if have_user_tolerances:
for i in range(len(self.domains)):
self.domains[i].set_steady_tolerances(abs=atol_ss_final[i],
rel=rtol_ss_final[i])
self.domains[i].set_transient_tolerances(abs=atol_ts_final[i],
rel=rtol_ts_final[i])

for dom in self.domains:
dom.set_default_tolerances()

Expand Down Expand Up @@ -1203,6 +1212,7 @@ cdef class Sim1D:
# restore settings before re-raising exception
set_transport(transport)
set_soret(True)
restore_tolerances()
raise e

# If initial solve using energy equation fails, fall back on the
Expand All @@ -1221,6 +1231,7 @@ cdef class Sim1D:
# restore settings before re-raising exception
set_transport(transport)
set_soret(True)
restore_tolerances()
raise e

if solved:
Expand All @@ -1236,6 +1247,7 @@ cdef class Sim1D:
# restore settings before re-raising exception
set_transport(transport)
set_soret(True)
restore_tolerances()
raise e

if solved and not self.extinct() and refine_grid:
Expand All @@ -1251,6 +1263,7 @@ cdef class Sim1D:
# restore settings before re-raising exception
set_transport(transport)
set_soret(True)
restore_tolerances()
raise e

if solved and not self.extinct():
Expand All @@ -1276,11 +1289,7 @@ cdef class Sim1D:

if have_user_tolerances:
log('Solving with user-specified tolerances')
for i in range(len(self.domains)):
self.domains[i].set_steady_tolerances(abs=atol_ss_final[i],
rel=rtol_ss_final[i])
self.domains[i].set_transient_tolerances(abs=atol_ts_final[i],
rel=rtol_ts_final[i])
restore_tolerances()

# Final call with expensive options enabled
if have_user_tolerances or solve_multi or soret_doms:
Expand Down
4 changes: 3 additions & 1 deletion interfaces/cython/cantera/test/test_onedim.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,12 @@ def test_auto_width2(self):
width=0.1)

self.sim.set_refine_criteria(ratio=4, slope=0.8, curve=0.8)
self.sim.flame.set_steady_tolerances(T=(2e-4, 1e-8))
self.sim.solve(refine_grid=True, auto=True, loglevel=0)

self.assertNear(self.sim.velocity[0], 17.02, 1e-1)
self.assertLess(self.sim.grid[-1], 2.0) # grid should not be too wide

self.assertEqual(self.sim.flame.tolerances("T"), (2e-4, 1e-8))

@utilities.slow_test
def test_converge_adiabatic(self):
Expand Down

0 comments on commit ee8c7b0

Please sign in to comment.