RepresentationFormulaLaplace.cpp 5.53 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
/*!
 * @file    RepresentationFormulaLaplace.cpp
 * @author  Michal Merta 
 * @date    November 5, 2013
 * 
 */

#ifdef REPRESENTATIONFORMULALAPLACE_H

namespace bem4i {

template<class LO, class SC>
RepresentationFormulaLaplace<LO, SC>::RepresentationFormulaLaplace( ) {
}

template<class LO, class SC>
RepresentationFormulaLaplace<LO, SC>::RepresentationFormulaLaplace(
  const RepresentationFormulaLaplace& orig
  ) {
}

template<class LO, class SC>
RepresentationFormulaLaplace<LO, SC>::RepresentationFormulaLaplace(
  BESpace<LO, SC> * space,
  Vector<LO, SC> * dirichlet,
  Vector<LO, SC> * neumann,
  int quadOrderDisj
  ) {

  this->space = space;
  this->dirichlet = dirichlet;
  this->neumann = neumann;
  this->quadOrderDisj = quadOrderDisj;
}

template<class LO, class SC>
RepresentationFormulaLaplace<LO, SC>::~RepresentationFormulaLaplace( ) {
}

template<class LO, class SC>
SC RepresentationFormulaLaplace<LO, SC>::evaluate(
  const SCVT * x
  ) const {

  return 0.0;
}

template<class LO, class SC>
void RepresentationFormulaLaplace<LO, SC>::evaluate(
  const SCVT * x,
  LO n,
  bool interior,
  Vector<LO, SC> & values
  ) const {

  int * qOrderDisj = nullptr;
  if( this->quadOrderDisj > 0 ){
    qOrderDisj = new int[ 2 ];
    qOrderDisj[ 0 ] = this->quadOrderDisj;
    qOrderDisj[ 1 ] = this->quadOrderDisj;
  }

  int mpiSize = 1;
  int mpiRank = 0;

  int * numElems = new int[ mpiSize ];
  int * displs = new int[ mpiSize ];
  displs[ 0 ] = 0;
  int nElemsPerMPI = n / mpiSize;
  for ( int i = 0; i < mpiSize - 1; i++ ) {
    numElems[ i ] = nElemsPerMPI;
    displs[ i + 1 ] = displs[ i ] + numElems[ i ];
  }
  numElems[ mpiSize - 1 ] = nElemsPerMPI + ( n % mpiSize );

  SC * valuesPerMPI = new SC[ numElems[ mpiRank ] ];

#pragma omp parallel
  {
    int ompSize = omp_get_num_threads( );
    int ompRank = omp_get_thread_num( );
    LO nLocalElems = numElems[ mpiRank ] / ompSize;
    LO myElems = nLocalElems;
    if ( ompRank == ompSize - 1 ) {
      myElems = nLocalElems + numElems[ mpiRank ] % ompSize;
    }

    BEIntegratorLaplace<LO, SC> integrator( this->space, qOrderDisj );
    Vector<LO, SC> localValues( myElems );
    integrator.representationFormula( x + 3 * ( displs[ mpiRank ] +
      ompRank * nLocalElems ), myElems, *this->dirichlet, *this->neumann,
      interior, localValues );
    for ( LO i = 0; i < myElems; ++i ) {
      valuesPerMPI[ ompRank * nLocalElems + i ] = localValues.get( i );
    }
  }

    memcpy( values.getData( ), valuesPerMPI, sizeof (SC ) * numElems[ 0 ] );

  delete [ ] numElems;
  delete [ ] displs;
  delete [ ] valuesPerMPI;
  if( qOrderDisj ) delete [ ] qOrderDisj;
}

template<class LO, class SC>
void RepresentationFormulaLaplace<LO, SC>::evaluate(
  Mesh<LO, SC> & mesh,
  bool interior,
  Vector<LO, SC> & values
  ) const {

  SCVT * nodes = mesh.getNodes( )->data( );
  LO nNodes = mesh.getNNodes( );

  this->evaluate( nodes, nNodes, interior, values );
}

template<class LO, class SC>
void RepresentationFormulaLaplace<LO, SC>::printParaviewVtu(
  const string & meshFile,
  SC * points,
  LO nPoints,
  int nNodal,
  string * nodeNames,
  Vector<LO, SC> ** nodalData
  ) const {

  std::cout << "Printing  " << meshFile << " ... ";

  std::ofstream file_vtu( meshFile.c_str( ) );

  file_vtu.setf( std::ios::showpoint | std::ios::scientific );
  file_vtu.precision( 10 );

  if ( !file_vtu.is_open( ) ) {
    std::cout << "File could not be opened!" << std::endl;
    return;
  }

  file_vtu << "<?xml version=\"1.0\"?>" << std::endl;
  file_vtu << "<VTKFile type=\"PolyData\" version=\"0.1\">" << std::endl;
  file_vtu << "  <PolyData>" << std::endl;

  file_vtu << "    <Piece NumberOfPoints=\"" << nPoints <<
    "\" NumberOfVerts=\"" << nPoints << "\">" << std::endl;

  file_vtu << "      <Points>" << std::endl;
  file_vtu << "        <DataArray type=\"Float32\" Name=\"points\" NumberOfComponents=\"3\" format=\"ascii\">" << std::endl;

  for ( LO i = 0; i < nPoints; i++ ) {
    file_vtu << "          "
      << points[ 3 * i ] << " "
      << points[ 3 * i + 1 ] << " "
      << points[ 3 * i + 2 ] << std::endl;
  }

  file_vtu << "        </DataArray>" << std::endl;
  file_vtu << "      </Points>" << std::endl;
  file_vtu << "      <Verts>" << std::endl;
  file_vtu << "        <DataArray type=\"Int32\" Name=\"connectivity\" format=\"ascii\">" << std::endl;

  for ( LO i = 0; i < nPoints; i++ ) {
    file_vtu << "          " << i << std::endl;
  }

  file_vtu << "        </DataArray>" << std::endl;
  file_vtu << "        <DataArray type=\"Int32\" Name=\"offsets\" format=\"ascii\">" << std::endl;

  for ( LO offset = 1; offset <= nPoints; offset++ ) {
    file_vtu << "          " << offset << std::endl;
  }

  file_vtu << "        </DataArray>" << std::endl;
  file_vtu << "      </Verts>" << std::endl;

  string header;
  if ( nNodal > 0 ) {
    string header = nodeNames[ 0 ];
    for ( int j = 1; j < nNodal; j++ ) {
      header += "," + nodeNames[ j ];
    }
    file_vtu << "      <PointData Scalars=\"" + header + "\">" << std::endl;
    for ( int j = 0; j < nNodal; j++ ) {
      file_vtu << "        <DataArray type=\"Float32\" Name=\"" + nodeNames[ j ] + "\" format=\"ascii\">" << std::endl;
      for ( LO i = 0; i < nPoints; i++ ) {
        file_vtu << "          " << nodalData[ j ]->get( i ) << std::endl;
      }
      file_vtu << "        </DataArray>" << std::endl;
    }
    file_vtu << "      </PointData>" << std::endl;
  }

  file_vtu << "    </Piece>" << std::endl;
  file_vtu << "  </PolyData>" << std::endl;
  file_vtu << "</VTKFile>" << std::endl;
  file_vtu.close( );

  std::cout << "done." << std::endl;
}

}

#endif