示例#1
0
    def extract_faces(self,
                      id_faces_to_extract,
                      return_index=False,
                      name=None):
        """
        Extracts a new mesh from a selection of faces ids

        Parameters
        ----------
        id_faces_to_extract : ndarray
            Indices of faces that have to be extracted
        return_index: bool, optional
            Flag to output old indices
        name: string, optional
            Name for the new mesh

        Returns
        -------
        Mesh
            A new Mesh instance composed of the extracted faces
        """
        nv = self.nb_vertices

        # Determination of the vertices to keep
        vertices_mask = np.zeros(nv, dtype=bool)
        vertices_mask[self._faces[id_faces_to_extract].flatten()] = True
        id_v = np.arange(nv)[vertices_mask]

        # Building up the vertex array
        v_extracted = self._vertices[id_v]
        new_id__v = np.arange(nv)
        new_id__v[id_v] = np.arange(len(id_v))

        faces_extracted = self._faces[id_faces_to_extract]
        faces_extracted = new_id__v[faces_extracted.flatten()].reshape(
            (len(id_faces_to_extract), 4))

        extracted_mesh = Mesh(v_extracted, faces_extracted)

        for prop in self.__internals__:
            if prop[:4] == "face":
                extracted_mesh.__internals__[prop] = self.__internals__[prop][
                    id_faces_to_extract]

        if name is None:
            if self.name is not None and self.name.startswith(
                    "mesh_extracted_from_"):
                extracted_mesh.name = self.name
            else:
                extracted_mesh.name = f"mesh_extracted_from_{self.name}"
        else:
            extracted_mesh.name = name

        if return_index:
            return extracted_mesh, id_v
        else:
            return extracted_mesh
示例#2
0
    def _read_nemoh_parameters(self, pyHDB, cal_file, test=True):
        """Reads the Nemoh parameters from the Nemoh.cal file

        Parameters
        ----------
        pyHDB : object
            pyHDB object for storing the hydrodynamic database.
        cal_file : str
            Path (relative or absolute) to the Nemoh.cal file
        test : bool, optional
            If True (default), it will test against consistency between frequencies taken for BEM computations and
            meshe's discretization
        """

        if not os.path.exists(cal_file):
            raise IOError("File %s not found" % cal_file)

        with open(cal_file, 'r') as f:

            # Getting root directory of the Nemoh computation project
            abspath = os.path.abspath(cal_file)
            dirname = os.path.dirname(abspath)

            self.dirname = dirname

            def skip_line():
                f.readline()

            # Environment parameters
            skip_line()
            skip_line() # Rho_water.
            skip_line() # Grav.
            skip_line() # Depth
            skip_line() # xreff and yreff.

            # Number of bodies
            skip_line()
            skip_line() # nb_bodies.

            for ibody in range(pyHDB.nb_bodies):
                skip_line()
                meshfile = str(f.readline().split()[0])
                abs_meshfile = os.path.join(dirname, meshfile)

                # Instantiating mesh object
                vertices, faces = load_MAR(abs_meshfile)
                mesh = Mesh(vertices, faces)

                nb_vertices, nb_faces = list(map(int, f.readline().split()[:2]))

                # Verification of mesh information consistency
                assert nb_vertices == mesh.nb_vertices
                assert nb_faces == mesh.nb_faces

                # De-symmetrizing meshes
                with open(abs_meshfile, 'r') as fmesh:
                    _, isym = fmesh.readline().split()
                if int(isym) == 1:  # Mesh is symmetric
                    mesh.symmetrize(Plane([0, 1, 0]))

                mesh.name, _ = os.path.splitext(meshfile)

                # Instantiating BodyDB.
                body = body_db.BodyDB(ibody, pyHDB.nb_bodies, pyHDB.nb_wave_freq, pyHDB.nb_wave_dir , mesh)

                # RADIATION.
                nb_dof = int(f.readline().split()[0])
                for idof in range(nb_dof):
                    dof = f.readline().split()[:7]
                    motion_type = int(dof[0])
                    direction = list(map(float, dof[1:4]))
                    point = list(map(float, dof[4:]))

                    # Center of gravity.
                    body.position = point

                    if motion_type == 1: # Translation.
                        if(direction[0] == 1):
                            body.Motion_mask[0] = 1
                        elif(direction[1] == 1):
                            body.Motion_mask[1] = 1
                        elif(direction[2] == 1):
                            body.Motion_mask[2] = 1
                        else:
                            "The linear motion direction must be a unit vector ex, ey or ez. Generalized modes are not taken into account."
                            exit()
                    elif motion_type == 2: # Rotation.
                        if (direction[0] == 1):
                            body.Motion_mask[3] = 1
                        elif (direction[1] == 1):
                            body.Motion_mask[4] = 1
                        elif (direction[2] == 1):
                            body.Motion_mask[5] = 1
                        else:
                            "The angular motion direction must be a unit vector ex, ey or ez. Generalized modes are not taken into account."
                            exit()
                    else:
                        raise UnknownMotionMode(motion_type, abspath)

                # INTEGRATION.
                nb_force = int(f.readline().split()[0])
                for iforce in range(nb_force):
                    force = f.readline().split()[:7]
                    force_type = int(force[0])
                    direction = list(map(float, force[1:4]))
                    point = list(map(float, force[4:]))
                    if force_type == 1:  # Pure force
                        if (direction[0] == 1):
                            body.Force_mask[0] = 1
                        elif (direction[1] == 1):
                            body.Force_mask[1] = 1
                        elif (direction[2] == 1):
                            body.Force_mask[2] = 1
                        else:
                            "The force direction must be a unit vector ex, ey or ez. Generalized modes are not taken into account."
                            exit()
                    elif force_type == 2:
                        if (direction[0] == 1):
                            body.Force_mask[3] = 1
                            body.point[0,:] = point
                        elif (direction[1] == 1):
                            body.Force_mask[4] = 1
                            body.point[1, :] = point
                        elif (direction[2] == 1):
                            body.Force_mask[5] = 1
                            body.point[2, :] = point
                        else:
                            "The angular motion direction must be a unit vector ex, ey or ez. Generalized modes are not taken into account."
                            exit()
                    else:
                        raise UnknownForceMode(force_type, abspath)

                # Skipping additional lines.
                nb_add_line = int(f.readline().split()[0])
                for i_add_line in range(nb_add_line):
                    skip_line()

                # Add body to pyHDB.
                pyHDB.append(body)

            # Reading discretization.
            skip_line()

            # Getting frequency discretization.
            skip_line()

            # Getting wave directions discretization.
            skip_line()

            # Getting post-processing information
            skip_line()

            # Impulse response function
            data = f.readline().split()[:3]
            # has_irf = bool(int(data[0]))
            # if has_irf:
            #     self.dt_irf, self.tf_irf = map(float, data[1:])

            # TODO: si ok lire  -> remettre en place si besoin...
            # Show pressure
            skip_line()
            # self.has_pressure = bool(int(f.readline().split()[0])) # Enlever le skip_line() ci-dessus si décommenté.

            # Kochin functions.
            data = f.readline().split()[:3]
            pyHDB.has_kochin = bool(float(data[0]))
            if pyHDB.has_kochin:
                pyHDB.nb_angle_kochin = int(data[0])
                pyHDB.min_angle_kochin, pyHDB.max_angle_kochin = list(map(float, data[1:]))
                pyHDB.set_wave_directions_Kochin()


            # # TODO: si ok, lire  -> remettre en place si besoin...
            # # Free surface elevation
            # data = f.readline().split()[:4]
            # has_fs_elevation = bool(int(data[0]))
            # if has_fs_elevation:
            #     self.nx_fs, self.ny_fs = map(int, data[:2])
            #     self.xlim_fs, self.ylim_fs = map(float, data[2:])

        if test:
            self.test_mesh_frequency_consistency(pyHDB)