Skip to content

CalculiX target: dlopen'd .so material (no ccx recompile per material) - #127

Merged
petlenz merged 10 commits into
mainfrom
feature-calculix-so
Nov 29, 2026
Merged

petlenz merged 10 commits into
mainfrom
feature-calculix-so

Conversation

@petlenz

@petlenz petlenz commented Aug 1, 2026

Copy link
Copy Markdown
Member

Adds CalculiXExternalTarget (make_target("calculix")) — codegen's first target whose output runs in a real external FE solver. It emits <Model>_ext.cpp compiling to lib<MODEL>.so, which CalculiX loads at runtime via dlopen: build ccx once with external-behaviour support, then each new material is just a new .so selected by name in the deck — no ccx recompile per material.

Supersedes the earlier two-PR split (#125 compiled-in umat_user_ + #126 external): the compiled-in approach needed a ccx relink per material and is dropped in favour of the .so path.

Mechanism (verified against ccx 2.22 source)

A deck *MATERIAL, NAME=@<LIB>_<FUNC> makes ccx (which uppercases the name) dlopen("lib<LIB>.so") + dlsym("<FUNC>") (external.c, -DCALCULIX_EXTERNAL_BEHAVIOURS_SUPPORT -ldl). The STANDARD interface passes native quantities under Abaqus-flavored names — STRAN1=emec (tensorial), STRESS=stre, DDSDDE=stiff(21) packed — handled by a tmech abq_std Voigt boundary + column-major stiff(21) packing (src/targets/calculix_boundary.h). The emitted plugin is a per-thread lifecycle: a thread_local stateless evaluator (ccx runs the element loop multi-threaded) that reads the material constants from MPROPS on every call (ccx interpolates them by temperature).

Verification

  • CI (no external deps): calculix_check_driver calls the emitted NCG_UMAT through the exact calculixptr ABI and checks stre/stiff vs an independent isotropic oracle + a packing-order negative control + a read-constants-every-call regression + the icmd==3 stress-only path. Full suite 275 + 7 green.
  • Real ccx (manual, examples/calculix/): build ccx once with external support, compile the codegen'd .so, run by @-name. Confirmed S11=0.027, S22=S33=0.013 (uniaxial) and S12=0.007 (simple shear — pins the tensorial convention).

Review

Went through a 3-lens review (architecture · C++/ABI · correctness); REVIEW.md has the findings. Fixes folded in: per-call MPROPS (was a stale thread_local cache — CRITICAL), symmetry guard (reject non-symmetric deformation-gradient inputs), shared boundary helper, shear-convention validation deck, single-table factory, and hardened build scripts.

Scope

Stateless (elastic). The STATEV round-trip for stateful J2 (numsim-materials-backed evaluate()) is the next step; the per-thread evaluator scaffold is already in place for it.

🤖 Generated with Claude Code

petlenz pushed a commit that referenced this pull request Aug 1, 2026
petlenz pushed a commit that referenced this pull request Aug 1, 2026
# Conflicts:
#	tests/calculix/linear_elastic/gold/gen_gold_shear.inp
#	tests/calculix/linear_elastic/gold/gen_gold_uniaxial.inp
#	tests/calculix/linear_elastic/shear.inp
#	tests/calculix/linear_elastic/uniaxial.inp
petlenz added a commit that referenced this pull request Sep 20, 2026
petlenz added a commit that referenced this pull request Sep 20, 2026
# Conflicts:
#	tests/calculix/linear_elastic/gold/gen_gold_shear.inp
#	tests/calculix/linear_elastic/gold/gen_gold_uniaxial.inp
#	tests/calculix/linear_elastic/shear.inp
#	tests/calculix/linear_elastic/uniaxial.inp
@petlenz
petlenz force-pushed the feature-calculix-so branch from 6b4d04b to bc0c084 Compare September 20, 2026 19:46
@petlenz

petlenz commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

C++ review

Verdict: the ABI, naming, symbol and packing are correct (confirmed against a real external-enabled ccx 2.22), but the plugin silently returned wrong answers for *INITIAL CONDITIONS, TYPE=STRESS, read unchecked MPROPS on a constant-count mismatch, could throw into Fortran, and did not compile when a recipe parameter was named like an emitted local. All fixed in 2322676. Issue #162 items 1, 2, 4 and the STATEV comment are real and fixed; item 3 (ielas) is not a bug for this target.

# Sev Where Problem and how observed Fix
1 Critical calculix_external.cpp (entry, beta argument commented out) A deck with *INITIAL CONDITIONS, TYPE=STRESS (0.001 on S11) through real ccx with the old plugin gave S11 = 2.7e-02, i.e. the initial stress was dropped. Entry refuses a nonzero beta (any of 6 components, NaN counts): message, zeroed outputs, exit(-1) as ccx itself does. Real ccx: ... a nonzero initial stress ... is not supported, exit 255.
2 High same (iorien commented out) *ORIENTATION through real ccx, old plugin: it ran and printed stress in rotated axes (S11=0.013, S22=0.027) with no diagnostic; for an anisotropic recipe the frame is simply wrong (umat_user.f says rotation is the material's job). Refused with a message (same behaviour as numsim-materials calculix_interface.h). Note this is conservative: an isotropic recipe with *ORIENTATION is also refused, because the target cannot tell.
3 High entry reads MPROPS[k] positionally; kode ignored CONSTANTS=1 or =3 for a 2-parameter recipe ran silently on stale/extra memory. -kode-100 must equal the parameter count, else refused. Real ccx with CONSTANTS=3 and 1: refused, exit 255.
4 High entry had no try/catch Any throw from the recipe call would cross extern "C" into ccx. Injected a throwing _compute into the real emitted file: previously propagates. try { evaluate } catch (...): zero outputs, *PNEWDT = 0.25, message.
5 High emitted evaluate() locals (mprops, strain_in, stress_out, tangent_stiff, icmd, <t>_D6) A recipe parameter named mprops / icmd / strain_in / tangent_stiff gave declaration of 'const double mprops' shadows a parameter (hard compile error in generated code). Internals renamed ncg_*; recipe identifiers starting with ncg_ or equal to <tensor>_ad are rejected at emit time with a clear message.
6 Medium library_name() ccx copies LIB into fixed 80-byte buffers (external.c b[80], b1[80]) without checks; a 71+ char name overruns ccx's stack. @ABAQUS_NCG_UMAT / @ABAQUSNL_... are stripped as interface prefixes before the LIB_FUNC split (read in external.c, not run), so models named Abaqus/AbaqusNL can never load. Emit throws for > 70 chars and for those two names.
7 Medium entry signature Under -fvisibility=hidden the entry is not exported (nm -D shows 0 symbols); ccx then says "unable to load function". Only a comment warned about it. NCG_EXPORT (visibility default / dllexport) on the entry. nm -D shows NCG_UMAT hidden or not.
8 Low comments Emitted header said "MPROPS ... unchecked" (now checked); STATEV comment implied nothing about layout. STATEV0/1 documented as the FULL xstate(nstate_, mi(1), #elem) arrays, never touched here; header documents the refusals.

#162 item 3 (ielas): deliberately not refused. The scope check admits only stateless recipes, i.e. a pure strain->stress map; an elastic iteration cannot change that answer, and refusing would break *BUCKLE/*FREQUENCY for no reason. Covered by AcceptsZeroInitialStressAndElasticIteration; #160/non-elastic recipes must add the check when they land.

Tests added (all fail on the pre-fix code, verified by reverting calculix_external.cpp): calculix_check_driver death tests (RefusesOrientation, RefusesNonzeroInitialStress incl. last-component-only, RefusesWrongConstantCount, ielas accepted); new calculix_throw_driver (real emitted plugin with the recipe call swapped for a throwing one: asserts no exception, zeroed outputs, PNEWDT 0.25; fails on old code); calculix_shadow_names compile-only object (fails to compile on old code); unit tests for name length, ABAQUS prefix, ncg_ prefix, 2D recipe, guards present.

Checked and held up: entry argument list vs calculixptr in ccx 2.22 call_external_umat_user.c (31 args, int size by value); @LIB_NCG_UMAT split on the first _, uppercase/alnum-only name; dlsym of NCG_UMAT verbatim; real ccx run uniaxial S11 = 0.027 / S22 = 0.013; stiff(21) order matches *ELASTIC,TYPE=ANISO, symmetrised (non-symmetric 6x6 loses only its skew part, same as umat_abaqus.f); scope guard rejects non-symmetric tensors, non-3D, scalar inputs, state, multiple tensors; evaluator is stack-only and const, so thread-safe (thread_local is redundant but harmless); engineering-vs-tensorial shear: shear deck gives S12 = 0.007 in real ccx (#129).

Outside this PR (pre-existing, all targets): the generated <Model>_compute names its locals t0..tN and outputs <name>_out; a parameter spelled like those collides (a parameter stress_out fails to compile). Not part of this target.

Final verification: commit 2322676; gcc-14 Debug -Werror, full build, ctest: 100% tests passed, 0 tests failed out of 368. clang++-18 -fsyntax-only on the touched TUs: no new diagnostics from this code.

Open decision: *ORIENTATION is also refused for isotropic recipes, since the target cannot tell them apart. Safe but stricter than necessary; an isotropy declaration on the recipe would allow relaxing it.

@petlenz
petlenz merged commit 1d0c621 into main Nov 29, 2026
5 checks passed
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.

1 participant