Skip to content

Commit

Permalink
Merge pull request #486 from RemDelaporteMathurin/fix_dt_value_null
Browse files Browse the repository at this point in the history
Initialise `Stepsize.value` in `Simulation.initialise()`
  • Loading branch information
RemDelaporteMathurin authored May 17, 2022
2 parents babb6e3 + dbb3707 commit 67b34e0
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
7 changes: 6 additions & 1 deletion FESTIM/generic_simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down Expand Up @@ -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):
Expand Down
9 changes: 8 additions & 1 deletion FESTIM/stepsize.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,14 @@ 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):
"""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):
"""Changes the stepsize based on convergence.
Expand Down
22 changes: 22 additions & 0 deletions Tests/simulation/test_initialise.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,25 @@ 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
)
my_model.dt.value.assign(26)

# run
my_model.initialise()

# test
assert my_model.dt.value(2) == 3

0 comments on commit 67b34e0

Please sign in to comment.