Repository navigation
[DNM] Implement CAD AD Example - #278
Draft
joergbrech wants to merge 22 commits into
Draft
joergbrech wants to merge 22 commits into
joergbrech wants to merge 22 commits into
Conversation
A standalone example (own pixi environment, own CMake build) demonstrating grunk alongside geoml/OpenCASCADE for CAD modeling, and ADOL-C for automatic differentiation through a dynamic recipe: - register_geoml wraps a handful of geoml/OCCT types and functions (gp_Pnt, bezier_curve, interpolate_curve_network) as grunk dynamic types, building a Gordon surface fuselage cross-section from a recipe. - register_adolc loads ADOL-C's tapeless adouble type via a compiled SWIG-Lua module (adtl.so, built separately per the README) through grunk::state's load_compiled_plugin/register_external_type, and runs a small autodiff recipe through it end to end (write, read back, evaluate). ADOL-C and the SWIG-Lua wrapper are built manually outside of pixi/CMake's own dependency fetching (see README.md for the build steps); everything else (grunk, geoml, opencascade, sol2/lua/yaml-cpp/libparametric) is fetched or resolved automatically via pixi/CMake FetchContent. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TS5yFa1xBMh7dhCLj5yNi3
register_external_type now auto-discovers member functions by probing an instance the first time each is looked up (merged in #262), so the explicit setADValue/getADValue/getValue listing is no longer needed to bridge adtl.adouble.
Move the gp_Pnt/bezier_curve/interpolate_curve_network registration out of register_geoml (called directly from cad_autodiff) into its own shared library (plugins/geoml/geoml_plugin.cpp, built as the geoml_plugin CMake target), and dlopen it at runtime via a new load_geoml_plugin, the same way adtl.so is already loaded. This is #235's "C++ plugin" kind, alongside the existing "compiled Lua module" mockup. Since this plugin shares sol2 usertype machinery for OCCT types with the host executable (unlike adtl.so, where ADOL-C's adouble crosses the boundary only as an opaque grunk::object), it needs RTLD_GLOBAL plus ENABLE_EXPORTS on cad_autodiff so both sides resolve to the same template instantiations - otherwise Handles built by the plugin fail to convert back on the host side. geoml_plugin is a shared library linking the static grunk/geoml libraries, so those need CMAKE_POSITION_INDEPENDENT_CODE too. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011PZm8wzearBkvqPRtyoyZs
Rename write_recipe_ad/read_recipe_ad -> write_autodiff_only_recipe/ read_autodiff_only_recipe (stage 1: AD, no CAD) and write_recipe/ read_recipe -> write_cad_only_recipe/read_cad_only_recipe (stage 2: CAD, no AD), and wire both into main() with printed stage headers - stage 2 was previously dead code. Strip the leftover broken/commented surface-interpolation and debug scaffolding that didn't serve either stage. Document the 3-stage story (stage 3 - CAD *with* AD, via a future geoml+ADOL-C plugin - not yet implemented) in a file-level comment and in README.md, explaining why the example is named "cad_autodiff". Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011PZm8wzearBkvqPRtyoyZs
Adds plugins/geoml_adolc/, the AD-enabled counterpart to geoml_plugin.so: a standalone CMake project (kept separate from the main one since it needs a different, AD-enabled geoml under the same CMake target name stock geoml already claims) plus geoml_adolc_plugin.cpp, which shares register_geoml with geoml_plugin.cpp unmodified via the new plugins/geoml_registration.hpp. Not yet buildable - it needs an adOCCT install and geoml's feature/autodiff branch built against it, with an OCCT version mismatch (geoml pins 7.6.2, adOCCT forks V7_9_0) reconciled first. README.md's new "Stage 3" section documents this, and every remaining action is tagged TODO(stage3) for `grep -rn 'TODO(stage3)'`. main.cpp gained write_cad_recipe(bool with_ad)/read_cad_recipe (renamed from write/read_cad_only_recipe) and read_recipe_ad: the same recipe script, with with_ad only choosing which plugin loads - the point of the whole example. The CAD recipe itself is simplified to a single bezier curve (a dev artifact for exercising the plugin swap, not a shape worth modeling), renamed gordon.grr.yml -> bezier_curve.grr.yml. occt_sol_traits.hpp moves from include/ into plugins/, and geoml_plugin gains an export_brep function so main.cpp no longer needs to include any OCCT header at all - even verifying a curve got built now happens via a registered function called from within the recipe. That also means geoml_plugin.so no longer needs RTLD_GLOBAL/ENABLE_EXPORTS to load correctly (reverting part of the previous commit): with no OCCT-typed value crossing into main.cpp's own translation unit, there's nothing left for the dynamic linker to unify. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011PZm8wzearBkvqPRtyoyZs
register_geoml now takes the namespace table + PluginInfo begin_plugin hands back, and registers everything (gp_Pnt, Geom_BezierCurve, bezier_curve, export_brep, and AD-only Standard_Real) under it with the qualifier that keeps serialization resolvable - both geoml_plugin.cpp (stage 2) and geoml_adolc_plugin.cpp (stage 3) now export the fixed grunk_plugin_info/grunk_plugin_register ABI instead of a bespoke per-plugin entry-point symbol, and main.cpp loads both via grunk::plugin::load_native instead of hand-rolled dlopen/dlsym. Recipe scripts updated for the resulting namespacing: gp_Pnt -> geoml.gp_Pnt, bezier_curve -> geoml.bezier_curve, export_brep -> geoml.export_brep, and the "ad" module's Standard_Real -> geoml.Standard_Real. Colon-call sites (curve:Value(...), point_at_half:X()/:Y()) are unaffected - native colon-call dispatch resolves via a C++-type-keyed registry, not Lua-side naming, so namespacing geoml's tables doesn't touch it. Verified end-to-end against a local grunk checkout (not yet on grunk-dev/grunk's main, which this example's CMakeLists.txt FetchContents from): all three stages produce identical output to before the migration, and the written recipe's uses: block now correctly lists "geoml". Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Moves the SWIG-bridging work (register_external_type + the __tostring metatable patch) out of main.cpp and into a proper plugin shim, plugins/adtl/adtl_plugin.cpp, which links directly against the prebuilt adtl.so and exports the same fixed grunk_plugin_info/grunk_plugin_register ABI geoml_plugin.so does - main.cpp now loads it via grunk::plugin::load_native uniformly, with zero SWIG-specific code left in application code. An application loading this plugin only ever sees PluginInfo + grunk::state, same as any other kind. This also let ADOL-C drop out of the main project entirely: main.cpp no longer includes adolc/adtl.h or links libadolc directly (both were vestigial - nothing in this file ever used adtl:: types in C++), and the ADOLC_BASE_DIR/ADOLC_INCLUDE_DIR/ADOLC_LIB_DIR find_library/find_file block is gone from CMakeLists.txt along with its pixi.toml configure flag - ADOL-C is now confined entirely behind adtl.so and, from there, behind adtl_plugin.so, the same way OCCT/geoml is confined behind the geoml plugins. adtl_plugin.so doesn't need it either: adtl.so already carries its own libadolc dependency, resolved transitively at load time. Verified end-to-end: all three stages produce identical output to before, including stage 1's serialized adtl.adouble round-trip. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Replaces the "dlopen'd... a stand-in for grunk's still-unwritten runtime plugin loader" framing (no longer true) with a description of the real grunk::plugin::load_native mechanism both plugins now use, adds adtl_plugin.so to the Project Structure section, fixes the ADOL-C prerequisites section's now-dead pixi.toml/ADOLC_BASE_DIR cross-reference, and updates the Stage 3 walkthrough's Lua snippets (gp_Pnt.*, Standard_Real.*, export_brep) to their namespaced geoml.* form. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
load_native was pure POSIX (<dlfcn.h>: dlopen/dlsym/dlerror) - it would not even compile on Windows. Splits the native-library-loading and symbol-lookup primitives into a POSIX branch (unchanged) and a Windows branch (LoadLibraryW/GetProcAddress/GetLastError+FormatMessage), behind a single native_handle/open_library/last_error_message/ find_symbol_checked seam so load_native's own logic stays identical on every platform. Also adds GRUNK_PLUGIN_EXPORT (grunk/plugin/loader.hpp): a plain extern "C" alone exports a symbol from a shared library by default on Linux/macOS, but not from a Windows DLL - GetProcAddress would find nothing without __declspec(dllexport) on the definition (or CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON on the *plugin's own* CMake project, which a third-party plugin build doesn't inherit from grunk's). Every existing native-plugin entry point (the cad_autodiff example's three plugins, tests/cpp's fixture) now uses this macro instead of a bare extern "C", so plugin source is portable across all three platforms without per-plugin CMake changes. Verified on Linux: full grunk test suite (168 tests) and cad_autodiff's three stages still pass/produce identical output. The Windows branch is unverified by compilation - no Windows toolchain available here - so treat it as reviewed-by-reading, not build-tested. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Second round of fixes from #275's review: - load_native no longer erases an already-loaded plugin's bookkeeping when a retry/double-load fails on a name collision. - forget_plugin now clears the namespace table it created (not just plugins() bookkeeping), so a rolled-back plugin's partial registration is actually gone. - clear_module also clears package.loaded, so load_compiled_plugin genuinely reloads instead of returning a stale cached module. - Starting a plugin under a name already used by a plain module now throws instead of silently clobbering that module. - modify_type refuses to overwrite the type registry with an invalid/missing usertype table instead of corrupting native colon-call dispatch. - load_compiled_plugin's namespace table now supports qualifier auto-inference; register_external_type rejects a name missing its namespace prefix instead of silently serializing an unresolvable one. - Guard the cad_autodiff example's CMP0169 policy set for CMake <3.30, and scope PIC to grunk's own targets instead of the whole build. - Fix a library-handle leak in load_native, an unchecked int->unsigned narrowing cast into an AD derivative buffer, and a SWIG __index override that masked lookup errors as nil. - Add GRUNK_PLUGIN_REGISTER: a plugin's exceptions are now caught on its own side of the dlopen boundary and reported as a plain error string, since unwinding a C++ exception across that boundary is unsafe unless the plugin and host share the exact same compiler/stdlib/library versions. All bundled plugins are migrated onto it. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
begin_plugin/load_compiled_plugin now return a grunk::plugin_namespace proxy
instead of a bare sol::table: it remembers its own namespace table and
qualifier, so register_type/register_function calls against it no longer
repeat "ns, info.name" at every call site. register_external_type gets a real
behavior change, not just less typing - it auto-prefixes a short name
("MyClass" -> "my_plugin.MyClass") instead of requiring the caller to
concatenate the qualifier by hand, the way the raw state::register_external_type
still requires (it only validates a caller-supplied fully-qualified name).
plugin_namespace is implicitly convertible to sol::table (and exposes
.table()/[]) as an escape hatch for code that still needs the raw
state::register_type/register_function API, e.g. to override the namespace's
own qualifier.
Updates every begin_plugin/load_compiled_plugin call site to the new style:
examples/cpp/cad_autodiff's geoml/adtl plugins, the plugin test fixtures, and
the docs/README examples. Adds a regression test for the escape hatch
(PluginBegin.namespace_table_interoperates_with_raw_state_api) and updates the
compiled-plugin external-type test to exercise the new auto-prefixing.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
… glob, document examples in README These hunks were originally bundled into mixed core+example commits when this history was split off of implement-plugins; they belong with the example since they exist to support examples/cpp/cad_autodiff. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
joergbrech
force-pushed
the
cad-autodiff-example-split
branch
from
September 17, 2026 13:14
e314085 to
91c5c69
Compare
Collaborator
Author
|
This PR is outdated. The grocc plugin has been ported to https://github.com/grunk-dev/grunk-occt and this repo contains examples and tests deomnstrating CAD AD with occt. I will close this entirely, as soon as we have grunk-geoml with AD support and tests and examples in this repo. |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR adds a previous CAD AD example, reimplemented in terms of the plugin interface introduced in #277. Therefore this PR should not be merged before #277. The example demonstrates that we can build a grunk plugin from occt and geoml in two flavors, with AD and without. In both build variants the symbols in the plugins remain the same, such that the recipes, if written carefully, need no adaption to either case. A user can run a single recipe with or without AD, depending on which flavor of plugin is loaded.
The example uses the current state of the plugin system and assumes that no occt or geoml plugins exist yet. It needs to grow as grunk and its plugin ecossytem grows.