Commit Graph

14 Commits

Author SHA1 Message Date
Jukka Aho 4968acd069 refactor(elements): redesign Element type to Ciarlet's triple (K,P,Σ) with DOF-based API
Major architectural refactoring: replace old field-based element system
with modern DOF-based design following Ciarlet's finite element triple.

- Redesign Element from Element{N,NIP,F,B} to Element{K,P,S,N}
- Replace connectivity with dof_indices (flat tuple of global DOF indices)
- Remove all old field system (sfields, dfields, fields, update_field!, etc.)
- Remove old constructors and compatibility shims
- Add compile-time DOF computation via @generated ndofs(K, S)
- Add field_dof_range for compile-time local DOF range computation
- Add local_to_global_map for type-stable DOF mapping
- Add topology_type, basis_type, dof_type query functions
- Add element_id, element_dofs, n_element_dofs, nnodes accessors
- Remove 593 lines of legacy code (963 → 370 lines)
- Simplify API: elements now store DOF indices, not node connectivity
- Support multi-field elements via DOFSet specifications
- Zero-allocation design with compile-time type information
2025-12-12 23:40:15 +02:00
Jukka Aho 39b90b210e refactor(elements): Migrate Element constructor to new Lagrange{P} API
- Changed Element(::Type{T}, connectivity) to use Lagrange{order} instead of Lagrange{base_topo,order}
- Commented out old API functions using Lagrange{T,P}: _create_topology_instance, jacobian, get_basis, get_dbasis (lines 680-761)
- Commented out get_integration_points_from_basis (lines 854-883)
- Restored get_base_topology function (needed by Element constructor)
- Updated comment explaining new API: topology passed separately, not in type parameter
2025-11-21 00:27:49 +02:00
Jukka Aho 249646b740 refactor(elements): Update get_basis/get_dbasis to use new topology API
Migrate element basis evaluation functions to use new topology-aware API.

Changes:
- Add _create_topology_instance() helper to construct topology from Lagrange{T,P}
- Update get_basis() to call get_basis_functions(topology, basis, xi)
- Update get_dbasis() to call get_basis_derivatives(topology, basis, xi)
- Add jacobian() function with embedding support (1D element in 2D/3D space)
- Constraint: B <: Lagrange added to method signatures

Migration from OLD API:
- eval_basis!(B, T, xi) → get_basis_functions(topology, basis, xi)
- eval_dbasis!(B, xi) → get_basis_derivatives(topology, basis, xi)

Maintains compatibility: still returns matrices/vectors for old code interface.
2025-11-15 04:41:59 +02:00
Jukka Aho b6f9cd3130 refactor(elements): Update integration points to use new API
- get_integration_points_from_basis() maps Lagrange types to Gauss quadrature
- get_base_topology() maps deprecated names to base topology (Tri6→Triangle)
- Use get_gauss_points!() for zero-allocation integration
- Fix interpolate() to handle both AbstractField and raw data
- Fix Jacobian computation: preserve connectivity order in Dict→Vec conversion
- Support order parameter for increased quadrature accuracy
- Gauss orders 1-5 supported for all topologies
2025-11-12 01:10:22 +02:00
Jukka Aho 30ca3de56a feat(elements): Add immutable update() function for elements
- Implement update() that returns new element (immutable pattern)
- Supports keyword arguments for ergonomic field updates
- Preserves backward compatibility with update!() (legacy)
- Dual-API approach: modern immutable + legacy mutable both supported
- 82 lines including documentation and examples
- See docs/book/fundamentals_element_creation.md for usage guide
2025-11-10 22:25:59 +02:00
Jukka Aho aab8b7d6ce feat(test): First test rewritten for immutable elements (test_elasticity_1d)
Rewrote test_elasticity_1d.jl to follow immutable element pattern.
This is the first fully working test with the new architecture!

Changes:
1. test/test_elasticity_1d.jl:
   - Convert Dict node data to element-local tuple format
   - Wrap data in DVTI field objects (Discrete, Variable, Time-Invariant)
   - Create element with fields at construction: Element(Seg2, conn; fields=(...))
   - Fix Jacobian shape expectation (3×1 not 1×3 for 1D in 3D)

2. src/JuliaFEM.jl:
   - Add minimal jacobian() function for AbstractBasis (non-parametric)
   - Handles embedding (1D element in 3D space) correctly
   - Returns Matrix instead of Tensor for flexibility

3. src/elements/elements.jl:
   - Fix Jacobian computation to handle both Tuple and IntegrationPoint
   - Fix detJ calculation logic for embedded elements (check m not size(JT,2))
   - Correctly handle 1D elements: detJ = ||∂X/∂ξ||

Result: test_elasticity_1d.jl passes! ✓

This validates the immutable architecture:
- Element created with fields at construction
- No mutation needed during test
- Field system integration working (DVTI fields)
- Jacobian computation working for embedded elements
2025-11-09 18:42:56 +02:00
Jukka Aho 41e09b2c92 feat(compat): Add compatibility shim for old mutable field API
Implements compatibility layer to allow old test code to run with new
immutable element design (though fields won't actually update).

src/elements/elements.jl:
- Replaced has_dfield/get_dfield to work with new fields API
- Fixed get_sfield/get_dfield to handle empty Tuple{} fields
- All dfield functions now map to element.fields (immutable NamedTuple)

src/topology/*.jl (seg2, tri3, quad4, tet4, hex8):
- Added nnodes() implementation for each topology type
- Returns corner node count (backwards compatibility)
- Example: nnodes(::Triangle) = 3, nnodes(::Hexahedron) = 8
- Note: Actual node count depends on basis degree in new architecture

Test Results:
- test_topology_standalone.jl: 36/36 tests passing ✓
- Full test suite: 43 errors (same as before)
- Error breakdown:
  * 40+ tests: Problem types not defined (Elasticity, Heat, Mortar)
  * 2 tests: Mesh readers not defined (aster_read_mesh)
  * 1 test: Tries to mutate empty element (test_elasticity_1d)

Next Steps:
- Tests that create empty elements then mutate need rewriting
- Pattern: Element(Seg2, (1,2)) + update!() → not compatible
- New pattern: Element(..., fields=(geometry=X, displacement=u))
- See docs/design/IMMUTABILITY.md for migration guide
2025-11-09 18:08:39 +02:00
Jukka Aho 7a23faf17d refactor(elements): GPU-ready Element with type-stable fields::F parameter
- Replace AbstractElement{M,B} with AbstractElement{F,B} (F=fields type)
- Replace Element struct: remove dfields Dict, sfields M, properties B
- Add Element struct: id, connectivity NTuple, integration_points NTuple, fields::F, basis::B
- Field container F is type-stable (NamedTuple, struct, or empty tuple)
- Immutable connectivity and fields (GPU-compatible, zero-allocation)
- Add Element(basis_type, connectivity; fields=(), id=0) constructor
- Add Element(topology_type, connectivity; kwargs...) convenience constructors
- Add infer_lagrange_order(topology, n_nodes) to auto-detect polynomial degree
- Support all 17 topologies: Segment, Triangle, Quad, Tet, Hex, Pyramid, Wedge
- Comment out element_info!() function (used BasisInfo from commented-out math.jl)
- BREAKING: Completely new Element API with type-stable fields
- GPU-ready: el.fields.E returns Float64 (compile-time known type)
2025-11-09 17:08:36 +02:00
Jukka Aho abdbcb37cc refactor(elements): Comment out old topology_to_basis shims
- Commented out topology_to_basis() helper and old Element constructors
- These mapped old names (Tri3→Tri3Basis) which no longer exist
- Need to update for new Lagrange{Topology, P} parametric architecture
- Added TODO comment explaining migration needed
- Temporary measure until new Element constructors are implemented
2025-11-09 09:29:06 +02:00
Jukka Aho 6b24ed9d76 refactor: Make Point immutable
Changed Point from 'mutable struct' to 'struct'.

The Dict for fields remains a reference type, so field updates via setindex!
and update! still work correctly. This change improves type stability and
enables better compiler optimizations.

Benefits:
- Better compiler optimizations (immutable types)
- Type stability improvements
- Stack allocation when possible
- No breaking changes (Dict fields still mutable)

Tests: All 157 tests passing
2025-11-09 03:30:29 +02:00
Jukka Aho 907ec0b183 refactor: Zero-allocation basis functions and immutable Element
MAJOR PERFORMANCE REFACTORING:

1. Shape functions return tuples instead of allocating vectors:
   - eval_basis!(): Returns NTuple{N,T} directly (zero allocations)
   - eval_dbasis!(): Returns NTuple{N,Vec{D}} directly (zero allocations)
   - API boundary (get_basis/get_dbasis) still returns vectors for compat

2. Element is now immutable with compile-time known structure:
   - connectivity: Vector{UInt} → NTuple{N,UInt}
   - integration_points: Vector{IP} → NTuple{NIP,IP}
   - Element{N,NIP,M,B} parametrized by connectivity/IP count
   - Changed from 'mutable struct' to 'struct'

3. Helper function for immutability:
   - with_integration_points(element, ips) returns new element
   - get_integration_points() returns tuple directly

Benefits:
- Zero allocations in hot paths (basis evaluation)
- Compile-time sizes enable better optimization
- Type stability improvements
- Stack allocation instead of heap

Breaking changes:
- Element.connectivity is now tuple (use collect() for vector)
- Element is immutable (use with_integration_points for updates)

Tests: All 157 tests passing
2025-11-09 03:29:36 +02:00
Jukka Aho 06e8276268 fix: Change node and element IDs to UInt (Issue #267)
Gmsh returns node and element IDs as UInt64, so we should use unsigned
integers consistently throughout JuliaFEM to avoid unnecessary conversions.

Changes:
- Point.id: Int → UInt
- Element.id: Int → UInt
- Element.connectivity: Vector{Int} → Vector{UInt}
- Element constructors: Accept Integer (converts to UInt internally)
- Default element_id: -1 → 0 (UInt has no negative values)

Benefits:
- Direct compatibility with Gmsh.jl (no Int/UInt conversions)
- Semantically correct (node/element IDs are never negative)
- Slightly more efficient (no sign checks)

Tests: All 156 tests passing

Closes #267
2025-11-09 03:17:34 +02:00
Jukka Aho 52ebe682e9 fix: Standardize on Tensors.jl Vec type throughout
Major architectural decision: Use Tensors.jl consistently everywhere
for geometric vectors, integration points, and coordinates.

Changes to src/elements/elements.jl:
- get_basis(): Convert ip to Vec, use Vector (not Matrix) for eval_basis!
- get_dbasis(): Convert ip to Vec
- jacobian evaluation: Convert geometry and ip.coords to Vec properly
- Handle both raw coordinates (Tuple) and IP struct transparently

New Tutorial 3: Numerical Integration and Jacobian (49 tests)
- Integration point structure and weights
- Jacobian determinant and matrix evaluation
- Numerical integration (constant, linear, quadratic functions)
- Multiple element types (Quad4, Seg2, Tri3)

Tests: 107 → 156 passing (49 new)
Runtime: ~7 seconds

Closes architectural standardization on Tensors.jl.
Related to Issue #250 (merge conflict resolution).

Why Tensors.jl:
- Type stability (100× performance vs Dict-based)
- Material science compatibility (stress tensors)
- Zero-cost abstractions
- Consistent API across all geometric calculations
2025-11-09 03:10:11 +02:00
Jukka Aho 73ec910589 feat: Consolidate FEMBase.jl into JuliaFEM (Phase 1 complete)
MAJOR MILESTONE: FEMBase + FEMBasis fully consolidated, JuliaFEM loads!

Consolidated files:
- src/elements/ (3 files): elements.jl, elements_lagrange.jl, integrate.jl
- src/fields/ (1 file): fields.jl (DCTI, DVTI, DCTV, DVTV, etc.)
- src/sparse/ (1 file): sparse.jl (SparseMatrixCOO, SparseVectorCOO)
- src/assembly/ (2 files): problems.jl, assembly.jl
- src/solvers/ (1 file): solvers_base.jl
- src/analysis.jl, src/core_types.jl (Node, IP, IntegrationPoint)

Changes to JuliaFEM.jl:
- Added dependencies: Tensors, Calculus
- Removed @reexport using FEMBase (now consolidated)
- Added 20+ include statements for consolidated files
- Include order: fields → core_types → fembase_compat → sparse → elements

Compatibility layer:
- Created fembase_compat.jl: Minimal FEMBase submodule for vendor packages
- Temporarily disabled vendor-specific Mortar2D functions in solvers_modal.jl

Bug fixes:
- Changed i == 1 → isequal(i, 1) in integrate.jl (== operator overridden by fields)
- Resolved all FEMBasis. namespace references throughout codebase

Result:
-  JuliaFEM loads successfully on Julia 1.12.1
-  134 exported symbols (was 171 with separate FEMBase)
-  Core types accessible: Seg2, Quad4, Problem, AbstractProblem, etc.
- ⚠️  Vendor packages show FEMBase cache warnings (expected, harmless)

TODO:
- Re-enable Mortar2D functions after vendor consolidation
- Field system == operator override needs redesign (Phase 4)
- Continue Phase 2: Consolidate remaining vendor packages
2025-11-08 09:39:16 +02:00