Files
MeltMind/tools/test_s3b_v04.py
T

148 lines
8.9 KiB
Python

"""Analytical batch mechanics and authorization/failure boundary tests."""
import unittest
from unittest.mock import patch
from pathlib import Path
import tempfile
import numpy as np
from scipy.sparse import csr_matrix
from s3b_v04_math import fill_batches, reroute_once, SolverFailure, PressureEngine
from s3b_v04_runtime import reserve_job, failure_classification, may_switch
# Retain geometry, strict-region and actual subprocess I/O barrier checks.
from test_s3b_graph import GraphTests
from s2_recovery_supervisor import save
from s3b_v04_runtime import read, OLD_CPU
from run_s3b_v04_ten import run as run_ten
class BatchTests(unittest.TestCase):
def test_chain_both_solvers(self):
for method in ['splu','gmres_ilu']:
a,r,s=fill_batches(np.array([[0,1],[1,2]]),np.array([2.,3.]),np.array([2.,3.,5.]),0,method)
np.testing.assert_allclose(a,[0,3,8],atol=2e-12)
self.assertEqual(s['status'],'COMPLETE');self.assertEqual(r.sum(),0)
self.assertEqual(s['initial_solves'],1)
self.assertEqual(s['pressure_calls'],s['initial_solves']+s['scheduled_solves']+s['early_resolves'])
def test_dead_end_keeps_half_filled_time_and_volume(self):
# A has capacity .01 and dies at t=.02 while B is only .01 filled.
# New pressure sends Q=1 to B; its capacity/time are not reset.
for method in ['splu','gmres_ilu']:
rows=[];states=[];pressures=[]
a,r,s=fill_batches(np.array([[0,1],[0,2]]),np.ones(2),np.array([1.,.01,10.]),0,method,
event_callback=rows.append,pressure_callback=pressures.append,
state_callback=lambda row,r,a:states.append((dict(row),r.copy())))
np.testing.assert_allclose(a,[0,.02,10.01],atol=1e-10)
self.assertEqual(s['early_resolves'],1)
self.assertEqual(pressures[1]['reason'],'DEAD_END')
self.assertTrue(rows[0]['early_resolve'])
self.assertAlmostEqual(rows[0]['new_occupied_volume'],.02)
self.assertAlmostEqual(states[1][1][2],9.99)
self.assertGreater(rows[1]['graph_time'],rows[0]['graph_time'])
self.assertAlmostEqual(rows[1]['batch_occupied_volume'],.005*11.01)
self.assertAlmostEqual(sum(x['new_occupied_volume'] for x in rows),10.01)
def test_single_reroute_simultaneous_sources_and_existing_rate(self):
adj=csr_matrix(np.array([[0,7,1,2],[7,0,3,0],[1,3,0,0],[2,0,0,0]],float))
original=np.array([.3,.4,.1,.2]);full=np.array([True,True,False,False])
new,info=reroute_once(adj,original,full,np.array([0.,0.,3.,4.]))
np.testing.assert_allclose(new,[0,0,.6,.4]);np.testing.assert_allclose(original,[.3,.4,.1,.2])
self.assertEqual(info['forwarded_nodes'],2);self.assertFalse(info['early_resolve'])
def test_dead_end_prevents_partial_transfer(self):
adj=csr_matrix(np.array([[0,0,1],[0,0,0],[1,0,0]],float))
rates=np.array([.4,.6,0.]);full=np.array([True,True,False])
new,info=reroute_once(adj,rates,full,np.array([0.,0.,2.]))
self.assertIsNone(new);self.assertTrue(info['early_resolve'])
np.testing.assert_array_equal(rates,[.4,.6,0.])
def test_diamond_ties_complete_without_final_solve(self):
for method in ['splu','gmres_ilu']:
a,r,s=fill_batches(np.array([[0,1],[0,2],[1,3],[2,3]]),np.ones(4),np.ones(4),0,method,batch_fraction=1.)
np.testing.assert_allclose(a,[0,2,2,3]);self.assertEqual(s['pressure_calls'],1)
self.assertEqual(s['events'],2);self.assertEqual(s['status'],'COMPLETE')
def test_Q_g_scaling_and_permutation(self):
e=np.array([[0,1],[0,2],[1,3],[2,3]]);g=np.array([1.,2.,3.,4.]);m=np.ones(4)
a,_,_=fill_batches(e,g,m,0,'splu')
b,_,_=fill_batches(e,g*13,m,0,'splu',Q=7)
np.testing.assert_allclose(a,b*7,atol=2e-11)
perm=np.array([2,3,1,0]);mm=np.empty_like(m);mm[perm]=m
c,_,_=fill_batches(perm[e[::-1]],g[::-1],mm,int(perm[0]),'splu')
np.testing.assert_allclose(a,c[perm],atol=2e-11)
def test_zero_conductance_capacity_is_retained(self):
for method in ['splu','gmres_ilu']:
a,r,s=fill_batches(np.array([[0,1],[1,2]]),np.array([1.,0.]),np.array([1.,2.,4.]),0,method)
self.assertEqual(s['status'],'BLOCKED');self.assertEqual(s['remaining_volume'],4)
self.assertTrue(np.isnan(a[2]));self.assertEqual(r[2],4)
def test_postcheck_rejects_bad_solver_exit_success(self):
class BadFactor:
def solve(self,b):return np.zeros_like(b)
with patch('s3b_v04_math.splu',return_value=BadFactor()):
with self.assertRaisesRegex(SolverFailure,'raw residual'):
fill_batches(np.array([[0,1]]),np.ones(1),np.ones(2),0,'splu')
with patch('s3b_v04_math.gmres',return_value=(np.zeros(1),0)):
with self.assertRaises(SolverFailure):
fill_batches(np.array([[0,1]]),np.ones(1),np.ones(2),0,'gmres_ilu')
def test_shared_budget_preserves_postfit(self):
self.assertTrue(reserve_job(61560.037823-1800))
self.assertFalse(reserve_job(61560.037823-1799.99))
def test_solver_failure_keeps_reason_and_reference_never_switches(self):
guard=dict(status='STOPPED_OR_FAILED',monitor_errors=0,stop_reason=None)
c=failure_classification(guard,dict(type='SolverFailure',message='raw residual=0.1'))
self.assertEqual(c['category'],'NUMERICAL_FAILURE');self.assertIn('residual',c['reason'])
self.assertTrue(may_switch('splu',c));self.assertFalse(may_switch('gmres_ilu',c))
self.assertFalse(may_switch('splu',c,gate='reference'))
self.assertFalse(may_switch('splu',failure_classification(guard,dict(type='RoutingFailure',message='mass'))))
for phase,expected in [('PRESSURE_FACTORIZE',True),('EXACT_MIDPOINTS',False),('SUBEVENT_ROUTING',False),('UNKNOWN',False)]:
c=failure_classification(dict(guard,stop_reason='wall_limit'),progress={'stage':phase})
self.assertEqual(may_switch('splu',c),expected)
self.assertFalse(may_switch('splu',failure_classification(guard)))
def coordinator_fixture(self,failures):
"""Exercise actual dispatch/fallback/ledger without any scientific input."""
calls=[]
def fake_supervise(command,dest,name,limits,**kw):
method=command[-1];index=int(command[-2]);calls.append((method,index))
out=dest/'output';out.mkdir()
fail=failures.get((method,index))
if fail:
save(out/'failure.json',dict(type=fail,message=method+' original reason'))
else:save(out/'result.json',dict(status='COMPLETE'))
return dict(status='STOPPED_OR_FAILED' if fail else 'PASS',stop_reason=None,
monitor_errors=0,cpu_seconds=2.)
with tempfile.TemporaryDirectory() as temp:
root=Path(temp);save(root/'contract.json',{});save(root/'code-freeze.json',{'git_commit':'synthetic'})
c=dict(coordinator_cpus=[0],worker_cpu=0,old_worker_CPU_s=OLD_CPU,new_synthetic_worker_CPU_s=3.,
candidate_order=['splu','gmres_ilu'],ten_parts=[{'case':str(i)} for i in range(10)],
limits=dict(part_CPU_s=1800,part_wall_s=1800,part_RSS_bytes=4*2**30,
ten_disk_bytes=5*2**30,total_worker_CPU_s=72000,postfit_reserve_CPU_s=7200))
with patch('run_s3b_v04_ten.contract',return_value=c),patch('run_s3b_v04_ten.supervise',side_effect=fake_supervise),patch('run_s3b_v04_ten.os.sched_setaffinity'),patch('run_s3b_v04_ten.signal.signal'):
exitcode=run_ten(root)
return exitcode,read(root/'complete.json'),calls
def test_dispatch_restarts_whole_ten_only_once_and_charges_failures(self):
code,r,calls=self.coordinator_fixture({('splu',2):'SolverFailure'})
self.assertEqual(code,0);self.assertEqual(r['selected_solver'],'gmres_ilu')
self.assertEqual(calls,[('splu',i) for i in range(3)]+[('gmres_ilu',i) for i in range(10)])
self.assertEqual(r['new_worker_CPU_s'],29.)
self.assertEqual(r['cumulative_worker_CPU_s'],OLD_CPU+29.)
self.assertEqual(r['candidates'][0]['classification']['category'],'NUMERICAL_FAILURE')
def test_two_numerical_failures_retained_without_third_candidate(self):
code,r,calls=self.coordinator_fixture({('splu',0):'SolverFailure',('gmres_ilu',1):'SolverFailure'})
self.assertEqual(code,1);self.assertIsNone(r['selected_solver'])
self.assertEqual(calls,[('splu',0),('gmres_ilu',0),('gmres_ilu',1)])
self.assertEqual([c['classification']['reason'] for c in r['candidates']],
['splu original reason','gmres_ilu original reason'])
def test_nonsolver_failure_stops_dispatch_without_fallback(self):
code,r,calls=self.coordinator_fixture({('splu',0):'RoutingFailure'})
self.assertEqual(code,1);self.assertEqual(calls,[('splu',0)])
if __name__=='__main__':unittest.main(verbosity=2)