Skip to content

Fmi2 directional derivatives - #807

Merged
davidhjp01 merged 9 commits into
masterfrom
fmi2-directional-derivatives
Sep 23, 2026
Merged

davidhjp01 merged 9 commits into
masterfrom
fmi2-directional-derivatives

Conversation

@davidhjp01

Copy link
Copy Markdown
Contributor

Add FMI 2 directional derivative support

Summary

Adds validated FMI 2.0 directional-derivative support to
fmi::v2::slave_instance, lifecycle-aware FMU state handling, deterministic
fixture coverage, and the dependency/CI updates required to build and test it.

Changes

  • Add provides_directional_derivatives() and
    get_directional_derivative().

  • Read and cache directional-derivative metadata from
    modelDescription.xml.

  • Validate lifecycle, value-reference type, reference validity, and seed and
    sensitivity sizes before calling the FMU.

  • Compute the Jacobian-vector product:

    sensitivity = d(unknowns) / d(knowns) * seed
    
  • Accept FMI OK and Warning; surface Discard, Error, Fatal, and
    Pending as errors.

  • Preserve caller output when the FMU call fails.

  • Track instantiated, initialization, step, and terminated states.

  • Serialize lifecycle state in export scheme 1 while retaining safe scheme 0
    compatibility.

  • Add cleanup and ownership checks for imported FMU state.

Test coverage

  • Add a deterministic FMI 2 Co-Simulation fixture with three Real inputs, two
    Real outputs, a fixed 2-by-3 Jacobian, configurable statuses, a call counter,
    and a non-Real reference.
  • Extend fmi_v2_fmu_unittest for lifecycle behavior, expected derivatives,
    reordered references, invalid inputs, status handling, output preservation,
    and unsupported capabilities.
  • Build the pinned Reference-FMUs v0.0.41 and package the fixture as
    Fmi2Derivatives.fmu; expose them through REFERENCE_FMU_V2_DIR.

Dependency and CI updates

  • Upgrade FMI Library to ~3.0.4 and proxyFMU to 0.4.3@osp/testing.

Use FMI Library 3.0.4 and carry the repository CI/package updates needed by the FMI 3 stack.
Implement validated FMI 2 directional derivatives with a dedicated fixture, reference-FMU wiring, and contract tests.
@davidhjp01 davidhjp01 self-assigned this Sep 18, 2026
@davidhjp01 davidhjp01 added the enhancement New feature or request label Sep 21, 2026

@kyllingstad kyllingstad left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks! Great to finally have some more FMI 2.0 support… :D I have a few comments, mostly on form rather than substance.

Comment thread .github/workflows/ci-cmake.yml Outdated
Comment thread src/cosim/fmi/v2/fmu.cpp Outdated
Comment thread src/cosim/fmi/v2/fmu.cpp Outdated
throw error(
make_error_code(errc::model_error),
instanceName_ +
": invalid lifecycle operation: GetDirectionalDerivative");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this error message is misleading. Getting directional derivatives is not a "lifecycle operation", it is an operation that may be invalid at the current state in the FMU's lifecycle. I'm also not sure whether errc::model_error is the right error code for this. Maybe we need a new one, e.g. errc::invalid_operation or something. (It wouldn't be unique to this particular situation; there are many cases in the FMI spec where certain operations are prohibited at a certain lifecycle stage. Could also be used for the "invalid (un)known" errors further down in this function.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated to throw invalid_operation and text

Comment thread src/cosim/fmi/v2/fmu.cpp Outdated
Comment thread tests/fmi2_derivative_fixture/model.c
Comment thread tests/fmi2_derivative_fixture/model.c
Comment thread tests/CMakeLists.txt

@kyllingstad kyllingstad left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Almost there now. :)

Comment thread src/cosim/fmi/v2/fmu.cpp Outdated
// Increment this number whenever the "exported state" changes form, and
// always consider whether backwards compatibility measures are warranted.
constexpr std::int32_t export_scheme_version = 1;
constexpr std::int32_t export_scheme_version = 0;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This should not be reverted, IMO. It is a new scheme version, my point was just that shouldn't worry about maintaining compatibility with the previous one.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Made it back to 1

@kyllingstad kyllingstad left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice, thanks for doing this!

@davidhjp01
davidhjp01 merged commit 5c7a253 into master Sep 23, 2026
20 checks passed
@davidhjp01
davidhjp01 deleted the fmi2-directional-derivatives branch September 23, 2026 06:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants