AgentFEM-Pipe-Components / source /check_full_component.py
HaomingLuo's picture
Release validated Pipe Assembly Lab v1.0
55acf7a verified
Raw History Blame Contribute Delete
1.82 kB
"""Verify static condensation against unreduced rigid-port FE, with mixed loads."""
from pathlib import Path
import json
import numpy as np
from scipy import sparse
from scipy.sparse.linalg import spsolve
from assembly import load
root=Path(__file__).resolve().parents[1];rows=[]
for name in ['straight-d200-t06-r5','elbow-d120-t04-r1.5','elbow-d240-t06-r3']:
folder=root/'data/components'/name;c=load(folder);K=sparse.load_npz(folder/'stiffness.npz')
ii=c['interior'];bb=c['boundary'];B=sparse.csr_matrix(c['B']);ni=len(ii)
# Mechanical full-order unknowns: interior displacements and 12 rigid-port DOFs.
A=sparse.bmat([[K[ii][:,ii],K[ii][:,bb]@B],[B.T@K[bb][:,ii],B.T@K[bb][:,bb]@B]],format='csr')
temp=100.;g=np.array([0,0,-9.81]);radial=np.zeros(K.shape[0]);radial[bb]=c['wT'][bb]*temp
F=c['ftemp']*temp+c['fG']@g-K@radial;ff=np.r_[F[ii],B.T@F[bb]]
for kind in ['free','spring','guide','fixed']:
fixed=list(range(ni,ni+6));S=c['S'].copy();Ac=A.copy().tolil()
if kind=='spring':Ac[ni+8,ni+8]+=1e5;S[8,8]+=1e5
if kind=='guide':fixed.append(ni+8)
if kind=='fixed':fixed.extend(range(ni+6,ni+12))
free=np.setdiff1d(np.arange(A.shape[0]),fixed);full=np.zeros(A.shape[0]);Ac=Ac.tocsr();full[free]=spsolve(Ac[free][:,free],ff[free]);q=full[-12:]
freeq=np.setdiff1d(np.arange(12),np.array(fixed)-ni);qr=np.zeros(12);fr=c['fT']*temp+c['g']@g;qr[freeq]=np.linalg.solve(S[np.ix_(freeq,freeq)],fr[freeq])
field=np.zeros(K.shape[0]);field[ii]=full[:ni];field[bb]=B@q;field+=radial
recovered=c['C']@qr+c['wT']*temp+c['wG']@g
err=float(np.linalg.norm(field-recovered)/np.linalg.norm(field));assert err<1e-7
rows.append({'component':name,'support':kind,'full_field_relative_error':err})
(root/'reports/static_full_order.json').write_text(json.dumps({'cases':rows,'passed':True},indent=2));print(rows)