ValueReader.cc 2.68 KB
Newer Older
1 2 3 4 5 6 7
/******************************************************************************
 *
 * AMDiS - Adaptive multidimensional simulations
 *
 * Copyright (C) 2013 Dresden University of Technology. All Rights Reserved.
 * Web: https://fusionforge.zih.tu-dresden.de/projects/amdis
 *
8
 * Authors:
9 10 11 12 13 14 15 16 17
 * Simon Vey, Thomas Witkowski, Andreas Naumann, Simon Praetorius, et al.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 *
 * This file is part of AMDiS
 *
 * See also license.opensource.txt in the distribution.
18
 *
19
 ******************************************************************************/
20 21


Thomas Witkowski's avatar
Thomas Witkowski committed
22
#include <cstring>
23 24 25
#include <fstream>
#include <vector>

26
#include "ValueReader.h"
27
#include "MacroInfo.h"
28

29
namespace AMDiS { namespace io {
30

31
  namespace ValueReader
32
  {
33
    using namespace std;
34

35 36 37 38 39 40 41 42 43 44 45 46 47
    /** \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.
    */
    void readValue(string filename, Mesh *mesh,
		   DOFVector<double> *dofVector,
		   MacroInfo *macroFileInfo)
    {
      FUNCNAME("readValue()");
48

49 50 51 52
      TEST_EXIT(filename != "")("Filename not specified!\n");
      TEST_EXIT(mesh)("no mesh specified\n");
      TEST_EXIT(dofVector)("no DOF vector specified\n");
      TEST_EXIT(macroFileInfo)("no MacroInfo specified\n");
53

54
      string line;
55

56 57
      // open the file and read the data to the vector values.
      ifstream file(filename.c_str(), ios_base::in);
58

59 60 61 62 63 64 65
      while (!file.eof()) {
	getline(file, line);
	if (line.find("vertex values:") != string::npos)
	  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");
66

67 68 69
      double value;
      vector<double> values;
      values.clear();
70

71 72 73 74 75 76
      while (!file.eof()) {
	file >> value;
	if (!file.good())
	  break;
	values.push_back(value);
      }
77

78
      file.close();
79

80 81 82 83 84 85 86 87
      // 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];
	  DegreeOfFreedom dofIndex = *(macroFileInfo->dof[fileIndex]);
	  (*dofVector)[dofIndex] = values[fileIndex];
	}
88 89
      }
    }
90

91 92
  } // end namespace ValueReader
} } // end namespace io, AMDiS