"""Independent synthetic algebra checks, not an execution of the Fortran UMAT.
Frozen internal variable, DTIME=0 concept, J=1 base, zero-spin symmetric
engineering shear perturbation. This is NOT a full algorithmic tangent test,
Abaqus hybrid verification, material calibration, or physical validation.
"""
import numpy as np,json
from pathlib import Path
p=Path(__file__).parent
F=np.eye(3);Cvi=np.diag([4.,.5,.5]);mu_eq=mu_neq=1.
def stress(F,Cvi):
 J=np.linalg.det(F);Fb=J**(-1/3)*F;B=Fb@Fb.T;Be=Fb@Cvi@Fb.T
 dev=lambda A:A-np.trace(A)*np.eye(3)/3
 return (mu_eq*dev(B)+mu_neq*dev(Be))/J
rows=[]
for i,j in [(0,1),(0,2),(1,2)]:
 E=np.zeros((3,3));E[i,j]=E[j,i]=.5
 B=F@F.T;Be=F@Cvi@F.T
 literal=mu_eq/2*(B[i,i]+B[j,j])+mu_neq/2*(B[i,i]+B[j,j])
 expected=mu_eq/2*(B[i,i]+B[j,j])+mu_neq/2*(Be[i,i]+Be[j,j])
 fd=[]
 for h in [1e-3,1e-4,1e-5,1e-6,1e-7]:
  val=(stress((np.eye(3)+h*E)@F,Cvi)[i,j]-stress((np.eye(3)-h*E)@F,Cvi)[i,j])/(2*h)
  fd.append({'h':h,'derivative':float(val),'relative_error_to_expected':float(abs(val-expected)/abs(expected))})
 rows.append({'engineering_shear':f'{i+1}{j+1}','source_literal_D':float(literal),'frozen_state_analytic_D':float(expected),'finite_difference':fd})
# Control state has no viscous memory: the two expressions coincide.
control={'Cvi':'I','source_D':2.,'analytic_D':2.}
# Positive determinant alone does not establish positive-definiteness.
A=np.diag([-1.,-1.,1.]);Ac=A/(np.linalg.det(A)**(1/3))
# A single 180-deg endpoint rotation linearly interpolated through midpoint.
R=np.diag([-1.,-1.,1.]);mid=(np.eye(3)+R)/2
result={'scope':__doc__,'synthetic_parameters':{'F':F.tolist(),'Cvi':Cvi.tolist(),'mu_eq':mu_eq,'mu_neq':mu_neq,'units':'arbitrary consistent stiffness units; not battery values'},'shear_diagnostic':rows,'identity_control':control,'determinant_only_counterexample':{'det_after':float(np.linalg.det(Ac)),'eigenvalues_after':np.linalg.eigvalsh(Ac).tolist()},'linear_rotation_path_counterexample':{'det_start':1,'det_end':1,'det_midpoint':float(np.linalg.det(mid))},'conclusion':'The literal source shear diagonal expressions differ from the frozen-state stress derivative in this synthetic non-equilibrium state. Confirm actual solver behavior with compiled UMAT, objective tangent convention and hybrid element tests before patching.'}
(p/'lefevre_diagnostics.json').write_text(json.dumps(result,indent=2));print(json.dumps({'shear':[{k:v for k,v in r.items() if k!='finite_difference'} for r in rows],'scope':'Python algebra only, not Fortran/Abaqus'}))
