Skip to content

Commit

Permalink
Merge pull request #936 from jhdark/stepsize_float
Browse files Browse the repository at this point in the history
provide Stepsize as float or int
  • Loading branch information
jhdark authored Jan 14, 2025
2 parents 95cfdd2 + 59c2eb4 commit af3b3c3
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
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::

If your stepsize is constant, you can define it simply 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
21 changes: 21 additions & 0 deletions test/unit/test_stepsize.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,24 @@ 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, festim.Stepsize(1.0), "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
assert isinstance(my_model.dt, festim.Stepsize)
else:
with pytest.raises(
TypeError, match="dt must be an int, float, or festim.Stepsize"
):
my_model.dt = time

0 comments on commit af3b3c3

Please sign in to comment.