VtkWriter.cc 7.26 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
//
// 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.


13
#include <stdio.h>
14
15
#include <string>
#include <fstream>
Thomas Witkowski's avatar
Thomas Witkowski committed
16
#include <sstream>
Peter Gottschling's avatar
Peter Gottschling committed
17
#include <cmath>
18
19
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/convenience.hpp>
Thomas Witkowski's avatar
Thomas Witkowski committed
20
#include <boost/lexical_cast.hpp>
21

Thomas Witkowski's avatar
Thomas Witkowski committed
22
23
24
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
#include <mpi.h>
#endif
Thomas Witkowski's avatar
Thomas Witkowski committed
25

26
27
28
29
30
#include "VtkWriter.h"
#include "DataCollector.h"
#include "DOFVector.h"
#include "SurfaceRegion_ED.h"
#include "ElementRegion_ED.h"
31
#include "AdaptInfo.h"
32
33
34

namespace AMDiS { 

35
36
37
  using namespace std;

  int VtkWriter::writeFile(string name)
38
  {
39
    FUNCNAME("VtkWriter::writeFile()");
40
    
41
42
43
44
    boost::iostreams::filtering_ostream file;
    switch (compress) {
    case GZIP:
      file.push(boost::iostreams::gzip_compressor());
45
      name.append(".gz");
46
47
48
      break;
    case BZIP2:
      file.push(boost::iostreams::bzip2_compressor());
49
      name.append(".bz2");
50
51
52
      break;
    default:
      break;
53
    }
54

55
    {
56
      ofstream swapfile(name.c_str(), ios::out | ios::trunc);
57
58
      TEST_EXIT(swapfile.is_open())
	("Cannot open file %s for writing!\n", name.c_str());
59
60
      swapfile.close();
    }
61

62
    file.push(boost::iostreams::file_descriptor_sink(name, ios::trunc));
63
    writeFileToStream(file);
64

65
    return 0;
66
67
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
68

69
70
  void VtkWriter::writeParallelFile(string name, int nRanks,
				    string fnPrefix, string fnPostfix,
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
71
				    vector<string> &componentNames)
Thomas Witkowski's avatar
Thomas Witkowski committed
72
  {
73
74
    FUNCNAME("VtkWriter::writeParallelFile()");

Thomas Witkowski's avatar
Thomas Witkowski committed
75
76
    using boost::lexical_cast;

77
78
    boost::iostreams::filtering_ostream file;
    {
79
      ofstream swapfile(name.c_str(), ios::out | ios::trunc);
80
81
      TEST_EXIT(swapfile.is_open())
	("Cannot open file %s for writing!\n", name.c_str());
82
83
      swapfile.close();
    }
84
    file.push(boost::iostreams::file_descriptor_sink(name, ios::trunc)); 
Thomas Witkowski's avatar
Thomas Witkowski committed
85
86
87
88
89
90
91
92
93
94
95
96
97
98

    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";

Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
99
100
101
102
    for (unsigned int i = 0; i < componentNames.size(); i++)
      file << "      <PDataArray type=\"Float32\" Name=\"" 
	   << componentNames[i] 
	   << "\" format=\"ascii\"/>\n";
Thomas Witkowski's avatar
Thomas Witkowski committed
103
104
105
106

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

    for (int i = 0; i < nRanks; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
107
108
      string pname(fnPrefix + "-p" + lexical_cast<string>(i) + "-" + fnPostfix);
      boost::filesystem::path filepath(pname);
Thomas Witkowski's avatar
Thomas Witkowski committed
109
110
      file << "    <Piece Source=\"" 
	   << boost::filesystem::basename(filepath)
Thomas Witkowski's avatar
Thomas Witkowski committed
111
	   << boost::filesystem::extension(filepath) << "\"/>\n";      
Thomas Witkowski's avatar
Thomas Witkowski committed
112
113
114
115
116
117
118
    }

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


119
  int VtkWriter::updateAnimationFile(AdaptInfo *adaptInfo,
120
121
122
				     string valueFilename,
				     vector< pair<double, string> > *paraViewAnimationFrames,
				     string animationFilename)
123
  {
124
125
    FUNCNAME("VtkWriter::updateAnimationFile()");

126
    size_t found = valueFilename.find_last_of("/\\");
127
128
    paraViewAnimationFrames->push_back(make_pair(adaptInfo->getTime(),
						 valueFilename.substr(found + 1)));
129

130
131
    boost::iostreams::filtering_ostream file;
    {
132
133
      ofstream swapfile(animationFilename.c_str(), 
			     ios::out | ios::trunc);
134
135
      TEST_EXIT(swapfile.is_open())
	("Cannot open file %s for writing!\n", animationFilename.c_str());
136
137
      swapfile.close();
    }
138
    file.push(boost::iostreams::file_descriptor_sink(animationFilename, 
139
						     ios::trunc)); 
140

Thomas Witkowski's avatar
Thomas Witkowski committed
141
142
143
    file << "<?xml version=\"1.0\"?>\n";
    file << "<VTKFile type=\"Collection\" version=\"0.1\" >"  << "\n";
    file << "<Collection>\n";
144

145
146
147
148
    for (vector<pair<double, string> >::iterator it = paraViewAnimationFrames->begin();
	 it < paraViewAnimationFrames->end(); ++it) {
      file << "<DataSet timestep=\"" << it->first
	   << "\" part=\"0\" file=\"" << it->second << "\"/>\n";
149
    }
150

Thomas Witkowski's avatar
Thomas Witkowski committed
151
152
    file << "</Collection>\n";
    file << "</VTKFile>\n";
153
154
155

    return 0;
  }
156

157

158
  void VtkWriter::writeFile(DOFVector<double> *values, 
159
			    string filename,
160
			    bool writeParallel)
161
  {
162
163
    FUNCNAME("VtkWriter::writeFile()");

164
    DataCollector<> dc(values->getFeSpace(), values);
165
    vector<DataCollector<>*> dcList(0);
166
    dcList.push_back(&dc);
167
    writeFile(dcList, filename, writeParallel);
168
169
  }

170

171
172
  void VtkWriter::writeFile(vector<DOFVector<double>* > &values,
			    string filename,
173
			    bool writeParallel)
174
  {
175
    vector<DataCollector<>*> dcList(0);
176
    for (unsigned int i = 0; i < values.size(); i++)
177
      dcList.push_back(new DataCollector<>(values[i]->getFeSpace(), values[i]));	
178
179
180
181
182
    writeFile(dcList, filename, writeParallel);
    for (unsigned int i = 0; i < values.size(); i++)
      delete dcList[i];	
  }

183

184
  void VtkWriter::writeFile(WorldVector<DOFVector<double>* > &values,
185
			    string filename,
186
			    bool writeParallel)
187
  {
188
    vector<DataCollector<>*> dcList(0);
189
    for (int i = 0; i < values.getSize(); i++)
190
      dcList.push_back(new DataCollector<>(values[i]->getFeSpace(), values[i]));	
191
192
193
194
195
    writeFile(dcList, filename, writeParallel);
    for (int i = 0; i < values.getSize(); i++)
      delete dcList[i];	
  }

196
197

  void VtkWriter::writeFile(DOFVector<WorldVector<double> > *values,
198
			    string filename,
199
			    bool writeParallel)
200
  {
201
202
    WorldVector<DOFVector<double>*> valuesWV;
    for (int i =0 ; i < valuesWV.getSize(); i++)
Praetorius, Simon's avatar
Praetorius, Simon committed
203
      valuesWV[i] = new DOFVector<double>(values->getFeSpace(), "values["+boost::lexical_cast<std::string>(i)+"]");
204
205
206
207
208
    transform(values, &valuesWV);
    writeFile(valuesWV, filename, writeParallel);
    for (int i = 0; i < valuesWV.getSize(); i++)
      delete valuesWV[i];
  }
209

210
  
211
  void VtkWriter::writeFile(SystemVector *values, 
212
			    string filename,
213
			    bool writeParallel)
214
  {
215
    vector<DataCollector<>*> dcList(0);
216
    for (int i = 0; i < values->getSize(); i++)
217
      dcList.push_back(new DataCollector<>(values->getDOFVector(i)->getFeSpace(), 
218
219
220
221
222
223
224
					 values->getDOFVector(i)));    
    writeFile(dcList, filename, writeParallel);
    for (unsigned i = 0; i < dcList.size(); i++)
      delete dcList[i];    
  }
  
  
225
226
  void VtkWriter::writeFile(vector<DataCollector<>*> &dcList,
			    string filename,
227
			    bool writeParallel)
228
  {
229
230
231
232
233
234
235
236
    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");
237
      string name = filename.substr(0, sPos);
238
      
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
239
240
241
242
243
      if (MPI::COMM_WORLD.Get_rank() == 0) {
	vector<string> componentNames;
	for (unsigned int i = 0; i < dcList.size(); i++)
	  componentNames.push_back(dcList[i]->getValues()->getName());

244
245
246
	writer.writeParallelFile(name + ".pvtu", 
				 MPI::COMM_WORLD.Get_size(), 
				 name, ".vtu",
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
247
248
				 componentNames);
      }
249
      
250
      filename = name + "-p" + lexical_cast<string>(MPI::COMM_WORLD.Get_rank()) + "-.vtu";
251
252
253
254
    }
#endif
    writer.writeFile(filename);
  }
255
}