Nektar++
ProcessInterpField.cpp
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // File: ProcessInterpField.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 one field to another.
32 //
33 ////////////////////////////////////////////////////////////////////////////////
34 #include <iostream>
35 #include <string>
36 using namespace std;
37 
38 #include <boost/core/ignore_unused.hpp>
39 #include <boost/geometry.hpp>
40 #include <boost/math/special_functions/fpclassify.hpp>
41 
46 
47 #include "ProcessInterpField.h"
48 
49 namespace bg = boost::geometry;
50 namespace bgi = boost::geometry::index;
51 
52 namespace Nektar
53 {
54 namespace FieldUtils
55 {
56 
57 ModuleKey ProcessInterpField::className =
59  ModuleKey(eProcessModule, "interpfield"),
60  ProcessInterpField::create,
61  "Interpolates one field to another, requires fromxml, "
62  "fromfld to be defined");
63 
64 ProcessInterpField::ProcessInterpField(FieldSharedPtr f) : ProcessModule(f)
65 {
66 
67  m_config["fromxml"] = ConfigOption(
68  false, "NotSet", "Xml file from which to interpolate field");
69  m_config["fromfld"] = ConfigOption(
70  false, "NotSet", "Fld file from which to interpolate field");
71 
72  m_config["clamptolowervalue"] =
73  ConfigOption(false, "-10000000", "Lower bound for interpolation value");
74  m_config["clamptouppervalue"] =
75  ConfigOption(false, "10000000", "Upper bound for interpolation value");
76  m_config["defaultvalue"] =
77  ConfigOption(false, "0", "Default value if point is outside domain");
78  m_config["realmodetoimag"] = ConfigOption(
79  false, "NotSet", "Take fields as sin mode");
80 }
81 
83 {
84 }
85 
86 void ProcessInterpField::Process(po::variables_map &vm)
87 {
88  m_f->SetUpExp(vm);
89 
90  FieldSharedPtr fromField = std::shared_ptr<Field>(new Field());
91 
92  std::vector<std::string> files;
93 
94  // set up session file for from field
95  char *argv[] = { const_cast<char *>("FieldConvert"), nullptr };
96  ParseUtils::GenerateVector(m_config["fromxml"].as<string>(), files);
97  fromField->m_session =
99  1, argv, files,
100  LibUtilities::GetCommFactory().CreateInstance("Serial", 0, 0));
101 
102  // Set up range based on min and max of local parallel partition
105 
106  int numHomoDir = m_f->m_numHomogeneousDir;
107  int coordim = m_f->m_exp[0]->GetCoordim(0) + numHomoDir;
108  int npts = m_f->m_exp[0]->GetTotPoints();
110 
111  for (int i = 0; i < coordim; ++i)
112  {
113  coords[i] = Array<OneD, NekDouble>(npts);
114  }
115 
116  for (int i = coordim; i < 3; ++i)
117  {
118  coords[i] = NullNekDouble1DArray;
119  }
120 
121  m_f->m_exp[0]->GetCoords(coords[0], coords[1], coords[2]);
122 
123  rng->m_checkShape = false;
124  switch (coordim)
125  {
126  case 3:
127  rng->m_doZrange = true;
128  rng->m_zmin = Vmath::Vmin(npts, coords[2], 1);
129  rng->m_zmax = Vmath::Vmax(npts, coords[2], 1);
130  /* Falls through. */
131  case 2:
132  rng->m_doYrange = true;
133  rng->m_ymin = Vmath::Vmin(npts, coords[1], 1);
134  rng->m_ymax = Vmath::Vmax(npts, coords[1], 1);
135  /* Falls through. */
136  case 1:
137  rng->m_doXrange = true;
138  rng->m_xmin = Vmath::Vmin(npts, coords[0], 1);
139  rng->m_xmax = Vmath::Vmax(npts, coords[0], 1);
140  break;
141  default:
142  NEKERROR(ErrorUtil::efatal, "coordim should be <= 3");
143  }
144 
145  // setup rng parameters.
146  fromField->m_graph =
147  SpatialDomains::MeshGraph::Read(fromField->m_session, rng);
148 
149  // Read in local from field partitions
150  const SpatialDomains::ExpansionInfoMap &expansions =
151  fromField->m_graph->GetExpansionInfo();
152 
153  // check for case where no elements are specified on this
154  // parallel partition
155  if (!expansions.size())
156  {
157  return;
158  }
159 
160  Array<OneD, int> ElementGIDs(expansions.size());
161 
162  int i = 0;
163  for (auto &expIt : expansions)
164  {
165  ElementGIDs[i++] = expIt.second->m_geomShPtr->GetGlobalID();
166  }
167 
168  string fromfld = m_config["fromfld"].as<string>();
169  m_f->FieldIOForFile(fromfld)->Import(
170  fromfld, fromField->m_fielddef, fromField->m_data,
172 
173  int fromNumHomoDir = fromField->m_fielddef[0]->m_numHomogeneousDir;
174  for (i=0; i<fromField->m_fielddef.size(); ++i)
175  {
176  int d1 = fromField->m_fielddef[i]->m_basis.size();
177  d1 -= 1;
178  if (d1 >= 0 &&
179  (fromField->m_fielddef[i]->m_basis[d1] ==
181  fromField->m_fielddef[i]->m_basis[d1] ==
183  {
184  fromField->m_fielddef[i]->m_homogeneousZIDs[0] += 2;
185  fromField->m_fielddef[i]->m_numModes[d1] = 4;
186  fromField->m_fielddef[i]->m_basis[d1] = LibUtilities::eFourier;
187  }
188  }
189 
190  //----------------------------------------------
191  // Set up Expansion information to use mode order from field
192  fromField->m_graph->SetExpansionInfo(fromField->m_fielddef);
193 
194  int nfields = fromField->m_fielddef[0]->m_fields.size();
195 
196  fromField->m_exp.resize(nfields);
197  fromField->m_exp[0] =
198  fromField->SetUpFirstExpList(fromNumHomoDir, true);
199 
200  m_f->m_exp.resize(nfields);
201 
202  // declare auxiliary fields.
203  for (i = 1; i < nfields; ++i)
204  {
205  m_f->m_exp[i] = m_f->AppendExpList(numHomoDir);
206  fromField->m_exp[i] = fromField->AppendExpList(fromNumHomoDir);
207  }
208 
209  // load field into expansion in fromfield.
210  set<int> sinmode;
211  if (m_config["realmodetoimag"].as<string>().compare("NotSet"))
212  {
213  vector<int> value;
215  m_config["realmodetoimag"].as<string>(), value),
216  "Failed to interpret realmodetoimag string");
217  for (int j: value)
218  {
219  sinmode.insert(j);
220  }
221  }
222  for (int j = 0; j < nfields; ++j)
223  {
224  for (i = 0; i < fromField->m_fielddef.size(); i++)
225  {
226  fromField->m_exp[j]->ExtractDataToCoeffs(
227  fromField->m_fielddef[i], fromField->m_data[i],
228  fromField->m_fielddef[0]->m_fields[j],
229  fromField->m_exp[j]->UpdateCoeffs());
230  }
231  if (fromNumHomoDir == 1)
232  {
233  fromField->m_exp[j]->SetWaveSpace(true);
234  if (sinmode.count(j))
235  {
236  int Ncoeff = fromField->m_exp[j]->GetPlane(2)->GetNcoeffs();
237  Vmath::Smul(Ncoeff, -1.,
238  fromField->m_exp[j]->GetPlane(2)->GetCoeffs() , 1,
239  fromField->m_exp[j]->GetPlane(3)->UpdateCoeffs(), 1);
240  Vmath::Zero(Ncoeff,
241  fromField->m_exp[j]->GetPlane(2)->UpdateCoeffs(), 1);
242  }
243  }
244  fromField->m_exp[j]->BwdTrans(fromField->m_exp[j]->GetCoeffs(),
245  fromField->m_exp[j]->UpdatePhys());
246  }
247 
248  int nq1 = m_f->m_exp[0]->GetTotPoints();
249 
250  NekDouble clamp_low = m_config["clamptolowervalue"].as<NekDouble>();
251  NekDouble clamp_up = m_config["clamptouppervalue"].as<NekDouble>();
252  NekDouble def_value = m_config["defaultvalue"].as<NekDouble>();
253 
254  for (int i = 0; i < nfields; i++)
255  {
256  for (int j = 0; j < nq1; ++j)
257  {
258  m_f->m_exp[i]->UpdatePhys()[j] = def_value;
259  }
260  }
261 
262  Interpolator interp;
263  if (m_f->m_verbose && m_f->m_comm->TreatAsRankZero())
264  {
266  }
267  interp.Interpolate(fromField->m_exp, m_f->m_exp);
268  if (m_f->m_verbose && m_f->m_comm->TreatAsRankZero())
269  {
270  cout << endl;
271  }
272 
273  for (int i = 0; i < nfields; ++i)
274  {
275  for (int j = 0; j < nq1; ++j)
276  {
277  if (m_f->m_exp[i]->GetPhys()[j] > clamp_up)
278  {
279  m_f->m_exp[i]->UpdatePhys()[j] = clamp_up;
280  }
281  else if (m_f->m_exp[i]->GetPhys()[j] < clamp_low)
282  {
283  m_f->m_exp[i]->UpdatePhys()[j] = clamp_low;
284  }
285  }
286  m_f->m_exp[i]->FwdTrans_IterPerExp(
287  m_f->m_exp[i]->GetPhys(), m_f->m_exp[i]->UpdateCoeffs());
288  }
289  // save field names
290  m_f->m_variables = fromField->m_fielddef[0]->m_fields;
291 }
292 
293 void ProcessInterpField::PrintProgressbar(const int position,
294  const int goal) const
295 {
296  LibUtilities::PrintProgressbar(position, goal, "Interpolating");
297 }
298 }
299 }
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:216
#define NEKERROR(type, msg)
Assert Level 0 – Fundamental assert which is used whether in FULLDEBUG, DEBUG or OPT compilation mode...
Definition: ErrorUtil.hpp:209
A class that contains algorithms for interpolation between pts fields, expansions and different meshe...
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.
FieldSharedPtr m_f
Field object.
Definition: Module.h:230
std::map< std::string, ConfigOption > m_config
List of configuration values.
Definition: Module.h:233
void PrintProgressbar(const int position, const int goal) const
virtual void Process(po::variables_map &vm)
Write mesh to output file.
Abstract base class for processing modules.
Definition: Module.h:265
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:200
static SessionReaderSharedPtr CreateInstance(int argc, char *argv[])
Creates an instance of the SessionReader class.
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:135
static MeshGraphSharedPtr Read(const LibUtilities::SessionReaderSharedPtr pSession, LibUtilities::DomainRangeShPtr rng=LibUtilities::NullDomainRangeShPtr, bool fillGraph=true)
Definition: MeshGraph.cpp:113
std::shared_ptr< Field > FieldSharedPtr
Definition: Field.hpp:989
std::pair< ModuleType, std::string > ModuleKey
Definition: Module.h:290
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
static FieldMetaDataMap NullFieldMetaDataMap
Definition: FieldIO.h:53
std::shared_ptr< DomainRange > DomainRangeShPtr
Definition: DomainRange.h:61
CommFactory & GetCommFactory()
@ eFourierHalfModeIm
Fourier Modified expansions with just the imaginary part of the first mode
Definition: BasisType.h:61
@ eFourierHalfModeRe
Fourier Modified expansions with just the real part of the first mode
Definition: BasisType.h:60
@ eFourier
Fourier Expansion .
Definition: BasisType.h:53
std::map< int, ExpansionInfoShPtr > ExpansionInfoMap
Definition: MeshGraph.h:143
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:1
static Array< OneD, NekDouble > NullNekDouble1DArray
double NekDouble
T Vmin(int n, const T *x, const int incx)
Return the minimum element in x - called vmin to avoid conflict with min.
Definition: Vmath.cpp:992
void Smul(int n, const T alpha, const T *x, const int incx, T *y, const int incy)
Scalar multiply y = alpha*x.
Definition: Vmath.cpp:225
void Zero(int n, T *x, const int incx)
Zero vector.
Definition: Vmath.cpp:436
T Vmax(int n, const T *x, const int incx)
Return the maximum element in x – called vmax to avoid conflict with max.
Definition: Vmath.cpp:892
Represents a command-line configuration option.
Definition: Module.h:134