Skip to content

Feature/rigid body#28

Open
chemiskyy wants to merge 14 commits into
masterfrom
feature/rigid_body
Open

Feature/rigid body#28
chemiskyy wants to merge 14 commits into
masterfrom
feature/rigid_body

Conversation

@chemiskyy
Copy link
Copy Markdown
Member

This pull request adds four new example scripts demonstrating rigid body dynamics and contact using the Fedoo library, including bouncing and free fall scenarios for various meshes (sphere, bunny, cow) with PyVista visualization and analytical comparisons. Additionally, it introduces support for rigid body contact in the IPCContact constraint, enabling mixed rigid-deformable contact problems, and updates the constraint module exports.

New example scripts for rigid body dynamics and contact:

  • Added rigid_body_freefall.py: Validates RigidBody and NonLinearNewmark by simulating a sphere in free fall and comparing with the analytical solution. Includes PyVista animation and trajectory plots.
  • Added rigid_body_bounce_ipc.py: Simulates a rigid sphere bouncing on a plane using a 6x6 solver with IPC contact and Rayleigh damping, with analytical comparison and PyVista animation.
  • Added rigid_body_bunny_bounce.py: Simulates a convex hull of the Stanford bunny mesh bouncing on a plane with IPC contact, including full rigid body rotation and PyVista animation.
  • Added rigid_body_cow_bounce.py: Simulates a watertight cow mesh bouncing on a plane with IPC contact and Rayleigh damping, with PyVista animation and plots.

IPCContact constraint enhancements for rigid body support:

  • ipc_contact.py: Added methods and logic to register rigid bodies and build a scatter matrix that maps rigid body surface DOFs to global rigid body DOFs, enabling mixed rigid-deformable contact in IPCContact. [1] [2]

Module export updates:

Introduce full rigid-body support and example simulations. Added a new rigid_body module (RigidBody, RigidBodyAssembly) implementing 6-DOF dynamics, quaternion-based large-rotation updates, direct 6x6 Newmark solver and IPC contact handling (J^T@F projection). Extended IPCContact to support rigid bodies: add_rigid_body, scatter-matrix mapping of surface DOFs to global rigid DOFs using exact rotation derivatives, runtime rebuild of the scatter matrix, and various checks/adjustments for gradients, padding and CCD/OGC incompatibilities. Export RigidBody from fedoo.constraint. Add example scripts demonstrating free-fall and bouncing rigid bodies (sphere, bunny, cow) with PyVista animations and plots. Removed a macOS .DS_Store file. These changes enable mixed rigid/deformable IPC contact and fast standalone rigid-body integration for validation and demos.
Replace Euler-XYZ incremental rotations with rotation-vector (exponential map) usage across examples and core constraint code. Instances of Rotation.from_euler(..., "XYZ") were changed to Rotation.from_rotvec(...), and RigidTie now provides _dR_drotvec(...) to compute exact dR/dω derivatives (Rodrigues / Gallego & Yezzi) which are chained with the quaternion base. This removes Euler ordering and small-angle approximations, ensuring correct composition with Q_base and accurate linearization for MPC/constraint computations.
Replace the old direct 6x6 Newmark integrator with Fedoo's NonLinear-based workflow and update example scripts accordingly. Key changes:

- RigidBodyAssembly: added Newmark parameters (beta, gamma), Rayleigh alpha, and 6-DOF Newmark state variables; implemented mass matrix helper, effective dynamic stiffness assembly (K_eff), residual computation, update(), set_start(), and to_start() to integrate rigid DOFs into the NonLinear solver flow (including IPC contact updates and damping).
- RigidBody: solve() now wraps/creates a NonLinear problem and calls nlsolve; removed the bespoke 6x6 integrator and related CCD/line-search code. set_rayleigh_damping now syncs alpha into the assembly.
- Examples (sphere, bunny, cow, freefall): migrated to use the NonLinear problem or return problem from body.solve; simplified mesh creation (fd.Mesh.from_pyvista), triangulation of planes, consistent animation output, and added try/except to import Rotation (simcoon or scipy fallback). Various cleanups to prints, timings, and frame handling.
- Constitutivelaw: guard in ElasticAnisotropic to handle uninitialized/zero total_strain (avoid errors on first Newmark step).

Overall this integrates rigid-body Newmark time integration into Fedoo's NonLinear solver, centralizes state management, and updates examples to the new API and improved mesh/animation handling.
Clean up example scripts to avoid hard-coded sys.path and use paths relative to each file for output GIFs. Simplify fedoo.constraint.rigid_body: remove simcoon import fallback, streamline Newmark state handling (use copies for sv_start), eliminate some unused attributes/comments (e.g. _ipc_kappa_auto, _rayleigh_alpha) and tidy IPC/contact and solver-related code paths. Wire rayleigh damping into assembly.rayleigh_alpha. Add unit tests covering rigid body free-fall, IPC kinematics/Jacobian, RigidTie derivative correctness, and quaternion rollback behavior.
Use a safe __file__ fallback (os.getcwd()) when building GIF output paths in examples/rigid_body_bounce_ipc.py, examples/rigid_body_bunny_bounce.py, and examples/rigid_body_cow_bounce.py so they work in environments without __file__. In fedoo/constraint/rigid_body.py, initialize sv['Acceleration'] (and sv_start) by solving the mass matrix when the acceleration vector is all zeros, ensuring the rigid body starts with the correct initial acceleration derived from forces.
Add an early return in RigidBodyAssembly (rigid_body.py) when delta_u has no non-zero entries (using np.any). This avoids unnecessary computation of new accelerations/velocities and prevents potential numerical noise or redundant state updates when there is no displacement change.
Delegate rotation-derivative computation to simcoon.dR_drotvec instead of the local Rodrigues differentiation routine. Add a fallback that sets _simcoon_dR_drotvec=None when simcoon is unavailable and raise a clear ImportError if derivatives are required. Also update pyproject.toml to require simcoon>=1.11.2 to ensure dR_drotvec is present.
Stop padding IPCContact global_matrix/global_vector for extra global DOFs. The scatter matrix P already maps to the full n_dof (mesh DOFs + global DOFs), so the previous padding inflated matrices to n_dof + n_global_dof and caused inconsistent-shape errors during assembly. Also add a regression test (tests/test_ipc_rigid_tie_shape.py) that exercises RigidTie+IPC and PeriodicBC+IPC cases to ensure assembled matrices/vectors have consistent shapes and no ValueError is raised.
Add support for wiring RigidBody instances into a shared IPCContact and improve rigid-body APIs and static-vs-dynamic handling. Key changes:

- New example: examples/contact/ipc/rigid_deformable_punch.py demonstrating IPCContact.add_rigid_body with mixed rigid+deformable mesh.
- Replace per-body enable_ipc_contact with RigidBody.set_static_obstacle for rigid-vs-static use; update example calls accordingly.
- IPCContact.add_rigid_body signature changed to (rigid_body, surface_node_indices=None); body assembly is pointed to the shared mesh and scatter matrix computes exact rigid Jacobian.
- IPCContact: rebuild scatter matrix handling, avoid double-padding global DOFs, clarify CCD/OGC compatibility, and refresh scatter on update/compute paths to keep tangents consistent.
- RigidBody / RigidBodyAssembly: introduce dynamic flag (static mode disables Newmark inertia/damping), handle dt==0 safely, add tiny static regularisation, improve initialization and set_start logic, and make add_to_problem idempotent.
- Mesh: add merge_coincident_nodes helper and set parent_node_indices on extract_elements to expose active node lists for submeshes.
- Problem (Linear, NonLinear): auto-register RigidTie constraints from Assembly.sum so rigid ties are present before assembly initialization; fix NR step alpha handling to correctly apply Dirichlet increments and avoid leftover values.
- Tests: add tests/tests for rigid-ipc coupling and shape-consistency (tests/test_ipc_rigid_tie_shape.py and tests/test_rigid_body_ipc_deformable.py) to guard matrix/vector shapes, Newton's third law, and proper kinematic coupling.

These changes fix shape-mismatch bugs, ensure correct scatter/jacobian behaviour for rigid-slaved nodes, and provide clearer APIs for static vs shared IPC contact scenarios.
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