Source code for g3d.mesh.modules.generic

"""base class - generic mesh module"""
from typing import Any, Union

import os

if os.environ.get('__USE_CUDA', '0') == '1':
    import cupy as np
    from cupy import ndarray
else:
    import numpy as np
    from numpy import ndarray

import matplotlib.pyplot as plt

from g3d.defs import G3DWorld
from g3d.defs import G3DDisplay
from g3d.defs import DIMMode
from g3d.defs import NodeDistribution
from g3d.defs import Direction
from g3d.mesh.geo import Grid
from g3d.mesh.geo import Edge
from g3d.mesh.geo import Coord


[docs] class MeshModule(Grid): def __init__(self, *args: Any, **kwargs: Any) -> None: super().__init__(*args, **kwargs) self._data_file = 'filename' self._data = [] self._smooth_faces = False self._n_smooth = 0 self._edges = [] self._coordinates = [] self._nk3D = 2 self._tags = {} self._initialize_module(**kwargs) def _initialize_module(self, **kwargs: Any) -> None: self._populate_tags() def _set_default(self) -> None: super()._set_default() g3d_world = G3DWorld() self._tag = g3d_world.info.code_sub_description
[docs] def set_from_key_value(self, key: str, value: Any) -> bool: g3d_world = G3DWorld() if super().set_from_key_value(key, value): if key == 'dimension' or key == 'dim': if g3d_world.options.dim_mode == DIMMode.G2D: self._nk3D = 2 return True elif key == 'data_file': self._data_file = value self._read_data_from_file() return True elif key == 'smooth_faces': self._smooth_faces = value self.__set_default_smooth_iterations() return True elif key == 'n_smooth': self._n_smooth = value self.__set_smooth_status() return True elif key == 'nk3D': self._nk3D = value return True return False
def _set_code_type(self) -> None: super()._set_code_type() g3d_world = G3DWorld() g3d_world.info.code_sub_description = 'generic mesh module' @property def data_file(self) -> str: """ :return: """ return self._data_file @data_file.setter def data_file(self, val: str) -> None: self._data_file = val self._read_data_from_file() @property def data(self) -> Union[list, ndarray]: """ :return: """ return self._data @data.setter def data(self, val: Union[list, ndarray]) -> None: self._data = val @property def nk3D(self) -> int: """ :return: """ return self._nk3D @nk3D.setter def nk3D(self, val: int) -> None: self._nk3D = val @property def smooth_faces(self) -> bool: """ :return: """ return self._smooth_faces @smooth_faces.setter def smooth_faces(self, val: bool) -> None: self._smooth_faces = val self.__set_default_smooth_iterations() @property def n_smooth(self) -> int: """ :return: """ return self._n_smooth @n_smooth.setter def n_smooth(self, val: int) -> None: self._n_smooth = val self.__set_smooth_status() @property def edges(self) -> list: """ :return: """ return self._edges @property def coordinates(self) -> list: """ :return: """ return self._coordinates @property def tags(self) -> dict: """ :return: """ return self._tags
[docs] def add_tag(self, key: str, tag: str) -> None: """ :param key: :param tag: """ if key not in self._tags.keys(): self._tags[key] = tag
def __set_default_smooth_iterations(self) -> None: g3d_world = G3DWorld() if self._smooth_faces and self._n_smooth == 0: self._n_smooth = 10 if not g3d_world.display.silent: print('n_smooth set to %d' % self._n_smooth) def __set_smooth_status(self) -> None: g3d_world = G3DWorld() self._smooth_faces = False if self._n_smooth > 0: self._smooth_faces = True if not g3d_world.display.silent: print('smooth activated') def _populate_tags(self) -> None: pass def _read_data_from_file(self) -> None: pass def _generate_mesh_pre(self) -> None: pass def _generate_mesh_post(self) -> int: self._add_boundaries() self.glue_blocks() n_neg_vol = self.check_volumes() self.export_connections() self.export() self.plot_edges() return n_neg_vol def _generate_mesh(self) -> None: pass
[docs] def generate_mesh(self) -> int: """ :return: """ self._generate_mesh_pre() self._generate_mesh() return self._generate_mesh_post()
def _smooth(self) -> None: pass def _add_boundaries(self) -> None: pass
[docs] def export_3D_connections(self) -> None: """ """ pass
[docs] def add_Edge(self, e: Edge) -> None: """ :param e: """ self._edges.append(e)
[docs] def add_Coord(self, c: Coord) -> None: """ :param c: """ self._coordinates.append(c)
[docs] def reset_edges(self) -> None: """ """ self._edges = []
[docs] def reset_coordinates(self) -> None: """ """ self._coordinates = []
[docs] def print_coordinates(self) -> None: """ """ g3d_disp = G3DDisplay() if not g3d_disp.silent: ic = 0 for c in self._coordinates: ic += 1 g3d_disp.header('module reference point %d' % ic) c.print()
[docs] def plot_module_edges(self) -> None: """ """ g3d_world = G3DWorld() if g3d_world.display.plot_edges: for e in self._edges: e.scatter_XY(plt) for c in self._coordinates: c.scatter_XY(plt, m='s') plt.show()
[docs] def plot_edges(self) -> None: """ """ g3d_world = G3DWorld() if g3d_world.display.plot_edges: if len(self._blocks) > 1: for b in self._blocks: for e in b.edges: e.scatter_XY(plt) for c in b.coordinates: c.scatter_XY(plt, m='s') plt.title('block %d edges' % b.im) plt.show() for b in self._blocks: for e in b.edges: e.scatter_XY(plt) for c in b.coordinates: c.scatter_XY(plt, m='s') plt.title('block edges') plt.show()
[docs] @staticmethod def circular_edge(c: Coord, ni: int, **kwargs: Any) -> 'Edge': """ :param c: :param ni: :param kwargs: :return: """ N = 36 direction = Direction.X_DIRECTION for key, value in kwargs.items(): if key == 'N': N = value if key == 'direction': direction = value cyl_theta = 2.0 * np.pi - np.linspace(0., 2.0 * np.pi, N) r = c.length() cyl_x = r * np.cos(cyl_theta) cyl_y = r * np.sin(cyl_theta) cyl_XYZ = np.zeros((N, 3)) for i in range(N): cyl_XYZ[i, 0] = cyl_x[i] cyl_XYZ[i, 1] = cyl_y[i] e = Edge(ni, c, c, distribute=NodeDistribution.CURVE_1, direction=direction, data=cyl_XYZ) return e