36 #include <Epetra_ConfigDefs.h> 39 #include <Epetra_MpiComm.h> 41 #include <Epetra_SerialComm.h> 44 #include <Teuchos_ParameterList.hpp> 45 #include <Teuchos_XMLParameterListHelpers.hpp> 46 #include <Teuchos_RCP.hpp> 49 #include <lifev/core/LifeV.hpp> 50 #include <lifev/core/mesh/RegionMesh3DStructured.hpp> 51 #include <lifev/core/mesh/MeshData.hpp> 52 #include <lifev/core/mesh/RegionMesh.hpp> 53 #include <lifev/core/mesh/MeshUtility.hpp> 54 #include <lifev/core/mesh/MeshPartitioner.hpp> 55 #include <lifev/core/fem/FESpace.hpp> 56 #include <lifev/core/fem/BCManage.hpp> 57 #include <lifev/core/array/MatrixEpetra.hpp> 58 #include <lifev/core/solver/ADRAssembler.hpp> 59 #include <lifev/core/algorithm/PreconditionerIfpack.hpp> 60 #include <lifev/core/algorithm/SolverAztecOO.hpp> 61 #include <lifev/core/algorithm/LinearSolver.hpp> 62 #include <lifev/core/function/Laplacian.hpp> 65 #define TEST_TOLERANCE 1e-13
67 using namespace LifeV;
95 return Laplacian::uexact (t, x, y, z, i);
99 const Real& z,
const ID& i )
const 104 return Laplacian::duexactdx ( t, x, y, z, i );
106 return Laplacian::duexactdy ( t, x, y, z, i );
108 return Laplacian::duexactdz ( t, x, y, z, i );
119 Real& uL2Error,
Real& uH1Error,
bool verbose )
126 uL2Error = uFESpace->l2Error (exactU, velocity, 0, &uRelativeError );
127 Real uH1RelativeError;
128 uH1Error = uFESpace->h1Error (exactU, velocity, 0, &uH1RelativeError );
132 std::cout <<
"Velocity" << std::endl;
136 std::cout <<
" L2 error : " << uL2Error << std::endl;
140 std::cout <<
" Relative error: " << uRelativeError << std::endl;
144 std::cout <<
" H1 error : " << uH1Error << std::endl;
148 std::cout <<
" Relative error: " << uH1RelativeError << std::endl;
160 MPI_Init (&argc, &argv);
166 std::shared_ptr<Epetra_Comm> Comm (
new Epetra_MpiComm ( MPI_COMM_WORLD ) );
168 std::shared_ptr<Epetra_Comm> Comm (
new Epetra_SerialComm );
171 const bool verbose ( Comm->MyPID() == 0 );
175 <<
" +-----------------------------------------------+" << std::endl
176 <<
" | LinearSolver Test |" << std::endl
177 <<
" +-----------------------------------------------+" << std::endl
179 <<
" +-----------------------------------------------+" << std::endl
180 <<
" | Author: Gwenol Grandperrin |" << std::endl
181 <<
" | Date: 2010-08-03 |" << std::endl
182 <<
" +-----------------------------------------------+" << std::endl
185 std::cout <<
"[Initilization of MPI]" << std::endl;
187 std::cout <<
"Using MPI (" << Comm->NumProc() <<
" proc.)" << std::endl;
189 std::cout <<
"Using serial version" << std::endl;
198 std::cout << std::endl <<
"[Loading the data]" << std::endl;
200 LifeChrono globalChrono;
202 globalChrono.start();
205 GetPot command_line ( argc, argv );
206 const std::string dataFileName = command_line.follow (
"data", 2,
"-f",
"--file" );
207 GetPot dataFile ( dataFileName );
211 const UInt numMeshElem = dataFile (
"mesh/num_elements", 10);
212 const std::string uOrder = dataFile (
"finite_element/velocity",
"P1" );
215 Laplacian::setModes ( 1, 1, 1 );
222 std::cout << std::endl <<
"[Loading the mesh]" << std::endl;
225 meshPtr_Type fullMeshPtr (
new mesh_Type ( Comm ) );
226 const UInt& geoDim =
static_cast<UInt> ( mesh_Type::S_geoDimensions );
231 regularMesh3D ( *fullMeshPtr,
233 numMeshElem, numMeshElem, numMeshElem,
238 if ( verbose ) std::cout <<
"Mesh source: regular mesh(" 239 << numMeshElem <<
"x" << numMeshElem <<
"x" << numMeshElem <<
")" << std::endl;
243 std::cout <<
"Mesh size : " << MeshUtility::MeshStatistics::computeSize ( *fullMeshPtr ).maxH << std::endl;
247 std::cout <<
"Partitioning the mesh ... " << std::endl;
249 meshPtr_Type meshPtr;
251 MeshPartitioner< mesh_Type > meshPart ( fullMeshPtr, Comm );
252 meshPtr = meshPart.meshPartition();
261 std::cout << std::endl <<
"[Creating the FE spaces]" << std::endl;
265 std::cout <<
"FE for the velocity: " << uOrder << std::endl;
270 std::cout <<
"Building the velocity FE space ... " << std::flush;
272 fespacePtr_Type uFESpace (
new fespace_Type ( meshPtr, uOrder, geoDim, Comm ) );
275 std::cout <<
"ok." << std::endl;
279 UInt numDofs = geoDim * uFESpace->dof().numTotalDof();
283 std::cout <<
"Total Velocity Dof: " << numDofs << std::endl;
291 std::cout << std::endl <<
"[Boundary conditions]" << std::endl;
294 BCFunctionBase uExact ( Laplacian::uexact );
295 BCFunctionBase fRHS ( Laplacian::f );
299 std::cout <<
"Setting Dirichlet BC... " << std::flush;
301 for ( UInt iDirichlet ( 1 ); iDirichlet <= 26; ++iDirichlet )
303 bcHandler.addBC (
"Wall", iDirichlet, Essential, Full, uExact, geoDim );
307 std::cout <<
"ok." << std::endl;
311 bcHandler.bcUpdate ( *uFESpace->mesh(), uFESpace->feBd(), uFESpace->dof() );
318 std::cout << std::endl <<
"[Matrices Assembly]" << std::endl;
323 std::cout <<
"Setting up assembler... " << std::flush;
325 ADRAssembler<mesh_Type, matrix_Type, vector_Type> adrAssembler;
326 adrAssembler.setup ( uFESpace, uFESpace );
329 std::cout <<
"done" << std::endl;
334 std::cout <<
"Defining the matrices... " << std::flush;
336 std::shared_ptr<matrix_Type> systemMatrix (
new matrix_Type ( uFESpace->map() ) );
339 std::cout <<
"done" << std::endl;
344 std::cout <<
"Adding the viscous stress... " << std::flush;
346 adrAssembler.addDiffusion ( systemMatrix, 1.0 );
349 std::cout <<
"done" << std::endl;
357 std::cout << std::endl <<
"[Solvers initialization]" << std::endl;
359 prec_Type* precRawPtr;
360 basePrecPtr_Type precPtr;
361 precRawPtr =
new prec_Type;
362 precRawPtr->setDataFromGetPot ( dataFile,
"prec" );
363 precPtr.reset ( precRawPtr );
367 std::cout <<
"Setting up SolverAztecOO... " << std::flush;
369 SolverAztecOO linearSolver1;
370 linearSolver1.setCommunicator ( Comm );
371 linearSolver1.setDataFromGetPot ( dataFile,
"solver" );
372 linearSolver1.setTolerance ( 1e-10 );
373 linearSolver1.setPreconditioner ( precPtr );
376 std::cout <<
"done" << std::endl;
381 std::cout <<
"Setting up LinearSolver (Belos)... " << std::flush;
383 Teuchos::RCP< Teuchos::ParameterList > belosList2 = Teuchos::rcp (
new Teuchos::ParameterList );
384 belosList2 = Teuchos::getParametersFromXmlFile (
"SolverParamList2.xml" );
386 LinearSolver linearSolver2;
387 linearSolver2.setCommunicator ( Comm );
388 linearSolver2.setParameters ( *belosList2 );
389 linearSolver2.setPreconditioner ( precPtr );
392 std::cout <<
"done" << std::endl;
394 linearSolver2.showMe();
398 std::cout <<
"Setting up LinearSolver (AztecOO)... " << std::flush;
400 Teuchos::RCP< Teuchos::ParameterList > belosList3 = Teuchos::rcp (
new Teuchos::ParameterList );
401 belosList3 = Teuchos::getParametersFromXmlFile (
"SolverParamList3.xml" );
403 LinearSolver linearSolver3;
404 linearSolver3.setCommunicator ( Comm );
405 linearSolver3.setParameters ( *belosList3 );
406 linearSolver3.setPreconditioner ( precPtr );
409 std::cout <<
"done" << std::endl;
411 linearSolver3.showMe();
418 std::cout << std::endl <<
"[Initialization of the simulation]" << std::endl;
422 std::cout <<
"Creation of vectors... " << std::flush;
425 std::shared_ptr<vector_Type> rhs;
426 rhs.reset (
new vector_Type ( uFESpace->map(), Repeated ) );
428 vector_Type fInterpolated ( uFESpace->map(), Repeated );
429 adrAssembler.addMassRhs ( *rhs, fRHS, 0.0 );
430 rhs->globalAssemble();
433 std::cout <<
"done" << std::endl;
437 uFESpace->interpolate ( uExact, fInterpolated, 0.0 );
442 std::cout <<
"Applying BC... " << std::flush;
444 systemMatrix->globalAssemble();
445 std::shared_ptr<vector_Type> rhsBC;
446 rhsBC.reset (
new vector_Type ( *rhs, Unique ) );
447 bcManage ( *systemMatrix, *rhsBC, *uFESpace->mesh(), uFESpace->dof(), bcHandler, uFESpace->feBd(), 1.0, 0.0 );
450 std::cout <<
"done" << std::endl;
455 std::cout << std::endl <<
"Solving the system with SolverAztec00... " << std::endl;
457 std::shared_ptr<vector_Type> solution;
458 solution.reset (
new vector_Type ( uFESpace->map(), Unique ) );
459 linearSolver1.setMatrix ( *systemMatrix );
460 linearSolver1.solveSystem ( *rhsBC, *solution, systemMatrix );
464 std::cout << std::endl <<
"Solving the system with LinearSolver (Belos)... " << std::endl;
466 std::shared_ptr<vector_Type> solution2;
467 solution2.reset (
new vector_Type ( uFESpace->map(), Unique ) );
468 linearSolver2.setOperator ( systemMatrix );
469 linearSolver2.setRightHandSide ( rhsBC );
470 linearSolver2.solve ( solution2 );
474 std::cout << std::endl <<
"Solving the system with LinearSolver (AztecOO)... " << std::endl;
476 std::shared_ptr<vector_Type> solution3;
477 solution3.reset (
new vector_Type ( uFESpace->map(), Unique ) );
478 linearSolver3.setOperator ( systemMatrix );
479 linearSolver3.setRightHandSide ( rhsBC );
480 linearSolver3.solve ( solution3 );
487 std::cout << std::endl <<
"[Errors computation]" << std::endl;
489 vector_Type solutionErr ( *solution );
491 uFESpace->interpolate (
static_cast<function_Type> ( Laplacian::uexact ), solutionErr, 0.0 );
492 solutionErr -= *solution;
495 vector_Type solution2Err ( *solution2 );
497 uFESpace->interpolate (
static_cast<function_Type> ( Laplacian::uexact ), solution2Err, 0.0 );
498 solution2Err -= *solution2;
501 vector_Type solution3Err ( *solution3 );
503 uFESpace->interpolate (
static_cast<function_Type> ( Laplacian::uexact ), solution3Err, 0.0 );
504 solution3Err -= *solution3;
507 vector_Type solutionsDiff ( *solution2 );
508 solutionsDiff -= *solution;
509 Real solutionsDiffNorm = solutionsDiff.norm2();
511 vector_Type solutionsDiff2 ( *solution2 );
512 solutionsDiff2 -= *solution3;
513 Real solutionsDiffNorm2 = solutionsDiff2.norm2();
523 std::cout <<
"AztecOO solver" << std::endl;
525 Real uL2AztecOO, uH1AztecOO;
526 printErrors ( *solution, uFESpace, uL2AztecOO, uH1AztecOO, verbose );
530 std::cout <<
"Linear solver Belos" << std::endl;
532 Real uL2Belos, uH1Belos;
533 printErrors ( *solution2, uFESpace, uL2Belos, uH1Belos, verbose );
537 std::cout <<
"Linear solver AztecOO" << std::endl;
539 Real uL2AztecOO3, uH1AztecOO3;
540 printErrors ( *solution3, uFESpace, uL2AztecOO3, uH1AztecOO3, verbose );
544 std::cout <<
"Difference between the Azteco and the Belos solutions: " << solutionsDiffNorm << std::endl;
550 std::cout <<
"The difference between the two solutions is too large." << std::endl;
554 std::cout <<
"Test status: FAILED" << std::endl;
556 return ( EXIT_FAILURE );
560 std::cout <<
"Difference between the two AztecOO solvers solutions: " << solutionsDiffNorm2 << std::endl;
566 std::cout <<
"The difference between the two solutions is too large." << std::endl;
570 std::cout <<
"Test status: FAILED" << std::endl;
572 return ( EXIT_FAILURE );
575 if ( uL2AztecOO > 4.602e-03 || uH1AztecOO > 3.855e-01
576 || uL2Belos > 4.602e-03 || uH1Belos > 3.855e-01
577 || uL2AztecOO3 > 4.602e-03 || uH1AztecOO3 > 3.855e-01)
581 std::cout <<
"The error in L2 norm or H1 norm is too large." << std::endl;
585 std::cout <<
"Test status: FAILED" << std::endl;
587 return ( EXIT_FAILURE );
596 std::cout << std::endl <<
"Total simulation time: " << globalChrono.diff() <<
" s." << std::endl;
600 std::cout << std::endl <<
"[[END_SIMULATION]]" << std::endl;
609 std::cout <<
"Test status: SUCCESS" << std::endl;
611 return ( EXIT_SUCCESS );
LifeV::Preconditioner basePrec_Type
LifeV::PreconditionerIfpack prec_Type
std::shared_ptr< mesh_Type > meshPtr_Type
FESpace - Short description here please!
FESpace< mesh_Type, MapEpetra > fespace_Type
RegionMesh< LinearTetra > mesh_Type
Real operator()(const Real &t, const Real &x, const Real &y, const Real &z, const ID &i) const
std::shared_ptr< basePrec_Type > basePrecPtr_Type
void printErrors(const vector_Type &solution, fespacePtr_Type uFESpace, Real &uL2Error, Real &uH1Error, bool verbose)
double Real
Generic real data.
std::shared_ptr< fespace_Type > fespacePtr_Type
MatrixEpetra< Real > matrix_Type
std::shared_ptr< VectorEpetra > vectorPtr_Type
Real grad(const ID &iCoor, const Real &t, const Real &x, const Real &y, const Real &z, const ID &i) const
int main(int argc, char **argv)
std::function< Real(Real const &, Real const &, Real const &, Real const &, UInt const &) > function_Type
std::shared_ptr< prec_Type > precPtr_Type