VtkWriter.cc 7.85 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
    ::std::ofstream file;
30
31
32
33
34
    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;
35
36
    file << "    <Piece NumberOfPoints=\"" << nVertices 
	 << "\" NumberOfCells=\"" << nElements << "\">" << ::std::endl;
37
38
39
    file << "      <Points>" << ::std::endl;
    file << "        <DataArray type=\"Float32\" NumberOfComponents=\"3\" format=\"ascii\">" << ::std::endl;

40
    writeVertexCoords(file);
41
42
43
44
45

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

    for (int i = 0; i < nElements; i++) {
48
49
50
      file << " " << (i + 1) * vertices << ::std::endl;
    }

51
    file << "        </DataArray>" << ::std::endl;
52
    file << "        <DataArray type=\"UInt8\" Name=\"types\">" << ::std::endl;
53
54

    for (int i = 0; i < nElements; i++) {
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
      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;
      }	   
    }

70
    file << "        </DataArray>" << ::std::endl;
71
    file << "        <DataArray type=\"Int32\" Name=\"connectivity\">" << ::std::endl;
72

73
    if ((dim_ == 2) && (degree_ == 2)) {
74
75
76
      writeConnectivity_dim2_degree2(file);
    } else {
      writeConnectivity(file);
77
    }
78
79

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

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

    return 0;
  }
101

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

  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);
      }
    }

119
120
121
122
123
124
125
126
127
    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);
	}
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
153
      }
    }
  }


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

154
155
156
157
158
159
160
161
162
163
164
165
    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;
	  }
166
167
168
169
170
171
172
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
	}
      }
    }
  }


  void VtkWriter::writeConnectivity(::std::ofstream &file) 
  {
    ::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;
    } 
  }


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

  }


222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
  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;
  }
252
253


254
}