Repository: RemDelaporteMathurin/FESTIM

Issue:
[BUG] DerivedQuantities not exported at final timestep
**Describe the bug**
Derived quantities are not exported at final timestep

**To Reproduce**
Run this with latest FESTIM version:

```python
import numpy as np

import festim as F

my_model = F.Simulation()

my_model.mesh = F.MeshFromVertices(np.linspace(0, 1, num=100))

tungsten = F.Material(id=1, D_0=1, E_D=0)

my_model.materials = tungsten


my_model.T = F.Temperature(value=300)

my_model.dt = F.Stepsize(
    initial_value=99.9999999,
)

my_model.settings = F.Settings(
    absolute_tolerance=1e-10, relative_tolerance=1e-10, final_time=100
)

list_of_derived_quantities = [F.TotalVolume("solute", volume=1)]

derived_quantities = F.DerivedQuantities(
    list_of_derived_quantities,
    filename="mwe/out.csv",
)


my_model.exports = [derived_quantities]


my_model.initialise()
my_model.run()
```
**Expected behavior**
Derived quantities CSV file created


Discussion:

Patch:
diff --git a/festim/exports/derived_quantities/derived_quantities.py b/festim/exports/derived_quantities/derived_quantities.py
index 7b67d606..a7798c6e 100644
--- a/festim/exports/derived_quantities/derived_quantities.py
+++ b/festim/exports/derived_quantities/derived_quantities.py
@@ -123,7 +123,7 @@ class DerivedQuantities:
             nb_its_between_exports = self.nb_iterations_between_exports
             if nb_its_between_exports is None:
                 # export at the end
-                return t >= final_time
+                return np.isclose(t, final_time)
             else:
                 # export every N iterations
                 return nb_iterations % nb_its_between_exports == 0


Test patch:
diff --git a/test/system/test_misc.py b/test/system/test_misc.py
index 85e18cd1..faa4892d 100644
--- a/test/system/test_misc.py
+++ b/test/system/test_misc.py
@@ -178,3 +178,36 @@ def test_finaltime_overshoot():
     my_model.run()
 
     assert np.isclose(my_model.t, my_model.settings.final_time)
+
+
+def test_derived_quantities_exported_last_timestep_with_small_stepsize(tmp_path):
+    """test to catch bug #566"""
+    my_model = F.Simulation()
+
+    my_model.mesh = F.MeshFromVertices(np.linspace(0, 1, num=100))
+
+    my_model.materials = F.Material(id=1, D_0=1, E_D=0)
+
+    my_model.T = F.Temperature(value=300)
+
+    my_model.dt = F.Stepsize(
+        initial_value=99.9999999,
+    )
+
+    my_model.settings = F.Settings(
+        absolute_tolerance=1e-10, relative_tolerance=1e-10, final_time=100
+    )
+
+    list_of_derived_quantities = [F.TotalVolume("solute", volume=1)]
+
+    derived_quantities = F.DerivedQuantities(
+        list_of_derived_quantities,
+        filename=f"{tmp_path}/out.csv",
+    )
+
+    my_model.exports = [derived_quantities]
+
+    my_model.initialise()
+    my_model.run()
+
+    assert os.path.exists(f"{tmp_path}/out.csv")