"""New static Fill_time card; reuse existing four geometry caches, no dynamic audit rerun.""" import argparse import csv from decimal import Decimal import json from pathlib import Path import re import time from xml.etree import ElementTree as ET from zipfile import ZipFile import numpy as np import yaml from audit_samples import decode_array def printed_number(text,label): m=re.search(r'^[ \t]*'+re.escape(label)+r'[ \t]*=[ \t]*([-+0-9.eE]+)',text,re.M) if not m:return None raw=m[1];value=Decimal(raw);half=Decimal(10)**value.as_tuple().exponent/2 return dict(raw=raw,value=float(value),half_last_digit=float(half)) def load_static(z,stem): points=None;cells={};fill=None with z.open(stem+'_sim.vtu') as stream: for event,e in ET.iterparse(stream,events=('start','end')): if event=='start' and e.tag=='VTKFile': if e.get('byte_order')!='LittleEndian' or e.get('compressor')!='vtkZLibDataCompressor':raise ValueError('Unsupported encoding') header=e.get('header_type','UInt32') elif event=='end' and e.tag=='DataArray': name=e.get('Name') if name=='Points':points=decode_array(e,header).copy() elif name in ['connectivity','offsets','types']:cells[name]=decode_array(e,header).copy() elif name=='Fill_time':fill=decode_array(e,header).copy() e.clear() if points is not None and len(cells)==3 and fill is not None:break if points is None or fill is None or len(fill)!=len(points):raise ValueError('Missing static data') types=cells['types'];offset=np.r_[0,cells['offsets']];ids=np.flatnonzero(types==10) if not set(np.unique(types))<={5,10} or not np.all(offset[ids+1]-offset[ids]==4):raise ValueError('Unsupported topology') rawtet=cells['connectivity'][offset[ids,None]+np.arange(4)] selected=np.unique(rawtet);tet=np.searchsorted(selected,rawtet) return points[selected],tet,fill[selected],selected def main(): p=argparse.ArgumentParser();p.add_argument('archive',type=Path);p.add_argument('intake',type=Path);p.add_argument('output',type=Path);a=p.parse_args() target=a.output/'static_data_card.csv' if target.exists():raise FileExistsError(target) ver=json.loads((a.intake/'archive_verification.json').read_text());stat=a.archive.stat() if (stat.st_size,stat.st_mtime_ns,stat.st_ino)!=(ver['bytes'],ver['mtime_ns'],ver['inode']):raise ValueError('Changed archive') census=json.loads((a.intake/'cell_type_census.json').read_text());rows=[];start=time.monotonic() with ZipFile(a.archive) as z: for entry in census['rows']: tick=time.monotonic();stem=entry['case'];flags=[];cache=a.intake/'samples'/(stem+'.npz') if cache.exists(): d=np.load(cache);x=d['points_raw'];tet=d['tetra'];fill=d['fill_time_raw'];raw_ids=np.arange(len(x));reused=True else:x,tet,fill,raw_ids=load_static(z,stem);reused=False finite=np.isfinite(fill) if not finite.all():flags.append('nonfinite_tet_fill') if (fill[finite]<0).any():flags.append('negative_tet_fill') q=x[tet];vol=np.abs(np.linalg.det(q[:,1:]-q[:,:1]))/6;total=float(vol.sum()) if (vol==0).any():flags.append('zero_volume_tets') gate=yaml.safe_load(z.read(stem+'.yml'));distance=np.linalg.norm(x-np.array(gate['inlet_location']),axis=1);near=int(distance.argmin()) log=z.read(stem+'.log').decode().replace('\r','');lv=printed_number(log,'Volume of tetrahedral elements');lt=printed_number(log,'Current time from start of cycle') maximum=float(fill[finite].max()) if finite.any() else None if not lv:flags.append('missing_log_volume') if not lt:flags.append('missing_log_fill_end_time') # 1e-9 is an explicit numerical mm^3 -> m^3 hypothesis, not a sourced field unit. volume_consistent=abs(total*1e-9-lv['value'])<=lv['half_last_digit']+1e-12*max(total*1e-9,abs(lv['value'])) if lv else None time_consistent=abs(maximum-lt['value'])<=lt['half_last_digit']+5e-5*max(maximum,1e-12) if lt and maximum is not None else None if volume_consistent is False:flags.append('volume_outside_log_print_interval_mm_hypothesis') if time_consistent is False:flags.append('fill_end_mismatch_beyond_print_and_export_tolerance') if distance[near]>1e-6*max(float(np.linalg.norm(np.ptp(x,axis=0))),1e-12):flags.append('gate_not_near_node') if finite[near] and fill[near]>1e-6*max(maximum,1e-12):flags.append('gate_fill_not_near_zero') mixed='5' in entry['types'] if mixed:flags.append('mixed_topology') row=dict(case=stem,source_key=stem.split('_')[1],tet_nodes=len(x),tet_cells=len(tet),mixed_topology=mixed,nonfinite_fill=int((~finite).sum()),negative_fill=int((fill[finite]<0).sum()),zero_fill=int((fill==0).sum()),max_fill_time_raw=maximum,log_end_fill_time=lt['value'] if lt else None,log_end_fill_token=lt['raw'] if lt else None,time_consistent=time_consistent,total_tet_volume_raw=total,log_tet_volume=lv['value'] if lv else None,log_volume_token=lv['raw'] if lv else None,volume_consistent_mm_hypothesis=volume_consistent,gate_raw_node_id=int(raw_ids[near]),gate_distance_raw=float(distance[near]),gate_fill_time_raw=float(fill[near]) if finite[near] else None,reused_cache=reused,wall_seconds=time.monotonic()-tick,flags=';'.join(flags)) rows.append(row) if len(rows)%100==0:print(f'Static data card {len(rows)}/624',flush=True) with target.open('w',newline='') as f:w=csv.DictWriter(f,fieldnames=list(rows[0]));w.writeheader();w.writerows(rows) counts={flag:sum(flag in r['flags'].split(';') for r in rows) for flag in sorted({f for r in rows for f in r['flags'].split(';') if f})} summary=dict(cases=len(rows),reused_cached_cases=sum(r['reused_cache'] for r in rows),wall_seconds=time.monotonic()-start,flag_counts=counts,fill_all_finite_nonnegative=all(r['nonfinite_fill']==r['negative_fill']==0 for r in rows),source_sha256=ver['sha256'],scope='Static only. Read through Fill_time then stop; no full per-member CRC or dynamic-field rescan. Explicit raw tet node IDs used internally; raw archive intact. Units remain numerical hypotheses. Time consistency tolerance is LOG half-last-digit plus 5e-5 of exported maximum, declared in code before execution.') (a.output/'static_data_card_summary.json').write_text(json.dumps(summary,indent=2)+'\n');print(json.dumps(summary,indent=2)) if __name__=='__main__':main()