96 lines
7.0 KiB
Python
96 lines
7.0 KiB
Python
"""One panel part: existing geometry -> validated wall distance -> counts only."""
|
|
import argparse
|
|
import hashlib
|
|
import json
|
|
from pathlib import Path
|
|
import resource
|
|
import time
|
|
from xml.etree import ElementTree as ET
|
|
from zipfile import ZipFile
|
|
import numpy as np
|
|
import yaml
|
|
from scipy.spatial import cKDTree
|
|
from audit_samples import decode_array,tetra_geometry
|
|
from boundary_distance import SurfaceDistance,triangle_distance_squared
|
|
from run_e6_real_kernel import samples
|
|
|
|
|
|
def sha(p):return hashlib.sha256(Path(p).read_bytes()).hexdigest()
|
|
def save(p,x):Path(p).write_text(json.dumps(x,indent=2,allow_nan=False)+'\n')
|
|
|
|
|
|
def load_geometry(archive,case):
|
|
x=None;cells={}
|
|
with archive.open(case+'_sim.vtu') as stream:
|
|
for event,e in ET.iterparse(stream,events=('start','end')):
|
|
if event=='start' and e.tag=='VTKFile':
|
|
assert e.get('byte_order')=='LittleEndian' and e.get('compressor')=='vtkZLibDataCompressor'
|
|
header=e.get('header_type','UInt32')
|
|
elif event=='end' and e.tag=='DataArray':
|
|
name=e.get('Name')
|
|
if name=='Points':x=decode_array(e,header).copy()
|
|
elif name in ['connectivity','offsets','types']:cells[name]=decode_array(e,header).copy()
|
|
e.clear()
|
|
if x is not None and len(cells)==3:break
|
|
assert x is not None and np.isfinite(x).all()
|
|
offsets=np.r_[0,cells['offsets']];tet_ids=np.flatnonzero(cells['types']==10)
|
|
assert np.all(offsets[tet_ids+1]-offsets[tet_ids]==4)
|
|
rawtet=cells['connectivity'][offsets[tet_ids,None]+np.arange(4)]
|
|
ids=np.unique(rawtet);tet=np.searchsorted(ids,rawtet);x=x[ids]
|
|
report,data=tetra_geometry(x,dict(types=np.full(len(tet),10),offsets=np.arange(1,len(tet)+1)*4,connectivity=tet.ravel()))
|
|
assert report['zero_volume_cells']==0 and report['nonmanifold_faces']==0
|
|
gate=yaml.safe_load(archive.read(case+'.yml'))
|
|
return x,data,ids,gate,report
|
|
|
|
|
|
def main():
|
|
p=argparse.ArgumentParser();p.add_argument('output',type=Path);p.add_argument('index',type=int);args=p.parse_args();out=args.output
|
|
c=json.loads((out/'count_contract.json').read_text());spec=c['cases'][args.index];case=spec['case'];dest=out/case
|
|
dest.mkdir(exist_ok=False);start=time.monotonic();save(dest/'started.json',dict(contract_sha256=sha(out/'count_contract.json'),index=args.index))
|
|
for path,h in c['code_sha256'].items():assert sha(path)==h,path
|
|
for path,h in c['metadata_sha256'].items():assert sha(path)==h,path
|
|
ver=json.loads(Path(c['archive_verification']).read_text());st=Path(c['archive']).stat()
|
|
assert (st.st_size,st.st_mtime_ns,st.st_ino)==(ver['bytes'],ver['mtime_ns'],ver['inode'])
|
|
with ZipFile(c['archive']) as z:
|
|
x,data,raw_ids,gate,geometry=load_geometry(z,case)
|
|
source={n:dict(crc32=f'{z.getinfo(n).CRC:08x}',bytes=z.getinfo(n).file_size) for n in [case+'_sim.vtu',case+'.yml']}
|
|
tet=data['tetra'];mass=data['node_volume_raw'];faces=data['boundary_triangles'];loaded=time.monotonic()
|
|
assert len(x)==spec['tet_nodes'] and len(tet)==spec['tet_cells']
|
|
np.savez_compressed(dest/'geometry.npz',points_raw=x,raw_node_ids=raw_ids,gate_location_raw=np.array(gate['inlet_location']),**data)
|
|
save(dest/'geometry.json',dict(geometry=geometry,gate_source=gate,source_members=source,archive_sha256=ver['sha256'],label_arrays_decoded=[]))
|
|
boundary=np.zeros(len(x),bool);boundary[np.unique(faces)]=True;interior=np.flatnonzero(~boundary);d=np.zeros(len(x));surface=SurfaceDistance(x[faces])
|
|
for begin in range(0,len(interior),128):
|
|
ids=interior[begin:begin+128];d[ids]=surface.query(x[ids])
|
|
save(dest/'progress.json',dict(stage='boundary_distance',done=min(begin+128,len(interior)),total=len(interior),elapsed_seconds=time.monotonic()-start))
|
|
assert np.isfinite(d).all() and np.all(d>=0)
|
|
# Same bounded validation principle as the existing distance command.
|
|
ids=np.random.default_rng(1729).choice(len(x),min(30,len(x)),replace=False)
|
|
brute=np.array([np.sqrt(triangle_distance_squared(q,x[faces]).min()) for q in x[ids]])
|
|
error=float(abs(brute-d[ids]).max());assert error<=1e-10*max(1.,np.ptp(x,axis=0).max())
|
|
np.save(dest/'boundary_distance_raw.npy',d);distance_done=time.monotonic();del surface
|
|
order=np.argsort(d,kind='stable');median=float(d[order[np.searchsorted(np.cumsum(mass[order]),mass.sum()/2)]])
|
|
if not np.isfinite(median) or median<=0:raise ValueError('Nonpositive full-node weighted median; no substitute R')
|
|
radius=10*median;sx,sm,_=samples(x,tet,mass,'tetra_four_point');sources_done=time.monotonic();tree=cKDTree(sx);tree_done=time.monotonic()
|
|
counts=np.empty(len(x),np.int64)
|
|
for begin in range(0,len(x),128):
|
|
stop=min(begin+128,len(x));counts[begin:stop]=tree.query_ball_point(x[begin:stop],radius,return_length=True,workers=1)
|
|
save(dest/'progress.json',dict(stage='neighbor_count',done=stop,total=len(x),neighbor_visits=int(counts[:stop].sum()),elapsed_seconds=time.monotonic()-start))
|
|
counted=time.monotonic();np.savez_compressed(dest/'counts.npz',neighbor_counts=counts,boundary_mask=boundary,node_lumped_mass=mass)
|
|
groups={}
|
|
for name,mask in [('all',np.ones(len(x),bool)),('positive_distance',d>0),('boundary',boundary)]:
|
|
v=counts[mask];groups[name]=dict(nodes=int(mask.sum()),neighbor_visits=int(v.sum()),mass_fraction=float(mass[mask].sum()/mass.sum()),quantiles=dict(zip(['min','p50','p90','p99','max'],np.quantile(v,[0,.5,.9,.99,1]).tolist())) if len(v) else None)
|
|
peak=resource.getrusage(resource.RUSAGE_SELF).ru_maxrss*1024;S=len(sx);N=len(x);max_count=int(counts.max())
|
|
result=dict(case=case,panel_index=args.index,status='COUNT_COMPLETE',nodes=N,tets=len(tet),four_point_sources=S,weighted_median_distance=median,t_part=4*median,radius=radius,
|
|
boundary_nodes=int(boundary.sum()),distance_brute_queries=len(ids),distance_brute_max_abs_error=error,groups=groups,
|
|
neighbor_visit_alarm_exceeded=int(counts.sum())>500000000,visit_cap=None,
|
|
timings=dict(load_geometry_and_topology_seconds=loaded-start,write_geometry_and_distance_seconds=distance_done-loaded,source_positions_seconds=sources_done-distance_done,tree_seconds=tree_done-sources_done,count_seconds=counted-tree_done,write_result_seconds=time.monotonic()-counted),
|
|
wall_seconds=time.monotonic()-start,peak_rss_bytes=peak,
|
|
future_kernel_memory_planning_bytes=peak+72*S+72*N+120*N+256*max_count+128*1024**2,
|
|
memory_model='Planning allowance only, not measured kernel RSS or guaranteed bound: count peak + source tensors72S + raw72N + E6 output120N + scratch256*max_neighbors +128MiB. O2 node/edge readouts not included.',
|
|
new_distance_queries=int(len(interior)),new_kernel_evaluations=0,S1_scoring=False,training=False,
|
|
output_sha256={n:sha(dest/n) for n in ['geometry.npz','geometry.json','boundary_distance_raw.npy','counts.npz']})
|
|
save(dest/'result.json',result);save(dest/'progress.json',dict(stage='complete',elapsed_seconds=time.monotonic()-start));print('COMPLETE',case,result['wall_seconds'],flush=True)
|
|
|
|
|
|
if __name__=='__main__':main()
|