Skip to content

Commit

Permalink
fix: first callback execution
Browse files Browse the repository at this point in the history
  • Loading branch information
skim0119 committed Jul 1, 2024
1 parent 3847502 commit 454e9f7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
3 changes: 3 additions & 0 deletions elastica/modules/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ def _finalize_callback(self: SystemCollectionProtocol) -> None:
self._callback_list.clear()
del self._callback_list

# First callback execution
self.apply_callbacks(time=np.float64(0.0), current_step=0)


class _CallBack:
"""
Expand Down
15 changes: 15 additions & 0 deletions tests/test_modules/test_callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,18 @@ def test_callback_finalize_sorted(self, load_rod_with_callbacks):
for x, _ in scwc._callback_list:
assert num < x
num = x

def test_first_call_callback_during_finalize(self, mocker, load_rod_with_callbacks):
"""
This test is to check if the callback is called during the finalize.
If this test fails, check if `apply_callbacks` is called during the finalization step.
"""
scwc, callback_cls = load_rod_with_callbacks
callback_features = [d for d in scwc._callback_list]

spy = mocker.spy(scwc, "apply_callbacks")
scwc._finalize_callback()

assert spy.call_count == 1
assert spy.call_args[1]["time"] == np.float64(0.0)
assert spy.call_args[1]["current_step"] == 0

0 comments on commit 454e9f7

Please sign in to comment.