Skip to content

Commit

Permalink
added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
RemDelaporteMathurin committed Oct 8, 2023
1 parent 57eb930 commit 53a41d0
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions test/unit/test_stepsize.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import festim
import pytest
import numpy as np


class TestAdapt:
Expand Down Expand Up @@ -36,3 +37,47 @@ def test_hit_stepsize_max(self, my_stepsize):
my_stepsize.adapt(t=6, converged=True, nb_it=2)
new_value = float(my_stepsize.value)
assert new_value == my_stepsize.adaptive_stepsize["stepsize_stop_max"]

def test_milestones_are_hit():
"""Test that the milestones are hit at the correct times
"""
# create a StepSize object
step_size = festim.Stepsize(1.0, stepsize_change_ratio=2, milestones=[1.5, 2.0, 10.3])

# set the initial time
t = 0.0

# create a list of times
times = []
final_time = 11.0

# loop over the time until the final time is reached
while t < final_time:
# call the adapt method being tested
step_size.adapt(t, nb_it=2, converged=True)

# update the time and number of iterations
t += float(step_size.value)

# add the current time to the list of times
times.append(t)

# check that all the milestones are in the list of times
for milestone in step_size.milestones:
assert any(np.isclose(milestone, times))


def test_next_milestone():
"""Test that the next milestone is correct for a given t value
"""
# Create a StepSize object
step_size = festim.Stepsize(milestones=[10.0, 20.0, 30.0])

# Set t values
t_values = [5.0, 10.0, 30.0]
expected_milestones = [10.0, 20.0, None]

# Check that the next milestone is correct for each t value
for t, expected_milestone in zip(t_values, expected_milestones):
next_milestone = step_size.next_milestone(t)
assert np.isclose(next_milestone, expected_milestone) if expected_milestone is not None else next_milestone is None

0 comments on commit 53a41d0

Please sign in to comment.