LifeV
test_bdf.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 Umberto Villa <uvilla@emory.edu>
32  @date 14-04-2010
33  */
34 
35 
36 // ===================================================
37 //! Includes
38 // ===================================================
39 
40 #include <Epetra_ConfigDefs.h>
41 #ifdef EPETRA_MPI
42 #include <mpi.h>
43 #include <Epetra_MpiComm.h>
44 #else
45 #include <Epetra_SerialComm.h>
46 #endif
47 
48 
49 #include <lifev/core/array/MapEpetra.hpp>
50 #include <lifev/core/mesh/MeshData.hpp>
51 #include <lifev/core/mesh/MeshPartitioner.hpp>
52 #include <lifev/core/algorithm/SolverAztecOO.hpp>
53 #include <lifev/core/algorithm/LinearSolver.hpp>
54 #include <lifev/core/fem/Assembly.hpp>
55 #include <lifev/core/fem/AssemblyElemental.hpp>
56 #include <lifev/core/fem/BCHandler.hpp>
57 #include <lifev/core/fem/BCManage.hpp>
58 #include <lifev/core/fem/FESpace.hpp>
59 #include <lifev/core/fem/TimeAdvanceBDFVariableStep.hpp>
60 
61 #include <lifev/core/algorithm/PreconditionerIfpack.hpp>
62 #include <lifev/core/algorithm/PreconditionerML.hpp>
63 
64 #include <lifev/core/filter/Exporter.hpp>
65 #include <lifev/core/filter/ExporterEmpty.hpp>
66 #include <lifev/core/filter/ExporterEnsight.hpp>
67 #include <lifev/core/filter/ExporterHDF5.hpp>
68 
69 #include <Teuchos_ParameterList.hpp>
70 #include <Teuchos_XMLParameterListHelpers.hpp>
71 #include <Teuchos_RCP.hpp>
72 
73 #include "ud_functions.hpp"
74 #include "test_bdf.hpp"
75 
76 // ===================================================
77 //! Namespaces & Define
78 // ===================================================
79 using namespace LifeV;
80 
81 const int TOP = 6;
82 const int BOTTOM = 5;
83 const int LEFT = 3;
84 const int RIGHT = 4;
85 const int FRONT = 2;
86 const int BACK = 1;
87 
89 
90 const std::string discretization_section = "space_discretization";
91 
92 // ===================================================
93 //! Private Members
94 // ===================================================
95 struct test_bdf::Private
96 {
98  {
99  }
100 
101  typedef std::function < Real (Real const&, Real const&, Real const&,
102  Real const&, ID const&) > fct_type;
103 
107 
108 };
109 
110 // ===================================================
111 //! Constructors
112 // ===================================================
113 test_bdf::test_bdf (int argc, char** argv) :
114  Members (new Private)
115 {
116  GetPot command_line (argc, argv);
117  const std::string data_file_name =
118  command_line.follow ("data", 2, "-f", "--file");
119  GetPot dataFile (data_file_name);
120 
121  Members->data_file_name = data_file_name;
122 
123 #ifdef EPETRA_MPI
124  std::cout << "Epetra Initialization" << std::endl;
125  Members->comm.reset (new Epetra_MpiComm (MPI_COMM_WORLD) );
126 #else
127  Members->comm.reset (new Epetra_SerialComm() );
128 #endif
129 }
130 
131 // ===================================================
132 //! Methods
133 // ===================================================
134 void test_bdf::run()
135 {
136  //Useful typedef
137  typedef VectorEpetra vector_Type;
138  typedef std::shared_ptr<vector_Type> vectorPtr_Type;
139 
140  typedef LifeV::Preconditioner basePrec_Type;
141  typedef std::shared_ptr<basePrec_Type> basePrecPtr_Type;
142  typedef LifeV::PreconditionerIfpack prec_Type;
143  typedef std::shared_ptr<prec_Type> precPtr_Type;
144 
145  // Reading from data file
146  GetPot dataFile (Members->data_file_name.c_str() );
147  // Select Pid(0) to write on std output
148  bool verbose = (Members->comm->MyPID() == 0);
149 
150  if (verbose)
151  {
152  std::cout << "The BDF Solver" << std::flush;
153  }
154 
155  //the forcing term
156  SourceFct sf;
157  // the boundary conditions
158  BCFunctionBase g_Ess (AnalyticalSol::u);
159 
160  BCHandler bc;
161 
162  bc.addBC ("Top", TOP, Essential, Full, g_Ess, 1);
163  bc.addBC ("Bottom", BOTTOM, Essential, Full, g_Ess, 1);
164  bc.addBC ("Left", LEFT, Essential, Full, g_Ess, 1);
165  bc.addBC ("Right", RIGHT, Essential, Full, g_Ess, 1);
166  bc.addBC ("Front", FRONT, Essential, Full, g_Ess, 1);
167  bc.addBC ("Back", BACK, Essential, Full, g_Ess, 1);
168 
169  //=============================================================================
170  //Mesh stuff
171  Members->comm->Barrier();
172  MeshData meshData (dataFile, ("bdf/" + discretization_section).c_str() );
173  std::shared_ptr<regionMesh> fullMeshPtr ( new regionMesh ( Members->comm ) );
174  readMesh (*fullMeshPtr, meshData);
175  std::shared_ptr<regionMesh> meshPtr;
176  {
177  MeshPartitioner<regionMesh> meshPart ( fullMeshPtr, Members->comm );
178  meshPtr = meshPart.meshPartition();
179  }
180 
181  //=============================================================================
182  //finite element space of the solution
183  std::shared_ptr<FESpace<regionMesh, MapEpetra> > feSpacePtr (
184  new FESpace<regionMesh, MapEpetra> (
185  meshPtr, dataFile ( ("bdf/" + discretization_section + "/order").c_str(), "P2"), 1, Members->comm) );
186 
187  if (verbose)
188  std::cout << " Number of unknowns : "
189  << feSpacePtr->map().map (Unique)->NumGlobalElements()
190  << std::endl;
191 
192  bc.bcUpdate (* (feSpacePtr->mesh() ), feSpacePtr->feBd(), feSpacePtr->dof() );
193 
194  //=============================================================================
195  //Fe Matrices and vectors
196  MatrixElemental elmat (feSpacePtr->fe().nbFEDof(), 1, 1); //local matrix
197  MatrixEpetra<double> matM (feSpacePtr->map() ); //mass matrix
198  std::shared_ptr<MatrixEpetra<double> > matA_ptr (
199  new MatrixEpetra<double> (feSpacePtr->map() ) ); //stiff matrix
200  vectorPtr_Type u ( new vector_Type (feSpacePtr->map(), Unique) ); // solution vector
201  vectorPtr_Type f ( new vector_Type (feSpacePtr->map(), Unique) ); // forcing term vector
202 
203  LifeChrono chrono;
204  //Assembling Matrix M
205  Members->comm->Barrier();
206  chrono.start();
207  for (UInt iVol = 0; iVol < feSpacePtr->mesh()->numElements(); iVol++)
208  {
209  feSpacePtr->fe().updateJac (feSpacePtr->mesh()->element (iVol) );
210  elmat.zero();
211  AssemblyElemental::mass (1., elmat, feSpacePtr->fe(), 0, 0);
212  assembleMatrix (matM, elmat, feSpacePtr->fe(), feSpacePtr->fe(), feSpacePtr->dof(),
213  feSpacePtr->dof(), 0, 0, 0, 0);
214  }
215  matM.globalAssemble();
216  Members->comm->Barrier();
217  chrono.stop();
218  if (verbose)
219  std::cout << "\n \n -- Mass matrix assembling time = " << chrono.diff() << std::endl
220  << std::endl;
221 
222  // ==========================================
223  // Definition of the time integration stuff
224  // ==========================================
225  Real Tfin = dataFile ("bdf/endtime", 10.0);
226  Real delta_t = dataFile ("bdf/timestep", 0.5);
227  Real t0 = 1.;
228  UInt ord_bdf = dataFile ("bdf/order", 3);
229  TimeAdvanceBDFVariableStep<VectorEpetra> bdf;
230  bdf.setup (ord_bdf);
231 
232  //Initialization
233  bdf.setInitialCondition<Real (*) (Real, Real, Real, Real, UInt), FESpace<regionMesh, MapEpetra> > (AnalyticalSol::u, *u, *feSpacePtr, t0, delta_t);
234 
235  if (verbose)
236  {
237  bdf.showMe();
238  }
239  Members->comm->Barrier();
240 
241  //===================================================
242  // post processing setup
243  std::shared_ptr<Exporter<regionMesh> > exporter;
244  std::string const exporterType = dataFile ( "exporter/type", "hdf5");
245 
246 #ifdef HAVE_HDF5
247  if (exporterType.compare ("hdf5") == 0)
248  {
249  exporter.reset ( new ExporterHDF5<regionMesh > ( dataFile, "bdf_test" ) );
250  }
251  else
252 #endif
253  {
254  if (exporterType.compare ("none") == 0)
255  {
256  exporter.reset ( new ExporterEmpty<regionMesh > ( dataFile, meshPtr, "bdf_test", Members->comm->MyPID() ) );
257  }
258  else
259  {
260  exporter.reset ( new ExporterEnsight<regionMesh > ( dataFile, meshPtr, "bdf_test", Members->comm->MyPID() ) );
261  }
262  }
263 
264  exporter->setPostDir ( "./" );
265  exporter->setMeshProcId ( meshPtr, Members->comm->MyPID() );
266 
267  std::shared_ptr<VectorEpetra> u_display_ptr (new VectorEpetra (
268  feSpacePtr->map(), exporter->mapType() ) );
269  exporter->addVariable (ExporterData<regionMesh >::ScalarField, "u", feSpacePtr,
270  u_display_ptr, UInt (0) );
271  *u_display_ptr = *u;
272  exporter->postProcess (0);
273 
274 
275  //===================================================
276  //Definition of the linear solver
277  LinearSolver linearSolver;
278 
279  Teuchos::RCP< Teuchos::ParameterList > belosList = Teuchos::rcp ( new Teuchos::ParameterList );
280  belosList = Teuchos::getParametersFromXmlFile ( "SolverParamList.xml" );
281 
282  linearSolver.setCommunicator ( Members->comm );
283  linearSolver.setParameters ( *belosList );
284 
285  prec_Type* precRawPtr;
286  basePrecPtr_Type precPtr;
287  precRawPtr = new prec_Type;
288  precRawPtr->setDataFromGetPot ( dataFile, "bdf/prec" );
289  precPtr.reset ( precRawPtr );
290  linearSolver.setPreconditioner ( precPtr );
291 
292  //===================================================
293  // TIME LOOP
294  //===================================================
295 
296  matA_ptr.reset (new MatrixEpetra<double> (feSpacePtr->map() ) );
297 
298  for (Real t = t0 + delta_t; t <= Tfin; t += delta_t)
299  {
300  Members->comm->Barrier();
301  if (verbose)
302  {
303  std::cout << "Now we are at time " << t << std::endl;
304  }
305 
306  chrono.start();
307  //Assemble A
308  Real coeff = bdf.coefficientDerivative (0) / delta_t;
309  Real visc = nu (t);
310  Real s = sigma (t);
311  for (UInt i = 0; i < feSpacePtr->mesh()->numElements(); i++)
312  {
313  feSpacePtr->fe().updateFirstDerivQuadPt (feSpacePtr->mesh()->element (i) );
314  elmat.zero();
315  AssemblyElemental::mass (coeff + s, elmat, feSpacePtr->fe() );
316  AssemblyElemental::stiff (visc, elmat, feSpacePtr->fe() );
317  assembleMatrix (*matA_ptr, elmat, feSpacePtr->fe(), feSpacePtr->fe(),
318  feSpacePtr->dof(), feSpacePtr->dof(), 0, 0, 0, 0);
319  }
320 
321  chrono.stop();
322  if (verbose)
323  std::cout << "A has been constructed in " << chrono.diff() << "s." << std::endl;
324  {
325  }
326 
327  // Handling of the right hand side
328  *f = (matM * bdf.rhsContribution() ); //f = M*\sum_{i=1}^{orderBdf} \alpha_i u_{n-i}
329  feSpacePtr->l2ScalarProduct (sf, *f, t); //f +=\int_\Omega{ volumeForces *v dV}
330  Members->comm->Barrier();
331 
332  // Treatment of the Boundary conditions
333  if (verbose)
334  {
335  std::cout << "*** BC Management: " << std::endl;
336  }
337  Real tgv = 1.;
338  chrono.start();
339  bcManage (*matA_ptr, *f, *feSpacePtr->mesh(), feSpacePtr->dof(), bc, feSpacePtr->feBd(), tgv, t);
340  matA_ptr->globalAssemble();
341  chrono.stop();
342  if (verbose)
343  {
344  std::cout << chrono.diff() << "s." << std::endl;
345  }
346 
347  //Set Up the linear system
348  Members->comm->Barrier();
349  chrono.start();
350  linearSolver.setOperator ( matA_ptr );
351  linearSolver.setReusePreconditioner (false);
352  linearSolver.setRightHandSide ( f );
353 
354  Members->comm->Barrier();
355 
356  linearSolver.solve ( u );
357 
358  chrono.stop();
359 
360  bdf.shiftRight (*u);
361 
362  if (verbose)
363  std::cout << "*** Solution computed in " << chrono.diff() << "s."
364  << std::endl;
365 
366  Members->comm->Barrier();
367 
368  // Error in the L2
369  vector_Type uComputed (*u, Repeated);
370 
371  Real L2_Error, L2_RelError;
372 
373  L2_Error = feSpacePtr->l2Error (AnalyticalSol::u, uComputed, t, &L2_RelError);
374 
375  if (verbose)
376  std::cout << "Error Norm L2: " << L2_Error
377  << "\nRelative Error Norm L2: " << L2_RelError << std::endl;
378 
379  Members->errorNorm = L2_Error;
380 
381  //transfer the solution at time t.
382  *u_display_ptr = *u;
383 
384  matA_ptr->zero();
385 
386  exporter->postProcess (t);
387  }
388 
389 
390 
391 }
392 
393 bool test_bdf::check()
394 {
395  // Reading from data file
396  GetPot dataFile (Members->data_file_name.c_str() );
397  return Members->errorNorm < dataFile ("errorNorms/l2Error", -10e10);
398 }
double operator()(const char *VarName, const double &Default) const
Definition: GetPot.hpp:2033
const int TOP
Definition: test_bdf.cpp:81
const int RIGHT
Definition: test_bdf.cpp:84
GetPot(const int argc_, char **argv_, const char *FieldSeparator=0x0)
Definition: GetPot.hpp:507
std::shared_ptr< Epetra_Comm > comm
Definition: test_bdf.cpp:105
const std::string discretization_section
Definition: test_bdf.cpp:90
TimeAdvanceBDFVariableStep - Backward differencing formula time discretization with non-uniform time ...
const int BOTTOM
Definition: test_bdf.cpp:82
std::shared_ptr< std::vector< Int > > M_isOnProc
const int LEFT
Definition: test_bdf.cpp:83
MeshData - class for handling spatial discretization.
Definition: MeshData.hpp:72
const int FRONT
Definition: test_bdf.cpp:85
RegionMesh< LinearTetra > regionMesh
Definition: test_bdf.cpp:88
double Real
Generic real data.
Definition: LifeV.hpp:175
std::string data_file_name
Definition: test_bdf.cpp:104
int operator()(const char *VarName, int Default) const
Definition: GetPot.hpp:2021
std::function< Real(Real const &, Real const &, Real const &, Real const &, ID const &) > fct_type
Definition: test_bdf.cpp:102
Private Members.
Definition: test_bdf.cpp:95
const int BACK
Definition: test_bdf.cpp:86
uint32_type UInt
generic unsigned integer (used mainly for addressing)
Definition: LifeV.hpp:191