Skip to content

Commit

Permalink
fix max stepsize check in modify_value & add test (thanks @RemDelapor…
Browse files Browse the repository at this point in the history
  • Loading branch information
kaelyndunnell committed Nov 5, 2024
1 parent dbb7994 commit 1e57e01
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/festim/stepsize.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,18 +124,18 @@ def modify_value(self, value, nb_iterations, t=None):
if not self.is_adapt(t):
return value

max_step = self.get_max_stepsize(t)

if nb_iterations < self.target_nb_iterations:
updated_value = min(
value * self.growth_factor,
max_step if max_step is not None else value * self.growth_factor,
)
updated_value = value * self.growth_factor
elif nb_iterations > self.target_nb_iterations:
updated_value = value * self.cutback_factor
else:
updated_value = value

max_step = self.get_max_stepsize(t)
if max_step:
if updated_value > max_step:
updated_value = max_step

next_milestone = self.next_milestone(t)
if next_milestone is not None:
time_to_milestone = next_milestone - t
Expand Down
19 changes: 19 additions & 0 deletions test/test_stepsize.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,22 @@ def test_no_milestones():
# Test that setting milestones to None works
stepsize.milestones = None
assert stepsize.milestones is None


def test_modify_for_stepsize():
"""Tests that modify_value returns max_stepsize
when max_stepsize is less than next stepsize.
"""
my_stepsize = F.Stepsize(initial_value=1)
my_stepsize.max_stepsize = 1
my_stepsize.growth_factor = 1.1
my_stepsize.target_nb_iterations = 4

current_value = 2
new_value = my_stepsize.modify_value(
value=current_value,
nb_iterations=my_stepsize.target_nb_iterations,
)

expected_value = 1
assert new_value == expected_value

0 comments on commit 1e57e01

Please sign in to comment.