LifeV
ExporterEnsight.hpp
Go to the documentation of this file.
1 //@HEADER
2 /*
3 ************************************************************************
4 
5  This file is part of the LifeV Applications.
6  Copyright (C) 2009-2010 EPFL, Politecnico di Milano
7 
8  This library is free software; you can redistribute it and/or modify
9  it under the terms of the GNU Lesser General Public License as
10  published by the Free Software Foundation; either version 2.1 of the
11  License, or (at your option) any later version.
12 
13  This library is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public
19  License along with this library; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21  USA
22 
23 ************************************************************************
24 */
25 //@HEADER
26 
27 /*!
28  * @file
29  * @brief This file provides an interface for post-processing with ensight
30  *
31  * @author M.A. Fernandez
32  * @author C. Prud'homme
33  * @author S. Deparis
34  * @date 1-10-2005
35  *
36  * @contributor Tiziano Passerini <tiziano@mathcs.emory.edu>
37  * @maintainer Radu Popescu <radu.popescu@epfl.ch>
38  */
39 
40 #ifndef EXPORTER_ENSIGHT_H
41 #define EXPORTER_ENSIGHT_H
42 
43 #include <lifev/core/filter/Exporter.hpp>
44 
45 namespace LifeV
46 {
47 
48 const UInt ensightOffset = 1; //the offset of the IDs in ensight files
49 
50 /**
51  * @class ExporterEnsight
52  * @brief ExporterEnsight data exporter
53  */
54 template<typename MeshType>
55 class ExporterEnsight : public Exporter<MeshType>
56 {
57 
58 public:
59  //! @name Public typedefs
60  //@{
61  typedef MeshType mesh_Type;
62  typedef Exporter<mesh_Type> super;
63  typedef typename super::meshPtr_Type meshPtr_Type;
64  typedef typename super::commPtr_Type commPtr_Type;
65  typedef typename super::vectorPtr_Type vectorPtr_Type;
67  //@}
68 
69  //! @name Static members
70 
71  //! returns the type of the map to use for the VectorEpetra
72  static MapEpetraType const MapType;
73  //@}
74 
75  //! @name Constructors and destructor
76  //@{
77  //! Default constructor
79 
80 
81  //! Constructor for ExporterEnsight
82  /*!
83  @param dfile the GetPot data file where you must provide and [ensight] section with:
84  "start" (start index for filenames 0 for 000, 1 for 001 etc.),
85  "save" (how many time steps per posptrocessing)
86  "multimesh" (=true if the mesh has to be saved at each post-processing step)
87 
88  @param mesh the mesh
89 
90  @param the prefix for the case file (ex. "test" for test.case)
91 
92  @param the procId determines de CPU id. if negative, it ussemes there is only one processor
93  */
94  ExporterEnsight (const GetPot& dfile, meshPtr_Type mesh, const std::string& prefix, const Int& procId );
95 
96  //! Constructor for ExporterEnsight
97  ExporterEnsight (const GetPot& dfile, const std::string& prefix);
98  //@}
99 
100  //! @name Public methods
101  //@{
102 
103  //! Post-process the variables added to the list
104  /*!
105  @param time the solver time
106  */
107  void postProcess (const Real& time);
108 
109  //! Import data from previous simulations at a certain time
110  /*!
111  @param Time the time of the data to be imported
112 
113  Not yet implemented for ExporterEnsight
114  */
115  UInt importFromTime ( const Real& /*time*/ )
116  {
117  ERROR_MSG ( "ExporterEnsight::importFromTime has not yet been implemented.");
118  return -1;
119  }
120 
121  //! Import data from previous simulations and rebuild the internal time counters
122  /*!
123  @param importTime the time of the snapshot to be imported
124  @param dt the time step, is used to rebuild the history up to now
125  */
126  void import (const Real& importTime, const Real& dt);
127 
128  //! Import data from previous simulations
129  /*!
130  @param importTime the time of the snapshot to be imported
131  */
132  void import (const Real& importTime);
133 
134  //! Read variable
135  /*!
136  @param dvar the ExporterData object
137  */
139  {
140  super::readVariable (dvar);
141  }
142 
143  //! Set the mesh and the processor id
144  /*!
145  @param mesh a pointer to the mesh
146  @param procId the ID of the current process
147  */
148  void setMeshProcId ( const meshPtr_Type mesh, const Int& procId );
149 
150  //! temporary: the method should work form the Exporter class
151  void exportPID ( meshPtr_Type /*meshPart*/, commPtr_Type comm, const bool /*binaryFormat*/ = false )
152  {
153  if ( !comm->MyPID() )
154  {
155  std::cerr << " X- exportPID is not working with Ensight" << std::endl;
156  }
157  }
158  //@}
159 
160  //! @name Get methods
161  //@{
162 
163  //! returns the type of the map to use for the VectorEpetra
165  {
166  return MapType;
167  }
168 
169  //@}
170 
171 private:
172  //! @name Private methods
173  //@{
174  //! Compose the .case file
175  /*!
176  @param Time the time of the snapshot
177  */
178  void writeCase (const Real& time);
179  //! Compose the .geo file
180  /*!
181  @param geoFile the name of the file to be produced
182  */
183  void writeAsciiGeometry ( const std::string geoFile );
184  //! The ASCII writer
185  /*!
186  @param dvar the ExporterData object
187  */
188  void writeAscii ( const exporterData_Type& dvar);
189  //! The ASCII writer
190  /*!
191  @param dvar the ExporterData object
192  @param suffix the file suffix (.scl or .vct)
193  */
194  void writeAsciiValues (const exporterData_Type& dvar, const std::string& suffix);
195  //! Compose the "mesh" section of the .case file
196  /*!
197  @param casef the file object
198 
199  Specify whether or not the mesh is changing for each snapshot
200  */
201  void caseMeshSection (std::ofstream& casef);
202  //! Compose the "variable" section of the .case file
203  /*!
204  @param casef the file object
205 
206  The file name will be different based on the regime and the kind of
207  field (scalar / vector)
208  */
209  void caseVariableSection (std::ofstream& casef);
210  //! Compose the "time" section of the .case file
211  /*!
212  @param casef the file object
213  @param time the current time
214 
215  The file will contain the updated number of time steps and the complete
216  list of time values
217  */
218  void caseTimeSection (std::ofstream& casef, const Real& time);
219  //! Dump on file the IDs of the global DOFs associated to this process
220  /*!
221  @param filename the file name
222  */
223  void writeGlobalIDs (const std::string& filename);
224  //! The ASCII reader
225  /*!
226  @param dvar the ExporterData object
227  */
228  void readAscii ( exporterData_Type& dvar );
229  //! The ASCII reader
230  /*!
231  @param dvar the ExporterData object
232  @param suffix the file suffix (.scl or .vct)
233  */
234  void readAsciiValues ( exporterData_Type& dvar, const std::string& suffix );
235  //! Read from file and store in a vector a list of global IDs
236  /*!
237  @param filename the file name
238  @param globalDOF the stored list of IDs
239  */
240  void readGlobalIDs ( const std::string& filename,
241  std::vector<Real>& globalDOF );
242  //! The generic reader (specialization of the parent class method)
243  /*!
244  @param dvar the ExporterData object
245  */
247  {
248  readAsciiValues (dvar, ".scl");
249  }
250  //! The generic reader (specialization of the parent class method)
251  /*!
252  @param dvar the ExporterData object
253  */
255  {
256  readAsciiValues (dvar, ".vct");
257  }
258  //! initialize the internal data structures storing the ID of the current process
259  void initProcId();
260  //! Set the local-to-global map of DOFs
261  /*!
262  @param ltGNodesMap the local-to-global map
263 
264  ltGNodesMap[i] is the global ID of the i-th DOF owned by the current process
265  */
266  void setNodesMap ( std::vector<Int> ltGNodesMap )
267  {
268  M_ltGNodesMap = ltGNodesMap;
269  }
270  //! Build the local-to-global map of DOFs
271  /*!
272  @param ltGNodesMap the local-to-global map
273 
274  ltGNodesMap[i] is the global ID of the i-th DOF owned by the current process
275  */
276  void initNodesMap();
277  //@}
278 
279  //! @name Private members
280  //@{
281  //! the counter of the number of steps processed
283  //! the local-to-global map: ltGNodesMap[i] is the global ID of the i-th DOF
284  //! owned by the current process
286  //! the ID of the current process
287  std::string M_me;
288  //! a string label for the FE space
289  std::string M_FEstr;
290  //! a string label for the boundary FE space
291  std::string M_bdFEstr;
292  //! the number of local DOFs (per element)
294  //! the number of local boundary DOFs (per element)
296  //! are we performing the first post-processing operation?
298  //@}
299 };
300 
301 
302 // ==============
303 // Implementation
304 // ==============
305 
306 template<typename MeshType>
307 MapEpetraType const ExporterEnsight<MeshType>::MapType (Repeated);
308 
309 // ==============
310 // Constructors
311 // ==============
312 
313 template<typename MeshType>
315  super(),
316  M_steps (0),
317  M_ltGNodesMap(),
318  M_me(),
319  M_firstTimeStep (true)
320 {
321 }
322 
323 template<typename MeshType>
324 ExporterEnsight<MeshType>::ExporterEnsight (const GetPot& dfile, meshPtr_Type mesh, const std::string& prefix,
325  const Int& procId)
326  :
327  super (dfile, prefix),
328  M_steps (0),
329  M_ltGNodesMap(),
330  M_me(),
331  M_firstTimeStep (true)
332 {
333  this->setDataFromGetPot (dfile, "exporter");
334  this->setMeshProcId (mesh, procId);
335 }
336 
337 template<typename MeshType>
338 ExporterEnsight<MeshType>::ExporterEnsight (const GetPot& dfile, const std::string& prefix) :
339  super (dfile, prefix),
340  M_steps (0),
341  M_ltGNodesMap(),
342  M_me(),
343  M_firstTimeStep (true)
344 {
345  this->setDataFromGetPot (dfile, "exporter");
346 }
347 
348 // =====================
349 // Public methods
350 // =====================
351 
352 template<typename MeshType>
353 void ExporterEnsight<MeshType>::postProcess (const Real& time)
354 {
355  // writing the geo file and the list of global IDs, but only upon the first instance
356  if ( M_firstTimeStep )
357  {
358  if (!this->M_multimesh)
359  {
360  writeAsciiGeometry ( this->M_postDir + this->M_prefix + this->M_me + ".geo" );
361  }
362 
363  writeGlobalIDs ( this->M_postDir + super::M_prefix + "_globalIDs" +
364  this->M_me + ".scl" );
365 
366  M_firstTimeStep = false;
367  }
368  // prepare the file postfix
369  this->computePostfix();
370 
371  // the postfix will be full of stars, if this time step is not going to generate a snapshot
372  std::size_t found ( this->M_postfix.find ( "*" ) );
373  if ( found == std::string::npos )
374  {
375  if (!this->M_procId)
376  {
377  std::cout << " X- ExporterEnsight post-processing ... " << std::flush;
378  }
379  LifeChrono chrono;
380  chrono.start();
381  for (typename super::dataVectorIterator_Type i = this->M_dataVector.begin();
382  i != this->M_dataVector.end(); ++i)
383  {
384  // the "regime" attribute needs to be valid
385  if ( i->regime() != exporterData_Type::NullRegime )
386  {
387  writeAscii (*i);
388  }
389  // if the solution is steady, we do not need to export it at each time step
390  if (i->regime() == exporterData_Type::SteadyRegime)
391  {
392  i->setRegime ( exporterData_Type::NullRegime );
393  }
394  }
395  // write an updated case file
396  writeCase (time);
397 
398  // write an updated geo file, if needed
399  if (this->M_multimesh)
400  {
401  writeAsciiGeometry ( this->M_postDir + this->M_prefix + this->M_postfix + this->M_me + ".geo" );
402  }
403  chrono.stop();
404  if (!this->M_procId)
405  {
406  std::cout << " done in " << chrono.diff() << " s." << std::endl;
407  }
408  }
409 
410 }
411 
412 template<typename MeshType>
413 void ExporterEnsight<MeshType>::import (const Real& startTime, const Real& dt)
414 {
415  // dt is used to rebuild the history up to now
416  Real time (startTime - this->M_timeIndex * dt);
417 
418  for ( UInt count (0); count < this->M_timeIndex; ++count)
419  {
420  this->M_timeSteps.push_back (time);
421  ++this->M_steps;
422  time += dt;
423  }
424 
425  time += dt;
426 
427  import (time);
428 
429 }
430 
431 template<typename MeshType>
432 void ExporterEnsight<MeshType>::import (const Real& time)
433 {
434  this->M_timeSteps.push_back (time);
435  ++this->M_steps;
436 
437  // typedef std::list< exporterData_Type >::iterator Iterator;
438 
439  this->computePostfix();
440 
441  assert ( this->M_postfix.find ( "*" ) == std::string::npos );
442 
443  if (!this->M_procId)
444  {
445  std::cout << " X- ExporterEnsight importing ..." << std::endl;
446  }
447 
448  LifeChrono chrono;
449  chrono.start();
450  for (typename super::dataVectorIterator_Type i = this->M_dataVector.begin(); i != this->M_dataVector.end(); ++i)
451  {
452  this->readVariable (*i);
453  }
454  chrono.stop();
455  if (!this->M_procId)
456  {
457  std::cout << " done in " << chrono.diff() << " s." << std::endl;
458  }
459 
460 }
461 
462 template<typename MeshType>
463 void ExporterEnsight<MeshType>::setMeshProcId ( const meshPtr_Type mesh, const Int& procId )
464 {
465  super::setMeshProcId ( mesh, procId );
466 
468  initProcId();
469 
470  typedef typename MeshType::elementShape_Type elementShape_Type;
471 
472  switch ( elementShape_Type::S_shape )
473  {
474  case TETRA:
475  M_FEstr = "tetra4";
476  M_bdFEstr = "tria3";
477  M_nbLocalBdDof = 3;
478  M_nbLocalDof = 4;
479  break;
480  case HEXA:
481  M_FEstr = "hexa8";
482  M_bdFEstr = "quad4";
483  M_nbLocalBdDof = 4;
484  M_nbLocalDof = 8;
485  break;
486  case TRIANGLE:
487  M_FEstr = "tria3";
488  M_bdFEstr = "bar2";
489  M_nbLocalBdDof = 2;
490  M_nbLocalDof = 3;
491  break;
492  case QUAD:
493  M_FEstr = "quad4";
494  M_bdFEstr = "bar2";
495  M_nbLocalBdDof = 4;
496  M_nbLocalDof = 3;
497  break;
498  default:
499  ERROR_MSG ( "FE not allowed in ExporterEnsight writer" );
500  break;
501  }
502 
503 }
504 
505 // ===================
506 // Get methods
507 // ===================
508 
509 // ===================
510 // Private methods
511 // ===================
512 
513 template <typename MeshType>
514 void ExporterEnsight<MeshType>::writeCase (const Real& time)
515 {
516  std::string filename ( this->M_postDir + this->M_prefix + this->M_me + ".case" );
517  std::ofstream casef ( filename.c_str() );
518  ASSERT (casef.is_open(), "There is an error while opening " + filename );
519  ASSERT (casef.good(), "There is an error while writing to " + filename );
520  casef << "FORMAT\ntype: ensight\n";
521  caseMeshSection (casef);
522  caseVariableSection (casef);
523  caseTimeSection (casef, time);
524  casef.close();
525 }
526 
527 template <typename MeshType>
528 void ExporterEnsight<MeshType>::writeAsciiGeometry (const std::string gFile)
529 {
530  using std::setw;
531 
532  std::ofstream geoFile (gFile.c_str() );
533  ASSERT (geoFile.is_open(), "There is an error while opening " + gFile );
534  ID vertexNumber = this->M_mesh->numVertices();
535  ID elementNumber = this->M_mesh->numElements();
536  UInt part = 0;
537  ASSERT (geoFile.good(), "There is an error while writing to " + gFile );
538  geoFile << "Geometry file\nGenerated by LifeV\nnode id given\nelement id given\ncoordinates\n";
539  geoFile.setf (std::ios::right | std::ios_base::scientific);
540  geoFile.precision (5);
541  ASSERT (geoFile.good(), "There is an error while writing to " + gFile );
542  geoFile << setw (8) << vertexNumber << "\n";
543  for (ID i = 0; i < vertexNumber; ++i)
544  {
545  ASSERT (geoFile.good(), "There is an error while writing to " + gFile );
546  geoFile << setw (8) << i + ensightOffset;
547  for (UInt icoor = 0; icoor < nDimensions; icoor++)
548  {
549  ASSERT (geoFile.good(), "There is an error while writing to " + gFile );
550  geoFile << setw (12) << static_cast<float> (this->M_mesh->pointList (i).coordinatesArray() [icoor]);
551  }
552  ASSERT (geoFile.good(), "There is an error while opening " + gFile );
553  geoFile << "\n";
554  }
555 
556  ASSERT (geoFile.good(), "There is an error while writing to " + gFile );
557  ++part;
558  geoFile << "part" << setw (8) << part << "\nfull geometry\n"
559  // elements
560  << M_FEstr << "\n" << setw (8) << elementNumber << "\n";
561  for (ID i = 0; i < elementNumber; ++i)
562  {
563  ASSERT (geoFile.good(), "There is an error while writing to " + gFile );
564  geoFile << setw (8) << i + ensightOffset;
565  for (ID j = 0; j < M_nbLocalDof; ++j)
566  {
567  ASSERT (geoFile.good(), "There is an error while writing to " + gFile );
568  geoFile << setw (8) << this->M_mesh->element (i).point (j).localId() + ensightOffset;
569  }
570  ASSERT (geoFile.good(), "There is an error while writing to " + gFile );
571  geoFile << "\n";
572 
573  }
574  geoFile.close();
575 }
576 
577 template <typename MeshType>
578 void ExporterEnsight<MeshType>::writeAscii (const exporterData_Type& dvar)
579 {
580 
581  switch ( dvar.fieldType() )
582  {
583  case exporterData_Type::ScalarField:
584  writeAsciiValues (dvar, ".scl");
585  break;
586  case exporterData_Type::VectorField:
587  writeAsciiValues (dvar, ".vct");
588  break;
589  default:
590  ERROR_MSG ( "Unknown field type" )
591  break;
592  }
593 
594 }
595 
596 template <typename MeshType>
597 void ExporterEnsight<MeshType>::writeAsciiValues (const exporterData_Type& dvar, const std::string& suffix)
598 {
599  using std::setw;
600 
601  std::ofstream exportFile;
602  std::string filename;
603 
604  if ( dvar.regime() == exporterData_Type::SteadyRegime )
605  filename = this->M_postDir + super::M_prefix + "_" + dvar.variableName() +
606  this->M_me + suffix;
607  else
608  filename = this->M_postDir + super::M_prefix + "_" + dvar.variableName() +
609  this->M_postfix + this->M_me + suffix;
610 
611  exportFile.open ( filename.c_str() );
612 
613  ASSERT (exportFile.is_open(), "There is an error while opening " + filename );
614 
615  UInt count = 0;
616 
617  const UInt size = dvar.numDOF();
618  const UInt start = dvar.start();
619  const UInt vertexNumber = static_cast<UInt> (this->M_ltGNodesMap.size() );
620 
621  ASSERT (exportFile.good(), "There is an error while writing to " + filename );
622  if ( suffix.compare (".vct") == 0 )
623  {
624  exportFile << "Vector per node\n";
625  }
626  else if ( suffix.compare (".scl") == 0 )
627  {
628  exportFile << "Scalar per node\n";
629  }
630 
631  exportFile.setf (std::ios::right | std::ios_base::scientific);
632  exportFile.precision (5);
633 
634  for (UInt i = 0; i < vertexNumber; ++i)
635  for (UInt j = 0; j < dvar.fieldDim(); ++j)
636  {
637  const Int id = this->M_ltGNodesMap[i];
638  ASSERT (exportFile.good(), "There is an error while writing to " + filename );
639  exportFile << setw (12) << static_cast<float> (dvar (start + j * size + id) ) ;
640  ++count;
641  if ( count == 6 )
642  {
643  ASSERT (exportFile.good(), "There is an error while writing to " + filename );
644  exportFile << "\n";
645  count = 0;
646  }
647  }
648  ASSERT (exportFile.good(), "There is an error while writing to " + filename );
649  exportFile << std::endl;
650 
651  exportFile.close();
652 }
653 
654 template <typename MeshType>
655 void ExporterEnsight<MeshType>::writeGlobalIDs (const std::string& filename)
656 {
657  using std::setw;
658 
659  std::ofstream globalIDsFile;
660 
661  globalIDsFile.open ( filename.c_str() );
662  ASSERT (globalIDsFile.is_open(), "There is an error while opening " + filename );
663 
664  UInt count = 0;
665 
666  const UInt vertexNumber = static_cast<UInt> (this->M_ltGNodesMap.size() );
667  ASSERT (globalIDsFile.good(), "There is an error while writing to " + filename );
668  globalIDsFile << "Node global ID " << vertexNumber << "\n";
669 
670  for (UInt i = 0; i < vertexNumber; ++i)
671  {
672  const Int id = this->M_ltGNodesMap[i];
673  ASSERT (globalIDsFile.good(), "There is an error while writing to " + filename );
674  globalIDsFile << setw (12) << id ;
675  ++count;
676  if ( count == 6 )
677  {
678  ASSERT (globalIDsFile.good(), "There is an error while writing to " + filename );
679  globalIDsFile << "\n";
680  count = 0;
681  }
682  }
683  ASSERT (globalIDsFile.good(), "There is an error while writing to " + filename );
684  globalIDsFile << std::endl;
685  globalIDsFile.close();
686 }
687 
688 template <typename MeshType>
689 void ExporterEnsight<MeshType>::caseMeshSection (std::ofstream& casef)
690 {
691  ASSERT (casef.good(), "There is an error while writing to file" );
692  casef << "GEOMETRY\n";
693  if ( this->M_multimesh )
694  {
695  std::string stars (".");
696  for (UInt cc (0); cc < this->M_timeIndexWidth; ++cc)
697  {
698  stars += "*";
699  }
700 
701  ASSERT (casef.good(), "There is an error while writing to file" );
702  casef << "model: 1 " + this->M_prefix + stars << this->M_me << ".geo change_coords_only\n";
703  }
704  else
705  {
706  ASSERT (casef.good(), "There is an error while writing to file" );
707  casef << "model: 1 " + this->M_prefix + this->M_me + ".geo\n";
708  }
709 }
710 
711 template <typename MeshType>
712 void ExporterEnsight<MeshType>::caseVariableSection (std::ofstream& casef)
713 {
714  ASSERT (casef.good(), "There is an error while writing to file" );
715  casef << "VARIABLE\n";
716  std::string aux, str;
717  for (typename super::dataVectorIterator_Type i = this->M_dataVector.begin(); i != this->M_dataVector.end(); ++i)
718  {
719  if ( i->regime() == exporterData_Type::SteadyRegime )
720  {
721  str = "";
722  }
723  else
724  {
725  std::string stars (".");
726  for (UInt cc (0); cc < this->M_timeIndexWidth; ++cc)
727  {
728  stars += "*";
729  }
730 
731  str = stars;
732  }
733  aux = i->variableName() + " " + super::M_prefix + "_" + i->variableName();
734  switch ( i->fieldType() )
735  {
736  case exporterData_Type::ScalarField:
737  ASSERT (casef.good(), "There is an error while writing to file" );
738  casef << "scalar per node: 1 " + aux + str << this->M_me << ".scl\n";
739  break;
740  case exporterData_Type::VectorField:
741  ASSERT (casef.good(), "There is an error while writing to file" );
742  casef << "vector per node: 1 " + aux + str << this->M_me << ".vct\n";
743  break;
744  }
745  }
746 }
747 
748 template <typename MeshType>
749 void ExporterEnsight<MeshType>::caseTimeSection (std::ofstream& casef, const Real& time)
750 {
751  ASSERT (casef.good(), "There is an error while writing to file" );
752  this->M_timeSteps.push_back (time);
753  ++this->M_steps;
754  casef << "TIME\ntime set: 1\nnumber of steps: " << this->M_steps << "\n"
755  << "filename start number: " << this->M_timeIndexStart << "\n"
756  << "filename increment: 1\ntime values:\n";
757 
758  UInt count = 0;
759 
760  typedef std::list<Real>::const_iterator Iterator;
761  for (Iterator i = this->M_timeSteps.begin(); i != this->M_timeSteps.end(); ++i)
762  {
763  ASSERT (casef.good(), "There is an error while writing to file" );
764  casef << *i << " " ;
765  ++count;
766  if ( count == 6)
767  {
768  ASSERT (casef.good(), "There is an error while writing to file" );
769  casef << "\n";
770  count = 0;
771  }
772  }
773 }
774 
775 template <typename MeshType>
777 {
778 
779  switch ( dvar.fieldType() )
780  {
781  case exporterData_Type::ScalarField:
782  writeAsciiValues (dvar, ".scl");
783  break;
784  case exporterData_Type::VectorField:
785  writeAsciiValues (dvar, ".vct");
786  break;
787  default:
788  ERROR_MSG ("Unsupported field type!");
789  break;
790  }
791 
792 }
793 
794 template <typename MeshType> void ExporterEnsight<MeshType>::readAsciiValues (exporterData_Type& dvar,
795  const std::string& suffix)
796 {
797  ASSERT ( this->M_numImportProc, "The number of pieces to be loaded was not specified." );
798 
799  // this vector lists the global IDs of DOFs listed in each piece
800  std::vector<Real> globalDOF;
801 
802  // Each processor will read all the files, and fill just its own component of the vectors
803  for ( UInt iProc = 0; iProc < this->M_numImportProc; ++iProc )
804  {
805  // build the postfix for the file corresponding to part iProc
806  std::ostringstream index;
807  index.fill ( '0' );
808  index << std::setw (1) << "." ;
809  index << std::setw (3) << iProc;
810 
811  // fill globalDOF, the list of DOF IDs on which we are going to operate
812  readGlobalIDs ( this->M_postDir + super::M_prefix + "_globalIDs" + index.str() + ".scl", globalDOF );
813 
814  // open the file with the vector field to be imported
815  const std::string filename ( this->M_postDir + super::M_prefix + "_" + dvar.variableName() +
816  this->M_postfix + index.str() + suffix );
817  std::ifstream importFile ( filename.c_str() );
818 
819  // debugging step
820  if (!this->M_procId)
821  {
822  std::cout << "\tfile " << filename << std::endl;
823  }
824 
825  ASSERT (importFile.is_open(), "There is an error while reading " + filename );
826  // discard the header of the file
827  std::string line;
828  ASSERT (importFile.good(), "There is an error while reading from " + filename );
829  getline ( importFile, line ); // the line looks like this: "Vector/Scalar per node"
830 
831  // parameters to access ExporterData structures
832  const UInt size = dvar.numDOF();
833  const UInt start = dvar.start();
834 
835  // loop over the DOFs listed in the current piece
836  for (UInt i = 0; i < globalDOF.size(); ++i)
837  {
838  // extract the global ID of each DOF
839  const Int id = globalDOF[i];
840  // we are working with vectorial fields
841  for (UInt j = 0; j < dvar.fieldDim(); ++j)
842  {
843  // this is the value of the field, to be imported
844  Real value (0);
845 
846  ASSERT (importFile.good(), "There is an error while reading from " + filename );
847  importFile >> value;
848 
849  // do the actual import only if the global ID belongs to the current process
850  if ( dvar.feSpacePtr()->map().map (Repeated)->MyGID ( id ) )
851  {
852  dvar (start + j * size + id) = value;
853  }
854  }
855  }
856 
857  ASSERT (!importFile.fail(), "There is an error while reading " + filename );
858  importFile.close();
859  }
860 }
861 
862 template<typename MeshType>
863 void ExporterEnsight<MeshType>::initProcId()
864 {
865  std::ostringstream index;
866  index.fill ( '0' );
867  if (this->M_procId >= 0)
868  {
869  index << std::setw (1) << "." ;
870  index << std::setw (3) << this->M_procId;
871  }
872  M_me = index.str();
873 }
874 
875 template<typename MeshType>
876 void ExporterEnsight<MeshType>::initNodesMap()
877 {
878  UInt vertexNumber = this->M_mesh->numVertices();
879  M_ltGNodesMap.resize (vertexNumber);
880  for (UInt i = 0; i < vertexNumber; ++i)
881  {
882  M_ltGNodesMap[i] = this->M_mesh->pointList ( i ).id();
883  }
884 }
885 
886 template<typename MeshType>
887 void ExporterEnsight<MeshType>::readGlobalIDs ( const std::string& filename,
888  std::vector<Real>& globalDOF )
889 {
890  std::ifstream globalIDsFile ( filename.c_str() );
891 
892  if (!this->M_procId)
893  {
894  std::cout << "\tfile " << filename << std::endl;
895  }
896 
897  ASSERT (globalIDsFile.is_open(), "There is an error while opening " + filename );
898 
899  UInt vertexNumber;
900  globalDOF.resize (0);
901 
902  // file parsing: line by line
903  std::string line;
904  std::stringstream parseLine;
905 
906  ASSERT (globalIDsFile.good(), "There is an error while reading " + filename );
907  getline ( globalIDsFile, line ); // the line looks like this: "Node global ID N"
908  parseLine.str (line);
909  std::string trashcan;
910  parseLine >> trashcan >> trashcan >> trashcan >> vertexNumber;
911 
912  for ( UInt iNode = 0; iNode < vertexNumber; ++iNode )
913  {
914  Real gID (0);
915  ASSERT (globalIDsFile.good(), "There is an error while reading " + filename );
916  globalIDsFile >> gID;
917  globalDOF.push_back ( gID );
918  }
919 
920  ASSERT (!globalIDsFile.fail(), "There is an error while reading " + filename );
921  globalIDsFile.close();
922 }
923 
924 } // Namespace LifeV
925 
926 #endif // EXPORTER_ENSIGHT_H
const UInt ensightOffset
UInt importFromTime(const Real &)
Import data from previous simulations at a certain time.
super::exporterData_Type exporterData_Type
void postProcess(const Real &time)
Post-process the variables added to the list.
bool M_firstTimeStep
are we performing the first post-processing operation?
void import(const Real &importTime, const Real &dt)
Import data from previous simulations and rebuild the internal time counters.
std::vector< Int > M_ltGNodesMap
the local-to-global map: ltGNodesMap[i] is the global ID of the i-th DOF owned by the current process...
void start()
Start the timer.
Definition: LifeChrono.hpp:93
ExporterEnsight data exporter.
ExporterEnsight()
Default constructor.
ExporterEnsight(const GetPot &dfile, const std::string &prefix)
Constructor for ExporterEnsight.
void readVector(exporterData_Type &dvar)
The generic reader (specialization of the parent class method)
void writeAsciiGeometry(const std::string geoFile)
Compose the .geo file.
void setNodesMap(std::vector< Int > ltGNodesMap)
Set the local-to-global map of DOFs.
int32_type Int
Generic integer data.
Definition: LifeV.hpp:188
void writeAsciiValues(const exporterData_Type &dvar, const std::string &suffix)
The ASCII writer.
void exportPID(meshPtr_Type, commPtr_Type comm, const bool=false)
temporary: the method should work form the Exporter class
super::vectorPtr_Type vectorPtr_Type
UInt M_steps
the counter of the number of steps processed
#define ERROR_MSG(A)
Definition: LifeAssert.hpp:69
void setMeshProcId(const meshPtr_Type mesh, const Int &procId)
Set the mesh and the processor id.
MapEpetraType mapType() const
returns the type of the map to use for the VectorEpetra
void readGlobalIDs(const std::string &filename, std::vector< Real > &globalDOF)
Read from file and store in a vector a list of global IDs.
#define ASSERT(X, A)
Definition: LifeAssert.hpp:90
uint32_type ID
IDs.
Definition: LifeV.hpp:194
std::string M_FEstr
a string label for the FE space
void readAscii(exporterData_Type &dvar)
The ASCII reader.
ExporterEnsight(const GetPot &dfile, meshPtr_Type mesh, const std::string &prefix, const Int &procId)
Constructor for ExporterEnsight.
UInt M_nbLocalDof
the number of local DOFs (per element)
super::meshPtr_Type meshPtr_Type
static MapEpetraType const MapType
returns the type of the map to use for the VectorEpetra
void writeGlobalIDs(const std::string &filename)
Dump on file the IDs of the global DOFs associated to this process.
void initProcId()
initialize the internal data structures storing the ID of the current process
void caseVariableSection(std::ofstream &casef)
Compose the "variable" section of the .case file.
UInt M_nbLocalBdDof
the number of local boundary DOFs (per element)
double Real
Generic real data.
Definition: LifeV.hpp:175
void caseTimeSection(std::ofstream &casef, const Real &time)
Compose the "time" section of the .case file.
void readScalar(exporterData_Type &dvar)
The generic reader (specialization of the parent class method)
const UInt nDimensions(NDIM)
void readAsciiValues(exporterData_Type &dvar, const std::string &suffix)
The ASCII reader.
std::string M_bdFEstr
a string label for the boundary FE space
void stop()
Stop the timer.
Definition: LifeChrono.hpp:100
super::commPtr_Type commPtr_Type
void writeCase(const Real &time)
Compose the .case file.
std::string M_me
the ID of the current process
void readVariable(exporterData_Type &dvar)
Read variable.
Exporter< mesh_Type > super
void caseMeshSection(std::ofstream &casef)
Compose the "mesh" section of the .case file.
void initNodesMap()
Build the local-to-global map of DOFs.
uint32_type UInt
generic unsigned integer (used mainly for addressing)
Definition: LifeV.hpp:191
void import(const Real &importTime)
Import data from previous simulations.
void writeAscii(const exporterData_Type &dvar)
The ASCII writer.