Exemplo n.º 1
0
    def compute(self, inputs, outputs):
        """Launches the module"""

        # Updating inputs in CPACS file
        cpacs_path = mif.get_toolinput_file_path(self.module_name)
        tixi = cpsf.open_tixi(cpacs_path)
        for name in inputs:
            if name in optim_var_dict:
                xpath = optim_var_dict[name][4]
                cpsf.add_float_vector(tixi, xpath, inputs[name])
        cpsf.close_tixi(tixi, cpacs_path)

        # Running the module
        wkf.run_subworkflow([self.module_name])

        # Feeding CPACS file restults to outputs
        cpacs_path = mif.get_tooloutput_file_path(self.module_name)
        tixi = cpsf.open_tixi(cpacs_path)
        for name in outputs:
            if name in optim_var_dict:
                xpath = optim_var_dict[name][4]
                outputs[name] = cpsf.get_value(tixi, xpath)

        # Copy CPACS to input folder of next module
        index = Rt.modules.index(self.module_name) + 1
        if index != len(Rt.modules):
            cpacs_path = mif.get_toolinput_file_path(Rt.modules[index])
        else:
            cpacs_path = mif.get_toolinput_file_path(Rt.modules[0])
        cpsf.close_tixi(tixi, cpacs_path)
Exemplo n.º 2
0
def first_run(Rt):
    """Run subworkflow once for the optimisation problem.

    This function runs a first loop to ensure that all problem variables
    are created an can be fed to the optimisation setup program.

    Args:
        Rt (Routine object): Class that contains the routine informations.

    """

    log.info('Launching initialization workflow')
    Rt.modules.insert(0, 'Optimisation')

    # Settings needed for CFD calculation
    if 'SettingsGUI' in Rt.modules:
        Rt.modules.remove('SettingsGUI')
        Rt.modules.insert(0, 'SettingsGUI')
        
    # First iteration to create aeromap results if no pre-workflow
    if Rt.modules[0] == 'Optimisation':
        wkf.copy_module_to_module('Optimisation', 'in', Rt.modules[1], 'in')
    else:
        wkf.copy_module_to_module('Optimisation', 'in', Rt.modules[0], 'in')

    wkf.run_subworkflow(Rt.modules)
    wkf.copy_module_to_module(Rt.modules[-1], 'out', 'Optimisation', 'in')

    # SettingsGUI only needed at the first iteration
    if 'SettingsGUI' in Rt.modules: #and added_gui:
        Rt.modules.remove('SettingsGUI')

    # Optimisation parameters only needed for the first run
    Rt.modules.remove('Optimisation')
Exemplo n.º 3
0
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()
Exemplo n.º 4
0
    def compute(self, inputs, outputs):
        """Launches the module"""

        # Updating inputs in CPACS file
        cpacs_path = mif.get_toolinput_file_path(self.module_name)
        tixi = cpsf.open_tixi(cpacs_path)
        for name in inputs:
            if name in optim_var_dict:
                xpath = optim_var_dict[name][4]
                # Change only the first vector value for aeromap param
                if name in apmf.XSTATES:
                    size = tixi.getVectorSize(xpath)
                    v = list(tixi.getFloatVector(xpath, size))
                    v.pop(0)
                    v.insert(0, inputs[name])
                    tixi.updateFloatVector(xpath, v, size, '%g')
                else:
                    cpsf.add_float_vector(tixi, xpath, inputs[name])
        cpsf.close_tixi(tixi, cpacs_path)

        # Running the module
        wkf.run_subworkflow([self.module_name])

        # Feeding CPACS file results to outputs
        cpacs_path = mif.get_tooloutput_file_path(self.module_name)
        tixi = cpsf.open_tixi(cpacs_path)
        for name in outputs:
            if name in optim_var_dict:
                xpath = optim_var_dict[name][4]
                if name in apmf.COEF_LIST:
                    val = cpsf.get_value(tixi, xpath)
                    if isinstance(val, str):
                        val = val.split(';')
                        outputs[name] = val[0]
                    else:
                        outputs[name] = val
                else:
                    outputs[name] = cpsf.get_value(tixi, xpath)

        # Copy CPACS to input folder of next module
        index = Rt.modules.index(self.module_name) + 1
        if index != len(Rt.modules):
            cpacs_path = mif.get_toolinput_file_path(Rt.modules[index])
        else:
            cpacs_path = mif.get_toolinput_file_path(Rt.modules[0])
        cpsf.close_tixi(tixi, cpacs_path)
Exemplo n.º 5
0
def first_run(module_list, modules_pre_list=[]):
    """Run subworkflow once for the optimisation problem.

    This function runs a first loop to ensure that all problem variables
    are created an can be fed to the optimisation setup program.

    Args:
        module_list (lst) : List of modules to run.
        module_pre_list (lst) : List of modules that were run in a previous
        workflow.

    Returns:
        None.

    """
    # Check if aeromap results already exists, else run workflow
    global XPATH
    global XPATH_PRE

    log.info('Launching initialization workflow')

    XPATH = tls.get_aeromap_path(module_list)
    if 'PyTornado' in modules_pre_list or 'SU2Run' in modules_pre_list:
        XPATH_PRE = tls.get_aeromap_path(modules_pre_list)
    else:
        XPATH_PRE = XPATH

    # Settings needed for CFD calculation
    if 'Optimisation' not in modules_pre_list:
        module_list.insert(0, 'Optimisation')
    if 'SettingsGUI' not in module_list or 'SettingsGUI' not in modules_pre_list:
        module_list.insert(0, 'SettingsGUI')

    # First iteration to create aeromap results if no pre-workflow
    if XPATH != XPATH_PRE or modules_pre_list == []:
        wkf.copy_module_to_module('Optimisation', 'in', module_list[0], 'in')
        wkf.run_subworkflow(module_list)
        wkf.copy_module_to_module(module_list[-1], 'out', 'Optimisation', 'in')

    # SettingsGUI only needed at the first iteration
    if 'SettingsGUI' in module_list:
        module_list.pop(module_list.index('SettingsGUI'))

    # Optimisation parameters only needed for the first run
    module_list.pop(module_list.index('Optimisation'))
Exemplo n.º 6
0
def init_dict(cpacs_path, module_list, modules_pre_list=[]):
    """
    Create dictionnaries for the optimisation problem.

    Parameters
    ----------
    cpacs_path : String
        Path to the CPACS file.
    module_list : List
        List of modules.

    Returns
    -------
    Dict
        All dictionnaries needed for the optimisation problem.

    """
    # Check if aeromap results already exists, else run workflow
    global XPATH
    global XPATH_PRE

    XPATH = optf.get_aeromap_path(module_list)
    if 'PyTornado' in modules_pre_list or 'SU2Run' in modules_pre_list:
        XPATH_PRE = optf.get_aeromap_path(modules_pre_list)
    else:
        XPATH_PRE = XPATH

    # Settings needed for CFD calculation
    if 'SettingsGUI' not in module_list or 'SettingsGUI' not in modules_pre_list:
        module_list.insert(0, 'SettingsGUI')

    # First iteration to create aeromap results if no pre-workflow
    if XPATH != XPATH_PRE or modules_pre_list == []:
        wkf.copy_module_to_module('Optimisation', 'in', module_list[0], 'in')
        wkf.run_subworkflow(module_list)
        wkf.copy_module_to_module(module_list[-1], 'out', 'Optimisation', 'in')

    # If settingsGUI only needed at the first iteration
    if 'SettingsGUI' in module_list:
        module_list.pop(module_list.index('SettingsGUI'))

    tixi = cpsf.open_tixi(cpacs_path)

    return init_res_dict(tixi), init_design_var_dict(tixi)
Exemplo n.º 7
0
        Opt.module_post = []

    # Copy ToolInput.xml in ToolInput dir if not already there
    cpacs_path = mi.get_toolinput_file_path(MODULE_NAME)
    if not Opt.cpacs_path == cpacs_path:
        shutil.copy(Opt.cpacs_path, cpacs_path)
        Opt.cpacs_path = cpacs_path

    # Create a new wkdir
    tixi = cpsf.open_tixi(Opt.cpacs_path)
    wkdir = ceaf.get_wkdir_or_create_new(tixi)
    cpsf.close_tixi(tixi, Opt.cpacs_path)

    # Run Pre-otimisation workflow
    if Opt.module_pre:
        wkf.run_subworkflow(Opt.module_pre, Opt.cpacs_path)

        if not Opt.module_optim and not Opt.module_post:
            shutil.copy(mi.get_tooloutput_file_path(Opt.module_pre[-1]),
                        cpacs_path_out)

    # Run Optimisation workflow
    if Opt.module_optim:
        if Opt.module_pre:
            wkf.copy_module_to_module(Opt.module_pre[-1], 'out',
                                      'Optimisation', 'in')
        else:
            wkf.copy_module_to_module('WorkflowCreator', 'in', 'Optimisation',
                                      'in')

        if Opt.optim_method != 'None':
Exemplo n.º 8
0
def aeromap_case_gen(modules):
    """
    Generate a CSV file containing a dataset generated with aeromap parameters
    only.

    Args:
        modules (lst) : list of modules to execute.

    Returns:
        file (str) : Path to CSV file.

    """
    file = MODULE_DIR + '/Aeromap_generated.csv'
    infile = mi.get_toolinput_file_path('PredictiveTool')
    outfile = mi.get_tooloutput_file_path('PredictiveTool')

    tixi = cpsf.open_tixi(infile)

    # Inputs
    alt = [0, 0]
    mach = [0.5, 0.5]
    aoa = [-10, 10]
    aos = [0, 0]
    nt = 100
    bounds = np.array([alt, mach, aoa, aos])
    # Sort criterion : ‘center’, ‘maximin’, ‘centermaximin’, ‘correlation’
    crit = 'corr'

    # Generate sample points, LHS or FullFactorial
    sampling = smp.LHS(xlimits=bounds, criterion=crit)
    xd = sampling(nt)
    xd = xd.transpose()
    # Delete the other aeromaps... maybe conserve them ?
    for uid in apmf.get_aeromap_uid_list(tixi):
        apmf.delete_aeromap(tixi, uid)

    # Create new aeromap
    aeromap_uid = 'DoE_Aeromap'
    am_xpath = tls.get_aeromap_path(modules)
    apmf.create_empty_aeromap(tixi, aeromap_uid)
    cpsf.add_string_vector(tixi, am_xpath + '/aeroMapUID', [aeromap_uid])

    # Add parameters to aeromap
    Param = apmf.AeroCoefficient()
    for i in range(0, xd.shape[1]):
        Param.add_param_point(xd[0][i], xd[1][i], xd[2][i], xd[3][i])
    apmf.save_parameters(tixi, aeromap_uid, Param)
    cpsf.close_tixi(tixi, infile)

    wkf.run_subworkflow(modules, cpacs_path_in=infile, cpacs_path_out=outfile)

    # Get Aerocoefficient
    tixi = cpsf.open_tixi(outfile)
    am_xpath = tls.get_aeromap_path(modules)
    aeromap_uid = cpsf.get_value(tixi, am_xpath + '/aeroMapUID')
    AeroCoefficient = apmf.get_aeromap(tixi, aeromap_uid)
    cpsf.close_tixi(tixi, outfile)

    dct = AeroCoefficient.to_dict()

    # Write to CSV
    df = pd.DataFrame(dct)
    df = df.transpose()
    var_type = [
        'obj' if i in objectives else
        'des' if i in ['alt', 'mach', 'aoa', 'aos'] else 'const'
        for i in df.index
    ]
    df.insert(0, 'type', var_type)
    df.to_csv(file, index=True)
    return file
Exemplo n.º 9
0
def run_workflow(Otp):
    """ Run the complete Worflow

    Args:
        Opt (class): Cl
        cpacs_out_path (str): Path to the output CPACS file
        module_list (list): List of module to inclue in the GUI

    """

    # Copy ToolInput.xml in ToolInput dir if not already there
    cpacs_path = mi.get_toolinput_file_path(MODULE_NAME)
    if not os.path.abspath(Opt.cpacs_path) == os.path.abspath(cpacs_path):
        shutil.copy(Opt.cpacs_path, cpacs_path)
        Opt.cpacs_path = os.path.abspath(cpacs_path)

    # Create a new wkdir
    tixi = cpsf.open_tixi(Opt.cpacs_path)
    wkdir = ceaf.get_wkdir_or_create_new(tixi)
    cpsf.close_tixi(tixi, Opt.cpacs_path)

    # Run Pre-otimisation workflow
    if Opt.module_pre:
        wkf.run_subworkflow(Opt.module_pre, Opt.cpacs_path)

        if not Opt.module_optim and not Opt.module_post:
            shutil.copy(mi.get_tooloutput_file_path(Opt.module_pre[-1]),
                        cpacs_path_out)

    # Run Optimisation workflow
    if Opt.module_optim:
        if Opt.module_pre:
            wkf.copy_module_to_module(Opt.module_pre[-1], 'out',
                                      'Optimisation', 'in')
        else:
            wkf.copy_module_to_module('WorkflowCreator', 'in', 'Optimisation',
                                      'in')

        if Opt.optim_method != 'None':
            routine_launcher(Opt)
        else:
            log.warning('No optimization method has been selected!')
            log.warning('The modules will be run as a simple workflow')
            wkf.run_subworkflow(Opt.module_optim)

        if not Opt.module_post:
            shutil.copy(mi.get_tooloutput_file_path(Opt.module_optim[-1]),
                        cpacs_path_out)

    # Run Post-optimisation workflow
    if Opt.module_post:

        if Opt.module_optim:
            wkf.copy_module_to_module(Opt.module_optim[-1], 'out',
                                      Opt.module_post[0], 'in')
        elif Opt.module_pre:
            wkf.copy_module_to_module(Opt.module_pre[-1], 'out',
                                      Opt.module_post[0], 'in')
        else:
            wkf.copy_module_to_module('WorkflowCreator', 'in',
                                      Opt.module_post[0], 'in')

        # wkf.copy_module_to_module('CPACSUpdater','out',Opt.module_post[0],'in')  usefuel?
        wkf.run_subworkflow(Opt.module_post)
        shutil.copy(mi.get_tooloutput_file_path(Opt.module_post[-1]),
                    cpacs_path_out)
Exemplo n.º 10
0
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
Exemplo n.º 11
0
    # Settings: 'SettingsGUI'
    # Geometry and mesh: 'CPACSCreator','CPACS2SUMO','SUMOAutoMesh'
    # Weight and balance: 'WeightConventional','WeightUnconventional','BalanceConventional','BalanceUnconventional'
    # Aerodynamics: 'CLCalculator','PyTornado','SkinFriction','PlotAeroCoefficients','SU2Run'
    # Mission analysis: 'Range'

    # Copy-Paste the module you want to execute, they will be run in order
    module_pre = ['SettingsGUI','WeightConventional','SkinFriction','CLCalculator','Range']

    module_optim = ['WeightConventional','PyTornado']
    module_post = []
    optim =  True
    doe = False

    # Pre Workflow (only run once)------------------------
    wkf.run_subworkflow(module_pre,cpacs_path)

    # Optim Workflow ------------------------------------
    if optim:
        wkf.copy_module_to_module(module_pre[-1],'out','CPACSUpdater','in')
        #run_optimizer()
        optimize(module_optim)

        # Post optim -------------------------------
        if module_post:
            wkf.copy_module_to_module('CPACSUpdater','out',module_post[0],'in')
            wkf.run_subworkflow(module_post)
            shutil.copy(mi.get_tooloutput_file_path(module_post[-1]),cpacs_path_out)
        else:
            shutil.copy(mi.get_tooloutput_file_path(module_optim[-1]),cpacs_path_out)