Datasets:
Tasks:
Other
Formats:
json
Size:
10K - 100K
Tags:
agentfem
soft-robotics
programmable-matter
structural-mechanics
inverse-design
scientific-machine-learning
License:
Download multizone-v02/source/rod.py from HaomingLuo/AgentFEM-Programmable-Soft-Rods: direct link, hf CLI and curl.
- Browser
- Download file 4.58 kB
-
https://huggingface.co/datasets/HaomingLuo/AgentFEM-Programmable-Soft-Rods/resolve/main/multizone-v02/source/rod.py
- Command line
-
hf download hf://datasets/HaomingLuo/AgentFEM-Programmable-Soft-Rods/multizone-v02/source/rod.py
-
curl -L -o rod.py https://huggingface.co/datasets/HaomingLuo/AgentFEM-Programmable-Soft-Rods/resolve/main/multizone-v02/source/rod.py
4.58 kB
| """Conservative planar extensible rod. L=EA=1; chi=EI/(EA L^2). | |
| Rod: linear angle FE, midpoint axial elimination. Chain: Ye et al. (2026) | |
| cell rotations, first cell clamped; kr=chi*n. Independent implementations | |
| share ONLY the reduced local energy density. No contact or dynamics. | |
| SPDX-License-Identifier: Apache-2.0 | |
| """ | |
| from dataclasses import dataclass, asdict | |
| import numpy as np | |
| from scipy.linalg import solve, eigh | |
| from scipy.optimize import root | |
| class Controls: | |
| chi: float = .001 | |
| fx: float = 0. | |
| fy: float = 0. | |
| moment: float = 0. | |
| h: float = 0. | |
| delta: float = .1 | |
| k1: float = 0. | |
| k2: float = 0. | |
| k3: float = 0. | |
| curvatures: tuple | None = None | |
| class Rod: | |
| def __init__(self, n=48, kind="rod"): | |
| self.n, self.kind, self.ds = n, kind, 1/n | |
| if kind == "rod": | |
| self.d = n | |
| A = np.zeros((n,n+1)); B = A.copy() | |
| for i in range(n): | |
| A[i,i:i+2] = .5 | |
| B[i,i:i+2] = [-n,n] | |
| self.A,self.B = A[:,1:],B[:,1:] | |
| self.mass = (A[:,1:].T@A[:,1:])/n | |
| elif kind == "chain": | |
| self.d = n-1 | |
| self.A = np.eye(n)[:,1:] | |
| self.B = np.diff(np.eye(n),axis=0)[:,1:]*n | |
| self.mass = np.eye(n-1)/n | |
| else: raise ValueError(kind) | |
| self.s = (np.arange(n)+.5)/n | |
| def terms(self, z, c): | |
| angle=self.A@z | |
| ks=np.asarray(c.curvatures if c.curvatures is not None else [c.k1,c.k2,c.k3]) | |
| intrinsic=ks[np.minimum((np.arange(self.B.shape[0])+.5)*len(ks)/self.B.shape[0],len(ks)-1).astype(int)] | |
| curvature=self.B@z-intrinsic | |
| a=c.fx+2*c.h | |
| f=a*np.cos(angle)+c.fy*np.sin(angle) | |
| fp=-a*np.sin(angle)+c.fy*np.cos(angle) | |
| g=4*c.h*c.delta | |
| density=-f-.5*f*f+g*np.cos(angle) | |
| energy=self.ds*(.5*c.chi*(curvature@curvature)+density.sum())-c.moment*z[-1] | |
| local_g=-(1+f)*fp-g*np.sin(angle) | |
| local_h=(1+f)*f-fp*fp-g*np.cos(angle) | |
| grad=self.ds*(c.chi*self.B.T@curvature+self.A.T@local_g) | |
| grad[-1]-=c.moment | |
| tangent=self.ds*(c.chi*self.B.T@self.B+self.A.T@(local_h[:,None]*self.A)) | |
| return energy,grad,tangent | |
| def solve(self,c,seed=None): | |
| z=np.zeros(self.d) if seed is None else np.array(seed,dtype=float).copy() | |
| it=0; trace=[]; fallback_nfev=0 | |
| for it in range(60): | |
| _,r,K=self.terms(z,c) | |
| trace.append(dict(iteration=it,residual_inf=float(np.max(np.abs(r))))) | |
| if np.max(np.abs(r))<1e-10: break | |
| try: dz=solve(K,-r,assume_a='sym') | |
| except Exception: break | |
| old=np.linalg.norm(r) | |
| step=1. | |
| for _ in range(16): | |
| trial=z+step*dz | |
| if np.linalg.norm(self.terms(trial,c)[1])<old: break | |
| step*=.5 | |
| else: break | |
| z=trial | |
| trace[-1]['accepted_step_scale']=step | |
| residual=float(np.max(np.abs(self.terms(z,c)[1]))) | |
| if residual>1e-9: | |
| sol=root(lambda v:self.terms(v,c)[1],z,jac=lambda v:self.terms(v,c)[2],tol=1e-10) | |
| z=sol.x; fallback_nfev=sol.nfev | |
| energy,r,K=self.terms(z,c) | |
| residual=float(np.max(np.abs(r))) | |
| angle=self.A@z | |
| stretch=1+(c.fx+2*c.h)*np.cos(angle)+c.fy*np.sin(angle) | |
| admissible=bool(np.min(stretch)>(2*c.delta if c.h!=0 else 0)) | |
| good=bool(residual<1e-9 and admissible and np.all(np.isfinite(z))) | |
| eig=float(eigh(K,self.mass,subset_by_index=[0,0],eigvals_only=True)[0]/c.chi) if good else None | |
| stability='unknown' if eig is None else ('stable' if eig>1e-4 else 'unstable' if eig< -1e-4 else 'critical') | |
| segments=self.ds*stretch[:,None]*np.column_stack([np.cos(angle),np.sin(angle)]) | |
| xy=np.vstack([np.zeros(2),np.cumsum(segments,axis=0)]) | |
| # Output resampled on reference coordinate, not current arc length. | |
| center=np.column_stack([np.interp(np.linspace(0,1,65),np.linspace(0,1,self.n+1),xy[:,j]) for j in range(2)]) | |
| return dict(z=z,centerline=center,angle=angle,strain=stretch-1,energy=float(energy),residual=residual, | |
| iterations=it+1,accepted=good,lambda_min_over_chi=eig,stability=stability, | |
| tip=center[-1],min_stretch=float(stretch.min()),solver_trace=trace,fallback_nfev=fallback_nfev) | |
| def mode_seed(self,amplitude): | |
| s=np.arange(1,self.d+1)/self.n | |
| return amplitude*np.sin(np.pi*s/2) | |
| def serial(result): | |
| return {k:(v.tolist() if isinstance(v,np.ndarray) else v) for k,v in result.items()} | |