Skip to content

Commit

Permalink
Catch some bugs when bad mesh files are used in dtocean-wec (#14)
Browse files Browse the repository at this point in the history
-   Renamed `dtocean_wec.submodule.utils.mesh_util` module as `dtocean_wec.submodule.utils.mesh` and refactored
-   Fixed bugs that occur if opening wrong type of mesh file in dtocean-wec
-   Bump to version 3.2.0
  • Loading branch information
H0R5E committed Sep 12, 2022
1 parent 25bc9dd commit a54366b
Show file tree
Hide file tree
Showing 16 changed files with 1,096 additions and 577 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [3.2.0] - 2022-09-12

### Changed

- Renamed dtocean_wec.submodule.utils.mesh_util module as
dtocean_wec.submodule.utils.mesh and refactored.

### Fixed

- Fixed bugs that occur if opening wrong type of mesh file in dtocean-wec.

## [3.1.1] - 2022-07-15

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ $ conda activate _dtocean_hydro
Install pytest to the environment (one time only):

```
$ conda install -y mock pytest pytest-mock pytest-qt
$ conda install -y mock pytest pytest-catchlog pytest-mock pytest-qt
```

Run the tests:
Expand Down
4 changes: 2 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#---------------------------------#

# version format
version: 3.1.1.build{build}
version: 3.2.0.build{build}

image:
- Visual Studio 2015
Expand Down Expand Up @@ -41,7 +41,7 @@ install:
- python setup.py bootstrap
- conda install --file requirements-conda-dev.txt
- pip install -e .
- conda install mock "pytest>=3.6,<4" pytest-cov pytest-mock pytest-qt
- conda install mock "pytest>=3.6,<4" pytest-catchlog pytest-cov pytest-mock pytest-qt

build: off

Expand Down
75 changes: 43 additions & 32 deletions dtocean_wec/submodule/nemoh_run.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-

# Copyright (C) 2016 Pau Mercadez Ruiz
# Copyright (C) 2017-2018 Mathew Topper
# Copyright (C) 2017-2022 Mathew Topper
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -33,9 +33,10 @@
import subprocess

import numpy as np
from numpy import linalg as LA

from hydrostatics import Hydrostatics_Nemohcal
from utils.mesh_util import MeshBem
from utils.mesh import MeshBem
from utils.multibody_analysis import MultiBodyAnalysis

# Start logging
Expand Down Expand Up @@ -198,29 +199,13 @@ def __init__(self,
self.mesh_obj += [msh_obj]

# identify the size of the circumscribing cylinder
if self.Cyl_Nzeta == 0 or self.Cyl_Ntheta == 0 or (not self.__get_array_mat):
if (self.Cyl_Nzeta == 0 or
self.Cyl_Ntheta == 0 or
not self.__get_array_mat):
self.Cyl_radius = 0
else :
coord = np.empty((0, 3))
for b_mesh in self.mesh_obj:
coord = np.vstack((coord, b_mesh.Vertex))
area = 0.0
for panel in b_mesh.panels:
if panel.area > area:
area = panel.area
d = max([(panel.x[0]-panel.x[s])**2+(panel.y[0]-panel.y[s])**2+
(panel.z[0]-panel.z[s])**2 for s in [1,-1]])

centroid = coord.mean(0)
if not np.sqrt(np.sum(centroid[:2]**2))/coord.max() < 1e-3:
module_logger.warning("WARNING: the centroid of the mesh file is not centered in the"
" origin of the mesh coordinate system. \nConsider to regenerate"
" a mesh that satisfy this condition.")

self.Cyl_radius = (np.sqrt(coord[:,0]**2+coord[:,1]**2)).max()
self.Cyl_radius += np.sqrt(d)


else:
self.Cyl_radius = _get_cylinder_radius(self.mesh_obj)

def load_folder_tree(self):
self.path_prj_hdy = os.path.join(self.prj_folder,'hydrodynamic')
self.path_prj_dyn_res = os.path.join(self.path_prj_hdy,'results')
Expand Down Expand Up @@ -254,14 +239,8 @@ def gen_mesh_files(self, show_norm=False):
for nb in range(self.n_bodies):
msh_obj = self.mesh_obj[nb]
if self._debug: msh_obj.visualise_mesh()

if self.bodies[nb]['mesh'][-3:].lower()=="gdf":
msh_obj.mesh_generation("nemoh", output_path=self.path_prj_dyn_mesh)

else:
msh_obj.mesh_generation("nemoh", output_path=self.path_prj_dyn_mesh)

if self._debug and self.n_bodies>1: msh_obj.visualise_mesh()
msh_obj.mesh_generation("nemoh",
output_path=self.path_prj_dyn_mesh)

def gen_multibody_structure(self):
mb_obj = MultiBodyAnalysis(self.bodies, self.shared_dof_binary, self.point_application)
Expand Down Expand Up @@ -376,7 +355,39 @@ def run_nemoh(self, nemoh_folder, start=True):
os.path.join(nemoh_folder, 'postprocessor.exe')
)
os.chdir(actual_dir)


def _get_cylinder_radius(meshes):

coord = np.empty((0, 3))
d = None

for mesh in meshes:

coord = np.vstack((coord, mesh.vertices))

for panel in mesh.connectivity:

d_panel = max([LA.norm(mesh.vertices[panel[0]] -
mesh.vertices[panel[s]]) for s in [1, -1]])

if d is None or d_panel > d:
d = d_panel

if d is None:
raise RuntimeError("Can not determine mesh extents")

centroid = coord.mean(0)

if not np.sqrt(np.sum(centroid[:2] ** 2)) / coord.max() < 1e-3:
module_logger.warning(
"WARNING: the centroid of the mesh file is not centered "
"at the origin of the mesh coordinate system.\n"
"Consider regenerating a mesh to satisfy this condition.")

return (np.sqrt(coord[:, 0] ** 2 + coord[:, 1] ** 2)).max() + d


def rot_matrix(ang):
Rz = lambda angle: np.array([[np.cos(angle), -np.sin(angle), 0], [np.sin(angle), np.cos(angle), 0], [0, 0, 1]])
Ry = lambda angle: np.array([[np.cos(angle), 0, np.sin(angle)], [0, 1, 0], [-np.sin(angle), 0, np.cos(angle)]])
Expand Down
Loading

0 comments on commit a54366b

Please sign in to comment.