#NEWLINE# def run( self , problem ) : #NEWLINE# #INDENT# 'pyOpt execution. Note that pyOpt controls the execution, and the\n        individual optimizers (i.e., SNOPT) control the iteration.\n\n        Args\n        ----\n        problem : `Problem`\n            Our parent `Problem`.\n        ' #NEWLINE# self . pyopt_solution = None #NEWLINE# rel = problem . root . _probdata . relevance #NEWLINE# self . metadata = create_local_meta ( None , self . options [ 'optimizer' ] ) #NEWLINE# self . iter_count = 0 #NEWLINE# update_local_meta ( self . metadata , ( self . iter_count , ) ) #NEWLINE# with problem . root . _dircontext : #NEWLINE# #INDENT# problem . root . solve_nonlinear ( metadata = self . metadata ) #NEWLINE# #UNINDENT# opt_prob = Optimization ( jac . options [ 'title' ] , self . _objfunc ) #NEWLINE# param_meta = self . get_desvar_metadata ( ) #NEWLINE# self . indep_list = indep_list = list ( param_meta ) #NEWLINE# param_vals = self . get_desvars ( ) #NEWLINE# for ( name , meta ) in iteritems ( param_meta ) : #NEWLINE# #INDENT# opt_prob . addVarGroup ( name , meta [ 'size' ] , type = 'c' , value = param_vals [ name ] , lower = meta [ 'lower' ] , upper = meta [ 'upper' ] ) #NEWLINE# #UNINDENT# opt_prob . finalizeDesignVariables ( ) #NEWLINE# sub_param_conns = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# full_param_conns = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# for name in indep_list : #NEWLINE# #INDENT# pathname = problem . root . unknowns . metadata ( name ) [ 'pathname' ] #NEWLINE# sub_param_conns [ name ] = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# full_param_conns [ name ] = set ( ) #NEWLINE# for ( target , info ) in iteritems ( problem . root . connections ) : #NEWLINE# #INDENT# ( src , indices ) = info #NEWLINE# if ( src == pathname ) : #NEWLINE# #INDENT# if ( indices is not None ) : #NEWLINE# #INDENT# dv_idx = param_meta [ name ] . get ( 'indices' ) #NEWLINE# indices = set ( indices ) #NEWLINE# if ( dv_idx is not None ) : #NEWLINE# #INDENT# indices . intersection_update ( dv_idx ) #NEWLINE# ldv_idx = list ( dv_idx ) #NEWLINE# mapped_idx = [ ldv_idx . index ( item ) for item in indices ] #NEWLINE# sub_param_conns [ name ] [ target ] = mapped_idx #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# sub_param_conns [ name ] [ target ] = indices #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# full_param_conns [ name ] . add ( target ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# objs = self . get_objectives ( ) #NEWLINE# self . quantities = list ( objs ) #NEWLINE# self . sparsity = OrderedDict ( ) #NEWLINE# self . sub_sparsity = OrderedDict ( ) #NEWLINE# for name in objs : #NEWLINE# #INDENT# opt_prob . addObj ( name ) #NEWLINE# self . sparsity [ name ] = self . indep_list #NEWLINE# #UNINDENT# lcons = self . get_constraints ( lintype = 'linear' ) . keys ( ) #NEWLINE# if ( len ( lcons ) > 0 ) : #NEWLINE# #INDENT# self . lin_jacs = problem . calc_gradient ( indep_list , lcons , return_format = 'dict' ) #NEWLINE# #UNINDENT# econs = self . get_constraints ( ctype = 'eq' , lintype = 'nonlinear' ) #NEWLINE# con_meta = self . get_constraint_metadata ( ) #NEWLINE# self . quantities += list ( econs ) #NEWLINE# for name in self . get_constraints ( ctype = 'eq' ) : #NEWLINE# #INDENT# meta = con_meta [ name ] #NEWLINE# size = meta [ 'size' ] #NEWLINE# lower = upper = meta [ 'equals' ] #NEWLINE# rels = rel . relevant [ name ] #NEWLINE# wrt = rels . intersection ( indep_list ) #NEWLINE# self . sparsity [ name ] = wrt #NEWLINE# if meta [ 'linear' ] : #NEWLINE# #INDENT# opt_prob . addConGroup ( name , size , lower = lower , upper = upper , linear = True , wrt = wrt , jac = self . lin_jacs [ name ] ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# jac = self . _build_sparse ( name , wrt , size , param_vals , sub_param_conns , full_param_conns , rels ) #NEWLINE# opt_prob . addConGroup ( name , size , lower = lower , upper = upper , wrt = wrt , jac = jac ) #NEWLINE# #UNINDENT# #UNINDENT# incons = self . get_constraints ( ctype = 'ineq' , lintype = 'nonlinear' ) #NEWLINE# self . quantities += list ( incons ) #NEWLINE# for name in self . get_constraints ( ctype = 'ineq' ) : #NEWLINE# #INDENT# meta = con_meta [ name ] #NEWLINE# size = meta [ 'size' ] #NEWLINE# lower = meta [ 'lower' ] #NEWLINE# upper = meta [ 'upper' ] #NEWLINE# rels = rel . relevant [ name ] #NEWLINE# wrt = rels . intersection ( indep_list ) #NEWLINE# self . sparsity [ name ] = wrt #NEWLINE# if meta [ 'linear' ] : #NEWLINE# #INDENT# opt_prob . addConGroup ( name , size , upper = upper , lower = lower , linear = True , wrt = wrt , jac = self . lin_jacs [ name ] ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# jac = self . _build_sparse ( name , wrt , size , param_vals , sub_param_conns , full_param_conns , rels ) #NEWLINE# opt_prob . addConGroup ( name , size , upper = upper , lower = lower , wrt = wrt , jac = jac ) #NEWLINE# #UNINDENT# #UNINDENT# optimizer = self . options [ 'optimizer' ] #NEWLINE# try : #NEWLINE# #INDENT# exec ( ( 'from pyoptsparse import %s' % optimizer ) ) #NEWLINE# #UNINDENT# except ImportError : #NEWLINE# #INDENT# msg = ( 'Optimizer %s is not available in this installation.' % optimizer ) #NEWLINE# raise ImportError ( msg ) #NEWLINE# #UNINDENT# optname = vars ( ) [ optimizer ] #NEWLINE# opt = optname ( ) #NEWLINE# for ( option , value ) in self . opt_settings . items ( ) : #NEWLINE# #INDENT# opt . setOption ( option , value ) #NEWLINE# #UNINDENT# self . _problem = problem #NEWLINE# if ( self . options [ 'gradient method' ] == 'pyopt_fd' ) : #NEWLINE# #INDENT# fd_step = problem . root . fd_options [ 'step_size' ] #NEWLINE# sol = opt ( opt_prob , sens = 'FD' , sensStep = fd_step , storeHistory = self . hist_file ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( self . options [ 'gradient method' ] == 'snopt_fd' ) : #NEWLINE# #INDENT# if ( self . options [ 'optimizer' ] == 'SNOPT' ) : #NEWLINE# #INDENT# fd_step = problem . root . fd_options [ 'step_size' ] #NEWLINE# sol = opt ( opt_prob , sens = None , sensStep = fd_step , storeHistory = self . hist_file ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# msg = "SNOPT's internal finite difference can only be used with SNOPT" #NEWLINE# raise Exception ( msg ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# sol = opt ( opt_prob , sens = self . _gradfunc , storeHistory = self . hist_file ) #NEWLINE# #UNINDENT# #UNINDENT# self . _problem = None #NEWLINE# if self . options [ 'print_results' ] : #NEWLINE# #INDENT# print ( sol ) #NEWLINE# #UNINDENT# dv_dict = sol . getDVs ( ) #NEWLINE# for name in indep_list : #NEWLINE# #INDENT# val = dv_dict [ name ] #NEWLINE# self . set_desvar ( name , val ) #NEWLINE# #UNINDENT# with self . root . _dircontext : #NEWLINE# #INDENT# self . root . solve_nonlinear ( metadata = self . metadata ) #NEWLINE# #UNINDENT# self . pyopt_solution = sol #NEWLINE# try : #NEWLINE# #INDENT# exit_status = sol . optInform [ 'value' ] #NEWLINE# self . exit_flag = 1 #NEWLINE# if ( exit_status > 2 ) : #NEWLINE# #INDENT# self . exit_flag = 0 #NEWLINE# #UNINDENT# #UNINDENT# except KeyError : #NEWLINE# #INDENT# self . exit_flag = 0