示例#1
0
def test_expected_values_every_tenth_of_a_degree():
    r"""Test against expected values"""
    os.chdir(xfoil_exe_dir)

    foil = 's1010.dat'
    reynolds = 10000
    # Compute every tenth of degree
    polar = Polar(foil_data_folder=None,
                  filename=XFOIL_EXE_TO_DAT_RELPATH % foil,
                  angles_of_attack_spec=[0., 10., 0.1],
                  reynolds_number=reynolds,
                  ncrit=2.,
                  iterlim=200,
                  use_precomputed_data=False)
    polar.compute()
    tolerance_default = 1e-5
    max_lift_to_drag_value, max_lift_to_drag_angle = polar.max_lift_to_drag
    max_lift_value, max_lift_angle = polar.maximum_lift
    min_drag_value, min_drag_angle = polar.minimum_drag
    assert abs(max_lift_to_drag_value - 10.540968764146672) < tolerance_default
    assert abs(max_lift_to_drag_angle - 4.6) < tolerance_default
    assert abs(max_lift_value - 0.66379999999999995) < 1e-3

    # Due to interpolation (PChip), the max lift angle lies at one of the
    # computed angles
    assert abs(max_lift_angle - 9.1) < tolerance_default

    assert abs(min_drag_value - 0.032079999999999997) < tolerance_default

    # Minimum drag may occur close to but not at 0 degree
    assert abs(min_drag_angle) < 0.1 + tolerance_default

    os.chdir(cwd)
示例#2
0
def test_expected_values_every_degree():
    r"""Test against expected values"""
    os.chdir(xfoil_exe_dir)

    foil = 's1010.dat'
    reynolds = 10000
    polar = Polar(foil_data_folder=None,
                  filename=XFOIL_EXE_TO_DAT_RELPATH % foil,
                  angles_of_attack_spec=[0., 10., 1.],
                  reynolds_number=reynolds,
                  ncrit=2.,
                  iterlim=200,
                  use_precomputed_data=False)
    polar.compute()
    tolerance_default = 1e-5
    max_lift_to_drag_value, max_lift_to_drag_angle = polar.max_lift_to_drag
    max_lift_value, max_lift_angle = polar.maximum_lift
    min_drag_value, min_drag_angle = polar.minimum_drag
    assert abs(max_lift_to_drag_value - 10.534996828082047) < 1e-2
    assert abs(max_lift_to_drag_angle - 5.) < tolerance_default
    assert abs(max_lift_value - 0.66110000000000002) < 1e-3

    # Due to interpolation (PChip), the max lift angle lies at one of the
    # computed angles
    assert abs(max_lift_angle - 9.) < tolerance_default

    assert abs(min_drag_value - 0.032079999999999997) < 1e-4
    assert abs(min_drag_angle) < tolerance_default

    os.chdir(cwd)
示例#3
0
def test_polar_vs_polar_matrix():
    r"""A matrix is made using 3 times the same reynolds number
    and twice the same ncrit

    The averaged results of the matrix should be the same as the results
    of a polar for the same reynolds and ncrit

    """
    os.chdir(xfoil_exe_dir)

    foil = 's1010.dat'
    aos_spec = [0., 10., 1.]
    reynolds = 10000
    ncrit = 2.
    polar = Polar("",
                  filename=XFOIL_EXE_TO_DAT_RELPATH % foil,
                  angles_of_attack_spec=aos_spec,
                  reynolds_number=reynolds,
                  ncrit=ncrit)
    polar_matrix = PolarMatrix("",
                               filename=XFOIL_EXE_TO_DAT_RELPATH % foil,
                               angles_of_attack_spec=aos_spec,
                               reynolds_numbers=[reynolds, reynolds, reynolds],
                               ncrits=[ncrit, ncrit])
    polar.compute()
    polar_matrix.compute()

    tolerance = 1e-5

    polar_max_lift_to_drag_value, polar_max_lift_to_drag_angle = \
        polar.max_lift_to_drag
    polar_matrix_max_lift_to_drag_value, polar_matrix_max_lift_to_drag_angle = \
        polar_matrix.avg_max_lift_to_drag
    assert abs(polar_max_lift_to_drag_value -
               polar_matrix_max_lift_to_drag_value) < tolerance
    assert abs(polar_max_lift_to_drag_angle -
               polar_matrix_max_lift_to_drag_angle) < tolerance

    polar_max_lift_value, polar_max_lift_angle = polar.maximum_lift
    polar_matrix_max_lift_value, polar_matrix_max_lift_angle = \
        polar_matrix.avg_max_lift
    assert abs(polar_max_lift_value - polar_matrix_max_lift_value) < tolerance
    assert abs(polar_max_lift_angle - polar_matrix_max_lift_angle) < tolerance

    polar_min_drag_value, polar_min_drag_angle = polar.minimum_drag
    polar_matrix_min_drag_value, polar_matrix_min_drag_angle = \
        polar_matrix.avg_min_drag
    assert abs(polar_min_drag_value - polar_matrix_min_drag_value) < tolerance
    assert abs(polar_min_drag_angle - polar_matrix_min_drag_angle) < tolerance

    os.chdir(cwd)
示例#4
0
def test_same_ncrits():
    r"""Make sure calls with the same parameters have the same results
    whether using precomputed data or not"""
    os.chdir(xfoil_exe_dir)

    foil = 'naca0006.dat'
    reynolds = 10000
    polar_1 = Polar("",
                    filename=XFOIL_EXE_TO_DAT_RELPATH % foil,
                    angles_of_attack_spec=[0., 10., 1.],
                    reynolds_number=reynolds,
                    ncrit=2.,
                    use_precomputed_data=False)
    polar_2 = Polar("../../foil_data",
                    filename=XFOIL_EXE_TO_DAT_RELPATH % foil,
                    angles_of_attack_spec=[0., 10., 1.],
                    reynolds_number=reynolds,
                    ncrit=2.,
                    use_precomputed_data=True)
    print(XFOIL_EXE_TO_DAT_RELPATH % foil)
    tolerance = 1e-3
    polar_1.compute()
    polar_2.compute()
    max_lift_to_drag_value_1, max_lift_to_drag_angle_1 = \
        polar_1.max_lift_to_drag
    max_lift_to_drag_value_2, max_lift_to_drag_angle_2 = \
        polar_2.max_lift_to_drag
    assert abs(max_lift_to_drag_value_1 - max_lift_to_drag_value_2) < tolerance
    assert abs(max_lift_to_drag_angle_1 - max_lift_to_drag_angle_2) < 5e-2
    assert abs(polar_1.maximum_lift - polar_2.maximum_lift) < 1e-3
    assert abs(polar_1.minimum_drag - polar_2.minimum_drag) < 1e-5

    os.chdir(cwd)
示例#5
0
def test_different_ncrits():
    r"""test call with different ncrits result in different values"""
    os.chdir(xfoil_exe_dir)

    foil = 's1010.dat'
    reynolds = 10000
    polar_1 = Polar("",
                    filename=XFOIL_EXE_TO_DAT_RELPATH % foil,
                    angles_of_attack_spec=[0., 10., 1.],
                    reynolds_number=reynolds,
                    ncrit=2.)
    polar_2 = Polar("",
                    filename=XFOIL_EXE_TO_DAT_RELPATH % foil,
                    angles_of_attack_spec=[0., 10., 1.],
                    reynolds_number=reynolds,
                    ncrit=3.)
    polar_1.compute()
    polar_2.compute()
    assert polar_1.max_lift_to_drag != polar_2.max_lift_to_drag
    assert polar_1.maximum_lift != polar_2.maximum_lift

    # Minimum drag seems to depend little on ncrit !
    # assert polar_1.minimum_drag != polar_2.minimum_drag

    os.chdir(cwd)
def section_analysis_example(foils_to_analyze):
    r"""Runs the example

    Parameters
    ----------
    foils_to_analyze : list[str]

    """
    # MATPLOTLIBRC = os.path.join(os.path.dirname(__file__),
    #                             'matplotlibrc_defaults')
    WORKING_DIRECTORY = os.path.join(os.path.dirname(__file__),
                                     '../../foilix/xfoil')
    XFOIL_EXE_TO_DAT_RELPATH = '../../foil_dat/%s'

    # Set the matplotlib rc_context from file
    # matplotlib.rc_context(fname=MATPLOTLIBRC)

    # Set the working directory to where the xfoil executable resides
    logger.info("Changing working directory to : %s" % WORKING_DIRECTORY)
    os.chdir(WORKING_DIRECTORY)

    # Analysis configuration
    angles_of_attack = [0, 10, 1]
    reynolds_number = 150000
    ncrit = 2.0

    foils_dat_files = [XFOIL_EXE_TO_DAT_RELPATH % f for f in foils_to_analyze]

    _, axarr = plt.subplots(2, 2)
    axarr[-1, -1].axis('off')  # hide bottom right subplot that is not used
    axarr[0, 0].set_title("Cl, Cd = f(angle)")
    axarr[0, 1].set_title("Cl = f(Cd)")
    axarr[1, 0].set_title("L/D = f(angle)")

    colors = ["red", "blue", "orange", "pink"]

    for i, dat_file in enumerate(foils_dat_files):
        # polar = foilix.xfoil.polar.Polar(foils_dat_files[0],
        #                                  angles_of_attack,
        #                                  reynolds_number,
        #                                  ncrit,
        # iterlim=100)
        polar = Polar(foil_data_folder="../../foil_data",
                      filename=dat_file,
                      angles_of_attack_spec=angles_of_attack,
                      reynolds_number=reynolds_number,
                      ncrit=ncrit,
                      iterlim=100,
                      use_precomputed_data=True)
        polar.compute()

        print("Max L/D : %f @ %f°" %
              (polar.max_lift_to_drag[0], polar.max_lift_to_drag[1]))
        print("%i angles computed out of %i "
              "specified" % (len(polar.angles_of_attack_computed),
                             len(polar.angles_of_attack_spec)))

        # Plot Cl, Cd = f(angle)
        axarr[0, 0].plot(polar.interp_aoas, [
            polar.coefficients_of_lift_interpolator(aoa)
            for aoa in polar.interp_aoas
        ],
                         'r-',
                         label="%s Cl" % os.path.basename(dat_file),
                         color=colors[i])
        axarr[0, 0].scatter(polar.angles_of_attack_computed,
                            polar.coefficients_of_lift,
                            color=colors[i])
        axarr[0, 0].plot(polar.interp_aoas, [
            polar.coefficients_of_drag_interpolator(aoa)
            for aoa in polar.interp_aoas
        ],
                         'r--',
                         label="%s Cd" % os.path.basename(dat_file),
                         color=colors[i])
        axarr[0, 0].scatter(polar.angles_of_attack_computed,
                            polar.coefficients_of_drag,
                            color=colors[i])
        axarr[0, 0].legend(loc="upper left")

        # Plot Cl = f(Cd)
        axarr[0, 1].plot([
            polar.coefficients_of_drag_interpolator(aoa)
            for aoa in polar.interp_aoas
        ], [
            polar.coefficients_of_lift_interpolator(aoa)
            for aoa in polar.interp_aoas
        ],
                         'r-',
                         label="%s Cl" % os.path.basename(dat_file),
                         color=colors[i])
        axarr[0, 1].scatter(polar.coefficients_of_drag,
                            polar.coefficients_of_lift,
                            color=colors[i])

        # Plot L/D = f(aoa)

        axarr[1, 0].plot(polar.interp_aoas, [
            polar.lift_to_drag_interpolator(aoa) for aoa in polar.interp_aoas
        ],
                         'r-',
                         label="%s L/D" % os.path.basename(dat_file),
                         color=colors[i])
        axarr[1, 0].scatter(polar.angles_of_attack_computed,
                            polar.lift_to_drag,
                            color=colors[i])

    plt.gcf().suptitle(
        str("%s reynolds; %s ncrit" % (str(reynolds_number), str(ncrit))))
    plt.show()
def main(parameterization):
    # Set the matplotlib rc_context from file
    MATPLOTLIBRC = join(dirname(__file__), 'matplotlibrc_defaults')
    matplotlib.rc_context(fname=MATPLOTLIBRC)

    # read global_best.data to rebuild foil
    if parameterization == "nurbs":
        global_best_data_file = "global_best_nurbs.data"
        global_best_dat_file = "global_best_nurbs.dat"
        if not isfile(join(getcwd(), global_best_data_file)):
            msg = "Cannot find global_best_nurbs.data in CWD"
            # logger.critical(msg)
            raise AssertionError(msg)
        foil = nurbs_foil()
    elif parameterization == "parsec":
        global_best_data_file = "global_best_parsec.data"
        global_best_dat_file = "global_best_parsec.dat"
        if not isfile(join(getcwd(), global_best_data_file)):
            msg = "Cannot find global_best_parsec.data in CWD"
            # logger.critical(msg)
            raise AssertionError(msg)
        foil = parsec_foil()
    else:
        raise ValueError("Parameterization should be nurbs or parsec")

    print(foil)

    foil.plot_foil("Global best %s foil" % parameterization)

    with open(join(getcwd(), global_best_dat_file), "w") as f:
        f.write("GB\n")
        f.write(foil.get_coords_plain())
    # print(foil.get_coords_plain())

    # compare against best of db digging

    WORKING_DIRECTORY = join(dirname(__file__), '../../foilix/xfoil')
    XFOIL_EXE_TO_DAT_RELPATH = '../foil_dat/%s'

    INITIAL_WORKDIR = getcwd()

    # Set the working directory to where the xfoil executable resides
    print("Changing working directory to : %s" % WORKING_DIRECTORY)
    chdir(WORKING_DIRECTORY)

    # Analysis configuration
    angles_of_attack = config["aoa_spec"]
    # use the average
    reynolds_number = sum(config["reynolds_numbers"]) / len(
        config["reynolds_numbers"])
    ncrit = sum(config["ncrits"]) / len(config["ncrits"])  # use the average

    # Foils to analyze

    # get the names of the best foils in db
    with open(join(INITIAL_WORKDIR, "data_digging.csv")) as f:
        lines = f.readlines()
    foils_to_analyze = [lines[i].split(",")[0] for i in range(8, 11)]

    # put the global best file in foil_data
    global_best_dat = join(INITIAL_WORKDIR, global_best_dat_file)
    shutil.copyfile(global_best_dat,
                    join(config["foil_dat_folder"], basename(global_best_dat)))

    foils_to_analyze.append(basename(global_best_dat))

    foils_dat_files = [XFOIL_EXE_TO_DAT_RELPATH % f for f in foils_to_analyze]

    f, axarr = plt.subplots(2, 2)
    axarr[-1, -1].axis('off')  # hide bottom right subplot that is not used
    axarr[0, 0].set_title("Cl, Cd = f(angle)")
    axarr[0, 1].set_title("Cl = f(Cd)")
    axarr[1, 0].set_title("L/D = f(angle)")

    colors = ["red", "orange", "pink", "blue"]

    for i, dat_file in enumerate(foils_dat_files):
        print("**** analyzing : %s ****" % dat_file)
        try:
            polar = Polar(config["foil_data_folder"],
                          dat_file,
                          angles_of_attack,
                          reynolds_number,
                          ncrit,
                          iterlim=100,
                          use_precomputed_data=True)
            polar.compute()
        except FileNotFoundError:
            print("CWD : %s" % getcwd())
            print("dat_file : %s" % dat_file)
            polar = Polar("",
                          dat_file,
                          angles_of_attack,
                          reynolds_number,
                          ncrit,
                          iterlim=100,
                          use_precomputed_data=False)
            polar.compute()

        print("Max L/D : %f @ %f°" %
              (polar.max_lift_to_drag[0], polar.max_lift_to_drag[1]))
        print("%i angles computed out of %i specified" %
              (len(polar.angles_of_attack_computed),
               len(polar.angles_of_attack_spec)))

        # Plot Cl, Cd = f(angle)
        axarr[0, 0].plot(polar.interp_aoas, [
            polar.coefficients_of_lift_interpolator(aoa)
            for aoa in polar.interp_aoas
        ],
                         'r-',
                         label="%s" % basename(dat_file),
                         color=colors[i])
        axarr[0, 0].scatter(polar.angles_of_attack_computed,
                            polar.coefficients_of_lift,
                            color=colors[i])
        axarr[0, 0].plot(polar.interp_aoas, [
            polar.coefficients_of_drag_interpolator(aoa)
            for aoa in polar.interp_aoas
        ],
                         'r--',
                         color=colors[i])
        axarr[0, 0].scatter(polar.angles_of_attack_computed,
                            polar.coefficients_of_drag,
                            color=colors[i])
        leg = axarr[0, 0].legend(loc="upper left")
        if leg:
            leg.draggable()

        # Plot Cl = f(Cd)
        axarr[0, 1].plot([
            polar.coefficients_of_drag_interpolator(aoa)
            for aoa in polar.interp_aoas
        ], [
            polar.coefficients_of_lift_interpolator(aoa)
            for aoa in polar.interp_aoas
        ],
                         'r-',
                         label="%s Cl" % basename(dat_file),
                         color=colors[i])
        axarr[0, 1].scatter(polar.coefficients_of_drag,
                            polar.coefficients_of_lift,
                            color=colors[i])

        # Plot L/D = f(aoa)
        axarr[1, 0].plot(polar.interp_aoas, [
            polar.lift_to_drag_interpolator(aoa) for aoa in polar.interp_aoas
        ],
                         'r-',
                         label="%s L/D" % basename(dat_file),
                         color=colors[i])
        axarr[1, 0].scatter(polar.angles_of_attack_computed,
                            polar.lift_to_drag,
                            color=colors[i])

    plt.gcf().suptitle(
        str("%s reynolds; %s ncrit" % (str(reynolds_number), str(ncrit))))
    plt.show()
    os.remove(join(config["foil_dat_folder"], basename(global_best_dat)))

    print("Changing working directory back to : %s" % INITIAL_WORKDIR)
    chdir(INITIAL_WORKDIR)
示例#8
0
#!/usr/bin/python
# coding: utf-8
r"""Polar example
"""

from __future__ import print_function

from foilix.xfoil.polar import Polar

reynolds_number = [2.5e4, 3.5e4, 3.5e4, 4.5e4]
ncrits = [2.5, 3.]

polar = Polar(foil_data_folder="../../foil_data",
              filename="gf_0001.dat",
              angles_of_attack_spec=[0, 7, 1],
              reynolds_number=2.5e4,
              ncrit=3.,
              use_precomputed_data=True)
polar.compute()

print("--interp_aoas--")
print(polar.interp_aoas)
print("--warnings--")
print(polar.warnings)
print("--angles_of_attack_computed--")
print(polar.angles_of_attack_computed)
print("--coefficients_of_lift--")
print(polar.coefficients_of_lift)
print("--coefficients_of_drag--")
print(polar.coefficients_of_drag)
print("--lift_to_drag--")