-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #616 from jhdark/settings_stepsize
Settings and Stepsize classes
- Loading branch information
Showing
11 changed files
with
154 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import festim as F | ||
|
||
|
||
class Settings: | ||
"""Settings for a festim simulation. | ||
Args: | ||
atol (float): Absolute tolerance for the solver. | ||
rtol (float): Relative tolerance for the solver. | ||
max_iterations (int, optional): Maximum number of iterations for the | ||
solver. Defaults to 30. | ||
final_time (float, optional): Final time for a transient simulation. | ||
Defaults to None | ||
stepsize (festim.Stepsize, optional): stepsize for a transient | ||
simulation. Defaults to None | ||
Attributes: | ||
atol (float): Absolute tolerance for the solver. | ||
rtol (float): Relative tolerance for the solver. | ||
max_iterations (int): Maximum number of iterations for the solver. | ||
final_time (float): Final time for a transient simulation. | ||
stepsize (festim.Stepsize): stepsize for a transient | ||
simulation. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
atol, | ||
rtol, | ||
max_iterations=30, | ||
final_time=None, | ||
stepsize=None, | ||
) -> None: | ||
self.atol = atol | ||
self.rtol = rtol | ||
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import festim as F | ||
|
||
|
||
class Stepsize: | ||
""" | ||
A class for evaluating the stepsize of transient simulations. | ||
Args: | ||
initial_value (float, int): initial stepsize. | ||
Attributes: | ||
initial_value (float, int): initial stepsize. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
initial_value, | ||
) -> None: | ||
self.initial_value = initial_value | ||
|
||
def get_dt(self, mesh): | ||
"""Defines the dt value | ||
Args: | ||
mesh (dolfinx.mesh.Mesh): the domain mesh | ||
Returns: | ||
fem.Constant: the dt value | ||
""" | ||
return F.as_fenics_constant(self.initial_value, mesh) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import festim as F | ||
import numpy as np | ||
import pytest | ||
|
||
|
||
@pytest.mark.parametrize("test_type", [int, F.Stepsize, float]) | ||
def test_stepsize_value(test_type): | ||
"""Test that the stepsize is correctly set""" | ||
test_value = 23.0 | ||
my_settings = F.Settings(atol=1, rtol=0.1) | ||
my_settings.stepsize = test_type(test_value) | ||
|
||
assert isinstance(my_settings.stepsize, F.Stepsize) | ||
assert np.isclose(my_settings.stepsize.initial_value, test_value) | ||
|
||
|
||
def test_stepsize_value_wrong_type(): | ||
"""Checks that an error is raised when the wrong type is given""" | ||
my_settings = F.Settings(atol=1, rtol=0.1) | ||
|
||
with pytest.raises(TypeError): | ||
my_settings.stepsize = "coucou" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters