ArhWriter.cc 3.73 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
//
// Software License for AMDiS
//
// Copyright (c) 2010 Dresden University of Technology 
// All rights reserved.
// Authors: Simon Vey, Thomas Witkowski et al.
//
// This file is part of AMDiS
//
// See also license.opensource.txt in the distribution.


13
#include <fstream>
14
#include <stdint.h>
15
16
17
18
19

#include "ArhWriter.h"
#include "Mesh.h"
#include "MeshStructure.h"
#include "Traverse.h"
20
#include "DOFVector.h"
21
22
23
24
25

namespace AMDiS {
  
  using namespace std;

26

27
28
29
30
  void ArhWriter::write(string filename, Mesh *mesh,
			DOFVector<double>* vec0, 
			DOFVector<double>* vec1,
			DOFVector<double>* vec2)
31
  {
32
33
34
35
36
37
38
    vector<DOFVector<double>*> vecs(0);
    if (vec0 != NULL)
      vecs.push_back(vec0);
    if (vec1 != NULL)
      vecs.push_back(vec1);
    if (vec2 != NULL)
      vecs.push_back(vec2);
39
40
41
42
43

    ArhWriter::write(filename, mesh, vecs);
  }


44
45
  void ArhWriter::write(string filename, Mesh *mesh, 
			vector<DOFVector<double>*> vecs)
46
47
48
49
50
51
  {
    FUNCNAME("ArhWriter::write()");

    ofstream file;
    file.open(filename.c_str(), ios::out | ios::binary | ios::trunc);

52
    string typeId = "arhP";
53
54
    file.write(typeId.c_str(), 4);

55
    uint32_t nMacroElements = 0;
56
57
58
59
60
61
62
63
64
    TraverseStack stack;
    ElInfo *elInfo = stack.traverseFirst(mesh, 0, Mesh::CALL_EL_LEVEL);
    while (elInfo) {
      nMacroElements++;
      elInfo = stack.traverseNext(elInfo);
    }

    file.write(reinterpret_cast<char*>(&nMacroElements), 4);
    
65
    uint32_t nValueVectors = vecs.size();
66
67
    file.write(reinterpret_cast<char*>(&nValueVectors), 4);

68
    uint32_t nAllValues = 
69
      (vecs.size() > 0 ? vecs[0]->getFeSpace()->getAdmin()->getUsedDofs() : 0);
70
71
72
    file.write(reinterpret_cast<char*>(&nAllValues), 4);

    MeshStructure elementStructure;
73
    vector<vector<double> > values(vecs.size());
74
    int32_t macroElIndex = -1;
75
76
77
78
79
80

    elInfo = stack.traverseFirst(mesh, -1, Mesh::CALL_EVERY_EL_PREORDER);
    while (elInfo) {
      if (elInfo->getLevel() == 0) {
	if (macroElIndex != -1) {
	  elementStructure.commit();
81
	  writeMacroElement(file, elementStructure, values, macroElIndex);
82
83
84
85
	}

	elementStructure.clear();
	macroElIndex = elInfo->getElement()->getIndex();
86
87
88
	for (unsigned int i = 0; i < vecs.size(); i++) {
	  values[i].clear();

89
	  for (int j = 0; j < mesh->getGeo(VERTEX); j++)
90
	    values[i].push_back((*vecs[i])[elInfo->getElement()->getDof(j, 0)]);
91
	}
92
93
94
      }

      elementStructure.insertElement(elInfo->getElement()->isLeaf());
95
96
97
      
      if (!elInfo->getElement()->isLeaf()) {
	for (unsigned int i = 0; i < vecs.size(); i++)
98
	  values[i].push_back((*vecs[i])[elInfo->getElement()->getChild(0)->getDof(mesh->getDim(), 0)]);
99
100
      }

101
102
103
104
105
106
      elInfo = stack.traverseNext(elInfo);
    }

    // And write the last macro element to file.
    TEST_EXIT_DBG(macroElIndex != -1)("Should not happen!\n");
    elementStructure.commit();
107
    writeMacroElement(file, elementStructure, values, macroElIndex);
108
109
110
111
112
113
114
    
    file.close();

    MSG("ARH file written to: %s\n", filename.c_str());
  }

  
115
  void ArhWriter::writeMacroElement(ofstream &file, 
116
				    MeshStructure &code,
117
				    vector<vector<double> >& values,
118
				    int32_t elIndex)
119
120
121
  {
    file.write(reinterpret_cast<char*>(&elIndex), 4); 
    
122
    uint32_t nStructureCodes = code.getCode().size();
123
124
    file.write(reinterpret_cast<char*>(&nStructureCodes), 4);
    
125
    uint32_t codeSize = code.getNumElements();
126
127
    file.write(reinterpret_cast<char*>(&codeSize), 4);

128
    file.write(reinterpret_cast<char*>(&(const_cast<vector<uint64_t>&>(code.getCode())[0])), 
129
	       8 * nStructureCodes);
130

131
    uint32_t nValuesPerVector = (values.size() > 0 ? values[0].size() : 0);
132
    file.write(reinterpret_cast<char*>(&nValuesPerVector), 4);
133
134
135

    for (unsigned int i = 0; i < values.size(); i++)
      file.write(reinterpret_cast<char*>(&(values[i][0])), 8 * nValuesPerVector);
136
137
138
  }

}