Skip to content

Feature: Reusable switching functions#29

Merged
Schlevidon merged 76 commits intopublicfrom
reusable-switching-functions
May 13, 2025
Merged

Feature: Reusable switching functions#29
Schlevidon merged 76 commits intopublicfrom
reusable-switching-functions

Conversation

@Schlevidon
Copy link
Copy Markdown
Collaborator

@Schlevidon Schlevidon commented Apr 1, 2025

Motivation

Generating switching functions during integration is relatively expensive (complex mtree manipulation, exporting to file, etc.). In many cases, the same switching function will be generated several times (e.g. when integrating the same RHS multiple times with different parameters for optimization). To improve performance when encountering a switch, we could instead check if the corresponding switching function already exists before creating a new one from scratch.

Changes

  • Fully refactored the process used to generate switching functions.
    • Completely separated the interface from the datahandle to make it significantly easier to use and test.
    • Added ability to correctly handle RHS which contain recursive helper functions.
    • Improved readability, maintainability and code quality in general.
  • Introduced a new signature class, which can be used to uniquely identify a switching function based on the switch_condition, ctrlif_index and function_index.
  • Naming of switching functions is now based on the hash of the signature, which makes it possible to detect if a switching function already exists and can be reused.
    • Although hash collisions are extermely unlikely, they are handled appropriately by adding a collision index to the name if necessary.
  • When encountering a switch, IFDIFF will first compute the signature and hash, search the switching function directory to determine if this function already exists and finally, either reuse an existing function or create a new function from scratch.
    • The same mechanism also applies to jump functions.
    • This can be disabled in the config, e.g. for running tests or debugging.

Also working on implementing switching function generation for helper
functions
For better interface and reusing information from previous switching
function creation.
@Schlevidon Schlevidon marked this pull request as ready for review May 6, 2025 11:46
@Schlevidon
Copy link
Copy Markdown
Collaborator Author

Note for review: Tests for the new functionality are not added yet, but will be before merging this pull requests.
May start the review already, since the missing tests don't inhibit the review process for the rest of the code.

@Schlevidon Schlevidon requested a review from MichaelStrik May 6, 2025 11:46
Comment thread solveODE.m
Comment thread GenerateSwitchingFunctions/FunctionIterator.m
@MichaelStrik
Copy link
Copy Markdown
Collaborator

Great work, the code is much more readable, well structured and I could confirm a significant reduction in runtime.
I suggest minor cosmetic changes that you can merge from pull request #33 if you agree.

@andreassommer
Copy link
Copy Markdown
Owner

andreassommer commented May 13, 2025 via email

@Schlevidon Schlevidon merged commit 86b64df into public May 13, 2025
@Schlevidon Schlevidon deleted the reusable-switching-functions branch June 2, 2025 11:15
@Schlevidon Schlevidon restored the reusable-switching-functions branch June 2, 2025 11:15
@Schlevidon Schlevidon deleted the reusable-switching-functions branch October 6, 2025 12:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants