def solve_hydrostatics(self, verbose=True): if MPI: n_cores = MPI.COMM_WORLD.Get_size() max_cores = min(n_cores, self.max_cores) rank = MPI.COMM_WORLD.Get_rank() else: n_cores = 1 max_cores = 1 rank = 0 if rank == 0: mesh = meshk.Mesh(vertices=self._cpt_hydrobody.mesh.vertices,faces=self._cpt_hydrobody.mesh.faces) hydrostatics = hydrok.Hydrostatics( mesh, cog = self.hydrobody.VAWT.GC_FloatingStructure, mass = self.hydrobody.VAWT.m_FloatingStructure/1000.0, rho_water = self.water_rho, grav = self.grav ) LL = self.hydrobody.VAWT.L_Pont + self.hydrobody.VAWT.D_OCol/2.0 pretension = self.grav*(hydrostatics.displacement*1000 - self._hydrobody.VAWT.m_FloatingStructure) F1 = hydrok.Force(point=(-LL,0.0,-self.hydrobody.VAWT.H_Draft), value=(0.0,0.0,-pretension/3.0), mode='absolute') F2 = hydrok.Force(point=(LL*np.cos(np.pi/3.0),-LL*np.sin(np.pi/3.0),-self.hydrobody.VAWT.H_Draft), value=(0.0,0.0,-pretension/3.0), mode='absolute') F3 = hydrok.Force(point=(LL*np.cos(np.pi/3.0),LL*np.sin(np.pi/3.0),-self.hydrobody.VAWT.H_Draft), value=(0.0,0.0,-pretension/3.0), mode='absolute') hydrostatics.add_force(F1) hydrostatics.add_force(F2) hydrostatics.add_force(F3) self._hydrostatics = hydrostatics hydrostatic_coeff = np.zeros((6,6), dtype=float) hydrostatic_coeff[2,2] = hydrostatics.S33 hydrostatic_coeff[3,3] = hydrostatics.S44 \ - (self.hydrobody.VAWT.m_FloatingStructure - 1000.0*hydrostatics.displacement) \ *self.grav*self.hydrobody.VAWT.GC_FloatingStructure[2] hydrostatic_coeff[4,4] = hydrostatics.S55 \ - (self.hydrobody.VAWT.m_FloatingStructure - 1000.0*hydrostatics.displacement) \ *self.grav*self.hydrobody.VAWT.GC_FloatingStructure[2] self._hydrostatic_coeff = hydrostatic_coeff self._pretension = pretension if verbose: print('\nHydrostatic stiffness matrix:') print(pd.DataFrame(self._hydrostatic_coeff)) else: self._hydrostatics = None self._hydrostatic_coeff = None self._pretension = None
def generate_boat() -> cpt.FloatingBody: boat = cpt.FloatingBody.from_file("Simple_Model_Scaledby0.001 v5.stl", file_format="stl", name="hexagon") #boat.translate([0,0,-.2032]) boat.show() boat.add_all_rigid_body_dofs() boat.keep_immersed_part() # The computation of the RAO requires the values of the inertia matrix and the hydrostatic stiffness matrix. if hs is not None and mm is not None: # You can use Meshmagick to compute the hydrostatic stiffness matrix. hsd = hs.Hydrostatics(mm.Mesh(boat.mesh.vertices, boat.mesh.faces)).hs_data m = hsd['disp_mass'] I = np.array([[hsd['Ixx'], -1 * hsd['Ixy'], -1 * hsd['Ixz']], [-1 * hsd['Ixy'], hsd['Iyy'], -1 * hsd['Iyz']], [-1 * hsd['Ixz'], -1 * hsd['Iyz'], hsd['Izz']]]) M = block_diag(m, m, m, I) boat.mass = boat.add_dofs_labels_to_matrix(M) kHS = block_diag(0, 0, hsd['stiffness_matrix'], 0) print(kHS) boat.hydrostatic_stiffness = boat.add_dofs_labels_to_matrix(kHS) else: # Alternatively, you can define these by hand boat.mass = boat.add_dofs_labels_to_matrix([[1e6, 0, 0, 0, 0, 0], [0, 1e6, 0, 0, 0, 0], [0, 0, 1e6, 0, 0, 0], [0, 0, 0, 1e7, 0, 2e5], [0, 0, 0, 0, 4e7, 0], [0, 0, 0, 2e5, 0, 5e7]]) boat.hydrostatic_stiffness = boat.add_dofs_labels_to_matrix( [[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 3e6, 0, -7e6, 0], [0, 0, 0, 2e7, 0, 0], [0, 0, -7e6, 0, 1e8, 0], [0, 0, 0, 0, 0, 0]]) return boat
def hydrostatics(myBodies, savepath=''): ''' use meshmagick functions to calculate and output the hydrostatic stiffness, interia, center of gravity, center of buoyancy and displaced volume of a capytaine bodies. Output is saved to Hydrostatics.dat and KH.dat files in the same manner as Nemoh Example of output format: Hydrostatics.dat: XF = 0.000 - XG = 0.000 YF = 0.000 - YG = 0.000 ZF = -2.500 - ZG = -2.500 Displacement = 0.4999997E+03 Waterplane area = 0.1000002E+03 KH.dat: 0.0000000E+00 0.0000000E+00 0.0000000E+00 0.0000000E+00 0.0000000E+00 0.0000000E+00 0.0000000E+00 0.0000000E+00 0.0000000E+00 0.0000000E+00 0.0000000E+00 0.0000000E+00 0.0000000E+00 0.0000000E+00 0.9810053E+06 -0.1464844E-01 -0.5859375E-02 0.0000000E+00 0.0000000E+00 0.0000000E+00 -0.1464844E-01 0.8160803E+07 0.0000000E+00 0.0000000E+00 0.0000000E+00 0.0000000E+00 -0.5859375E-02 0.0000000E+00 0.8160810E+07 0.0000000E+00 0.0000000E+00 0.0000000E+00 0.0000000E+00 0.0000000E+00 0.0000000E+00 0.0000000E+00 Parameters ---------- myBodies : List A list of capytaine floating bodies. Returns ------- None ''' nbod = len(myBodies) for i, body in enumerate(myBodies): cg = body.center_of_mass # use meshmagick to compute hydrostatic stiffness matrix # NOTE: meshmagick currently has issue if a body is completely submerged (OSWEC base) # See issues #7, #19 on meshmagick GitHub page. # use try-except statement to catch this error use alternate function for cb/vo body_mesh = mmm.Mesh(body.mesh.vertices, body.mesh.faces, name=body.mesh.name) # try: # body_hs = mmhs.Hydrostatics(working_mesh=body_mesh, # cog=body.center_of_mass, # rho_water=1023.0, # grav=9.81) # vo = body_hs.displacement_volume # cb = body_hs.buoyancy_center # khs = body_hs.hydrostatic_stiffness_matrix # except: # # Exception if body is fully submerged. # # if fully submerged: # # stiffness is 0 as small displacements do not change the displaced volume # # displaced volume is the total mesh volume # # center of buoyancy is equal to the center of gravity of the mesh with a constant density # vo = body_mesh.volume # khs = np.zeros((3,3)) # inertia = body_mesh.eval_plain_mesh_inertias() # cb = inertia.gravity_center body_hs = mmhs.Hydrostatics(working_mesh=body_mesh, cog=body.center_of_mass, rho_water=1023.0, grav=9.81) vo = body_hs.displacement_volume cb = body_hs.buoyancy_center khs = body_hs.hydrostatic_stiffness_matrix # set file index fileind = '' if nbod != 1: fileind = '_' + str(i) # Write hydrostatic stiffness to KH.dat file as khs_full = np.zeros((6, 6)) khs_full[2:5, 2:5] += khs tmp = savepath + 'KH' + fileind + '.dat' np.savetxt(tmp, khs_full) # Write the other hydrostatics data to Hydrostatics.dat file tmp = savepath + 'Hydrostatics' + fileind + '.dat' # f = open(rf'{tmp}','w') f = open(tmp, 'w') for j in [0, 1, 2]: line = f'XF = {cb[j]:7.3f} - XG = {cg[j]:7.3f} \n' f.write(line) line = f'Displacement = {vo:E}' f.write(line) f.close()
def body_hydrostatics(myBodies, savepath=''): ''' use meshmagick functions to calculate and output the hydrostatic stiffness, interia, center of gravity, center of buoyancy and displaced volume of a capytaine bodies. Output is saved to Hydrostatics.dat and KH.dat files in the same manner as Nemoh Example of output format: Hydrostatics.dat: XF = 0.000 - XG = 0.000 YF = 0.000 - YG = 0.000 ZF = -2.500 - ZG = -2.500 Displacement = 0.4999997E+03 Waterplane area = 0.1000002E+03 KH.dat: 0.0000000E+00 0.0000000E+00 0.0000000E+00 0.0000000E+00 0.0000000E+00 0.0000000E+00 0.0000000E+00 0.0000000E+00 0.0000000E+00 0.0000000E+00 0.0000000E+00 0.0000000E+00 0.0000000E+00 0.0000000E+00 0.9810053E+06 -0.1464844E-01 -0.5859375E-02 0.0000000E+00 0.0000000E+00 0.0000000E+00 -0.1464844E-01 0.8160803E+07 0.0000000E+00 0.0000000E+00 0.0000000E+00 0.0000000E+00 -0.5859375E-02 0.0000000E+00 0.8160810E+07 0.0000000E+00 0.0000000E+00 0.0000000E+00 0.0000000E+00 0.0000000E+00 0.0000000E+00 0.0000000E+00 Parameters ---------- myBodies : List A list of capytaine floating bodies. Returns ------- None ''' nbod = len(myBodies) for i, body in enumerate(myBodies): cg = body.center_of_mass # use meshmagick to compute hydrostatic stiffness matrix # NOTE: meshmagick currently has issue if a body is copmletely submerged (OSWEC base) # use try-except statement to catch this error use alternate function for cb/vo # if completely submerged, stiffness is 0 body_mesh = mmm.Mesh(body.mesh.vertices, body.mesh.faces, name=body.mesh.name) try: body_hs = mmhs.Hydrostatics(working_mesh=body_mesh, cog=body.center_of_mass, rho_water=1023.0, grav=9.81) vo = body_hs.displacement_volume cb = body_hs.buoyancy_center khs = body_hs.hydrostatic_stiffness_matrix except: # Exception if body is fully submerged vo = body_mesh.volume cb = cg hs_s = [0, 0, 0, 0, 0, 0] khs = np.zeros((3, 3)) # set file index fileind = '' if nbod != 1: fileind = '_' + str(i) # set file path based on where call_capytaine is called from (using ncFName) # filepath = '' # if savepath is not '': # filepath,tmp = os.path.split(ncFName) # Write hydrostatic stiffness to KH.dat file as khs_full = np.zeros((6, 6)) khs_full[2:5, 2:5] += khs tmp = savepath + 'KH' + fileind + '.dat' np.savetxt(tmp, khs_full) # Write the other hydrostatics data to Hydrostatics.dat file tmp = savepath + 'Hydrostatics' + fileind + '.dat' # f = open(rf'{tmp}','w') f = open(tmp, 'w') for j in [0, 1, 2]: line = f'XF = {cb[j]:7.3f} - XG = {cg[j]:7.3f} \n' f.write(line) line = f'Displacement = {vo:E}' f.write(line) f.close()
np.set_printoptions(linewidth=160) import meshmagick.hydrostatics as hs import meshmagick.mesh as mm from scipy.linalg import block_diag import matplotlib.pyplot as plt bem_solver = cpt.BEMSolver() body = cpt.FloatingBody.from_file("PEARLmesh_sharmi v1v4.stl", file_format="stl") body.add_all_rigid_body_dofs() body.keep_immersed_part() hsd = hs.Hydrostatics(mm.Mesh(body.mesh.vertices, body.mesh.faces)).hs_data # Inertial properties for neutrally buoyant constant density body #you can use meshmagick to find inertia matrix but they use constant density #alternatively use a CAD software to acqurie inertia matrix m = 130.78 I = np.array([[34.298, 1.133E-10, 0], [1.133E-10, 34.303, 0], [0, 0, 33.331]]) M = block_diag(m, m, m, I) body.mass = body.add_dofs_labels_to_matrix(M) # print(body.mass) # Hydrostatics kHS = block_diag(0, 0, hsd['stiffness_matrix'], 0) body.hydrostatic_stiffness = body.add_dofs_labels_to_matrix(kHS) # change omega values
# Initialize floating body r = 1.0 sphere = cpt.Sphere( radius=r, # Dimension center=(0, 0, 0), # Position nphi=20, ntheta=20, # Fineness of the mesh ) sphere.add_all_rigid_body_dofs() # Visualize the body # sphere.show() # Create a hydrostatics body in Meshmagick hsd = hs.Hydrostatics(mm.Mesh(sphere.mesh.vertices, sphere.mesh.faces), cog=(0, 0, 0), rho_water=rho_water, grav=g).hs_data # Inertial properties for neutrally buoyant constant density body m = hsd['disp_mass'] I = np.array([[hsd['Ixx'], -1 * hsd['Ixy'], -1 * hsd['Ixz']], [-1 * hsd['Ixy'], hsd['Iyy'], -1 * hsd['Iyz']], [-1 * hsd['Ixz'], -1 * hsd['Iyz'], hsd['Izz']]]) M = block_diag(m, m, m, I) sphere.mass = sphere.add_dofs_labels_to_matrix(M) print(sphere.mass) assert np.isclose(m, 1 / 2 * 4 / 3 * r**3 * np.pi * rho_water, rtol=1e-1)
def __init__(self, mesh, f0, num_freq, modes, run_bem=True, cog=None, kinematics=None, name=None, wrk_dir=None, verbose=True): self.params = dict() if not isinstance(f0, (float,int)): raise TypeError('f0 must be of type float or int') self.params['f0'] = f0 if not isinstance(num_freq, int): raise TypeError('num_freq must be of type int') self.params['num_freq'] = num_freq freq = np.arange(1, num_freq+1)*f0 assert len(modes) == 6 # TODO times number of bodies assert isinstance(modes, (np.ndarray, list)) modes = np.asarray(modes) # assert np.all(np.isin(modes,[0,1])) # TODO self.params['modes'] = modes.tolist() if cog is not None: raise NotImplementedError else: cog = [0,0,0] self.kinematics = kinematics if name is None: date_str = datetime.datetime.now().strftime('%Y%m%d%H%M%S%f') name = 'wec_{:}'.format(date_str) if not isinstance(name, str): raise TypeError('Name must be a string') self.params['name'] = name if wrk_dir is None: wrk_dir = os.path.join('.',self.params['name']) elif wrk_dir is False: wrk_dir = tempfile.mkdtemp() if not isinstance(wrk_dir, str): raise TypeError('wrk_dir must be of type str') self.params['wrk_dir'] = wrk_dir os.makedirs(self.params['wrk_dir'], exist_ok=True) if verbose is not True: raise NotImplementedError else: log_file = os.path.join(self.params['wrk_dir'], 'log.txt') logging.basicConfig(filename=log_file) self.files = dict() self.files['geo'] = os.path.join(self.params['wrk_dir'], self.params['name'] + '.geo') self.files['stl'] = os.path.join(self.params['wrk_dir'], self.params['name'] + '.stl') if isinstance(mesh,str): warnings.warn("Using existing STL file") elif isinstance(mesh,meshio._mesh.Mesh): mesh.write(self.files['stl']) else: raise TypeError('Must be an STL file path or meshio mesh object') self.fb = cpy.FloatingBody.from_file(self.files['stl'], name=self.params['name']) self.fb.keep_immersed_part() self.fb.add_all_rigid_body_dofs() tmp_mesh = mm.Mesh(self.fb.mesh.vertices, self.fb.mesh.faces) self.hsa = hs.Hydrostatics(tmp_mesh, cog=cog, mass=None, # TODO rho_water=1e3, # TODO verbose=True) hsd = self.hsa.hs_data m = hsd['disp_mass'] I = np.array([[hsd['Ixx'], -1*hsd['Ixy'], -1*hsd['Ixz']], [-1*hsd['Ixy'], hsd['Iyy'], -1*hsd['Iyz']], [-1*hsd['Ixz'], -1*hsd['Iyz'], hsd['Izz']]]) M = block_diag(m, m, m, I) # TODO - scale inertias if mass is specified by user self.fb.mass = self.fb.add_dofs_labels_to_matrix(M) kHS = block_diag(0,0,hsd['stiffness_matrix'],0) self.fb.hydrostatic_stiffness = self.fb.add_dofs_labels_to_matrix(kHS) assert isinstance(run_bem, (bool,str)) if run_bem == True: self.run_bem(freq) self.write_bem() elif isinstance(run_bem, str): self.hydro = self.__read_bem__(run_bem) self.files['json'] = os.path.join(self.params['wrk_dir'], self.params['name'] + '.json') with open(self.files['json'], "w") as outfile: json.dump(self.params, outfile, sort_keys=True, indent=4)