-
Notifications
You must be signed in to change notification settings - Fork 1
/
capytaine_runner.py
78 lines (56 loc) · 2.57 KB
/
capytaine_runner.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import logging
import numpy as np
from numpy import pi
import capytaine as cpt
from capytaine.io.xarray import separate_complex_values
from capytaine.meshes.geometry import xOz_Plane
from capytaine.meshes.symmetric import ReflectionSymmetricMesh
from mafredo import Hyddb1
logging.basicConfig(level=logging.INFO, format='%(levelname)-8s: %(message)s')
bem_solver = cpt.BEMSolver()
# Helper
def make_database(body, omegas, wave_directions, waterdepth=0):
# SOLVE BEM PROBLEMS
problems = []
for wave_direction in wave_directions:
for omega in omegas:
problems += [cpt.RadiationProblem(omega=omega, body=body, radiating_dof=dof, water_depth=waterdepth) for dof in body.dofs]
problems += [cpt.DiffractionProblem(omega=omega, body=body, wave_direction=wave_direction, water_depth=waterdepth)]
results = [bem_solver.solve(problem) for problem in problems]
*radiation_results, diffraction_result = results
dataset = cpt.assemble_dataset(results)
dataset['diffraction_result'] = diffraction_result
return dataset
def run_capytaine(name: str, # name of the body eg "boat"
file_grid : str, # input file, e.g. grid.stl
periods : np.array, # periods in seconds
directions_deg : np.array,
waterdepth : float = 0, # waterdepth
symmetry : bool = False, # symmetry in XZ plane
show_only : bool = False # show only the mesh
):
periods = np.array(periods)
directions = np.array(directions_deg) * pi / 180
file_out_nc = file_grid[:-4] + '.nc'
outfile = "{}.dhyd".format(file_grid[:-4])
omega = 2 * np.pi / periods
boat = cpt.FloatingBody.from_file(file_grid, file_format="stl", name=name)
if symmetry:
mesh = ReflectionSymmetricMesh(boat.mesh, plane=xOz_Plane, name=f"{name}_mesh")
boat = cpt.FloatingBody(mesh=mesh)
boat.add_all_rigid_body_dofs()
boat.keep_immersed_part()
boat.show()
if show_only:
return
dataset = make_database(body=boat, omegas=omega, wave_directions=directions, waterdepth=waterdepth)
sep = separate_complex_values(dataset)
sep.to_netcdf(file_out_nc,
encoding={'radiating_dof': {'dtype': 'U'},
'influenced_dof': {'dtype': 'U'},
'diffraction_result': {'dtype': 'U'}})
print(f'saved NC results as {file_out_nc}')
hyd = Hyddb1.create_from_capytaine(filename=file_out_nc)
hyd.save_as(outfile)
print(f'Saved as: {outfile}')
hyd.plot(do_show=True)