def fill_dataset(self, dataset, bodies, **kwargs): """Solve a set of problems defined by the coordinates of an xarray dataset. Parameters ---------- dataset : xarray Dataset dataset containing the problems parameters: frequency, radiating_dof, water_depth, ... bodies : list of FloatingBody the bodies involved in the problems Returns ------- xarray Dataset """ attrs = { 'start_of_computation': datetime.now().isoformat(), **self.exportable_settings } problems = problems_from_dataset(dataset, bodies) if 'theta' in dataset.coords: results = self.solve_all(problems, keep_details=True) kochin = kochin_data_array(results, dataset.coords['theta']) dataset = assemble_dataset(results, attrs=attrs, **kwargs) dataset['kochin'] = kochin else: results = self.solve_all(problems, keep_details=False) dataset = assemble_dataset(results, attrs=attrs, **kwargs) return dataset
def test_assemble_dataset(): body = Sphere(center=(0, 0, -4), name="sphere") body.add_translation_dof(name="Heave") pb_1 = DiffractionProblem(body=body, wave_direction=1.0, omega=1.0) res_1 = solver.solve(pb_1) ds1 = assemble_dataset([res_1]) assert "Froude_Krylov_force" in ds1 pb_2 = RadiationProblem(body=body, radiating_dof="Heave", omega=1.0) res_2 = solver.solve(pb_2) ds2 = assemble_dataset([res_2]) assert "added_mass" in ds2 ds12 = assemble_dataset([res_1, res_2]) assert "Froude_Krylov_force" in ds12 assert "added_mass" in ds12
def test_multibody(): """Compare with Nemoh 2.0 for two bodies.""" sphere = Sphere(radius=1.0, ntheta=5, nphi=20) sphere.translate_z(-2.0) sphere.add_translation_dof(direction=(1, 0, 0), name="Surge") sphere.add_translation_dof(direction=(0, 0, 1), name="Heave") cylinder = HorizontalCylinder(length=5.0, radius=1.0, nx=10, nr=1, ntheta=10) cylinder.translate([+1.5, 3.0, -3.0]) cylinder.add_translation_dof(direction=(1, 0, 0), name="Surge") cylinder.add_translation_dof(direction=(0, 0, 1), name="Heave") both = cylinder + sphere total_volume = cylinder.volume + sphere.volume # both.show() problems = [ RadiationProblem(body=both, radiating_dof=dof, omega=1.0) for dof in both.dofs ] problems += [DiffractionProblem(body=both, wave_direction=0.0, omega=1.0)] results = [solver.solve(problem) for problem in problems] data = assemble_dataset(results) data_from_nemoh_2 = np.array([ [ 3961.86548, 50.0367661, -3.32347107, 6.36901855E-02, 172.704819, 19.2018471, -5.67303181, -2.98873377 ], [ -3.08301544, 5.72392941E-02, 14522.1689, 271.796814, 128.413834, 6.03351116, 427.167358, 64.1587067 ], [ 161.125534, 17.8332844, 126.392113, 5.88006783, 2242.47412, 7.17850924, 1.29002571, 0.393169671 ], [ -5.02560759, -2.75930357, 419.927460, 63.3179016, 1.23501396, 0.416424811, 2341.57593, 15.8266096 ], ]) dofs_names = list(both.dofs.keys()) assert np.allclose(data['added_mass'].sel( omega=1.0, radiating_dof=dofs_names, influenced_dof=dofs_names).values, data_from_nemoh_2[:, ::2], atol=1e-3 * total_volume * problems[0].rho) assert np.allclose(data['radiation_damping'].sel( omega=1.0, radiating_dof=dofs_names, influenced_dof=dofs_names).values, data_from_nemoh_2[:, 1::2], atol=1e-3 * total_volume * problems[0].rho)
def test_two_vertical_cylinders(): distance = 5 buoy = VerticalCylinder(length=3, radius=0.5, center=(-distance / 2, -1, 0), nx=8, nr=3, ntheta=8) buoy.mesh = buoy.mesh.merged() buoy.mesh = buoy.mesh.keep_immersed_part() buoy.add_translation_dof(name="Sway") two_buoys = FloatingBody.join_bodies(buoy, buoy.translated_x(distance)) two_buoys.mesh = buoy.mesh.symmetrized( yOz_Plane) # Use a ReflectionSymmetry as mesh... problems = [ RadiationProblem(body=two_buoys, omega=1.0, radiating_dof=dof) for dof in two_buoys.dofs ] results = assemble_dataset(solver_without_sym.solve_all(problems)) # Check that the resulting matrix is symmetric assert np.isclose(results['added_mass'].data[0, 0, 0], results['added_mass'].data[0, 1, 1]) assert np.isclose(results['added_mass'].data[0, 1, 0], results['added_mass'].data[0, 0, 1]) assert np.isclose(results['radiation_damping'].data[0, 0, 0], results['radiation_damping'].data[0, 1, 1]) assert np.isclose(results['radiation_damping'].data[0, 1, 0], results['radiation_damping'].data[0, 0, 1]) results_with_sym = assemble_dataset(solver_with_sym.solve_all(problems)) assert np.allclose(results['added_mass'].data, results_with_sym['added_mass'].data) assert np.allclose(results['radiation_damping'].data, results_with_sym['radiation_damping'].data)
def fill_dataset(self, dataset, bodies, *, n_jobs=1, **kwargs): """Solve a set of problems defined by the coordinates of an xarray dataset. Parameters ---------- dataset : xarray Dataset dataset containing the problems parameters: frequency, radiating_dof, water_depth, ... bodies : FloatingBody or list of FloatingBody The body or bodies involved in the problems They should all have different names. n_jobs: int, optional (default: 1) the number of jobs to run in parallel using the optional dependency `joblib` By defaults: do not use joblib and solve sequentially. Returns ------- xarray Dataset """ attrs = { 'start_of_computation': datetime.now().isoformat(), **self.exportable_settings } problems = problems_from_dataset(dataset, bodies) if 'theta' in dataset.coords: results = self.solve_all(problems, keep_details=True, n_jobs=n_jobs) kochin = kochin_data_array(results, dataset.coords['theta']) dataset = assemble_dataset(results, attrs=attrs, **kwargs) dataset.update(kochin) else: results = self.solve_all(problems, keep_details=False, n_jobs=n_jobs) dataset = assemble_dataset(results, attrs=attrs, **kwargs) return dataset
def main(): args = parser.parse_args() for paramfile in args.paramfiles: problems = import_cal_file(paramfile) solver = BEMSolver() results = [solver.solve(pb) for pb in problems] data = assemble_dataset(results) print(data) results_directory = os.path.join(os.path.dirname(paramfile), 'results') try: os.mkdir(results_directory) except FileExistsError: LOG.warning( "The 'results' directory already exists. You might be overwriting existing data." ) LOG.info("Write results in legacy tecplot format.") write_dataset_as_tecplot_files(results_directory, data)