From 7695f7ccf8d87be653441ea8df48973a1817032d Mon Sep 17 00:00:00 2001 From: RemDelaporteMathurin Date: Tue, 17 May 2022 11:44:34 +0000 Subject: [PATCH 1/6] added test that catches the bug --- Tests/simulation/test_initialise.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/Tests/simulation/test_initialise.py b/Tests/simulation/test_initialise.py index fa3d492ff..188854bae 100644 --- a/Tests/simulation/test_initialise.py +++ b/Tests/simulation/test_initialise.py @@ -49,3 +49,27 @@ def test_initialise_sets_t_to_zero(): # check that my_model.t is reinitialised to zero assert my_model.t == 0 + + +def test_initialise_initialise_dt(): + """Creates a Simulation object and checks that .initialise() sets + the value attribute of the dt attribute to dt.initial_value + """ + # build + my_model = F.Simulation() + my_model.mesh = F.MeshFromVertices([1, 2, 3]) + my_model.materials = F.Material(id=1, D_0=1, E_D=0, thermal_cond=1) + my_model.T = F.Temperature(100) + my_model.dt = F.Stepsize(initial_value=3) + my_model.settings = F.Settings( + absolute_tolerance=1e-10, relative_tolerance=1e-10, final_time=4 + ) + + # run + my_model.initialise() + my_model.run() + + my_model.initialise() + + # test + assert my_model.dt.value(2) == 3 From 583a2f2f38338108749327faa0990f5889c749d5 Mon Sep 17 00:00:00 2001 From: RemDelaporteMathurin Date: Tue, 17 May 2022 11:45:47 +0000 Subject: [PATCH 2/6] simpler test --- Tests/simulation/test_initialise.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Tests/simulation/test_initialise.py b/Tests/simulation/test_initialise.py index 188854bae..d31f71779 100644 --- a/Tests/simulation/test_initialise.py +++ b/Tests/simulation/test_initialise.py @@ -66,8 +66,7 @@ def test_initialise_initialise_dt(): ) # run - my_model.initialise() - my_model.run() + my_model.dt.value.assign(26) my_model.initialise() From a3a06d865459ec11cb129462bf6ff19cf4a5c318 Mon Sep 17 00:00:00 2001 From: RemDelaporteMathurin Date: Tue, 17 May 2022 11:46:23 +0000 Subject: [PATCH 3/6] rearranged lines --- Tests/simulation/test_initialise.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Tests/simulation/test_initialise.py b/Tests/simulation/test_initialise.py index d31f71779..d72ae2c4b 100644 --- a/Tests/simulation/test_initialise.py +++ b/Tests/simulation/test_initialise.py @@ -64,10 +64,9 @@ def test_initialise_initialise_dt(): my_model.settings = F.Settings( absolute_tolerance=1e-10, relative_tolerance=1e-10, final_time=4 ) - - # run my_model.dt.value.assign(26) + # run my_model.initialise() # test From 27eba55acace5e2c7525c9723b9c4b3f10965043 Mon Sep 17 00:00:00 2001 From: RemDelaporteMathurin Date: Tue, 17 May 2022 11:47:04 +0000 Subject: [PATCH 4/6] added initialise_value method to Stepsize --- FESTIM/stepsize.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/FESTIM/stepsize.py b/FESTIM/stepsize.py index 7e439e7ce..22ab0c038 100644 --- a/FESTIM/stepsize.py +++ b/FESTIM/stepsize.py @@ -40,7 +40,12 @@ def __init__( "stepsize_stop_max": stepsize_stop_max, "dt_min": dt_min, } - self.value = f.Constant(initial_value, name="dt") + self.initial_value = initial_value + self.value = None + self.initialise_value() + + def initialise_value(self): + self.value = f.Constant(self.initial_value, name="dt") def adapt(self, t, nb_it, converged): """Changes the stepsize based on convergence. From b524cdd2f130af49f1f123d28b01e81b7d8b279e Mon Sep 17 00:00:00 2001 From: RemDelaporteMathurin Date: Tue, 17 May 2022 11:48:14 +0000 Subject: [PATCH 5/6] docstrings --- FESTIM/stepsize.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/FESTIM/stepsize.py b/FESTIM/stepsize.py index 22ab0c038..bcbc20a7a 100644 --- a/FESTIM/stepsize.py +++ b/FESTIM/stepsize.py @@ -45,6 +45,8 @@ def __init__( self.initialise_value() def initialise_value(self): + """Creates a fenics.Constant object initialised with self.initial_value + and stores it in self.value""" self.value = f.Constant(self.initial_value, name="dt") def adapt(self, t, nb_it, converged): From dbb37076c7e902e318ab33bf1409ddecb36b5c42 Mon Sep 17 00:00:00 2001 From: RemDelaporteMathurin Date: Tue, 17 May 2022 11:50:05 +0000 Subject: [PATCH 6/6] initialise dt in initialise + only change dt value if not final time --- FESTIM/generic_simulation.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/FESTIM/generic_simulation.py b/FESTIM/generic_simulation.py index afc266fa8..a56188503 100644 --- a/FESTIM/generic_simulation.py +++ b/FESTIM/generic_simulation.py @@ -218,6 +218,11 @@ def initialise(self): raise AttributeError("dt must be None in steady state simulations") if self.settings.transient and self.dt is None: raise AttributeError("dt must be provided in transient simulations") + + # initialise dt + if self.settings.transient: + self.dt.initialise_value() + self.h_transport_problem = HTransportProblem( self.mobile, self.traps, self.T, self.settings, self.initial_conditions ) @@ -328,7 +333,7 @@ def iterate(self): # avoid t > final_time next_time = self.t + float(self.dt.value) - if next_time > self.settings.final_time: + if next_time > self.settings.final_time and self.t != self.settings.final_time: self.dt.value.assign(self.settings.final_time - self.t) def display_time(self):