Datasets:
Formats:
csv
Languages:
English
Size:
< 1K
Tags:
mathematics
homogenization
spectral-theory
variational-methods
matrix-analysis
scientific-computing
Download research/code/verify_nexus_uniform.py from PureOne/EVE-QELTHA: direct link, hf CLI and curl.
- Browser
- Download file 5.36 kB
-
https://huggingface.co/datasets/PureOne/EVE-QELTHA/resolve/main/research/code/verify_nexus_uniform.py
- Command line
-
hf download hf://datasets/PureOne/EVE-QELTHA/research/code/verify_nexus_uniform.py
-
curl -L -o verify_nexus_uniform.py https://huggingface.co/datasets/PureOne/EVE-QELTHA/resolve/main/research/code/verify_nexus_uniform.py
5.36 kB
| #!/usr/bin/env python3 | |
| """Full Cartesian-FEM audit of arbitrary-strength balanced-port robustness. | |
| The nearby nexus_core.py is an unchanged portable copy of the full Cartesian | |
| pressure FEM. This verifier adds finite-rank penalties or exact constraints to | |
| that discretized energy. It checks balanced strengths from zero to 1e8 and | |
| infinity, and anchored strengths lambda=tau*epsilon. | |
| """ | |
| from pathlib import Path | |
| import argparse | |
| import json | |
| import numpy as np | |
| from nexus_core import _base_problem | |
| def solve_port(base, epsilon, kind, strength): | |
| if kind=='balanced': | |
| row=(base['port'][0]-base['port'][1])[None,:] | |
| elif kind=='anchored': | |
| row=(base['port'][0]+base['port'][1])[None,:] | |
| else:raise ValueError('Unknown port kind') | |
| free=base['free'];L=row[:,free] | |
| z0=base['z0'];W=base['solve'](L.T) | |
| S=L@W;y0=L@z0+row[:,1] | |
| hard=np.isinf(strength) | |
| if hard: | |
| multiplier=np.linalg.solve(S,y0) | |
| else: | |
| multiplier=strength*np.linalg.solve(np.eye(len(row))+strength*S,y0) | |
| z=z0-W@multiplier | |
| # Evaluate the original positive local energy first, then the exact port | |
| # penalty. Hard constraints do not add a finite quadratic port energy. | |
| full,e=base['evaluate'](z,np.zeros_like(row)) | |
| port_value=float((row@full)[0]) | |
| port_energy=0. if hard else float(strength*port_value*port_value) | |
| capacity=e['capacity']+port_energy | |
| residual=base['Hff']@z-base['rhs']+L.T@multiplier | |
| relres=float(np.linalg.norm(residual)/max(np.linalg.norm(base['rhs']),1.)) | |
| return dict(epsilon=epsilon,kind=kind, | |
| strength='infinity' if hard else float(strength), | |
| cells=base['cells'],xmax=float(base['xmax']), | |
| capacity=float(capacity),scaled_capacity=float(capacity/epsilon), | |
| impedance=float(1/capacity),port_value=port_value, | |
| derivative_energy=e['derivative_energy'], | |
| local_leakage_energy=e['local_leakage_energy'], | |
| nonlocal_port_energy=port_energy, | |
| relative_stationarity_residual=relres) | |
| def run(output): | |
| rows=[] | |
| strengths=[0.,1.,1e4,1e8,np.inf] | |
| taus=[0.,.25,1.,4.] | |
| for eps in [1e-4,1e-5,1e-6]: | |
| base=_base_problem(eps,cells=8000) | |
| for strength in strengths: | |
| row=solve_port(base,eps,'balanced',strength) | |
| row['predicted_scaled_capacity']=3. | |
| row['relative_error_to_limit']=abs(row['scaled_capacity']/3.-1) | |
| rows.append(row) | |
| for tau in taus: | |
| row=solve_port(base,eps,'anchored',tau*eps) | |
| row['tau']=tau | |
| row['predicted_scaled_capacity']=3.+4*tau | |
| row['relative_error_to_limit']=abs(row['scaled_capacity']/row['predicted_scaled_capacity']-1) | |
| rows.append(row) | |
| checks=[] | |
| for cells in [4000,8000,16000]: | |
| base=_base_problem(1e-5,cells=cells) | |
| for kind,strength in [('balanced',np.inf),('anchored',1e-5)]: | |
| row=solve_port(base,1e-5,kind,strength) | |
| row['check']='mesh';checks.append(row) | |
| base=_base_problem(1e-5,cells=16000,endpoint=5.) | |
| tail=[] | |
| for kind,strength in [('balanced',np.inf),('anchored',1e-5)]: | |
| row=solve_port(base,1e-5,kind,strength) | |
| row['check']='endpoint';tail.append(row) | |
| for kind in ['balanced','anchored']: | |
| mesh=[r for r in checks if r['kind']==kind] | |
| relmesh=abs(mesh[-1]['capacity']/mesh[-2]['capacity']-1) | |
| reltail=abs(next(r for r in tail if r['kind']==kind)['capacity']/mesh[-1]['capacity']-1) | |
| assert relmesh<3e-4,(kind,'mesh',relmesh) | |
| assert reltail<3e-4,(kind,'endpoint',reltail) | |
| last=[r for r in rows if r['epsilon']==1e-6] | |
| assert max(r['relative_error_to_limit'] for r in last)<2e-4 | |
| hard=[r for r in rows+checks+tail if r['strength']=='infinity'] | |
| assert max(abs(r['port_value']) for r in hard)<1e-10 | |
| assert max(r['relative_stationarity_residual'] for r in rows+checks+tail)<1e-9 | |
| result=dict(status='PASS',method='Full Cartesian pressure FEM with six-point quadrature; low-rank passive updates and exact hard constraints', | |
| model=dict(a=2,b=.8,s=3,A=1,B=1, | |
| f1='indicator [1,2]',f2='indicator [3,4]'), | |
| balanced_prediction='kappa/epsilon -> 3 uniformly over fixed or epsilon-dependent strength in [0,infinity]', | |
| anchored_prediction='for lambda=tau*epsilon, kappa/epsilon -> 3+4*tau', | |
| hard_constraint_max_absolute_error=max(abs(r['port_value']) for r in hard), | |
| rows=rows+checks+tail, | |
| scope='Finite parameter and finite mesh checks illustrate, but do not independently prove, the analytic uniform theorem') | |
| output=Path(output);output.parent.mkdir(parents=True,exist_ok=True) | |
| output.write_text(json.dumps(result,indent=2)+'\n') | |
| print(json.dumps(dict(status=result['status'],runs=len(result['rows']), | |
| last_parameter_rows=last, | |
| hard_constraint_error=result['hard_constraint_max_absolute_error'], | |
| output=str(output)),indent=2)) | |
| return result | |
| if __name__=='__main__': | |
| parser=argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument('--output',default=str(Path(__file__).resolve().parents[1]/'results'/'nexus_uniform.json')) | |
| run(parser.parse_args().output) | |