# Nominal design

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':   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,
	# }

	# Prepare one corner, module 'tm', nominal op parameters, defined for all heads
	corners={
		'nom': {
			'params': nominalOp, 
			'modules': ['tm']
		}
	}
	
	# Prepare parallel environment
	cOS.setVM(MPI(mirrorMap={'*':'.'}))
	
	# 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:   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
#
#        area  <  9.000e-09    |      2.495e-10    nom : 0
#        cmrr  >  6.000e+01    |      8.422e+01    nom : 0
#        gain  >  6.000e+01    |      6.073e+01    nom : 0
#        isup  <  1.000e-03    |      1.477e-04    nom : 0
#      out_op  >  -1.250e-02   |     -4.960e-03    nom : 0
#      out_op  <  1.250e-02    |     -4.960e-03    nom : 0
#    overshdn  <  1.500e-01    |      1.814e-02    nom : 0
#    overshup  <  1.500e-01    |      1.929e-02    nom : 0
#    ph_slope  <  0.000e+00    |     -0.000e+00    nom : 0
#          pm  >  5.000e+01    |      8.171e+01    nom : 0
#    psrr_vdd  >  6.000e+01    |      6.114e+01    nom : 0
#    psrr_vss  >  6.000e+01    |      9.409e+01    nom : 0
#      slewdn  >  2.000e+06    |      1.204e+07    nom : 0
#      slewup  >  2.000e+06    |      1.173e+07    nom : 0
#       swing  >  1.000e+00    |      1.389e+00    nom : 0
#      tsetdn  <  1.000e-06    |      8.767e-08    nom : 0
#      tsetup  <  1.000e-06    |      8.706e-08    nom : 0
#        ugbw  >  1.000e+07    |      1.675e+07    nom : 0
#     vds_drv  >  0.000e+00    |      1.852e-01    nom : 0
#     vgs_drv  >  0.000e+00    |      1.721e-02    nom : 0
