VtkWriter.cc 7.02 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
#include "VtkWriter.h"
#include "DataCollector.h"
28
#include "DataCollector.hh"
29
30
31
32
33
34
#include "DOFVector.h"
#include "SurfaceRegion_ED.h"
#include "ElementRegion_ED.h"

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
71
  void VtkWriter::writeParallelFile(string name, int nRanks,
				    string fnPrefix, string fnPostfix,
				    int nComponents)
Thomas Witkowski's avatar
Thomas Witkowski committed
72
  {
73
74
    FUNCNAME("VtkWriter::writeParallelFile()");

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

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

97
    for (int i = 0; i < nComponents; i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
98
99
100
101
102
103
      file << "      <PDataArray type=\"Float32\" Name=\"value" 
	   << i << "\" format=\"ascii\"/>\n";

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

    for (int i = 0; i < nRanks; i++) {
104
      stringstream oss;
Thomas Witkowski's avatar
Thomas Witkowski committed
105
106
107
108
109
110
111
112
113
114
115
116
117
      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";
  }


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

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

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

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

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

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

    return 0;
  }
155

156

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

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

169

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

182

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

195
196

  void VtkWriter::writeFile(DOFVector<WorldVector<double> > *values,
197
			    string filename,
198
			    bool writeParallel)
199
  {
200
201
202
203
204
205
206
207
    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];
  }
208

209
  
210
  void VtkWriter::writeFile(SystemVector *values, 
211
			    string filename,
212
			    bool writeParallel)
213
  {
214
    vector<DataCollector<>*> dcList(0);
215
    for (int i = 0; i < values->getSize(); i++)
216
      dcList.push_back(new DataCollector<>(values->getDOFVector(i)->getFeSpace(), 
217
218
219
220
221
222
223
					 values->getDOFVector(i)));    
    writeFile(dcList, filename, writeParallel);
    for (unsigned i = 0; i < dcList.size(); i++)
      delete dcList[i];    
  }
  
  
224
225
  void VtkWriter::writeFile(vector<DataCollector<>*> &dcList,
			    string filename,
226
			    bool writeParallel)
227
  {
228
229
230
231
232
233
234
235
    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");
236
      string name = filename.substr(0, sPos);
237
238
      
      if (MPI::COMM_WORLD.Get_rank() == 0)
239
240
241
242
	writer.writeParallelFile(name + ".pvtu", 
				 MPI::COMM_WORLD.Get_size(), 
				 name, ".vtu",
				 static_cast<int>(dcList.size()));    
243
      
244
      filename = name + "-p" + lexical_cast<string>(MPI::COMM_WORLD.Get_rank()) + "-.vtu";
245
246
247
248
    }
#endif
    writer.writeFile(filename);
  }
249
}