# Sizing across corners, IHP SG13G2

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
	# Initial values from definitions.py
	# Replace with the result of nominal design (02-nominal-design.py)
	initialDesign={ name: desc['init'] 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)
	        'c_out':   7.7507762451146688e-14,
	       'diff_l':   1.1649252201495867e-06,
	       'diff_w':   2.6508745835821509e-07,
	       'load_l':   2.2661057928825525e-06,
	       'load_w':   3.9774636803384396e-06,
	       'mirr_l':   3.4338341532743677e-06,
	      'mirr_ld':   1.5296246101676927e-06,
	       'mirr_w':   9.8091256473937519e-06,
	      'mirr_wd':   2.0920475096812400e-07,
	      'mirr_wo':   3.2972814707484994e-06,
	        'out_l':   2.4159636267552384e-07,
	        'out_w':   2.9192336080807594e-06,
	        'r_out':   2.5192399400171300e+04,
	}
	# 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':   9.2132481378181631e-14,
	#        'diff_l':   1.9876789842451631e-06,
	#        'diff_w':   5.9470741770308871e-07,
	#        'load_l':   1.3769480500855740e-06,
	#        'load_w':   2.6271513552777004e-06,
	#        'mirr_l':   1.1821750158617230e-06,
	#       'mirr_ld':   3.9731613534464806e-06,
	#        'mirr_w':   9.3116365445489056e-06,
	#       'mirr_wd':   4.3637596337679738e-07,
	#       'mirr_wo':   2.6849714265031990e-06,
	#         'out_l':   3.5306329415803983e-07,
	#         'out_w':   8.7444228736840739e-06,
	#         'r_out':   4.9513888402490549e+04,
	# }

	# Prepare corners, cartesian product of
	#   MOS model:    ss, ff, sf, and fs (named ss, ff, sf, and fs)
	#   voltages:     1.08 and 1.32      (named vl and vh)
	#   temperatures: 0 and 100          (named tl and th)
	corners, names = generateCorners(
		specs=[
			(
				'model', 'mos',
				['ss', 'ff', 'sf', 'fs'],
				['ss', 'ff', 'sf', 'fs']
			),
			(
				'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 tt, 1.2V, 25degC), defined for head vacask
	nominalCorner={
		('nom',('vacask',)): {
			'modules': [ 'tt' ],
			'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
	# Hidden files are not matched by '*' so .vacaskrc.toml is listed explicitly
	cOS.setVM(MPI(mirrorMap={'*':'.', '.vacaskrc.toml':'.'}, 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 },
		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:   9.2132481378181631e-14
#          diff_l:   1.9876789842451631e-06
#          diff_w:   5.9470741770308871e-07
#          load_l:   1.3769480500855740e-06
#          load_w:   2.6271513552777004e-06
#          mirr_l:   1.1821750158617230e-06
#         mirr_ld:   3.9731613534464806e-06
#          mirr_w:   9.3116365445489056e-06
#         mirr_wd:   4.3637596337679738e-07
#         mirr_wo:   2.6849714265031990e-06
#           out_l:   3.5306329415803983e-07
#           out_w:   8.7444228736840739e-06
#           r_out:   4.9513888402490549e+04
# 
#         area  <  9.000e-09    |      6.889e-10    nom : 0
#         cmrr  >  6.000e+01    |      6.448e+01 c6_ff_vl_th : 0
#         gain  >  6.000e+01    |      6.017e+01 c14_fs_vl_th : 0
#         isup  <  1.000e-03    |      7.795e-05 c8_ff_vh_th : 0
#       out_op  >  -1.250e-02   |     -6.118e-04 c1_ss_vl_tl : 0
#       out_op  <  1.250e-02    |      2.568e-04 c8_ff_vh_th : 0
#     overshdn  <  1.500e-01    |      2.185e-02 c5_ff_vl_tl : 0
#     overshup  <  1.500e-01    |      3.281e-02 c7_ff_vh_tl : 0
#     ph_slope  <  0.000e+00    |     -0.000e+00 c1_ss_vl_tl : 0
#           pm  >  5.000e+01    |      6.428e+01 c5_ff_vl_tl : 0
#     psrr_vdd  >  6.000e+01    |      6.555e+01 c1_ss_vl_tl : 0
#     psrr_vss  >  6.000e+01    |      7.514e+01 c6_ff_vl_th : 0
#       slewdn  >  2.000e+06    |      8.603e+06 c1_ss_vl_tl : 0
#       slewup  >  2.000e+06    |      7.988e+06 c1_ss_vl_tl : 0
#        swing  >  7.000e-01    |      7.385e-01 c6_ff_vl_th : 0
#       tsetdn  <  1.000e-06    |      7.098e-08 c2_ss_vl_th : 0
#       tsetup  <  1.000e-06    |      7.182e-08 c2_ss_vl_th : 0
#         ugbw  >  1.000e+07    |      1.039e+07 c2_ss_vl_th : 0
#      vds_drv  >  0.000e+00    |      1.893e-02 c2_ss_vl_th : 0
#      vgs_drv  >  0.000e+00    |      1.247e-03 c10_sf_vl_th : 0
