Mesh.h 6.69 KB
Newer Older
Jan Zapletal's avatar
Jan Zapletal committed
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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
/*!
 * @file    Mesh.h
 * @author  Michal Merta 
 * @author  Jan Zapletal
 * @date    July 4, 2013
 * @brief   Header file for abstract class Mesh
 * 
 */

#ifndef MESH_H
#define	MESH_H

#include <string>
#include <vector>
#include <set>
#include <cmath>
#include "Vector.h"
#include "Macros.h"

using std::string;
using std::vector;

namespace bem4i {

#ifdef OPTIMIZATION
template< class LO, class SC >
class OpenMeshWrapper;
#endif

//! struct of multipole coefficients of cluster - for fast BEM

template<class LO, class SC>
struct MultipoleMoments {
  std::complex<SC>* momentsSL; // multipole moments of a cell - filled by the 
  // upward method of appropriate kernel 
  std::complex<SC>* momentsDL; // moments for double layer potential

  std::complex<SC>* momTilde;
  std::complex<SC>* momTildeDL;

  std::complex<SC>* localExpCoefSL;
  std::complex<SC>* localExpCoefDL;

  ~MultipoleMoments( ) {
    if ( momentsSL )
      delete [] momentsSL;
    if ( momentsDL )
      delete [] momentsDL;
    if ( momTilde )
      delete [] momTilde;
    if ( momTildeDL )
      delete [] momTildeDL;
    if ( localExpCoefSL )
      delete [] localExpCoefSL;
    if ( localExpCoefDL )
      delete [] localExpCoefDL;
  }
};

//! cluster of boundary elements (for creation of cluster tree)  

template<class LO, class SC>
struct BECluster {
  typedef typename GetType<LO, SC>::SCVT SCVT;

  //! coordinates of the centroid
  SCVT *centroid;

  //! radius of the cluster
  SCVT radius;

  //! mass (length/area) of the cluster
  SCVT mass;

  //! number of elements                          
  LO nelems;

  //! number of nodes
  LO nnodes;

  //! element indices
  std::vector<LO>* elems;

  //! global nodes indices
  std::set<LO>* nodes;

  // The following data are used in fast multipole method

  MultipoleMoments<LO, SC>* moments;

  BECluster<LO, SC> *parent;


  std::vector<BECluster<LO, SC>* > *admClusters;

  // M2L and L2L coefficients
  std::complex<SCVT> **M2L;
  std::complex<SCVT> **L2L;

  BECluster( ) {
    centroid = nullptr;
    elems = nullptr;
    moments = nullptr;
    admClusters = nullptr;
    M2L = nullptr;
    L2L = nullptr;
    nodes = nullptr;
  }

  ~BECluster( ) {
    if ( centroid ) {
      delete [] centroid;
    }
    if ( elems ) {
      delete elems;
    }
    if ( nodes ) {
      delete nodes;
    }
    if ( moments )
      delete moments;
    if ( admClusters )
      delete admClusters;
  }
};

/*! 
 * Abstract class representing an arbitrary mesh in 2D or 3D 
 * 
 */
template<class LO, class SC>
class Mesh {
  typedef typename GetType<LO, SC>::SCVT SCVT;

public:

  //! default constructor
  Mesh( );

  //! copy constructor
  Mesh( const Mesh& orig );

  //! destructor
  virtual ~Mesh( );

  //! returns a dimension of a mesh

  inline int getDim( ) const {
    return dim;
  }

  //! returns number of nodes

  inline LO getNNodes( ) const {
    return nNodes;
  }

  //! returns number of edges

  inline LO getNEdges( ) const {
    return nEdges;
  }

  //! returns number of elements

  inline LO getNElements( ) const {
    return nElems;
  }

  /*!
   * Returns element of a mesh
   * 
   * @param   idx index of element to return
   * @param   element pointer to <em>preallocated</em> array of LOs where the element indices will be copied
   */
  inline void getElement(
      LO idx,
      LO * element
      ) const {

    memcpy( element, &elems[idx * nNodesPerElem], sizeof (LO ) * nNodesPerElem );
  }

  /*!
   * Returns node of a mesh
   * 
   * @param   idx index of node to return
   * @param   node pointer to <em>preallocated</em> array of doubles where the node coordinates will be copied
   */
  inline void getNode( LO idx, SCVT* node ) const {
    memcpy( node, &nodes[idx * dim], sizeof (SCVT ) * dim );
  }

  /*!
   * Returns three nodes of a triangular mesh associated with a given element
   * 
   * @param[in]       elemIdx   index of element
   * @param[in,out]   x1        pointer to preallocated array for first node
   * @param[in,out]   x2        pointer to preallocated array for second node
   * @param[in,out]   x3        pointer to preallocated array for third node
   */
  inline void getNodes( LO elemIdx, SCVT * x1, SCVT * x2, SCVT * x3 ) const {
    memcpy( x1, &nodes[elems[elemIdx * 3] * dim], sizeof (SCVT ) * dim );
    memcpy( x2, &nodes[elems[elemIdx * 3 + 1] * dim], sizeof (SCVT ) * dim );
    memcpy( x3, &nodes[elems[elemIdx * 3 + 2] * dim], sizeof (SCVT ) * dim );
  }

  inline vector<SCVT>* getAreas( ) {
    return & this->area;
  }




  /*!
   * Loads mesh from a default file format
   * 
   * @param   meshFile a string with a path to the mesh file
   */
  virtual bool load( const string& meshFile, SCVT scaleFactor = 1.0 ) = 0;

  /*!
   * Loads mesh from a netgen file format
   * 
   * @param   meshFile a string with a path to the mesh file
   */
  virtual bool loadFromNetgen( const string& meshFile ) = 0;

  /*!
   * Loads mesh from a paraview file format
   * 
   * @param   meshFile a string with a path to the mesh file
   */
  virtual bool loadFromParaview( const string& meshFile ) = 0;

  /*!
   * Prints mesh info to stdout
   */
  virtual void printInfo( ) = 0;

  /*!
   * Prints mesh to the legacy paraview file format
   * 
   * @param[in]   meshFile string with the target file name
   */
  virtual void printParaviewVtk( const string& meshFile ) = 0;

  /*!
   * Prints mesh to the xml paraview file format
   * 
   * @param[in]   meshFile string with the target file name
   */
  virtual void printParaviewVtu( const string& meshFile ) = 0;

  /*!
   * computes areas of all elements
   */
  virtual void initArea( ) = 0;

  //! returns area of given element (must be precomputed by initArea())

  SCVT getElemArea( LO elemIdx ) const {
    return (SCVT) area[elemIdx];
  }

  //! returns number of nodes per one element

  inline int getNNodesPerElem( ) {
    return nNodesPerElem;
  }

  inline vector<LO> * getElements( ) {
    return & this->elems;
  }

  inline vector<SCVT> * getNodes( ) {
    return & this->nodes;
  }

protected:

  //! dimension of the mesh
  int dim;

  //! number of mesh nodes
  LO nNodes;

  //! number of mesh elements
  LO nElems;

  //! number of edges
  LO nEdges;

  //! number of nodes per one element
  int nNodesPerElem;

  //! 1D vector of node coordinates
  /*! Nodes stored as [x1_1, x1_2, x1_3, x2_1, x2_2, x2_3, ...]. */
  vector<SCVT> nodes;

  //! 1D vector of elements mapping to the vector of nodes
  /*! Elements stored as [node1_1, node1_2, node1_3, node2_1, node2_2, node2_3, ...]. */
  vector<LO> elems;

  //! indices of edges
  vector<LO> edges;

  //! vector of element areas
  vector<SCVT> area;

  //! total area of a mesh
  SCVT totalArea;

private:

};

}

// include .cpp file to overcome linking problems due to templates
#include "Mesh.cpp"

#endif	/* MESH_H */