def compute(self, inputs, outputs): """Compute the objective expression""" global counter counter += 1 cpacs_path = mif.get_tooloutput_file_path(Rt.modules[-1]) # Save the CPACS file for this iteration if counter % Rt.save_iter == 0: loc = optim_dir_path + '/Geometry/' + 'iter_{}.xml'.format(counter) shutil.copy(cpacs_path, loc) # Add new variables to dictionnary tixi = cpsf.open_tixi(cpacs_path) dct.update_dict(tixi, optim_var_dict) # Change local wkdir for the next iteration tixi.updateTextElement(opf.WKDIR_XPATH, ceaf.create_new_wkdir(Rt.date, Rt.type)) for obj in Rt.objective: var_list = splt('[+*/-]', obj) for v in var_list: if not v.isdigit() and v != '': exec('{} = inputs["{}"]'.format(v, v)) result = eval(obj) if Rt.minmax == 'min': outputs['Objective function ' + obj] = -result else: outputs['Objective function ' + obj] = result cpsf.close_tixi(tixi, cpacs_path)
def one_iteration(): """ Compute the objective function. Function 'one_iteration' will exectute in order all the module contained in '...' and extract the ... value from the last CPACS file, this value will be returned to the optimizer CPACSUpdater.... """ global counter counter += 1 # Create the parameter in CPACS with 'CPACSUpdater' module cpacs_path = mi.get_toolinput_file_path('CPACSUpdater') cpacs_out_path = mi.get_tooloutput_file_path('CPACSUpdater') tixi = cpsf.open_tixi(cpacs_path) wkdir_path = ceaf.create_new_wkdir(Rt.date) WKDIR_XPATH = '/cpacs/toolspecific/CEASIOMpy/filesPath/wkdirPath' tixi.updateTextElement(WKDIR_XPATH, wkdir_path) # TODO: improve this part! (maybe move somewhere else) # To delete coef from previous iter if opf.get_aeromap_path(Rt.modules) != 'None': xpath = opf.get_aeromap_path(Rt.modules) aeromap_uid = cpsf.get_value(tixi, xpath + '/aeroMapUID') Coef = apmf.get_aeromap(tixi, aeromap_uid) apmf.delete_aeromap(tixi, aeromap_uid) apmf.create_empty_aeromap(tixi, aeromap_uid, 'test_optim') apmf.save_parameters(tixi, aeromap_uid, Coef) cpsf.close_tixi(tixi, cpacs_path) # Update the CPACS file with the parameters contained in design_var_dict update_cpacs_file(cpacs_path, cpacs_out_path, design_var_dict) # Save the CPACS file if counter % 1 == 0: file_copy_from = mi.get_tooloutput_file_path('CPACSUpdater') shutil.copy( file_copy_from, optim_dir_path + '/Geometry/' + 'iter_{}.xml'.format(counter)) # Run optimisation sub workflow wkf.copy_module_to_module('CPACSUpdater', 'out', Rt.modules[0], 'in') wkf.run_subworkflow(Rt.modules) wkf.copy_module_to_module(Rt.modules[-1], 'out', 'CPACSUpdater', 'in') # Extract results TODO: improve this part cpacs_results_path = mi.get_tooloutput_file_path(Rt.modules[-1]) log.info('Results will be extracted from:' + cpacs_results_path) tixi = cpsf.open_tixi(cpacs_results_path) # Update the constraints values update_res_var_dict(tixi) return compute_obj()
def routine_launcher(Opt): """Run an optimisation routine or DoE using the OpenMDAO library. This function launches the setup for the routine by setting up the problem with the OpenMDAO component, creating of reading a file containing all the problem parameters and launching the driver. It also specifies where to save the case recordings and launches the results plotting at the end of the routine. Args: Opt (class) : Indicates which modules to use and the routine type (Optim or DoE). """ global optim_var_dict, am_dict, Rt, am_length Rt.type = Opt.optim_method Rt.modules = Opt.module_optim ## Initialize CPACS file and problem dictionary ## create_routine_folder() opf.first_run(Rt) tixi = cpsf.open_tixi(opf.CPACS_OPTIM_PATH) tixi.updateTextElement(opf.WKDIR_XPATH, ceaf.create_new_wkdir(optim_dir_path)) Rt.get_user_inputs(tixi) optim_var_dict = opf.create_variable_library(Rt, tixi, optim_dir_path) am_dict = opf.create_am_lib(Rt, tixi) cpsf.close_tixi(tixi, opf.CPACS_OPTIM_PATH) wkf.copy_module_to_module('Optimisation', 'in', Rt.modules[0], 'in') ## Instantiate components and subsystems ## prob = om.Problem() create_om_problem(prob) ## Run the model ## prob.run_driver() generate_results(prob)
def one_optim_iter(): """Function to evaluate the value to optimize. Function 'one_optim_iter' will exectute in order all the module contained in '...' and extract the ... value from the last CPACS file, this value will be returned to the optimizer CPACSUpdater.... """ # Create the parameter in CPACS with 'CPACSUpdater' module cpacs_path = mi.get_toolinput_file_path('CPACSUpdater') cpacs_out_path = mi.get_tooloutput_file_path('CPACSUpdater') tixi = cpsf.open_tixi(cpacs_path) wkdir_path = ceaf.create_new_wkdir() WKDIR_XPATH = '/cpacs/toolspecific/CEASIOMpy/filesPath/wkdirPath' tixi.updateTextElement(WKDIR_XPATH, wkdir_path) # TODO: improve this part! (maybe move somewhere else) # To delete coef from previous iter aeromap_uid = cpsf.get_value(tixi, SU2_XPATH + '/aeroMapUID') Coef = apmf.get_aeromap(tixi, aeromap_uid) apmf.delete_aeromap(tixi, aeromap_uid) apmf.create_empty_aeromap(tixi, aeromap_uid, 'test_optim') apmf.save_parameters(tixi, aeromap_uid, Coef) cpsf.close_tixi(tixi, cpacs_path) # Update the CPACS file with the parameters contained in optim_var_dict update_cpacs_file(cpacs_path, cpacs_out_path, optim_var_dict) # Run optimisation sub workflow wkf.copy_module_to_module('CPACSUpdater', 'out', module_optim[0], 'in') wkf.run_subworkflow(module_optim) wkf.copy_module_to_module(module_optim[-1], 'out', 'CPACSUpdater', 'in') # Extract results TODO: improve this part cpacs_results_path = mi.get_tooloutput_file_path(module_optim[-1]) log.info('Results will be extracted from:' + cpacs_results_path) tixi = cpsf.open_tixi(cpacs_results_path) mtom = cpsf.get_value( tixi, '/cpacs/vehicles/aircraft/model/analyses/massBreakdown/designMasses/mTOM/mass' ) aeromap_uid = cpsf.get_value(tixi, SU2_XPATH + '/aeroMapUID') Coef = apmf.get_aeromap(tixi, aeromap_uid) cl = Coef.cl[0] cd = Coef.cd[0] cm = Coef.cms[0] log.info('=========================') for key, (name, listval, minval, maxval, command) in optim_var_dict.items(): log.info(name, ': ', listval[-1]) log.info('Cl/Cd: ' + str(cl / cd)) log.info('Cl: ' + str(cl)) log.info('Cd: ' + str(cd)) log.info('Cd: ' + str(cm)) log.info('MTOM:' + str(mtom)) log.info('(Cl)/MTOM:' + str(cl / mtom)) log.info('=========================') # TODO: add option to choose what will be returned # return -mtom # return -cl # return cd # return -cl/cd return -cl / cd / mtom