Files
MeltMind/tools/census_cells.py
T

45 lines
2.8 KiB
Python

"""Census of VTK cell types; preserve mixed topology without conversion."""
import sys,json,time,argparse
from pathlib import Path
from collections import Counter
from zipfile import ZipFile
from xml.etree import ElementTree as ET
import numpy as np
from audit_samples import decode_array
parser=argparse.ArgumentParser();parser.add_argument('archive',type=Path);parser.add_argument('output',type=Path)
a=parser.parse_args();out=a.output
verified=json.loads((out/'archive_verification.json').read_text());st=a.archive.stat()
if verified['status']!='official_size_and_md5_pass' or (st.st_size,st.st_mtime_ns,st.st_ino)!=(verified['bytes'],verified['mtime_ns'],verified['inode']):raise ValueError('Missing or stale verification')
rows=[];start=time.monotonic()
with ZipFile(a.archive) as z:
for n in sorted(n for n in z.namelist() if n.endswith('_sim.vtu')):
cells={};points=None
with z.open(n) as f:
for event,e in ET.iterparse(f,events=('start','end')):
if event=='start' and e.tag=='VTKFile':
if e.get('byte_order')!='LittleEndian' or e.get('compressor')!='vtkZLibDataCompressor':raise ValueError('Unsupported VTU encoding')
header=e.get('header_type','UInt32')
if event=='end' and e.tag=='DataArray':
name=e.get('Name')
if name=='Points':points=decode_array(e,header).copy()
if name in ('connectivity','offsets','types'):cells[name]=decode_array(e,header).copy()
e.clear()
if points is not None and len(cells)==3:break
counts=Counter(map(int,cells['types']));row={'case':n[:-8],'types':dict(counts),'points':len(points),'cells':len(cells['types'])}
if set(counts)!={10}:
offsets=np.r_[0,cells['offsets']];conn=cells['connectivity'];details=[]
for kind in counts:
ids=np.where(cells['types']==kind)[0];widths=offsets[ids+1]-offsets[ids]
used=np.unique(np.concatenate([conn[offsets[i]:offsets[i+1]] for i in ids]))
detail={'type':kind,'cells':len(ids),'nodes':len(used),'width_counts':dict(Counter(map(int,widths))),'node_id_min':int(used.min()),'node_id_max':int(used.max())}
if kind==10:
tet=conn[(offsets[ids,None]+np.arange(4)).ravel()].reshape(-1,4);q=points[tet]
detail['tetra_raw_volume']=float(np.abs(np.einsum('ij,ij->i',np.cross(q[:,1]-q[:,0],q[:,2]-q[:,0]),q[:,3]-q[:,0])/6).sum())
details.append(detail)
row['details']=details
rows.append(row)
if len(rows)%100==0:print(len(rows),flush=True)
result={'scope':'Geometry prefix only; type numbers are VTK identifiers; no mixed mesh converted or stripped','cases':len(rows),'wall_seconds':time.monotonic()-start,'type_totals':dict(sum((Counter(r['types']) for r in rows),Counter())),'mixed_cases':[r for r in rows if set(r['types'])!={10}],'rows':rows}
(out/'cell_type_census.json').write_text(json.dumps(result,indent=2)+'\n')
print(json.dumps({k:v for k,v in result.items() if k!='rows'},indent=2))