Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better error message for divergence with no dt_min #889

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.")
Comment on lines +99 to +100
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix looks ok to me. Maybe, combine if-clauses? dt_min has to exists for the second check.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If dt_min is None, then the second check won't even be run.
If you're talking about the if float(self.value) < dt_min: line

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, I understand.
I meant smth like that so the checks would be placed together:

if dt_min is None:
    ...
elif float(self.value) < dt_min:
    ...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need since the first check will raise an error if True. They can be considered as completely independent


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()
Loading