VtkReader.hh 4.17 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/******************************************************************************
 *
 * AMDiS - Adaptive multidimensional simulations
 *
 * Copyright (C) 2013 Dresden University of Technology. All Rights Reserved.
 * Web: https://fusionforge.zih.tu-dresden.de/projects/amdis
 *
 * Authors: 
 * 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.
 * 
 ******************************************************************************/


/** \file VtuReader.hh */

// need some extension-methods/libs (pugixml, nanoflann)
#ifdef HAVE_EXTENSIONS

#include "Mesh.h"
#include "boost/filesystem.hpp"
#include "pugixml.hpp"
#include "kdtree_nanoflann.h"
#include "VectorOperations.h"
#include "detail/VtkReader.h"

namespace AMDiS 
{  
  namespace io 
  {
    namespace VtkReader
    {
      
      template<typename T>
      void readByName(std::string filename,
		      std::vector<DOFVector<T>*> dofVectors,
		      std::vector<std::string> componentNames)
      {
	using namespace pugi;
	using namespace AMDiS::io::VtkReader;

	FUNCNAME("VtuReader::readValue()");
	
	TEST_EXIT(filename != "")("Filename not specified!\n");
	TEST_EXIT(dofVectors.size() > 0)("no DOF vectors specified\n");
	TEST_EXIT(componentNames.size() > 0)("no componentName specified\n");

	TEST_EXIT(boost::filesystem::exists(filename))((filename + " does not exist!\n").c_str());

	xml_document vtu;
	TEST_EXIT(vtu.load_file(filename.c_str()))("Could not load vtu file! Error in xml structure.\n");

	xml_node VTKFile = vtu.child("VTKFile");
	xml_node UnstructuredGrid = VTKFile.child("UnstructuredGrid");
	xml_node Piece = UnstructuredGrid.child("Piece");
	xml_node PointData = Piece.child("PointData");
	xml_node Points = Piece.child("Points");

	std::string points = Points.child_value("DataArray");
	typedef std::vector<WorldVector<double> > PL;
	PL pointList;
	detail::string2pointList(points, pointList);

	std::vector<std::vector<T> > valueList(dofVectors.size());
	T test;

	for (xml_node DataArray = PointData.child("DataArray"); DataArray; DataArray = DataArray.next_sibling("DataArray")) {
	  std::string Name = DataArray.attribute("Name").value();
	  for (size_t i = 0; i < componentNames.size(); i++) {
	    if (Name == componentNames[i]) {
	      std::string values = DataArray.last_child().value();
	      int nComponents = -1;
	      if (DataArray.attribute("NumberOfComponents"))
		nComponents = DataArray.attribute("NumberOfComponents").as_int();
	      TEST_EXIT(nComponents == -1 || static_cast<int>(vector_operations::num_rows(test)) <= nComponents)
		("Can not read values in DOFVector with given value type. Too many components!\n");
	      detail::string2valueList(values, valueList[i], vector_operations::num_rows(test), nComponents);
	      break;
	    }
	  }
	}
	for (size_t i = 0; i < dofVectors.size(); i++) {
	  TEST_EXIT(valueList[i].size() != 0)
	    ("No values found for component name '%s'!\n", componentNames[i].c_str());
	  TEST_EXIT(pointList.size() == valueList[i].size())
	    ("Coordinates and values[%d] do not match!\n", i);
	}

	DOFVector<WorldVector<double> > coords(dofVectors[0]->getFeSpace(), "coords");
	dofVectors[0]->getFeSpace()->getMesh()->getDofIndexCoords(coords);

	experimental::KD_Tree tree(Global::getGeo(WORLD), pointList, 10);
	tree.index->buildIndex();

	DOFIterator<WorldVector<double> > it_coords(&coords, USED_DOFS);
	std::vector<DOFIterator<T>*> it_results;
	for (size_t i = 0; i < dofVectors.size(); i++) {
	  it_results.push_back(new DOFIterator<T>(dofVectors[i], USED_DOFS));
	  it_results[i]->reset();
	}
	for (it_coords.reset(); !it_coords.end(); ++it_coords) {
	  size_t idx = detail::getNearestIndex(tree, *it_coords);

	  for (size_t i = 0; i < dofVectors.size(); i++) {
	    if (valueList[i].size() > idx)
	      *(*it_results[i]) = valueList[i][idx];
	    (*it_results[i])++;
	  }
	}
	
	
	MSG("VTU file read from: %s\n", filename.c_str());
      }
      
    } // end namespace VtuReader
  } // end namespace io
} // end namespace

#endif // HAVE_EXTENSIONS