コード例 #1
0
ファイル: smuse.py プロジェクト: mldmnn/CEASIOMpy
def aeromap_calculation(sm, tixi):
    """Make a prediction using only the aeromap entries.

    By using only the aeromap functions this module is way faster to execute. Only
    works  with the aeromap as the other values of the CPACs are not vectors and
    have to be evaluated with a different CPACS every time.

    Args:
        sm (Surrogate model object): Surrogate used to predict the function output.
        tixi (Tixi handle): Handle of the current CPACS.

    Returns:
        None.

    """

    tigl = cpsf.open_tigl(tixi)
    aircraft = cpud.get_aircraft(tigl)
    wings = aircraft.get_wings()
    fuselage = aircraft.get_fuselages().get_fuselage(1)

    aeromap_uid = apmf.get_current_aeromap_uid(tixi, 'SMUse')
    log.info('Using aeromap :'+aeromap_uid)
    Coef = apmf.get_aeromap(tixi, aeromap_uid)

    inputs = np.array([Coef.alt, Coef.mach, Coef.aoa, Coef.aos]).T

    outputs = sm.predict_values(inputs)

    # Re-initiates the values of the results to write the new ones
    Coef.cl = []
    Coef.cd = []
    Coef.cs = []
    Coef.cml = []
    Coef.cmd = []
    Coef.cms = []

    for i in range(outputs.shape[0]):
        Coef.add_coefficients(outputs[i, 0], outputs[i, 1], outputs[i, 2],
                              outputs[i, 3], outputs[i, 4], outputs[i, 5])
    Coef.print_coef_list()
    apmf.save_coefficients(tixi, aeromap_uid, Coef)

    tigl.close()
コード例 #2
0
ファイル: skinfriction.py プロジェクト: mldmnn/CEASIOMpy
def add_skin_friction(cpacs_path, cpacs_out_path):
    """ Function to add the skin frinction drag coeffienct to aerodynamic coefficients

    Function 'add_skin_friction' add the skin friction drag 'cd0' to  the
    SU2 and pyTornado aeroMap, if their UID is not geven, it will add skin
    friction to all aeroMap. For each aeroMap it creates a new aeroMap where
    the skin friction drag coeffienct is added with the correct projcetions.

    Args:
        cpacs_path (str):  Path to CPACS file
        cpacs_out_path (str): Path to CPACS output file
    """

    tixi = cpsf.open_tixi(cpacs_path)
    tigl = cpsf.open_tigl(tixi)

    wing_area_max, wing_span_max = get_largest_wing_dim(tixi, tigl)

    analyses_xpath = '/cpacs/toolspecific/CEASIOMpy/geometry/analysis'

    # Requiered input data from CPACS
    wetted_area = cpsf.get_value(tixi, analyses_xpath + '/wettedArea')

    # Wing area/span, default values will be calated if no value found in the CPACS file
    wing_area_xpath = analyses_xpath + '/wingArea'
    wing_area = cpsf.get_value_or_default(tixi, wing_area_xpath, wing_area_max)
    wing_span_xpath = analyses_xpath + '/wingSpan'
    wing_span = cpsf.get_value_or_default(tixi, wing_span_xpath, wing_span_max)

    aeromap_uid_list = []

    # Try to get aeroMapToCalculate
    aeroMap_to_clculate_xpath = SF_XPATH + '/aeroMapToCalculate'
    if tixi.checkElement(aeroMap_to_clculate_xpath):
        aeromap_uid_list = cpsf.get_string_vector(tixi,
                                                  aeroMap_to_clculate_xpath)
    else:
        aeromap_uid_list = []

    # If no aeroMap in aeroMapToCalculate, get all existing aeroMap
    if len(aeromap_uid_list) == 0:
        try:
            aeromap_uid_list = apmf.get_aeromap_uid_list(tixi)
        except:
            raise ValueError(
                'No aeroMap has been found in this CPACS file, skin friction cannot be added!'
            )

    # Get unique aeroMap list
    aeromap_uid_list = list(set(aeromap_uid_list))
    new_aeromap_uid_list = []

    # Add skin friction to all listed aeroMap
    for aeromap_uid in aeromap_uid_list:

        log.info('adding skin friction coefficients to: ' + aeromap_uid)

        # Get orignial aeroPerformanceMap
        AeroCoef = apmf.get_aeromap(tixi, aeromap_uid)
        AeroCoef.complete_with_zeros()

        # Create new aeroCoefficient object to store coef with added skin friction
        AeroCoefSF = apmf.AeroCoefficient()
        AeroCoefSF.alt = AeroCoef.alt
        AeroCoefSF.mach = AeroCoef.mach
        AeroCoefSF.aoa = AeroCoef.aoa
        AeroCoefSF.aos = AeroCoef.aos

        # Iterate over all cases
        case_count = AeroCoef.get_count()
        for case in range(case_count):

            # Get parameters for this case
            alt = AeroCoef.alt[case]
            mach = AeroCoef.mach[case]
            aoa = AeroCoef.aoa[case]
            aos = AeroCoef.aos[case]

            # Calculate Cd0 for this case
            cd0 = estimate_skin_friction_coef(wetted_area,wing_area,wing_span, \
                                              mach,alt)

            # Projection of cd0 on cl, cd and cs axis
            #TODO: Should Cd0 be projected or not???
            aoa_rad = math.radians(aoa)
            aos_rad = math.radians(aos)
            cd0_cl = cd0 * math.sin(aoa_rad)
            cd0_cd = cd0 * math.cos(aoa_rad) * math.cos(aos_rad)
            cd0_cs = cd0 * math.sin(aos_rad)

            # Update aerodynamic coefficients
            cl = AeroCoef.cl[case] + cd0_cl
            cd = AeroCoef.cd[case] + cd0_cd
            cs = AeroCoef.cs[case] + cd0_cs

            # Shoud we change something? e.i. if a force is not apply at aero center...?
            if len(AeroCoef.cml):
                cml = AeroCoef.cml[case]
            else:
                cml = 0.0  # Shoud be change, just to test pyTornado
            if len(AeroCoef.cmd):
                cmd = AeroCoef.cmd[case]
            else:
                cmd = 0.0
            if len(AeroCoef.cms):
                cms = AeroCoef.cms[case]
            else:
                cms = 0.0

            # Add new coefficients into the aeroCoefficient object
            AeroCoefSF.add_coefficients(cl, cd, cs, cml, cmd, cms)

        # Create new aeroMap UID
        aeromap_sf_uid = aeromap_uid + '_SkinFriction'
        new_aeromap_uid_list.append(aeromap_sf_uid)

        # Create new description
        description_xpath = tixi.uIDGetXPath(aeromap_uid) + '/description'
        sf_description = cpsf.get_value(
            tixi,
            description_xpath) + ' Skin friction has been add to this AeroMap.'
        apmf.create_empty_aeromap(tixi, aeromap_sf_uid, sf_description)

        # Save aeroCoefficient object Coef in the CPACS file
        apmf.save_parameters(tixi, aeromap_sf_uid, AeroCoefSF)
        apmf.save_coefficients(tixi, aeromap_sf_uid, AeroCoefSF)

    # Get aeroMap list to plot
    plot_xpath = '/cpacs/toolspecific/CEASIOMpy/aerodynamics/plotAeroCoefficient'
    aeromap_to_plot_xpath = plot_xpath + '/aeroMapToPlot'

    if tixi.checkElement(aeromap_to_plot_xpath):
        aeromap_uid_list = cpsf.get_string_vector(tixi, aeromap_to_plot_xpath)
        new_aeromap_to_plot = aeromap_uid_list + new_aeromap_uid_list
        new_aeromap_to_plot = list(set(new_aeromap_to_plot))
        cpsf.add_string_vector(tixi, aeromap_to_plot_xpath,
                               new_aeromap_to_plot)
    else:
        cpsf.create_branch(tixi, aeromap_to_plot_xpath)
        cpsf.add_string_vector(tixi, aeromap_to_plot_xpath,
                               new_aeromap_uid_list)

    log.info('AeroMap "' + aeromap_uid + '" has been added to the CPACS file')

    cpsf.close_tixi(tixi, cpacs_out_path)
コード例 #3
0
def get_su2_results(cpacs_path, cpacs_out_path, wkdir):
    """ Function to write SU2 results in a CPACS file.

    Function 'get_su2_results' get available results from the latest SU2
    calculation and put it at the correct place in the CPACS file.

    '/cpacs/vehicles/aircraft/model/analyses/aeroPerformance/aerpMap[n]/aeroPerformanceMap'

    Args:
        cpacs_path (str): Path to input CPACS file
        cpacs_out_path (str): Path to output CPACS file
        wkdir (str): Path to the working directory

    """

    tixi = cpsf.open_tixi(cpacs_path)

    # TODO Check and reactivate that
    # save_timestamp(tixi,SU2_XPATH) <-- ceaf.replace by get get_execution_date()

    if not os.path.exists(wkdir):
        raise OSError('The working directory : ' + wkdir + 'does not exit!')

    os.chdir(wkdir)
    dir_list = os.listdir(wkdir)

    # Get and save Wetted area
    wetted_area = get_wetted_area(wkdir)

    wetted_area_xpath = '/cpacs/toolspecific/CEASIOMpy/geometry/analysis/wettedArea'
    cpsf.create_branch(tixi, wetted_area_xpath)

    tixi.updateDoubleElement(wetted_area_xpath, wetted_area, '%g')

    # Get and save CL/CD ratio
    fixed_cl_xpath = SU2_XPATH + '/fixedCL'
    fixed_cl = cpsf.get_value_or_default(tixi, fixed_cl_xpath, 'NO')
    # TODO
    # if fixed_cl == 'YES':
    # find force_file_name = 'forces_breakdown.dat'
    # cl_cd = get_efficiency(force_path)
    # lDRatio_xpath = '/cpacs/toolspecific/CEASIOMpy/ranges/lDRatio' # TODO: probalby change xpath and name
    # cpsf.create_branch(tixi, lDRatio_xpath)
    # tixi.updateDoubleElement(lDRatio_xpath,cl_cd,'%g')

    # Save aeroPerformanceMap
    su2_aeromap_xpath = SU2_XPATH + '/aeroMapUID'
    aeromap_uid = cpsf.get_value(tixi, su2_aeromap_xpath)

    # Check if loads shoud be extracted
    check_extract_loads_xpath = SU2_XPATH + '/results/extractLoads'
    check_extract_loads = cpsf.get_value_or_default(tixi,
                                                    check_extract_loads_xpath,
                                                    False)

    # Create an oject to store the aerodynamic coefficients
    apmf.check_aeromap(tixi, aeromap_uid)

    # TODO: create a function to earase previous results...
    Coef2 = apmf.get_aeromap(tixi, aeromap_uid)
    Coef = apmf.AeroCoefficient()
    Coef.alt = Coef2.alt
    Coef.mach = Coef2.mach
    Coef.aoa = Coef2.aoa
    Coef.aos = Coef2.aos

    case_dir_list = [dir for dir in dir_list if 'Case' in dir]

    for config_dir in sorted(case_dir_list):
        if os.path.isdir(config_dir):
            os.chdir(config_dir)
            force_file_name = 'forces_breakdown.dat'
            if not os.path.isfile(force_file_name):
                raise OSError('No result force file have been found!')

            # Read result file
            with open(force_file_name) as f:
                for line in f.readlines():
                    if 'Total CL:' in line:
                        cl = float(line.split(':')[1].split('|')[0])
                    if 'Total CD:' in line:
                        cd = float(line.split(':')[1].split('|')[0])
                    if 'Total CSF:' in line:
                        cs = float(line.split(':')[1].split('|')[0])
                    # TODO: Check which axis name corespond to waht: cml, cmd, cms
                    if 'Total CMx:' in line:
                        cmd = float(line.split(':')[1].split('|')[0])
                    if 'Total CMy:' in line:
                        cms = float(line.split(':')[1].split('|')[0])
                    if 'Total CMz:' in line:
                        cml = float(line.split(':')[1].split('|')[0])
                    if ('Free-stream velocity' in line and 'm/s' in line):
                        velocity = float(line.split(' ')[7])

            # Damping derivatives
            rotation_rate_xpath = SU2_XPATH + '/options/rotationRate'
            rotation_rate = cpsf.get_value_or_default(tixi,
                                                      rotation_rate_xpath, 1.0)
            ref_xpath = '/cpacs/vehicles/aircraft/model/reference'
            ref_len = cpsf.get_value(tixi, ref_xpath + '/length')
            adim_rot_rate = rotation_rate * ref_len / velocity

            if '_dp' in config_dir:
                dcl = (cl - Coef.cl[-1]) / adim_rot_rate
                dcd = (cd - Coef.cd[-1]) / adim_rot_rate
                dcs = (cs - Coef.cs[-1]) / adim_rot_rate
                dcml = (cml - Coef.cml[-1]) / adim_rot_rate
                dcmd = (cmd - Coef.cmd[-1]) / adim_rot_rate
                dcms = (cms - Coef.cms[-1]) / adim_rot_rate
                Coef.damping_derivatives.add_damping_der_coef(
                    dcl, dcd, dcs, dcml, dcmd, dcms, '_dp')

            elif '_dq' in config_dir:
                dcl = (cl - Coef.cl[-1]) / adim_rot_rate
                dcd = (cd - Coef.cd[-1]) / adim_rot_rate
                dcs = (cs - Coef.cs[-1]) / adim_rot_rate
                dcml = (cml - Coef.cml[-1]) / adim_rot_rate
                dcmd = (cmd - Coef.cmd[-1]) / adim_rot_rate
                dcms = (cms - Coef.cms[-1]) / adim_rot_rate
                Coef.damping_derivatives.add_damping_der_coef(
                    dcl, dcd, dcs, dcml, dcmd, dcms, '_dq')

            elif '_dr' in config_dir:
                dcl = (cl - Coef.cl[-1]) / adim_rot_rate
                dcd = (cd - Coef.cd[-1]) / adim_rot_rate
                dcs = (cs - Coef.cs[-1]) / adim_rot_rate
                dcml = (cml - Coef.cml[-1]) / adim_rot_rate
                dcmd = (cmd - Coef.cmd[-1]) / adim_rot_rate
                dcms = (cms - Coef.cms[-1]) / adim_rot_rate
                Coef.damping_derivatives.add_damping_der_coef(
                    dcl, dcd, dcs, dcml, dcmd, dcms, '_dr')

            elif '_TED_' in config_dir:

                config_dir_split = config_dir.split('_')
                ted_idx = config_dir_split.index('TED')
                ted_uid = config_dir_split[ted_idx + 1]
                defl_angle = float(config_dir.split('_defl')[1])

                try:
                    print(Coef.IncrMap.dcl)
                except AttributeError:
                    Coef.IncrMap = apmf.IncrementMap(ted_uid)

                # TODO: still in development, for now only 1 ted and 1 defl
                print(ted_uid, defl_angle)

                dcl = (cl - Coef.cl[-1])
                dcd = (cd - Coef.cd[-1])
                dcs = (cs - Coef.cs[-1])
                dcml = (cml - Coef.cml[-1])
                dcmd = (cmd - Coef.cmd[-1])
                dcms = (cms - Coef.cms[-1])

                control_parameter = -1

                Coef.IncrMap.add_cs_coef(dcl, dcd, dcs, dcml, dcmd, dcms,
                                         ted_uid, control_parameter)

            else:  # No damping derivative or control surfaces case
                Coef.add_coefficients(cl, cd, cs, cml, cmd, cms)

            if check_extract_loads:
                results_files_dir = os.path.join(wkdir, config_dir)
                extract_loads(results_files_dir)

            os.chdir(wkdir)

    # Save object Coef in the CPACS file
    apmf.save_coefficients(tixi, aeromap_uid, Coef)

    cpsf.close_tixi(tixi, cpacs_out_path)