File size: 2,310 Bytes
3bb2683
 
 
424d972
3bb2683
 
 
 
4fe5203
3bb2683
424d972
4fe5203
3bb2683
 
 
 
 
424d972
3bb2683
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
"""Cross-check the deployed browser engine against the Python reference."""
import unittest,json,subprocess
from pathlib import Path
from models import analyze,control,state_model,analyze_pu,state_model_pu
ROOT=Path(__file__).resolve().parent

class BrowserEngineTest(unittest.TestCase):
 def test_browser_python_agreement(self):
  cases=[{'kind':'analyze','payload':{'ratio':r,'topologies':['xfly2p','xfly4p','3lbuck','2sbuck','buck','boost','ns','buck2','sp2']}} for r in [2,4,6,8,16]]
  cases +=[{'kind':'analyze','payload':{'ratio':3,'rs_mohm':30,'rc_mohm':5,'rl_mohm':10,'cf_nf':100,'fs_mhz':20}}]
  cases +=[{'kind':'analyzePU','payload':{'ratio':r,'rho_c':c,'rho_l':l,'rho_q':q}} for r in [2,4,6,8,16] for c,l,q in [(1,1,20),(0,0,0),(.3,2,7)]]
  cases +=[{'kind':'analyzePU','payload':{'ratio':r,'topologies':['buck2','sp2'],'rho_c':c,'rho_l':l,'rho_q':q}} for r in [2,4,8] for c,l,q in [(1,1,20),(0,0,0),(.3,2,7)]]
  cases +=[{'kind':'control','payload':{'fc_khz':r}} for r in [2,5,10,22,50]]
  cases +=[{'kind':'stateModel','payload':{'states':[{'fraction':.5,'switch_currents':[.5,0,.5,0],'capacitor_currents':[.25],'inductor_currents':[.5,.5]},{'fraction':.5,'switch_currents':[0,.5,0,.5],'capacitor_currents':[-.25],'inductor_currents':[.5,.5]}]}}]
  js="const fs=require('fs'),e=require('./static/engine.js'),data=require('./static/models-data.json');const cases=JSON.parse(fs.readFileSync(0,'utf8'));console.log(JSON.stringify(cases.map(c=>e[c.kind](c.payload,data))))"
  result=json.loads(subprocess.check_output(['node','-e',js],cwd=ROOT,input=json.dumps(cases).encode()))
  for case,actual in zip(cases,result):
   expected={'analyze':analyze,'control':control,'stateModel':state_model,'analyzePU':analyze_pu,'stateModelPU':state_model_pu}[case['kind']](case['payload'])
   # Browser library records identical equations, scope and all reference arrays.
   self.compare(expected,actual)
 def compare(self,a,b):
  if isinstance(a,dict):
   for k in a:
    if a[k] is not None:self.compare(a[k],b.get(k))
  elif isinstance(a,list):
   self.assertEqual(len(a),len(b))
   for x,y in zip(a,b):self.compare(x,y)
  elif isinstance(a,(int,float)) and not isinstance(a,bool):self.assertAlmostEqual(a,b,delta=max(1e-11,abs(a)*1e-10))
  else:self.assertEqual(a,b)

if __name__=='__main__':unittest.main()