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
  void ArhWriter::write(string filename, Mesh *mesh)
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
  {
    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)
45
46
47
48
49
50
  {
    FUNCNAME("ArhWriter::write()");

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

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

54
    uint32_t nMacroElements = 0;
55
56
57
58
59
60
61
62
63
    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);
    
64
    uint32_t nValueVectors = vecs.size();
65
66
    file.write(reinterpret_cast<char*>(&nValueVectors), 4);

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

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

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

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

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

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

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

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

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

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

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

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

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

}