VtuReader.h 8.75 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
// ============================================================================
// ==                                                                        ==
// == AMDiS - Adaptive multidimensional simulations                          ==
// ==                                                                        ==
// ==  http://www.amdis-fem.org                                              ==
// ==                                                                        ==
// ============================================================================
//
// 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.



/** \file VtuReader.h */

#ifndef AMDIS_VTUREADER_H
#define AMDIS_VTUREADER_H

#include <cstring>
#include "DOFVector.h"
#include "Mesh.h"
#include "boost/filesystem.hpp"
Praetorius, Simon's avatar
Praetorius, Simon committed
30
#include "pugixml.hpp"
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include "kdtree_nanoflann.h"

namespace AMDiS {

  namespace VtuReader {
    
    /// Copies the values of a VTU-file to a DOF vector.
    template<typename T>
    inline void readValue(std::string filename,
			  Mesh *mesh,
			  DOFVector<T> *dofVector,
			  std::string componentName);

    /// Copies the values of a VTU-file to a vector of DOF vectors.
    template<typename T>
    inline void readValue(std::string filename,
			  Mesh *mesh,
			  std::vector<DOFVector<T>*> dofVectors,
			  std::vector<std::string> componentNames);


    namespace details {

      // num_rows for scalar types
      template<typename T>
      size_t num_rows(T &v) {
	return 1;
      };
      // for WorldVectors print 3 components
      template<typename T>
      size_t num_rows(WorldVector<T> &v) {
	return static_cast<size_t>(v.getSize());
      }
      // num_rows for vector types
      template<typename T>
      size_t num_rows(std::vector<T> &v) {
	return v.size();
      };
      template<typename T>
      size_t num_rows(const std::vector<T> &v) {
	return v.size();
      };
      template<typename T>
      size_t num_rows(mtl::dense_vector<T> &v) {
	return mtl::num_rows(v);
      };
      template<typename T>
      size_t num_rows(const mtl::dense_vector<T> &v) {
	return mtl::num_rows(v);
      };

      template<typename T>
      struct LessPairEps
      {
	LessPairEps() : eps(1.e-7) {}
	bool operator()(const std::pair<WorldVector<double>, T>& lhs, const std::pair<WorldVector<double>, T>& rhs) const
	{
	  return (lhs.first.getSize()==1 ? vertexi_compare1(lhs.first, rhs.first) :
		    (lhs.first.getSize()==2 ? vertexi_compare2(lhs.first, rhs.first) :
		      (lhs.first.getSize()==3 ? vertexi_compare3(lhs.first, rhs.first) : false)));
	}
	bool operator()(const std::pair<WorldVector<double>, T>& lhs, const WorldVector<double>& rhs) const
	{
	  return (lhs.first.getSize()==1 ? vertexi_compare1(lhs.first, rhs) :
		    (lhs.first.getSize()==2 ? vertexi_compare2(lhs.first, rhs) :
		      (lhs.first.getSize()==3 ? vertexi_compare3(lhs.first, rhs) : false)));
	}
	bool operator()(const WorldVector<double>& lhs, const std::pair<WorldVector<double>, T>& rhs) const
	{
	  return (lhs.getSize()==1 ? vertexi_compare1(lhs, rhs.first) :
		    (lhs.getSize()==2 ? vertexi_compare2(lhs, rhs.first) :
		      (lhs.getSize()==3 ? vertexi_compare3(lhs, rhs.first) : false)));
	}
	bool operator()(const WorldVector<double>& lhs, const WorldVector<double>& rhs) const
	{
	  return (lhs.getSize()==1 ? vertexi_compare1(lhs, rhs) :
		    (lhs.getSize()==2 ? vertexi_compare2(lhs, rhs) :
		      (lhs.getSize()==3 ? vertexi_compare3(lhs, rhs) : false)));
	}
      private:
	double eps;

	//returns true if vertex lhs is "smaller" than vertex rhs
	//----------------------------------------< vertexi_compare1 >
	bool vertexi_compare1(const WorldVector<double> lhs, const WorldVector<double> rhs) const {
	  return (lhs[0]-rhs[0]>eps);
	}

	//----------------------------------------< vertexi_compare2 >
	bool vertexi_compare2(const WorldVector<double> lhs, const WorldVector<double> rhs) const {
	  return (lhs[0]-rhs[0]>eps)
		|| (abs(lhs[0]-rhs[0])<=eps && lhs[1]-rhs[1]>eps);
	}

	//----------------------------------------< vertexi_compare3 >
	bool vertexi_compare3(const WorldVector<double> lhs, const WorldVector<double> rhs) const {
	  return (lhs[0]-rhs[0]>eps)
		|| (abs(lhs[0]-rhs[0])<=eps && lhs[1]-rhs[1]>eps)
		|| (abs(lhs[0]-rhs[0])<=eps && abs(lhs[1]-rhs[1])<=eps
		&& lhs[2]-rhs[2]>eps);
	}
      };

      
      inline void string2pointList(std::string& input, std::vector<WorldVector<double> >& pointList)
      {
	int dow = Global::getGeo(WORLD);
	int dowExplicit = 3;

	std::vector<std::string> valueStringList;
	std::stringstream ss(input);
	std::string buf;
	while (ss >> buf)
	  valueStringList.push_back(buf);

	pointList.clear();

	std::vector<std::string>::iterator it;
	size_t j = 0;
	for (it = valueStringList.begin(); it != valueStringList.end();j++) {
	  WorldVector<double> p;
	  for (int i = 0; i < dowExplicit && it != valueStringList.end(); i++, it++)
	    if (i < dow)
	      p[i] = boost::lexical_cast<double>(*it);
	  pointList.push_back(p);
	}
      }

      template<typename T>
      inline void string2valueList(std::string& input, std::vector<T>& valueList, int numComponent = 1, int numComponentMax = -1)
      {
	if (numComponentMax < 0)
	  numComponentMax = numComponent;

	std::vector<std::string> valueStringList;
	std::stringstream ss(input);
	std::string buf;
	while (ss >> buf)
	  valueStringList.push_back(buf);

	valueList.clear();

	std::vector<std::string>::iterator it;
	for (it = valueStringList.begin(); it != valueStringList.end();) {
	  std::vector<double> p;
	  for (int i = 0; i < numComponentMax && it != valueStringList.end(); i++, it++)
	    if (i < numComponent)
	      p.push_back(boost::lexical_cast<double>(*it));
	  T value; valueVector2type(p, value);
	  valueList.push_back(value);
	}
      }


      template<typename T>
      inline void string2dataMap(std::string& strPoints, std::string& strValues, std::map<WorldVector<double>, T, LessPairEps<T> >& dataList, int numComponent = 1, int numComponentMax = -1)
      {
	int dow = Global::getGeo(WORLD);
	int dowExplicit = 3;
	if (numComponentMax < 0)
	  numComponentMax = numComponent;
	std::string buf;

	std::vector<std::string> pointStringList;
	std::stringstream ss1(strPoints);
	while (ss1 >> buf)
	  pointStringList.push_back(buf);

	std::vector<std::string> valueStringList;
	std::stringstream ss2(strValues);
	while (ss2 >> buf)
	  valueStringList.push_back(buf);

	if (pointStringList.size()/dowExplicit != valueStringList.size()/numComponentMax)
	  throw(std::runtime_error("Coordinates and values do not match! nPoints="
				    + boost::lexical_cast<std::string>(pointStringList.size()/dowExplicit)
				    + ", nValues="
				    + boost::lexical_cast<std::string>(valueStringList.size()/numComponentMax)));

	std::vector<std::string>::iterator it1, it2;
	it1 = pointStringList.begin();
	it2 = valueStringList.begin();
	for (; it1 != pointStringList.end() && it2 != valueStringList.end();) {
	  WorldVector<double> p;
	  for (int i = 0; i < dowExplicit && it1 != pointStringList.end(); i++, it1++)
	    if (i < dow)
	      p[i] = boost::lexical_cast<double>(*it1);

	  std::vector<double> v;
	  for (int i = 0; i < numComponentMax && it2 != valueStringList.end(); i++, it2++)
	    if (i < numComponent)
	      v.push_back(boost::lexical_cast<double>(*it2));
	  T value; valueVector2type(v, value);

	  dataList[p] = value;
	}
      }

      inline void valueVector2type(std::vector<double> p, double& value)
      {
	if (p.size() == 0)
	  throw(std::runtime_error("Not enough data for assignment!\n"));
	value = p[0];
      }

      inline void valueVector2type(std::vector<double> p, WorldVector<double>& value)
      {
	if (p.size() != Global::getGeo(WORLD))
	  throw(std::runtime_error("Not enough data for assignment!\n"));
	for (int i = 0; i < Global::getGeo(WORLD); i++)
	  value[i] = p[i];
      }

      inline void valueVector2type(std::vector<double> p, std::vector<double>& value)
      {
	if (p.size() == 0)
	  throw(std::runtime_error("Not enough data for assignment!\n"));
	for (int i = 0; i < p.size(); i++)
	  value.push_back(p[i]);
      }


      // find point in mesh using KD-tree structure
      inline size_t getNearestIndex(experimental::KD_Tree& tree, WorldVector<double>& x)
      {
	// do a knn search
	const size_t num_nnp = 1;
	std::vector<size_t>   ret_indexes(num_nnp);
	std::vector<double> out_dists_sqr(num_nnp);

	nanoflann::KNNResultSet<double> resultSet(num_nnp);
	resultSet.init(&ret_indexes[0], &out_dists_sqr[0] );

	tree.index->findNeighbors(resultSet, x.begin(), nanoflann::SearchParams(10));
	return ret_indexes[0];
      }

    } // end namespace details
    
  } // end namespace VtuReader
  
} // end namespace AMDiS

#include "VtuReader.hh"

#endif // AMDIS_VTUREADER_H