Skip to content

Conversation

@julianlitz
Copy link
Contributor

@julianlitz julianlitz commented Jun 30, 2025

Changes and Information

This PR extends automatic differentiation (AD) support to the Secirvvs model: 1308
It also improves the consistency of floating-point types in the code.
However, achieving full consistency across the entire codebase is beyond the scope of this PR.

Please briefly list the changes (main added features, changed items, or corrected bugs) made:

  • Addition of FP template parameters to support AD datatypes.
  • Added new functions that allows UncertainValue<FP> to be used in math expressions with AD types.
  • Removed FP = double / FP = ScalarType.
  • Switched get_matrix_at(FP)get_matrix_at(SimulationTime<FP>).
  • Replaced critical auto types with explicit FP to prevent segmentation faults with AD types.
  • Moved data/analyze_result.cpp logic into a header to enable templating.
  • The new templates were added to the python bindings.

If need be, add additional information and what the reviewer should look out for in particular:

  • Addition of functions in UncertainValue<FP> to automatically call .value() in math expressions.
    This is needed for the AD datatypes. It has no negative impact on performance in the benchmarks.
    It is fairly unintuitive when to call .value() and would clutter the codebase unnecessary.

New FP templatizations

  • ContactMatrixGroup<FP>, MobilityCoefficients<FP>
  • SimulationTime<FP>, AdoptionRate<FP,…>
  • Simulation<FP>, SimulationNode<FP,…>, IntegratorCore<FP>
  • ParameterStudy<FP,…>, Model<FP,…>, StartDay<FP>
  • DampingMatrixExpressionGroup<FP>, Damping<FP>, Dampings<FP>
  • StateAgeFunctionWrapper<FP>, StateAgeFunction<FP>

Interesting things

  • FP + FP doesn’t yield an FP type; returning auto or passing it without explicit function<FP>() can break AD instantiations.
  • return statement of std::accumulate requires explicit casting to FP.
  • min({a,b,c}) isn’t supported by AD overloads.
  • std::numeric_limits<FP> does not work for AD types. E.g. ::max would return 0.0. Use std::numeric_limits<ScalarType> instead.
  • Casting each conditional operand to FP() is necessary for automatic differentiation (AD) compatibility. Example

Merge Request - Guideline Checklist

Please check our git workflow. Use the draft feature if the Pull Request is not yet ready to review.

Checks by code author

  • Every addressed issue is linked (use the "Closes #ISSUE" keyword below)
  • New code adheres to coding guidelines
  • No large data files have been added (files should in sum not exceed 100 KB, avoid PDFs, Word docs, etc.)
  • Tests are added for new functionality and a local test run was successful (with and without OpenMP)
  • Appropriate documentation for new functionality has been added (Doxygen in the code and explanations in the online documentation)
  • Proper attention to licenses, especially no new third-party software with conflicting license has been added
  • (For ABM development) Checked benchmark results and ran and posted a local test above from before and after development to ensure performance is monitored.

Checks by code reviewer(s)

  • Corresponding issue(s) is/are linked and addressed
  • Code is clean of development artifacts (no deactivated or commented code lines, no debugging printouts, etc.)
  • Appropriate unit tests have been added, CI passes, code coverage and performance is acceptable (did not decrease)
  • No large data files added in the whole history of commits(files should in sum not exceed 100 KB, avoid PDFs, Word docs, etc.)
  • On merge, add 2-5 lines with the changes (main added features, changed items, or corrected bugs) to the merge-commit-message. This can be taken from the briefly-list-the-changes above (best case) or the separate commit messages (worst case).

Closes #1308 (Extending AD to the Secirvvs Model)
Closes #1258 (Bug in Example: ad_odeint_example)
Closes #1260 (Segmentation Fault in Example: ode_seair_optimization)
Closes #594 (comment)

Copy link
Member

@reneSchm reneSchm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, one more detail in the tests, then I think everything looks good.

@jubicker @annawendler @MaxBetzDLR can you make a final pass as well?

Copy link
Member

@annawendler annawendler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

Since you implemented the use of ScalarType within the IDE-SEIR model, you can also close issue #594 with this PR (in agreement with @lenaploetzke) :)

Copy link
Member

@MaxBetzDLR MaxBetzDLR left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adaptation to Python bindings look good.

Copy link
Contributor

@HenrZu HenrZu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just had a look at some models. Only some short comments. Otherwise, it seems to be fine :)

Thank you for the effort Julian! However, its almost impossible to trace all changes (more trust that the tests are good :D )

Co-authored-by: Henrik Zunker <69154294+HenrZu@users.noreply.github.com>
@julianlitz
Copy link
Contributor Author

julianlitz commented Sep 12, 2025

On merge, add 2-5 lines with the changes (main added features, changed items, or corrected bugs) to the merge-commit-message. This can be taken from the briefly-list-the-changes above (best case) or the separate commit messages (worst case).

Extended automatic differentiation (AD) support to the Secirvvs model.
Templatized floating-point types (FP) across key classes and functions for AD compatibility.
Added math operations for UncertainValue to work correctly with AD types.

@reneSchm reneSchm merged commit 75ce311 into main Sep 15, 2025
57 checks passed
@reneSchm reneSchm deleted the 1018-use-floating-point-types-consistently branch September 15, 2025 11:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

class::improvement Cleanup that doesn't affect functionality loc::backend This issue concerns the C++ backend implementation.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Extending AD to the Secirvvs Model Segmentation Fault in Example: ode_seair_optimization Bug in Example: ad_odeint_example Rework IDE SEIR model

7 participants