VtkVectorWriter.h 8.29 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// ============================================================================
// ==                                                                        ==
// == AMDiS - Adaptive multidimensional simulations                          ==
// ==                                                                        ==
// ==  http://www.amdis-fem.org                                              ==
// ==                                                                        ==
// ============================================================================
//
// Software License for AMDiS
//
// Copyright (c) 2010 Dresden University of Technology 
// All rights reserved.
// Authors: Simon Vey, Thomas Witkowski et al.
//
// This file is part of AMDiS
//
// See also license.opensource.txt in the distribution.



/** \file VtkVectorWriter.h */

#ifndef AMDIS_VTKVECTORWRITER_H
#define AMDIS_VTKVECTORWRITER_H

#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/device/file_descriptor.hpp>
#include <boost/iostreams/filter/gzip.hpp>
#include <boost/iostreams/filter/bzip2.hpp>

#include <fstream>
#include "BasisFunction.h"
#include "DataCollector.h"
#include "FileWriter.h"
#include "SystemVector.h"

namespace AMDiS {

  
  class VtkVectorWriter
  {
Praetorius, Simon's avatar
Praetorius, Simon committed
42
43
44
  public:
    
  template<typename S>
45
  struct Impl {
46
     Impl(std::vector<DataCollector<S>*> *dc, bool writeAs3dVector_=false)
47
48
      : dataCollector(dc),
	compress(NONE),
49
	writeAs3dVector(writeAs3dVector_),
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
	three(3)
    {
      degree = (*dataCollector)[0]->getFeSpace()->getBasisFcts()->getDegree();
      dim = (*dataCollector)[0]->getMesh()->getDim();
    }

    /// Writes a ParaView-VTK file.
    int writeFile(std::string name);

    /// Writes a pvtu file, which contains the links to all the rank files.
    void writeParallelFile(std::string name, int nRanks,
			   std::string fnPrefix, std::string fnPostfix);

    /// Set a compressing method for file output.
    void setCompression(FileCompression c)
    {
      compress = c;
    }
Praetorius, Simon's avatar
Praetorius, Simon committed
68
69
70
71
72

    void setWriteAs3dVector(bool w) {
      writeAs3dVector = w;
    }

73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
  protected:
    /// Writes the VTK file to an arbitrary stream.
    template<typename T>
    void writeFileToStream(T &file);

    /// Writes all coordinates of vertices and interpolation points to an output file.
    template<typename T>
    void writeVertexCoords(T &file);

    /// Writes all values of vertices and interpolation point to an output file.
    template<typename T>
    void writeVertexValues(T &file, int componentNo);

    /// Writes the connectivity of all simplices to an output file.
    template<typename T>
    void writeConnectivity(T &file);

    /// Writes the connectivity for the case dim = 2 and degree = 2 to an output file.
    template<typename T>
    void writeConnectivity_dim2_degree2(T &file);

    /// Writes the connectivity for the case dim = 2 and degree = 3 to an output file.
    template<typename T>
    void writeConnectivity_dim2_degree3(T &file);

    /// Writes the connectivity for the case dim = 2 and degree = 4 to an output file.
    template<typename T>
    void writeConnectivity_dim2_degree4(T &file);

    /// Writes a world coordinate to a given file.
    inline void writeCoord(boost::iostreams::filtering_ostream &file,
			   WorldVector<double> coord)
    {
      for (int i = 0; i < Global::getGeo(WORLD); i++)
	file << " " << std::scientific << coord[i];
      for (int i = Global::getGeo(WORLD); i < 3; i++)
	file << " "<<0.0;

      file << "\n";
    }

    /// Writes a world coordinate to a given file.
    inline void writeCoord(std::ofstream &file, WorldVector<double> coord)
    {
      for (int i = 0; i < Global::getGeo(WORLD); i++)
	file << " " << std::scientific << coord[i];
      for (int i = Global::getGeo(WORLD); i < 3; i++)
	file << " "<<0.0;

      file << "\n";
    }

    std::ostream& print(const WorldVector<double> &l, std::ostream &o) {
      for (size_t i = 0; i < static_cast<size_t>(l.getSize()); i++) {
	o << (fabs(l[i]) < 1e-40 ? 0.0 : l[i]) << " ";
      }
      for (int i = l.getSize(); i < 3; i++)
	o << 0.0 << " ";
      return o;
    }
    
    std::ostream& print(const std::vector<double> &l, std::ostream &o) {
      std::vector<double>::const_iterator it = l.begin();
136
137
      for (size_t i = 0; i < l.size(); i++) {
	o << (fabs(l[i]) < 1e-40 ? 0.0 : l[i]) << " ";
138
      }
139
140
141
142
      if (writeAs3dVector) {
	for (int i = l.size(); i < 3; i++)
	  o << 0.0 << " ";
      }
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
      return o;
    }

    template<typename T>
    std::ostream& print(const T &value, std::ostream &o) {
      o << (fabs(value) < 1e-40 ? 0.0 : value);
      return o;
    }
    
    // num_rows for scalar types
    template<typename T>
    size_t num_rows(T &v) {
      return 1;
    };
    // for WorldVectors print 3 components
    template<typename T>
    size_t num_rows(WorldVector<T> &v) {
      return three; //(writeAs3dVector ? three : static_cast<size_t>(v.getSize()));
    }
    // num_rows for vector types
    template<typename T>
    size_t num_rows(std::vector<T> &v) {
      size_t v_size = v.size();
      return (writeAs3dVector ? std::max(three,v_size) : v_size);
    };
    template<typename T>
    size_t num_rows(const std::vector<T> &v) {
      size_t v_size = v.size();
      return (writeAs3dVector ? std::max(three,v_size) : v_size);
    };
    template<typename T>
    size_t num_rows(mtl::dense_vector<T> &v) {
      size_t v_size = mtl::num_rows(v);
      return (writeAs3dVector ? std::max(three,v_size) : v_size);
    };
    template<typename T>
    size_t num_rows(const mtl::dense_vector<T> &v) {
      size_t v_size = mtl::num_rows(v);
      return (writeAs3dVector ? std::max(three,v_size) : v_size);
    };

  private:
    /// List of DataCollectors, for each component of the problem one.
    std::vector<DataCollector<S>*> *dataCollector;

    /** \brief
     * Defines if the file has to be compressed for ouput, and with which
     * kind of compress method.
     */
    FileCompression compress;

    /// Degree of the basis function of the problem.
    int degree;

    /// Dimension of the geometry.
    int dim;

    bool writeAs3dVector;
    size_t three;
  };
  public:


    /// May be used to simply write ParaView files.
    template<typename T>
    static void writeFile(DOFVector<T> *values,
			  std::string filename, 
210
211
			  bool writeParallel = true,
			  bool writeAs3dVector = false)
212
213
214
    {
      std::vector<DataCollector<T>*> dcList(0);
      dcList.push_back(new DataCollector<T>(values->getFeSpace(), values));
215
      writeFile(dcList, filename, writeParallel, writeAs3dVector);
216
217
218
219
      delete dcList[0];
    }

    /// May be used to simply write ParaView files.
220
221
    template<typename T>
    static void writeFile(DOFVector<T> &values,
222
			  std::string filename,
223
224
			  bool writeParallel = true,
			  bool writeAs3dVector = false)
225
    {
226
      writeFile(&values, filename, writeParallel, writeAs3dVector);
227
228
229
230
231
    }

    /// May be used to simply write ParaView files with a list of values.
    static void writeFile(std::vector<DOFVector<double>*> &values,
			  std::string filename, 
232
233
			  bool writeParallel = true,
			  bool writeAs3dVector = false);
234
235
236

    static void writeFile(WorldVector<DOFVector<double>*> &values,
			  std::string filename, 
237
238
			  bool writeParallel = true,
			  bool writeAs3dVector = false);
239
240
241

    static void writeFile(SystemVector *values,
			  std::string filename, 
242
243
			  bool writeParallel = true,
			  bool writeAs3dVector = false);
244
245
246
247

    template<typename T>
    static void writeFile(std::vector<DataCollector<T>*> &dcList,
			  std::string filename,
248
249
			  bool writeParallel = true,
			  bool writeAs3dVector = false)
250
    {
251
      Impl<T> writer(&dcList, writeAs3dVector);
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278

  #ifdef HAVE_PARALLEL_DOMAIN_AMDIS
      if (writeParallel) {
	using boost::lexical_cast;

	int sPos = filename.find(".vtu");
	TEST_EXIT(sPos >= 0)("Failed to find file postfix!\n");
	std::string name = filename.substr(0, sPos);

	if (MPI::COMM_WORLD.Get_rank() == 0)
	  writer.writeParallelFile(name + ".pvtu", MPI::COMM_WORLD.Get_size(), name, ".vtu");

	filename = name + "-p" + lexical_cast<std::string>(MPI::COMM_WORLD.Get_rank()) + "-.vtu";
      }
  #endif
      writer.writeFile(filename);
    }

    /// Adds a new entry to a ParaView animation file.
    static int updateAnimationFile(std::string valueFilename,
			    std::vector< std::string > *paraViewAnimationFrames,
			    std::string animationFilename);

  };
}

#endif // AMDIS_VTKVECTORWRITER_H