def test_extrude2d(self): """Test function `extrude2d`.""" limits = [-0.5, 0.5] n = 5 # Check fail if neither `n` nor `ds` are provided. with self.assertRaises(ValueError): petibmpy.extrude2d(self.x, self.y, limits) # Check fail if limits provided at too close. left_limit = 1.0 right_limit = left_limit + random.random() * 1e-6 with self.assertRaises(ValueError): petibmpy.extrude2d(self.x, self.y, [left_limit, right_limit], n=n) # Check size of 3D body x, y, z = petibmpy.extrude2d(self.x, self.y, limits, n=n) self.assertEqual(x.size, n * self.x.size) self.assertEqual(y.size, n * self.y.size) self.assertEqual(z.size, x.size) self.assertTrue(numpy.allclose(x[:self.x.size], self.x)) # Check size of 3D body when given spacing ds. ds = abs(limits[1] - limits[0]) / n x, y, z = petibmpy.extrude2d(self.x, self.y, limits, ds=ds) self.assertEqual(x.size, n * self.x.size) self.assertEqual(y.size, n * self.y.size) self.assertEqual(z.size, x.size) self.assertTrue(numpy.allclose(x[:self.x.size], self.x)) # Check `force` optional argument. x, y, z = petibmpy.extrude2d(self.x, self.y, limits, n=n, force=True) self.assertEqual(z[0], limits[0]) self.assertEqual(z[-1], limits[-1])
"""Create a flat plate with aspect ratio 2 and a 60-degree inclination.""" import numpy import pathlib import petibmpy # Flat-plate's parameters. L = 1.0 # chord length AR = 2.0 # aspect ratio xc, yc, zc = 0.0, 0.0, 0.0 # center's coordinates aoa = 60.0 # angle of inclination in degrees ds = 0.04 # mesh spacing # Generate coordinates of an inclined line. n = numpy.ceil(L / ds) s = numpy.linspace(xc - L / 2, xc + L / 2, num=n + 1) x = xc + numpy.cos(numpy.radians(-aoa)) * s y = yc + numpy.sin(numpy.radians(-aoa)) * s # Extrude the line along the z direction. zlim = (zc - L * AR / 2, zc + L * AR / 2) nz = numpy.ceil(L * AR / ds) x, y, z = petibmpy.extrude2d(x, y, zlim, n=nz, force=True) # Save coordinates to file. simudir = pathlib.Path(__file__).absolute().parents[1] filepath = simudir / f'flatplate_aoa{int(aoa)}.body' petibmpy.write_body(filepath, x, y, z)
"""Generate the coordinates of the snake boundary.""" import pathlib import numpy import petibmpy simudir = pathlib.Path(__file__).absolute().parents[1] # Read the original coordinates of the section. filepath = simudir / 'data/snake2d.body' with open(filepath, 'r') as infile: x, y = numpy.loadtxt(infile, skiprows=1, unpack=True) # Apply rotation and regularize the geometry to desired resolution. x, y = petibmpy.rotate2d(x, y, center=(0.0, 0.0), angle=-35.0) x, y = petibmpy.regularize2d(x, y, ds=0.008) # Extrude the section along the z direction. x, y, z = petibmpy.extrude2d(x, y, ds=0.08, limits=(0.0, 3.2)) # Write new coordinates in file located in simulation directory. filepath = simudir / 'snake3d35.body' with open(filepath, 'w') as outfile: outfile.write(f'{x.size}\n') with open(filepath, 'ab') as outfile: numpy.savetxt(outfile, numpy.c_[x, y, z])