LifeV
ensightToHdf5.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 Convert from Ensight to HDF5
30  *
31  * @author Simone Deparis <simone.deparis@epfl.ch>
32  * @date 08-08-2008
33  */
34 
35 
36 #include <Epetra_ConfigDefs.h>
37 #ifdef EPETRA_MPI
38 #include <mpi.h>
39 #include <Epetra_MpiComm.h>
40 #else
41 #include <Epetra_SerialComm.h>
42 #endif
43 
44 
45 #include <lifev/core/array/MatrixEpetra.hpp>
46 #include <lifev/core/array/MapEpetra.hpp>
47 #include <lifev/core/mesh/MeshData.hpp>
48 #include <lifev/core/mesh/MeshPartitioner.hpp>
49 #include <lifev/core/fem/FESpace.hpp>
50 #include <lifev/core/filter/ExporterEnsight.hpp>
51 #include <lifev/core/filter/ExporterHDF5.hpp>
52 #include <lifev/core/fem/ReferenceFE.hpp>
53 #include <lifev/core/fem/QuadratureRule.hpp>
54 #include <lifev/navier_stokes/fem/TimeAdvanceBDFNavierStokes.hpp>
55 #include <lifev/navier_stokes/solver/OseenSolver.hpp>
56 #include <lifev/navier_stokes/solver/OseenData.hpp>
57 
58 #include <iostream>
59 
60 #include "ensightToHdf5.hpp"
61 #include <array>
62 
63 using namespace LifeV;
64 
70 
71 
72 void
73 computeP0pressure (const feSpacePtr_Type& pFESpacePtr,
74  const feSpacePtr_Type& p0FESpacePtr,
75  const feSpacePtr_Type& uFESpacePtr,
76  const vector_Type& velAndPressureExport,
77  vector_Type& P0pres, Real /*time*/ );
78 
80 {
81  Private() {}
82 
84 
86 };
87 
89  char** argv )
90  :
91  d ( new Private )
92 {
93  GetPot command_line (argc, argv);
94  std::string data_file_name = command_line.follow ("data", 2, "-f", "--file");
95  GetPot dataFile ( data_file_name );
96 
97  d->data_file_name = data_file_name;
98 
99 #ifdef EPETRA_MPI
100  std::cout << "mpi initialization ... " << std::endl;
101 
102  // MPI_Init(&argc,&argv);
103  d->comm.reset ( new Epetra_MpiComm ( MPI_COMM_WORLD ) );
104  // int err = MPI_Comm_size(MPI_COMM_WORLD, &ntasks);
105 #else
106  d->comm.reset ( new Epetra_SerialComm() );
107 #endif
108 
109  if (!d->comm->MyPID() )
110  {
111  std::cout << "My PID = " << d->comm->MyPID() << " out of " << d->comm->NumProc () << " running." << std::endl;
112  }
113 }
114 
115 void
117 {
118 
119  // Reading from data file
120  GetPot dataFile ( d->data_file_name.c_str() );
121 
122  bool verbose = (d->comm->MyPID() == 0);
123 
124  // Fluid solver
125  std::shared_ptr<OseenData> oseenData (new OseenData() );
126  oseenData->setup ( dataFile );
127 
128  MeshData meshData;
129  meshData.setup (dataFile, "fluid/space_discretization");
130 
131  std::shared_ptr<mesh_Type > fullMeshPtr (new mesh_Type ( d->comm ) );
132  readMesh (*fullMeshPtr, meshData);
133 
134  // writeMesh("test.mesh", *fullMeshPtr);
135  // Scale, Rotate, Translate (if necessary)
136  std::array< Real, NDIM > geometryScale;
137  std::array< Real, NDIM > geometryRotate;
138  std::array< Real, NDIM > geometryTranslate;
139 
140  geometryScale[0] = dataFile ( "fluid/space_discretization/transform", 1., 0);
141  geometryScale[1] = dataFile ( "fluid/space_discretization/transform", 1., 1);
142  geometryScale[2] = dataFile ( "fluid/space_discretization/transform", 1., 2);
143 
144  geometryRotate[0] = dataFile ( "fluid/space_discretization/transform", 0., 3) * M_PI / 180;
145  geometryRotate[1] = dataFile ( "fluid/space_discretization/transform", 0., 4) * M_PI / 180;
146  geometryRotate[2] = dataFile ( "fluid/space_discretization/transform", 0., 5) * M_PI / 180;
147 
148  geometryTranslate[0] = dataFile ( "fluid/space_discretization/transform", 0., 6);
149  geometryTranslate[1] = dataFile ( "fluid/space_discretization/transform", 0., 7);
150  geometryTranslate[2] = dataFile ( "fluid/space_discretization/transform", 0., 8);
151 
152  MeshUtility::MeshTransformer<mesh_Type, mesh_Type::markerCommon_Type > _transformMesh (*fullMeshPtr);
153  _transformMesh.transformMesh ( geometryScale, geometryRotate, geometryTranslate );
154 
155  std::shared_ptr<mesh_Type > meshPtr;
156  {
157  MeshPartitioner< mesh_Type > meshPart (fullMeshPtr, d->comm);
158  meshPtr = meshPart.meshPartition();
159  }
160 
161  std::string uOrder = dataFile ( "fluid/space_discretization/vel_order", "P1");
162  std::string pOrder = dataFile ( "fluid/space_discretization/press_order", "P1");
163 
164  //oseenData->meshData()->setMesh(meshPtr);
165 
166  if (verbose)
167  {
168  std::cout << "Building the velocity FE space ... " << std::flush;
169  }
170 
171  feSpacePtr_Type uFESpacePtr ( new feSpace_Type (meshPtr, uOrder, 3, d->comm) );
172 
173  if (verbose)
174  {
175  std::cout << "ok." << std::endl;
176  }
177 
178  if (verbose)
179  {
180  std::cout << "Building the pressure FE space ... " << std::flush;
181  }
182 
183  feSpacePtr_Type pFESpacePtr ( new feSpace_Type ( meshPtr, pOrder, 1, d->comm ) );
184 
185  if (verbose)
186  {
187  std::cout << "ok." << std::endl;
188  }
189 
190  if (verbose)
191  {
192  std::cout << "Building the P0 pressure FE space ... " << std::flush;
193  }
194 
195  feSpacePtr_Type p0FESpacePtr ( new feSpace_Type ( meshPtr, feTetraP0, quadRuleTetra1pt,
196  quadRuleTria1pt, 1, d->comm ) );
197 
198  if (verbose)
199  {
200  std::cout << "ok." << std::endl;
201  }
202 
203 
204 
205  UInt totalVelDof = uFESpacePtr->map().map (Unique)->NumGlobalElements();
206  UInt totalPressDof = pFESpacePtr->map().map (Unique)->NumGlobalElements();
207  UInt totalP0PresDof = p0FESpacePtr->map().map (Unique)->NumGlobalElements();
208 
209  if (verbose)
210  {
211  std::cout << "Total Velocity DOF = " << totalVelDof << std::endl;
212  }
213  if (verbose)
214  {
215  std::cout << "Total Pressure DOF = " << totalPressDof << std::endl;
216  }
217  if (verbose)
218  {
219  std::cout << "Total P0Press DOF = " << totalP0PresDof << std::endl;
220  }
221 
222  if (verbose)
223  {
224  std::cout << "Calling the fluid constructor ... ";
225  }
226 
227  OseenSolver< mesh_Type > fluid (oseenData,
228  *uFESpacePtr,
229  *pFESpacePtr,
230  d->comm);
231  MapEpetra fullMap (fluid.getMap() );
232 
233  if (verbose)
234  {
235  std::cout << "ok." << std::endl;
236  }
237 
238  fluid.setUp (dataFile);
239 
240  // Initialization
241  Real dt = oseenData->dataTime()->timeStep();
242  Real t0 = oseenData->dataTime()->initialTime();
243  Real tFinal = oseenData->dataTime()->endTime();
244 
245  std::shared_ptr< Exporter<mesh_Type > > exporter;
246  std::shared_ptr< Exporter<mesh_Type > > importer;
247 
248  std::string const exporterType = dataFile ( "exporter/type", "hdf5");
249  std::string const exporterName = dataFile ( "exporter/filename", "ethiersteinman");
250  std::string const exportDir = dataFile ( "exporter/exportDir", "./");
251 
252  std::string const importerType = dataFile ( "importer/type", "ensight");
253  std::string const importerName = dataFile ( "importer/filename", "ethiersteinman");
254  std::string const importDir = dataFile ( "importer/importDir", "importDir/");
255 
256 #ifdef HAVE_HDF5
257  if (exporterType.compare ("hdf5") == 0)
258  {
259  exporter.reset ( new ExporterHDF5<mesh_Type > ( dataFile, exporterName ) );
260  }
261  else
262 #endif
263  exporter.reset ( new ExporterEnsight<mesh_Type > ( dataFile, exporterName ) );
264 
265  exporter->setPostDir ( exportDir ); // This is a test to see if M_post_dir is working
266  exporter->setMeshProcId ( meshPtr, d->comm->MyPID() );
267 
268 #ifdef HAVE_HDF5
269  if (importerType.compare ("hdf5") == 0)
270  {
271  importer.reset ( new ExporterHDF5<mesh_Type > ( dataFile, importerName ) );
272  }
273  else
274 #endif
275  importer.reset ( new ExporterEnsight<mesh_Type > ( dataFile, importerName ) );
276 
277  // todo this will not work with the ExporterEnsight filter (it uses M_importDir, a private variable)
278  importer->setPostDir ( importDir ); // This is a test to see if M_post_dir is working
279  importer->setMeshProcId ( meshPtr, d->comm->MyPID() );
280 
281  vectorPtr_Type velAndPressureExport ( new vector_Type (*fluid.solution(), exporter->mapType() ) );
282  vectorPtr_Type velAndPressureImport ( new vector_Type (*fluid.solution(), importer->mapType() ) );
283 
284  if ( exporter->mapType() == Unique )
285  {
286  velAndPressureExport->setCombineMode (Zero);
287  }
288 
289  importer->addVariable ( ExporterData<mesh_Type>::VectorField, "velocity", uFESpacePtr,
290  velAndPressureImport, UInt (0) );
291  importer->addVariable ( ExporterData<mesh_Type>::ScalarField, "pressure", pFESpacePtr,
292  velAndPressureImport, UInt (3 * uFESpacePtr->dof().numTotalDof() ) );
293  importer->import ( t0 );
294 
295  *velAndPressureExport = *velAndPressureImport;
296 
297 
298  vectorPtr_Type P0pres ( new vector_Type (p0FESpacePtr->map() ) );
299  MPI_Barrier (MPI_COMM_WORLD);
300  computeP0pressure (pFESpacePtr, p0FESpacePtr, uFESpacePtr, *velAndPressureImport, *P0pres, t0);
301 
302  exporter->addVariable ( ExporterData<mesh_Type>::VectorField, "velocity", uFESpacePtr,
303  velAndPressureExport, UInt (0) );
304 
305  exporter->addVariable ( ExporterData<mesh_Type>::ScalarField, "pressure", pFESpacePtr,
306  velAndPressureExport, UInt (3 * uFESpacePtr->dof().numTotalDof() ) );
307 
308  exporter->addVariable (ExporterData<mesh_Type>::ScalarField, "P0pressure", p0FESpacePtr,
309  P0pres, UInt (0),
310  ExporterData<mesh_Type>::SteadyRegime, ExporterData<mesh_Type>::Cell );
311  exporter->postProcess ( t0 );
312 
313  // Temporal loop
314  LifeChrono chrono;
315  int iter = 1;
316 
317  for ( Real time = t0 + dt ; time <= tFinal + dt / 2.; time += dt, iter++)
318  {
319  chrono.stop();
320  importer->import ( time );
321 
322  *velAndPressureExport = *velAndPressureImport;
323  MPI_Barrier (MPI_COMM_WORLD);
324  computeP0pressure (pFESpacePtr, p0FESpacePtr, uFESpacePtr, *velAndPressureImport, *P0pres, time);
325 
326  exporter->postProcess ( time );
327 
328  chrono.stop();
329  if (verbose)
330  {
331  std::cout << "Total iteration time " << chrono.diff() << " s." << std::endl;
332  }
333  }
334 }
335 
336 
337 void
338 computeP0pressure (const feSpacePtr_Type& pFESpacePtr,
339  const feSpacePtr_Type& p0FESpacePtr,
340  const feSpacePtr_Type& uFESpacePtr,
341  const vector_Type& velAndPressure,
342  vector_Type& P0pres,
343  Real /*time*/)
344 {
345 
346  int MyPID;
347  MPI_Comm_rank (MPI_COMM_WORLD, &MyPID);
348  UInt offset = 3 * uFESpacePtr->dof().numTotalDof();
349 
350  std::vector<int> gid0Vec (0);
351  gid0Vec.reserve (p0FESpacePtr->mesh()->numVolumes() );
352  std::vector<Real> val0Vec (0);
353  val0Vec.reserve (p0FESpacePtr->mesh()->numVolumes() );
354 
355  for (UInt ivol = 0; ivol < pFESpacePtr->mesh()->numVolumes(); ++ivol)
356  {
357 
358  pFESpacePtr->fe().update ( pFESpacePtr->mesh()->volumeList ( ivol ), UPDATE_DPHI );
359  p0FESpacePtr->fe().update ( p0FESpacePtr->mesh()->volumeList ( ivol ) );
360 
361  UInt eleID = pFESpacePtr->fe().currentLocalId();
362 
363  double tmpsum = 0.;
364  for (UInt iNode = 0; iNode < (UInt) pFESpacePtr->fe().nbFEDof(); iNode++)
365  {
366  int ig = pFESpacePtr->dof().localToGlobalMap ( eleID, iNode );
367  tmpsum += velAndPressure (ig + offset);
368  gid0Vec.push_back ( p0FESpacePtr->fe().currentId() );
369  val0Vec.push_back ( tmpsum / (double) pFESpacePtr->fe().nbFEDof() );
370  }
371  }
372  P0pres.setCoefficients (gid0Vec, val0Vec);
373  P0pres.globalAssemble();
374  MPI_Barrier (MPI_COMM_WORLD);
375 
376 }
std::shared_ptr< Epetra_Comm > comm
FESpace - Short description here please!
Definition: FESpace.hpp:78
std::shared_ptr< feSpace_Type > feSpacePtr_Type
GetPot(const int argc_, char **argv_, const char *FieldSeparator=0x0)
Definition: GetPot.hpp:507
OseenSolver< mesh_Type >::vectorPtr_Type vectorPtr_Type
OseenSolver< mesh_Type >::vector_Type vector_Type
#define M_PI
Definition: winmath.h:20
#define NDIM
Definition: LifeV.hpp:265
RegionMesh< LinearTetra > mesh_Type
Epetra_Import const & importer()
Getter for the Epetra_Import.
Definition: MapEpetra.cpp:394
void computeP0pressure(const feSpacePtr_Type &pFESpacePtr, const feSpacePtr_Type &p0FESpacePtr, const feSpacePtr_Type &uFESpacePtr, const vector_Type &velAndPressureExport, vector_Type &P0pres, Real)
std::shared_ptr< std::vector< Int > > M_isOnProc
MeshData - class for handling spatial discretization.
Definition: MeshData.hpp:72
double Real
Generic real data.
Definition: LifeV.hpp:175
EnsightToHdf5(int argc, char **argv)
FESpace< mesh_Type, MapEpetra > feSpace_Type
uint32_type UInt
generic unsigned integer (used mainly for addressing)
Definition: LifeV.hpp:191