ArhWriter.cc 2.25 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#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);

    long nMacroElements = 0;
    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);
    
    long nValueVectors = 0;
    file.write(reinterpret_cast<char*>(&nValueVectors), 4);

    long nAllValues = 0;
    file.write(reinterpret_cast<char*>(&nAllValues), 4);

    MeshStructure elementStructure;
    long macroElIndex = -1;

    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, 
				    long elIndex)
  {
    file.write(reinterpret_cast<char*>(&elIndex), 4); 
    
    long nStructureCodes = code.getCode().size();
    file.write(reinterpret_cast<char*>(&nStructureCodes), 4);
    
    long codeSize = code.getNumElements();
    file.write(reinterpret_cast<char*>(&codeSize), 4);

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

    long nValuesPerVector = 0;
    file.write(reinterpret_cast<char*>(&nValuesPerVector), 4);
  }

}