LifeV
lifev/navier_stokes_blocks/testsuite/nonlinear_time_dependent_navier_stokes/main.cpp
Go to the documentation of this file.
1 /* -*- mode: c++ -*-
2 
3  This file is part of the LifeV library.
4  Copyright (C) 2010 EPFL
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2.1 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful, but
12  WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  USA
20 */
21 /**
22  \file main.cpp
23  \author Davide Forti <davide.forti@epfl.ch>
24  \date 2014-02-06
25  */
26 
27 
28 #include <Epetra_ConfigDefs.h>
29 #ifdef EPETRA_MPI
30 #include <mpi.h>
31 #include <Epetra_MpiComm.h>
32 #else
33 #include <Epetra_SerialComm.h>
34 #endif
35 
36 
37 #include <lifev/core/LifeV.hpp>
38 #include <lifev/core/mesh/MeshData.hpp>
39 #include <lifev/core/mesh/MeshPartitioner.hpp>
40 #include <lifev/navier_stokes_blocks/solver/NavierStokesSolverBlocks.hpp>
41 #include <lifev/core/fem/TimeAndExtrapolationHandler.hpp>
42 #include <lifev/core/filter/ExporterEnsight.hpp>
43 #include <lifev/core/filter/ExporterHDF5.hpp>
44 #include <lifev/core/filter/ExporterVTK.hpp>
45 #include <Teuchos_XMLParameterListHelpers.hpp>
46 
48 
49 using namespace LifeV;
50 
51 int
52 main ( int argc, char** argv )
53 {
54  bool verbose (false);
55 #ifdef HAVE_MPI
56  MPI_Init (&argc, &argv);
57  std::shared_ptr<Epetra_Comm> Comm ( new Epetra_MpiComm (MPI_COMM_WORLD) );
58  if ( Comm->MyPID() == 0 )
59  {
60  verbose = true;
61  }
62 #else
63  std::shared_ptr<Epetra_Comm> Comm( new Epetra_SerialComm () );
64  verbose = true;
65 #endif
66 
67  typedef RegionMesh<LinearTetra> mesh_Type;
68  typedef VectorEpetra vector_Type;
69  typedef std::shared_ptr<vector_Type> vectorPtr_Type;
70 
71  // Reading the dataFile
72  const std::string defaultDataName = "data";
73  GetPot command_line (argc, argv);
74  std::string data_file_name = command_line.follow (defaultDataName.c_str(), 2, "-f", "--file");
75  GetPot dataFile( data_file_name );
76 
77  // reading the mesh
78  std::shared_ptr<mesh_Type > fullMeshPtr ( new mesh_Type ( Comm ) );
79  MeshData meshData;
80  meshData.setup (dataFile, "fluid/space_discretization");
81  readMesh (*fullMeshPtr, meshData);
82 
83  // mesh partitioner
84  MeshPartitioner< mesh_Type > meshPart (fullMeshPtr, Comm);
85  std::shared_ptr<mesh_Type > localMeshPtr ( new mesh_Type ( Comm ) );
86  localMeshPtr = meshPart.meshPartition();
87  fullMeshPtr.reset();
88 
89  // Time handler objects to deal with time advancing and extrapolation
90  TimeAndExtrapolationHandler timeVelocity;
91  Real dt = dataFile("fluid/time_discretization/timestep",0.0);
92  Real t0 = dataFile("fluid/time_discretization/initialtime",0.0);
93  Real tFinal = dataFile("fluid/time_discretization/endtime",0.0);
94  UInt orderBDF = dataFile("fluid/time_discretization/BDF_order",2);
95 
96  // Order of BDF and extrapolation for the velocity
97  timeVelocity.setBDForder(orderBDF);
98  timeVelocity.setMaximumExtrapolationOrder(orderBDF);
99  timeVelocity.setTimeStep(dt);
100 
101  // create the solver
102  NavierStokesSolverBlocks ns( dataFile, Comm);
103  ns.setup(localMeshPtr);
105 
106  // Initialize time advance
107  vector_Type velocityInitial ( ns.uFESpace()->map() );
108  std::vector<vector_Type> initialStateVelocity;
109  velocityInitial *= 0 ;
110  for ( UInt i = 0; i < orderBDF; ++i )
111  initialStateVelocity.push_back(velocityInitial);
112 
113  timeVelocity.initialize(initialStateVelocity);
114 
115  // Exporter
116  std::string outputName = dataFile ( "exporter/filename", "result");
117  std::shared_ptr< Exporter<mesh_Type > > exporter;
118  std::string const exporterType = dataFile ( "exporter/type", "ensight");
119 
120 #ifdef HAVE_HDF5
121  if (exporterType.compare ("hdf5") == 0)
122  {
123  exporter.reset ( new ExporterHDF5<mesh_Type > ( dataFile, outputName ) );
124  exporter->setPostDir ( "./" ); // This is a test to see if M_post_dir is working
125  exporter->setMeshProcId ( localMeshPtr, Comm->MyPID() );
126  }
127 #endif
128  else if(exporterType.compare ("vtk") == 0)
129  {
130  exporter.reset ( new ExporterVTK<mesh_Type > ( dataFile, outputName ) );
131  exporter->setPostDir ( "./" ); // This is a test to see if M_post_dir is working
132  exporter->setMeshProcId ( localMeshPtr, Comm->MyPID() );
133  }
134 
135  vectorPtr_Type velocity( new vector_Type(ns.uFESpace()->map(), exporter->mapType() ) );
136  vectorPtr_Type pressure( new vector_Type(ns.pFESpace()->map(), exporter->mapType() ) );
137  exporter->addVariable ( ExporterData<mesh_Type>::VectorField, "velocity", ns.uFESpace(), velocity, UInt (0) );
138  exporter->addVariable ( ExporterData<mesh_Type>::ScalarField, "pressure", ns.pFESpace(), pressure, UInt (0) );
139  exporter->postProcess ( t0 );
140 
141  // Boundary conditions
142  std::shared_ptr<BCHandler> bc ( new BCHandler (*BCh_fluid ()) );
143 
144  // Set boundary conditions
145  ns.setBoundaryConditions( bc );
146 
147  // Time loop
148  LifeChrono iterChrono;
149  Real time = t0 + dt;
150 
151  vectorPtr_Type rhs_velocity( new vector_Type(ns.uFESpace()->map(), Unique ) );
152 
153  ns.setAlpha(timeVelocity.alpha());
154  ns.setTimeStep(dt);
155 
156  ns.buildSystem();
157 
158  for ( ; time <= tFinal + dt / 2.; time += dt)
159  {
160  if (verbose)
161  std::cout << "\nWe are at time " << time << " s\n\n";
162 
163  iterChrono.reset();
164  iterChrono.start();
165 
166  rhs_velocity->zero();
167  timeVelocity.rhsContribution (*rhs_velocity);
168 
169  ns.setRhsVelocity(rhs_velocity);
170 
171  ns.iterate_nonlinear( time );
172 
173  ns.updateVelocity(velocity);
174  ns.updatePressure(pressure);
175 
176  iterChrono.stop();
177 
178  if (verbose)
179  std::cout << "\nTimestep solved in " << iterChrono.diff() << " s\n";
180 
181  exporter->postProcess ( time );
182 
183  timeVelocity.shift(*velocity);
184 
185  }
186 
187  exporter->closeFile();
188 
189  Real normTwo_Velo = velocity->norm2();
190  Real normTwo_Pres = pressure->norm2();
191 
192 #ifdef HAVE_MPI
193  if (verbose)
194  {
195  std::cout << "\nMPI Finalization" << std::endl;
196  }
197  MPI_Finalize();
198 #endif
199 
200  if ( std::abs(normTwo_Velo - 36.87669 ) < 1.0e-4 && std::abs(normTwo_Pres - 118.83078 ) < 1.0e-4 )
201  {
202  return ( EXIT_SUCCESS );
203  }
204  else
205  {
206  return ( EXIT_FAILURE );
207  }
208 }
void setAlpha(const Real &alpha)
Set coefficient associated to the time discretization scheme.
double operator()(const char *VarName, const double &Default) const
Definition: GetPot.hpp:2033
GetPot(const int argc_, char **argv_, const char *FieldSeparator=0x0)
Definition: GetPot.hpp:507
void setRhsVelocity(const vectorPtr_Type &vel_rhs)
Set the rhs vector, velocity component.
std::shared_ptr< std::vector< Int > > M_isOnProc
MeshData - class for handling spatial discretization.
Definition: MeshData.hpp:72
void setParameters()
Set parameters of the solver.
double Real
Generic real data.
Definition: LifeV.hpp:175
void iterate_nonlinear(const Real &time)
Solve the Navier-Stokes equations at a certain time.
void updatePressure(vectorPtr_Type &pressure)
Get the pressure vector.
void setTimeStep(const Real &dt)
Set time step.
int operator()(const char *VarName, int Default) const
Definition: GetPot.hpp:2021
void buildSystem()
Assemble constant terms.
int main(int argc, char **argv)
uint32_type UInt
generic unsigned integer (used mainly for addressing)
Definition: LifeV.hpp:191