VtkWriter.cc 5.14 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
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
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
65
66
67
68
69
70
71
72
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

#include "VtkWriter.h"
#include "DataCollector.h"
#include "DOFVector.h"
#include "SurfaceRegion_ED.h"
#include "ElementRegion_ED.h"

namespace AMDiS { 
  int VtkWriter::writeFile(::std::vector<DataCollector*> *dc,
			   const char *name)
  {
    ::std::ofstream file;
    ::std::list<ElementInfo> *elements = (*dc)[0]->getElementInfos();

    DOFVector< ::std::list<VertexInfo> > *vertexInfos = (*dc)[0]->getVertexInfos();

    int nv = (*dc)[0]->getNumberVertices();
    int ne = (*dc)[0]->getNumberElements();
    int vertices = (*dc)[0]->getMesh()->getGeo(VERTEX);

    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;
    file << "    <Piece NumberOfPoints=\"" << nv << "\" NumberOfCells=\"" << ne << "\">" << ::std::endl;
    file << "      <Points>" << ::std::endl;
    file << "        <DataArray type=\"Float32\" NumberOfComponents=\"3\" format=\"ascii\">" << ::std::endl;


    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++;
	for (int i = 0; i < Global::getGeo(WORLD); i++) {
	  file << " " << ::std::scientific << it2->coords[i];
	}
	for (int i = Global::getGeo(WORLD); i < 3; i++) {
	  file << " 0.0";
	}
	file << ::std::endl;
      }
    }

    file << "        </DataArray>" << ::std::endl;
    file << "      </Points>" << ::std::endl;
    file << "      <Cells>" << ::std::endl;

    file << "        <DataArray type=\"Int32\" Name=\"offsets\">" << ::std::endl;
    for (int i = 0; i < ne; i++) {
      file << " " << (i + 1) * vertices << ::std::endl;
    }
    file << "        </DataArray>" << ::std::endl;

    file << "        <DataArray type=\"UInt8\" Name=\"types\">" << ::std::endl;
    for (int i = 0; i < ne; i++) {
      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;
      }	   
    }
    file << "        </DataArray>" << ::std::endl;

    file << "        <DataArray type=\"Int32\" Name=\"connectivity\">" << ::std::endl;
    ::std::list<ElementInfo>::iterator elementIt;
    
    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;
    }
    file << "        </DataArray>" << ::std::endl;
    
    file << "      </Cells>" << ::std::endl;
    file << "      <PointData>" << ::std::endl;

    /* ----- write vertex values -----*/
    for (int i = 0; i < static_cast<int>(dc->size()); i++) {
      file << "        <DataArray type=\"Float32\" Name=\"value" << i 
	   << "\" format=\"ascii\">" << ::std::endl;

      DOFVector<int> *interpPointInd = (*dc)[i]->getInterpPointInd();
      DOFVector<double> *values = (*dc)[i]->getValues();
      DOFVector< ::std::list<WorldVector<double> > > *dofCoords = (*dc)[i]->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 j = 0; j < static_cast<int>(coordIt->size()); j++) {
Peter Gottschling's avatar
Peter Gottschling committed
116
		file << " " << (fabs(*valueIt) < 1e-40 ? 0.0 : *valueIt) << "\n";
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
	    }
	  }
	}
      
      file << "        </DataArray>" << ::std::endl;
    }

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

    return 0;
  }
133
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

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