# Worst case distance

from definitions import *
from pyopus.design.wcd import WorstCaseDistance
from pyopus.design.wc import WorstCase
# If MPI is imported an application not using MPI will behave correctly
# (i.e. only slot 0 will run the program) even when started with mpirun/mpiexec
from pyopus.parallel.mpi import MPI
from pyopus.parallel.cooperative import cOS


if __name__=='__main__':
	atDesign={
		# After yield targetting (beta=3)
		   'c_out':   9.5537235290798509e-13,
		  'diff_l':   1.0658148158983008e-06,
		  'diff_w':   6.5013057901064406e-06,
		  'load_l':   1.8508141777987862e-06,
		  'load_w':   1.7279146059387616e-05,
		  'mirr_l':   1.4093974490004466e-06,
		 'mirr_ld':   7.3743199883038955e-07,
		  'mirr_w':   2.2719257009593370e-05,
		 'mirr_wd':   1.6958385464233149e-06,
		 'mirr_wo':   1.7156644921472576e-05,
		   'out_l':   2.6680173515769919e-07,
		   'out_w':   1.1692244166193500e-05,
		   'r_out':   1.1678997023822742e+01,
	}
	
	# Create corner with the Monte Carlo model
	corners={
		'mc': {
			'params': {}, 
			'modules': [ 'mc' ]
		}
	}
		
	# Worst case performances (skip area, phase slope)
	wcList=list(measures.keys())
	wcList.sort()
	wcList.remove('area')
	wcList.remove('ph_slope')
		
	# Prepare parallel environment
	cOS.setVM(MPI(mirrorMap={'*':'.'}))
	
	# 3-sigma worst case
	wc=WorstCaseDistance(
		heads, analyses, measures, corners, statParams, opParams, variables=variables, 
		fixedParams=atDesign, 
		stepTol=0.01,  # Try 0.0001
		sigmaBox=10, 
		debug=2, 
		spawnerLevel=0
	)
		
	wcresults, analysisCount = wc(wcList)
	# wcresults, analysisCount = wc(['vgs_drv'])
	# wcresults, analysisCount = wc(['cmrr', 'vgs_drv'])
	# wcresults, analysisCount = wc(['cmrr'])
	# wcresults, analysisCount = wc(['ugbw'])
	# wcresults, analysisCount = wc(['vgs_drv'])
	
	print(wc.formatResults())
	print(wc.analysisCount)
	
	# Finalize cOS parallel environment
	cOS.finalize()

# Result of yield targeting (WCD)
#       cmrr >     6.00000e+01 wc=    5.99749e+01 wcd=    5.32766e+00 neval= 742 2 OK
#       gain >     6.00000e+01 wc=    6.43964e+01 wcd=    1.99879e+01 neval= 207 2 OUTSIDE+
#       isup <     1.00000e-03 wc=    2.09763e-04 wcd=    2.64353e+01 neval= 407 2 SENS+
#     out_op >    -1.25000e-02 wc=   -1.25000e-02 wcd=    3.11414e+00 neval= 185 1 OK
#     out_op <     1.25000e-02 wc=    1.25054e-02 wcd=    3.07106e+00 neval= 115 1 OK
#   overshdn <     1.50000e-01 wc=    1.50059e-01 wcd=    1.53488e+01 neval= 540 2 OK
#   overshup <     1.50000e-01 wc=    1.50075e-01 wcd=    8.64908e+00 neval= 343 2 OK
#         pm >     5.00000e+01 wc=    4.99603e+01 wcd=    1.47063e+01 neval= 249 1 OK
#   psrr_vdd >     6.00000e+01 wc=    6.29224e+01 wcd=    2.58846e+01 neval=1452 2 OUTSIDE+
#   psrr_vss >     6.00000e+01 wc=    6.00418e+01 wcd=    5.07270e+00 neval= 228 1 OK
#     slewdn >     2.00000e+06 wc=    5.65117e+06 wcd=    2.22903e+01 neval= 341 2 OUTSIDE+
#     slewup >     2.00000e+06 wc=    3.91481e+06 wcd=    2.23607e+01 neval= 151 1 OUTSIDE+
#      swing >     1.00000e+00 wc=    9.99985e-01 wcd=    1.47554e+01 neval= 345 1 OK
#     tsetdn <     1.00000e-06 wc=    2.86659e-07 wcd=    2.70286e+01 neval= 370 1 SENS+
#     tsetup <     1.00000e-06 wc=    2.71437e-07 wcd=    2.23597e+01 neval= 572 2 OUTSIDE+
#       ugbw >     1.00000e+07 wc=    9.99719e+06 wcd=    7.83917e+00 neval= 240 1 OK
# vds_drv[0] >     0.00000e+00 wc=   -1.15879e-03 wcd=    1.72132e+01 neval= 139 1 OK
# vds_drv[1] >     0.00000e+00 wc=   -9.42108e-04 wcd=    1.69051e+01 neval= 139 1 OK
# vds_drv[2] >     0.00000e+00 wc=    8.85407e-02 wcd=    1.00000e+01 neval= 122 1 OUTSIDE+
# vds_drv[3] >     0.00000e+00 wc=   -7.38603e-04 wcd=    5.74299e+00 neval= 164 1 OK
# vds_drv[4] >     0.00000e+00 wc=    4.45124e-01 wcd=    2.42680e+01 neval= 304 1 SENS+
# vds_drv[5] >     0.00000e+00 wc=    6.75109e-02 wcd=    1.00000e+01 neval= 113 1 OUTSIDE+
# vds_drv[6] >     0.00000e+00 wc=    6.81449e-02 wcd=    1.00000e+01 neval= 111 1 OUTSIDE+
# vds_drv[7] >     0.00000e+00 wc=    5.21605e-01 wcd=    2.23607e+01 neval= 366 1 SENS+
# vgs_drv[0] >     0.00000e+00 wc=    1.78126e-05 wcd=    1.29257e+01 neval= 125 1 OK
# vgs_drv[1] >     0.00000e+00 wc=    6.31513e-05 wcd=    1.30302e+01 neval= 171 1 OK
# vgs_drv[2] >     0.00000e+00 wc=    1.51870e-01 wcd=    1.36814e+01 neval= 138 1 OUTSIDE+
# vgs_drv[3] >     0.00000e+00 wc=    8.32646e-02 wcd=    1.98177e+01 neval= 127 1 OUTSIDE+
# vgs_drv[4] >     0.00000e+00 wc=    1.38505e-01 wcd=    1.96888e+01 neval= 210 1 OUTSIDE+
# vgs_drv[5] >     0.00000e+00 wc=    9.16682e-02 wcd=    1.41428e+01 neval= 183 2 OUTSIDE+
# vgs_drv[6] >     0.00000e+00 wc=    9.15788e-02 wcd=    1.41434e+01 neval= 187 2 OUTSIDE+
# vgs_drv[7] >     0.00000e+00 wc=    7.39968e-02 wcd=    1.92870e+01 neval= 580 3 OUTSIDE+
