Skip to content

Generalize the temperature scalar type for forward-mode AD - #35

Open
Samm-Py wants to merge 6 commits into
ORNL-MDF:mainfrom
Samm-Py:scalar-template-ad
Open

Generalize the temperature scalar type for forward-mode AD#35
Samm-Py wants to merge 6 commits into
ORNL-MDF:mainfrom
Samm-Py:scalar-template-ad

Conversation

@Samm-Py

@Samm-Py Samm-Py commented Aug 5, 2026

Copy link
Copy Markdown

Summary

  • Make the temperature field scalar configurable in Grid, Boundary,
    Layer, and SolidificationData, while retaining double as the default.
  • Derive the solver scalar from its temperature view and add scalar-valued
    material/source properties plus ADL-based math and value customization
    points. Finch core remains independent of any AD library.
  • Add an optional finch_sensitivity application using cpp_oti_lib to
    compute first-order sensitivities for six material and source parameters in
    one solve and compare them with central finite differences.

Compatibility

  • Existing callers continue to use double without source changes.
  • Mesh geometry, phase-change thresholds, and downstream solidification-event
    storage remain double.
  • The sensitivity application is built only when cpp_oti_lib is found; the
    standard Finch build has no new dependency.

Validation

  • clang-format 14.0.6 check passed for every changed C++ file.
  • GCC 9.4 Release builds passed with -Wall -Wextra -pedantic -Werror, both
    with and without cpp_oti_lib available.
  • The single_line temperature outputs are bit-for-bit identical to upstream
    main; solidification records are identical (their parallel emission order
    is nondeterministic).
  • The sensitivity application completed on one and four MPI ranks. OTI values
    matched the ordinary double solve, and all six derivatives were validated
    against central finite differences using FINCH_FD_STEP=1e-8.

Finch's field type was fixed to double. This threads a Scalar template
parameter (defaulting to double) through Grid, Boundary, Solver, Layer and
SolidificationData, so the temperature field can carry a user-supplied
arithmetic type. With double the generated code and temperature fields are
unchanged; verified bit-identical temperature output and identical
solidification records on the single_line case.

The differentiated material and source inputs move into a MaterialProperties
struct so a caller can supply values of the field scalar type rather than plain
doubles read from the input deck. Quantities that only select a branch
(solidus, liquidus) and all mesh geometry stay double.

Finch_Scalar.hpp adds exp/fmin/fmax dispatch: arithmetic types forward to
Kokkos as before, anything else resolves by ADL into the scalar type's own
namespace. Finch therefore carries no dependency on any AD library.

The optional finch_sensitivity application demonstrates the result with
cpp_oti_lib, obtaining d(QoI)/dp for six material and source parameters from a
single solve and validating each against central finite differences.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PNkQTS2g7P6dNDybhWRkwz
@Samm-Py

Samm-Py commented Aug 5, 2026

Copy link
Copy Markdown
Author

@Rombur The PR is open and ready for review.

Comment thread applications/Finch_OTI.hpp Outdated
/****************************************************************************
* OTI (order-truncated imaginary) scalar type glue for Finch.
*
* This header is the ONLY place where Finch and cpp_oti_lib meet. It lives in

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Change: I would pick a third place rather than put it here

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Moved the Finch/Sparrow adapter out of applications into integrations/sparrow/Finch_Sparrow.hpp.

Comment thread applications/CMakeLists.txt Outdated

# Optional forward-mode AD sensitivity application. Built only when the
# header-only cpp_oti_lib is available; Finch itself never depends on it.
find_path(CPP_OTI_LIB_INCLUDE_DIR otinum/otinum.hpp

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Clarification: where is the OTI lib? And can we make it a real target instead? What's the plan there

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The library is header only. Including this file into your path is enough.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sure, but why is Sam hiding it from me

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

We are going to move it to a private repo inside https://github.com/ORNL-MDF and then open source it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggestion: I think SingleLayer_Sensitivity.cpp is more clear

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Done!

Comment thread applications/Finch_OTI.hpp Outdated
KOKKOS_INLINE_FUNCTION static double
value( const oti::otinum<M, N, Coeff>& x )
{
return static_cast<double>( x.real() );

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Clarification: if we're going to all the trouble of propagating these AD types, is it worth also exposing the underlying scalar type as double/float?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Changes made to preserve underlying float/double coefficient type.

Comment thread applications/CMakeLists.txt Outdated

# Optional forward-mode AD sensitivity application. Built only when the
# header-only cpp_oti_lib is available; Finch itself never depends on it.
find_path(CPP_OTI_LIB_INCLUDE_DIR otinum/otinum.hpp

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The library is header only. Including this file into your path is enough.

Comment thread applications/Finch_OTI.hpp Outdated
Comment thread applications/Finch_OTI.hpp Outdated
case TwoSigma:
return "two_sigma";
default:
return "unknown";

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

You can assert here or does the code work with unknown?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Replaced silent unknown parameter labels with exceptions to prevent masked invalid-index errors.

@@ -0,0 +1,161 @@
/****************************************************************************

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Here and in the new files, you forgot the copyright for Finch

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Added Finch’s standard copyright and BSD-3-Clause license header to every new file.

Comment thread applications/Sensitivity.cpp Outdated

constexpr int NP = Finch::Sensitivity::NumParameters;

// First-order jets: one variable per differentiated parameter, derivatives

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Please add a comment what jets means.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Changed Jet to OTINumber

{
std::array<std::string, 6> bc_types = { "adiabatic", "adiabatic",
"adiabatic", "adiabatic",
"adiabatic", "adiabatic" };

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Should these boundary conditions come from the input file @streeve ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yeah, that would be a nice improvement

Comment thread applications/Sensitivity.cpp Outdated

// Evaluate the QoIs on the host. Copying the field to a host mirror keeps
// this independent of Kokkos reducer support for compound scalar types,
// which is not needed for a once-per-run diagnostic.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Does this mean that parallel_reduce does not work out of the box with otinum?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

parallel_reduce works with a Kokkos-enabled otinum. The original version conservatively copied the temperature field to the CPU and summed it serially. We have since replaced that with Cabana’s grid_parallel_reduce and validated it on OpenMP and CUDA.

Comment thread src/Finch_Grid.hpp Outdated
Comment thread src/Finch_Solver.hpp Outdated
// solver only ever compares against -- solidus and liquidus -- stay double:
// they select a branch rather than entering the arithmetic.
template <typename Scalar>
struct MaterialProperties

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I am not sure about the name because absorption and two_sigma are source properties.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Renamed the bundle to SolverParameters because it contains both material and source parameters.

Samm-Py and others added 2 commits August 7, 2026 11:22
Co-authored-by: Bruno Turcksin <bruno.turcksin@gmail.com>
Co-authored-by: Bruno Turcksin <bruno.turcksin@gmail.com>
@Samm-Py
Samm-Py requested review from Rombur and streeve August 7, 2026 19:22
@Samm-Py Samm-Py mentioned this pull request Aug 7, 2026
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.

4 participants