VtkWriter.cc 6.25 KB
Newer Older
1
#include <stdio.h>
2
3
#include <string>
#include <fstream>
Thomas Witkowski's avatar
Thomas Witkowski committed
4
#include <sstream>
Peter Gottschling's avatar
Peter Gottschling committed
5
#include <cmath>
6
7
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/convenience.hpp>
Thomas Witkowski's avatar
Thomas Witkowski committed
8
#include <boost/lexical_cast.hpp>
9

Thomas Witkowski's avatar
Thomas Witkowski committed
10
11
12
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
#include <mpi.h>
#endif
Thomas Witkowski's avatar
Thomas Witkowski committed
13

14
15
16
17
18
19
20
21
#include "VtkWriter.h"
#include "DataCollector.h"
#include "DOFVector.h"
#include "SurfaceRegion_ED.h"
#include "ElementRegion_ED.h"

namespace AMDiS { 

Thomas Witkowski's avatar
Thomas Witkowski committed
22
  int VtkWriter::writeFile(std::string name)
23
  {
24
25
    FUNCNAME("VtkWriter::writeFile()");

26
#ifdef HAVE_BOOST
27
    
28
29
30
31
    boost::iostreams::filtering_ostream file;
    switch (compress) {
    case GZIP:
      file.push(boost::iostreams::gzip_compressor());
32
      file.push(boost::iostreams::file_descriptor_sink(name.append(".gz")));    
33
34
35
      break;
    case BZIP2:
      file.push(boost::iostreams::bzip2_compressor());
36
      file.push(boost::iostreams::file_descriptor_sink(name.append(".bz2")));
37
38
39
40
41
      break;
    default:
      file.push(boost::iostreams::file_descriptor_sink(name));
      break;
    } 
42
    writeFileToStream(file);
43
44
45

#else

Thomas Witkowski's avatar
Thomas Witkowski committed
46
    std::ofstream file;
Thomas Witkowski's avatar
Thomas Witkowski committed
47
48
    file.open(name.c_str());
    TEST_EXIT(file.is_open())("Cannot open file %s for writing\n", name.c_str());
49
    writeFileToStream(file);    
50
    file.close();
51

Thomas Witkowski's avatar
Thomas Witkowski committed
52
#endif
53
      
54
    return 0;
55
56
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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

  void VtkWriter::writeParallelFile(std::string name, int nRanks,
				    std::string fnPrefix, std::string fnPostfix)
  {
    std::ofstream file;
    file.open(name.c_str());

    file << "<?xml version=\"1.0\"?>\n";
    file << "<VTKFile type=\"PUnstructuredGrid\">\n";
    file << "  <PUnstructuredGrid GhostLevel=\"0\">\n";
    file << "    <PPoints>\n"
	 << "      <PDataArray type=\"Float32\" NumberOfComponents=\"3\" format=\"ascii\"/>\n"
	 << "    </PPoints>\n";
    file << "    <PCells>\n"
	 << "      <PDataArray type=\"Int32\" Name=\"offsets\"/>\n"
	 << "      <PDataArray type=\"UInt8\" Name=\"types\"/>\n"
	 << "      <PDataArray type=\"Int32\" Name=\"connectivity\"/>\n"
	 << "    </PCells>\n";
    file << "    <PPointData>\n";

    for (int i = 0; i < static_cast<int>(dataCollector->size()); i++)
      file << "      <PDataArray type=\"Float32\" Name=\"value" 
	   << i << "\" format=\"ascii\"/>\n";

    file << "    </PPointData>\n";

    for (int i = 0; i < nRanks; i++) {
      std::stringstream oss;
      oss << fnPrefix << "-p" << i << "-" << fnPostfix;
      boost::filesystem::path filepath(oss.str());
      file << "    <Piece Source=\"" 
	   << boost::filesystem::basename(filepath)
	   << boost::filesystem::extension(filepath) << "\"/>\n";
      
    }

    file << "  </PUnstructuredGrid>\n";
    file << "</VTKFile>\n";

    file.close();
  }


Thomas Witkowski's avatar
Thomas Witkowski committed
100
101
  int VtkWriter::updateAnimationFile(std::string valueFilename,
				     std::vector< std::string > *paraViewAnimationFrames,
102
				     std::string animationFilename)
103
104
105
106
  {
    size_t found = valueFilename.find_last_of("/\\");
    paraViewAnimationFrames->push_back(valueFilename.substr(found + 1));

Thomas Witkowski's avatar
Thomas Witkowski committed
107
    std::ofstream file;
108
    file.open(animationFilename.c_str());
109

Thomas Witkowski's avatar
Thomas Witkowski committed
110
111
112
    file << "<?xml version=\"1.0\"?>\n";
    file << "<VTKFile type=\"Collection\" version=\"0.1\" >"  << "\n";
    file << "<Collection>\n";
113
114

    int counter = 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
115
    std::vector< std::string >::iterator it;
116
117
118
119
    for (it = paraViewAnimationFrames->begin(); 
	 it < paraViewAnimationFrames->end(); 
	 ++it, counter++) {
      file << "<DataSet timestep=\"" << counter
Thomas Witkowski's avatar
Thomas Witkowski committed
120
	   << "\" part=\"0\" file=\"" << (*it) << "\"/>\n";      
121
122
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
123
124
    file << "</Collection>\n";
    file << "</VTKFile>\n";
125
126
127
128
129

    file.close();
    
    return 0;
  }
130

131

132
  void VtkWriter::writeFile(DOFVector<double> *values, 
133
134
			    std::string filename,
			    bool writeParallel)
135
  {
136
137
138
139
140
141
142
143
    FUNCNAME("VtkWriter::writeFile()");

    DataCollector dc(values->getFeSpace(), values);
    std::vector<DataCollector*> dcList(0);
    dcList.push_back(&dc);
    writeFile(dcList,filename,writeParallel);
  }

144
145

  void VtkWriter::writeFile(std::vector<DOFVector<double>* > &values,
146
147
			    std::string filename,
			    bool writeParallel)
148
  {
149
150
151
152
153
154
155
156
    std::vector<DataCollector*> dcList(0);
    for (unsigned int i = 0; i < values.size(); i++)
      dcList.push_back(new DataCollector(values[i]->getFeSpace(), values[i]));	
    writeFile(dcList, filename, writeParallel);
    for (unsigned int i = 0; i < values.size(); i++)
      delete dcList[i];	
  }

157

158
  void VtkWriter::writeFile(WorldVector<DOFVector<double>* > &values,
159
160
			    std::string filename,
			    bool writeParallel)
161
  {
162
163
164
165
166
167
168
169
    std::vector<DataCollector*> dcList(0);
    for (int i = 0; i < values.getSize(); i++)
      dcList.push_back(new DataCollector(values[i]->getFeSpace(), values[i]));	
    writeFile(dcList, filename, writeParallel);
    for (int i = 0; i < values.getSize(); i++)
      delete dcList[i];	
  }

170
171

  void VtkWriter::writeFile(DOFVector<WorldVector<double> > *values,
172
173
			    std::string filename,
			    bool writeParallel)
174
  {
175
176
177
178
179
180
181
182
    WorldVector<DOFVector<double>*> valuesWV;
    for (int i =0 ; i < valuesWV.getSize(); i++)
      valuesWV[i] = new DOFVector<double>(values->getFeSpace(), "valuesWV_i");
    transform(values, &valuesWV);
    writeFile(valuesWV, filename, writeParallel);
    for (int i = 0; i < valuesWV.getSize(); i++)
      delete valuesWV[i];
  }
183

184
  
185
  void VtkWriter::writeFile(SystemVector *values, 
186
187
			    std::string filename,
			    bool writeParallel)
188
  {
189
190
191
192
193
194
195
196
197
198
    std::vector<DataCollector*> dcList(0);
    for (int i = 0; i < values->getSize(); i++)
      dcList.push_back(new DataCollector(values->getDOFVector(i)->getFeSpace(), 
					 values->getDOFVector(i)));    
    writeFile(dcList, filename, writeParallel);
    for (unsigned i = 0; i < dcList.size(); i++)
      delete dcList[i];    
  }
  
  
199
  void VtkWriter::writeFile(std::vector<DataCollector*> &dcList,
200
201
			    std::string filename,
			    bool writeParallel)
202
  {
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
    VtkWriter writer(&dcList);
    
#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);
  }
221
}