ug4
invdist_user_data_impl.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015: G-CSC, Goethe University Frankfurt
3  * Author: Dmitry Logashenko
4  *
5  * This file is part of UG4.
6  *
7  * UG4 is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License version 3 (as published by the
9  * Free Software Foundation) with the following additional attribution
10  * requirements (according to LGPL/GPL v3 §7):
11  *
12  * (1) The following notice must be displayed in the Appropriate Legal Notices
13  * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14  *
15  * (2) The following notice must be displayed at a prominent place in the
16  * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17  *
18  * (3) The following bibliography is recommended for citation and must be
19  * preserved in all covered files:
20  * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21  * parallel geometric multigrid solver on hierarchically distributed grids.
22  * Computing and visualization in science 16, 4 (2013), 151-164"
23  * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24  * flexible software system for simulating pde based models on high performance
25  * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26  *
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30  * GNU Lesser General Public License for more details.
31  */
32 
33 /*
34  * Implementation of the Inverse Distance Weighting (IDW) interpolation for data sets
35  */
36 
37 namespace ug {
38 
42 template <int WDim, typename TPntIterator, typename TData>
44 (
45  data_type & res,
46  const MathVector<dim> & pos,
47  t_pnt_iter pnt_beg,
48  t_pnt_iter pnt_end,
49  number order,
50  number small_dist
51 )
52 {
53  if (pnt_beg == pnt_end)
54  UG_THROW ("IDWInterpolation: Cannot interpolate using 0 data points!");
55 
56  data_type sum = 0;
57  number factor = 0;
58  for (t_pnt_iter pnt = pnt_beg; pnt != pnt_end; ++pnt)
59  {
60  number dist;
61  if ((dist = VecDistance (pos, pnt->pos)) < small_dist)
62  { /* We are at a data point: */
63  res = pnt->value;
64  return;
65  }
66  dist = pow (dist, order);
67  data_type value = pnt->value;
68  value /= dist;
69  sum += value;
70  factor += 1 / dist;
71  }
72  sum /= factor;
73  res = sum;
74 }
75 
79 template <int WDim, typename TPntIterator, typename TData>
81 (
82  data_type & res,
83  const MathVector<dim> & pos,
84  number R,
85  t_pnt_iter pnt_beg,
86  t_pnt_iter pnt_end,
87  number order,
88  number small_dist
89 )
90 {
91  if (R == 0.0) // a special case: we do not consider the ball
92  {
94  (res, pos, pnt_beg, pnt_end, order, small_dist);
95  return;
96  }
97 
98  if (pnt_beg == pnt_end)
99  UG_THROW ("IDWInterpolation: Cannot interpolate using 0 data points!");
100 
101  data_type sum = 0;
102  number factor = 0;
103  bool no_data_in_ball = true;
104  for (t_pnt_iter pnt = pnt_beg; pnt != pnt_end; ++pnt)
105  {
106  number dist;
107  if ((dist = VecDistance (pos, pnt->pos)) < small_dist)
108  { /* We are at a data point: */
109  res = pnt->value;
110  return;
111  }
112  if (dist > R) continue;
113  no_data_in_ball = false;
114  dist = pow (dist, order);
115  data_type value = pnt->value;
116  value /= dist;
117  sum += value;
118  factor += 1 / dist;
119  }
120  if (no_data_in_ball)
121  UG_THROW ("IDWInterpolation: No interpolation points in the ball with R = " << R
122  << " and center at" << pos << ".");
123  sum /= factor;
124  res = sum;
125 }
126 
130 template <int WDim, typename TData>
132 {
133  std::string input_line;
134  std::istringstream line_stream;
136  data_type value;
137 
138  line_stream.exceptions (std::istream::failbit | std::istream::badbit);
139 
140  while (! input.eof ())
141  {
142  if (input.fail ())
143  UG_THROW ("IDWUserData: Could not load the interpolation points from the file!");
144  std::getline (input, input_line);
145  if (input_line.length () == 0)
146  continue;
147  try
148  {
149  line_stream.str (input_line);
150  read_plain_txt (line_stream, pos);
151  line_stream >> value;
152  }
153  catch (std::istream::failure& e)
154  {
155  UG_THROW ("IDWUserData: Failed to parse line '" << input_line << "' in the input file.");
156  };
157 
158  this->append (pos, value);
159  }
160 }
161 
165 template <int WDim, typename TData>
166 void IDWUserData<WDim, TData>::load_data_from (const char * file_name)
167 {
168  std::ifstream input (file_name, std::ifstream::in);
169  if (input.fail ())
170  UG_THROW ("IDWUserData: Cannot open data file '" << file_name << "' for input!");
171  this->load_data_from (input);
172 }
173 
174 } // end namespace ug
175 
176 /* End of File */
static void compute(data_type &res, const MathVector< dim > &pos, t_pnt_iter pnt_beg, t_pnt_iter pnt_end, number order, number small_dist=1e-7)
computes the interpolation basing on all the interpolation points
Definition: invdist_user_data_impl.h:44
TPntIterator t_pnt_iter
type of the interpolation point iterator
Definition: invdist_user_data.h:102
TData data_type
type of the data to extrapolate
Definition: invdist_user_data.h:105
void load_data_from(std::istream &in)
loads data from a given stream (and appends the loaded points to the current list)
Definition: invdist_user_data_impl.h:131
TData data_type
type of the data to extrapolate
Definition: invdist_user_data.h:155
function table append(t1, t2)
#define UG_THROW(msg)
Definition: error.h:57
double number
Definition: types.h:124
vector_t::value_type VecDistance(const vector_t &v1, const vector_t &v2)
returns the distance of two vector_ts.
Definition: math_vector_functions_common_impl.hpp:375
std::istream & read_plain_txt(std::istream &inStream, ug::MathVector< 1 > &v)
plain text input of MathVectors: read space-separated coordinates
Definition: math_vector.cpp:95
static int input(void)
the ug namespace