VtkWriter.hh 12.1 KB
Newer Older
Thomas Witkowski's avatar
Thomas Witkowski committed
1
2
3
#include <list>
#include <vector>
#include "DOFVector.h"
4
#include "mpi.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
5
6
7
8

namespace AMDiS {

  template<typename T>
9
  void VtkWriter::writeFileToStream(T &file)
Thomas Witkowski's avatar
Thomas Witkowski committed
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
  {
    int nVertices = (*dataCollector)[0]->getNumberVertices();
    int nElements = (*dataCollector)[0]->getNumberElements();
    int vertices = (*dataCollector)[0]->getMesh()->getGeo(VERTEX);

    if ((dim == 2) && (degree == 2)) {
      nVertices += (*dataCollector)[0]->getNumberInterpPoints();
      nElements *= 4;
    } else if ((dim == 2) && (degree == 3)) {
      nVertices += (*dataCollector)[0]->getNumberInterpPoints();
      nElements *= 9;
    } else if ((dim == 2) && (degree == 4)) {
      nVertices += (*dataCollector)[0]->getNumberInterpPoints();
      nElements *= 16;
    }
    file << "<?xml version=\"1.0\"?>\n";
    file << "<VTKFile type=\"UnstructuredGrid\" version=\"0.1\" byte_order=\"LittleEndian\">\n";
    file << "  <UnstructuredGrid>\n";
    file << "    <Piece NumberOfPoints=\"" << nVertices 
	 << "\" NumberOfCells=\"" << nElements << "\">\n";
    file << "      <Points>\n";
    file << "        <DataArray type=\"Float32\" NumberOfComponents=\"3\" format=\"ascii\">\n";
    
    writeVertexCoords(file);
    
    file << "        </DataArray>\n";
    file << "      </Points>\n";
    file << "      <Cells>\n";
    file << "        <DataArray type=\"Int32\" Name=\"offsets\">\n";
    
    for (int i = 0; i < nElements; i++) {
      file << " " << (i + 1) * vertices << "\n";
    }

    file << "        </DataArray>\n";
    file << "        <DataArray type=\"UInt8\" Name=\"types\">\n";

    for (int i = 0; i < nElements; i++) {
      switch (vertices) {
      case 2:
	file << " 3\n";
	break;
      case 3: 
	file << " 5\n";
	break;
      case 4:
	file << " 10\n";
	break;		    
      default:
	break;
      }	   
    }

    file << "        </DataArray>\n";
    file << "        <DataArray type=\"Int32\" Name=\"connectivity\">\n";
65

Thomas Witkowski's avatar
Thomas Witkowski committed
66
67
68
69
70
71
72
73
74
    writeConnectivity(file);
    
    file << "        </DataArray>\n";    
    file << "      </Cells>\n";
    file << "      <PointData>\n";
    
    for (int i = 0; i < static_cast<int>(dataCollector->size()); i++) {
      file << "        <DataArray type=\"Float32\" Name=\"value" << i 
	   << "\" format=\"ascii\">\n";
75

Thomas Witkowski's avatar
Thomas Witkowski committed
76
77
78
79
80
81
82
83
84
85
86
87
      writeVertexValues(file, i);
      
      file << "        </DataArray>\n";
    }
    
    file << "      </PointData>\n";
    file << "    </Piece>\n";
    file << "  </UnstructuredGrid>\n";
    file << "</VTKFile>\n";
  }
  
  template<typename T>
Thomas Witkowski's avatar
Thomas Witkowski committed
88
  void VtkWriter::writeVertexCoords(T &file)
Thomas Witkowski's avatar
Thomas Witkowski committed
89
90
91
92
93
94
95
96
97
98
99
100
  {
    DOFVector< std::list<VertexInfo> > *vertexInfos = (*dataCollector)[0]->getVertexInfos();
    DOFVector< std::list<VertexInfo> >::Iterator it(vertexInfos, USED_DOFS);
    int counter = 0;

    // For all DOFs of vertices, write the coordinates.
    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);
101
      }     
Thomas Witkowski's avatar
Thomas Witkowski committed
102
103
104
105
106
107
108
109
110
    }

    // For the second dim case, write also the interpolation points.
    if ((dim == 2) && (degree > 1)) {
      DOFVector< std::list< WorldVector<double> > > *interpPointCoords = (*dataCollector)[0]->getInterpPointCoords();
      DOFVector< std::list< WorldVector<double> > >::Iterator pointIt(interpPointCoords, USED_DOFS);
      
      for (pointIt.reset(); !pointIt.end(); ++pointIt) {
	std::list< WorldVector<double> >::iterator it2;
111
	for (it2 = pointIt->begin(); it2 != pointIt->end(); ++it2)
Thomas Witkowski's avatar
Thomas Witkowski committed
112
113
114
115
116
117
	  writeCoord(file, *it2);
      }
    }
  }

  template<typename T>
Thomas Witkowski's avatar
Thomas Witkowski committed
118
  void VtkWriter::writeVertexValues(T &file, int componentNo)
Thomas Witkowski's avatar
Thomas Witkowski committed
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
  {
    //    DOFVector<int> *interpPointInd;
    //    DOFVector<double> *values;
    //    DOFVector< std::list<WorldVector<double> > > *dofCoords;

    DOFVector<int> *interpPointInd = (*dataCollector)[componentNo]->getInterpPointInd();
    DOFVector<double> *values = (*dataCollector)[componentNo]->getValues();
    DOFVector< std::list<WorldVector<double> > > *dofCoords = (*dataCollector)[componentNo]->getDofCoords();

    /*
#ifdef _OPENMP
#pragma omp critical 
#endif
    {   
      interpPointInd = (*dataCollector)[componentNo]->getInterpPointInd();
      values = (*dataCollector)[componentNo]->getValues();
      dofCoords = (*dataCollector)[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);
141
142

    file << std::fixed;
143
         
Thomas Witkowski's avatar
Thomas Witkowski committed
144
145
146
147
148
    // Write the values for all vertex DOFs.
    for (intPointIt.reset(), valueIt.reset(), coordIt.reset();
	 !intPointIt.end(); 
	 ++intPointIt, ++valueIt, ++coordIt) {

149
150
151
      if (*intPointIt == -2) 
	for (int i = 0; i < static_cast<int>(coordIt->size()); i++) 
	  file << " " << (fabs(*valueIt) < 1e-40 ? 0.0 : *valueIt) << "\n";      
Thomas Witkowski's avatar
Thomas Witkowski committed
152
153
154
155
156
157
158
159
160
161
162
163
    }        

    // For the second dim case, write also the values of the interpolation points.
    if ((dim == 2) && (degree > 1)) {
      DOFVector< std::list<WorldVector<double> > >::Iterator 
	interpCoordIt((*dataCollector)[componentNo]->getInterpPointCoords(), USED_DOFS);
    
      for (intPointIt.reset(), valueIt.reset(), interpCoordIt.reset();
	   !intPointIt.end(); 
	   ++intPointIt, ++valueIt, ++interpCoordIt) {      
	
	if (*intPointIt >= 0) {
164
	  for (int i = 0; i < static_cast<int>(interpCoordIt->size()); i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
165
166
167
168
169
170
171
	    file << " " << (fabs(*valueIt) < 1e-40 ? 0.0 : *valueIt) << "\n";
	}
      }
    }    
  }
  
  template<typename T>
Thomas Witkowski's avatar
Thomas Witkowski committed
172
  void VtkWriter::writeConnectivity(T &file)
Thomas Witkowski's avatar
Thomas Witkowski committed
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
  {
    // For the second dim case, and if higher order Lagrange elements are used,
    // write the connectivity by extra functions.
    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 = (*dataCollector)[0]->getElementInfos();
      std::list<ElementInfo>::iterator elementIt;
      int vertices = (*dataCollector)[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 << "\n";
      } 
    }
  }
  
  template<typename T>
Thomas Witkowski's avatar
Thomas Witkowski committed
198
  void VtkWriter::writeConnectivity_dim2_degree2(T &file)
Thomas Witkowski's avatar
Thomas Witkowski committed
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
229
230
  {
    std::list<ElementInfo> *elements = (*dataCollector)[0]->getElementInfos();
    std::list<ElementInfo>::iterator elementIt;

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

    int nVertices = (*dataCollector)[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 << "\n";

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

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

      file << " " << (*pointIt)[0] + nVertices
	   << " " << (*pointIt)[1] + nVertices 
	   << " " << (*pointIt)[2] + nVertices << "\n";
    }
  }

  template<typename T>
Thomas Witkowski's avatar
Thomas Witkowski committed
231
  void VtkWriter::writeConnectivity_dim2_degree3(T &file)
Thomas Witkowski's avatar
Thomas Witkowski committed
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
  {
    std::list<ElementInfo> *elements = (*dataCollector)[0]->getElementInfos();
    std::list<ElementInfo>::iterator elementIt;

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

    int nVertices = (*dataCollector)[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 << "\n";

      file << " " << (*pointIt)[4] + nVertices
	   << " " << (*pointIt)[5] + nVertices
	   << " " << (*pointIt)[6] + nVertices << "\n";

      file << " " << (*pointIt)[3] + nVertices
	   << " " << (*pointIt)[4] + nVertices
	   << " " << (*pointIt)[6] + nVertices << "\n";

      file << " " << (*pointIt)[2] + nVertices
	   << " " << (*pointIt)[3] + nVertices
	   << " " << (*pointIt)[6] + nVertices << "\n";

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

      file << " " << (*pointIt)[0] + nVertices
	   << " " << (*pointIt)[6] + nVertices
	   << " " << (*pointIt)[5] + nVertices << "\n";

      file << " " << (*pointIt)[0] + nVertices
	   << " " << (*pointIt)[1] + nVertices
	   << " " << (*pointIt)[6] + nVertices << "\n";

      file << " " << (*pointIt)[1] + nVertices
	   << " " << (*pointIt)[2] + nVertices
	   << " " << (*pointIt)[6] + nVertices << "\n";

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

  template<typename T>
Thomas Witkowski's avatar
Thomas Witkowski committed
286
  void VtkWriter::writeConnectivity_dim2_degree4(T &file)
Thomas Witkowski's avatar
Thomas Witkowski committed
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
366
367
  {
    std::list<ElementInfo> *elements = (*dataCollector)[0]->getElementInfos();
    std::list<ElementInfo>::iterator elementIt;

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

    int nVertices = (*dataCollector)[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 << "\n";

      file << " " << (*pointIt)[5] + nVertices
	   << " " << (*pointIt)[9] + nVertices
	   << " " << (*pointIt)[6] + nVertices << "\n";

      file << " " << (*pointIt)[6] + nVertices
	   << " " << (*pointIt)[7] + nVertices
	   << " " << (*pointIt)[9] + nVertices << "\n";

      file << " " << (*pointIt)[7] + nVertices
	   << " " << (*pointIt)[9] + nVertices
	   << " " << (*pointIt)[10] + nVertices << "\n";

      file << " " << (*pointIt)[7] + nVertices
	   << " " << (*pointIt)[8] + nVertices
	   << " " << (*pointIt)[10] + nVertices << "\n";

      file << " " << (*pointIt)[0] + nVertices
	   << " " << (*pointIt)[8] + nVertices
	   << " " << (*pointIt)[10] + nVertices << "\n";

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

      file << " " << (*pointIt)[4] + nVertices
	   << " " << (*pointIt)[5] + nVertices
	   << " " << (*pointIt)[9] + nVertices << "\n";

      file << " " << (*pointIt)[4] + nVertices
	   << " " << (*pointIt)[9] + nVertices
	   << " " << (*pointIt)[11] + nVertices << "\n";

      file << " " << (*pointIt)[9] + nVertices
	   << " " << (*pointIt)[10] + nVertices
	   << " " << (*pointIt)[11] + nVertices << "\n";

      file << " " << (*pointIt)[1] + nVertices
	   << " " << (*pointIt)[10] + nVertices
	   << " " << (*pointIt)[11] + nVertices << "\n";

      file << " " << (*pointIt)[0] + nVertices
	   << " " << (*pointIt)[1] + nVertices
	   << " " << (*pointIt)[10] + nVertices << "\n";

      file << " " << (*pointIt)[3] + nVertices
	   << " " << (*pointIt)[4] + nVertices
	   << " " << (*pointIt)[11] + nVertices << "\n";

      file << " " << (*pointIt)[2] + nVertices
	   << " " << (*pointIt)[3] + nVertices
	   << " " << (*pointIt)[11] + nVertices << "\n";

      file << " " << (*pointIt)[1] + nVertices
	   << " " << (*pointIt)[2] + nVertices
	   << " " << (*pointIt)[11] + nVertices << "\n";

      file << " " << elementIt->vertexInfo[2]->outputIndex 
	   << " " << (*pointIt)[2] + nVertices
	   << " " << (*pointIt)[3] + nVertices << "\n";

    }
  }

}