SurfaceMesh3D.h 16.9 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 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776
/*!
 * @file    SurfaceMesh3D.h
 * @author  Michal Merta 
 * @author  Jan Zapletal
 * @date    July 5, 2013
 * @brief   Header file for class SurfaceMesh3D
 * 
 */

#ifndef SURFACEMESH3D_H
#define SURFACEMESH3D_H

#include <iostream>
#include <fstream>
#include <set>
#include <algorithm>
#include <iterator>
#include "Mesh.h"
#include "Quadratures.h"
#include "Macros.h"
#include "BESpace.h"
#include "PotentialsLaplace.h"
#include "BEIntegratorLaplace.h"
#include "SparseMatrix.h"

namespace bem4i {

/*! 
 * Class representing a surface mesh of a 3D body 
 * 
 */
template<class LO, class SC>
class SurfaceMesh3D : public Mesh<LO, SC> {
  typedef typename GetType<LO, SC>::SCVT SCVT;

  friend class SurfaceMesh3D<LO, SCVT>;
  friend class SurfaceMesh3D<LO, std::complex<SCVT> >;

public:
  //! default constructor
  SurfaceMesh3D( );

  //! read from file
  SurfaceMesh3D(
    const string& meshFile
    );

  //! construct from vector
  SurfaceMesh3D(
    const std::vector< SCVT > & nodes,
    const std::vector< LO > & elems
    );

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

  //! destructor
  virtual ~SurfaceMesh3D( );

  void copyToComplex(
    SurfaceMesh3D< LO, std::complex< SCVT > > & copy
    ) const;

  void copyToReal(
    SurfaceMesh3D< LO, SCVT > & copy
    ) const;

  /*!
   * 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
    );

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

  virtual bool loadSmfOpenFTLOptimize(
    const string& meshFile,
    Vector< LO, SCVT > & fixity,
    Vector< LO, SCVT > & curvature
    );

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

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

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

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

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

    this->printParaviewVtu( meshFile, nullptr, nullptr, nullptr, nullptr );
  }

  /*!
   * Prints mesh to the input format
   * 
   * @param[in]   meshFile string with the target file name
   */
  virtual void print(
    const string & meshFile
    );

  /*!
   * Prints mesh to the smf format
   * 
   * @param[in]   meshFile string with the target file name
   */
  virtual void printSmf(
    const string & meshFile
    );

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

  /*!
   * Prints mesh to the xml paraview file format
   * 
   * @param[in]   meshFile string with the target file name
   */
  virtual void printParaviewVtu(
    const string& meshFile,
    int nNodal,
    string* nodeNames,
    Vector< LO, SC >** nodalData,
    int nElem,
    string* elemNames,
    Vector< LO, SC >** elemData
    );

  /*!
   * Prints mesh to the xml paraview file format
   * 
   * @param[in] meshFile string with the target file name
   */
  void printParaviewVtu(
    const string & meshFile,
    const std::vector< string > * nodeNames,
    const std::vector< Vector< LO, SC >* > * nodalData,
    const std::vector< string > * elemNames,
    const std::vector< Vector< LO, SC >* > * elemData,
    const std::vector< string > * nodeVNames = nullptr,
    const std::vector< Vector< LO, SC >* > * nodalVData = nullptr,
    const std::vector< string > * elemVNames = nullptr,
    const std::vector< Vector< LO, SC >* > * elemVData = nullptr
    ) const;

  /*!
   * Updates node position
   * 
   * @param[in] idx node index
   * @param[in] node new coordinates 
   */
  void setNode(
    LO idx,
    const SCVT * node
    ) {
    this->nodes[ 3 * idx ] = node[ 0 ];
    this->nodes[ 3 * idx + 1 ] = node[ 1 ];
    this->nodes[ 3 * idx + 2 ] = node[ 2 ];
  }

  //! computes area of one element

  inline SCVT computeElemArea(
    LO elemIdx
    ) const {

    const SCVT *x1 = &this->nodes[this->elems[elemIdx * 3] * this->dim];
    const SCVT *x2 = &this->nodes[this->elems[elemIdx * 3 + 1] * this->dim];
    const SCVT *x3 = &this->nodes[this->elems[elemIdx * 3 + 2] * this->dim];

    SCVT u[3], v[3];
    for ( int i = 0; i < 3; i++ ) {
      u[i] = x1[i] - x2[i];
      v[i] = x3[i] - x2[i];
    }

    SCVT s[3];
    s[0] = u[1] * v[2] - u[2] * v[1];
    s[1] = u[2] * v[0] - u[0] * v[2];
    s[2] = u[0] * v[1] - u[1] * v[0];
    return 0.5 * std::sqrt( s[0] * s[0] + s[1] * s[1] + s[2] * s[2] );
  }

  //! returns mesh discretization parameter

  inline SCVT getDiscretizationParameter( ) {
    return std::sqrt(
      *std::max_element( this->area.begin( ), this->area.end( ) ) );
  }

  inline LO getElementOrientationFirst(
    LO i
    ) {
    return this->elementOrientation.first.at( i );
  }

  inline LO getElementOrientationSecond(
    LO i
    ) {
    return this->elementOrientation.second.at( i );
  }

  //! returns the centroid of the given element

  inline void getCentroid(
    LO i,
    SCVT centroid[ 3 ]
    ) const {
    SCVT x1[ 3 ], x2[ 3 ], x3[ 3 ];
    this->getNodes( i, x1, x2, x3 );
    centroid[ 0 ] = 1.0 / 3.0 * ( x1[ 0 ] + x2[ 0 ] + x3[ 0 ] );
    centroid[ 1 ] = 1.0 / 3.0 * ( x1[ 1 ] + x2[ 1 ] + x3[ 1 ] );
    centroid[ 2 ] = 1.0 / 3.0 * ( x1[ 2 ] + x2[ 2 ] + x3[ 2 ] );
  }

  //! returns the centroid of the mesh

  inline void getCentroid(
    SCVT centroid[ 3 ]
    ) const {
    SCVT x[ 3 ];
    centroid[ 0 ] = centroid[ 1 ] = centroid[ 2 ] = 0.0;
    for ( LO i = 0; i < this->nNodes; ++i ) {
      this->getNode( i, x );
      centroid[ 0 ] += x[ 0 ];
      centroid[ 1 ] += x[ 1 ];
      centroid[ 2 ] += x[ 2 ];
    }
    centroid[ 0 ] /= this->nNodes;
    centroid[ 1 ] /= this->nNodes;
    centroid[ 2 ] /= this->nNodes;
  }

  bool isInside(
    const SCVT * x
    ) const;

  void isInside(
    const SCVT * x,
    LO nPoints,
    bool * ret
    ) const;

  void trimEvaluationGrid(
    SurfaceMesh3D< LO, SC > & grid,
    SurfaceMesh3D< LO, SC > & gridTrimmed,
    bool trimInterior = true
    ) const;

  void trimEvaluationGrid(
    SurfaceMesh3D< LO, SC > & grid,
    SurfaceMesh3D< LO, SC > & interior,
    SurfaceMesh3D< LO, SC > & exterior
    ) const;

  /*!
   * computes area of all elements
   */
  virtual void initArea( );

  /*!
   * Initializes mapping from nodes to adjacent elements
   */
  void initNode2Elems( );

  /*!
   * Get elems connected to node idx (need to initNode2Elems first!)
   * 
   * @param[in] idx index of the node
   * @param[in,out] elems vector of elems indices connected to the node idx
   */
  inline void getElements(
    LO idx,
    vector< LO > & elems
    ) const {
    if ( idx < this->node2elems.size( ) )
      elems = this->node2elems[ idx ];
  };

  inline void getElementGlobal(
    LO idx,
    LO * element
    ) const {

    this->getElement( idx, element );
    element[ 0 ] = this->l2gNodes[ element[ 0 ] ];
    element[ 1 ] = this->l2gNodes[ element[ 1 ] ];
    element[ 2 ] = this->l2gNodes[ element[ 2 ] ];
  }

  using Mesh< LO, SC >::getElements;

  /*!
   * Initializes mapping from nodes to adjacent elements
   */
  void initAdditiveCurvature( );

  /*!
   * Returns volume of closed surfaces
   */
  inline SCVT getVolume( ) const;

  /*!
   * Returns total surface area
   */
  inline SCVT getArea( ) const;

  /*!
   * Returns the curvature in node idx
   */
  inline SCVT getCurvature(
    LO idx
    ) const;

  /*!
   * Returns the curvature vector
   */
  inline void getCurvatureVector(
    Vector< LO, SC > & curvature
    ) const;

  /*!
   * Checks the size of curvature vector, returns true if equal to nNodes.
   */
  inline bool curvatureReady( ) const {
    return ( this->additiveCurvature.size( ) == this->nNodes ) ?
      true : false;
  }

  /*!
   * computes outer normal to each element
   */
  void initNormals( );

  /*!
   * changes orientation of surface elements
   */
  void flipNormals( );

  /*!
   * computes outer normal to each element
   */
  void initLocalCoordinates( );

  /*!
   * initializes default mapping from local to global indexing for elements
   */
  void initL2g( );

  /*!
   * Method sets the vector of local to global mapping of elements
   */
  void setL2gElems( std::vector<LO> & l2g ) {
    this->l2gElems = l2g;
  }

  /*!
   * Method sets the vector of local to global mapping of nodes
   */
  void setL2gNodes( std::vector<LO> & l2g ) {
    this->l2gNodes = l2g;
  }

  /*!
   * returns normal to idx-th element
   * 
   * [in]     idx     index of given element
   * [in,out] normal  user allocated 3D array of type SCVT
   */
  inline void getNormal(
    LO idx,
    SCVT* normal
    ) const {
    //std::cout << "Size n: " << this->normals.size() << std::endl;
    normal[0] = this->normals[3 * idx];
    normal[1] = this->normals[3 * idx + 1];
    normal[2] = this->normals[3 * idx + 2];
  }

  /*!
   * returns weighted normal to idx-th node
   * 
   * [in]     idx     index of given node
   * [in,out] normal  user allocated 3D array of type SCVT
   */
  inline void getNormalNodal(
    LO nodeIdx,
    SCVT* normal
    ) const {
    //TODO: need to init first. some flag?
    //this->initNode2Elems( );

    normal[ 0 ] = normal[ 1 ] = normal[ 2 ] = 0.0;
    SCVT area;
    SCVT totalArea = 0;
    LO elemIdx;
    SCVT elemNormal[ 3 ];

    for ( int i = 0; i < this->node2elems[ nodeIdx ].size( ); ++i ) {
      elemIdx = this->node2elems[ nodeIdx ][ i ];
      area = this->getElemArea( elemIdx );
      totalArea += area;
      this->getNormal( elemIdx, elemNormal );
      normal[ 0 ] += area * elemNormal[ 0 ];
      normal[ 1 ] += area * elemNormal[ 1 ];
      normal[ 2 ] += area * elemNormal[ 2 ];
    }

    SCVT norm = std::sqrt( normal[0] * normal[0] + normal[1] * normal[1] +
      normal[2] * normal[2] );
    normal[ 0 ] /= norm;
    normal[ 1 ] /= norm;
    normal[ 2 ] /= norm;
  }

  inline void getR1( LO idx, SCVT* r1, int rotate ) const {
    //std::cout << "Size r1: " << this->r1.size() << std::endl;
    r1[0] = this->r1[3 * ( 3 * idx + rotate )];
    r1[1] = this->r1[3 * ( 3 * idx + rotate ) + 1];
    r1[2] = this->r1[3 * ( 3 * idx + rotate ) + 2];
  }

  inline void getR2( LO idx, SCVT* r2, int rotate ) const {
    r2[0] = this->r2[3 * ( 3 * idx + rotate )];
    r2[1] = this->r2[3 * ( 3 * idx + rotate ) + 1];
    r2[2] = this->r2[3 * ( 3 * idx + rotate ) + 2];
  }

  inline void getAlpha1( LO idx, SCVT &alpha1, int rotate ) const {
    alpha1 = this->alpha1[ 3 * idx + rotate ];
  }

  inline void getAlpha2( LO idx, SCVT &alpha2, int rotate ) const {
    alpha2 = this->alpha2[ 3 * idx + rotate ];
  }

  inline void getSk( LO idx, SCVT &stau, int rotate ) const {
    stau = this->stau[ 3 * idx + rotate ];
  }

  /*!
   * fills given (user preallocated) array with indices of element edges
   * 
   * [in]     idx     index of given element
   * [in,out] edges   user allocated array for 3 LO indices of edges
   */
  inline void getEdges(
    LO idx,
    LO *edges
    ) {
    edges[0] = this->elem2edges[idx * 3];
    edges[1] = this->elem2edges[idx * 3 + 1];
    edges[2] = this->elem2edges[idx * 3 + 2];
  }

  inline void getEdge(
    LO idx,
    LO *edge
    ) {
    edge[0] = this->edges[2 * idx];
    edge[1] = this->edges[2 * idx + 1];
  }

  void getQuadratureNodes(
    SCVT const *x1,
    SCVT const *x2,
    SCVT const *x3,
    SCVT* nodes,
    int order = 5
    ) const;

  //! returns pointer to the auxiliary array of curls

  Vector<LO, SCVT>* getCurls( ) const {
    return this->auxCurl;
  }

  SCVT * getCurls(
    LO i
    ) {
    return this->auxCurl->getData( ) + 9 * i;
  }

  SCVT * getRInv(
    LO i
    ) {
    return this->RInv.data( ) + 6 * i;
  }

  SCVT * getRStInv(
    LO i
    ) {
    return this->RStInv.data( ) + 6 * i;
  }

  /*!
   * Uniformly refines the mesh. 
   */
  void refine(
    int level = 1,
    int type = 2
    );

  /*!
   * Uniformly refines the mesh by quadrisection. 
   */
  void refine_2sect(
    int level
    );

  /*!
   * Uniformly refines the mesh by dividing each side to thirds. 
   */
  void refine_3sect(
    int level
    );

  /*!
   * Maps the mesh nodes to unit ball. 
   */
  void mapToUnitBall( );

  /*!
   * Appends this mesh by mesh. Does not perform any check if the resulting
   * mesh is computationally regular!
   * 
   * @param[ in ] mesh mesh to be appended the this mesh
   */
  void append(
    const SurfaceMesh3D< LO, SC >& mesh
    );

  /*!
   * Scales the mesh nodes by factor. 
   * 
   * @param[ in ] factor scaling factor
   */
  void scale(
    SCVT factor
    ) {
    this->scale( factor, factor, factor );
  }

  /*!
   * Scales the mesh nodes by different factors in each direction. 
   * 
   * @param[ in ] f1 x1 scaling factor
   * @param[ in ] f2 x2 scaling factor
   * @param[ in ] f3 x3 scaling factor
   */
  void scale(
    SCVT f1,
    SCVT f2,
    SCVT f3
    );

  /*!
   * Move centroid to [0,0,0], scale, move back
   * 
   * @param[ in ] factor scaling factor
   */
  void scaleAroundCentroid(
    SCVT factor
    ) {
    this->scaleAroundCentroid( factor, factor, factor );
  }

  /*!
   * Move centroid to [0,0,0], scale, move back
   * 
   * @param[ in ] f1 x1 scaling factor
   * @param[ in ] f2 x2 scaling factor
   * @param[ in ] f3 x3 scaling factor
   */
  void scaleAroundCentroid(
    SCVT f1,
    SCVT f2,
    SCVT f3
    );

  /*!
   * Rotates mesh about the axis u by the angle alpha
   * 
   * @param[ in ] u rotation vector
   * @param[ in ] alpha rotation angle
   */
  void rotate(
    SCVT * v,
    SCVT alpha
    );

  //! Moves centroid to the origin
  void moveCentroidToOrigin( );

  /*!
   * Moves the mesh nodes in direction [x1,x2,x3]. 
   * 
   * @param[ in ] x1 shift in x1 direction
   * @param[ in ] x2 shift in x2 direction
   * @param[ in ] x2 shift in x3 direction
   */
  void move(
    SCVT x1,
    SCVT x2,
    SCVT x3
    );

  /*!
   * Moves the node idx in direction [x1,x2,x3]. 
   * 
   * @param[ in ] idx 
   * @param[ in ] x1 shift in x1 direction
   * @param[ in ] x2 shift in x2 direction
   * @param[ in ] x2 shift in x3 direction
   */
  void move(
    LO idx,
    SCVT x1,
    SCVT x2,
    SCVT x3
    );

  /*!
   * Expands the mesh nodes in normal direction [x1,x2,x3]. 
   * 
   * @param[ in ] alpha moves in alpha*normal
   */
  void expand(
    SCVT alpha = 1.0
    );

  /*!
   * Computes and returns a radius of a mesh
   */
  SCVT getRadius( );

  //! initilizes matrices T for Lame equation
  void assembleT(
    SparseMatrix< LO, SCVT > & T12,
    SparseMatrix< LO, SCVT > & T13,
    SparseMatrix< LO, SCVT > & T23,
    SparseMatrix< LO, SCVT > & T
    );

  inline vector<SCVT>* getNormals( ) {
    return & this->normals;
  }

  LO getL2gElem( LO idx ) {
    return l2gElems[idx];
  }

  LO getL2gNode( LO idx ) {
    return l2gNodes[idx];
  }

protected:

  //! center of mass of each element
  vector<SCVT>* centroids;

  //! vector of outer normals to each element [n1_x, n1_y, n1_z, n2_x, n2_y, ...]
  vector<SCVT> normals;

  std::pair< std::vector< LO >, std::vector< LO > > elementOrientation;

  vector< SCVT > additiveCurvature;

  //! mapping from element to its edges
  vector<LO> elem2edges;

  //! mapping from nodes to elements
  vector< vector< LO > > node2elems;

  //! local coordinate vectors for all orientations
  vector<SCVT> r1, r2, alpha1, alpha2, stau;

  //! vector of curls of p1 functions on all elements
  Vector<LO, SCVT> *auxCurl;

  //! inverse transpose of reference triangle mapping Jacobian
  // valid for Sauter-Schwab reference element!!!
  vector<SCVT> RInv;

  //! inverse transpose of reference triangle mapping Jacobian
  // valid for Steinbach reference element!!!
  vector<SCVT> RStInv;

  //! mapping from local indexing to global indexing (for MPI distributed)
  vector<LO> l2gElems;

  //! mapping from local indexing to global indexing (for MPI distributed)
  vector<LO> l2gNodes;

  //! initializes array of edges
  void initEdges( );

private:

  //! get local coordinates for a specific rotation of a triangle
  void getLocalCoordinates(
    const SCVT* x1,
    const SCVT* x2,
    const SCVT* x3,
    SCVT* r1,
    SCVT* r2,
    SCVT* stau,
    SCVT* alpha1,
    SCVT* alpha2
    ) const;

  //! initializes vector of curls
  void initCurl( );

  //! compute curvature in one node
  SCVT computeNodalAdditiveCurvature( LO i );

};

}

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

#endif /* SURFACEMESH3D_H */