VtkWriter.cc 6.92 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
31
32
33
#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
34
35
36
  using namespace std;

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

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

Thomas Witkowski's avatar
Thomas Witkowski committed
61
    file.push(boost::iostreams::file_descriptor_sink(name, ios::trunc));
62
    writeFileToStream(file);
63

64
    return 0;
65
66
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
67

Thomas Witkowski's avatar
Thomas Witkowski committed
68
69
  void VtkWriter::writeParallelFile(string name, int nRanks,
				    string fnPrefix, string fnPostfix)
Thomas Witkowski's avatar
Thomas Witkowski committed
70
  {
71
72
    FUNCNAME("VtkWriter::writeParallelFile()");

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

    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++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
102
      stringstream oss;
Thomas Witkowski's avatar
Thomas Witkowski committed
103
104
105
106
107
108
109
110
111
112
113
114
115
      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";
  }


Thomas Witkowski's avatar
Thomas Witkowski committed
116
117
118
119
  int VtkWriter::updateAnimationFile(AdaptInfo *adaptInfo,
				     string valueFilename,
				     vector<pair<double, string > > *paraViewAnimationFrames,
				     string animationFilename)
120
  {
121
122
    FUNCNAME("VtkWriter::updateAnimationFile()");

123
    size_t found = valueFilename.find_last_of("/\\");
Thomas Witkowski's avatar
Thomas Witkowski committed
124
125
    paraViewAnimationFrames->push_back(make_pair(adaptInfo->getTime(), 
						 valueFilename.substr(found + 1)));
126

127
128
    boost::iostreams::filtering_ostream file;
    {
Thomas Witkowski's avatar
Thomas Witkowski committed
129
130
      ofstream swapfile(animationFilename.c_str(), 
			     ios::out | ios::trunc);
131
132
      TEST_EXIT(swapfile.is_open())
	("Cannot open file %s for writing!\n", animationFilename.c_str());
133
134
      swapfile.close();
    }
135
    file.push(boost::iostreams::file_descriptor_sink(animationFilename, 
Thomas Witkowski's avatar
Thomas Witkowski committed
136
						     ios::trunc)); 
137

Thomas Witkowski's avatar
Thomas Witkowski committed
138
139
140
    file << "<?xml version=\"1.0\"?>\n";
    file << "<VTKFile type=\"Collection\" version=\"0.1\" >"  << "\n";
    file << "<Collection>\n";
141

Thomas Witkowski's avatar
Thomas Witkowski committed
142
143
144
145
    for (vector<pair<double, string> >::iterator it = paraViewAnimationFrames->begin(); 
	 it < paraViewAnimationFrames->end(); ++it)
      file << "<DataSet timestep=\"" << it->first
	   << "\" part=\"0\" file=\"" << it->second << "\"/>\n"; 
146

Thomas Witkowski's avatar
Thomas Witkowski committed
147
148
    file << "</Collection>\n";
    file << "</VTKFile>\n";
149
150
151

    return 0;
  }
152

153

154
  void VtkWriter::writeFile(DOFVector<double> *values, 
Thomas Witkowski's avatar
Thomas Witkowski committed
155
			    string filename,
156
			    bool writeParallel)
157
  {
158
159
160
    FUNCNAME("VtkWriter::writeFile()");

    DataCollector dc(values->getFeSpace(), values);
Thomas Witkowski's avatar
Thomas Witkowski committed
161
    vector<DataCollector*> dcList(0);
162
    dcList.push_back(&dc);
163
    writeFile(dcList, filename, writeParallel);
164
165
  }

166

Thomas Witkowski's avatar
Thomas Witkowski committed
167
168
  void VtkWriter::writeFile(vector<DOFVector<double>* > &values,
			    string filename,
169
			    bool writeParallel)
170
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
171
    vector<DataCollector*> dcList(0);
172
173
174
175
176
177
178
    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];	
  }

179

180
  void VtkWriter::writeFile(WorldVector<DOFVector<double>* > &values,
Thomas Witkowski's avatar
Thomas Witkowski committed
181
			    string filename,
182
			    bool writeParallel)
183
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
184
    vector<DataCollector*> dcList(0);
185
186
187
188
189
190
191
    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];	
  }

192
193

  void VtkWriter::writeFile(DOFVector<WorldVector<double> > *values,
Thomas Witkowski's avatar
Thomas Witkowski committed
194
			    string filename,
195
			    bool writeParallel)
196
  {
197
198
199
200
201
202
203
204
    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];
  }
205

206
  
207
  void VtkWriter::writeFile(SystemVector *values, 
Thomas Witkowski's avatar
Thomas Witkowski committed
208
			    string filename,
209
			    bool writeParallel)
210
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
211
    vector<DataCollector*> dcList(0);
212
213
214
215
216
217
218
219
220
    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];    
  }
  
  
Thomas Witkowski's avatar
Thomas Witkowski committed
221
222
  void VtkWriter::writeFile(vector<DataCollector*> &dcList,
			    string filename,
223
			    bool writeParallel)
224
  {
225
226
227
228
229
230
231
232
    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");
Thomas Witkowski's avatar
Thomas Witkowski committed
233
      string name = filename.substr(0, sPos);
234
235
236
237
      
      if (MPI::COMM_WORLD.Get_rank() == 0)
	writer.writeParallelFile(name + ".pvtu", MPI::COMM_WORLD.Get_size(), name, ".vtu");    
      
Thomas Witkowski's avatar
Thomas Witkowski committed
238
      filename = name + "-p" + lexical_cast<string>(MPI::COMM_WORLD.Get_rank()) + "-.vtu";
239
240
241
242
    }
#endif
    writer.writeFile(filename);
  }
243
}