ArhWriter.cc 4.1 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
28
29
30
  void ArhWriter::write(string filename, Mesh *mesh,
			DOFVector<double>* vec0, 
			DOFVector<double>* vec1,
			DOFVector<double>* vec2)
31
  {
32
33
34
35
36
37
38
    vector<DOFVector<double>*> vecs(0);
    if (vec0 != NULL)
      vecs.push_back(vec0);
    if (vec1 != NULL)
      vecs.push_back(vec1);
    if (vec2 != NULL)
      vecs.push_back(vec2);
39
40
41
42
43

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


44
  void ArhWriter::write(string filename, Mesh *mesh, 
45
46
			vector<DOFVector<double>*> vecs,
			bool writeParallel)
47
48
49
  {
    FUNCNAME("ArhWriter::write()");

50
51
52
53
54
55
56
57
58
59
60
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
    if (writeParallel) {
      using boost::lexical_cast;
      
      int sPos = filename.find(".arh");
      TEST_EXIT(sPos >= 0)("Failed to find file postfix!\n");
      string name = filename.substr(0, sPos);      
      filename = name + "-p" + lexical_cast<string>(MPI::COMM_WORLD.Get_rank()) + "-.arh";
    }
#endif

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

64
    string typeId = "arhP";
65
66
    file.write(typeId.c_str(), 4);

67
    uint32_t nMacroElements = 0;
68
69
70
71
72
73
74
75
76
    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);
    
77
    uint32_t nValueVectors = vecs.size();
78
79
    file.write(reinterpret_cast<char*>(&nValueVectors), 4);

80
    uint32_t nAllValues = 
81
      (vecs.size() > 0 ? vecs[0]->getFeSpace()->getAdmin()->getUsedDofs() : 0);
82
83
84
    file.write(reinterpret_cast<char*>(&nAllValues), 4);

    MeshStructure elementStructure;
85
    vector<vector<double> > values(vecs.size());
86
    int32_t macroElIndex = -1;
87
88
89
90
91
92

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

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

101
	  for (int j = 0; j < mesh->getGeo(VERTEX); j++)
102
	    values[i].push_back((*vecs[i])[elInfo->getElement()->getDof(j, 0)]);
103
	}
104
105
106
      }

      elementStructure.insertElement(elInfo->getElement()->isLeaf());
107
108
109
      
      if (!elInfo->getElement()->isLeaf()) {
	for (unsigned int i = 0; i < vecs.size(); i++)
110
	  values[i].push_back((*vecs[i])[elInfo->getElement()->getChild(0)->getDof(mesh->getDim(), 0)]);
111
112
      }

113
114
115
116
117
118
      elInfo = stack.traverseNext(elInfo);
    }

    // And write the last macro element to file.
    TEST_EXIT_DBG(macroElIndex != -1)("Should not happen!\n");
    elementStructure.commit();
119
    writeMacroElement(file, elementStructure, values, macroElIndex);
120
121
122
123
124
125
126
    
    file.close();

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

  
127
  void ArhWriter::writeMacroElement(ofstream &file, 
128
				    MeshStructure &code,
129
				    vector<vector<double> >& values,
130
				    int32_t elIndex)
131
132
133
  {
    file.write(reinterpret_cast<char*>(&elIndex), 4); 
    
134
    uint32_t nStructureCodes = code.getCode().size();
135
136
    file.write(reinterpret_cast<char*>(&nStructureCodes), 4);
    
137
    uint32_t codeSize = code.getNumElements();
138
139
    file.write(reinterpret_cast<char*>(&codeSize), 4);

140
    file.write(reinterpret_cast<char*>(&(const_cast<vector<uint64_t>&>(code.getCode())[0])), 
141
	       8 * nStructureCodes);
142

143
    uint32_t nValuesPerVector = (values.size() > 0 ? values[0].size() : 0);
144
    file.write(reinterpret_cast<char*>(&nValuesPerVector), 4);
145
146
147

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

}