Exemple #1
0
def get_vertical_profile_xy(filepath, name, time, xloc, zloc):
    """Return the profile along the y direction at given x and z locations.

    The function loads the solution of from the volume probe at a given time.
    The solution is first interpolated along the z direction and then,
    along the x direction.

    Parameters
    ----------
    filepath : pathlib.Path
        Path of the file with the volume probe data.
    name : str
        Name of the variable to load.
    time : float
        Time value at which to load the data.
    xloc : float
        Location along the x direction at which to interpolate data.
    zloc : float
        Location along the z direction at which to interpolate data.

    Returns
    -------
    numpy.ndarray
        y locations along the profile as a 1D array of floats.
    numpy.ndarray
        Values of the interpolated variable as a 1D array of floats.

    """
    probe = petibmpy.ProbeVolume(name, name)
    (x, y, z), u = probe.read_hdf5(filepath, time)
    u = petibmpy.linear_interpolation(u, z, zloc)
    u = numpy.swapaxes(u, 0, 1)
    u = petibmpy.linear_interpolation(u, x, xloc)
    assert y.size == u.size
    return y, u
def get_field_slices_2d(grid, field, xlocs):
    x, y, z = grid
    # Interpolate field at given x-locations.
    field_slices = []
    field = numpy.swapaxes(field, 0, -1)  # swap z and x axes
    for xloc in xlocs:
        field_slice = petibmpy.linear_interpolation(field, x, xloc)
        field_slices.append(field_slice)
    # Return the y and z gridline coordinates
    # and the slices of the field solution.
    return (y, z), field_slices
Exemple #3
0
 def test_linear_interpolation(self):
     """Test the function to do linear interpolation along first axis."""
     nz = numpy.shape(self.values)[0]
     z = numpy.linspace(0.0, 1.0, num=nz)
     i = -2
     # Exact location.
     values = petibmpy.linear_interpolation(self.values, z, z[i])
     self.assertTrue(numpy.allclose(values, self.values[i]))
     # Mid location.
     zi = 0.5 * (z[i - 1] + z[i])
     values = petibmpy.linear_interpolation(self.values, z, zi)
     self.assertTrue(
         numpy.allclose(values,
                        0.5 * (self.values[i - 1] + self.values[i])))
     # Compare results to scipy.interpolate.interp1d.
     nx = 100
     x = numpy.linspace(0.0, 1.0, num=nx)
     values = numpy.random.rand(nx)
     xi = random.random()
     v1 = petibmpy.linear_interpolation(values, x, xi)
     f = interpolate.interp1d(x, values)
     self.assertAlmostEqual(v1, f([xi])[0], places=12)
# ----------------------------------------------------------------------------
# Plot the 2D slice of the streamwise vorticity in the y/z plane at x/c = 0.3.
# ----------------------------------------------------------------------------

# Load the gridline coordinates from file.
filepath = datadir / 'grid.h5'
x, y, z = petibmpy.read_grid_hdf5(filepath, 'wx')

# Load the field solution from file.
filepath = datadir / '{:0>7}.h5'.format(timestep)
wx = petibmpy.read_field_hdf5(filepath, 'wx')

# Interpolate the vorticity component in the y/z plane in the near wake.
xloc = 0.3  # location along the x direction (near wake)
wx_tmp = numpy.swapaxes(wx, -1, 0)
wx_xloc = petibmpy.linear_interpolation(wx_tmp, x, xloc)

# Create the Matplotlib figure.
fig, ax = pyplot.subplots(figsize=(4.0, 4.0))
ax.set_xlabel('z/c')
ax.set_ylabel('y/c')

# Add wing to the plot.
ax.add_patch(
    patches.Ellipse((0.0, 0.0),
                    config.S,
                    config.c * numpy.sin(theta),
                    edgecolor='C0',
                    linewidth=3.0,
                    facecolor='gray',
                    alpha=0.5))
    adir = rootdir / 'data'
    filepath = adir / 'ghia_et_al_1982_lid_driven_cavity.dat'
    yu_g, uc_g, xv_g, vc_g = load_data_ghia_et_al_1982(filepath, str(Re))

    # Load gridlines and velocity fields.
    simudir = maindir / f'liddrivencavity2dRe{Re}'
    datadir = simudir / 'output'
    filepath = datadir / 'grid.h5'
    xu, yu = petibmpy.read_grid_hdf5(filepath, 'u')
    xv, yv = petibmpy.read_grid_hdf5(filepath, 'v')
    filepath = datadir / f'{timestep:0>7}.h5'
    u = petibmpy.read_field_hdf5(filepath, 'u')
    v = petibmpy.read_field_hdf5(filepath, 'v')

    # Interpolate solution at centerlines of the cavity.
    uc = petibmpy.linear_interpolation(u.T, xu, 0.5)
    vc = petibmpy.linear_interpolation(v, yv, 0.5)

    # Plot the velocity profiles.
    ax1.plot(yu, uc, label=f'Re = {Re}')
    label = ('Ghia et al. (1982)' if Re == Res[-1] else None)
    ax1.plot(yu_g,
             uc_g,
             label=label,
             linewidth=0,
             color='black',
             marker='o',
             markerfacecolor='none')
    ax2.plot(xv, vc, label=f'Re = {Re}')
    ax2.plot(xv_g,
             vc_g,
Exemple #6
0
    # Load information about the text annotations to add to figures.
    filepath = pathlib.Path(__file__).parent / 'wx_slices_annotations.yaml'
    with open(filepath, 'r') as infile:
        annot = yaml.safe_load(infile)['xlocs']

# Loop over the slices to compute and plot.
for xloc in xlocs:
    print(f'[xloc = {xloc}] Computing and plotting wx slice ...')

    # Initialize figure.
    fig = pyplot.figure(figsize=(width / scale, height / scale),
                        dpi=300, frameon=False)
    ax = pyplot.Axes(fig, [0.0, 0.0, 1.0, 1.0])

    # Interpolate the 3D vorticity compenent at given x location.
    wx_xloc = petibmpy.linear_interpolation(wx, x, xloc)

    # Represent the wing on the figure.
    ax.add_patch(patches.Ellipse((0.0, 0.0),
                                 config.S, config.c * numpy.sin(theta),
                                 edgecolor='black', facecolor='gray',
                                 alpha=0.5))

    # Add the contours of the vorticity component.
    contourf_levels = numpy.linspace(-5.0, 5.0, num=50)
    contour_levels = numpy.linspace(-5.0, 5.0, num=10)
    ax.contourf(z - config.S / 2, y, wx_xloc.T,
                levels=contourf_levels, extend='both', cmap='viridis')
    ax.contour(z - config.S / 2, y, wx_xloc.T,
               levels=contour_levels, linewidths=0.25, colors='black')
filepath = datadir / 'probe-p.h5'
probe = petibmpy.ProbeVolume(name, name)
(x, y), p = probe.read_hdf5(filepath, time)

# Define circle outside support region of delta function.
dx = 1.5 / 90  # grid-spacing in the uniform region
R = 0.5 + 3 * dx  # radius 3 cells away from real boundary
nb = 100
theta = numpy.linspace(0.0, 2 * numpy.pi, num=nb + 1)[:-1]
xc, yc = 0.0, 0.0
xb, yb = xc + R * numpy.cos(theta), yc + R * numpy.sin(theta)

# Interpolate the field on extended boundary.
pb = numpy.empty_like(xb)
for i, (xbi, ybi) in enumerate(zip(xb, yb)):
    pi = petibmpy.linear_interpolation(p, y, ybi)
    pb[i] = petibmpy.linear_interpolation(pi, x, xbi)

# Compute the pressure coefficient.
rho = 1.0  # fluid density
U_inf = 1.0  # freestream speed
p_inf = 0.0  # far-away pressure
cp = (pb - p_inf) / (0.5 * rho * U_inf**2)

# Re-arrange values to split apart lower and upper surfaces.
cp_lower = numpy.append(cp[nb // 2:], [cp[-1]])
theta_lower = numpy.linspace(0.0, 180.0, num=cp_lower.size)
cp_upper = cp[:nb // 2 + 1][::-1]
theta_upper = numpy.linspace(0.0, 180.0, num=cp_upper.size)

# Load digitized values from Li et al. (2016).
pyplot.rc('font', family='serif', size=12)
nrows, ncols = 3, 2
fig, ax = pyplot.subplots(nrows=nrows, ncols=ncols, figsize=(10.0, 10.0))
phases = [180, 210, 330]  # phase angles (in degrees)
times = [17.5, 17.916, 19.584]  # corresponding time units
x_locs = [-0.6, 0.0, 0.6, 1.2]  # cross-section locations
for row in range(nrows):
    for col in range(ncols):
        name = 'u' if col == 0 else 'v'
        time, phase = times[row], phases[row]
        filepath = datadir / f'probe{phase}-{name}.h5'
        for xi in x_locs:
            probe = petibmpy.ProbeVolume(name, name)
            (x, y), u = probe.read_hdf5(filepath, time)
            ui = petibmpy.linear_interpolation(u.T, x, xi)
            ax[row, col].plot(ui / Um, y / D, label=f'{xi}')
        ax[row, col].set_xlabel('$u / U_m$' if col == 0 else '$v / U_m$')
        ax[row, col].set_ylabel('$y / D$')
        ax[row, col].set_xlim(-1.5, 1.5)
        ax[row, col].set_ylim(-1.1, 1.1)
        ax[row, col].text(0.7, 0.8, r'$\phi = {}^o$'.format(phases[row]))
ax[0, 1].legend(title='$x/D$', loc='center left',
                prop={'size': 12}, frameon=False)
fig.tight_layout()

# Save the figure.
figdir = simudir / 'figures'
figdir.mkdir(parents=True, exist_ok=True)
filepath = figdir / 'velocity_profiles.png'
fig.savefig(filepath, dpi=300, bbox_inches='tight')