LifeV
lifev/navier_stokes_blocks/examples/example_external_flow/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  {
68 
69  typedef RegionMesh<LinearTetra> mesh_Type;
70  typedef VectorEpetra vector_Type;
71  typedef std::shared_ptr<vector_Type> vectorPtr_Type;
72 
73  // Reading the dataFile
74  const std::string defaultDataName = "data";
75  GetPot command_line (argc, argv);
76  std::string data_file_name = command_line.follow (defaultDataName.c_str(), 2, "-f", "--file");
77  GetPot dataFile( data_file_name );
78 
79  // reading the mesh
80  std::shared_ptr<mesh_Type > fullMeshPtr ( new mesh_Type ( Comm ) );
81  MeshData meshData;
82  meshData.setup (dataFile, "fluid/space_discretization");
83  readMesh (*fullMeshPtr, meshData);
84 
85  // mesh partitioner
86  MeshPartitioner< mesh_Type > meshPart (fullMeshPtr, Comm);
87  std::shared_ptr<mesh_Type > localMeshPtr ( new mesh_Type ( Comm ) );
88  localMeshPtr = meshPart.meshPartition();
89  fullMeshPtr.reset();
90 
91  // create the solver
92  NavierStokesSolverBlocks ns( dataFile, Comm);
93  ns.setup(localMeshPtr);
96 
97  Real saveAfter = dataFile("fluid/save_after", 0.0);
98 
99  bool useStabilization = dataFile("fluid/stabilization/use", false);
100  std::string stabilizationType = dataFile("fluid/stabilization/type", "none");
101 
102  int saveEvery = dataFile ( "fluid/save_every", 1 );
103  int counterSaveEvery = saveEvery;
104 
105  // Time handler objects to deal with time advancing and extrapolation
106  TimeAndExtrapolationHandler timeVelocity;
107  Real dt = dataFile("fluid/time_discretization/timestep",0.0);
108  Real t0 = dataFile("fluid/time_discretization/initialtime",0.0);
109  Real tFinal = dataFile("fluid/time_discretization/endtime",0.0);
110  UInt orderBDF = dataFile("fluid/time_discretization/BDF_order",2);
111 
112  // Order of BDF and extrapolation for the velocity
113  timeVelocity.setBDForder(orderBDF);
114  timeVelocity.setMaximumExtrapolationOrder(orderBDF);
115  timeVelocity.setTimeStep(dt);
116 
117  // Initialize time advance
118  vector_Type velocityInitial ( ns.uFESpace()->map() );
119  std::vector<vector_Type> initialStateVelocity;
120  velocityInitial *= 0 ;
121  for ( UInt i = 0; i < orderBDF; ++i )
122  initialStateVelocity.push_back(velocityInitial);
123 
124  timeVelocity.initialize(initialStateVelocity);
125 
126  TimeAndExtrapolationHandler timePressure;
127  if ( useStabilization && stabilizationType.compare("VMSLES_SEMI_IMPLICIT")==0 )
128  {
129  timePressure.setBDForder(orderBDF);
130  timePressure.setMaximumExtrapolationOrder(orderBDF);
131  timePressure.setTimeStep(dt);
132 
133  vector_Type pressureInitial ( ns.pFESpace()->map() );
134  std::vector<vector_Type> initialStatePressure;
135  pressureInitial.zero();
136  for ( UInt i = 0; i < orderBDF; ++i )
137  initialStatePressure.push_back(pressureInitial);
138 
139  timePressure.initialize(initialStatePressure);
140  }
141 
142  // Exporter
143  std::string outputName = dataFile ( "exporter/filename", "result");
144  std::shared_ptr< ExporterHDF5<mesh_Type > > exporter;
145 
146  exporter.reset ( new ExporterHDF5<mesh_Type > ( dataFile, outputName ) );
147  exporter->setPostDir ( "./" );
148  exporter->setMeshProcId ( localMeshPtr, Comm->MyPID() );
149  vectorPtr_Type velocity( new vector_Type(ns.uFESpace()->map(), exporter->mapType() ) );
150  vectorPtr_Type pressure( new vector_Type(ns.pFESpace()->map(), exporter->mapType() ) );
151  exporter->addVariable ( ExporterData<mesh_Type>::VectorField, "velocity", ns.uFESpace(), velocity, UInt (0) );
152  exporter->addVariable ( ExporterData<mesh_Type>::ScalarField, "pressure", ns.pFESpace(), pressure, UInt (0) );
153 
154  exporter->postProcess ( t0 );
155 
156  // Boundary conditions
157  std::shared_ptr<BCHandler> bc ( new BCHandler (*BCh_fluid ()) );
158  std::shared_ptr<BCHandler> bc_drag ( new BCHandler (*BCh_drag ()) );
159  std::shared_ptr<BCHandler> bc_lift ( new BCHandler (*BCh_lift ()) );
160 
161  std::string preconditioner = dataFile("fluid/preconditionerType","none");
162 
163  // Time loop
164  LifeChrono iterChrono;
165  Real time = t0 + dt;
166 
167  vectorPtr_Type u_star( new vector_Type(ns.uFESpace()->map(), Unique ) );
168  vectorPtr_Type p_star( new vector_Type(ns.pFESpace()->map(), Unique ) );
169  vectorPtr_Type rhs_velocity( new vector_Type(ns.uFESpace()->map(), Unique ) );
170 
171  ns.setAlpha(timeVelocity.alpha());
172  ns.setTimeStep(dt);
173 
174  int time_step_count = 0;
175 
176  VectorSmall<2> AerodynamicCoefficients;
177  Real S = 0.5;
178  Real factor = 2.0/(1000.0*22.0*22.0*S);
179  std::ofstream outputFile_coefficients;
180 
181  if ( verbose )
182  {
183  outputFile_coefficients.open ("Aerodynamic_Coefficients.txt");
184  outputFile_coefficients << "% time / X force / Y force \n" << std::flush;
185  }
186 
187  for ( ; time <= tFinal + dt / 2.; time += dt)
188  {
189  time_step_count += 1;
190 
191  if (verbose)
192  std::cout << "\nWe are at time " << time << " s\n\n";
193 
194  iterChrono.reset();
195  iterChrono.start();
196 
197  u_star->zero();
198  rhs_velocity->zero();
199  timeVelocity.extrapolate (orderBDF, *u_star);
200  timeVelocity.rhsContribution (*rhs_velocity);
201 
202  if ( useStabilization && stabilizationType.compare("VMSLES_SEMI_IMPLICIT")==0 )
203  {
204  timePressure.extrapolate (orderBDF,*p_star);
206  }
207 
208  ns.updateSystem ( u_star, rhs_velocity );
209  ns.iterate( bc, time );
210 
211  AerodynamicCoefficients = ns.computeForces(*bc_drag, *bc_lift);
212 
213  if (verbose)
214  {
215  std::cout << "\nDrag coefficient " << AerodynamicCoefficients[0]*factor << "\n";
216  std::cout << "\nLift coefficient " << AerodynamicCoefficients[1]*factor << "\n";
217  outputFile_coefficients << time << " " << AerodynamicCoefficients[0]*factor << " " << AerodynamicCoefficients[1]*factor << "\n" << std::flush;
218  }
219 
220  ns.updateVelocity(velocity);
221  ns.updatePressure(pressure);
222 
223  iterChrono.stop();
224 
225  if (verbose)
226  std::cout << "\nTimestep solved in " << iterChrono.diff() << " s\n";
227 
228  // This part below handles the exporter of the solution.
229  // The code takes care of exporting the solution also at
230  // the previous time steps such that, if later a restart
231  // of the simulation is performed, it will works correctly.
232  if ( orderBDF == 1 )
233  {
234  if ( time_step_count == counterSaveEvery )
235  {
236  if ( time >= saveAfter )
237  {
238  exporter->postProcess ( time );
239  }
240  }
241  }
242  else if ( orderBDF == 2 )
243  {
244  if ( time_step_count == (counterSaveEvery-1) )
245  {
246  if ( time >= saveAfter )
247  {
248  exporter->postProcess ( time );
249  }
250  }
251  else if ( time_step_count == counterSaveEvery )
252  {
253  if ( time >= saveAfter )
254  {
255  exporter->postProcess ( time );
256  }
257  counterSaveEvery += saveEvery;
258  }
259  }
260 
261  timeVelocity.shift(*velocity);
262 
263  if ( useStabilization && stabilizationType.compare("VMSLES_SEMI_IMPLICIT")==0 )
264  timePressure.shift(*pressure);
265 
266  }
267 
268  if (verbose )
269  {
270  outputFile_coefficients.close();
271  }
272 
273  exporter->closeFile();
274 
275  }
276 
277 #ifdef HAVE_MPI
278  if (verbose)
279  {
280  std::cout << "\nMPI Finalization" << std::endl;
281  }
282  MPI_Finalize();
283 #endif
284  return ( EXIT_SUCCESS );
285 }
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
void updateSystem(const vectorPtr_Type &u_star, const vectorPtr_Type &rhs_velocity)
Update the convective term and the right hand side.
GetPot(const int argc_, char **argv_, const char *FieldSeparator=0x0)
Definition: GetPot.hpp:507
std::shared_ptr< std::vector< Int > > M_isOnProc
void setExtrapolatedPressure(const vectorPtr_Type &pressure_extrapolated)
Set the extrapolated pressure vector (used by semi-implicit VMS-LES stabilization) ...
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 updateVelocity(vectorPtr_Type &velocity)
Get the velocity vector.
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.
bool operator()(const char *VarName, bool Default) const
Definition: GetPot.hpp:2008
int main(int argc, char **argv)
uint32_type UInt
generic unsigned integer (used mainly for addressing)
Definition: LifeV.hpp:191