Skip to content

Commit

Permalink
new setter and test
Browse files Browse the repository at this point in the history
  • Loading branch information
jhdark committed Oct 20, 2023
1 parent 0d71c93 commit b094ab8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
18 changes: 18 additions & 0 deletions festim/settings.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import festim as F


class Settings:
"""Settings for a festim simulation.
Expand Down Expand Up @@ -33,3 +36,18 @@ def __init__(
self.max_iterations = max_iterations
self.final_time = final_time
self.stepsize = stepsize

@property
def stepsize(self):
return self._stepsize

@stepsize.setter
def stepsize(self, value):
if value is None:
self._stepsize = None
elif isinstance(value, (float, int)):
self._stepsize = F.Stepsize(initial_value=value)
elif isinstance(value, F.Stepsize):
self._stepsize = value
else:
raise TypeError("stepsize must be an of type int, float or festim.Stepsize")
21 changes: 21 additions & 0 deletions test/test_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import festim as F
import numpy as np
import pytest


@pytest.mark.parametrize("value", [1, F.Stepsize(initial_value=23.0), 1.0, "coucou"])
def test_stepsize_value(value):
"""Test that the stepsize is correctly set"""
my_model = F.HydrogenTransportProblem()
my_model.settings = F.Settings(atol=1, rtol=0.1)

if isinstance(value, (int, float)):
my_model.settings.stepsize = value
assert isinstance(my_model.settings.stepsize, F.Stepsize)
assert np.isclose(my_model.settings.stepsize.initial_value, value)
elif isinstance(value, F.Stepsize):
my_model.settings.stepsize = value
assert np.isclose(my_model.settings.stepsize.initial_value, 23)
else:
with pytest.raises(TypeError):
my_model.settings.stepsize = value

0 comments on commit b094ab8

Please sign in to comment.