def get_CD_CL(self, filepath, bodypath): """Load the forces from file and return the force coefficients.""" t, fx, fy = petibmpy.read_forces(filepath) # Non-dimensionalize time by the period. t_nodim = self.f * t # Reverse sign of force in x-direction when the body moving # in the positive x-direction. U0, _ = self.translational_velocity(t) fx *= -U0 / numpy.abs(U0) # Normalize the forces by the maximum quasi-steady force on the wing. D, L = [], [] x0 = petibmpy.read_body(bodypath, skiprows=1, usecols=0) y0 = numpy.zeros_like(x0) for ti in t: alpha = self.orientation_angle(ti) x, y = petibmpy.rotate2d(x0, y0, center=(0.0, 0.0), angle=alpha, mode='rad') Di, Li = self.quasi_steady_forces(ti, x, y, 0.0, 0.0, rho=1.0) D.append(numpy.max(Di)) L.append(numpy.max(Li)) D, L = numpy.array(D), numpy.array(L) cd, cl = fx / numpy.max(D), fy / numpy.max(L) return {'CD': (t_nodim, cd), 'CL': (t_nodim, cl)}
def test_read_body_2d(self): """Test function `read_body` for 2D configuration.""" filepath = self.datadir / 'body2d.txt' x, y = petibmpy.read_body(filepath, skiprows=1) with open(filepath, 'r') as infile: num = int(infile.readline()) self.assertEqual(x.size, num) self.assertEqual(y.size, num)
def test_read_write_body(self): """Test functions `read_body` and `write_body`.""" filepath = self.datadir / 'body.txt' coords = (self.x, self.y, self.z) for d in [2, 3]: petibmpy.write_body(filepath, *coords[:d]) coords2 = petibmpy.read_body(filepath, skiprows=1) self.assertEqual(len(coords2), d) for i, coord in enumerate(coords2): self.assertTrue(numpy.allclose(coord, coords[i])) filepath.unlink()
timestep = 20000 # final time-step index show_figure = True # if True, display the figure # Set the simulation and data directories. simudir = pathlib.Path(__file__).absolute().parents[1] datadir = simudir / 'output' # Load the gridlines from file. filepath = datadir / 'grid.h5' x, y = petibmpy.read_grid_hdf5(filepath, name) # Load the vorticity field from file. filepath = datadir / f'{timestep:0>7}.h5' wz = petibmpy.read_field_hdf5(filepath, name) # Load the boundary coordinates from file. filepath = simudir / 'cylinder.body' xb, yb = petibmpy.read_body(filepath, skiprows=1) # Plot the contours of the vorticity field. pyplot.rc('font', family='serif', size=16) fig, ax = pyplot.subplots(figsize=(6.0, 6.0)) ax.set_xlabel('x / D') ax.set_ylabel('y / D') levels = numpy.arange(-3.0, 3.0 + 0.4 / 2, 0.4) ax.contour(x, y, wz, levels=levels, colors='black') ax.plot(xb, yb, color='red') ax.axis('scaled', adjustable='box') ax.set_xlim(-1.0, 5.0) ax.set_ylim(-2.0, 2.0) fig.tight_layout() # Save the figure.
show_figure = True # if True, display the figure(s) simudir = pathlib.Path(__file__).absolute().parents[1] datadir = simudir / 'output' # Load the grid from file. name = 'wz' # name of the vorticity variable filepath = datadir / 'grid.h5' x, y = petibmpy.read_grid_hdf5(filepath, name) # Load the vorticity at phase angles 0 deg during the last period. timestep = 7500 filepath = datadir / '{:0>7}.h5'.format(timestep) wz1 = petibmpy.read_field_hdf5(filepath, name) # Load the body coordinates at the same time. filepath = datadir / 'circle_{:0>7}.2D'.format(timestep) body1 = petibmpy.read_body(filepath) # Load the vorticity at phase angles 288 deg during the last period. timestep = 9500 filepath = datadir / '{:0>7}.h5'.format(timestep) wz2 = petibmpy.read_field_hdf5(filepath, name) # Load the body coordinates at the same time. filepath = datadir / 'circle_{:0>7}.2D'.format(timestep) body2 = petibmpy.read_body(filepath) # Plot the contours of the vorticity at the two time steps. pyplot.rc('font', family='serif', size=12) fig, (ax1, ax2) = pyplot.subplots(ncols=2, figsize=(8.0, 4.0)) levels = numpy.linspace(-20.0, 20.0, num=30) ax1.set_xlabel('x') ax1.set_ylabel('y')
import petibmpy import rodney args = rodney.parse_command_line() timestep, dt = 2000, 1e-4 # time-step index and time-step size time = timestep * dt # time value nu = 0.1 # coefficient of viscosity # Change default font size and family for Matplotlib figures. pyplot.rc('font', family='serif', size=14) # If path to file with immersed body is provided, load coordinates. body = None if args.body_path is not None: body = petibmpy.read_body(args.body_path, skiprows=1) # Load x-velocity, compute reference solution, and plot side by side. grid_u, u = rodney.petibm_load_grid_and_field(args.data_dir, timestep, 'u') u_ref, _, _ = rodney.taylor_green_vortex(*grid_u, time, nu) levels = numpy.linspace(-0.5, 0.5, num=21) fig, ax = rodney.plot_contourf_field(grid_u, u, 'u', ref=u_ref, body=body, levels=levels, figsize=(8.0, 4.0)) # Load y-velocity, compute reference solution, and plot side by side. grid_v, v = rodney.petibm_load_grid_and_field(args.data_dir, timestep, 'v')
def load_body(self, filepath, **kwargs): x, y, z = petibmpy.read_body(filepath, **kwargs) self.set_coordinates(x, y, z, org=True)
# Load the grid from file. filepath = datadir / 'grid.h5' grid = petibmpy.read_grid_hdf5(filepath, name) states = [2400, 2600, 2800, 3000, 3200] pyplot.rc('font', family='serif', size=14) fig, (ax1, ax2) = pyplot.subplots(nrows=2, ncols=5, figsize=(10.0, 5.0)) levels = numpy.linspace(-20.0, 20.0, num=40) # contour levels for i, state in enumerate(states): print(f'[time step {state}] Load and plot contours of {name}') # Load data from file. filepath = datadir / f'{state:0>7}.h5' data = petibmpy.read_field_hdf5(filepath, name) # Load body coordinates from file. filepath = datadir / f'ellipse_{state:0>7}.2D' body = petibmpy.read_body(filepath) # Plot the contour of the field variable. ax1[i].contour(*grid, data, levels=levels, linewidths=0.5, extend='both') ax1[i].plot(*body, color='black', linewidth=0.5) ax1[i].axis('scaled', adjustable='box') ax1[i].set_xlim(-3.5, 2.5) ax1[i].set_ylim(-5.0, 1.0) ax1[i].axis('off') # Add images from Li et al. (2015) to the figure. datadir = simudir.parent / 'data' times = [3.0, 3.25, 3.5, 3.75, 4.0] for i, time in enumerate(times): print(f'[time {time}] Display image from Li et al. (2015)') filepath = datadir / f'li_et_al_2015_flapping_wz_{time:.2f}.png' im = image.imread(str(filepath))