VtkWriter.cc 12.9 KB
Newer Older
1
#include <stdio.h>
2
3
#include <string>
#include <fstream>
Peter Gottschling's avatar
Peter Gottschling committed
4
#include <cmath>
5
6

#include "VtkWriter.h"
7
#include "Traverse.h"
8
9
10
11
12
13
14
#include "DataCollector.h"
#include "DOFVector.h"
#include "SurfaceRegion_ED.h"
#include "ElementRegion_ED.h"

namespace AMDiS { 

15
16
17
18
19
  int VtkWriter::writeFile(const char *name)
  {
    int nVertices = (*dc_)[0]->getNumberVertices();
    int nElements = (*dc_)[0]->getNumberElements();
    int vertices = (*dc_)[0]->getMesh()->getGeo(VERTEX);
20

21
    if ((dim_ == 2) && (degree_ == 2)) {
22
23
      nVertices += (*dc_)[0]->getNumberInterpPoints();
      nElements *= 4;
24
25
26
    } else if ((dim_ == 2) && (degree_ == 3)) {
      nVertices += (*dc_)[0]->getNumberInterpPoints();
      nElements *= 9;
27
28
29
    } else if ((dim_ == 2) && (degree_ == 4)) {
      nVertices += (*dc_)[0]->getNumberInterpPoints();
      nElements *= 16;
30
    }
31

32
    ::std::ofstream file;
33
34
35
36
37
    file.open(name);

    file << "<?xml version=\"1.0\"?>" << ::std::endl;
    file << "<VTKFile type=\"UnstructuredGrid\" version=\"0.1\" byte_order=\"LittleEndian\">" << ::std::endl;
    file << "  <UnstructuredGrid>" << ::std::endl;
38
39
    file << "    <Piece NumberOfPoints=\"" << nVertices 
	 << "\" NumberOfCells=\"" << nElements << "\">" << ::std::endl;
40
41
42
    file << "      <Points>" << ::std::endl;
    file << "        <DataArray type=\"Float32\" NumberOfComponents=\"3\" format=\"ascii\">" << ::std::endl;

43
    writeVertexCoords(file);
44
45
46
47
48

    file << "        </DataArray>" << ::std::endl;
    file << "      </Points>" << ::std::endl;
    file << "      <Cells>" << ::std::endl;
    file << "        <DataArray type=\"Int32\" Name=\"offsets\">" << ::std::endl;
49
50

    for (int i = 0; i < nElements; i++) {
51
52
53
      file << " " << (i + 1) * vertices << ::std::endl;
    }

54
    file << "        </DataArray>" << ::std::endl;
55
    file << "        <DataArray type=\"UInt8\" Name=\"types\">" << ::std::endl;
56
57

    for (int i = 0; i < nElements; i++) {
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
      switch (vertices) {
      case 2:
	file << " 3" << ::std::endl;
	break;
      case 3: 
	file << " 5" << ::std::endl;
	break;
      case 4:
	file << " 10" << ::std::endl;
	break;		    
      default:
	break;
      }	   
    }

73
    file << "        </DataArray>" << ::std::endl;
74
    file << "        <DataArray type=\"Int32\" Name=\"connectivity\">" << ::std::endl;
75

76
77
    writeConnectivity(file);
    
78
    file << "        </DataArray>" << ::std::endl;    
79
80
    file << "      </Cells>" << ::std::endl;
    file << "      <PointData>" << ::std::endl;
81
82
    
    for (int i = 0; i < static_cast<int>(dc_->size()); i++) {
83
84
85
      file << "        <DataArray type=\"Float32\" Name=\"value" << i 
	   << "\" format=\"ascii\">" << ::std::endl;
      
86
87
      writeVertexValues(file, i);
      
88
89
90
91
92
93
94
95
96
97
98
99
      file << "        </DataArray>" << ::std::endl;
    }

    file << "      </PointData>" << ::std::endl;
    file << "    </Piece>" << ::std::endl;
    file << "  </UnstructuredGrid>" << ::std::endl;
    file << "</VTKFile>" << ::std::endl;
    
    file.close();

    return 0;
  }
100

101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117

  void VtkWriter::writeVertexCoords(::std::ofstream &file)
  {
    DOFVector< ::std::list<VertexInfo> > *vertexInfos = (*dc_)[0]->getVertexInfos();
    DOFVector< ::std::list<VertexInfo> >::Iterator it(vertexInfos, USED_DOFS);
    int counter = 0;

    // for all DOFs
    for (it.reset(); !it.end(); ++it) {
      // for all vertex infos of this DOF
      ::std::list<VertexInfo>::iterator it2;
      for (it2 = it->begin(); it2 != it->end(); ++it2) {
	it2->outputIndex = counter++;
	writeCoord(file, it2->coords);
      }
    }

118
119
120
121
122
123
124
125
126
    if ((dim_ == 2) && (degree_ > 1)) {
      DOFVector< ::std::list< WorldVector<double> > > *interpPointCoords = (*dc_)[0]->getInterpPointCoords();
      DOFVector< ::std::list< WorldVector<double> > >::Iterator pointIt(interpPointCoords, USED_DOFS);
      
      for (pointIt.reset(); !pointIt.end(); ++pointIt) {
	::std::list< WorldVector<double> >::iterator it2;
	for (it2 = pointIt->begin(); it2 != pointIt->end(); ++it2) {
	  writeCoord(file, *it2);
	}
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
      }
    }
  }


  void VtkWriter::writeVertexValues(::std::ofstream &file, int componentNo)
  {    
    DOFVector<int> *interpPointInd = (*dc_)[componentNo]->getInterpPointInd();
    DOFVector<double> *values = (*dc_)[componentNo]->getValues();
    DOFVector< ::std::list<WorldVector<double> > > *dofCoords = (*dc_)[componentNo]->getDofCoords();
    
    DOFVector<int>::Iterator intPointIt(interpPointInd, USED_DOFS);
    DOFVector<double>::Iterator valueIt(values, USED_DOFS);
    DOFVector< ::std::list<WorldVector<double> > >::Iterator coordIt(dofCoords, USED_DOFS);
      
    for (intPointIt.reset(), valueIt.reset(), coordIt.reset();
	 !intPointIt.end(); 
	 ++intPointIt, ++valueIt, ++coordIt) {

      if (*intPointIt == -2) {
	for (int i = 0; i < static_cast<int>(coordIt->size()); i++) {
	  file << " " << (fabs(*valueIt) < 1e-40 ? 0.0 : *valueIt) << ::std::endl;
	}
      }
    }        

153
154
155
156
157
158
159
160
161
162
163
164
    if ((dim_ == 2) && (degree_ > 1)) {
      DOFVector< ::std::list<WorldVector<double> > >::Iterator 
	interpCoordIt((*dc_)[componentNo]->getInterpPointCoords(), USED_DOFS);
    
      for (intPointIt.reset(), valueIt.reset(), interpCoordIt.reset();
	   !intPointIt.end(); 
	   ++intPointIt, ++valueIt, ++interpCoordIt) {      
	
	if (*intPointIt >= 0) {
	  for (int i = 0; i < static_cast<int>(interpCoordIt->size()); i++) {
	    file << " " << (fabs(*valueIt) < 1e-40 ? 0.0 : *valueIt) << ::std::endl;
	  }
165
166
167
168
169
170
171
172
	}
      }
    }
  }


  void VtkWriter::writeConnectivity(::std::ofstream &file) 
  {
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
    if ((dim_ == 2) && (degree_ == 2)) {
      writeConnectivity_dim2_degree2(file);
    } else if ((dim_ == 2) && (degree_ == 3)) {
      writeConnectivity_dim2_degree3(file);
    } else if ((dim_ == 2) && (degree_ == 4)) {
      writeConnectivity_dim2_degree4(file);   
    } else {
      ::std::list<ElementInfo> *elements = (*dc_)[0]->getElementInfos();
      ::std::list<ElementInfo>::iterator elementIt;
      int vertices = (*dc_)[0]->getMesh()->getGeo(VERTEX);
      
      for (elementIt = elements->begin(); elementIt != elements->end(); ++elementIt) {
	// for all vertices
	for (int i = 0; i < vertices; i++) {
	  file << " " << elementIt->vertexInfo[i]->outputIndex;
	}
	file << ::std::endl;
      } 
    }
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
  }


  void VtkWriter::writeConnectivity_dim2_degree2(::std::ofstream &file)
  {  
    ::std::list<ElementInfo> *elements = (*dc_)[0]->getElementInfos();
    ::std::list<ElementInfo>::iterator elementIt;

    ::std::vector< ::std::vector<int> > *interpPoints = (*dc_)[0]->getInterpPoints();
    ::std::vector< ::std::vector<int> >::iterator pointIt;

    int nVertices = (*dc_)[0]->getNumberVertices();

    for (pointIt = interpPoints->begin(), elementIt = elements->begin(); 
	 pointIt != interpPoints->end(); 
	 ++pointIt, ++elementIt) {
    
      file << " " << elementIt->vertexInfo[0]->outputIndex
	   << " " << (*pointIt)[1] + nVertices
	   << " " << (*pointIt)[2] + nVertices << ::std::endl;

      file << " " << elementIt->vertexInfo[2]->outputIndex 
	   << " " << (*pointIt)[0] + nVertices
	   << " " << (*pointIt)[1] + nVertices << ::std::endl;

      file << " " << elementIt->vertexInfo[1]->outputIndex
	   << " " << (*pointIt)[0] + nVertices
	   << " " << (*pointIt)[2] + nVertices << ::std::endl;

      file << " " << (*pointIt)[0] + nVertices
	   << " " << (*pointIt)[1] + nVertices 
	   << " " << (*pointIt)[2] + nVertices << ::std::endl;
    }

  }


229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
  void VtkWriter::writeConnectivity_dim2_degree3(::std::ofstream &file)
  {  
    ::std::list<ElementInfo> *elements = (*dc_)[0]->getElementInfos();
    ::std::list<ElementInfo>::iterator elementIt;

    ::std::vector< ::std::vector<int> > *interpPoints = (*dc_)[0]->getInterpPoints();
    ::std::vector< ::std::vector<int> >::iterator pointIt;

    int nVertices = (*dc_)[0]->getNumberVertices();

    for (pointIt = interpPoints->begin(), elementIt = elements->begin(); 
	 pointIt != interpPoints->end(); 
	 ++pointIt, ++elementIt) {
                          
      file << " " << elementIt->vertexInfo[0]->outputIndex
	   << " " << (*pointIt)[3] + nVertices
	   << " " << (*pointIt)[4] + nVertices << ::std::endl;

      file << " " << (*pointIt)[4] + nVertices
	   << " " << (*pointIt)[5] + nVertices
	   << " " << (*pointIt)[6] + nVertices << ::std::endl;

      file << " " << (*pointIt)[3] + nVertices
	   << " " << (*pointIt)[4] + nVertices
	   << " " << (*pointIt)[6] + nVertices << ::std::endl;

      file << " " << (*pointIt)[2] + nVertices
	   << " " << (*pointIt)[3] + nVertices
	   << " " << (*pointIt)[6] + nVertices << ::std::endl;

      file << " " << elementIt->vertexInfo[1]->outputIndex
	   << " " << (*pointIt)[0] + nVertices
	   << " " << (*pointIt)[5] + nVertices << ::std::endl;

      file << " " << (*pointIt)[0] + nVertices
	   << " " << (*pointIt)[6] + nVertices
	   << " " << (*pointIt)[5] + nVertices << ::std::endl;

      file << " " << (*pointIt)[0] + nVertices
	   << " " << (*pointIt)[1] + nVertices
	   << " " << (*pointIt)[6] + nVertices << ::std::endl;

      file << " " << (*pointIt)[1] + nVertices
	   << " " << (*pointIt)[2] + nVertices
	   << " " << (*pointIt)[6] + nVertices << ::std::endl;

      file << " " << elementIt->vertexInfo[2]->outputIndex 
	   << " " << (*pointIt)[1] + nVertices
	   << " " << (*pointIt)[2] + nVertices << ::std::endl;
     
      
    }

  }


  void VtkWriter::writeConnectivity_dim2_degree4(::std::ofstream &file)
  {
    ::std::list<ElementInfo> *elements = (*dc_)[0]->getElementInfos();
    ::std::list<ElementInfo>::iterator elementIt;

    ::std::vector< ::std::vector<int> > *interpPoints = (*dc_)[0]->getInterpPoints();
    ::std::vector< ::std::vector<int> >::iterator pointIt;

    int nVertices = (*dc_)[0]->getNumberVertices();

    for (pointIt = interpPoints->begin(), elementIt = elements->begin(); 
	 pointIt != interpPoints->end(); 
	 ++pointIt, ++elementIt) {

      file << " " << elementIt->vertexInfo[0]->outputIndex 
	   << " " << (*pointIt)[5] + nVertices
	   << " " << (*pointIt)[6] + nVertices << ::std::endl;

      file << " " << (*pointIt)[5] + nVertices
	   << " " << (*pointIt)[9] + nVertices
	   << " " << (*pointIt)[6] + nVertices << ::std::endl;

      file << " " << (*pointIt)[6] + nVertices
	   << " " << (*pointIt)[7] + nVertices
	   << " " << (*pointIt)[9] + nVertices << ::std::endl;

      file << " " << (*pointIt)[7] + nVertices
	   << " " << (*pointIt)[9] + nVertices
	   << " " << (*pointIt)[10] + nVertices << ::std::endl;

      file << " " << (*pointIt)[7] + nVertices
	   << " " << (*pointIt)[8] + nVertices
	   << " " << (*pointIt)[10] + nVertices << ::std::endl;

      file << " " << (*pointIt)[0] + nVertices
	   << " " << (*pointIt)[8] + nVertices
	   << " " << (*pointIt)[10] + nVertices << ::std::endl;

      file << " " << elementIt->vertexInfo[1]->outputIndex 
	   << " " << (*pointIt)[0] + nVertices
	   << " " << (*pointIt)[8] + nVertices << ::std::endl;

      file << " " << (*pointIt)[4] + nVertices
	   << " " << (*pointIt)[5] + nVertices
	   << " " << (*pointIt)[9] + nVertices << ::std::endl;

      file << " " << (*pointIt)[4] + nVertices
	   << " " << (*pointIt)[9] + nVertices
	   << " " << (*pointIt)[11] + nVertices << ::std::endl;

      file << " " << (*pointIt)[9] + nVertices
	   << " " << (*pointIt)[10] + nVertices
	   << " " << (*pointIt)[11] + nVertices << ::std::endl;

      file << " " << (*pointIt)[1] + nVertices
	   << " " << (*pointIt)[10] + nVertices
	   << " " << (*pointIt)[11] + nVertices << ::std::endl;

      file << " " << (*pointIt)[0] + nVertices
	   << " " << (*pointIt)[1] + nVertices
	   << " " << (*pointIt)[10] + nVertices << ::std::endl;

      file << " " << (*pointIt)[3] + nVertices
	   << " " << (*pointIt)[4] + nVertices
	   << " " << (*pointIt)[11] + nVertices << ::std::endl;

      file << " " << (*pointIt)[2] + nVertices
	   << " " << (*pointIt)[3] + nVertices
	   << " " << (*pointIt)[11] + nVertices << ::std::endl;

      file << " " << (*pointIt)[1] + nVertices
	   << " " << (*pointIt)[2] + nVertices
	   << " " << (*pointIt)[11] + nVertices << ::std::endl;

      file << " " << elementIt->vertexInfo[2]->outputIndex 
	   << " " << (*pointIt)[2] + nVertices
	   << " " << (*pointIt)[3] + nVertices << ::std::endl;

    }
  }

366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
  int VtkWriter::updateAnimationFile(::std::string valueFilename,
				     ::std::vector< ::std::string > *paraViewAnimationFrames,
				     const char *animationFilename)
  {
    size_t found = valueFilename.find_last_of("/\\");
    paraViewAnimationFrames->push_back(valueFilename.substr(found + 1));

    ::std::ofstream file;
    file.open(animationFilename);

    file << "<?xml version=\"1.0\"?>" << ::std::endl;
    file << "<VTKFile type=\"Collection\" version=\"0.1\" >"  << ::std::endl;
    file << "<Collection>" << ::std::endl;

    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) << "\"/>" << ::std::endl;      
    }

    file << "</Collection>" << ::std::endl;
    file << "</VTKFile>" << ::std::endl;

    file.close();
    
    return 0;
  }
396
397


398
}