ArhWriter.cc 3.48 KB
Newer Older
1
#include <fstream>
2
#include <stdint.h>
3
4
5
6
7

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

namespace AMDiS {
  
  using namespace std;

14

15
  void ArhWriter::write(string filename, Mesh *mesh)
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
  {
    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)
33
34
35
36
37
38
  {
    FUNCNAME("ArhWriter::write()");

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

}