LifeV
lifev/level_set/testsuite/basic_test/main.cpp
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
30 
31  @author Samuel Quinodoz <samuel.quinodoz@epfl.ch>
32  @date 08-10-2010
33 
34  */
35 
36 
37 #include <Epetra_ConfigDefs.h>
38 #ifdef EPETRA_MPI
39 #include <mpi.h>
40 #include <Epetra_MpiComm.h>
41 #else
42 #include <Epetra_SerialComm.h>
43 #endif
44 
45 
46 #include <lifev/core/LifeV.hpp>
47 
48 #include <lifev/core/algorithm/PreconditionerIfpack.hpp>
49 #include <lifev/core/algorithm/PreconditionerML.hpp>
50 
51 #include <lifev/core/filter/ExporterEnsight.hpp>
52 #include <lifev/core/filter/ExporterHDF5.hpp>
53 
54 #include <lifev/core/fem/BCManage.hpp>
55 
56 #include <lifev/core/mesh/MeshPartitioner.hpp>
57 #include <lifev/core/mesh/RegionMesh3DStructured.hpp>
58 #include <lifev/core/mesh/RegionMesh.hpp>
59 
60 #include <lifev/core/mesh/MeshData.hpp>
61 
62 #include <lifev/level_set/solver/LevelSetSolver.hpp>
63 
64 using namespace LifeV;
65 
66 namespace
67 {
68 static bool regIF = (PRECFactory::instance().registerProduct ( "Ifpack", &createIfpack ) );
69 static bool regML = (PRECFactory::instance().registerProduct ( "ML", &createML ) );
70 }
71 
72 
73 Real betaFct ( const Real& /* t */, const Real& /* x */, const Real& /* y */, const Real& /* z */, const ID& i )
74 {
75  if (i == 0)
76  {
77  return 1.;
78  }
79  return 0;
80 }
81 
82 Real initLSFct ( const Real& /* t */, const Real& x, const Real& y, const Real& z, const ID& /* i */)
83 {
84  return sqrt ( (x + 0.7) * (x + 0.7) + y * y + z * z ) - 0.3;
85 }
86 
87 Real exactSolution ( const Real& t, const Real& x, const Real& y, const Real& z, const ID& /* i */)
88 {
89  return sqrt ( (x + 0.7 - t) * (x + 0.7 - t) + y * y + z * z ) - 0.3;
90 }
91 
92 
96 
97 int
98 main ( int argc, char** argv )
99 {
100 
101 #ifdef HAVE_MPI
102  MPI_Init (&argc, &argv);
103  std::shared_ptr<Epetra_Comm> Comm (new Epetra_MpiComm (MPI_COMM_WORLD) );
104 #else
105  std::shared_ptr<Epetra_Comm> Comm (new Epetra_SerialComm);
106 #endif
107 
108  const bool verbose (Comm->MyPID() == 0);
109 
110  // Read first the data needed
111 
112  if (verbose)
113  {
114  std::cout << " -- Reading the data ... " << std::flush;
115  }
116  GetPot dataFile ( "data" );
117  if (verbose)
118  {
119  std::cout << " done ! " << std::endl;
120  }
121 
122  const UInt Nelements (dataFile ("mesh/nelements", 20) );
123  if (verbose)
124  {
125  std::cout << " ---> Number of elements : " << Nelements << std::endl;
126  }
127 
128  // Build and partition the mesh
129 
130  if (verbose)
131  {
132  std::cout << " -- Building the mesh ... " << std::flush;
133  }
134  std::shared_ptr< mesh_Type > fullMeshPtr ( new RegionMesh<LinearTetra> ( Comm ) );
135  regularMesh3D ( *fullMeshPtr, 1, Nelements, Nelements, Nelements, false,
136  2.0, 2.0, 2.0,
137  -1.0, -1.0, -1.0);
138  if (verbose)
139  {
140  std::cout << " done ! " << std::endl;
141  }
142 
143  if (verbose)
144  {
145  std::cout << " -- Partitioning the mesh ... " << std::flush;
146  }
147  std::shared_ptr< mesh_Type > localMeshPtr;
148  {
149  MeshPartitioner< mesh_Type > meshPart (fullMeshPtr, Comm);
150  localMeshPtr = meshPart.meshPartition();
151  }
152  if (verbose)
153  {
154  std::cout << " done ! " << std::endl;
155  }
156 
157  if (verbose)
158  {
159  std::cout << " -- Freeing the global mesh ... " << std::flush;
160  }
161  fullMeshPtr.reset();
162  if (verbose)
163  {
164  std::cout << " done ! " << std::endl;
165  }
166 
167  // Build the FESpaces
168 
169  if (verbose)
170  {
171  std::cout << " -- Building FESpaces ... " << std::flush;
172  }
173  std::string uOrder ("P1");
174  std::string bOrder ("P1");
175  std::shared_ptr<FESpace< mesh_Type, MapEpetra > > uFESpace ( new FESpace< mesh_Type, MapEpetra > (localMeshPtr, uOrder, 1, Comm) );
176  std::shared_ptr<FESpace< mesh_Type, MapEpetra > > betaFESpace ( new FESpace< mesh_Type, MapEpetra > (localMeshPtr, bOrder, 3, Comm) );
177  if (verbose)
178  {
179  std::cout << " done ! " << std::endl;
180  }
181  if (verbose)
182  {
183  std::cout << " ---> Dofs: " << uFESpace->dof().numTotalDof() << std::endl;
184  }
185 
186  // Read the data
187 
188  std::shared_ptr<DataLevelSet> data_level_set (new DataLevelSet);
189  data_level_set->setup (dataFile, "level-set");
190 
191  // Build the solver
192 
193  LevelSetSolver<mesh_Type> level_set (uFESpace, betaFESpace);
194  level_set.setup (data_level_set);
195 
196  vector_Type initLS (level_set.map() );
197  uFESpace->interpolate ( static_cast<FESpace< mesh_Type, MapEpetra >::function_Type> ( initLSFct ), initLS, 0.0 );
198  level_set.initialize (initLS);
199 
200  level_set.setupLinearSolver (dataFile, "");
201 
202  vector_Type beta (betaFESpace->map() );
203  betaFESpace->interpolate ( static_cast<FESpace< mesh_Type, MapEpetra >::function_Type> ( betaFct ), beta, 0.0);
204 
205  BCHandler bchandler;
206  BCFunctionBase BCu ( exactSolution );
207  bchandler.addBC ("Dirichlet", 1, Essential, Full, BCu, 1);
208  for (UInt i (2); i <= 6; ++i)
209  {
210  bchandler.addBC ("Dirichlet", i, Essential, Full, BCu, 1);
211  }
212 
213 #ifdef HAVE_HDF5
214  ExporterHDF5<mesh_Type> exporter ( dataFile, localMeshPtr, "solution", Comm->MyPID() );
215  exporter.setMultimesh (false);
216  std::shared_ptr<vector_Type> solutionPtr (new vector_Type (level_set.solution(), Repeated) );
217  exporter.addVariable ( ExporterData<mesh_Type>::ScalarField, "level-set", uFESpace, solutionPtr, UInt (0) );
218  exporter.postProcess (0);
219 #endif // HAVE_HDF5
220 
221  Real current_time ( data_level_set->dataTime()->initialTime() );
222  Real dt ( data_level_set->dataTime()->timeStep() );
223  Real final_time ( data_level_set->dataTime()->endTime() );
224 
225  while ( current_time < final_time)
226  {
227  current_time += dt;
228  data_level_set->dataTime()->updateTime();
229  if (verbose)
230  {
231  std::cout << " We are now at time " << current_time << std::endl;
232  }
233 
234  if (verbose)
235  {
236  std::cout << "[LS] Building system ... " << std::flush;
237  }
238  level_set.updateSystem (beta, bchandler, current_time);
239  if (verbose)
240  {
241  std::cout << " done " << std::endl;
242  }
243  if (verbose)
244  {
245  std::cout << "[LS] Solving system ... " << std::flush;
246  }
247  LifeChrono c;
248  c.start();
249  level_set.iterate();
250  c.stop();
251  if (verbose)
252  {
253  std::cout << " Iterate done in " << c.diff() << std::endl;
254  }
255  if (verbose)
256  {
257  std::cout << "[LS] Reinitizialization ... " << std::flush;
258  }
259  level_set.reinitializationDirect();
260  if (verbose)
261  {
262  std::cout << " done " << std::endl;
263  }
264  if (verbose)
265  {
266  std::cout << "[LS] Exporting ... " << std::flush;
267  }
268 #ifdef HAVE_HDF5
269  *solutionPtr = level_set.solution();
270  exporter.postProcess (current_time);
271 #endif // HAVE_HDF5
272  if (verbose)
273  {
274  std::cout << " done " << std::endl;
275  }
276  }
277 
278  Real N (level_set.solution().norm1() );
279  if (verbose)
280  {
281  std::cout << "Final norm of the solution : " << N << std::endl;
282  }
283 
284  if ( (N < 6900) || (N > 7100) )
285  {
286  return (EXIT_FAILURE);
287  }
288 
289 #ifdef HAVE_HDF5
290  exporter.closeFile();
291 #endif // HAVE_HDF5
292 
293  if (verbose)
294  {
295  std::cout << "End Result: TEST PASSED" << std::endl;
296  }
297 
298 #ifdef HAVE_MPI
299  MPI_Finalize();
300 #endif
301 
302  return ( EXIT_SUCCESS );
303 }
VectorEpetra - The Epetra Vector format Wrapper.
Real exactSolution(const Real &t, const Real &x, const Real &y, const Real &z, const ID &)
void importFromHDF5(std::string const &fileName, std::string const &matrixName="matrix")
Read a matrix from a HDF5 (.h5) file.
MatrixEpetra< Real > matrix_Type
Real betaFct(const Real &, const Real &, const Real &, const Real &, const ID &i)
uint32_type ID
IDs.
Definition: LifeV.hpp:194
Real initLSFct(const Real &, const Real &x, const Real &y, const Real &z, const ID &)
double Real
Generic real data.
Definition: LifeV.hpp:175
int main(int argc, char **argv)