ElementFileWriter.cc 8.97 KB
Newer Older
1
2
3
4
5
6
#include "ElementFileWriter.h"
#include "BasisFunction.h"
#include "Parameters.h"
#include "Traverse.h"
#include "AdaptInfo.h"

7
8
namespace AMDiS {

9
  ElementFileWriter::ElementFileWriter(std::string name_, 
10
				       Mesh *mesh_,
Thomas Witkowski's avatar
Thomas Witkowski committed
11
				       std::map<int, double> &mapvec)
12
13
14
15
16
17
18
19
20
21
    : name(name_),
      amdisMeshDatExt(".elem.mesh"),
      vtkExt(".vtu"),
      writeAMDiSFormat(0),
      writeVtkFormat(0),
      appendIndex(0),
      indexLength(5),
      indexDecimals(3),
      tsModulo(1),
      timestepNumber(-1),
22
      mesh(mesh_),
Thomas Witkowski's avatar
Thomas Witkowski committed
23
      vec(mapvec)
24
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
25
26
27
28
29
30
31
32
33
34
    if (name != "") {
      GET_PARAMETER(0, name + "->output->filename", &filename);
      GET_PARAMETER(0, name + "->output->AMDiS format", "%d", &writeAMDiSFormat);
      GET_PARAMETER(0, name + "->output->AMDiS mesh-dat ext", &amdisMeshDatExt);
      GET_PARAMETER(0, name + "->output->ParaView format", "%d", &writeVtkFormat);
      GET_PARAMETER(0, name + "->output->append index", "%d", &appendIndex);
      GET_PARAMETER(0, name + "->output->index length", "%d", &indexLength);
      GET_PARAMETER(0, name + "->output->index decimals", "%d", &indexDecimals);
      GET_PARAMETER(0, name + "->output->write every i-th timestep", "%d", &tsModulo);
    }
35
  }
36

37

38
  void ElementFileWriter::writeFiles(AdaptInfo *adaptInfo, bool force,
Thomas Witkowski's avatar
Thomas Witkowski committed
39
40
				     int level, Flag traverseFlag, 
				     bool (*writeElem)(ElInfo*))
41
42
  {
    FUNCNAME("ElementFileWriter::writeFiles()");
43

44
45
    timestepNumber++;
    timestepNumber %= tsModulo;
46

47
    if (timestepNumber != 0 && !force) 
48
      return;
49

50
    std::string fn = filename;
51

52
53
54
55
    if (appendIndex) {
      TEST_EXIT(indexLength <= 99)("index lenght > 99\n");
      TEST_EXIT(indexDecimals <= 97)("index decimals > 97\n");
      TEST_EXIT(indexDecimals < indexLength)("index length <= index decimals\n");
56
    
57
58
      char formatStr[9];
      sprintf(formatStr, "%%0%d.%df", indexLength, indexDecimals);
59

60
61
      char timeStr[20];
      sprintf(timeStr, formatStr, adaptInfo ? adaptInfo->getTime() : 0.0);
62

63
64
      fn += timeStr;
    }
65

66
67
    if (writeAMDiSFormat) {
      TEST_EXIT(mesh)("no mesh\n");
68
    
69
      writeMeshDatValues(const_cast<char*>((fn + amdisMeshDatExt).c_str()),
70
71
72
			 adaptInfo ? adaptInfo->getTime() : 0.0);
      MSG("MeshDat file written to %s\n", (fn + amdisMeshDatExt).c_str());
    }
73

74
75
    if (writeVtkFormat) {
      TEST_EXIT(mesh)("no mesh\n");
76

77
      writeVtkValues(const_cast<char*>((fn + vtkExt).c_str()));
78
79
      MSG("VTK file written to %s\n", (fn + vtkExt).c_str());		     
    }
80
81
  }

82

Thomas Witkowski's avatar
Thomas Witkowski committed
83
  void ElementFileWriter::writeFile(std::map<int, double> &vec,
84
				    Mesh *mesh,
85
86
				    std::string filename,
				    int level)
Thomas Witkowski's avatar
Thomas Witkowski committed
87
  {
88
    ElementFileWriter efw("", mesh, vec);
89
    efw.writeVtkValues(filename, level);
Thomas Witkowski's avatar
Thomas Witkowski committed
90
91
92
  }
  

93
  void ElementFileWriter::writeMeshDatValues(std::string filename, double time)
94
95
  {
    FUNCNAME("ElementFileWriter::writeMeshDatValues()");
Thomas Witkowski's avatar
Thomas Witkowski committed
96
    std::ofstream fout(filename.c_str());
97

Thomas Witkowski's avatar
Thomas Witkowski committed
98
    TEST_EXIT(fout)("Could not open file %s !\n", filename.c_str());
99

100
101
    int dim = mesh->getDim();
    double val;
102

103
104
105
106
107
108
109
    // === Write header. ===
    fout << "mesh name: " << mesh->getName().c_str() << "\n\n";
    fout << "time: " << time << "\n\n"; 
    fout << "DIM: " << dim << "\n";
    fout << "DIM_OF_WORLD: " << Global::getGeo(WORLD) << "\n\n";
    fout << "number of vertices: " << (dim+1)*mesh->getNumberOfLeaves() << "\n";
    fout << "number of elements: " << mesh->getNumberOfLeaves() << "\n\n";
110
111


112
113
114
    // === Write vertex coordinates (every vertex for every element). ===
    fout << "vertex coordinates:\n";
    TraverseStack stack;
115

116
117
118
119
    ElInfo *elInfo = stack.traverseFirst(mesh,
					 -1, 
					 Mesh::CALL_LEAF_EL | 
					 Mesh::FILL_COORDS);
120

121
    while (elInfo) {
122

123
124
125
126
127
128
129
130
131
132
      // Write coordinates of all element vertices.
      for (int i = 0; i <= dim; ++i) {     
	for (int j = 0; j < dim; ++j) {
	  fout << elInfo->getCoord(i)[j] << " ";
	}
	fout << "\n";
      }
    
      elInfo = stack.traverseNext(elInfo);
    }
133
134


135
136
137
138
139
140
141
142
143
    // === Write elements. ===
    int numLeaves = mesh->getNumberOfLeaves();
    int vertCntr = 0;
    fout << "\n";
    fout << "element vertices:\n";
    for (int i = 0; i < numLeaves; ++i) {
      for (int j = 0; j <= dim; ++j) {
	fout << vertCntr << " ";
	++vertCntr;
144
145
146
147
148
      }
      fout << "\n";
    }


149
    // === Write values. ===
150

151
    // Write values header.
152
    fout << "\n";
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
    fout << "number of values: 1\n\n";
    fout << "value description: " << name.c_str() << "\n";
    fout << "number of interpolation points: 0" << "\n";
    fout << "type: scalar" << "\n";
    fout << "interpolation type: lagrange" << "\n";
    fout << "interpolation degree: 1" << "\n";
    fout << "end of description: " << name.c_str() << "\n\n";

    // Write values.
    fout << "vertex values: " << name.c_str() << "\n";

    fout.setf(std::ios::scientific,std::ios::floatfield);

    elInfo = stack.traverseFirst(mesh,
				 -1, 
				 Mesh::CALL_LEAF_EL);

    while (elInfo) {
      // Get element value.
      val = vec[elInfo->getElement()->getIndex()];
    
      // Write value for each vertex of each element.
      for (int i = 0; i <= dim; ++i) {      
	fout << val << "\n";
      }
    
      elInfo = stack.traverseNext(elInfo);
    }  // end of: mesh traverse
181
182


183
184
185
186
    // Write values trailor.
    fout << "\n";
    fout << "interpolation values: " << name.c_str() << "\n\n\n";
    fout << "element interpolation points: " << name.c_str() << "\n";
187
188


189
190
    fout.close();
  }
191

192

193
  void ElementFileWriter::writeVtkValues(std::string filename, int level)
194
195
  {
    FUNCNAME("ElementFileWriter::writeVtkValues()");
Thomas Witkowski's avatar
Thomas Witkowski committed
196
    std::ofstream fout(filename.c_str());
197

Thomas Witkowski's avatar
Thomas Witkowski committed
198
    TEST_EXIT(fout)("Could not open file %s !\n", filename.c_str());
199
200

    int dim = mesh->getDim();
201
    int dimOfWorld = Global::getGeo(WORLD);
202
203
    int vertices = mesh->getGeo(VERTEX);
    int nElements = mesh->getNumberOfLeaves();
204
205
206
207
208
209
210
211
212
213
214
215
    Flag traverseFlag = level == -1 ? Mesh::CALL_LEAF_EL : Mesh::CALL_EL_LEVEL;

    if (level != -1) {
      nElements = 0;

      TraverseStack stack;
      ElInfo *elInfo = stack.traverseFirst(mesh, level, Mesh::CALL_EL_LEVEL);      
      while (elInfo) {
	nElements++;
	elInfo = stack.traverseNext(elInfo);
      }
    }
216

217
218
219
    fout << "<?xml version=\"1.0\"?>" << std::endl;
    fout << "<VTKFile type=\"UnstructuredGrid\" version=\"0.1\" byte_order=\"LittleEndian\">" << std::endl;
    fout << "  <UnstructuredGrid>" << std::endl;
220
    fout << "    <Piece NumberOfPoints=\"" << (dim + 1) * nElements
221
222
223
	 << "\" NumberOfCells=\"" <<  nElements << "\">" << std::endl;
    fout << "      <Points>" << std::endl;
    fout << "        <DataArray type=\"Float32\" NumberOfComponents=\"3\" format=\"ascii\">" << std::endl;
224
225
226
227


    // === Write vertex coordinates (every vertex for every element). ===
    TraverseStack stack;
228
    ElInfo *elInfo = 
229
      stack.traverseFirst(mesh, level, traverseFlag | Mesh::FILL_COORDS);
230
231
232
233

    while (elInfo) {
      // Write coordinates of all element vertices.
      for (int i = 0; i <= dim; i++) {     
234
	for (int j = 0; j < dimOfWorld; j++) 
235
	  fout << elInfo->getCoord(i)[j] << " ";
236
237
	
	for (int j = dimOfWorld; j < 3; j++) 
238
	  fout << "0.0";
239
	
240
241
242
243
244
	fout << "\n";
      }
   
      elInfo = stack.traverseNext(elInfo);
    }
245

246
247
248
    fout << "        </DataArray>" << std::endl;
    fout << "      </Points>" << std::endl;
    fout << "      <Cells>" << std::endl;
249

250
    fout << "        <DataArray type=\"Int32\" Name=\"offsets\">" << std::endl;
251
252
    for (int i = 0; i < nElements; i++) 
      fout << " " << (i + 1) * vertices << std::endl;    
253
    fout << "        </DataArray>" << std::endl;
254
255


256
    fout << "        <DataArray type=\"UInt8\" Name=\"types\">" << std::endl;
257
258
259
    for (int i = 0; i < nElements; i++) {
      switch (vertices) {
      case 2:
260
	fout << " 3" << std::endl;
261
262
	break;
      case 3: 
263
	fout << " 5" << std::endl;
264
265
	break;
      case 4:
266
	fout << " 10" << std::endl;
267
268
269
270
271
	break;		    
      default:
	break;
      }	   
    }
272
    fout << "        </DataArray>" << std::endl;
273

274
    fout << "        <DataArray type=\"Int32\" Name=\"connectivity\">" << std::endl;
275
276
277
278
279
280
    int vertCntr = 0;
    for (int i = 0; i < nElements; ++i) {
      for (int j = 0; j <= dim; ++j) {
	fout << vertCntr << " ";
	++vertCntr;
      }
281
      fout << std::endl;
282
    }
283
    fout << "        </DataArray>" << std::endl;
284
    
285
286
287
    fout << "      </Cells>" << std::endl;
    fout << "      <PointData>" << std::endl;
    fout << "        <DataArray type=\"Float32\" Name=\"value\" format=\"ascii\">" << std::endl;
288
289
290

    fout.setf(std::ios::scientific,std::ios::floatfield);

291
    elInfo = stack.traverseFirst(mesh, level, traverseFlag | Mesh::FILL_COORDS);
292
293
294
    int vc = 0;
    while (elInfo) {
      // Get element value.
295
      double val = vec[elInfo->getElement()->getIndex()];
296
   
297
      // Write value for each vertex of each element.
298
      for (int i = 0; i <= dim; i++) 
299
	fout << (fabs(val) < 1e-40 ? 0.0 : val) << "\n";
300
      
301
302
303
      vc++;
      elInfo = stack.traverseNext(elInfo);
    } 
304

305
    fout << "        </DataArray>" << std::endl;
306
    
307

308
309
310
311
    fout << "      </PointData>" << std::endl;
    fout << "    </Piece>" << std::endl;
    fout << "  </UnstructuredGrid>" << std::endl;
    fout << "</VTKFile>" << std::endl;
312

313
314
    fout.close();
  }
315
}