LifeV
fefct.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 fefct.cpp
28  \author A. Fumagalli <alessio.fumagalli@mail.polimi.it>
29  \date 2011-05-02
30  */
31 /*
32  This simple test show how to use FEField (scalar and vector) and FEFunction.
33  After reading a mesh and creating the FESpaces the programs create three FEFields:
34  the first is a Raviart-Thomas field, the second is a P1 vector field and the third is a
35  P0 scalar field. Then the function is created, using the one written in user_fun.hpp,
36  and the fields are added. After that there is a computation of an error between the FEFunction
37  and its "free function" equivalence. Since we want to export the FEFunction we need to
38  interpolate it on a finite element space, in this case P0, so we call the interpolate over a new
39  field. The last part of the code contains the standard part to export the fields and the
40  interpolated function.
41 */
42 
43 // ===================================================
44 //! Includes
45 // ===================================================
46 
47 #include "fefct.hpp"
48 #include "user_fun.hpp"
49 
50 // ===================================================
51 //! Namespaces
52 // ===================================================
53 
54 using namespace LifeV;
55 
56 // ===================================================
57 //! Private Members
58 // ===================================================
59 
60 struct fefct::Private
61 {
62  Private() {}
63 
66 
68 
69 };
70 
71 // ===================================================
72 //! Constructors
73 // ===================================================
74 
75 fefct::fefct ( int argc,
76  char** argv )
77  : Members ( new Private )
78 {
79  GetPot command_line (argc, argv);
80  const std::string data_file_name = command_line.follow ("data", 2, "-f", "--file");
81  GetPot dataFile ( data_file_name );
82 
83  Members->data_file_name = data_file_name;
84  Members->discretization_section = "fefct";
85 
86 #ifdef EPETRA_MPI
87  std::cout << "Epetra Initialization" << std::endl;
88  Members->comm.reset ( new Epetra_MpiComm ( MPI_COMM_WORLD ) );
89  int ntasks;
90  MPI_Comm_size (MPI_COMM_WORLD, &ntasks);
91 #else
92  Members->comm.reset ( new Epetra_SerialComm() );
93 #endif
94 
95 }
96 
97 // ===================================================
98 //! Methods
99 // ===================================================
100 
101 Real
103 {
104  using namespace dataProblem;
105 
106  LifeChrono chronoTotal;
107  LifeChrono chronoReadAndPartitionMesh;
108  LifeChrono chronoFiniteElementSpace;
109  LifeChrono chronoFEFieldAndFEFct;
110  LifeChrono chronoAddFields;
111  LifeChrono chronoProblem;
112  LifeChrono chronoProcess;
113  LifeChrono chronoError;
114 
115  // Start chronoTotal for measure the total time for the computation
116  chronoTotal.start();
117 
118  // Reading from data file
119  GetPot dataFile ( Members->data_file_name.c_str() );
120 
121  // Create the displayer
122  Displayer displayer ( Members->comm );
123 
124  displayer.leaderPrint ( "The FEField and FEFunction test\n" );
125 
126  // Start chronoReadAndPartitionMesh for measure the total time for the creation of the local meshes
127  chronoReadAndPartitionMesh.start();
128 
129  // Create the mesh file handler
130  MeshData meshData;
131 
132  // Set up the mesh file
133  meshData.setup ( dataFile, Members->discretization_section + "/space_discretization");
134 
135  // Create the the mesh
136  regionMeshPtr_Type fullMeshPtr ( new regionMesh_Type ( Members->comm ) );
137 
138  // Set up the mesh
139  readMesh ( *fullMeshPtr, meshData );
140 
141  // Create local mesh using ParMetis partitioner
142  regionMeshPtr_Type meshPtr;
143  {
144  // local scope to properly delete the meshPart object
145  MeshPartitioner < regionMesh_Type > meshPart ( fullMeshPtr, Members->comm );
146  meshPtr = meshPart.meshPartition();
147  }
148 
149  // Stop chronoReadAndPartitionMesh
150  chronoReadAndPartitionMesh.stop();
151 
152  // The leader process print chronoReadAndPartitionMesh
153  displayer.leaderPrintMax ( "Time for read and partition the mesh ",
154  chronoReadAndPartitionMesh.diff() );
155 
156  // Create the FEField and FEFunction spaces
157 
158  // Start chronoFiniteElementSpace for measure the total time for create the finite element spaces
159  chronoFiniteElementSpace.start();
160 
161  // Finite element space of the first scalar field - RT0
162  FESpacePtr_Type scalarField1_FESpace ( new FESpace_Type ( meshPtr, feTetraP0, quadRuleTetra15pt,
163  quadRuleTria4pt, 1, Members->comm ) );
164 
165  // Finite element space of the second scalar field - P1
166  FESpacePtr_Type scalarField2_FESpace ( new FESpace_Type ( meshPtr, feTetraP1, quadRuleTetra15pt,
167  quadRuleTria4pt, 1, Members->comm ) );
168 
169  // Finite element space of the vector field - P0
170  FESpacePtr_Type vectorField_FESpace ( new FESpace_Type ( meshPtr, feTetraP0, quadRuleTetra15pt,
171  quadRuleTria4pt, 3, Members->comm ) );
172 
173  // Finite element space for the function visualization - P0
174  FESpacePtr_Type function_FESpace ( new FESpace_Type ( meshPtr, feTetraP0, quadRuleTetra15pt,
175  quadRuleTria4pt, 1, Members->comm ) );
176 
177  // Stop chronoFiniteElementSpace
178  chronoFiniteElementSpace.stop();
179 
180  // The leader process print chronoFiniteElementSpace
181  displayer.leaderPrintMax ( "Time for create the finite element spaces ",
182  chronoFiniteElementSpace.diff() );
183 
184  // Start chronoFEFieldAndFEFct for measure the total time for create the fields and the function
185  chronoFEFieldAndFEFct.start();
186 
187  // First scalar field
188  FEScalarFieldPtr_Type scalarField1 ( new FEScalarField_Type ( scalarField1_FESpace ) );
189  scalarField1->getVector() = 1. ;
190 
191  // Second scalar field
192  FEScalarFieldPtr_Type scalarField2 ( new FEScalarField_Type ( scalarField2_FESpace ) );
193  scalarField2->getVector() = 2.;
194 
195  // Vector field
196  FEVectorFieldPtr_Type vectorField ( new FEVectorField_Type ( vectorField_FESpace ) );
197  vectorField->getVector() = 3.;
198 
199  // Function
200  MyFun function;
201 
202  // Scalar field for visualize the function
203  FEScalarFieldPtr_Type scalarFieldFunction ( new FEScalarField_Type ( function_FESpace ) );
204 
205  // Stop chronoFEFieldAndFEFct
206  chronoFEFieldAndFEFct.stop();
207 
208  // The leader process print chronoFEFieldAndFEFct
209  displayer.leaderPrintMax ( "Time for create the fields and the function ",
210  chronoFEFieldAndFEFct.diff() );
211 
212  // Add the fields to the function
213 
214  // Start chronoAddFields
215  chronoAddFields.start();
216 
217  // Add the first scalar field
218  function.addScalarField ( scalarField1 );
219 
220  // Add the second scalar field
221  function.addScalarField ( scalarField2 );
222 
223  // Add the vector field
224  function.addVectorField ( vectorField );
225 
226  // Stop chronoAddFields
227  chronoAddFields.stop();
228 
229  // The leader process print chronoAddFields
230  displayer.leaderPrintMax ( "Time for create the add the fields to the function ",
231  chronoAddFields.diff() );
232 
233  // Define the dummy element and coordinate for the evaluation of the function
234  UInt iElem = 0;
235  Vector3D point;
236 
237  // Compute the error
238  Real error = 0;
239  error = std::fabs ( ( std::sin (1.) + 4. ) / 3. - function.eval ( iElem, point, 0. ) );
240 
241  // Interpolate the value of the function
242  function.interpolate ( *scalarFieldFunction );
243 
244  // Set the exporter for the fields
245  exporterPtr_Type exporter;
246 
247  // Type of the exporter
248  std::string const exporterType = dataFile ( "exporter/type", "hdf5");
249 
250  // Choose the exporter
251 #ifdef HAVE_HDF5
252  if ( exporterType.compare ("hdf5") == 0 )
253  {
254  exporter.reset ( new ExporterHDF5< regionMesh_Type > ( dataFile, dataFile ( "exporter/file_name", "FieldFunction" ) ) );
255 
256  // Set directory where to save the solution
257  exporter->setPostDir ( dataFile ( "exporter/folder", "./" ) );
258 
259  exporter->setMeshProcId ( meshPtr, Members->comm->MyPID() );
260  }
261  else
262 #endif
263  {
264  if ( exporterType.compare ("none") == 0 )
265  {
266  exporter.reset ( new ExporterEmpty< regionMesh_Type > ( dataFile, dataFile ( "exporter/file_name", "FieldFunction" ) ) );
267 
268  // Set directory where to save the solution
269  exporter->setPostDir ( dataFile ( "exporter/folder", "./" ) );
270 
271  exporter->setMeshProcId ( meshPtr, Members->comm->MyPID() );
272  }
273  else
274  {
275  exporter.reset ( new ExporterEnsight< regionMesh_Type > ( dataFile, dataFile ( "exporter/file_name", "FieldFunction" ) ) );
276 
277  // Set directory where to save the solution
278  exporter->setPostDir ( dataFile ( "exporter/folder", "./" ) );
279 
280  exporter->setMeshProcId ( meshPtr, Members->comm->MyPID() );
281  }
282  }
283 
284  const std::string fieldName = dataFile ( "exporter/name_field", "Field" );
285 
286  // Add the first scalar field to the exporter
287  exporter->addVariable ( ExporterData< regionMesh_Type >::ScalarField,
288  fieldName + "1",
289  scalarField1_FESpace,
290  scalarField1->getVectorPtr(),
291  static_cast<UInt> ( 0 ),
292  ExporterData< regionMesh_Type >::UnsteadyRegime,
293  ExporterData< regionMesh_Type >::Cell );
294 
295  // Add the second scalar field to the exporter
296  exporter->addVariable ( ExporterData< regionMesh_Type >::ScalarField,
297  fieldName + "2",
298  scalarField2_FESpace,
299  scalarField2->getVectorPtr(),
300  static_cast<UInt> ( 0 ),
301  ExporterData< regionMesh_Type >::UnsteadyRegime );
302 
303  // Add the vector field to the exporter
304  exporter->addVariable ( ExporterData< regionMesh_Type >::VectorField,
305  fieldName + "3",
306  vectorField_FESpace,
307  vectorField->getVectorPtr(),
308  static_cast<UInt> ( 0 ),
309  ExporterData< regionMesh_Type >::UnsteadyRegime,
310  ExporterData< regionMesh_Type >::Cell );
311 
312  const std::string fctName = dataFile ( "exporter/name_fct", "Function" );
313 
314  // Add the scalar field representing the function to the exporter
315  exporter->addVariable ( ExporterData< regionMesh_Type >::ScalarField,
316  fctName + "1",
317  function_FESpace,
318  scalarFieldFunction->getVectorPtr(),
319  static_cast<UInt> ( 0 ),
320  ExporterData< regionMesh_Type >::UnsteadyRegime,
321  ExporterData< regionMesh_Type >::Cell );
322 
323  // Export all the solutions
324  exporter->postProcess (0);
325 
326  // Return the error
327  return error;
328 
329 } // run
std::shared_ptr< FEVectorField_Type > FEVectorFieldPtr_Type
GetPot(const int argc_, char **argv_, const char *FieldSeparator=0x0)
Definition: GetPot.hpp:507
Includes.
Definition: fefct.hpp:62
std::shared_ptr< FESpace_Type > FESpacePtr_Type
RegionMesh< geoElement_Type > regionMesh_Type
std::shared_ptr< FEScalarField_Type > FEScalarFieldPtr_Type
std::string data_file_name
Definition: fefct.cpp:64
std::shared_ptr< std::vector< Int > > M_isOnProc
MeshData - class for handling spatial discretization.
Definition: MeshData.hpp:72
LifeV::Real run()
Methods.
Definition: fefct.cpp:102
std::string discretization_section
Definition: fefct.cpp:65
std::shared_ptr< regionMesh_Type > regionMeshPtr_Type
double Real
Generic real data.
Definition: LifeV.hpp:175
Private Members.
Definition: fefct.cpp:60
VectorSmall< 3 > Vector3D
std::shared_ptr< exporter_Type > exporterPtr_Type
std::shared_ptr< Epetra_Comm > comm
Definition: fefct.cpp:67
fefct(int argc, char **argv)
Constructors.
Definition: fefct.cpp:75
Displayer - This class is used to display messages in parallel simulations.
Definition: Displayer.hpp:62
uint32_type UInt
generic unsigned integer (used mainly for addressing)
Definition: LifeV.hpp:191