Nektar++
ProcessGrad.cpp
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // File: ProcessGrad.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: Computes gradient of fields.
32 //
33 ////////////////////////////////////////////////////////////////////////////////
34 
35 #include <iostream>
36 #include <string>
37 using namespace std;
38 
39 #include <boost/core/ignore_unused.hpp>
40 
41 #include <GlobalMapping/Mapping.h>
44 
45 #include "ProcessGrad.h"
46 #include "ProcessMapping.h"
47 
48 namespace Nektar
49 {
50 namespace FieldUtils
51 {
52 
53 ModuleKey ProcessGrad::className = GetModuleFactory().RegisterCreatorFunction(
54  ModuleKey(eProcessModule, "gradient"), ProcessGrad::create,
55  "Computes gradient of fields.");
56 
57 ProcessGrad::ProcessGrad(FieldSharedPtr f) : ProcessModule(f)
58 {
59  m_config["vars"] = ConfigOption(false, "NotSet", "Select variables");
60  m_config["dirs"] = ConfigOption(false, "NotSet", "Select directions");
61 }
62 
64 {
65 }
66 
67 void ProcessGrad::ParserOptions(std::set<int> &variables,
68  std::set<int> &directions)
69 {
70  if (m_config["vars"].as<string>().compare("NotSet"))
71  {
72  ParseUtils::GenerateVariableSet(m_config["vars"].as<string>(),
73  m_f->m_variables, variables);
74  }
75  else
76  {
77  for (int v = 0; v < m_f->m_variables.size(); ++v)
78  {
79  variables.insert(v);
80  }
81  }
82  vector<string> coords = {"x", "y", "z"};
83  int spacedim = m_f->m_numHomogeneousDir + m_f->m_graph->GetMeshDimension();
84  coords.resize(spacedim);
85  if (m_config["dirs"].as<string>().compare("NotSet"))
86  {
87  ParseUtils::GenerateVariableSet(m_config["dirs"].as<string>(), coords,
88  directions);
89  }
90  else
91  {
92  for (int d = 0; d < spacedim; ++d)
93  {
94  directions.insert(d);
95  }
96  }
97 }
98 
100 {
101  // Calculate Gradient
102  int n = 0;
103  for (int i : m_selectedVars)
104  {
105  for (int j : m_directions)
106  {
107  m_f->m_exp[i]->PhysDeriv(MultiRegions::DirCartesianMap[j],
108  m_f->m_exp[i]->GetPhys(), grad[n]);
109  ++n;
110  }
111  }
112 }
113 
115 {
116  int expdim = m_f->m_graph->GetMeshDimension();
117  int spacedim = m_f->m_numHomogeneousDir + expdim;
118  int nfields = m_f->m_variables.size();
119  bool hasvel = m_selectedVars.size() && (*m_selectedVars.begin() < spacedim);
120 
121  int npoints = m_f->m_exp[0]->GetNpoints();
122 
123  Array<OneD, Array<OneD, NekDouble>> tmp(spacedim);
124  for (int i = 0; i < spacedim; i++)
125  {
126  tmp[i] = Array<OneD, NekDouble>(npoints);
127  }
128 
129  // Get mapping
131 
132  // Get velocity and convert to Cartesian system,
133  // if it is still in transformed system
134  Array<OneD, Array<OneD, NekDouble>> vel(spacedim);
135  if (hasvel && m_f->m_fieldMetaDataMap.count("MappingCartesianVel"))
136  {
137  if (m_f->m_fieldMetaDataMap["MappingCartesianVel"] == "False")
138  {
139  // Initialize arrays and copy velocity
140  for (int i = 0; i < spacedim; ++i)
141  {
142  vel[i] = Array<OneD, NekDouble>(npoints);
143  if (m_f->m_exp[0]->GetWaveSpace())
144  {
145  m_f->m_exp[0]->HomogeneousBwdTrans(
146  npoints, m_f->m_exp[i]->GetPhys(), vel[i]);
147  }
148  else
149  {
150  Vmath::Vcopy(npoints, m_f->m_exp[i]->GetPhys(), 1, vel[i],
151  1);
152  }
153  }
154  // Convert velocity to cartesian system
155  mapping->ContravarToCartesian(vel, vel);
156  // Convert back to wavespace if necessary
157  if (m_f->m_exp[0]->GetWaveSpace())
158  {
159  for (int i = 0; i < spacedim; ++i)
160  {
161  m_f->m_exp[0]->HomogeneousFwdTrans(npoints, vel[i], vel[i]);
162  }
163  }
164  }
165  else
166  {
167  for (int i = 0; i < spacedim; ++i)
168  {
169  vel[i] = Array<OneD, NekDouble>(npoints);
170  Vmath::Vcopy(npoints, m_f->m_exp[i]->GetPhys(), 1, vel[i], 1);
171  }
172  }
173  }
174  else if (hasvel)
175  {
176  for (int i = 0; i < spacedim && i < nfields; ++i)
177  {
178  vel[i] = Array<OneD, NekDouble>(npoints);
179  Vmath::Vcopy(npoints, m_f->m_exp[i]->GetPhys(), 1, vel[i], 1);
180  }
181  }
182 
183  // Calculate Gradient
184  int n = 0;
185  for (int i : m_selectedVars)
186  {
187  for (int j = 0; j < spacedim; ++j)
188  {
189  if (i < spacedim)
190  {
191  m_f->m_exp[i]->PhysDeriv(MultiRegions::DirCartesianMap[j],
192  vel[i], tmp[j]);
193  }
194  else
195  {
196  m_f->m_exp[i]->PhysDeriv(MultiRegions::DirCartesianMap[j],
197  m_f->m_exp[i]->GetPhys(), tmp[j]);
198  }
199  }
200  mapping->CovarToCartesian(tmp, tmp);
201  for (int j : m_directions)
202  {
203  Vmath::Vcopy(npoints, tmp[j], 1, grad[n], 1);
204  ++n;
205  }
206  }
207 }
208 
209 void ProcessGrad::v_Process(po::variables_map &vm)
210 {
211  m_f->SetUpExp(vm);
213 
214  int nfields = m_f->m_variables.size();
215  int addfields = m_selectedVars.size() * m_directions.size();
216  m_f->m_exp.resize(nfields + addfields);
217 
218  vector<string> coords = {"x", "y", "z"};
219  for (int i : m_selectedVars)
220  {
221  for (int j : m_directions)
222  {
223  m_f->m_variables.push_back(m_f->m_variables[i] + "_" + coords[j]);
224  }
225  }
226 
227  // Skip in case of empty partition
228  if (m_f->m_exp[0]->GetNumElmts() == 0)
229  {
230  return;
231  }
232 
233  int npoints = m_f->m_exp[0]->GetNpoints();
234  Array<OneD, Array<OneD, NekDouble>> grad(addfields);
235  for (int i = 0; i < addfields; ++i)
236  {
237  grad[i] = Array<OneD, NekDouble>(npoints);
238  }
239 
240  if (m_f->m_fieldMetaDataMap.count("MappingType"))
241  {
242  ProcessMappingFld(grad);
243  }
244  else
245  {
246  ProcessCartesianFld(grad);
247  }
248 
249  for (int i = 0; i < addfields; ++i)
250  {
251  m_f->m_exp[nfields + i] = m_f->AppendExpList(m_f->m_numHomogeneousDir);
252 
253  Vmath::Vcopy(npoints, grad[i], 1, m_f->m_exp[nfields + i]->UpdatePhys(),
254  1);
255  m_f->m_exp[nfields + i]->FwdTransLocalElmt(
256  grad[i], m_f->m_exp[nfields + i]->UpdateCoeffs());
257  }
258 }
259 } // namespace FieldUtils
260 } // namespace Nektar
FieldSharedPtr m_f
Field object.
Definition: Module.h:234
std::map< std::string, ConfigOption > m_config
List of configuration values.
Definition: Module.h:263
void ProcessCartesianFld(Array< OneD, Array< OneD, NekDouble >> &grad)
Definition: ProcessGrad.cpp:99
std::set< int > m_selectedVars
Definition: ProcessGrad.h:85
void ProcessMappingFld(Array< OneD, Array< OneD, NekDouble >> &grad)
void ParserOptions(std::set< int > &variables, std::set< int > &directions)
Definition: ProcessGrad.cpp:67
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
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, std::string pDesc="")
Register a class with the factory.
Definition: NekFactory.hpp:198
static bool GenerateVariableSet(const std::string &str, const std::vector< std::string > &variables, std::set< int > &out)
Generate a set of variable locations.
Definition: ParseUtils.cpp:166
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
MultiRegions::Direction const DirCartesianMap[]
Definition: ExpList.h:91
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:2
void Vcopy(int n, const T *x, const int incx, T *y, const int incy)
Definition: Vmath.cpp:1255
Represents a command-line configuration option.
Definition: Module.h:131