Nektar++
ProcessMapping.cpp
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // File: ProcessMapping.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: Add mapping coordinates to field
32 //
33 ////////////////////////////////////////////////////////////////////////////////
34 
35 #include <iostream>
36 #include <string>
37 using namespace std;
38 
39 #include <boost/core/ignore_unused.hpp>
40 
42 
43 #include "ProcessMapping.h"
44 
45 namespace Nektar
46 {
47 namespace FieldUtils
48 {
49 ModuleKey ProcessMapping::className =
51  ModuleKey(eProcessModule, "mapping"), ProcessMapping::create,
52  "Add mapping coordinates to output file.");
53 
54 ProcessMapping::ProcessMapping(FieldSharedPtr f) : ProcessModule(f)
55 {
56 }
57 
59 {
60 }
61 
62 void ProcessMapping::v_Process(po::variables_map &vm)
63 {
64  m_f->SetUpExp(vm);
65 
66  // Determine dimensions of mesh, solution, etc...
67  int npoints = m_f->m_exp[0]->GetNpoints();
68  int expdim = m_f->m_graph->GetMeshDimension();
69  int spacedim = expdim;
70  if ((m_f->m_numHomogeneousDir) == 1 || (m_f->m_numHomogeneousDir) == 2)
71  {
72  spacedim = 3;
73  }
74  int nfields = m_f->m_variables.size();
75  int addfields = expdim;
76 
77  string fieldNames[3] = {"xCoord", "yCoord", "zCoord"};
78  for (int i = 0; i < addfields; ++i)
79  {
80  m_f->m_variables.push_back(fieldNames[i]);
81  }
82 
83  // Skip in case of empty partition
84  if (m_f->m_exp[0]->GetNumElmts() == 0)
85  {
86  return;
87  }
88 
89  m_f->m_exp.resize(nfields + addfields);
90 
91  // Load mapping
93 
94  // Convert velocity to Cartesian system
95  if (m_f->m_fieldMetaDataMap.count("MappingCartesianVel"))
96  {
97  if (m_f->m_fieldMetaDataMap["MappingCartesianVel"] == "False")
98  {
99  m_f->m_fieldMetaDataMap["MappingCartesianVel"] = "True";
100 
101  Array<OneD, Array<OneD, NekDouble>> vel(spacedim);
102  // Initialize arrays and copy velocity
103  for (int i = 0; i < spacedim; ++i)
104  {
105  vel[i] = Array<OneD, NekDouble>(npoints);
106  if (m_f->m_exp[0]->GetWaveSpace())
107  {
108  m_f->m_exp[0]->HomogeneousBwdTrans(
109  npoints, m_f->m_exp[i]->GetPhys(), vel[i]);
110  }
111  else
112  {
113  Vmath::Vcopy(npoints, m_f->m_exp[i]->GetPhys(), 1, vel[i],
114  1);
115  }
116  }
117  // Convert velocity to cartesian system
118  mapping->ContravarToCartesian(vel, vel);
119  // Copy result back
120  for (int i = 0; i < spacedim; ++i)
121  {
122  if (m_f->m_exp[0]->GetWaveSpace())
123  {
124  m_f->m_exp[0]->HomogeneousFwdTrans(
125  npoints, vel[i], m_f->m_exp[i]->UpdatePhys());
126  }
127  else
128  {
129  Vmath::Vcopy(npoints, vel[i], 1,
130  m_f->m_exp[i]->UpdatePhys(), 1);
131  }
132  m_f->m_exp[i]->FwdTransLocalElmt(m_f->m_exp[i]->GetPhys(),
133  m_f->m_exp[i]->UpdateCoeffs());
134  }
135  }
136  }
137 
138  // Get coordinates from mapping
140  mapping->GetCartesianCoordinates(coords[0], coords[1], coords[2]);
141 
142  // Add new information to m_f
143  for (int i = 0; i < addfields; ++i)
144  {
145  m_f->m_exp[nfields + i] = m_f->AppendExpList(m_f->m_numHomogeneousDir);
146  Vmath::Vcopy(npoints, coords[i], 1,
147  m_f->m_exp[nfields + i]->UpdatePhys(), 1);
148  m_f->m_exp[nfields + i]->FwdTransLocalElmt(
149  coords[i], m_f->m_exp[nfields + i]->UpdateCoeffs());
150  }
151 }
152 
154 {
155  // Create mapping object
157  field[0] = f->m_exp[0];
159  GlobalMapping::Mapping::Load(f->m_session, field);
160 
161  // Get time from metadata
162  NekDouble time;
163  if (f->m_fieldMetaDataMap.count("Time"))
164  {
165  string s_time = f->m_fieldMetaDataMap["Time"];
166  time = atof(s_time.c_str());
167  }
168  else
169  {
170  time = 0.0;
171  }
172 
173  // Get field information
174  int npoints = f->m_exp[0]->GetNpoints();
175  int expdim = f->m_graph->GetMeshDimension();
176  int spacedim = expdim + f->m_numHomogeneousDir;
177 
178  // Declare coordinates storage
179  Array<OneD, Array<OneD, NekDouble>> coords_new(3);
180  Array<OneD, Array<OneD, NekDouble>> coords_vel(3);
181  for (int i = 0; i < 3; i++)
182  {
183  coords_new[i] = Array<OneD, NekDouble>(npoints);
184  coords_vel[i] = Array<OneD, NekDouble>(npoints, 0.0);
185  }
186 
187  string fieldNames[3] = {"x", "y", "z"};
188  string velFieldNames[3] = {"vx", "vy", "vz"};
189 
190  // Evaluate coordinates and coordinates velocity
191  if (f->m_fieldMetaDataMap.count("MappingType"))
192  {
193  if (f->m_fieldMetaDataMap["MappingType"] == "Expression")
194  {
195  // Get name of the functions
196  string funcName;
197  string velFuncName;
198  if (f->m_fieldMetaDataMap.count("MappingExpression"))
199  {
200  funcName = f->m_fieldMetaDataMap["MappingExpression"];
201  }
202  else
203  {
204  funcName = "";
205  }
206  if (f->m_fieldMetaDataMap.count("MappingVelExpression"))
207  {
208  velFuncName = f->m_fieldMetaDataMap["MappingVelExpression"];
209  }
210  else
211  {
212  velFuncName = "";
213  }
214 
215  // Get original coordinates (in case some of them are not changed)
217  for (int i = 0; i < 3; i++)
218  {
219  coords[i] = Array<OneD, NekDouble>(npoints);
220  }
221  f->m_exp[0]->GetCoords(coords[0], coords[1], coords[2]);
222 
223  // Load coordinates
224  std::string s_FieldStr;
225  for (int i = 0; i < 3; i++)
226  {
227  s_FieldStr = fieldNames[i];
228  if (f->m_session->DefinesFunction(funcName, s_FieldStr))
229  {
231  f->m_session->GetFunction(funcName, s_FieldStr);
232  ffunc->Evaluate(coords[0], coords[1], coords[2], time,
233  coords_new[i]);
234  }
235  else
236  {
237  // This coordinate is not defined, so use (x^i)' = x^i
238  Vmath::Vcopy(npoints, coords[i], 1, coords_new[i], 1);
239  }
240  }
241  // Load velocities
242  if (f->m_session->DefinesFunction(velFuncName))
243  {
244  for (int i = 0; i < 3; i++)
245  {
246  s_FieldStr = velFieldNames[i];
247  if (f->m_session->DefinesFunction(velFuncName, s_FieldStr))
248  {
250  f->m_session->GetFunction(velFuncName, s_FieldStr);
251  ffunc->Evaluate(coords[0], coords[1], coords[2], time,
252  coords_vel[i]);
253  }
254  }
255  }
256 
257  // Update mapping with coordinates
258  mapping->SetFromFunction(false);
259  mapping->UpdateMapping(time, coords_new, coords_vel);
260  }
261  else if (f->m_fieldMetaDataMap["MappingType"] == "File")
262  {
263  ASSERTL0(f->m_fieldMetaDataMap.count("FileName"),
264  "FileName parameter for Mapping missing in field file.");
265  string fileName = f->m_fieldMetaDataMap["FileName"];
266  std::vector<LibUtilities::FieldDefinitionsSharedPtr> FieldDef;
267  std::vector<std::vector<NekDouble>> FieldData;
268 
269  f->FieldIOForFile(fileName)->Import(fileName, FieldDef, FieldData);
270 
271  for (int j = 0; j < spacedim; ++j)
272  {
273  int ncoeffs = f->m_exp[0]->GetNcoeffs();
274  Array<OneD, NekDouble> fieldcoeffs(ncoeffs, 0.0);
275  for (int i = 0; i < FieldData.size(); ++i)
276  {
277  f->m_exp[j]->ExtractDataToCoeffs(
278  FieldDef[i], FieldData[i], fieldNames[j], fieldcoeffs);
279  }
280  bool wavespace = f->m_exp[0]->GetWaveSpace();
281  f->m_exp[0]->SetWaveSpace(false);
282 
283  f->m_exp[0]->BwdTrans(fieldcoeffs, coords_new[j]);
284 
285  // Load coordinate velocity
286  if (std::find(FieldDef[0]->m_fields.begin(),
287  FieldDef[0]->m_fields.end(),
288  velFieldNames[j]) != FieldDef[0]->m_fields.end())
289  {
290  for (int i = 0; i < FieldData.size(); ++i)
291  {
292  f->m_exp[j]->ExtractDataToCoeffs(
293  FieldDef[i], FieldData[i], velFieldNames[j],
294  fieldcoeffs);
295  }
296  f->m_exp[0]->BwdTrans(fieldcoeffs, coords_vel[j]);
297  }
298  f->m_exp[0]->SetWaveSpace(wavespace);
299  }
300  // Update mapping with coordinates
301  mapping->SetFromFunction(false);
302  mapping->UpdateMapping(time, coords_new, coords_vel);
303  }
304  }
305  else
306  {
307  // Use trivial mapping
309  Array<OneD, Array<OneD, NekDouble>> coords_vel(3);
310  for (int i = 0; i < 3; i++)
311  {
312  coords[i] = Array<OneD, NekDouble>(npoints);
313  coords_vel[i] = Array<OneD, NekDouble>(npoints, 0.0);
314  }
315  f->m_exp[0]->GetCoords(coords[0], coords[1], coords[2]);
316  mapping->SetFromFunction(false);
317  mapping->UpdateMapping(time, coords, coords_vel);
318  }
319 
320  return mapping;
321 }
322 } // namespace FieldUtils
323 } // namespace Nektar
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:215
FieldSharedPtr m_f
Field object.
Definition: Module.h:234
virtual void v_Process(po::variables_map &vm) override
Write mesh to output file.
static GlobalMapping::MappingSharedPtr GetMapping(FieldSharedPtr f)
Abstract base class for processing modules.
Definition: Module.h:292
static GLOBAL_MAPPING_EXPORT MappingSharedPtr Load(const LibUtilities::SessionReaderSharedPtr &pSession, const Array< OneD, MultiRegions::ExpListSharedPtr > &pFields)
Return a pointer to the mapping, creating it on first call.
Definition: Mapping.cpp:270
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, std::string pDesc="")
Register a class with the factory.
Definition: NekFactory.hpp:198
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
GLOBAL_MAPPING_EXPORT typedef std::shared_ptr< Mapping > MappingSharedPtr
A shared pointer to a Mapping object.
Definition: Mapping.h:50
std::shared_ptr< Equation > EquationSharedPtr
Definition: Equation.h:129
InputIterator find(InputIterator first, InputIterator last, InputIterator startingpoint, const EqualityComparable &value)
Definition: StdRegions.hpp:444
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:2
double NekDouble
void Vcopy(int n, const T *x, const int incx, T *y, const int incy)
Definition: Vmath.cpp:1255