Generalize the temperature scalar type for forward-mode AD - #35
Conversation
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
|
@Rombur The PR is open and ready for review. |
| /**************************************************************************** | ||
| * 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 |
There was a problem hiding this comment.
Change: I would pick a third place rather than put it here
There was a problem hiding this comment.
Moved the Finch/Sparrow adapter out of applications into integrations/sparrow/Finch_Sparrow.hpp.
|
|
||
| # 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 |
There was a problem hiding this comment.
Clarification: where is the OTI lib? And can we make it a real target instead? What's the plan there
There was a problem hiding this comment.
The library is header only. Including this file into your path is enough.
There was a problem hiding this comment.
Sure, but why is Sam hiding it from me
There was a problem hiding this comment.
We are going to move it to a private repo inside https://github.com/ORNL-MDF and then open source it.
There was a problem hiding this comment.
Suggestion: I think SingleLayer_Sensitivity.cpp is more clear
| KOKKOS_INLINE_FUNCTION static double | ||
| value( const oti::otinum<M, N, Coeff>& x ) | ||
| { | ||
| return static_cast<double>( x.real() ); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Changes made to preserve underlying float/double coefficient type.
|
|
||
| # 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 |
There was a problem hiding this comment.
The library is header only. Including this file into your path is enough.
| case TwoSigma: | ||
| return "two_sigma"; | ||
| default: | ||
| return "unknown"; |
There was a problem hiding this comment.
You can assert here or does the code work with unknown?
There was a problem hiding this comment.
Replaced silent unknown parameter labels with exceptions to prevent masked invalid-index errors.
| @@ -0,0 +1,161 @@ | |||
| /**************************************************************************** | |||
There was a problem hiding this comment.
Here and in the new files, you forgot the copyright for Finch
There was a problem hiding this comment.
Added Finch’s standard copyright and BSD-3-Clause license header to every new file.
|
|
||
| constexpr int NP = Finch::Sensitivity::NumParameters; | ||
|
|
||
| // First-order jets: one variable per differentiated parameter, derivatives |
| { | ||
| std::array<std::string, 6> bc_types = { "adiabatic", "adiabatic", | ||
| "adiabatic", "adiabatic", | ||
| "adiabatic", "adiabatic" }; |
There was a problem hiding this comment.
Should these boundary conditions come from the input file @streeve ?
There was a problem hiding this comment.
Yeah, that would be a nice improvement
|
|
||
| // 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. |
There was a problem hiding this comment.
Does this mean that parallel_reduce does not work out of the box with otinum?
There was a problem hiding this comment.
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.
| // solver only ever compares against -- solidus and liquidus -- stay double: | ||
| // they select a branch rather than entering the arithmetic. | ||
| template <typename Scalar> | ||
| struct MaterialProperties |
There was a problem hiding this comment.
I am not sure about the name because absorption and two_sigma are source properties.
There was a problem hiding this comment.
Renamed the bundle to SolverParameters because it contains both material and source parameters.
Co-authored-by: Bruno Turcksin <bruno.turcksin@gmail.com>
Co-authored-by: Bruno Turcksin <bruno.turcksin@gmail.com>
Summary
Grid,Boundary,Layer, andSolidificationData, while retainingdoubleas the default.material/source properties plus ADL-based math and value customization
points. Finch core remains independent of any AD library.
finch_sensitivityapplication usingcpp_oti_libtocompute first-order sensitivities for six material and source parameters in
one solve and compare them with central finite differences.
Compatibility
doublewithout source changes.storage remain
double.cpp_oti_libis found; thestandard Finch build has no new dependency.
Validation
clang-format14.0.6 check passed for every changed C++ file.-Wall -Wextra -pedantic -Werror, bothwith and without
cpp_oti_libavailable.single_linetemperature outputs are bit-for-bit identical to upstreammain; solidification records are identical (their parallel emission orderis nondeterministic).
matched the ordinary
doublesolve, and all six derivatives were validatedagainst central finite differences using
FINCH_FD_STEP=1e-8.