ArhWriter.cc 3.46 KB
Newer Older
1
2
3
4
5
6
#include <fstream>

#include "ArhWriter.h"
#include "Mesh.h"
#include "MeshStructure.h"
#include "Traverse.h"
7
#include "DOFVector.h"
8
9
10
11
12

namespace AMDiS {
  
  using namespace std;

13

14
  void ArhWriter::write(string filename, Mesh *mesh)
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  {
    std::vector<DOFVector<double>*> vecs(0);
    ArhWriter::write(filename, mesh, vecs);
  }


  void ArhWriter::write(std::string filename, Mesh *mesh, DOFVector<double>* vec)
  {
    std::vector<DOFVector<double>*> vecs(1);
    vecs[0] = vec;

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


  void ArhWriter::write(std::string filename, Mesh *mesh, 
			std::vector<DOFVector<double>*> vecs)
32
33
34
35
36
37
  {
    FUNCNAME("ArhWriter::write()");

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

38
    string typeId = "arhP";
39
40
    file.write(typeId.c_str(), 4);

41
    uint32_t nMacroElements = 0;
42
43
44
45
46
47
48
49
50
    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);
    
51
    uint32_t nValueVectors = vecs.size();
52
53
    file.write(reinterpret_cast<char*>(&nValueVectors), 4);

54
55
    uint32_t nAllValues = 
      (vecs.size() > 0 ? vecs[0]->getFeSpace()->getAdmin()->getUsedDOFs() : 0);
56
57
58
    file.write(reinterpret_cast<char*>(&nAllValues), 4);

    MeshStructure elementStructure;
59
    std::vector<std::vector<double> > values(vecs.size());
60
    int32_t macroElIndex = -1;
61
62
63
64
65
66

    elInfo = stack.traverseFirst(mesh, -1, Mesh::CALL_EVERY_EL_PREORDER);
    while (elInfo) {
      if (elInfo->getLevel() == 0) {
	if (macroElIndex != -1) {
	  elementStructure.commit();
67
	  writeMacroElement(file, elementStructure, values, macroElIndex);
68
69
70
71
	}

	elementStructure.clear();
	macroElIndex = elInfo->getElement()->getIndex();
72
73
74
75
76
77
	for (unsigned int i = 0; i < vecs.size(); i++) {
	  values[i].clear();

	  for (int j = 0; j <= mesh->getDim(); j++)
	    values[i].push_back((*vecs[i])[elInfo->getElement()->getDOF(j, 0)]);
	}
78
79
80
      }

      elementStructure.insertElement(elInfo->getElement()->isLeaf());
81
82
83
84
85
86
      
      if (!elInfo->getElement()->isLeaf()) {
	for (unsigned int i = 0; i < vecs.size(); i++)
	  values[i].push_back((*vecs[i])[elInfo->getElement()->getChild(0)->getDOF(mesh->getDim(), 0)]);
      }

87
88
89
90
91
92
      elInfo = stack.traverseNext(elInfo);
    }

    // And write the last macro element to file.
    TEST_EXIT_DBG(macroElIndex != -1)("Should not happen!\n");
    elementStructure.commit();
93
    writeMacroElement(file, elementStructure, values, macroElIndex);
94
95
96
97
98
99
100
101
    
    file.close();

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

  
  void ArhWriter::writeMacroElement(std::ofstream &file, 
102
103
				    MeshStructure &code,
				    std::vector<std::vector<double> >& values,
104
				    int32_t elIndex)
105
106
107
  {
    file.write(reinterpret_cast<char*>(&elIndex), 4); 
    
108
    uint32_t nStructureCodes = code.getCode().size();
109
110
    file.write(reinterpret_cast<char*>(&nStructureCodes), 4);
    
111
    uint32_t codeSize = code.getNumElements();
112
113
    file.write(reinterpret_cast<char*>(&codeSize), 4);

114
    file.write(reinterpret_cast<char*>(&(const_cast<vector<uint64_t>&>(code.getCode())[0])), 
115
	       8 * nStructureCodes);
116

117
    uint32_t nValuesPerVector = (values.size() > 0 ? values[0].size() : 0);
118
    file.write(reinterpret_cast<char*>(&nValuesPerVector), 4);
119
120
121

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

}