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

Fix bug with XDMF export in mode last #796

Merged
merged 3 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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: 2 additions & 1 deletion festim/exports/xdmf_export.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import warnings
from festim import Export
import fenics as f
import numpy as np


field_to_label = {
Expand Down Expand Up @@ -151,7 +152,7 @@ def is_export(self, t, final_time, nb_iterations):
Returns:
bool: True if export should be exported, else False
"""
if self.mode == "last" and t >= final_time:
if self.mode == "last" and np.isclose(t, final_time, atol=0):
return True
elif isinstance(self.mode, int):
if nb_iterations % self.mode == 0:
Expand Down
53 changes: 53 additions & 0 deletions test/system/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,3 +546,56 @@ def test_error_raised_when_no_IC_heat_transfer():
match="Initial condition is required for transient heat transfer simulations",
):
my_model.initialise()


def test_catch_bug_738(tmpdir):
"""
Test to catch bug #738
Set up a simple simulation with and XDMFExport in write mode "last"
RemDelaporteMathurin marked this conversation as resolved.
Show resolved Hide resolved
The stepsize is such that the first timestep is almost the final time (ie. dt = t_final - epsilon)
The simulation should detect that this is the last timestep and export the data.

We then check that the files are created
"""
filename = str(tmpdir.join("mobile_re.xdmf"))
my_model = F.Simulation(log_level=40)

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

my_model.materials = F.Material(id=1, D_0=1, E_D=0)

my_model.T = F.Temperature(value=1500)

my_model.dt = F.Stepsize(
initial_value=1e-10 - 1e-15,
)

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

my_model.exports = [
F.XDMFExport(
field="solute",
filename=filename,
checkpoint=False, # needed in 1D
mode="last",
)
]

# remove old xdmf file
if os.path.exists(filename):
os.remove(filename)
os.remove(str(tmpdir.join("mobile_re.h5")))

my_model.initialise()

my_model.run()

# check that xdmf file exists

assert os.path.exists(filename)
assert os.path.exists(str(tmpdir.join("mobile_re.h5")))
Loading