Skip to content

Commit af3b3c3

Browse files
authored
Merge pull request #936 from jhdark/stepsize_float
provide Stepsize as float or int
2 parents 95cfdd2 + 59c2eb4 commit af3b3c3

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

docs/source/userguide/stepsize.rst

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,15 @@ This will modify the stepsize as needed.
6868
dt_min=1e-6,
6969
max_stepsize=5,
7070
milestones=[1, 5, 6, 10]
71-
)
71+
)
72+
73+
74+
.. note::
75+
76+
If your stepsize is constant, you can define it simply as a ``float`` or ``int``:
77+
78+
.. testcode::
79+
80+
my_model = F.Simulation()
81+
82+
my_model.dt = 2.0

festim/generic_simulation.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,21 @@ def T(self, value):
182182
"accepted types for T attribute are int, float, sympy.Expr or festim.Temperature"
183183
)
184184

185+
@property
186+
def dt(self):
187+
return self._dt
188+
189+
@dt.setter
190+
def dt(self, value):
191+
if value is None:
192+
self._dt = value
193+
elif isinstance(value, (int, float)):
194+
self._dt = festim.Stepsize(value)
195+
elif isinstance(value, festim.Stepsize):
196+
self._dt = value
197+
else:
198+
raise TypeError("dt must be an int, float, or festim.Stepsize")
199+
185200
def attribute_source_terms(self):
186201
"""Assigns the source terms (in self.sources) to the correct field
187202
(self.mobile, self.T, or traps)

test/unit/test_stepsize.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,24 @@ def test_hit_stepsize_max_with_t_stop(time):
130130
)
131131
max_stepsize = lambda t: 1 if t >= 1 else None
132132
assert my_stepsize.adaptive_stepsize["max_stepsize"](time) == max_stepsize(time)
133+
134+
135+
@pytest.mark.parametrize(
136+
"time", [1, 2.5, festim.Stepsize(1.0), "coucou", np.array([1, 2]), [1, 2]]
137+
)
138+
def test_stepsize_as_value(time):
139+
"""
140+
A test to check that users can pass an int or float to the dt attribute when
141+
initialising a F.Simulation class, and an error is raised when passed anything else
142+
"""
143+
144+
my_model = festim.Simulation()
145+
146+
if isinstance(time, (int, float, festim.Stepsize)):
147+
my_model.dt = time
148+
assert isinstance(my_model.dt, festim.Stepsize)
149+
else:
150+
with pytest.raises(
151+
TypeError, match="dt must be an int, float, or festim.Stepsize"
152+
):
153+
my_model.dt = time

0 commit comments

Comments
 (0)