LifeV
lifev/structure/examples/example_computationNorm/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 \file main.cpp
28 \author Paolo Tricerri <paolo.tricerri@epfl.ch>
29 
30 
31 Attention: At the moment the restart works only if the solution is saved at
32 each time step and with the BDF method!!
33 
34 \date 2005-04-16
35 */
36 #ifdef TWODIM
37 #error test_structure cannot be compiled in 2D
38 #endif
39 
40 // Tell the compiler to ignore specific kind of warnings:
41 #pragma GCC diagnostic ignored "-Wunused-variable"
42 #pragma GCC diagnostic ignored "-Wunused-parameter"
43 
44 #include <Epetra_ConfigDefs.h>
45 #ifdef EPETRA_MPI
46 #include <mpi.h>
47 #include <Epetra_MpiComm.h>
48 #else
49 #include <Epetra_SerialComm.h>
50 #endif
51 
52 //Tell the compiler to restore the warning previously silented
53 #pragma GCC diagnostic warning "-Wunused-variable"
54 #pragma GCC diagnostic warning "-Wunused-parameter"
55 
56 #include <lifev/core/LifeV.hpp>
57 //Include fils which were in the structure.cpp file
58 #include <lifev/core/array/MapEpetra.hpp>
59 
60 #include <lifev/core/mesh/MeshData.hpp>
61 #include <lifev/core/mesh/MeshPartitioner.hpp>
62 #include <lifev/core/filter/PartitionIO.hpp>
63 #include <lifev/core/mesh/RegionMesh.hpp>
64 
65 #include <lifev/core/fem/FESpace.hpp>
66 
67 #include <lifev/core/filter/Exporter.hpp>
68 #include <lifev/core/filter/ExporterEnsight.hpp>
69 #ifdef HAVE_HDF5
70 #include <lifev/core/filter/ExporterHDF5.hpp>
71 #endif
72 #include <lifev/core/filter/ExporterEmpty.hpp>
73 
74 #include <iostream>
75 #include <sstream>
76 
77 
78 using namespace LifeV;
79 
80 int returnValue = EXIT_FAILURE;
81 
83 {
84  std::string stringList = list;
85  std::set<UInt> setList;
86  if ( list == "" )
87  {
88  return setList;
89  }
90  size_t commaPos = 0;
91  while ( commaPos != std::string::npos )
92  {
93  commaPos = stringList.find ( "," );
94  setList.insert ( atoi ( stringList.substr ( 0, commaPos ).c_str() ) );
95  stringList = stringList.substr ( commaPos + 1 );
96  }
97  setList.insert ( atoi ( stringList.c_str() ) );
98  return setList;
99 }
100 
101 
102 class Structure
103 {
104 public:
105 
107  typedef VectorEpetra vector_Type;
109 
110  typedef std::function<Real ( Real const&, Real const&, Real const&, Real const&, ID const& ) > fct_type;
111  //Exporters Typedefs
112  typedef LifeV::Exporter<mesh_Type > filter_Type;
114 
117 
120 
121 #ifdef HAVE_HDF5
124 #endif
125 
128 
129  /** @name Constructors, destructor
130  */
131  //@{
132  Structure ( int argc,
133  char** argv,
134  std::shared_ptr<Epetra_Comm> structComm );
135 
137  {}
138  //@}
139 
140  //@{
141  void run()
142  {
143  run3d();
144  }
145  //@}
146 
147 protected:
148 
149 private:
150 
151  /**
152  * run the 2D driven cylinder simulation
153  */
154  void run2d();
155 
156  /**
157  * run the 3D driven cylinder simulation
158  */
159  void run3d();
160 
161 private:
162  struct Private;
163  std::shared_ptr<Private> parameters;
164  filterPtr_Type importerSolid;
165 
166 };
167 
168 
169 
170 struct Structure::Private
171 {
173  rho (1), poisson (1), young (1), bulk (1), alpha (1), gamma (1)
174  {}
175  double rho, poisson, young, bulk, alpha, gamma;
176 
177  std::string data_file_name;
178 
179  std::shared_ptr<Epetra_Comm> comm;
180 
181  static Real uexact (const Real& /*t*/, const Real& X, const Real& Y, const Real& /*Z*/, const ID& i)
182  {
183  //Setting up the data of the problem
184  Real E = 8e+6;
185  Real poi = 0.45;
186  Real mu = E / ( 2 * ( 1 + poi ) );
187  Real lambda = ( E * poi ) / ( ( 1 + poi ) * ( 1 - 2 * poi ) );
188  Real Rin = 0.5;
189  Real Rout = 0.6;
190  Real Pin = 1000.0;
191  Real Pout = 0;
192 
193 
194  // Defining the new variables
195  Real radius = std::sqrt ( X * X + Y * Y );
196  Real theta = std::atan ( Y / X );
197 
198  switch (i)
199  {
200  case 0:
201  //u_x = cos(theta) * u_r(r)
202  return std::cos (theta) * ( ( ( radius / (2.0 * (mu + lambda) ) ) * ( ( Rin * Rin * Pin - Rout * Rout * Pout ) / ( Rout * Rout - Rin * Rin ) ) )
203  + ( ( (Rin * Rin * Rout * Rout ) / ( 2 * mu * radius) ) * ( ( Pin - Pout ) / ( Rout * Rout - Rin * Rin ) ) ) );
204 
205  break;
206  case 1:
207  //u_x = sin(theta) * u_r(r)
208  return std::sin (theta) * ( ( ( radius / (2.0 * (mu + lambda) ) ) * ( ( Rin * Rin * Pin - Rout * Rout * Pout ) / ( Rout * Rout - Rin * Rin ) ) )
209  + ( ( (Rin * Rin * Rout * Rout ) / ( 2 * mu * radius) ) * ( ( Pin - Pout ) / ( Rout * Rout - Rin * Rin ) ) ) );
210 
211  break;
212  case 2:
213  return 0.0;
214  break;
215  default:
216  ERROR_MSG ("This entry is not allowed!!");
217  return 0;
218  break;
219  }
220 
221  return 0.;
222  }
223 
224 };
225 
226 
227 
228 Structure::Structure ( int argc,
229  char** argv,
230  std::shared_ptr<Epetra_Comm> structComm) :
231  parameters ( new Private() )
232 {
233  GetPot command_line (argc, argv);
234  std::string data_file_name = command_line.follow ("data", 2, "-f", "--file");
235  GetPot dataFile ( data_file_name );
236  parameters->data_file_name = data_file_name;
237 
238  // parameters->rho = dataFile( "solid/physics/density", 1. );
239  // parameters->young = dataFile( "solid/physics/young", 1. );
240  // parameters->poisson = dataFile( "solid/physics/poisson", 1. );
241  // parameters->bulk = dataFile( "solid/physics/bulk", 1. );
242  // parameters->alpha = dataFile( "solid/physics/alpha", 1. );
243  // parameters->gamma = dataFile( "solid/physics/gamma", 1. );
244 
245  // std::cout << "density = " << parameters->rho << std::endl
246  // << "young = " << parameters->young << std::endl
247  // << "poisson = " << parameters->poisson << std::endl
248  // << "bulk = " << parameters->bulk << std::endl
249  // << "alpha = " << parameters->alpha << std::endl
250  // << "gamma = " << parameters->gamma << std::endl;
251 
252  parameters->comm = structComm;
253  int ntasks = parameters->comm->NumProc();
254 
255  if (!parameters->comm->MyPID() )
256  {
257  std::cout << "My PID = " << parameters->comm->MyPID() << " out of " << ntasks << " running." << std::endl;
258  }
259 }
260 
261 
262 
263 void
264 Structure::run2d()
265 {
266  std::cout << "2D cylinder test case is not available yet\n";
267 }
268 
269 
270 
271 void
272 Structure::run3d()
273 {
274 
275  bool verbose = (parameters->comm->MyPID() == 0);
276 
277  //! Number of boundary conditions for the velocity and mesh motion
278  std::shared_ptr<BCHandler> BCh ( new BCHandler() );
279 
280  //! dataElasticStructure
281  GetPot dataFile ( parameters->data_file_name.c_str() );
282 
283  //Loading a partitoned mesh or reading a new one
284  const std::string partitioningMesh = dataFile ( "partitioningOffline/loadMesh", "no");
285 
286  //Creation of pointers
287  std::shared_ptr<MeshPartitioner<mesh_Type> > meshPart;
288  std::shared_ptr<mesh_Type> pointerToMesh;
289 
290 #ifdef LIFEV_HAS_HDF5
291  if ( ! (partitioningMesh.compare ("no") ) )
292 #endif
293  {
294  std::shared_ptr<mesh_Type > fullMeshPtr (new mesh_Type ( ( parameters->comm ) ) );
295  //Creating a new mesh from scratch
296  MeshData meshData;
297  meshData.setup (dataFile, "solid/space_discretization");
298  readMesh (*fullMeshPtr, meshData);
299 
300  meshPart.reset ( new MeshPartitioner<mesh_Type> (fullMeshPtr, parameters->comm ) );
301 
302  pointerToMesh = meshPart->meshPartition();
303  }
304 #ifdef LIFEV_HAS_HDF5
305  else
306  {
307  //Creating a mesh object from a partitioned mesh
308  const std::string partsFileName (dataFile ("partitioningOffline/hdf5_file_name", "NO_DEFAULT_VALUE.h5") );
309 
310  std::shared_ptr<Epetra_MpiComm> mpiComm =
311  std::dynamic_pointer_cast<Epetra_MpiComm> (parameters->comm);
312  PartitionIO<mesh_Type> partitionIO (partsFileName, mpiComm);
313 
314  partitionIO.read (pointerToMesh);
315 
316  }
317 #endif
318 
319  std::string dOrder = dataFile ( "solid/space_discretization/order", "P1");
320 
321  solidFESpacePtr_Type dFESpace ( new solidFESpace_Type (pointerToMesh, dOrder, 3, parameters->comm) );
322 
323  // setting precise quadrature rule for fine meshes
324  const QuadratureRule fineQuadRule = quadRuleTetra15pt;
325  QuadratureRule fineBdQuadRule = quadRuleTria4pt;
326 
327  dFESpace->setQuadRule ( fineQuadRule );
328  dFESpace->setBdQuadRule ( fineBdQuadRule );
329  dFESpace->qr().showMe();
330 
331  if (verbose)
332  {
333  std::cout << std::endl;
334  }
335 
336  if (verbose)
337  {
338  std::cout << "Setting up the reader and the iterations!! " << std::endl;
339  }
340 
341  //Reading fileNames - setting data for reading
342  std::string const importerType = dataFile ( "importer/type", "ensight");
343  std::string const fileName = dataFile ( "importer/filename", "structure");
344  std::string const initialLoaded = dataFile ( "importer/initialSol", "NO_DEFAULT_VALUE");
345  LifeV::Real initialTime = dataFile ( "importer/initialTime", 0.0);
346 
347  //Creating the importer
348 #ifdef HAVE_HDF5
349  if ( !importerType.compare ("hdf5") )
350  {
351  importerSolid.reset ( new hdf5Filter_Type ( dataFile, fileName) );
352  }
353  else
354 #endif
355  {
356  if ( !importerType.compare ("none") )
357  {
358  importerSolid.reset ( new emptyExporter_Type ( dataFile, dFESpace->mesh(), "solid", dFESpace->map().comm().MyPID() ) );
359  }
360  else
361  {
362  importerSolid.reset ( new ensightFilter_Type ( dataFile, fileName) );
363  }
364  }
365 
366  importerSolid->setMeshProcId (dFESpace->mesh(), dFESpace->map().comm().MyPID() );
367 
368  //Creation of Exporter to check the loaded solution (working only for HDF5)
369  // std::string expVerFile = "verificationDisplExporter";
370  // LifeV::ExporterHDF5<RegionMesh<LinearTetra> > exporter( dataFile, pointerToMesh, expVerFile, parameters->comm->MyPID());
371  // vectorPtr_Type vectVer ( new vector_Type(solid.displacement(), LifeV::Unique ) );
372 
373  // exporter.addVariable( ExporterData<mesh_Type >::VectorField, "displVer", dFESpace, vectVer, UInt(0) );
374 
375  // exporter.postProcess(0.0);
376 
377  //Reading the displacement field
378  vectorPtr_Type solidDisp (new vector_Type (dFESpace->map(), importerSolid->mapType() ) );
379  *solidDisp *= 0.0;
380 
381  std::string iterationString;
382 
383  //Loading the stencil
384  iterationString = initialLoaded;
385 
386  //Reading
387  LifeV::ExporterData<mesh_Type> solidDataReader (LifeV::ExporterData<mesh_Type>::VectorField, std::string ("displacement." + iterationString), dFESpace, solidDisp, UInt (0), LifeV::ExporterData<mesh_Type>::UnsteadyRegime );
388 
389  importerSolid->readVariable (solidDataReader);
390 
391  //Exporting the just loaded solution (debug purposes)
392  // Real currentLoading(iterInit + 1.0);
393  // *vectVer = *solidDisp;
394  // exporter.postProcess( currentLoading );
395 
396  //It is not useful anymore.
397  importerSolid->closeFile();
398 
399  //Compute the error at the moment, the L2 error
400  Real L2_Error, L2_RelError;
401 
402  vector_Type solution (*solidDisp, Repeated);
403 
404 
405  //Creation of Exporter to check the loaded solution (working only for HDF5)
406  // std::string expVerFile = "exactSolution";
407  // LifeV::ExporterHDF5<RegionMesh<LinearTetra> > exporter( dataFile, pointerToMesh, expVerFile, parameters->comm->MyPID());
408  // vectorPtr_Type vectVer ( new vector_Type(*solidDisp, LifeV::Unique ) );
409 
410  // exporter.addVariable( ExporterData<mesh_Type >::VectorField, "exactDispl", dFESpace, vectVer, UInt(0) );
411 
412  // exporter.postProcess(0.0);
413 
414  // //interpolating the exact function on the FESpace
415  // dFESpace->interpolate( static_cast<solidFESpace_Type::function_Type>( Private::uexact ), *vectVer , 0);
416 
417  // exporter.postProcess(1.0);
418 
419  L2_Error = dFESpace->l2Error (Private::uexact, solution, initialTime , &L2_RelError);
420 
421  std::ofstream out_norm;
422  //save the norm
423  if (verbose)
424  {
425  out_norm.open ("norm.txt", std::ios::app);
426  out_norm << L2_Error << " "
427  << L2_RelError << "\n";
428  out_norm.close();
429  }
430 
431 
432  MPI_Barrier (MPI_COMM_WORLD);
433 
434  std::cout << "Relative error L2: " << L2_RelError << std::endl;
435 }
436 
437 int
438 main ( int argc, char** argv )
439 {
440 
441 #ifdef HAVE_MPI
442  MPI_Init (&argc, &argv);
443  std::shared_ptr<Epetra_MpiComm> Comm (new Epetra_MpiComm ( MPI_COMM_WORLD ) );
444  if ( Comm->MyPID() == 0 )
445  {
446  std::cout << "% using MPI" << std::endl;
447  }
448 #else
449  std::shared_ptr<Epetra_SerialComm> Comm ( new Epetra_SerialComm() );
450  std::cout << "% using serial Version" << std::endl;
451 #endif
452 
453  Structure structure ( argc, argv, Comm );
454  structure.run();
455 
456 #ifdef HAVE_MPI
457  MPI_Finalize();
458 #endif
459  return returnValue ;
460 }
Structure(int argc, char **argv, std::shared_ptr< Epetra_Comm > structComm)
ExporterEnsight data exporter.
const QuadratureRule quadRuleTetra15pt(pt_tetra_15pt, 5, "Quadrature rule 15 points on a tetraedra", TETRA, 15, 5)
const QuadratureRule quadRuleTria4pt(pt_tria_4pt, 3, "Quadrature rule 4 points on a triangle", TRIANGLE, 4, 3)
GetPot(const int argc_, char **argv_, const char *FieldSeparator=0x0)
Definition: GetPot.hpp:507
std::function< Real(Real const &, Real const &, Real const &, Real const &, ID const &) > fct_type
LifeV::ExporterEnsight< mesh_Type > ensightFilter_Type
LifeV::Exporter< mesh_Type > filter_Type
std::shared_ptr< hdf5Filter_Type > hdf5FilterPtr_Type
FESpace< RegionMesh< LinearTetra >, MapEpetra > solidFESpace_Type
std::set< UInt > parseList(const std::string &list)
#define ERROR_MSG(A)
Definition: LifeAssert.hpp:69
void run3d()
run the 3D driven cylinder simulation
uint32_type ID
IDs.
Definition: LifeV.hpp:194
LifeV::RegionMesh< LinearTetra > mesh_Type
void run2d()
run the 2D driven cylinder simulation
std::shared_ptr< ensightFilter_Type > ensightFilterPtr_Type
MeshData - class for handling spatial discretization.
Definition: MeshData.hpp:72
LifeV::ExporterEmpty< mesh_Type > emptyExporter_Type
double Real
Generic real data.
Definition: LifeV.hpp:175
std::shared_ptr< emptyExporter_Type > emptyExporterPtr_Type
std::shared_ptr< LifeV::Exporter< mesh_Type > > filterPtr_Type
QuadratureRule - The basis class for storing and accessing quadrature rules.
static Real uexact(const Real &, const Real &X, const Real &Y, const Real &, const ID &i)
int main(int argc, char **argv)
std::shared_ptr< solidFESpace_Type > solidFESpacePtr_Type