VtkWriter.cc 7.08 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
  int VtkWriter::writeFile(std::string name)
36
  {
37
    FUNCNAME("VtkWriter::writeFile()");
38
    
39
40
41
42
    boost::iostreams::filtering_ostream file;
    switch (compress) {
    case GZIP:
      file.push(boost::iostreams::gzip_compressor());
43
      name.append(".gz");
44
45
46
      break;
    case BZIP2:
      file.push(boost::iostreams::bzip2_compressor());
47
      name.append(".bz2");
48
49
50
      break;
    default:
      break;
51
    }
52

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

60
    file.push(boost::iostreams::file_descriptor_sink(name, std::ios::trunc));
61
    writeFileToStream(file);
62

63
    return 0;
64
65
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
66

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

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

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


115
116
117
  int VtkWriter::updateAnimationFile(std::string valueFilename,
				     std::vector< std::string > *paraViewAnimationFrames,
				     std::string animationFilename)
118
  {
119
120
    FUNCNAME("VtkWriter::updateAnimationFile()");

121
    size_t found = valueFilename.find_last_of("/\\");
122
    paraViewAnimationFrames->push_back(valueFilename.substr(found + 1));
123

124
125
    boost::iostreams::filtering_ostream file;
    {
126
127
      std::ofstream swapfile(animationFilename.c_str(), 
			     std::ios::out | std::ios::trunc);
128
129
      TEST_EXIT(swapfile.is_open())
	("Cannot open file %s for writing!\n", animationFilename.c_str());
130
131
      swapfile.close();
    }
132
    file.push(boost::iostreams::file_descriptor_sink(animationFilename, 
133
						     std::ios::trunc)); 
134

Thomas Witkowski's avatar
Thomas Witkowski committed
135
136
137
    file << "<?xml version=\"1.0\"?>\n";
    file << "<VTKFile type=\"Collection\" version=\"0.1\" >"  << "\n";
    file << "<Collection>\n";
138

139
140
141
142
143
144
145
146
    int counter = 0;
    std::vector< std::string >::iterator it;
    for (it = paraViewAnimationFrames->begin(); 
	 it < paraViewAnimationFrames->end(); 
	 ++it, counter++) {
      file << "<DataSet timestep=\"" << counter
	   << "\" part=\"0\" file=\"" << (*it) << "\"/>\n";      
    }
147

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

    return 0;
  }
153

154

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

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

167

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

180

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

193
194

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

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