# Nominal design, IHP SG13G2

from definitions import *
from pyopus.design.cbd import CornerBasedDesign
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
	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 (this script)
	#         '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,
	# }

	# Prepare one corner, module 'tt', nominal op parameters, defined for all heads
	corners={
		'nom': {
			'params': nominalOp,
			'modules': ['tt']
		}
	}

	# Prepare parallel environment
	# Hidden files are not matched by '*' so .vacaskrc.toml is listed explicitly
	cOS.setVM(MPI(mirrorMap={'*':'.', '.vacaskrc.toml':'.'}))

	# 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',
		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:   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
# 
#         area  <  9.000e-09    |      4.669e-10    nom : 0
#         cmrr  >  6.000e+01    |      6.771e+01    nom : 0
#         gain  >  6.000e+01    |      6.188e+01    nom : 0
#         isup  <  1.000e-03    |      8.133e-05    nom : 0
#       out_op  >  -1.250e-02   |     -1.014e-03    nom : 0
#       out_op  <  1.250e-02    |     -1.014e-03    nom : 0
#     overshdn  <  1.500e-01    |      1.447e-01    nom : 0
#     overshup  <  1.500e-01    |      1.116e-01    nom : 0
#     ph_slope  <  0.000e+00    |      0.000e+00    nom : 0
#           pm  >  5.000e+01    |      5.233e+01    nom : 0
#     psrr_vdd  >  6.000e+01    |      6.062e+01    nom : 0
#     psrr_vss  >  6.000e+01    |      6.539e+01    nom : 0
#       slewdn  >  2.000e+06    |      2.326e+07    nom : 0
#       slewup  >  2.000e+06    |      2.303e+07    nom : 0
#        swing  >  7.000e-01    |      8.094e-01    nom : 0
#       tsetdn  <  1.000e-06    |      5.249e-08    nom : 0
#       tsetup  <  1.000e-06    |      5.554e-08    nom : 0
#         ugbw  >  1.000e+07    |      2.098e+07    nom : 0
#      vds_drv  >  0.000e+00    |      6.258e-03    nom : 0
#      vgs_drv  >  0.000e+00    |      5.773e-02    nom : 0
