ValueReader.cc 1.88 KB
Newer Older
Thomas Witkowski's avatar
Thomas Witkowski committed
1
#include <cstring>
2 3
#include "ValueReader.h"
#include "MacroReader.h"
4
#include "MacroInfo.h"
5 6 7 8 9 10 11 12 13 14 15

namespace AMDiS {

  /** \brief
   * Copies the values of a value file to a DOF vector.
   *
   * The DOF vector must have been created by a corresponding mesh file. The
   * information about this file and the macro triangulation are stored in
   * macroFileInfo. The function now reads the corresponding value file and
   * copies the values to the correct positions in the DOF vector.
   */
Thomas Witkowski's avatar
Thomas Witkowski committed
16
  void ValueReader::readValue(std::string filename,
17 18 19 20 21 22
			      Mesh *mesh,
			      DOFVector<double> *dofVector,
			      MacroInfo *macroFileInfo)
  {
    FUNCNAME("ValueReader::readValue()");

23
    TEST_EXIT(filename != "")("Filename not specified!\n");
24 25 26 27
    TEST_EXIT(mesh)("no mesh specified\n");
    TEST_EXIT(dofVector)("no DOF vector specified\n");
    TEST_EXIT(macroFileInfo)("no MacroInfo specified\n");

28
    std::string line;
29 30

    // open the file and read the data to the vector values.
31
    std::ifstream file(filename.c_str(), std::ifstream::in);
32 33 34

    while (!file.eof()) {
      getline(file, line);
35
      if (line.find("vertex values:") != std::string::npos)
36 37 38 39 40 41
	break;
    }
    // A vertex value cannot be the last line of the file, if the file is correct.
    TEST_EXIT(!file.eof())("Value file does not contain vertex values\n");

    double value;
42
    std::vector<double> values;
43 44 45 46
    values.clear();

    while (!file.eof()) {
      file >> value;
Thomas Witkowski's avatar
Thomas Witkowski committed
47
      if (!file.good())
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
	break;
      values.push_back(value);
    }

    file.close();

    // Using the macroFileInfo structure, copy the values to the
    // correct positions in the DOF vector.
    for (int i = 0; i < mesh->getNumberOfMacros(); i++) {
      for (int j = 0; j < mesh->getGeo(VERTEX); j++) {
	int fileIndex = macroFileInfo->mel_vertex[i][j];
	int dofIndex = *(macroFileInfo->dof[fileIndex]);
	(*dofVector)[dofIndex] = values[fileIndex];
      }
    }
  }
}