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>
37using 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
51
52namespace Nektar
53{
54namespace FieldUtils
55{
56
59 ModuleKey(eProcessModule, "interpptstopts"),
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
66 : ProcessModule(f)
67{
68 m_config["topts"] =
69 ConfigOption(false, "NotSet", "Pts file to which interpolate field");
70 m_config["line"] = ConfigOption(false, "NotSet",
71 "Specify a line of N points using "
72 "line=N,x0,y0,z0,z1,y1,z1");
73 m_config["plane"] =
74 ConfigOption(false, "NotSet",
75 "Specify a plane of N1 x N2 points using "
76 "plane=N1,N2,x0,y0,z0,z1,y1,z1,x2,y2,z2,x3,y3,z3");
77 m_config["box"] =
78 ConfigOption(false, "NotSet",
79 "Specify a rectangular box of N1 x N2 x N3 points "
80 "using a box of points limited by box="
81 "N1,N2,N3,xmin,xmax,ymin,ymax,zmin,zmax");
82
83 m_config["clamptolowervalue"] =
84 ConfigOption(false, "-10000000", "Lower bound for interpolation value");
85 m_config["clamptouppervalue"] =
86 ConfigOption(false, "10000000", "Upper bound for interpolation value");
87 m_config["defaultvalue"] =
88 ConfigOption(false, "0", "Default value if point is outside domain");
89
90 m_config["cp"] =
91 ConfigOption(false, "NotSet",
92 "Parameters p0 and q to determine pressure coefficients");
93}
94
96{
97}
98
99void ProcessInterpPtsToPts::v_Process(po::variables_map &vm)
100{
102 "Should have a PtsField for ProcessInterpPtsToPts.");
103 ASSERTL0(m_f->m_comm->GetSpaceComm()->GetSize() == 1,
104 "ProcessInterpPtsToPts not implemented in parallel.");
105
106 // Move m_f->m_fieldPts
107 LibUtilities::PtsFieldSharedPtr oldPts = m_f->m_fieldPts;
108 m_f->m_fieldPts = LibUtilities::NullPtsField;
109
110 // Create new fieldPts
111 CreateFieldPts(vm);
112
113 int nfields = m_f->m_variables.size();
114 for (int j = 0; j < nfields; ++j)
115 {
116 Array<OneD, NekDouble> newPts(m_f->m_fieldPts->GetNpoints());
117 m_f->m_fieldPts->AddField(newPts, m_f->m_variables[j]);
118 }
119
120 NekDouble clamp_low = m_config["clamptolowervalue"].as<NekDouble>();
121 NekDouble clamp_up = m_config["clamptouppervalue"].as<NekDouble>();
122 NekDouble def_value = m_config["defaultvalue"].as<NekDouble>();
123
124 InterpolatePtsToPts(oldPts, m_f->m_fieldPts, clamp_low, clamp_up,
125 def_value);
126
127 if (!boost::iequals(m_config["cp"].as<string>(), "NotSet"))
128 {
129 calcCp0();
130 }
131}
132
133void ProcessInterpPtsToPts::CreateFieldPts(po::variables_map &vm)
134{
135 boost::ignore_unused(vm);
136
137 int rank = m_f->m_comm->GetSpaceComm()->GetRank();
138 int nprocs = m_f->m_comm->GetSpaceComm()->GetSize();
139 // Check for command line point specification
140 if (m_config["topts"].as<string>().compare("NotSet") != 0)
141 {
142 string inFile = m_config["topts"].as<string>();
143
144 if (boost::filesystem::path(inFile).extension() == ".pts")
145 {
148 m_f->m_comm);
149
150 ptsIO->Import(inFile, m_f->m_fieldPts);
151 }
152 else if (boost::filesystem::path(inFile).extension() == ".csv")
153 {
156 m_f->m_comm);
157
158 csvIO->Import(inFile, m_f->m_fieldPts);
159 }
160 else
161 {
162 ASSERTL0(false, "unknown topts file type");
163 }
164 }
165 else if (m_config["line"].as<string>().compare("NotSet") != 0)
166 {
167 vector<NekDouble> values;
168 ASSERTL0(
169 ParseUtils::GenerateVector(m_config["line"].as<string>(), values),
170 "Failed to interpret line string");
171
172 ASSERTL0(values.size() > 2, "line string should contain 2*Dim+1 values "
173 "N,x0,y0,z0,x1,y1,z1");
174
175 double tmp;
176 ASSERTL0(std::modf(values[0], &tmp) == 0.0, "N is not an integer");
177 ASSERTL0(values[0] > 1, "N is not a valid number");
178
179 int dim = (values.size() - 1) / 2;
180 int npts = values[0];
181
182 // Information for partitioning
183 int ptsPerProc = npts / nprocs;
184 int extraPts = (rank < nprocs - 1) ? 0 : npts % nprocs;
185 int locPts = ptsPerProc + extraPts;
186 int start = rank * ptsPerProc;
187 int end = start + locPts;
188
190 Array<OneD, NekDouble> delta(dim);
191 for (int i = 0; i < dim; ++i)
192 {
193 pts[i] = Array<OneD, NekDouble>(locPts);
194 delta[i] = (values[dim + i + 1] - values[i + 1]) / (npts - 1);
195 }
196
197 for (int i = 0, cntLoc = 0; i < npts; ++i)
198 {
199 if (i >= start && i < end)
200 {
201 for (int n = 0; n < dim; ++n)
202 {
203 pts[n][cntLoc] = values[n + 1] + i * delta[n];
204 }
205 ++cntLoc;
206 }
207 }
208
209 vector<size_t> ppe;
210 ppe.push_back(npts);
211 m_f->m_fieldPts =
213 m_f->m_fieldPts->SetPtsType(LibUtilities::ePtsLine);
214 m_f->m_fieldPts->SetPointsPerEdge(ppe);
215 }
216 else if (m_config["plane"].as<string>().compare("NotSet") != 0)
217 {
218 vector<NekDouble> values;
219 ASSERTL0(
220 ParseUtils::GenerateVector(m_config["plane"].as<string>(), values),
221 "Failed to interpret plane string");
222
223 ASSERTL0(values.size() > 9,
224 "plane string should contain 4 Dim+2 values "
225 "N1,N2,x0,y0,z0,x1,y1,z1,x2,y2,z2,x3,y3,z3");
226
227 double tmp;
228 ASSERTL0(std::modf(values[0], &tmp) == 0.0, "N1 is not an integer");
229 ASSERTL0(std::modf(values[1], &tmp) == 0.0, "N2 is not an integer");
230
231 ASSERTL0(values[0] > 1, "N1 is not a valid number");
232 ASSERTL0(values[1] > 1, "N2 is not a valid number");
233
234 int dim = (values.size() - 2) / 4;
235
236 Array<OneD, int> npts(2);
237 npts[0] = values[0];
238 npts[1] = values[1];
239
240 int totpts = npts[0] * npts[1];
241
242 // Information for partitioning
243 int ptsPerProc = totpts / nprocs;
244 int extraPts = (rank < nprocs - 1) ? 0 : totpts % nprocs;
245 int locPts = ptsPerProc + extraPts;
246 int start = rank * ptsPerProc;
247 int end = start + locPts;
248
250 Array<OneD, NekDouble> delta1(dim);
251 Array<OneD, NekDouble> delta2(dim);
252 for (int i = 0; i < dim; ++i)
253 {
254 pts[i] = Array<OneD, NekDouble>(locPts);
255 delta1[i] = (values[2 + 1 * dim + i] - values[2 + 0 * dim + i]) /
256 (npts[0] - 1);
257 delta2[i] = (values[2 + 2 * dim + i] - values[2 + 3 * dim + i]) /
258 (npts[0] - 1);
259 }
260
261 for (int j = 0, cnt = 0, cntLoc = 0; j < npts[1]; ++j)
262 {
263 for (int i = 0; i < npts[0]; ++i, ++cnt)
264 {
265 if (cnt >= start && cnt < end)
266 {
267 for (int n = 0; n < dim; ++n)
268 {
269 pts[n][cntLoc] =
270 (values[2 + n] + i * delta1[n]) *
271 (1.0 - j / ((NekDouble)(npts[1] - 1))) +
272 (values[2 + 3 * dim + n] + i * delta2[n]) *
273 (j / ((NekDouble)(npts[1] - 1)));
274 }
275 ++cntLoc;
276 }
277 }
278 }
279
280 vector<size_t> ppe;
281 ppe.push_back(npts[0]);
282 ppe.push_back(npts[1]);
283 m_f->m_fieldPts =
285 m_f->m_fieldPts->SetPtsType(LibUtilities::ePtsPlane);
286 m_f->m_fieldPts->SetPointsPerEdge(ppe);
287 }
288 else if (m_config["box"].as<string>().compare("NotSet") != 0)
289 {
290 vector<NekDouble> values;
291 ASSERTL0(
292 ParseUtils::GenerateVector(m_config["box"].as<string>(), values),
293 "Failed to interpret box string");
294
295 ASSERTL0(values.size() == 9, "box string should contain 9 values "
296 "N1,N2,N3,xmin,xmax,ymin,ymax,zmin,zmax");
297
298 int dim = 3;
299 Array<OneD, int> npts(3);
300 npts[0] = values[0];
301 npts[1] = values[1];
302 npts[2] = values[2];
303
304 int totpts = npts[0] * npts[1] * npts[2];
305
307 Array<OneD, NekDouble> delta(dim);
308
309 // Information for partitioning
310 int ptsPerProc = totpts / nprocs;
311 int extraPts = (rank < nprocs - 1) ? 0 : totpts % nprocs;
312 int locPts = ptsPerProc + extraPts;
313 int start = rank * ptsPerProc;
314 int end = start + locPts;
315
316 for (int i = 0; i < dim; ++i)
317 {
318 pts[i] = Array<OneD, NekDouble>(locPts);
319 delta[i] = (values[4 + 2 * i] - values[3 + 2 * i]) / (npts[i] - 1);
320 }
321
322 for (int k = 0, cnt = 0, cntLoc = 0; k < npts[2]; ++k)
323 {
324 for (int j = 0; j < npts[1]; ++j)
325 {
326 for (int i = 0; i < npts[0]; ++i, ++cnt)
327 {
328 if (cnt >= start && cnt < end)
329 {
330 pts[0][cntLoc] = values[3] + i * delta[0];
331 pts[1][cntLoc] = values[5] + j * delta[1];
332 pts[2][cntLoc] = values[7] + k * delta[2];
333 ++cntLoc;
334 }
335 }
336 }
337 }
338
339 vector<size_t> ppe;
340 ppe.push_back(npts[0]);
341 ppe.push_back(npts[1]);
342 ppe.push_back(npts[2]);
343 m_f->m_fieldPts =
345 m_f->m_fieldPts->SetPtsType(LibUtilities::ePtsBox);
346 m_f->m_fieldPts->SetPointsPerEdge(ppe);
347 vector<NekDouble> boxdim;
348 boxdim.assign(&values[3], &values[3] + 6);
349 m_f->m_fieldPts->SetBoxSize(boxdim);
350 }
351 else
352 {
353 ASSERTL0(false,
354 "ProcessInterpPtsToPts requires line, plane or box option.");
355 }
356}
357
361 NekDouble clamp_up, NekDouble def_value)
362{
363 boost::ignore_unused(def_value);
364
365 ASSERTL0(toPts->GetNFields() >= fromPts->GetNFields(),
366 "ptField has too few fields");
367
368 int nfields = fromPts->GetNFields();
369
371 if (m_f->m_comm->GetRank() == 0)
372 {
374 this);
375 }
376 interp.Interpolate(fromPts, toPts);
377 if (m_f->m_comm->GetRank() == 0)
378 {
379 cout << endl;
380 }
381
382 for (int f = 0; f < nfields; ++f)
383 {
384 for (int i = 0; i < toPts->GetNpoints(); ++i)
385 {
386 if (toPts->GetPointVal(f, i) > clamp_up)
387 {
388 toPts->SetPointVal(f, i, clamp_up);
389 }
390 else if (toPts->GetPointVal(f, i) < clamp_low)
391 {
392 toPts->SetPointVal(f, i, clamp_low);
393 }
394 }
395 }
396}
397
399{
400 LibUtilities::PtsFieldSharedPtr pts = m_f->m_fieldPts;
401 int dim = pts->GetDim();
402 int nq1 = pts->GetNpoints();
403 int r, f;
404 int pfield = -1;
405 NekDouble p0, qinv;
406 vector<int> velid;
407
408 vector<NekDouble> values;
409 ASSERTL0(ParseUtils::GenerateVector(m_config["cp"].as<string>(), values),
410 "Failed to interpret cp string");
411
412 ASSERTL0(values.size() == 2, "cp string should contain 2 values "
413 "p0 and q (=1/2 rho u^2)");
414
415 p0 = values[0];
416 qinv = 1.0 / values[1];
417
418 for (int i = 0; i < pts->GetNFields(); ++i)
419 {
420 if (boost::iequals(pts->GetFieldName(i), "p"))
421 {
422 pfield = i;
423 }
424
425 if (boost::iequals(pts->GetFieldName(i), "u") ||
426 boost::iequals(pts->GetFieldName(i), "v") ||
427 boost::iequals(pts->GetFieldName(i), "w"))
428 {
429 velid.push_back(i);
430 }
431 }
432
433 if (pfield != -1)
434 {
435 if (!velid.size())
436 {
437 WARNINGL0(false, "Did not find velocity components for Cp0");
438 }
439 }
440 else
441 {
442 WARNINGL0(false, "Failed to find 'p' field to determine cp0");
443 }
444
445 // Allocate data storage
447
448 for (f = 0; f < 2; ++f)
449 {
450 data[f] = Array<OneD, NekDouble>(nq1, 0.0);
451 }
452
453 for (r = 0; r < nq1; r++)
454 {
455 if (pfield != -1) // calculate cp
456 {
457 data[0][r] = qinv * (pts->GetPointVal(dim + pfield, r) - p0);
458
459 if (velid.size()) // calculate cp0
460 {
461 NekDouble q = 0;
462 for (int i = 0; i < velid.size(); ++i)
463 {
464 q += 0.5 * pts->GetPointVal(dim + velid[i], r) *
465 pts->GetPointVal(dim + velid[i], r);
466 }
467 data[1][r] =
468 qinv * (pts->GetPointVal(dim + pfield, r) + q - p0);
469 }
470 }
471 }
472
473 if (pfield != -1)
474 {
475 pts->AddField(data[0], "Cp");
476 m_f->m_variables.push_back("Cp");
477 if (velid.size())
478 {
479 pts->AddField(data[1], "Cp0");
480 m_f->m_variables.push_back("Cp0");
481 }
482 }
483}
484
486 const int goal) const
487{
488 LibUtilities::PrintProgressbar(position, goal, "Interpolating");
489}
490} // namespace FieldUtils
491} // namespace Nektar
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:215
#define WARNINGL0(condition, msg)
Definition: ErrorUtil.hpp:222
A class that contains algorithms for interpolation between pts fields, expansions and different meshe...
FIELD_UTILS_EXPORT void Interpolate(const T expInField, T &expOutField, NekDouble def_value=0.0)
Interpolate from an expansion to an expansion.
FieldSharedPtr m_f
Field object.
Definition: Module.h:234
std::map< std::string, ConfigOption > m_config
List of configuration values.
Definition: Module.h:263
void InterpolatePtsToPts(LibUtilities::PtsFieldSharedPtr &fromPts, LibUtilities::PtsFieldSharedPtr &toPts, NekDouble clamp_low, NekDouble clamp_up, NekDouble def_value)
static std::shared_ptr< Module > create(FieldSharedPtr f)
Creates an instance of this class.
void PrintProgressbar(const int position, const int goal) const
virtual void v_Process(po::variables_map &vm) override
Write mesh to output file.
Abstract base class for processing modules.
Definition: Module.h:292
void SetProgressCallback(FuncPointerT func, ObjectPointerT obj)
sets a callback funtion which gets called every time the interpolation progresses
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, std::string pDesc="")
Register a class with the factory.
Definition: NekFactory.hpp:198
static std::shared_ptr< DataType > AllocateSharedPtr(const Args &...args)
Allocate a shared pointer from the memory pool.
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:131
std::shared_ptr< Field > FieldSharedPtr
Definition: Field.hpp:991
std::pair< ModuleType, std::string > ModuleKey
Definition: Module.h:317
ModuleFactory & GetModuleFactory()
Definition: Module.cpp:49
int PrintProgressbar(const int position, const int goal, const std::string message, int lastprogress=-1)
Prints a progressbar.
Definition: Progressbar.hpp:67
std::shared_ptr< PtsField > PtsFieldSharedPtr
Definition: PtsField.h:190
std::shared_ptr< CsvIO > CsvIOSharedPtr
Definition: CsvIO.h:80
static PtsFieldSharedPtr NullPtsField
Definition: PtsField.h:191
std::shared_ptr< PtsIO > PtsIOSharedPtr
Definition: PtsIO.h:96
std::vector< double > q(NPUPPER *NPUPPER)
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:2
double NekDouble
Represents a command-line configuration option.
Definition: Module.h:131