G+Smo  25.01.0
Geometry + Simulation Modules
 
Loading...
Searching...
No Matches
gsParaviewCollection.h
Go to the documentation of this file.
1
14#pragma once
15
17#include <gsIO/gsFileManager.h>
19
20
21
22#include<fstream>
23
24namespace gismo {
76class GISMO_EXPORT gsParaviewCollection
77{
78public:
79 typedef std::string String;
80public:
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
95 if ( !gsFileManager::isFullyQualified(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
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
225private:
227 std::stringstream mfile;
228
230 std::string m_filename;
231
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
245private:
246 // Construction without a filename is not allowed
248};
249
250//=================================================================================================
251
252
253
258inline void makeCollection(std::string const & fn, std::string const & ext, int n = 0)
259{
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
Generic evaluator of isogeometric expressions.
Definition gsExprEvaluator.h:39
Class which holds a list of parameters/options, and provides easy access to them.
Definition gsOptionList.h:33
This class is used to create a Paraview .pvd (collection) file.
Definition gsParaviewCollection.h:77
void addFields(Rest... rest)
All arguments are forwarded to gsParaviewDataSet::addFields().
Definition gsParaviewCollection.h:189
bool m_isSaved
Flag for checking if collection is already saved.
Definition gsParaviewCollection.h:233
gsParaviewCollection(String const &fn, gsExprEvaluator<> *evaluator=nullptr)
Constructor using a filename and an (optional) evaluator.
Definition gsParaviewCollection.h:83
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
void saveTimeStep()
The current timestep is saved and files written to disk.
Definition gsParaviewCollection.h:196
void save()
Definition gsParaviewCollection.h:203
void addField(Rest... rest)
All arguments are forwarded to gsParaviewDataSet::addField().
Definition gsParaviewCollection.h:181
std::string m_filename
File name.
Definition gsParaviewCollection.h:230
std::stringstream mfile
Pointer to char stream.
Definition gsParaviewCollection.h:227
gsOptionList & options()
Accessor to the current options.
Definition gsParaviewCollection.h:223
This class represents a group of vtk (Paraview) files that refer to one multiPatch,...
Definition gsParaviewDataSet.h:38
#define index_t
Definition gsConfig.h:32
#define gsDebug
Definition gsDebug.h:61
#define GISMO_ERROR(message)
Definition gsDebug.h:118
#define GISMO_ENSURE(cond, message)
Definition gsDebug.h:102
#define GISMO_ASSERT(cond, message)
Definition gsDebug.h:89
Utility class for finding files and handling paths.
Provides forward declarations of types and structs.
Provides a helper class to write Paraview (.vts) files.
The G+Smo namespace, containing all definitions for the library.
void makeCollection(std::string const &fn, std::string const &ext, int n=0)
Definition gsParaviewCollection.h:258