Skip to content

Commit

Permalink
Merge pull request festim-dev#889 from RemDelaporteMathurin/better-di…
Browse files Browse the repository at this point in the history
…vergence-error

Better error message for divergence with no `dt_min`
  • Loading branch information
RemDelaporteMathurin authored Oct 8, 2024
2 parents a1e3aec + ebb4d70 commit ad56dc5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
3 changes: 3 additions & 0 deletions festim/stepsize.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ def adapt(self, t, nb_it, converged):
max_stepsize = self.adaptive_stepsize["max_stepsize"]

if not converged:
if dt_min is None:
raise ValueError("Solver diverged but dt_min is not set.")

self.value.assign(float(self.value) / change_ratio)
if float(self.value) < dt_min:
raise ValueError("stepsize reached minimal value")
Expand Down
27 changes: 27 additions & 0 deletions test/system/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -850,3 +850,30 @@ def test_soret_surface_flux_mass_balance(coordinates, surface_flux_class):

assert not np.isclose(flux_left.data[0], 0)
assert np.isclose(np.abs(flux_left.data[0]), np.abs(flux_right.data[0]), rtol=1e-2)


def test_error_raised_when_diverge_with_no_dt_min():
my_model = F.Simulation()

tungsten = F.Material(id=1, D_0=4.10e-7, E_D=0.39)
my_model.materials = tungsten

my_model.mesh = F.MeshFromVertices(vertices=np.linspace(0, 1, 10))
my_model.T = 400

my_model.boundary_conditions = [
F.DirichletBC(surfaces=[1, 2], value=1e20, field="solute")
]

my_model.dt = F.Stepsize(0.001, stepsize_change_ratio=1.1)

my_model.settings = F.Settings(
absolute_tolerance=1e-10,
relative_tolerance=1e-10,
final_time=100,
)

my_model.initialise()

with pytest.raises(ValueError, match="Solver diverged but dt_min is not set."):
my_model.run()

0 comments on commit ad56dc5

Please sign in to comment.