186 lines
10 KiB
Python
186 lines
10 KiB
Python
"""Approved volume-batched graph prior; no filesystem or label access."""
|
|
import time
|
|
import numpy as np
|
|
from scipy.sparse import coo_matrix, diags
|
|
from scipy.sparse.linalg import splu, spilu, gmres, LinearOperator
|
|
from s3b_graph import SOLVER as TOL, NumericalFailure
|
|
|
|
PARAMETERS = dict(batch_volume_fraction=.005,
|
|
splu=dict(permc_spec='COLAMD', diag_pivot_thresh=1.),
|
|
gmres=dict(restart=50, maxiter=40, rtol=1e-9, atol=0., callback_type='pr_norm'),
|
|
spilu=dict(drop_tol=1e-4, fill_factor=10., drop_rule='basic,area',
|
|
permc_spec='COLAMD', diag_pivot_thresh=1.),
|
|
maxiter_unit='restart cycles; at most2000 inner steps',
|
|
early_resolve='newly full node has positive rate but no unfilled positive-g neighbour')
|
|
|
|
|
|
class SolverFailure(NumericalFailure):
|
|
pass
|
|
|
|
|
|
class RoutingFailure(NumericalFailure):
|
|
pass
|
|
|
|
|
|
def reroute_once(adjacency, rates, newly_full, remaining):
|
|
"""All exhausted nodes handled simultaneously. No occupied volume is sent."""
|
|
ids=np.flatnonzero(newly_full & (rates > 0))
|
|
recipients=[]
|
|
for i in ids:
|
|
a,b=adjacency.indptr[i:i+2];j=adjacency.indices[a:b];g=adjacency.data[a:b]
|
|
keep=(remaining[j]>0)&(g>0)
|
|
if not keep.any():
|
|
# No partial transfers. The caller ends this batch and re-solves.
|
|
return None, dict(early_resolve=True,dead_end_node=int(i),forwarded_nodes=0)
|
|
recipients.append((int(i),j[keep],g[keep]))
|
|
result=rates.copy();result[newly_full]=0.
|
|
for i,j,g in recipients:
|
|
result[j] += rates[i]*g/g.sum()
|
|
return result,dict(early_resolve=False,dead_end_node=None,forwarded_nodes=len(ids))
|
|
|
|
|
|
class PressureEngine:
|
|
def __init__(self,edges,g,n,method,stage=None,record=None):
|
|
if method not in ('splu','gmres_ilu'):raise ValueError('Unfrozen solver')
|
|
if not np.isfinite(g).all() or np.any(g<0):raise ValueError('Invalid conductance')
|
|
keep=g>0;self.scale=float(g.max()) if keep.any() else 1.
|
|
data=g[keep]/self.scale
|
|
if np.any(data<=0):raise SolverFailure('Positive g normalization underflow')
|
|
e=edges[keep]
|
|
self.adj=coo_matrix((np.tile(data,2),(np.r_[e[:,0],e[:,1]],np.r_[e[:,1],e[:,0]])),shape=(n,n)).tocsr()
|
|
self.lap=diags(np.asarray(self.adj.sum(axis=1)).ravel())-self.adj
|
|
self.previous=np.zeros(n);self.method=method;self.stage=stage;self.record=record
|
|
self.calls=0;self.CPU=0.;self.initial=0;self.scheduled=0;self.early=0
|
|
|
|
def notify(self,phase,**extra):
|
|
if self.stage:self.stage(phase,method=self.method,pressure_calls=self.calls,
|
|
completed_pressure_CPU_s=self.CPU,**extra)
|
|
|
|
def has_front(self,wet):
|
|
v=np.asarray(self.adj@wet.astype(float));v[wet]=0.
|
|
return bool((v>0).any())
|
|
|
|
def solve(self,wet,seed,Q,reason):
|
|
started=time.process_time();wall=time.monotonic();self.calls+=1
|
|
if reason=='INITIAL':self.initial+=1
|
|
elif reason=='DEAD_END':self.early+=1
|
|
else:self.scheduled+=1
|
|
self.notify('PRESSURE_MATRIX',wetted_nodes=int(wet.sum()),reason=reason)
|
|
ids=np.flatnonzero(wet);a=self.lap[ids][:,ids].tocsc()
|
|
b=np.zeros(len(ids));b[np.searchsorted(ids,seed)]=Q
|
|
iterations=0;callback_residual=None
|
|
def callback(value):
|
|
nonlocal iterations,callback_residual
|
|
iterations+=1;callback_residual=float(value)
|
|
try:
|
|
self.notify('PRESSURE_FACTORIZE',wetted_nodes=len(ids),matrix_nnz=a.nnz,reason=reason)
|
|
factor_start=time.process_time()
|
|
if self.method=='splu':factor=splu(a,**PARAMETERS['splu'])
|
|
else:factor=spilu(a,**PARAMETERS['spilu'])
|
|
factor_CPU=time.process_time()-factor_start
|
|
self.notify('PRESSURE_SOLVE',wetted_nodes=len(ids),factor_CPU_s=factor_CPU,reason=reason)
|
|
solve_start=time.process_time()
|
|
if self.method=='splu':p=factor.solve(b);info=0
|
|
else:
|
|
preconditioner=LinearOperator(a.shape,matvec=factor.solve,dtype=np.float64)
|
|
p,info=gmres(a,b,x0=self.previous[ids],M=preconditioner,callback=callback,**PARAMETERS['gmres'])
|
|
solve_CPU=time.process_time()-solve_start
|
|
self.notify('PRESSURE_POSTCHECK',wetted_nodes=len(ids),reason=reason)
|
|
residual=float(np.linalg.norm(b-a@p)/np.linalg.norm(b))
|
|
if info!=0 or not np.isfinite(p).all() or not np.isfinite(residual) or residual>TOL['acceptance_residual']:
|
|
raise SolverFailure(f'Pressure returned invalid: info={info}, raw residual={residual}')
|
|
pressure=np.zeros(len(wet));pressure[ids]=p
|
|
rates=np.asarray(self.adj@pressure);rates[wet]=0.
|
|
if not np.isfinite(rates).all() or np.any(rates < -TOL['negative_flux_tolerance']*Q):
|
|
raise SolverFailure('Invalid/negative pressure front flux')
|
|
negative=float(-rates[rates<0].sum());rates[rates<0]=0.
|
|
imbalance=float(abs(rates.sum()-Q)/Q)
|
|
if imbalance>TOL['flux_tolerance']:raise SolverFailure(f'Pressure flux imbalance={imbalance}')
|
|
self.previous=pressure
|
|
cost=time.process_time()-started;self.CPU+=cost
|
|
record=dict(call=self.calls,reason=reason,method=self.method,wetted_nodes=len(ids),matrix_nnz=a.nnz,
|
|
factor_CPU_s=factor_CPU,linear_solve_CPU_s=solve_CPU,total_pressure_CPU_s=cost,
|
|
wall_s=time.monotonic()-wall,iterations=iterations,callback_residual=callback_residual,
|
|
original_residual=residual,flux_imbalance=imbalance,negative_roundoff=negative)
|
|
if self.record:self.record(record)
|
|
return rates
|
|
except SolverFailure:raise
|
|
except Exception as exc:
|
|
raise SolverFailure(f'{self.method} pressure factor/solve: {type(exc).__name__}: {exc}') from exc
|
|
|
|
|
|
def fill_batches(edges,g,mass,seed,method,event_callback=None,state_callback=None,
|
|
stage_callback=None,pressure_callback=None,Q=1.,batch_fraction=None):
|
|
"""Batched pressure, exact capacity subevents, and declared early re-solves."""
|
|
# Optional fraction is for analytical synthetic fixtures only. Production
|
|
# calls omit it and contract pins PARAMETERS; no tuning command-line knob.
|
|
fraction=PARAMETERS['batch_volume_fraction'] if batch_fraction is None else batch_fraction
|
|
if not 0<fraction<=1 or not np.isfinite(mass).all() or np.any(mass<=0) or Q<=0:
|
|
raise ValueError('Invalid capacity/batch domain')
|
|
n=len(mass);M=float(mass.sum());initial=float(mass[seed])
|
|
engine=PressureEngine(edges,g,n,method,stage_callback,pressure_callback)
|
|
remaining=np.array(mass,float);remaining[seed]=0.
|
|
wet=np.zeros(n,bool);wet[seed]=True
|
|
arrival=np.full(n,np.nan);arrival[seed]=0.
|
|
physical_time=0.;rounding=0.;events=0;batches=0;reason='INITIAL'
|
|
def summary(status):
|
|
return dict(status=status,events=events,batches=batches,filled_nodes=int(wet.sum()),
|
|
remaining_volume=float(remaining.sum()),total_volume=M,initial_seed_volume=initial,
|
|
graph_time=physical_time,cumulative_rounding_volume=rounding,
|
|
pressure_calls=engine.calls,initial_solves=engine.initial,scheduled_solves=engine.scheduled,
|
|
early_resolves=engine.early,pressure_CPU_s=engine.CPU,conductance_normalization=engine.scale)
|
|
def emit(status):
|
|
if state_callback:state_callback(summary(status),remaining,arrival)
|
|
emit('INITIALIZED')
|
|
while not wet.all():
|
|
if not engine.has_front(wet):return arrival,remaining,summary('BLOCKED')
|
|
rates=engine.solve(wet,seed,Q,reason);batches+=1
|
|
target=min(fraction*M,float(remaining.sum()));occupied=0.
|
|
while True:
|
|
if stage_callback:stage_callback('SUBEVENT_ROUTING',method=method,pressure_calls=engine.calls,
|
|
completed_pressure_CPU_s=engine.CPU,batch=batches,event=events)
|
|
active=rates>0
|
|
if not active.any() or abs(rates.sum()-Q)>TOL['flux_tolerance']*Q:
|
|
raise RoutingFailure('Routing lost/duplicated total Q')
|
|
left=target-occupied
|
|
if left<=16*np.finfo(float).eps*M:
|
|
reason='SCHEDULED';break
|
|
dt=min(left/Q,float(np.min(remaining[active]/rates[active])))
|
|
if not np.isfinite(dt) or dt<=0 or physical_time+dt<=physical_time:
|
|
raise RoutingFailure('Invalid subevent dt')
|
|
before=float(remaining.sum());new=remaining-dt*rates
|
|
tol=TOL['tie_mass_tolerance']*M
|
|
if np.min(new)<-tol:raise RoutingFailure('Capacity overshoot')
|
|
full=(~wet)&active&(new<=tol)
|
|
correction=float(np.abs(new[full]).sum());new[full]=0.
|
|
consumed=before-float(new.sum())
|
|
if abs(consumed-Q*dt)>TOL['mass_tolerance']*M:
|
|
raise RoutingFailure('Subevent conservation')
|
|
next_time=physical_time+dt
|
|
if rounding+correction>TOL['mass_tolerance']*M or abs(M-new.sum()-initial-Q*next_time)>TOL['mass_tolerance']*M:
|
|
raise RoutingFailure('Cumulative conservation')
|
|
remaining=new;wet[full]=True;physical_time=next_time;arrival[full]=physical_time
|
|
rounding+=correction;occupied+=consumed;events+=1
|
|
record=dict(event=events,batch=batches,newly_wet=int(full.sum()),filled_nodes=int(wet.sum()),
|
|
graph_time=physical_time,delta_time=dt,new_occupied_volume=consumed,batch_occupied_volume=occupied,
|
|
remaining_volume=float(remaining.sum()),rounding_volume=correction,
|
|
rate_sum=float(rates.sum()),front_count=int(active.sum()),early_resolve=False,
|
|
forwarded_nodes=0,pressure_calls=engine.calls)
|
|
if wet.all():
|
|
if event_callback:event_callback(record)
|
|
emit('COMPLETE');return arrival,remaining,summary('COMPLETE')
|
|
boundary=occupied>=target-16*np.finfo(float).eps*M
|
|
if boundary:reason='SCHEDULED'
|
|
elif full.any():
|
|
updated,route=reroute_once(engine.adj,rates,full,remaining)
|
|
record.update(route)
|
|
if route['early_resolve']:reason='DEAD_END';boundary=True
|
|
else:
|
|
rates=updated
|
|
if np.any(rates < -TOL['negative_flux_tolerance']*Q) or abs(rates.sum()-Q)>TOL['flux_tolerance']*Q:
|
|
raise RoutingFailure('Single-reroute conservation')
|
|
if event_callback:event_callback(record)
|
|
emit('RUNNING')
|
|
if boundary:break
|
|
return arrival,remaining,summary('COMPLETE')
|