def load_force_coefficients(filepath, config): """Load forces from file and return force coefficients.""" # Load forces from file. t, fx, fy, fz = petibmpy.read_forces(filepath) fx *= -1.0 # drag to thrust # Convert forces to force coefficients. rho, U_inf, A_plan = (getattr(config, name) for name in ('rho', 'U_inf', 'A_plan')) coeff = 1 / (0.5 * rho * U_inf**2 * A_plan) # Non-dimensionalize time values by period. t /= config.T ct, cl, cz = petibmpy.get_force_coefficients(fx, fy, fz, coeff=coeff) return Solution(t=t, ct=ct, cl=cl, cz=cz)
def test_get_force_coefficients(self): """Test function `get_force_coefficients`.""" dim = 3 # Load forces from file. filepath = self.datadir / f'forces{dim}d-single-0.txt' _, fx, fy, fz = petibmpy.read_forces(filepath) # Convert one direction with unit coefficient. cd, = petibmpy.get_force_coefficients(fx) self.assertTrue(numpy.allclose(cd, fx)) # Convert two directions with unit coefficient. cd, cl = petibmpy.get_force_coefficients(fx, fy) self.assertTrue(numpy.allclose(cd, fx)) self.assertTrue(numpy.allclose(cl, fy)) # Convert three directions with unit coefficient. cd, cl, cz = petibmpy.get_force_coefficients(fx, fy, fz) self.assertTrue(numpy.allclose(cd, fx)) self.assertTrue(numpy.allclose(cl, fy)) self.assertTrue(numpy.allclose(cz, fz)) # Convert three directions with random coefficient. coeff = random.uniform(0.1, 5.0) cd, cl, cz = petibmpy.get_force_coefficients(fx, fy, fz, coeff=coeff) self.assertTrue(numpy.allclose(cd / coeff, fx)) self.assertTrue(numpy.allclose(cl / coeff, fy)) self.assertTrue(numpy.allclose(cz / coeff, fz))
import pathlib from matplotlib import pyplot import petibmpy simudir = pathlib.Path(__file__).absolute().parents[1] filepath = simudir / 'output' / 'forces-0.txt' t, fx, fy, fz = petibmpy.read_forces(filepath) rho, u_inf = 1.0, 1.0 # density and freestream speed dyn_pressure = 0.5 * rho * u_inf**2 # dynamic pressure c = 1.0 # chord length Lz = 3.2 * c # spanwise length coeff = 1 / (dyn_pressure * c * Lz) # scaling factor for force coefficients cd, cl, cz = petibmpy.get_force_coefficients(fx, fy, fz, coeff=coeff) pyplot.rc('font', family='serif', size=16) fig, ax = pyplot.subplots(figsize=(8.0, 4.0)) ax.set_xlabel('Non-dimensional time') ax.set_ylabel('Force coefficients') ax.grid() ax.plot(t, cd, label='$C_D$') ax.plot(t, cl, label='$C_L$') ax.plot(t, cz, label='$C_z$') ax.legend() ax.set_xlim(t[0], t[-1]) ax.set_ylim(-0.1, 3.0) fig.tight_layout() figdir = simudir / 'figures'
# Create the wing kinematics. wing = rodney.WingKinematics(Re=200.0, St=0.6, psi=110.0, nt_period=2000) # Compute the cycle-averaged thrust. filepath = datadir / 'forces-0.txt' t, fx, _, _ = petibmpy.read_forces(filepath) thrust = -fx # switch from drag to thrust time_limits = (4 * wing.T, 5 * wing.T) # interval to consider for average thrust_avg, = petibmpy.get_time_averaged_values(t, thrust, limits=time_limits) # Compute the cycle-averaged thrust coefficient. rho, U_inf, A_plan = (getattr(wing, name) for name in ('rho', 'U_inf', 'A_plan')) scale = 1 / (0.5 * rho * U_inf**2 * A_plan) ct, = petibmpy.get_force_coefficients(thrust, coeff=scale) ct_avg, = petibmpy.get_time_averaged_values(t, ct, limits=time_limits) # Load original boundary coordinates from file. filepath = simudir / 'wing.body' wing.load_body(filepath, skiprows=1) # Compute surface area associated with each Lagrangian marker. ds = wing.A_plan / wing.size # Create virtual boundary around flat plate. # The flat surface is extended by d grid cells on lower and upper surfaces. d = 0.03 * wing.c # normal distance from original markers (3% chord length) x0, y0, z0 = wing.get_coordinates() xv0 = numpy.tile(x0, 2) yv0 = numpy.concatenate((y0 - d, y0 + d))
"""Generate a figure of the drag and lift force coefficients over time. Save the figure in the sub-folder `figures` of the simulation directory. """ import sys import pathlib from matplotlib import pyplot import petibmpy simudir = pathlib.Path(__file__).absolute().parents[1] filepath = simudir / 'output' / 'forces-0.txt' t, fx, fy = petibmpy.read_forces(filepath) cd, cl = petibmpy.get_force_coefficients(fx, fy, coeff=2.0) pyplot.rc('font', family='serif', size=16) fig, ax = pyplot.subplots(figsize=(8.0, 4.0)) ax.set_xlabel('Non-dimensional time') ax.set_ylabel('Force coefficients') ax.grid() ax.plot(t, cd, label='$C_D$') ax.plot(t, cl, label='$C_L$') ax.legend(ncol=2) ax.set_xlim(t[0], t[-1]) ax.set_ylim(0.0, 3.0) fig.tight_layout() figdir = simudir / 'figures' figdir.mkdir(parents=True, exist_ok=True)