VtkWriter.cc 7.68 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/******************************************************************************
 *
 * AMDiS - Adaptive multidimensional simulations
 *
 * Copyright (C) 2013 Dresden University of Technology. All Rights Reserved.
 * Web: https://fusionforge.zih.tu-dresden.de/projects/amdis
 *
 * Authors: 
 * Simon Vey, Thomas Witkowski, Andreas Naumann, Simon Praetorius, et al.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 *
 * This file is part of AMDiS
 *
 * See also license.opensource.txt in the distribution.
 * 
 ******************************************************************************/


#include <stdio.h>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
#include <cmath>

#include "boost/filesystem/operations.hpp"
#include "boost/filesystem/convenience.hpp"

32
33
34
35
#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/device/file_descriptor.hpp>
#include <boost/iostreams/copy.hpp>
36
#ifdef HAVE_COMPRESSION
37
#include <boost/iostreams/filter/zlib.hpp>
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#endif
#include "AdaptInfo.h"
#include "VtkWriter.h"

namespace AMDiS  { namespace io {
  
    namespace VtkWriter
    {
      
      using namespace std;

      int Aux::writeFile(string name)
      {
	FUNCNAME("VtkWriter::writeFile()");
	
	boost::iostreams::filtering_ostream file;
54

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

	file.push(boost::iostreams::file_descriptor_sink(name, ios::trunc));
63
64
65
66
67
68
69
	
 	if(format == APPENDED || format == APPENDED_COMPRESSED)
	  writeFileToStreamAppended(file);
	else {
	  TEST_EXIT(format == ASCII)("Unknown ParaView mode.\n");
	  writeFileToStream(file);
	}
70
71
72
	return 0;
      }
      
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
      std::string Aux::getStreamData()
      {
	using namespace std;

	string result(""), header(""), body("");
	
	switch(format) {
	  case APPENDED: {
	    BinaryStream hstream;
	    hstream << bstream.getSize();
	    body = bstream.str();
	    header = hstream.str();
	    result = detail::base64Encode(header + body);
	    break;
	  }
	  case APPENDED_COMPRESSED: {
#ifdef HAVE_COMPRESSION
	    BinaryStream hstream;
	    int nBlocks = bstream.getSize() / AMDIS_ZLIB_BLOCK_SIZE + 1;
	    int finalsize = bstream.getSize() % AMDIS_ZLIB_BLOCK_SIZE;
	    hstream << nBlocks << AMDIS_ZLIB_BLOCK_SIZE << finalsize;
	      
	    string data = bstream.str();
	      
	    for(int i = 0; i < nBlocks; i++) {
	      string subData = (nBlocks - 1 == i) ? data.substr(i * AMDIS_ZLIB_BLOCK_SIZE, finalsize) : data.substr(i * AMDIS_ZLIB_BLOCK_SIZE, AMDIS_ZLIB_BLOCK_SIZE);
	      stringstream tmp1, tmp2;
	      tmp1 << subData;
	      boost::iostreams::filtering_streambuf<boost::iostreams::input> in;
	      in.push(boost::iostreams::zlib_compressor());
	      in.push(tmp1);
		
	      boost::iostreams::copy(in, tmp2);
	      subData = tmp2.str();
	      int size = subData.length();
	      hstream << size;
	      body += subData;
	    }
	    header = hstream.str();
	    result = detail::base64Encode(header) + detail::base64Encode(body);
#endif
	    break;
	  }
	  case ASCII:
	  default:break;
	}
	bstream.str("");
	return result;
      }
      
123
124
125
126
// __________________________________________________________________________ //
      
      namespace detail 
      {
127
128
129
130
131
132
133
	std::string base64Encode(std::string text)
	{
	  unsigned int writePaddChars = (3-text.length()%3)%3;
	  std::string base64(binary_base64(text.begin()),binary_base64(text.end()));
	  base64.append(writePaddChars,'=');
	  return base64;
	}
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175

	int updateAnimationFile(AdaptInfo *adaptInfo,
				string valueFilename,
				vector< pair<double, string> > *paraViewAnimationFrames,
				string animationFilename)
	{
	  FUNCNAME("updateAnimationFile()");

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

	  boost::iostreams::filtering_ostream file;
	  {
	    ofstream swapfile(animationFilename.c_str(), 
				  ios::out | ios::trunc);
	    TEST_EXIT(swapfile.is_open())
	      ("Cannot open file %s for writing!\n", animationFilename.c_str());
	    swapfile.close();
	  }
	  file.push(boost::iostreams::file_descriptor_sink(animationFilename, 
							  ios::trunc)); 

	  file << "<?xml version=\"1.0\"?>\n";
	  file << "<VTKFile type=\"Collection\" version=\"0.1\" >"  << "\n";
	  file << "<Collection>\n";

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

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

	  return 0;
	}
	

	void writeParallelFile(string name, int nRanks,
			      string fnPrefix, string fnPostfix,
176
177
			      vector<string> &componentNames,
			      ::AMDiS::io::VtkWriter::Vtuformat format,
Praetorius, Simon's avatar
Praetorius, Simon committed
178
179
180
			      bool highPrecision,
			      bool writeAsVector
			      )
181
182
183
184
	{
	  FUNCNAME("writeParallelFile()");

	  using boost::lexical_cast;
185
186
187
188
189
	  
#ifndef HAVE_COMPRESSION
	  if(format == APPENDED_COMPRESSED) 
	    format = APPENDED;
#endif
190
191
192
193
194
195
196
197
198
199
200

	  boost::iostreams::filtering_ostream file;
	  {
	    ofstream swapfile(name.c_str(), ios::out | ios::trunc);
	    TEST_EXIT(swapfile.is_open())
	      ("Cannot open file %s for writing!\n", name.c_str());
	    swapfile.close();
	  }
	  file.push(boost::iostreams::file_descriptor_sink(name, ios::trunc)); 

	  file << "<?xml version=\"1.0\"?>\n";
201
202
203
204
205
206
	  
	  if(format == ::AMDiS::io::VtkWriter::APPENDED_COMPRESSED)
	    file << "<VTKFile type=\"PUnstructuredGrid\" compressor=\"vtkZLibDataCompressor\" >\n";
	  else
	    file << "<VTKFile type=\"PUnstructuredGrid\" >\n";
	  
207
	  file << "  <PUnstructuredGrid GhostLevel=\"0\">\n";
208
209
210
211
212
213
214
215
216
217
218
219
220
221
	  file << "    <PPoints>\n";
	  
	  if(highPrecision && format != ::AMDiS::io::VtkWriter::ASCII)
	    file << "      <PDataArray type=\"Float64\"";
	  else
	    file << "      <PDataArray type=\"Float32\"";
	  
	  if(format == ::AMDiS::io::VtkWriter::ASCII)
	    file << " NumberOfComponents=\"3\" format=\"ascii\"/>\n";
	  else
	    file << " NumberOfComponents=\"3\" format=\"appended\"/>\n";
	  
	  file << "    </PPoints>\n"
	      << "    <PCells>\n"
222
223
224
225
226
	      << "      <PDataArray type=\"Int32\" Name=\"offsets\"/>\n"
	      << "      <PDataArray type=\"UInt8\" Name=\"types\"/>\n"
	      << "      <PDataArray type=\"Int32\" Name=\"connectivity\"/>\n"
	      << "    </PCells>\n";
	  file << "    <PPointData>\n";
Praetorius, Simon's avatar
Praetorius, Simon committed
227
228
	  
	  for (size_t i = 0; i < writeAsVector ? 1 : componentNames.size(); i++) {
229
230
231
232
233
234
235
236
	    if(highPrecision && format != ::AMDiS::io::VtkWriter::ASCII)
	      file << "      <PDataArray type=\"Float64\" Name=\"";
	    else 
	      file << "      <PDataArray type=\"Float32\" Name=\"";
	    
	    file << componentNames[i];
	    
	    if(format == ::AMDiS::io::VtkWriter::ASCII)
Praetorius, Simon's avatar
Praetorius, Simon committed
237
	      file << "\" format=\"ascii\"";
238
	    else
Praetorius, Simon's avatar
Praetorius, Simon committed
239
240
241
242
	      file << "\" format=\"appended\"";
	    if (writeAsVector && componentNames.size() > 1)
	      file << " NumberOfComponents=\"" << std::max(3,static_cast<int>(componentNames.size())) << "\"";
	    file << "/>\n"; 
243
244
	  }
	  
Praetorius, Simon's avatar
Praetorius, Simon committed
245
	  
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
	  file << "    </PPointData>\n";

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

	  file << "  </PUnstructuredGrid>\n";
	  file << "</VTKFile>\n";
	}
	
      } // end namespace detail
    } // end namespace VtkWriter
} } // end namespace io, AMDiS