# Sizing across corners

from definitions import *
from pyopus.design.cbd import CornerBasedDesign, generateCorners
from pyopus.evaluator.aggregate import formatParameters
# 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__':
	# Prepare statistical parameters dictionary with nominal values (0)
	nominalStat={ name: 0.0 for name, desc in statParams.items() }
	
	# Prepare operating parameters dictionary with nominal values
	nominalOp={ name: desc['init'] for name, desc in opParams.items() }
	
	# Prepare initial design parameters dictionary
	# Uncomment this if you want to use initial values from definitions.py
	# initialDesign={ name: desc['init'] for name, desc in designParams.items() }
	# Uncomment this if you want to use lower bounds for initial values of design parameters
	# initialDesign={ name: desc['lo'] for name, desc in designParams.items() }
	# Uncomment this if you want to use the result of nominal design as initial point
	# initialDesign={
	# 	# Result of nominal design (02-nominal-design.py)
	initialDesign={
		    'c_out':   1.1323348426062233e-12,
		   'diff_l':   5.7516770732133537e-07,
		   'diff_w':   6.1849354089044224e-06,
		   'load_l':   3.8268628583035028e-07,
		   'load_w':   1.2356364701250253e-05,
		   'mirr_l':   1.1466286599641448e-06,
		  'mirr_ld':   6.9789073144994492e-07,
		   'mirr_w':   3.7169582862454505e-05,
		  'mirr_wd':   4.3785470907062925e-06,
		  'mirr_wo':   1.2084585789240755e-05,
		    'out_l':   4.5257181177292502e-07,
		    'out_w':   8.8996274971695620e-06,
		    'r_out':   4.3390727723875116e+03,
	}
	# Uncomment this if you want to use the result of design across corners as initial point
	# initialDesign={
	# 	# Result of design across corners (this script)
	# 		'c_out':   8.9927771078152417e-13,
	#        'diff_l':   5.5010127459253703e-07,
	#        'diff_w':   5.3904971215621194e-06,
	#        'load_l':   4.5632010212870358e-07,
	#        'load_w':   1.2903462611747256e-05,
	#        'mirr_l':   1.0780160698495493e-06,
	#       'mirr_ld':   6.7678926089552582e-07,
	#        'mirr_w':   3.5964595324068664e-05,
	#       'mirr_wd':   3.9105057741823637e-06,
	#       'mirr_wo':   1.2231772290762777e-05,
	#         'out_l':   4.3744273095898316e-07,
	#         'out_w':   9.4938237518467491e-06,
	#         'r_out':   4.9902298597233967e+03,
	# }

	# Prepare corners, cartesian product of 
	#   MOS model:    wp, ws, wo, and wz (named wp, ws, wo, and wz)
	#   voltages:     1.7 and 2.0        (named vl and vh)
	#   temperatures: 0 and 100          (named tl and th)
	corners, names = generateCorners(
		specs=[
			(
				'model', 'mos', 
				['wp', 'ws', 'wo', 'wz'], 
				['wp', 'ws', 'wo', 'wz']
			), 
			(
				'param', 'vdd', 
				[opParams['vdd']['lo'], opParams['vdd']['hi']], 
				['vl', 'vh'], 
			), 
			(
				'param', 'temperature', 
				[opParams['temperature']['lo'], opParams['temperature']['hi']], 
				['tl', 'th'], 
			), 
		], 
		heads=[ 'vacask' ],
	)
	
	# Add nominal corner (model tm, 1.8V, 25degC), defined for head vacask
	nominalCorner={
		('nom',('vacask',)): {
			'modules': [ 'tm' ], 
			'params': {
				'vdd': opParams['vdd']['init'], 
				'temperature': opParams['temperature']['init']
			}, 
		}
	}
	corners.update(nominalCorner)
	
	# Now we have 4x2x2+1=17 corners, print their names
	print("Corner names")
	print(sorted(corners.keys()))
	
	# Area should be evauated only in corner 'nom'
	measures['area']['corners']=[ 'nom' ]
	
	# Prepare parallel environment
	cOS.setVM(MPI(mirrorMap={'*':'.'}, persistentStorage=False))
	
	# Design it, nominal statistical parameters are treated as fixed parameters
	cbd=CornerBasedDesign(
		designParams, heads, analyses, measures, corners, 
		fixedParams=[nominalStat], variables=variables, norms={ 'area': 100e-12 }, 
		# tradeoff=1e-6, 
		# stepTol=1e-4, 
		initial=initialDesign, 
		method='global', 
		incrementalCorners=True, 
		evaluatorOptions={'debug': 0}, 
		debug=1
	)
	
	# Run optimization
	atDesign, aggregator, analysisCount = cbd()
	
	# Print design parameters and performance measures
	print(formatParameters(atDesign))
	print(aggregator.formatResults())
	
	# Finalize cOS parallel environment
	cOS.finalize()
	
# Result
#           c_out:   8.9927771078152417e-13
#          diff_l:   5.5010127459253703e-07
#          diff_w:   5.3904971215621194e-06
#          load_l:   4.5632010212870358e-07
#          load_w:   1.2903462611747256e-05
#          mirr_l:   1.0780160698495493e-06
#         mirr_ld:   6.7678926089552582e-07
#          mirr_w:   3.5964595324068664e-05
#         mirr_wd:   3.9105057741823637e-06
#         mirr_wo:   1.2231772290762777e-05
#           out_l:   4.3744273095898316e-07
#           out_w:   9.4938237518467491e-06
#           r_out:   4.9902298597233967e+03
#
#        area  <  9.000e-09    |      2.304e-10    nom : 0
#        cmrr  >  6.000e+01    |      7.556e+01 c13_wz_vl_tl : 0
#        gain  >  6.000e+01    |      6.027e+01 c1_wp_vl_tl : 0
#        isup  <  1.000e-03    |      1.492e-04 c4_wp_vh_th : 0
#      out_op  >  -1.250e-02   |     -4.889e-03 c2_wp_vl_th : 0
#      out_op  <  1.250e-02    |     -3.945e-03 c15_wz_vh_tl : 0
#    overshdn  <  1.500e-01    |      2.464e-02 c6_ws_vl_th : 0
#    overshup  <  1.500e-01    |      2.569e-02 c6_ws_vl_th : 0
#    ph_slope  <  0.000e+00    |     -0.000e+00 c1_wp_vl_tl : 0
#          pm  >  5.000e+01    |      7.525e+01 c10_wo_vl_th : 0
#    psrr_vdd  >  6.000e+01    |      6.067e+01 c1_wp_vl_tl : 0
#    psrr_vss  >  6.000e+01    |      7.795e+01 c5_ws_vl_tl : 0
#      slewdn  >  2.000e+06    |      1.305e+07 c13_wz_vl_tl : 0
#      slewup  >  2.000e+06    |      1.260e+07 c13_wz_vl_tl : 0
#       swing  >  1.000e+00    |      1.232e+00 c6_ws_vl_th : 0
#      tsetdn  <  1.000e-06    |      7.991e-08 c13_wz_vl_tl : 0
#      tsetup  <  1.000e-06    |      8.058e-08 c5_ws_vl_tl : 0
#        ugbw  >  1.000e+07    |      1.644e+07 c6_ws_vl_th : 0
#     vds_drv  >  0.000e+00    |      9.605e-02 c5_ws_vl_tl : 0
#     vgs_drv  >  0.000e+00    |      6.909e-03 c1_wp_vl_tl : 0
