G+Smo  24.08.0
Geometry + Simulation Modules
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gsParaviewCollection.h
Go to the documentation of this file.
1 
14 #pragma once
15 
17 #include <gsIO/gsFileManager.h>
18 #include <gsIO/gsParaviewDataSet.h>
19 
20 
21 
22 #include<fstream>
23 
24 namespace gismo {
76 class GISMO_EXPORT gsParaviewCollection
77 {
78 public:
79  typedef std::string String;
80 public:
81 
83  gsParaviewCollection(String const &fn,
84  gsExprEvaluator<> * evaluator=nullptr)
85  : m_filename(fn),
86  m_isSaved(false),
87  m_time(-1),
88  m_evaluator(evaluator),
89  m_options(gsParaviewDataSet::defaultOptions()),
90  counter(0)
91  {
92  std::string path = gsFileManager::getPath(m_filename);
93 
94  // If the path does not start with ./ or / , it is assumed to be a relative path
96  path = gsFileManager::getCurrentPath() + path;
97 
98  m_filename = path + gsFileManager::getBasename(m_filename) + ".pvd";
99  gsFileManager::mkdir( path );
100 
101  // if ( "" != m_filename.parent_path())
102  // GISMO_ENSURE( fsystem::exists( m_filename.parent_path() ),
103  // "The specified folder " << m_filename.parent_path() << " does not exist, please create it first.");
104  mfile <<"<?xml version=\"1.0\"?>\n";
105  mfile <<"<VTKFile type=\"Collection\" version=\"0.1\">\n";
106  mfile <<"<Collection>\n";
107  }
108 
114  void addPart(String const & fn, real_t tStep=-1, std::string name="", index_t part=-1)
115  {
116  std::string ext = "";
117  if (gsFileManager::getExtension(fn) == "")
118  {
119  if (name=="Mesh" || name=="mesh")
120  ext = ".vtp";
121  else if (name=="Geometry" || name=="geometry" || name=="Solution" || name=="solution")
122  ext = ".vts";
123  else
124  GISMO_ERROR("No extension could be found for file "<<fn<<". Try to add an extension or add name 'Mesh','Solution','Geometry'");
125  }
126 
127  GISMO_ASSERT( !m_isSaved , "Error: collection has been already saved." );
128  mfile << "<DataSet ";
129  if (part != -1) mfile << "part=\""<< part <<"\" ";
130  if (tStep != -1) mfile << "timestep=\""<< tStep <<"\" ";
131  if (name != "") mfile << "name=\"" << name << "\" ";
132  mfile << "file=\"" << fn+ext <<"\"/>\n";
133  }
134  // CAUTION!
135  // The previous 3 versions of gsParaviewCollection::addPart() have been combined into the one above
136  // since they were all doing basiacally the same thing. Below you can see a 'conversion table' that
137  // can help you adapt your code to the new syntax. For questions contact C. Karampatzakis (Github @ckarampa )
138  // OLD SYNTAX ( Deprecated ) | NEW SYNTAX
139  //-----------------------------------------------------------------------------------------------------------
140  // addPart(String const & fn) | addPart( fn, -1, "", counter++); |
141  // |
142  // addPart(String const & fn, String const & ext) | addPart( fn+ext, -1, "", counter++); |
143  // |
144  // addPart(String const & fn, int i, String const & ext) | addPart( fn+std::to_string(i)+ext, -1, "", i); |
145  // ----------------------------------------------------------------------------------------------------------
146 
147  // The following functions are all deprecated, only here for backwards compatibility!
148 
149  GISMO_DEPRECATED void addTimestep(String const & fn, double tstep, String const & ext)
150  {
151  // mfile << "<DataSet timestep=\""<<tstep<<"\" file=\""<<fn<<ext<<"\"/>\n";
152  addPart( fn+ext, tstep);
153  }
154 
155  GISMO_DEPRECATED void addTimestep(String const & fn, int part, double tstep, String const & ext)
156  {
157  // mfile << "<DataSet part=\""<<part<<"\" timestep=\""<<tstep<<"\" file=\""<<fn<<"_"<<part<<ext<<"\"/>\n";
158  addPart( fn+"_"+std::to_string(part)+ext, tstep, "", part);
159  }
160 
161  // End of deprecated functions
162 
163  GISMO_DEPRECATED void addPart(String const & fn, String extension)
164  {
165  addPart( fn+extension);
166  }
167 
171  void addDataSet(gsParaviewDataSet & dataSet, real_t time=-1);
172 
176  void newTimeStep(gsMultiPatch<real_t> * geometry, real_t time=-1);
177 
178 
180  template <typename... Rest>
181  void addField(Rest... rest)
182  {
183  GISMO_ENSURE( !m_dataset.isEmpty(), "The gsParaviewDataSet, stored internally by gsParaviewCollection, is empty! Try running newTimestep() before addField().");
184  m_dataset.addField(rest...);
185  }
186 
188  template <typename... Rest>
189  void addFields(Rest... rest)
190  {
191  GISMO_ENSURE( !m_dataset.isEmpty(), "The gsParaviewDataSet, stored internally by gsParaviewCollection, is empty! Try running newTimestep() before addFields().");
192  m_dataset.addFields(rest...);
193  }
194 
196  void saveTimeStep(){
197  GISMO_ENSURE( !m_dataset.isEmpty(), "The gsParaviewDataSet, stored internally by gsParaviewCollection, is empty! Try running newTimestep() before saveTimeStep().");
198  addDataSet(m_dataset,m_time);
199  };
200 
203  void save()
204  {
205  GISMO_ASSERT(!m_isSaved, "Error: gsParaviewCollection::save() already called." );
206  if (!m_isSaved)
207  {
208  mfile <<"</Collection>\n";
209  mfile <<"</VTKFile>\n";
210 
211  gsDebug << "Exporting to " << m_filename << "\n";
212  std::ofstream f( m_filename.c_str() );
213  GISMO_ASSERT(f.is_open(), "Error creating "<< m_filename );
214  f << mfile.rdbuf();
215  f.close();
216  mfile.str("");
217  m_isSaved=true;
218  counter = -1;
219  }
220  }
221 
223  gsOptionList & options() {return m_options;}
224 
225 private:
227  std::stringstream mfile;
228 
230  std::string m_filename;
231 
233  bool m_isSaved;
234 
235  int m_time;
236 
237  gsExprEvaluator<> * m_evaluator;
238 
239  gsParaviewDataSet m_dataset;
240 
241  gsOptionList m_options;
242 
243  index_t counter;
244 
245 private:
246  // Construction without a filename is not allowed
248 };
249 
250 //=================================================================================================
251 
252 
253 
258 inline void makeCollection(std::string const & fn, std::string const & ext, int n = 0)
259 {
260  gsParaviewCollection pc(fn);
261  if ( n > 0)
262  {
263  for (int i=0; i<n ; i++)
264  pc.addPart(fn + std::to_string(i) + ext);
265  }
266  else
267  pc.addPart(fn + ext);
268 
269  pc.save();
270 }
271 
272 
273 } // end namespace gismo
std::stringstream mfile
Pointer to char stream.
Definition: gsParaviewCollection.h:227
void addFields(Rest...rest)
All arguments are forwarded to gsParaviewDataSet::addFields().
Definition: gsParaviewCollection.h:189
#define gsDebug
Definition: gsDebug.h:61
static std::string getBasename(std::string const &fn)
Returns the base name without path and extension of the filename fn.
Definition: gsFileManager.cpp:579
void addField(Rest...rest)
All arguments are forwarded to gsParaviewDataSet::addField().
Definition: gsParaviewCollection.h:181
static std::string getExtension(std::string const &fn)
Returns the extension of the filename fn.
Definition: gsFileManager.cpp:568
#define index_t
Definition: gsConfig.h:32
#define GISMO_ENSURE(cond, message)
Definition: gsDebug.h:102
#define GISMO_ASSERT(cond, message)
Definition: gsDebug.h:89
std::string to_string(const unsigned &i)
Helper to convert small unsigned to string.
Definition: gsXml.cpp:74
void saveTimeStep()
The current timestep is saved and files written to disk.
Definition: gsParaviewCollection.h:196
void save()
Definition: gsParaviewCollection.h:203
static std::string getCurrentPath()
Get current directory.
Definition: gsFileManager.cpp:401
Provides forward declarations of types and structs.
Generic evaluator of isogeometric expressions.
Definition: gsExprEvaluator.h:38
bool m_isSaved
Flag for checking if collection is already saved.
Definition: gsParaviewCollection.h:233
This class is used to create a Paraview .pvd (collection) file.
Definition: gsParaviewCollection.h:76
void addPart(String const &fn, real_t tStep=-1, std::string name="", index_t part=-1)
Appends a file to the Paraview collection (.pvd file).
Definition: gsParaviewCollection.h:114
static std::string getPath(std::string const &fn, bool resolve=false)
Definition: gsFileManager.cpp:608
This class represents a group of vtk (Paraview) files that refer to one multiPatch, for one timestep.
Definition: gsParaviewDataSet.h:36
std::string m_filename
File name.
Definition: gsParaviewCollection.h:230
static bool isFullyQualified(const std::string &fn)
Checks if the path is fully qualified, also known as &quot;absolute path&quot; Under Unix, if a name starts wit...
Definition: gsFileManager.cpp:152
Utility class for finding files and handling paths.
#define GISMO_ERROR(message)
Definition: gsDebug.h:118
static bool mkdir(std::string fn)
Make directory.
Definition: gsFileManager.cpp:328
Class which holds a list of parameters/options, and provides easy access to them. ...
Definition: gsOptionList.h:32
gsParaviewCollection(String const &fn, gsExprEvaluator<> *evaluator=nullptr)
Constructor using a filename and an (optional) evaluator.
Definition: gsParaviewCollection.h:83
void makeCollection(std::string const &fn, std::string const &ext, int n=0)
Definition: gsParaviewCollection.h:258
gsOptionList & options()
Accessor to the current options.
Definition: gsParaviewCollection.h:223
Provides a helper class to write Paraview (.vts) files.