LifeV
RBFrescaledVectorial.hpp
Go to the documentation of this file.
1 //@HEADER
2 /*
3 *******************************************************************************
4 
5  Copyright (C) 2004, 2005, 2007 EPFL, Politecnico di Milano, INRIA
6  Copyright (C) 2010 EPFL, Politecnico di Milano, Emory University
7 
8  This file is part of LifeV.
9 
10  LifeV is free software; you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  LifeV is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with LifeV. If not, see <http://www.gnu.org/licenses/>.
22 
23 *******************************************************************************
24 */
25 //@HEADER
26 
27 /*!
28  @file
29  @brief A short description of the file content
30 
31  @author Davide Forti <forti@mathicsepc48.epfl.ch>
32  @date 14 Mar 2013
33 
34  A more detailed description of the file (if necessary)
35  */
36 
37 #ifndef RBFRESCALEDVECTORIAL_H
38 #define RBFRESCALEDVECTORIAL_H 1
39 
40 #include <lifev/core/interpolation/RBFInterpolation.hpp>
41 
42 namespace LifeV {
43 
44 template <typename mesh_Type>
45 class RBFrescaledVectorial: public RBFInterpolation<mesh_Type>
46 {
47 public:
48 
50 
51  typedef VectorEpetra vector_Type;
53 
54  typedef MatrixEpetra<double> matrix_Type;
56 
57  typedef std::vector<int> flagContainer_Type;
58 
60 
63 
64  typedef GhostHandler<mesh_Type> neighbors_Type;
66 
67  typedef LifeV::Preconditioner basePrec_Type;
69 
70  typedef LifeV::PreconditionerIfpack prec_Type;
72 
74 
76 
77  virtual ~RBFrescaledVectorial();
78 
79  void setup ( meshPtr_Type fullMeshKnown, meshPtr_Type localMeshKnown, meshPtr_Type fullMeshUnknown, meshPtr_Type localMeshUnknown, flagContainer_Type flags );
80 
81  void setupRBFData (vectorPtr_Type KnownField, vectorPtr_Type UnknownField, GetPot datafile, parameterList_Type belosList);
82 
83  void buildOperators();
84 
85  void interpolationOperator();
86 
87  void projectionOperator();
88 
89  void buildRhs();
90 
92 
93  void identifyNodes (meshPtr_Type LocalMesh, std::unordered_set<ID>& GID_nodes, vectorPtr_Type CheckVector);
94 
95  bool isInside (ID pointMarker, flagContainer_Type Flags);
96 
97  double rbf (double x1, double y1, double z1, double x2, double y2, double z2, double radius);
98 
99  void interpolate();
100 
101  void solution (vectorPtr_Type& Solution);
102 
103  void solutionrbf (vectorPtr_Type& Solution_rbf);
104 
105  void updateRhs(vectorPtr_Type newRhs);
106 
107  void setRadius ( double radius );
108 
109 private:
110 
133  double M_radius;
134 };
135 
136 template <typename mesh_Type>
138  M_radius(0)
139 {}
140 
141 template <typename mesh_Type>
143 {}
144 
145 
146 template <typename mesh_Type>
147 void RBFrescaledVectorial<mesh_Type>::setup ( meshPtr_Type fullMeshKnown, meshPtr_Type localMeshKnown, meshPtr_Type fullMeshUnknown, meshPtr_Type localMeshUnknown, flagContainer_Type flags )
148 {
149  M_fullMeshKnown = fullMeshKnown;
150  M_localMeshKnown = localMeshKnown;
151  M_fullMeshUnknown = fullMeshUnknown;
152  M_localMeshUnknown = localMeshUnknown;
153  M_flags = flags;
154 }
155 
156 template <typename mesh_Type>
157 void RBFrescaledVectorial<mesh_Type>::setRadius ( double radius )
158 {
159  M_radius = radius;
160 }
161 
162 template <typename mesh_Type>
163 void RBFrescaledVectorial<mesh_Type>::setupRBFData (vectorPtr_Type KnownField, vectorPtr_Type UnknownField, GetPot datafile, parameterList_Type belosList)
164 {
165  M_knownField = KnownField;
166  M_unknownField = UnknownField;
167  M_datafile = datafile;
168  M_belosList = belosList;
169 }
170 
171 template <typename Mesh>
173 {
174  this->interpolationOperator();
175  this->projectionOperator();
176  this->buildRhs();
177  this->interpolateCostantField();
178 }
179 
180 template <typename Mesh>
182 {
183  ASSERT(M_radius!=0, "Please set the basis radius using RBFrescaledScalar<mesh_Type>::setRadius(double radius)")
184  this->identifyNodes (M_localMeshKnown, M_GIdsKnownMesh, M_knownField);
185  M_neighbors.reset ( new neighbors_Type ( M_fullMeshKnown, M_localMeshKnown, M_knownField->mapPtr(), M_knownField->mapPtr()->commPtr() ) );
186  if (M_flags[0] == -1)
187  {
188  M_neighbors->setUpNeighbors ();
189  }
190  else
191  {
192  M_neighbors->createPointPointNeighborsList (M_flags);
193  }
194 
195  int LocalNodesNumber = M_GIdsKnownMesh.size();
196 
197  std::vector<std::unordered_set<ID> > MatrixGraph (LocalNodesNumber);
198  int* ElementsPerRow = new int[LocalNodesNumber];
199  int* GlobalID = new int[LocalNodesNumber];
200  int k = 0;
201  int Max_entries = 0;
202 
203  for (std::unordered_set<ID>::iterator it = M_GIdsKnownMesh.begin(); it != M_GIdsKnownMesh.end(); ++it)
204  {
205  GlobalID[k] = *it;
206  MatrixGraph[k] = M_neighbors->neighborsWithinRadius (M_radius, GlobalID[k]);
207  MatrixGraph[k].insert (GlobalID[k]);
208  ElementsPerRow[k] = MatrixGraph[k].size();
209  if (ElementsPerRow[k] > Max_entries)
210  {
211  Max_entries = ElementsPerRow[k];
212  }
213  ++k;
214  }
215 
216  M_interpolationOperatorMap.reset (new map_Type (-1, LocalNodesNumber, GlobalID, M_knownField->mapPtr()->commPtr() ) );
217  M_interpolationOperator.reset (new matrix_Type (*M_interpolationOperatorMap, ElementsPerRow) );
218 
219  int* Indices = new int[Max_entries];
220  double* Values = new double[Max_entries];
221 
222  for ( int i = 0 ; i < LocalNodesNumber; ++i )
223  {
224  k = 0;
225  for ( std::unordered_set<ID>::iterator it = MatrixGraph[i].begin(); it != MatrixGraph[i].end(); ++it)
226  {
227  Indices[k] = *it;
228  Values[k] = rbf ( M_fullMeshKnown->point (GlobalID[i]).x(),
229  M_fullMeshKnown->point (GlobalID[i]).y(),
230  M_fullMeshKnown->point (GlobalID[i]).z(),
231  M_fullMeshKnown->point (*it).x(),
232  M_fullMeshKnown->point (*it).y(),
233  M_fullMeshKnown->point (*it).z(),
234  M_radius);
235  ++k;
236  }
237  M_interpolationOperator->matrixPtr()->InsertGlobalValues (GlobalID[i], k, Values, Indices);
238  }
239  M_interpolationOperator->globalAssemble();
240  delete[] Indices;
241  delete[] Values;
242  delete[] ElementsPerRow;
243  delete[] GlobalID;
244 }
245 
246 template <typename mesh_Type>
248 {
249  this->identifyNodes (M_localMeshUnknown, M_GIdsUnknownMesh, M_unknownField);
250 
251  int LocalNodesNumber = M_GIdsUnknownMesh.size();
252 
253  std::vector<std::unordered_set<ID> > MatrixGraph (LocalNodesNumber);
254  int* ElementsPerRow = new int[LocalNodesNumber];
255  int* GlobalID = new int[LocalNodesNumber];
256  int k = 0;
257  int Max_entries = 0;
258  double d;
259  double d_min;
260  int nearestPoint;
261 
262  for (std::unordered_set<ID>::iterator it = M_GIdsUnknownMesh.begin(); it != M_GIdsUnknownMesh.end(); ++it)
263  {
264  GlobalID[k] = *it;
265  d_min = 100;
266  for (int j = 0; j < M_fullMeshKnown->numVertices(); ++j)
267  {
268  if ( M_flags[0] == -1 || this->isInside (M_fullMeshKnown->point (j).markerID(), M_flags) )
269  {
270  d = std::sqrt ( pow (M_fullMeshKnown->point (j).x() - M_fullMeshUnknown->point (GlobalID[k]).x(), 2)
271  + pow (M_fullMeshKnown->point (j).y() - M_fullMeshUnknown->point (GlobalID[k]).y(), 2)
272  + pow (M_fullMeshKnown->point (j).z() - M_fullMeshUnknown->point (GlobalID[k]).z(), 2) );
273  if (d < d_min)
274  {
275  d_min = d;
276  nearestPoint = M_fullMeshKnown->point (j).id();
277  }
278  }
279  }
280 
281  MatrixGraph[k] = M_neighbors->neighborsWithinRadius (M_radius, nearestPoint);
282  MatrixGraph[k].insert (nearestPoint);
283  ElementsPerRow[k] = MatrixGraph[k].size();
284  if (ElementsPerRow[k] > Max_entries)
285  {
286  Max_entries = ElementsPerRow[k];
287  }
288  ++k;
289  }
290 
291  M_projectionOperatorMap.reset (new map_Type (-1, LocalNodesNumber, GlobalID, M_unknownField->mapPtr()->commPtr() ) );
292  M_projectionOperator.reset (new matrix_Type (*M_projectionOperatorMap, ElementsPerRow) );
293 
294  int* Indices = new int[Max_entries];
295  double* Values = new double[Max_entries];
296 
297  for ( int i = 0 ; i < LocalNodesNumber; ++i )
298  {
299  k = 0;
300  for ( std::unordered_set<ID>::iterator it = MatrixGraph[i].begin(); it != MatrixGraph[i].end(); ++it)
301  {
302  Indices[k] = *it;
303  Values[k] = rbf ( M_fullMeshUnknown->point (GlobalID[i]).x(),
304  M_fullMeshUnknown->point (GlobalID[i]).y(),
305  M_fullMeshUnknown->point (GlobalID[i]).z(),
306  M_fullMeshKnown->point (*it).x(),
307  M_fullMeshKnown->point (*it).y(),
308  M_fullMeshKnown->point (*it).z(),
309  M_radius);
310  ++k;
311  }
312  M_projectionOperator->matrixPtr()->InsertGlobalValues (GlobalID[i], k, Values, Indices);
313  }
314  M_projectionOperator->globalAssemble (M_interpolationOperatorMap, M_projectionOperatorMap);
315  delete Indices;
316  delete Values;
317  delete ElementsPerRow;
318  delete GlobalID;
319 }
320 
321 template <typename mesh_Type>
322 void RBFrescaledVectorial<mesh_Type>::buildRhs()
323 {
324  M_RhsF1.reset (new vector_Type (*M_interpolationOperatorMap) );
325  M_RhsF2.reset (new vector_Type (*M_interpolationOperatorMap) );
326  M_RhsF3.reset (new vector_Type (*M_interpolationOperatorMap) );
327  M_RhsOne.reset (new vector_Type (*M_interpolationOperatorMap) );
328 
329  M_RhsF1->subset (*M_knownField, *M_interpolationOperatorMap, 0, 0);
330  M_RhsF2->subset (*M_knownField, *M_interpolationOperatorMap, M_knownField->size()/3, 0);
331  M_RhsF3->subset (*M_knownField, *M_interpolationOperatorMap, M_knownField->size()/3*2, 0);
332  *M_RhsOne += 1;
333 }
334 
335 template <typename mesh_Type>
337 {
338  vectorPtr_Type gamma_one;
339  gamma_one.reset (new vector_Type (*M_interpolationOperatorMap) );
340 
341  // Preconditioner
342  prec_Type* precRawPtr;
343  basePrecPtr_Type precPtr;
344  precRawPtr = new prec_Type;
345  precRawPtr->setDataFromGetPot ( M_datafile, "prec" );
346  precPtr.reset ( precRawPtr );
347 
348  LinearSolver solverOne;
349  solverOne.setCommunicator ( M_knownField->mapPtr()->commPtr() );
350  solverOne.setParameters ( *M_belosList );
351  solverOne.setPreconditioner ( precPtr );
352 
353  solverOne.setOperator (M_interpolationOperator);
354  solverOne.setRightHandSide ( M_RhsOne );
355  solverOne.solve ( gamma_one );
356 
357  M_rbf_one.reset (new vector_Type (*M_projectionOperatorMap) );
358  M_projectionOperator->multiply (false, *gamma_one, *M_rbf_one);
359 }
360 
361 template <typename mesh_Type>
363 {
364  vectorPtr_Type gamma_f1;
365  gamma_f1.reset (new vector_Type (*M_interpolationOperatorMap) );
366 
367  vectorPtr_Type gamma_f2;
368  gamma_f2.reset (new vector_Type (*M_interpolationOperatorMap) );
369 
370  vectorPtr_Type gamma_f3;
371  gamma_f3.reset (new vector_Type (*M_interpolationOperatorMap) );
372 
373 
374  // Preconditioner
375  prec_Type* precRawPtr;
376  basePrecPtr_Type precPtr;
377  precRawPtr = new prec_Type;
378  precRawPtr->setDataFromGetPot ( M_datafile, "prec" );
379  precPtr.reset ( precRawPtr );
380 
381  LinearSolver solverRBF1;
382  solverRBF1.setCommunicator ( M_knownField->mapPtr()->commPtr() );
383  solverRBF1.setParameters ( *M_belosList );
384  solverRBF1.setPreconditioner ( precPtr );
385 
386  solverRBF1.setOperator (M_interpolationOperator);
387  solverRBF1.setRightHandSide (M_RhsF1);
388  solverRBF1.solve (gamma_f1);
389 
390  LinearSolver solverRBF2;
391  solverRBF2.setCommunicator ( M_knownField->mapPtr()->commPtr() );
392  solverRBF2.setParameters ( *M_belosList );
393  solverRBF2.setPreconditioner ( precPtr );
394 
395  solverRBF2.setOperator (M_interpolationOperator);
396  solverRBF2.setRightHandSide (M_RhsF2);
397  solverRBF2.solve (gamma_f2);
398 
399  LinearSolver solverRBF3;
400  solverRBF3.setCommunicator ( M_knownField->mapPtr()->commPtr() );
401  solverRBF3.setParameters ( *M_belosList );
402  solverRBF3.setPreconditioner ( precPtr );
403 
404  solverRBF3.setOperator (M_interpolationOperator);
405  solverRBF3.setRightHandSide (M_RhsF3);
406  solverRBF3.solve (gamma_f3);
407 
408 
409  vectorPtr_Type rbf_f1;
410  rbf_f1.reset (new vector_Type (*M_projectionOperatorMap) );
411 
412  vectorPtr_Type solution1;
413  solution1.reset (new vector_Type (*M_projectionOperatorMap) );
414 
415  M_projectionOperator->multiply (false, *gamma_f1, *rbf_f1);
416 
417  *solution1 = *rbf_f1;
418  *solution1 /= *M_rbf_one;
419 
420  vectorPtr_Type rbf_f2;
421  rbf_f2.reset (new vector_Type (*M_projectionOperatorMap) );
422 
423  vectorPtr_Type solution2;
424  solution2.reset (new vector_Type (*M_projectionOperatorMap) );
425 
426  M_projectionOperator->multiply (false, *gamma_f2, *rbf_f2);
427 
428  *solution2 = *rbf_f2;
429  *solution2 /= *M_rbf_one;
430 
431  vectorPtr_Type rbf_f3;
432  rbf_f3.reset (new vector_Type (*M_projectionOperatorMap) );
433 
434  vectorPtr_Type solution3;
435  solution3.reset (new vector_Type (*M_projectionOperatorMap) );
436 
437  M_projectionOperator->multiply (false, *gamma_f3, *rbf_f3);
438 
439  *solution3 = *rbf_f3;
440  *solution3 /= *M_rbf_one;
441 
442  M_unknownField_rbf.reset (new vector_Type (M_unknownField->map() ) );
443  M_unknownField_rbf->subset (*rbf_f1, *M_projectionOperatorMap, 0, 0);
444  M_unknownField_rbf->subset (*rbf_f2, *M_projectionOperatorMap, 0, M_unknownField->size()/3);
445  M_unknownField_rbf->subset (*rbf_f3, *M_projectionOperatorMap, 0, M_unknownField->size()/3*2);
446 
447  M_unknownField->subset (*solution1, *M_projectionOperatorMap, 0, 0);
448  M_unknownField->subset (*solution2, *M_projectionOperatorMap, 0, M_unknownField->size()/3);
449  M_unknownField->subset (*solution3, *M_projectionOperatorMap, 0, M_unknownField->size()/3*2);
450 
451 }
452 
453 template <typename mesh_Type>
454 void RBFrescaledVectorial<mesh_Type>::identifyNodes (meshPtr_Type LocalMesh, std::unordered_set<ID>& GID_nodes, vectorPtr_Type CheckVector)
455 {
456  if (M_flags[0] == -1)
457  {
458  for ( UInt i = 0; i < LocalMesh->numVertices(); ++i )
459  if (CheckVector->blockMap().LID ( static_cast<EpetraInt_Type> (LocalMesh->point (i).id()) ) != -1)
460  {
461  GID_nodes.insert (LocalMesh->point (i).id() );
462  }
463  }
464  else
465  {
466  for ( UInt i = 0; i < LocalMesh->numVertices(); ++i )
467  if ( this->isInside (LocalMesh->point (i).markerID(), M_flags) )
468  if (CheckVector->blockMap().LID ( static_cast<EpetraInt_Type> (LocalMesh->point (i).id()) ) != -1)
469  {
470  GID_nodes.insert (LocalMesh->point (i).id() );
471  }
472  }
473 }
474 
475 template <typename mesh_Type>
476 bool RBFrescaledVectorial<mesh_Type>::isInside (ID pointMarker, flagContainer_Type flags)
477 {
478  int check = 0;
479  for (UInt i = 0; i < flags.size(); ++i)
480  if (pointMarker == flags[i])
481  {
482  ++check;
483  }
484  return (check > 0) ? true : false;
485 }
486 
487 
488 template <typename mesh_Type>
489 double RBFrescaledVectorial<mesh_Type>::rbf (double x1, double y1, double z1, double x2, double y2, double z2, double radius)
490 {
491  double distance = sqrt ( pow (x1 - x2, 2) + pow (y1 - y2, 2) + pow (z1 - z2, 2) );
492  return pow (1 - distance / radius, 4) * (4 * distance / radius + 1);
493 }
494 
495 template <typename mesh_Type>
497 {
498  *M_RhsF1 *= 0;
499  M_RhsF1->subset (*newRhs, *M_interpolationOperatorMap, 0, 0);
500  *M_RhsF2 *= 0;
501  M_RhsF2->subset (*newRhs, *M_interpolationOperatorMap, newRhs->size()/3, 0);
502  *M_RhsF3 *= 0;
503  M_RhsF3->subset (*newRhs, *M_interpolationOperatorMap, newRhs->size()/3*2, 0);
504 }
505 
506 template <typename mesh_Type>
507 void RBFrescaledVectorial<mesh_Type>::solution (vectorPtr_Type& Solution)
508 {
509  Solution = M_unknownField;
510 }
511 
512 template <typename mesh_Type>
513 void RBFrescaledVectorial<mesh_Type>::solutionrbf (vectorPtr_Type& Solution_rbf)
514 {
515  Solution_rbf = M_unknownField_rbf;
516 }
517 
518 //! Factory create function
519 template <typename mesh_Type>
521 {
522  return new RBFrescaledVectorial< mesh_Type > ();
523 }
524 namespace
525 {
528 }
529 
530 } // Namespace LifeV
531 
532 #endif /* RBFRESCALEDVECTORIAL_H */
void updateRhs(vectorPtr_Type newRhs)
std::unordered_set< ID > idContainer_Type
std::shared_ptr< neighbors_Type > neighborsPtr_Type
LifeV::Preconditioner basePrec_Type
RBFInterpolation< mesh_Type > * createRBFrescaledVectorial()
Factory create function.
void identifyNodes(meshPtr_Type LocalMesh, std::unordered_set< ID > &GID_nodes, vectorPtr_Type CheckVector)
void setupRBFData(vectorPtr_Type KnownField, vectorPtr_Type UnknownField, GetPot datafile, parameterList_Type belosList)
std::shared_ptr< prec_Type > precPtr_Type
void solution(vectorPtr_Type &Solution)
Epetra_Import const & importer()
Getter for the Epetra_Import.
Definition: MapEpetra.cpp:394
MatrixEpetra< double > matrix_Type
GhostHandler< mesh_Type > neighbors_Type
#define ASSERT(X, A)
Definition: LifeAssert.hpp:90
uint32_type ID
IDs.
Definition: LifeV.hpp:194
double rbf(double x1, double y1, double z1, double x2, double y2, double z2, double radius)
void solutionrbf(vectorPtr_Type &Solution_rbf)
std::shared_ptr< MapEpetra > mapPtr_Type
Teuchos::RCP< Teuchos::ParameterList > parameterList_Type
std::shared_ptr< matrix_Type > matrixPtr_Type
std::shared_ptr< mesh_Type > meshPtr_Type
bool isInside(ID pointMarker, flagContainer_Type Flags)
LifeV::PreconditionerIfpack prec_Type
std::shared_ptr< vector_Type > vectorPtr_Type
std::shared_ptr< basePrec_Type > basePrecPtr_Type
void importFromHDF5(std::string const &fileName="ghostmap")
Import neighbor lists to an hdf5 file.
void setup(meshPtr_Type fullMeshKnown, meshPtr_Type localMeshKnown, meshPtr_Type fullMeshUnknown, meshPtr_Type localMeshUnknown, flagContainer_Type flags)