Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
13 changes: 12 additions & 1 deletion docs/source/userguide/stepsize.rst
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,15 @@ This will modify the stepsize as needed.
dt_min=1e-6,
max_stepsize=5,
milestones=[1, 5, 6, 10]
)
)


.. note::

When defining the simulation, if your stepsize is constant, you can define it simply by providing it as a `float`` or `int`:

.. testcode::

my_model = F.Simulation()

my_model.dt = 2.0
15 changes: 15 additions & 0 deletions festim/generic_simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,21 @@ def T(self, value):
"accepted types for T attribute are int, float, sympy.Expr or festim.Temperature"
)

@property
def dt(self):
return self._dt

@dt.setter
def dt(self, value):
if value is None:
self._dt = value
elif isinstance(value, (int, float)):
self._dt = festim.Stepsize(value)
elif isinstance(value, festim.Stepsize):
self._dt = value
else:
raise TypeError("dt must be an int, float, or festim.Stepsize")

def attribute_source_terms(self):
"""Assigns the source terms (in self.sources) to the correct field
(self.mobile, self.T, or traps)
Expand Down
18 changes: 18 additions & 0 deletions test/unit/test_stepsize.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,21 @@ def test_hit_stepsize_max_with_t_stop(time):
)
max_stepsize = lambda t: 1 if t >= 1 else None
assert my_stepsize.adaptive_stepsize["max_stepsize"](time) == max_stepsize(time)


@pytest.mark.parametrize("time", [1, 2.5, "coucou", np.array([1, 2]), [1, 2]])
def test_stepsize_as_value(time):
"""
A test to check that users can pass an int or float to the dt attribute when
initialising a F.Simulation class, and an error is raised when passed anything else
"""

my_model = festim.Simulation()

if isinstance(time, (int, float, festim.Stepsize)):
my_model.dt = time
else:
with pytest.raises(
TypeError, match="dt must be an int, float, or festim.Stepsize"
):
my_model.dt = time
Loading