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