DUNE-FEM (unstable)

iointerface.hh
1#ifndef DUNE_FEM_IOINTERFACE_HH
2#define DUNE_FEM_IOINTERFACE_HH
3
4//- system includes
5#include <dirent.h>
6#include <iostream>
7#include <iomanip>
8#include <sstream>
9#include <utility>
10#include <sys/stat.h>
11#include <sys/types.h>
12
13
14//- Dune includes
16#include <dune/grid/yaspgrid.hh>
17#include <dune/grid/io/file/dgfparser/dgfparser.hh>
18
19
20// defines Parameter
21#include <dune/fem/io/parameter.hh>
22
23// binary data io
24#include <dune/fem/io/io.hh>
25
26#include <dune/fem/misc/capabilities.hh>
27
28// if grape was configured then include headers
29#if HAVE_GRAPE
30#include <dune/grid/io/visual/grapedatadisplay.hh>
31#endif
32
33namespace Dune
34{
35
36 namespace Fem
37 {
38
39 // generateFilename
40 // ----------------
41
42 inline std::string generateFilename ( const std::string &fn,
43 int ntime,
44 int precision = 6 )
45 {
46 std::ostringstream name;
47 name << fn << std::setw( precision ) << std::setfill( '0' ) << ntime;
48 return name.str();
49 }
50
51
52
53 class TimeProviderBase;
54
157
158 protected:
161
162 public:
164 virtual ~IOInterface () {}
165
169 virtual void writeData ( double sequenceStamp ) const = 0;
170
174 virtual void write( const TimeProviderBase& tp ) const = 0;
175
178 virtual void write() const = 0;
179
181 static std::string defaultGridKey ( int dimension, bool check = true )
182 {
183 return defaultGridKey( dimension, Parameter::container(), check );
184 }
185
186 static std::string defaultGridKey ( int dimension, const ParameterReader &parameter, bool check = true )
187 {
188 return defaultGridKey( "fem.io.macroGridFile", dimension, parameter, check );
189 }
190
191 static std::string defaultGridKey ( std::string base, int dimension, bool check = true )
192 {
193 return defaultGridKey( std::move( base ), dimension, Parameter::container(), check );
194 }
195
197 static std::string defaultGridKey ( std::string base, int dimension, const ParameterReader &parameter, bool check = true )
198 {
199 const std::string oldGridKey( base );
200
201 std::ostringstream gridKeyStream;
202 gridKeyStream << oldGridKey << "_" << dimension << "d";
203 const std::string newGridKey( gridKeyStream.str() );
204
205 // check for old parameter
206 if( parameter.exists( oldGridKey ) )
207 {
208 if( parameter.exists( newGridKey ) )
209 {
210 std::cerr << "WARNING: ignoring `" << oldGridKey << "' because `"
211 << newGridKey << "' was also found in parameter file." << std::endl;
212 return newGridKey;
213 }
214 else
215 {
216 std::cerr << "WARNING: change `" << oldGridKey << "' to `" << newGridKey
217 << "' in parameter file." << std::endl;
218 return oldGridKey;
219 }
220 }
221
222 // check for parameter with dimension
223 if( check && !parameter.exists( newGridKey ) )
224 {
225 std::cerr << "ERROR: Parameter `" << newGridKey << "' not found." << std::endl;
226 DUNE_THROW( ParameterNotFound, "Parameter `" << newGridKey << "' not found." );
227 }
228 return newGridKey;
229 }
230
232 static void createPath ( const std::string &path )
233 {
234 if( !createDirectory( path ) )
235 std::cerr << "Failed to create path `" << path << "'." << std::endl;
236 }
237
239 static std::string createPathName(const std::string& pathPref, int rank )
240 {
241 std::string path(pathPref);
242
243 // add proc number to path
244 {
245 path += "_";
246 std::stringstream rankDummy;
247 rankDummy << rank;
248 path += rankDummy.str();
249 }
250 return path;
251 }
252
255 static std::string readPath()
256 {
258 }
259
261 template <class CommunicatorType>
262 static void createGlobalPath(const CommunicatorType& comm,
263 const std::string& path)
264 {
265 // only rank 0 creates global dir
266 if( comm.rank() <= 0 )
267 {
268 // create directory
269 if( !createDirectory( path ) )
270 std::cerr << "Failed to create path `" << path << "'." << std::endl;
271 }
272
273 // wait for all procs to arrive here
274 comm.barrier ();
275 }
276
277 // copy path to filename and add a slash if necessary
278 static std::string copyPathToFilename( const std::string& path )
279 {
280 // first proc creates directory
281 std::string filename( path );
282
283 const char lastToken = filename.c_str()[ filename.size() - 1 ];
284 const char* slash = "/";
285 // add / if necessary
286 if( lastToken != slash[0] )
287 filename += "/";
288
289 return filename;
290 }
291
292 // creates path and processor sub pathes
293 template <class CommunicatorType>
294 static std::string createPath(const CommunicatorType& comm,
295 const std::string& pathPrefix,
296 const std::string& dataPrefix,
297 const int step,
298 const bool alsoCreateRankPath = true )
299 {
300 // first proc creates directory
301 std::string filename( copyPathToFilename( pathPrefix ));
302
303 filename += dataPrefix;
304 std::string path = generateFilename( filename, step );
305
306 // create global path
307 createGlobalPath( comm, path );
308
309 // also create path for each rank
310 if( alsoCreateRankPath )
311 {
312 // append path with p for proc
313 path += "/p";
314
315 // create path if not exists
316 path = createPathName( path, comm.rank() );
317
318 // create path if not exits
319 if( !createDirectory( path ) )
320 std::cerr << "Failed to create path `" << path << "'." << std::endl;
321 }
322 return path;
323 }
324
325 // creates path and processor sub pathes
326 static std::string createRecoverPath(
327 const std::string& pathPrefix,
328 const int rank,
329 const std::string& dataPrefix,
330 const int step,
331 const bool alsoUseRankPath = true )
332 {
333 // first proc creates directory
334 std::string filename( copyPathToFilename( pathPrefix ));
335
336 filename += dataPrefix;
337 std::string path = generateFilename( filename, step );
338
339 if( alsoUseRankPath )
340 {
341 // append path with p for proc
342 path += "/p";
343
344 // create proc dir
345 return createPathName( path , rank );
346 }
347 else
348 return path;
349 }
350 }; // end class IOInterface
351
352 } // end namespace Fem
353
354} // end namespace Dune
355#endif // #ifndef DUNE_FEM_IOINTERFACE_HH
IOInterface to write data to hard disk.
Definition: iointerface.hh:156
static void createGlobalPath(const CommunicatorType &comm, const std::string &path)
create global path for data output
Definition: iointerface.hh:262
static std::string defaultGridKey(int dimension, bool check=true)
return FEM key for macro grid reading
Definition: iointerface.hh:181
static void createPath(const std::string &path)
create given path in combination with rank
Definition: iointerface.hh:232
virtual void writeData(double sequenceStamp) const =0
write data with a given sequence stamp
virtual void write() const =0
write given data to disc, evaluates parameter savecount
virtual ~IOInterface()
destructor
Definition: iointerface.hh:164
static std::string readPath()
Definition: iointerface.hh:255
virtual void write(const TimeProviderBase &tp) const =0
write given data to disc, evaluates parameter savecount and savestep
static std::string defaultGridKey(std::string base, int dimension, const ParameterReader &parameter, bool check=true)
return FEM key for macro grid reading
Definition: iointerface.hh:197
static std::string createPathName(const std::string &pathPref, int rank)
create given path in combination with rank
Definition: iointerface.hh:239
IOInterface()
default constructor
Definition: iointerface.hh:160
static std::string commonOutputPath()
obtain common output path
Definition: parameter.hh:432
general base for time providers
Definition: timeprovider.hh:36
A few common exception classes.
#define DUNE_THROW(E, m)
Definition: exceptions.hh:218
Dune namespace.
Definition: alignedallocator.hh:13
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 13, 23:29, 2024)