Skip to content

Library-wide automatic differentiation#849

Open
hverhelst wants to merge 22 commits into
devfrom
newAutoDiff
Open

Library-wide automatic differentiation#849
hverhelst wants to merge 22 commits into
devfrom
newAutoDiff

Conversation

@hverhelst

@hverhelst hverhelst commented Dec 22, 2025

Copy link
Copy Markdown
Member

Enable automatic differentiation throughout the whole library, using dual_t (forward) and var_t (reverse).
The advantage over the existing external/gsAutoDiff is that the new autodiff types in optional/gsAutoDiff are using the licensed autodiff library, which also provides reverse mode.

The module optional/gsAutoDiff can probably not be externalized in a separate GitHub repo, since it implies quite some changes in the main gismo repository.

NEW:
Add the types of the autodiff library, into gsAutoDiff module
Add examples and unittests

IMPROVED:

FIXED:

API:
The gsSurfMesh is now templated over T

For each new/improved/fixed/api change use a new line with the
respective word prefix in capital.


Please consider the following checklist before issuing a pull
request:

  • Have you added an explanation of what your changes do and why
    you'd like us to include them?
  • Have you documented any new codes using Doxygen comments?
  • Have you written new tests or examples for your changes?

Add template to gsSurfMesh (move implementations from cpp to hpp files)
Remove obsolete files

NOTE: ternary operators have been replaced in some cases due to differences in type left and right of `:`. This also affects Eigen source code. I hope we can find a way to solve this differently
…stream components

- Fixed gsSurfMesh.h: Remove non-template operator<< from Vertex struct (free function exists)
- Fixed gsSurfMesh.hpp: Add 'typename' keyword for dependent iterator types in template methods
- Fixed gsSurfMesh.hpp: Add '.template' keyword for dependent template member access
- Added explicit template instantiation for gsSurfMesh<real_t> in gsSurfMesh_.cpp
- Added IO function instantiations (read_mesh, write_mesh, read/write_stl/off/obj/poly)
- Updated gsMultiPatch.hpp: toMesh() now returns gsSurfMesh<T>, preserving scalar type
- Fixed gsWriteParaview templating: Made function template on Scalar parameter
- Updated gsWriteParaview.hpp: Added '.template' for dependent template member access
- Added gsWriteParaview explicit instantiation for gsSurfMesh<real_t>
- Fixed IO_stl.hpp, IO_off.hpp: Added '.template' keywords for dependent template access
- Fixed gsSubdivScheme.cpp: Use Halfedge typedef instead of gsSurfMesh::Halfedge
- Updated example files to use auto or explicit gsSurfMesh<> template args

All examples compile and run successfully. IO operations work correctly with templated mesh.
- Added gsMath.h include for autodiff-aware math::abs function
- Replaced std::abs with math::abs in CmpVec comparator
- math::abs has overloads for autodiff::Dual and other scalar types
- Fixes compiler errors when gsSurfMesh is used with autodiff types
- Mesh examples (subdivSurfaces_example, patches_from_mesh) now build and run successfully with autodiff
Split gsSubDivScheme in `.h` and `.hpp` files as it is templated now.
Added gsSubDivScheme_.cpp for template instantiation
Add adjoint examples for poisson
@hverhelst hverhelst marked this pull request as ready for review March 5, 2026 16:42
@hverhelst hverhelst requested review from a team and filiatra as code owners March 5, 2026 16:42
Copilot AI review requested due to automatic review settings March 5, 2026 16:42

Copilot AI left a comment

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.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

{
const T epsilon( epsilon_type<ad_type_tag>::value() );
const T eps_norm = (max(T(1),max(abs(v0),abs(v1))) * epsilon);
const T eps_norm = (std::max(T(1),std::max(abs(v0),abs(v1))) * epsilon);

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.

Please remplase as math::max(..)

Same for math::abs(..) etc

static inline void acc_global(const T contrib, T & res)
{
# pragma omp atomic update
# pragma omp critical

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.

please use an #if/#else to check if the type is a build-in type.
In that case atomic is available, otherwise use critical

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants