diff --git a/docs/source/userguide/stepsize.rst b/docs/source/userguide/stepsize.rst index 8cc30b3a3..e6632e781 100644 --- a/docs/source/userguide/stepsize.rst +++ b/docs/source/userguide/stepsize.rst @@ -68,4 +68,15 @@ This will modify the stepsize as needed. dt_min=1e-6, max_stepsize=5, milestones=[1, 5, 6, 10] - ) \ No newline at end of file + ) + + +.. 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 \ No newline at end of file diff --git a/festim/generic_simulation.py b/festim/generic_simulation.py index 9c8bbd826..2bae174f3 100644 --- a/festim/generic_simulation.py +++ b/festim/generic_simulation.py @@ -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) diff --git a/test/unit/test_stepsize.py b/test/unit/test_stepsize.py index 514dbd7ea..83d08d041 100644 --- a/test/unit/test_stepsize.py +++ b/test/unit/test_stepsize.py @@ -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