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