ArhWriter.cc 2.28 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <fstream>

#include "ArhWriter.h"
#include "Mesh.h"
#include "MeshStructure.h"
#include "Traverse.h"

namespace AMDiS {
  
  using namespace std;

  void ArhWriter::write(string filename, Mesh *mesh)
  {
    FUNCNAME("ArhWriter::write()");

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

    string typeId = "arhS";
    file.write(typeId.c_str(), 4);

22
    uint32_t nMacroElements = 0;
23
24
25
26
27
28
29
30
31
    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);
    
32
    uint32_t nValueVectors = 0;
33
34
    file.write(reinterpret_cast<char*>(&nValueVectors), 4);

35
    uint32_t nAllValues = 0;
36
37
38
    file.write(reinterpret_cast<char*>(&nAllValues), 4);

    MeshStructure elementStructure;
39
    int32_t macroElIndex = -1;
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

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

	elementStructure.clear();
	macroElIndex = elInfo->getElement()->getIndex();
      }

      elementStructure.insertElement(elInfo->getElement()->isLeaf());
      elInfo = stack.traverseNext(elInfo);
    }

    // And write the last macro element to file.
    TEST_EXIT_DBG(macroElIndex != -1)("Should not happen!\n");
    elementStructure.commit();
    writeMacroElement(file, elementStructure, macroElIndex);
    
    file.close();

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

  
  void ArhWriter::writeMacroElement(std::ofstream &file, 
				    MeshStructure &code, 
70
				    int32_t elIndex)
71
72
73
  {
    file.write(reinterpret_cast<char*>(&elIndex), 4); 
    
74
    uint32_t nStructureCodes = code.getCode().size();
75
76
    file.write(reinterpret_cast<char*>(&nStructureCodes), 4);
    
77
    uint32_t codeSize = code.getNumElements();
78
79
80
    file.write(reinterpret_cast<char*>(&codeSize), 4);

    file.write(reinterpret_cast<char*>(&(const_cast<vector<unsigned long int>&>(code.getCode())[0])), 
81
	       8 * nStructureCodes);
82

83
    uint32_t nValuesPerVector = 0;
84
85
86
87
    file.write(reinterpret_cast<char*>(&nValuesPerVector), 4);
  }

}