from rawfile import rawread
from runtest import *
import numpy as np
import sys

raw = rawread('op1.raw').get()

tests=[]

# b1 drives i=M_PI*v(a,0)**2 from node a to node 0, in parallel with v1.
# By KCL at node a, v1 must sink exactly that current, so
# v1:flow(br) == -M_PI*v(a)**2.
va = raw['v1sweep']
i_meas = raw['v1:flow(br)']
i_exact = -np.pi*va**2

status = (relDiff(i_meas, i_exact, 1e-12) < 1e-6).all()
print("v1:flow(br) test", status)
tests.append(status)

# b2 enforces v(b,0)=M_PI*v(a)**3, so node b's voltage should track M_PI*va**3.
vb_meas = raw['b']
vb_exact = np.pi*va**3

status = (relDiff(vb_meas, vb_exact, 1e-12) < 1e-6).all()
print("v(b) test", status)
tests.append(status)

# b3 enforces v(c,0) = M_PI*(i(v1)-1)**2, where i(v1) is v1's branch flow
# (the same current verified above as v1:flow(br) == -M_PI*va**2). i(v1) is
# a current, converged only to the default reltol=1e-3, so allow more slack
# here than for the direct voltage checks above.
vc_meas = raw['c']
vc_exact = np.pi*(i_exact-1)**2

status = (relDiff(vc_meas, vc_exact, 1e-12) < 1e-3).all()
print("v(c) test", status)
tests.append(status)

if isTest():
    sys.exit(not all(tests))
