Nektar++
ProcessInterpPtsToPts.cpp
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // File: ProcessInterpPtsToPts.cpp
4 //
5 // For more information, please see: http://www.nektar.info/
6 //
7 // The MIT License
8 //
9 // Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10 // Department of Aeronautics, Imperial College London (UK), and Scientific
11 // Computing and Imaging Institute, University of Utah (USA).
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining a
14 // copy of this software and associated documentation files (the "Software"),
15 // to deal in the Software without restriction, including without limitation
16 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
17 // and/or sell copies of the Software, and to permit persons to whom the
18 // Software is furnished to do so, subject to the following conditions:
19 //
20 // The above copyright notice and this permission notice shall be included
21 // in all copies or substantial portions of the Software.
22 //
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29 // DEALINGS IN THE SOFTWARE.
30 //
31 // Description: Interpolate field to a series of specified points.
32 //
33 ////////////////////////////////////////////////////////////////////////////////
34 
35 #include <iostream>
36 #include <string>
37 using namespace std;
38 
39 #include <boost/core/ignore_unused.hpp>
40 #include <boost/lexical_cast.hpp>
41 #include <boost/math/special_functions/fpclassify.hpp>
42 
49 
50 #include "ProcessInterpPtsToPts.h"
51 
52 namespace Nektar
53 {
54 namespace FieldUtils
55 {
56 
57 ModuleKey ProcessInterpPtsToPts::className =
59  ModuleKey(eProcessModule, "interpptstopts"),
60  ProcessInterpPtsToPts::create,
61  "Interpolates a set of points to another, requires fromfld and "
62  "fromxml to be defined, a line, plane or block of points can be "
63  "defined");
64 
65 ProcessInterpPtsToPts::ProcessInterpPtsToPts(FieldSharedPtr f) : ProcessModule(f)
66 {
67  m_config["topts"] = ConfigOption(
68  false, "NotSet", "Pts file to which interpolate field");
69  m_config["line"] = ConfigOption(
70  false, "NotSet", "Specify a line of N points using "
71  "line=N,x0,y0,z0,z1,y1,z1");
72  m_config["plane"] = ConfigOption(
73  false, "NotSet", "Specify a plane of N1 x N2 points using "
74  "plane=N1,N2,x0,y0,z0,z1,y1,z1,x2,y2,z2,x3,y3,z3");
75  m_config["box"] = ConfigOption(
76  false, "NotSet", "Specify a rectangular box of N1 x N2 x N3 points "
77  "using a box of points limited by box="
78  "N1,N2,N3,xmin,xmax,ymin,ymax,zmin,zmax");
79 
80  m_config["clamptolowervalue"] =
81  ConfigOption(false, "-10000000", "Lower bound for interpolation value");
82  m_config["clamptouppervalue"] =
83  ConfigOption(false, "10000000", "Upper bound for interpolation value");
84  m_config["defaultvalue"] =
85  ConfigOption(false, "0", "Default value if point is outside domain");
86 
87  m_config["cp"] =
88  ConfigOption(false, "NotSet",
89  "Parameters p0 and q to determine pressure coefficients");
90 }
91 
93 {
94 }
95 
96 void ProcessInterpPtsToPts::Process(po::variables_map &vm)
97 {
99  "Should have a PtsField for ProcessInterpPtsToPts.");
100  ASSERTL0(m_f->m_comm->GetSize() == 1,
101  "ProcessInterpPtsToPts not implemented in parallel.");
102 
103  // Move m_f->m_fieldPts
104  LibUtilities::PtsFieldSharedPtr oldPts = m_f->m_fieldPts;
105  m_f->m_fieldPts = LibUtilities::NullPtsField;
106 
107  // Create new fieldPts
108  CreateFieldPts(vm);
109 
110  int nfields = m_f->m_variables.size();
111  for (int j = 0; j < nfields; ++j)
112  {
113  Array<OneD, NekDouble> newPts(m_f->m_fieldPts->GetNpoints());
114  m_f->m_fieldPts->AddField(newPts, m_f->m_variables[j]);
115  }
116 
117  NekDouble clamp_low = m_config["clamptolowervalue"].as<NekDouble>();
118  NekDouble clamp_up = m_config["clamptouppervalue"].as<NekDouble>();
119  NekDouble def_value = m_config["defaultvalue"].as<NekDouble>();
120 
121  InterpolatePtsToPts(oldPts, m_f->m_fieldPts, clamp_low,
122  clamp_up, def_value);
123 
124  if (!boost::iequals(m_config["cp"].as<string>(), "NotSet"))
125  {
126  calcCp0();
127  }
128 }
129 
130 void ProcessInterpPtsToPts::CreateFieldPts(po::variables_map &vm)
131 {
132  boost::ignore_unused(vm);
133 
134  int rank = m_f->m_comm->GetRank();
135  int nprocs = m_f->m_comm->GetSize();
136  // Check for command line point specification
137  if (m_config["topts"].as<string>().compare("NotSet") != 0)
138  {
139  string inFile = m_config["topts"].as<string>();
140 
141  if (boost::filesystem::path(inFile).extension() == ".pts")
142  {
145 
146  ptsIO->Import(inFile, m_f->m_fieldPts);
147  }
148  else if (boost::filesystem::path(inFile).extension() == ".csv")
149  {
152 
153  csvIO->Import(inFile, m_f->m_fieldPts);
154  }
155  else
156  {
157  ASSERTL0(false, "unknown topts file type");
158  }
159  }
160  else if (m_config["line"].as<string>().compare("NotSet") != 0)
161  {
162  vector<NekDouble> values;
164  m_config["line"].as<string>(), values),
165  "Failed to interpret line string");
166 
167  ASSERTL0(values.size() > 2,
168  "line string should contain 2*Dim+1 values "
169  "N,x0,y0,z0,x1,y1,z1");
170 
171  double tmp;
172  ASSERTL0(std::modf(values[0], &tmp) == 0.0, "N is not an integer");
173  ASSERTL0(values[0] > 1, "N is not a valid number");
174 
175  int dim = (values.size() - 1) / 2;
176  int npts = values[0];
177 
178  // Information for partitioning
179  int ptsPerProc = npts / nprocs;
180  int extraPts = (rank < nprocs - 1) ? 0: npts % nprocs;
181  int locPts = ptsPerProc + extraPts;
182  int start = rank * ptsPerProc;
183  int end = start + locPts;
184 
186  Array<OneD, NekDouble> delta(dim);
187  for (int i = 0; i < dim; ++i)
188  {
189  pts[i] = Array<OneD, NekDouble>(locPts);
190  delta[i] = (values[dim + i + 1] - values[ i + 1]) / (npts - 1);
191  }
192 
193 
194 
195  for (int i = 0, cntLoc = 0; i < npts; ++i)
196  {
197  if (i >= start && i < end)
198  {
199  for (int n = 0; n < dim; ++n)
200  {
201  pts[n][cntLoc] = values[n+1] + i * delta[n];
202  }
203  ++cntLoc;
204  }
205  }
206 
207  vector<size_t> ppe;
208  ppe.push_back(npts);
209  m_f->m_fieldPts =
211  pts);
212  m_f->m_fieldPts->SetPtsType(LibUtilities::ePtsLine);
213  m_f->m_fieldPts->SetPointsPerEdge(ppe);
214  }
215  else if (m_config["plane"].as<string>().compare("NotSet") != 0)
216  {
217  vector<NekDouble> values;
219  m_config["plane"].as<string>(), values),
220  "Failed to interpret plane string");
221 
222  ASSERTL0(values.size() > 9,
223  "plane string should contain 4 Dim+2 values "
224  "N1,N2,x0,y0,z0,x1,y1,z1,x2,y2,z2,x3,y3,z3");
225 
226  double tmp;
227  ASSERTL0(std::modf(values[0], &tmp) == 0.0, "N1 is not an integer");
228  ASSERTL0(std::modf(values[1], &tmp) == 0.0, "N2 is not an integer");
229 
230  ASSERTL0(values[0] > 1, "N1 is not a valid number");
231  ASSERTL0(values[1] > 1, "N2 is not a valid number");
232 
233  int dim = (values.size() - 2) / 4;
234 
235  Array<OneD, int> npts(2);
236  npts[0] = values[0];
237  npts[1] = values[1];
238 
239  int totpts = npts[0] * npts[1];
240 
241  // Information for partitioning
242  int ptsPerProc = totpts / nprocs;
243  int extraPts = (rank < nprocs - 1) ? 0: totpts % nprocs;
244  int locPts = ptsPerProc + extraPts;
245  int start = rank * ptsPerProc;
246  int end = start + locPts;
247 
249  Array<OneD, NekDouble> delta1(dim);
250  Array<OneD, NekDouble> delta2(dim);
251  for (int i = 0; i < dim; ++i)
252  {
253  pts[i] = Array<OneD, NekDouble>(locPts);
254  delta1[i] = (values[2+1*dim + i] - values[2+0*dim + i])/(npts[0]-1);
255  delta2[i] = (values[2+2*dim + i] - values[2+3*dim + i])/(npts[0]-1);
256  }
257 
258  for (int j = 0, cnt = 0, cntLoc = 0; j < npts[1]; ++j)
259  {
260  for (int i = 0; i < npts[0]; ++i, ++cnt)
261  {
262  if (cnt >= start && cnt < end)
263  {
264  for (int n = 0; n < dim; ++n)
265  {
266  pts[n][cntLoc] =
267  (values[2+n] + i * delta1[n]) *
268  (1.0 - j / ((NekDouble)(npts[1]-1))) +
269  (values[2 + 3*dim + n] + i * delta2[n]) *
270  ( j / ((NekDouble)(npts[1]-1)));
271  }
272  ++cntLoc;
273  }
274  }
275  }
276 
277  vector<size_t> ppe;
278  ppe.push_back(npts[0]);
279  ppe.push_back(npts[1]);
280  m_f->m_fieldPts =
282  pts);
283  m_f->m_fieldPts->SetPtsType(LibUtilities::ePtsPlane);
284  m_f->m_fieldPts->SetPointsPerEdge(ppe);
285  }
286  else if (m_config["box"].as<string>().compare("NotSet") != 0)
287  {
288  vector<NekDouble> values;
290  m_config["box"].as<string>(), values),
291  "Failed to interpret box string");
292 
293  ASSERTL0(values.size() == 9,
294  "box string should contain 9 values "
295  "N1,N2,N3,xmin,xmax,ymin,ymax,zmin,zmax");
296 
297  int dim = 3;
298  Array<OneD, int> npts(3);
299  npts[0] = values[0];
300  npts[1] = values[1];
301  npts[2] = values[2];
302 
303  int totpts = npts[0]*npts[1]*npts[2];
304 
306  Array<OneD, NekDouble> delta(dim);
307 
308  // Information for partitioning
309  int ptsPerProc = totpts / nprocs;
310  int extraPts = (rank < nprocs - 1) ? 0: totpts % nprocs;
311  int locPts = ptsPerProc + extraPts;
312  int start = rank * ptsPerProc;
313  int end = start + locPts;
314 
315  for (int i = 0; i < dim; ++i)
316  {
317  pts[i] = Array<OneD, NekDouble>(locPts);
318  delta[i] = (values[4 + 2*i] - values[3 + 2*i]) / (npts[i] - 1);
319  }
320 
321 
322 
323  for (int k = 0, cnt = 0, cntLoc = 0; k < npts[2]; ++k)
324  {
325  for (int j = 0; j < npts[1]; ++j)
326  {
327  for (int i = 0; i < npts[0]; ++i, ++cnt)
328  {
329  if (cnt >= start && cnt < end)
330  {
331  pts[0][cntLoc] = values[3] + i * delta[0];
332  pts[1][cntLoc] = values[5] + j * delta[1];
333  pts[2][cntLoc] = values[7] + k * delta[2];
334  ++cntLoc;
335  }
336  }
337  }
338  }
339 
340  vector<size_t> ppe;
341  ppe.push_back(npts[0]);
342  ppe.push_back(npts[1]);
343  ppe.push_back(npts[2]);
344  m_f->m_fieldPts =
346  pts);
347  m_f->m_fieldPts->SetPtsType(LibUtilities::ePtsBox);
348  m_f->m_fieldPts->SetPointsPerEdge(ppe);
349  vector<NekDouble> boxdim;
350  boxdim.assign(&values[3], &values[3] + 6);
351  m_f->m_fieldPts->SetBoxSize(boxdim);
352  }
353  else
354  {
355  ASSERTL0(false,
356  "ProcessInterpPtsToPts requires line, plane or box option.");
357  }
358 }
359 
363  NekDouble clamp_low,
364  NekDouble clamp_up,
365  NekDouble def_value)
366 {
367  boost::ignore_unused(def_value);
368 
369  ASSERTL0(toPts->GetNFields() >= fromPts->GetNFields(),
370  "ptField has too few fields");
371 
372  int nfields = fromPts->GetNFields();
373 
374  Interpolator interp;
375  if (m_f->m_comm->GetRank() == 0)
376  {
378  this);
379  }
380  interp.Interpolate(fromPts, toPts);
381  if (m_f->m_comm->GetRank() == 0)
382  {
383  cout << endl;
384  }
385 
386  for (int f = 0; f < nfields; ++f)
387  {
388  for (int i = 0; i < toPts->GetNpoints(); ++i)
389  {
390  if (toPts->GetPointVal(f, i) > clamp_up)
391  {
392  toPts->SetPointVal(f, i, clamp_up);
393  }
394  else if (toPts->GetPointVal(f, i) < clamp_low)
395  {
396  toPts->SetPointVal(f, i, clamp_low);
397  }
398  }
399  }
400 }
401 
403 {
404  LibUtilities::PtsFieldSharedPtr pts = m_f->m_fieldPts;
405  int dim = pts->GetDim();
406  int nq1 = pts->GetNpoints();
407  int r, f;
408  int pfield = -1;
409  NekDouble p0,qinv;
410  vector<int> velid;
411 
412  vector<NekDouble> values;
414  m_config["cp"].as<string>(),values),
415  "Failed to interpret cp string");
416 
417  ASSERTL0(values.size() == 2,
418  "cp string should contain 2 values "
419  "p0 and q (=1/2 rho u^2)");
420 
421  p0 = values[0];
422  qinv = 1.0/values[1];
423 
424  for(int i = 0; i < pts->GetNFields(); ++i)
425  {
426  if(boost::iequals(pts->GetFieldName(i),"p"))
427  {
428  pfield = i;
429  }
430 
431  if(boost::iequals(pts->GetFieldName(i),"u")||
432  boost::iequals(pts->GetFieldName(i),"v")||
433  boost::iequals(pts->GetFieldName(i),"w"))
434  {
435  velid.push_back(i);
436  }
437  }
438 
439  if(pfield != -1)
440  {
441  if(!velid.size())
442  {
443  WARNINGL0(false,"Did not find velocity components for Cp0");
444  }
445  }
446  else
447  {
448  WARNINGL0(false,"Failed to find 'p' field to determine cp0");
449  }
450 
451  // Allocate data storage
453 
454  for (f = 0; f < 2; ++f)
455  {
456  data[f] = Array< OneD, NekDouble>(nq1, 0.0);
457  }
458 
459  for (r = 0; r < nq1; r++)
460  {
461  if(pfield != -1) // calculate cp
462  {
463  data[0][r] = qinv*(pts->GetPointVal(dim + pfield, r) - p0);
464 
465  if(velid.size()) // calculate cp0
466  {
467  NekDouble q = 0;
468  for(int i = 0; i < velid.size(); ++i)
469  {
470  q += 0.5*pts->GetPointVal(dim + velid[i], r)*
471  pts->GetPointVal(dim + velid[i], r);
472  }
473  data[1][r] = qinv*(pts->GetPointVal(dim + pfield, r)+q - p0);
474  }
475  }
476  }
477 
478  if(pfield != -1)
479  {
480  pts->AddField(data[0], "Cp");
481  m_f->m_variables.push_back("Cp");
482  if(velid.size())
483  {
484  pts->AddField(data[1], "Cp0");
485  m_f->m_variables.push_back("Cp0");
486  }
487  }
488 }
489 
491  const int goal) const
492 {
493  LibUtilities::PrintProgressbar(position, goal, "Interpolating");
494 }
495 }
496 }
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:216
A class that contains algorithms for interpolation between pts fields, expansions and different meshe...
int PrintProgressbar(const int position, const int goal, const std::string message, int lastprogress=-1)
Prints a progressbar.
Definition: Progressbar.hpp:67
std::map< std::string, ConfigOption > m_config
List of configuration values.
void SetProgressCallback(FuncPointerT func, ObjectPointerT obj)
sets a callback funtion which gets called every time the interpolation progresses ...
virtual void Process(po::variables_map &vm)
Write mesh to output file.
Represents a command-line configuration option.
void InterpolatePtsToPts(LibUtilities::PtsFieldSharedPtr &fromPts, LibUtilities::PtsFieldSharedPtr &toPts, NekDouble clamp_low, NekDouble clamp_up, NekDouble def_value)
STL namespace.
std::shared_ptr< Field > FieldSharedPtr
Definition: Field.hpp:762
std::shared_ptr< PtsIO > PtsIOSharedPtr
Definition: PtsIO.h:96
std::shared_ptr< PtsField > PtsFieldSharedPtr
Definition: PtsField.h:179
std::pair< ModuleType, std::string > ModuleKey
#define WARNINGL0(condition, msg)
Definition: ErrorUtil.hpp:223
void PrintProgressbar(const int position, const int goal) const
static std::shared_ptr< DataType > AllocateSharedPtr(const Args &...args)
Allocate a shared pointer from the memory pool.
FIELD_UTILS_EXPORT void Interpolate(const std::vector< MultiRegions::ExpListSharedPtr > expInField, std::vector< MultiRegions::ExpListSharedPtr > &expOutField, NekDouble def_value=0.0)
Interpolate from an expansion to an expansion.
double NekDouble
static bool GenerateVector(const std::string &str, std::vector< T > &out)
Takes a comma-separated string and converts it to entries in a vector.
Definition: ParseUtils.cpp:135
static PtsFieldSharedPtr NullPtsField
Definition: PtsField.h:180
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, std::string pDesc="")
Register a class with the factory.
Definition: NekFactory.hpp:199
Abstract base class for processing modules.
std::shared_ptr< CsvIO > CsvIOSharedPtr
Definition: CsvIO.h:81
ModuleFactory & GetModuleFactory()
FieldSharedPtr m_f
Field object.