Files
MeltMind/tools/phase2_b10_validity.py
T

97 lines
7.5 KiB
Python

"""Twenty learning-side v5 parts: Freeze_time/shrinkage validity, no model."""
import argparse
import fcntl
import hashlib
import json
import os
from pathlib import Path
import re
import sys
import time
import traceback
from xml.etree import ElementTree as ET
from zipfile import ZipFile
import numpy as np
from audit_samples import decode_array,stats,TIME
from phase2_h1_worker_v2 import checked,sha,save
from r4bc_guard import supervise
BASE=Path('/home/frank14f/optane/MeltMind')
def read(p):return json.loads(Path(p).read_text())
def family(name):
return 'Freeze_time' if name=='Freeze_time' else 'Volumetric_shrinkage_(3D)' if name.startswith('Volumetric_shrinkage_(3D)_Block:') else None
def masks(array,raw,total):
if array.shape!=(total,):raise ValueError('Expected scalar on all raw PointData nodes')
keep=np.zeros(total,bool);keep[raw]=True;finite=np.isfinite(array)
return dict(all_raw=stats(array),retained_tet=stats(array[keep]),outside_tet=stats(array[~keep]),finite_retained_nodes=int(np.sum(finite&keep)),retained_nodes=len(raw),finite_outside_nodes=int(np.sum(finite&~keep)),outside_nodes=int(np.sum(~keep)),
finite_mask_sha256=hashlib.sha256(np.packbits(finite,bitorder='little').tobytes()).hexdigest(),zero_is_not_missing=True,finite_is_not_physical_validity=True)
def config(root):
c=read(root/'contract.json')
if c['approval']!='1794900702.867059' or len(c['parts'])!=20:raise PermissionError('No approved twenty-part read-only gate')
for p,h in c['source_sha256'].items():checked(BASE/p,h)
st=Path(c['archive']['path']).stat();s=c['archive']['stat']
if (st.st_size,st.st_mtime_ns,st.st_ino)!=(s['bytes'],s['mtime_ns'],s['inode']):raise ValueError('Archive identity differs')
return c
def worker(root):
c=config(root);os.sched_setaffinity(0,[0]);start=time.process_time();records=[]
with ZipFile(c['archive']['path']) as z:
for row in c['parts']:
case=row['case'];out=root/'parts'/case;out.mkdir(parents=True,exist_ok=False);partstart=time.process_time();fields=[]
raw=np.load(checked(Path(row['output'])/'raw_ids.npy',row['output_sha256']['raw_ids.npy']),allow_pickle=False)
oldlog=BASE/'artifacts/moldflow-time-map-2026-11-15/logs'/(case+'_sim.json')
logmember=case+'_sim.log';log=z.read(logmember);(out/'source.log').write_bytes(log)
lines=log.decode('utf-8').splitlines();evidence=[dict(line=i+1,text=s) for i,s in enumerate(lines) if re.search(r'Freeze|shrink|End of filling|Pack Analysis|Filling phase|Pack.*phase|Time.*\||\(s\)|[Uu]nit|[Tt]ransition temperature',s)]
logdata=read(oldlog) if oldlog.exists() else None
if logdata and hashlib.sha256(log).hexdigest()!=logdata['raw_sha256']:raise ValueError('Previously frozen LOG identity differs')
filling_end=(logdata or {}).get('diagnostic',{}).get('last_row',{}).get('time_s')
member=case+'_sim.vtu';stack=[];total=None;expected=[]
with z.open(member) as stream:
for event,element in ET.iterparse(stream,events=('start','end')):
if event=='start':
stack.append(element.tag)
if element.tag=='VTKFile':
if element.get('byte_order')!='LittleEndian' or element.get('compressor')!='vtkZLibDataCompressor':raise ValueError('Unsupported VTU encoding')
header=element.get('header_type','UInt32')
if element.tag=='Piece':total=int(element.attrib['NumberOfPoints'])
else:
if element.tag=='DataArray':
name=element.get('Name','');f=family(name)
if f:
if stack[-2]!='PointData' or total is None:raise ValueError('Field association differs')
arr=decode_array(element,header);index=len(fields);attrs=dict(element.attrib);p=out/f'field-{index:02d}.npz'
np.savez_compressed(p,values=arr,finite=np.isfinite(arr),retained_raw_ids=raw)
timing=TIME.match(name);tm=float(timing[3]) if timing else None
units={k:v for k,v in attrs.items() if 'unit' in k.lower()}
fields.append(dict(name=name,family=f,association='PointData',attributes=attrs,values_sha256=sha(p),masks=masks(arr,raw,total),
named_time_seconds=tm,time_suffix_is_value_unit=False,field_unit_attributes=units,
value_unit_status='EXPLICIT_ATTRIBUTE' if units else 'UNRESOLVED_NO_FIELD_UNIT_ATTRIBUTE',
relation_to_printed_fill_end=None if tm is None or filling_end is None else 'after_printed_filling_end' if tm>filling_end else 'at_or_before_printed_filling_end',
physical_definition_status='UNRESOLVED_FREEZING_CRITERION_AND_TIME_ORIGIN' if f=='Freeze_time' else 'UNRESOLVED_SHRINKAGE_REFERENCE_STATE_AND_UNIT',
numerically_complete_on_tets=bool(np.isfinite(arr[raw]).all()),learning_target_approved=False))
element.clear()
stack.pop()
if sum(x['family']=='Freeze_time' for x in fields)!=1 or not any(x['family'].startswith('Volumetric') for x in fields):raise ValueError('Expected fields missing')
result=dict(status='VALIDITY_AUDIT_COMPLETE',case=case,original_learning_role='grouped_fold0_100pct',raw_nodes=total,retained_tet_nodes=len(raw),fields=fields,
printed_filling_end_seconds=filling_end,log_evidence=evidence,log_sha256=sha(out/'source.log'),source_member_crc=z.getinfo(member).CRC,
units_and_physical_stage_not_inferred_from_values=True,models_fit=0,cpu_seconds=time.process_time()-partstart)
save(out/'result.json',result);records.append(dict(case=case,fields=len(fields),result_sha256=sha(out/'result.json')));save(root/'progress.json',dict(parts=len(records),cpu_seconds=time.process_time()-start))
save(root/'result.json',dict(status='COMPLETE',decision='PARK_PHYSICAL_DEFINITION_AND_UNITS',parts=len(records),fields=sum(x['fields'] for x in records),records=records,cpu_seconds=time.process_time()-start,models_fit=0,
limitation='Decoded values and finite masks do not establish freezing criterion, time origin, shrinkage reference state or field units; no imputation and no learning target enabled.'))
def run(root):
c=config(root);os.sched_setaffinity(0,[1]);start=time.process_time()
with (root/'run.lock').open('a') as lock:
fcntl.flock(lock,fcntl.LOCK_EX|fcntl.LOCK_NB)
if (root/'started.json').exists():raise FileExistsError('Single B10 attempt')
save(root/'started.json',dict(pid=os.getpid(),unix=time.time(),contract_sha256=sha(root/'contract.json')))
g=supervise([sys.executable,'tools/phase2_b10_validity.py','worker',str(root)],root/'guard',dict(cpu_seconds=7100,wall_seconds=7200,rss_bytes=2*2**30,disk_bytes=2*2**30))
save(root/'complete.json',dict(status='COMPLETE' if g['status']=='PASS' else 'STOPPED',guard=g,cpu_seconds=60+g['cpu_seconds']+time.process_time()-start,cpu_cap=7200,decision=read(root/'result.json')['decision'] if g['status']=='PASS' else 'INCOMPLETE',models_fit=0))
if __name__=='__main__':
p=argparse.ArgumentParser();p.add_argument('mode',choices=['run','worker']);p.add_argument('root',type=Path);a=p.parse_args();(run if a.mode=='run' else worker)(a.root)