LifeV
timeAdvance.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  \file timeAdvance.cpp
28 
29  Author(s): F. Nobile <fabio.nobile@polimi.it>
30  M. Pozzoli <matteo1.pozzoli@mail.polimi.it>
31  C. Vergara <christian.vergara@polimi.it >
32 
33  \date 2010-02-10
34  */
35 
36 /* ========================================================
37 
38 Simple Problem test with Dirichlet Boundary condition
39 
40 Solve the problem
41 
42  \frac{\partial u}{\partial t} - \Delta u = f
43 
44  u = u0 on the boundary
45 
46 linear_function.hpp:
47 
48  uexact = exp(-sin(Pi/2*t))*(x+y+z);
49 
50  f = (3 \pi^2 + 1 ) exp(-t) sin( \pi x) sin(\pi y) sin ( \pi z) on a cube
51 
52 nonlinear_function.hpp:
53 
54  uexact = exp(-sin(Pi/2*t))*cos(x *Pi)*cos(y*Pi)*cos(z*Pi);
55 
56  f = Pi2/4*( sin(Pi/2*t)+cos(Pi/2*t)*cos(Pi/2*t) )*exp(-sin(Pi/2*t))*cos(x *Pi)*cos(y*Pi)*cos(z*Pi);
57 */
58 
59 
60 // ===================================================
61 //! Includes
62 // ===================================================
63 
64 #include <Epetra_ConfigDefs.h>
65 #ifdef EPETRA_MPI
66 #include <mpi.h>
67 #include <Epetra_MpiComm.h>
68 #else
69 #include <Epetra_SerialComm.h>
70 #endif
71 
72 
73 #include <lifev/core/array/MapEpetra.hpp>
74 #include <lifev/core/mesh/MeshData.hpp>
75 #include <lifev/core/mesh/MeshPartitioner.hpp>
76 
77 #include <lifev/structure/solver/VenantKirchhoffViscoelasticSolver.hpp>
78 #include <lifev/structure/solver/VenantKirchhoffViscoelasticData.hpp>
79 
80 #include <lifev/core/fem/TimeData.hpp>
81 #include <lifev/core/fem/FESpace.hpp>
82 
83 #include <lifev/core/fem/TimeAdvance.hpp>
84 #include <lifev/core/fem/TimeAdvanceNewmark.hpp>
85 #include <lifev/core/fem/TimeAdvanceBDF.hpp>
86 
87 #include <lifev/core/filter/ExporterEnsight.hpp>
88 #include <lifev/core/filter/ExporterHDF5.hpp>
89 #include <lifev/core/filter/ExporterEmpty.hpp>
90 
91 #include <iostream>
92 
93 #include "timeAdvance.hpp"
94 
95 //choose of function.hpp
96 #include "linear_function.hpp"
97 //#include "nonlinear_function.hpp"
98 
99 // ===================================================
100 //! Namespaces & Define
101 // ===================================================
102 using namespace LifeV;
103 
104 const int TOP = 6;
105 const int BOTTOM = 5;
106 const int LEFT = 3;
107 const int RIGHT = 4;
108 const int FRONT = 2;
109 const int BACK = 1;
110 
111 
112 // ===================================================
113 //! Private members
114 // ===================================================
115 struct problem::Private
116 {
118  rho (1)
119  {}
120 
121  typedef std::function<Real ( Real const&, Real const&, Real const&, Real const&, ID const& ) > fct_type;
122  double rho;
124 
125 
127 
129  {
130  fct_type f;
131  f = std::bind (&UZero, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, std::placeholders::_5);
132  return f;
133  }
134 };
135 
136 
137 
138 
139 
140 // ===================================================
141 //! Constructors
142 // ===================================================
143 problem::problem ( int argc,
144  char** argv,
145  std::shared_ptr<Epetra_Comm> structComm) :
146  members ( new Private() )
147 {
148  GetPot command_line (argc, argv);
149  std::string data_file_name = command_line.follow ("data", 2, "-f", "--file");
150  GetPot dataFile ( data_file_name );
151  members->data_file_name = data_file_name;
152 
153  members->rho = dataFile ( "problem/physics/density", 1. );
154 
155  std::cout << "density = " << members->rho << std::endl;
156 
157  members->comm = structComm;
158  int ntasks = members->comm->NumProc();
159 
160  if (!members->comm->MyPID() )
161  {
162  std::cout << "My PID = " << members->comm->MyPID() << " out of " << ntasks << " running." << std::endl;
163  }
164 }
165 
166 // ===================================================
167 //! Methods
168 // ===================================================
169 void
170 problem::run()
171 {
172  typedef RegionMesh<LinearTetra> mesh_Type;
173  typedef VenantKirchhoffViscoelasticSolver< mesh_Type >::vector_type vector_Type;
174  typedef std::shared_ptr<vector_Type> vectorPtr_Type;
175 
176  typedef std::shared_ptr< TimeAdvance< vector_Type > > TimeAdvance_type;
177 
178  typedef FESpace< mesh_Type, MapEpetra > FESpace_type;
179  typedef std::shared_ptr<FESpace_type> FESpace_ptrtype;
180 
181  bool verbose = (members->comm->MyPID() == 0);
182 
183  //
184  // VenantKirchhoffViscoelasticData
185  //
186 
187  GetPot dataFile ( members->data_file_name.c_str() );
188  std::shared_ptr<VenantKirchhoffViscoelasticData> dataProblem (new VenantKirchhoffViscoelasticData( ) );
189  dataProblem->setup (dataFile, "problem");
190 
191 
192  MeshData meshData;
193  meshData.setup (dataFile, "problem/space_discretization");
194 
195  std::shared_ptr<mesh_Type > fullMeshPtr ( new mesh_Type ( members->comm ) );
196  readMesh (*fullMeshPtr, meshData);
197 
198  std::shared_ptr<mesh_Type > localMeshPtr;
199  {
200  MeshPartitioner< mesh_Type > meshPart ( fullMeshPtr, members->comm );
201  localMeshPtr = meshPart.meshPartition();
202  }
203 
204  //
205  // The Problem Solver
206  //
207 
208  if (verbose)
209  {
210  std::cout << "The Problem Solver" << std::flush;
211  }
212 
213  // Scalar Solution Space:
214 
215  std::string Order = dataFile ( "problem/space_discretization/order", "P1");
216 
217  if ( Order.compare ("P1") == 0 )
218  {
219  if (verbose)
220  {
221  std::cout << " Space order : P1" << std::flush;
222  }
223 
224  }
225  else if ( Order.compare ("P2") == 0 )
226  {
227  if (verbose)
228  {
229  std::cout << " Space order : P2";
230  }
231 
232  }
233  if (verbose)
234  {
235  std::cout << std::endl;
236  }
237 
238 
239  // finite element space of the solution
240  // finite element space of the solution
241  std::string dOrder = dataFile ( "problem/space_discretization/order", "P1");
242 
243  FESpace_ptrtype feSpace ( new FESpace_type (localMeshPtr, dOrder, 1, members->comm) );
244 
245  // instantiation of the VenantKirchhoffViscoelasticSolver class
246 
247  VenantKirchhoffViscoelasticSolver< mesh_Type > problem;
248 
249  problem.setup (dataProblem, feSpace,
250  members->comm);
251 
252  problem.setDataFromGetPot (dataFile);
253  // the boundary conditions
254 
255  BCFunctionBase uZero ( members->getUZero() );
256  BCFunctionBase uEx (uexact);
257 
258  BCHandler bcH;
259 
260  bcH.addBC ( "Top", TOP, Essential, Full, uEx, 1 );
261  bcH.addBC ( "Bottom", BOTTOM, Essential, Full, uEx, 1 );
262  bcH.addBC ( "Left", LEFT, Essential, Full, uEx, 1 );
263  bcH.addBC ( "Right", RIGHT, Essential, Full, uEx, 1 );
264  bcH.addBC ( "Front", FRONT, Essential, Full, uEx, 1 );
265  bcH.addBC ( "Back", BACK, Essential, Full, uEx, 1 );
266 
267  std::ofstream out_norm;
268  if (verbose)
269  {
270  out_norm.open ("norm.txt");
271  out_norm << " time "
272  << " L2_Error "
273  << " H1_Error "
274  << " L2_RelError "
275  << " H1_RelError \n";
276  out_norm.close();
277  }
278 
279  LifeChrono chrono;
280 
281  std::string TimeAdvanceMethod = dataFile ( "problem/time_discretization/method", "Newmark");
282 
283  TimeAdvance_type timeAdvance ( TimeAdvanceFactory::instance().createObject ( TimeAdvanceMethod ) );
284 
285  UInt OrderDev = 1;
286 
287  //! initialization of parameters of time Advance method:
288 
289  if (TimeAdvanceMethod == "Newmark")
290  {
291  timeAdvance->setup ( dataProblem->dataTimeAdvance()->coefficientsNewmark() , OrderDev);
292  }
293 
294  if (TimeAdvanceMethod == "BDF")
295  {
296  timeAdvance->setup (dataProblem->dataTimeAdvance()->orderBDF() , OrderDev);
297  }
298 
299  Real dt = dataProblem->dataTime()->timeStep();
300  Real T = dataProblem->dataTime()->endTime();
301 
302  chrono.start();
303 
304  dataProblem->setup (dataFile, "problem");
305 
306  double alpha = timeAdvance->coefficientFirstDerivative ( 0 ) / ( dataProblem->dataTime()->timeStep() );
307 
308  problem.buildSystem (alpha);
309 
310  MPI_Barrier (MPI_COMM_WORLD);
311 
312  if (verbose )
313  {
314  std::cout << "ok." << std::endl;
315  }
316 
317  // building some vectors:
318  MapEpetra uMap = problem.solution()->map();
319 
320  // computing the rhs
321  vector_Type rhs ( uMap, Unique );
322  vector_Type rhsV ( uMap, Unique );
323 
324  // postProcess
325  std::shared_ptr< Exporter<mesh_Type > > exporter;
326 
327  std::string const exporterType = dataFile ( "exporter/type", "ensight");
328 
329 #ifdef HAVE_HDF5
330  if (exporterType.compare ("hdf5") == 0)
331  {
332  exporter.reset ( new ExporterHDF5<mesh_Type > ( dataFile, "problem" ) );
333  }
334  else
335 #endif
336  {
337  if (exporterType.compare ("none") == 0)
338  {
339  exporter.reset ( new ExporterEmpty<mesh_Type > ( dataFile, localMeshPtr, "problem", members ->comm->MyPID() ) );
340  }
341  else
342  {
343  exporter.reset ( new ExporterEnsight<mesh_Type > ( dataFile, localMeshPtr, "problem", members->comm->MyPID() ) );
344  }
345  }
346 
347  exporter->setPostDir ( "./" ); // This is a test to see if M_post_dir is working
348  exporter->setMeshProcId ( localMeshPtr, members->comm->MyPID() );
349 
350  vectorPtr_Type U ( new vector_Type (*problem.solution(), exporter->mapType() ) );
351  vectorPtr_Type V ( new vector_Type (*problem.solution(), exporter->mapType() ) );
352  vectorPtr_Type Exact ( new vector_Type (*problem.solution(), exporter->mapType() ) );
353  vectorPtr_Type vExact ( new vector_Type (*problem.solution(), exporter->mapType() ) );
354  vectorPtr_Type RHS ( new vector_Type (*problem.solution(), exporter->mapType() ) );
355  exporter->addVariable ( ExporterData<mesh_Type>::ScalarField, "displacement",
356  feSpace, U, UInt (0) );
357 
358  exporter->addVariable ( ExporterData<mesh_Type>::ScalarField, "velocity",
359  feSpace, V, UInt (0) );
360 
361  exporter->addVariable ( ExporterData<mesh_Type>::ScalarField, "uexact",
362  feSpace, Exact, UInt (0) );
363 
364  exporter->addVariable ( ExporterData<mesh_Type>::ScalarField, "vexact",
365  feSpace, vExact, UInt (0) );
366 
367  exporter->postProcess ( 0 );
368 
369  //initialization of unk
370 
371  //evaluate disp and vel as interpolate the bcFunction d0 and v0
372  feSpace->interpolate ( static_cast<FESpace_type::function_Type> ( d0 ), *U, 0.0 );
373  feSpace->interpolate ( static_cast<FESpace_type::function_Type> ( v0 ), *V, 0.0 );
374 
375  //evaluate disp and vel as interpolate the bcFunction d0 and v0
376 
377  std::vector<vectorPtr_Type> uv0;
378 
379  if (TimeAdvanceMethod == "Newmark")
380  {
381  uv0.push_back (U);
382  uv0.push_back (V);
383  }
384  if (TimeAdvanceMethod == "BDF")
385  {
386  for ( UInt previousPass = 0; previousPass < dataProblem->dataTimeAdvance()->orderBDF() ; previousPass++)
387  {
388  Real previousTimeStep = -previousPass * dt;
389  feSpace->interpolate (static_cast<FESpace_type::function_Type> (uexact ), *U, previousTimeStep );
390  uv0.push_back (U);
391  }
392  }
393 
394  //the uv0[0] should be the displacement
395  //the uv0[1] should be the velocity
396 
397  //timeAdvance->setInitialCondition(uv0[0],uv0[1]);
398  timeAdvance->setInitialCondition (uv0);
399 
400  timeAdvance-> setTimeStep (dataProblem->dataTime()->timeStep() );
401 
402  timeAdvance->showMe();
403 
404  feSpace->interpolate ( static_cast<FESpace_type::function_Type> ( uexact ), *Exact , 0);
405  feSpace->interpolate ( static_cast<FESpace_type::function_Type> ( v0 ), *vExact , 0);
406 
407  *U = timeAdvance->solution();
408  *V = timeAdvance->firstDerivative();
409 
410  for (Real time = dt; time <= T; time += dt)
411  {
412  dataProblem->dataTime()->setTime (time);
413 
414  if (verbose)
415  {
416  std::cout << std::endl;
417  std::cout << " P - Now we are at time " << dataProblem->dataTime()->time() << " s." << std::endl;
418  }
419 
420  //evaluate rhs
421  rhs *= 0;
422  timeAdvance->updateRHSContribution (dt);
423  rhsV = timeAdvance->rhsContributionFirstDerivative();
424  feSpace->l2ScalarProduct (source_in, rhs, time);
425  rhs += problem.matrMass() * rhsV;
426 
427  //updateRHS
428  problem.updateRHS (rhs);
429 
430  //solver system
431  problem.iterate ( bcH ); // Computes the matrices and solves the system
432 
433  //update unknowns of timeAdvance
434  timeAdvance->shiftRight (*problem.solution() );
435 
436  //evaluate uexact solution
437  feSpace->interpolate ( static_cast<FESpace_type::function_Type> ( uexact ), *Exact , time);
438  feSpace->interpolate ( static_cast<FESpace_type::function_Type> ( v0 ), *vExact , time);
439  *U = timeAdvance->solution();
440  *V = timeAdvance->firstDerivative();
441 
442  //postProcess
443  exporter->postProcess (time);
444 
445  // Error L2 and H1 Norms
446  AnalyticalSol uExact;
447  vector_Type u (*problem.solution(), Repeated);
448 
449  Real H1_Error, H1_RelError, L2_Error, L2_RelError;
450 
451  L2_Error = feSpace->l2Error (uexact, u, time , &L2_RelError);
452  H1_Error = feSpace->h1Error (uExact, u, time , &H1_RelError);
453 
454  //save the norm
455  if (verbose)
456  {
457  out_norm.open ("norm.txt", std::ios::app);
458  out_norm << time << " "
459  << L2_Error << " "
460  << H1_Error << " "
461  << L2_RelError << " "
462  << H1_RelError << "\n";
463  out_norm.close();
464  }
465 
466  MPI_Barrier (MPI_COMM_WORLD);
467  }
468  chrono.stop();
469  if (verbose)
470  {
471  std::cout << "Total iteration time " << chrono.diff() << " s." << std::endl;
472  }
473 }
FESpace - Short description here please!
Definition: FESpace.hpp:78
GetPot(const int argc_, char **argv_, const char *FieldSeparator=0x0)
Definition: GetPot.hpp:507
Epetra_Import const & importer()
Getter for the Epetra_Import.
Definition: MapEpetra.cpp:394
std::shared_ptr< std::vector< Int > > M_isOnProc
const int BOTTOM
MeshData - class for handling spatial discretization.
Definition: MeshData.hpp:72
std::function< Real(Real const &, Real const &, Real const &, Real const &, ID const &) > fct_type
double Real
Generic real data.
Definition: LifeV.hpp:175
std::shared_ptr< Epetra_Comm > comm
const int FRONT
fct_type getUZero()
Private members.
const int LEFT
const int BACK
const int RIGHT
std::string data_file_name
const int TOP
uint32_type UInt
generic unsigned integer (used mainly for addressing)
Definition: LifeV.hpp:191