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