ArhReader.cc 3.31 KB
Newer Older
1
#include <fstream>
2
#include <stdint.h>
3 4 5 6

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

namespace AMDiS {
  
  using namespace std;

14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

  void ArhReader::read(std::string filename, Mesh *mesh)
  {
    std::vector<DOFVector<double>*> vecs(0);
    ArhReader::read(filename, mesh, vecs);
  }


  void ArhReader::read(std::string filename, Mesh *mesh, DOFVector<double>* vec)
  {
    std::vector<DOFVector<double>*> vecs(1);
    vecs[0] = vec;
    
    ArhReader::read(filename, mesh, vecs);
  }


  void ArhReader::read(string filename, Mesh *mesh, 
		       std::vector<DOFVector<double>*> vecs)
33 34 35
  {
    FUNCNAME("ArhReader::read()");

36
    RefinementManager *refManager = NULL;
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
    switch (mesh->getDim()) {
    case 2:
      refManager = new RefinementManager2d();
      break;
    case 3:
      refManager = new RefinementManager3d();
      break;
    default:
      ERROR_EXIT("Should not happen!\n");
    }

    ifstream file;
    file.open(filename.c_str(), ios::in | ios::binary);

    string typeId = "";
52 53 54
    uint32_t nMacroElements = 0;
    uint32_t nValueVectors = 0;
    uint32_t nAllValues = 0;
55 56 57 58 59 60

    file.read(const_cast<char*>(typeId.data()), 4);
    file.read(reinterpret_cast<char*>(&nMacroElements), 4);
    file.read(reinterpret_cast<char*>(&nValueVectors), 4);
    file.read(reinterpret_cast<char*>(&nAllValues), 4);

61 62 63 64
    TEST_EXIT(nValueVectors == vecs.size())
      ("File has %d vectors, but %d DOFVectors are provided!\n", 
       nValueVectors, vecs.size());

65 66 67 68
    for (unsigned int i = 0; i < nMacroElements; i++) {
      uint32_t elIndex = 0;
      uint32_t nStructureCodes = 0;
      uint32_t codeSize = 0;
69 70 71 72 73

      file.read(reinterpret_cast<char*>(&elIndex), 4);
      file.read(reinterpret_cast<char*>(&nStructureCodes), 4);
      file.read(reinterpret_cast<char*>(&codeSize), 4);

74
      vector<uint64_t> structureCode(nStructureCodes);
75
      file.read(reinterpret_cast<char*>(&(structureCode[0])), 8 * nStructureCodes);
76 77 78

      MeshStructure elementStructure;
      elementStructure.init(structureCode, codeSize);
79
      elementStructure.fitMeshToStructure(mesh, refManager, false, elIndex);
80

81
      uint32_t nValuesPerVector = 0;
82 83
      file.read(reinterpret_cast<char*>(&nValuesPerVector), 4);

84 85 86 87
      for (unsigned int j = 0; j < nValueVectors; j++) {
	vector<double> values(nValuesPerVector);
	file.read(reinterpret_cast<char*>(&(values[0])), 8 * nValuesPerVector);
	setDofValues(elIndex, mesh, values, vecs[j]);
88 89 90 91 92 93 94
      }
    }

    file.close();

    MSG("ARH file read from: %s\n", filename.c_str());
  }
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109


  void ArhReader::setDofValues(int macroElIndex, Mesh *mesh,
			       std::vector<double>& values, DOFVector<double>* vec)
  {
    FUNCNAME("ArhReader::setDofValues()");

    bool macroElement = false;
    int valuePos = 0;
    TraverseStack stack;
    ElInfo *elInfo = stack.traverseFirstOneMacro(mesh, macroElIndex, -1,
						 Mesh::CALL_EVERY_EL_PREORDER);
    while (elInfo) {
      if (!macroElement) {
	Element *mEl = elInfo->getMacroElement()->getElement();
110
	for (int i = 0; i < mesh->getGeo(VERTEX); i++)
111
	  (*vec)[mEl->getDof(i, 0)] = values[valuePos++];	
112 113 114 115 116
	macroElement = true;
      }

      Element *el = elInfo->getElement();
      if (!el->isLeaf())
117
	(*vec)[el->getChild(0)->getDof(mesh->getDim(), 0)] = values[valuePos++];
118 119 120 121

      elInfo = stack.traverseNext(elInfo);
    }
  }
122
}