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>
43 
44 #include "ProcessGrad.h"
45 #include "ProcessMapping.h"
46 
47 namespace Nektar
48 {
49 namespace FieldUtils
50 {
51 
52 ModuleKey ProcessGrad::className = GetModuleFactory().RegisterCreatorFunction(
53  ModuleKey(eProcessModule, "gradient"), ProcessGrad::create,
54  "Computes gradient of fields.");
55 
56 ProcessGrad::ProcessGrad(FieldSharedPtr f) : ProcessModule(f)
57 {
58 }
59 
61 {
62 }
63 
64 void ProcessGrad::Process(po::variables_map &vm)
65 {
66  m_f->SetUpExp(vm);
67 
68  int i, j;
69  int expdim = m_f->m_graph->GetMeshDimension();
70  int spacedim = m_f->m_numHomogeneousDir + expdim;
71  int nfields = m_f->m_variables.size();
72  int addfields = nfields * spacedim;
73 
74  for (i = 0; i < nfields; ++i)
75  {
76  if (spacedim == 1)
77  {
78  m_f->m_variables.push_back(m_f->m_variables[i] + "_x");
79  }
80  else if (spacedim == 2)
81  {
82  m_f->m_variables.push_back(m_f->m_variables[i] + "_x");
83  m_f->m_variables.push_back(m_f->m_variables[i] + "_y");
84  }
85  else if (spacedim == 3)
86  {
87  m_f->m_variables.push_back(m_f->m_variables[i] + "_x");
88  m_f->m_variables.push_back(m_f->m_variables[i] + "_y");
89  m_f->m_variables.push_back(m_f->m_variables[i] + "_z");
90  }
91  }
92 
93  // Skip in case of empty partition
94  if (m_f->m_exp[0]->GetNumElmts() == 0)
95  {
96  return;
97  }
98 
99  int npoints = m_f->m_exp[0]->GetNpoints();
100  Array<OneD, Array<OneD, NekDouble>> grad(addfields);
101  m_f->m_exp.resize(nfields + addfields);
102 
103  for (i = 0; i < addfields; ++i)
104  {
105  grad[i] = Array<OneD, NekDouble>(npoints);
106  }
107 
108  Array<OneD, Array<OneD, NekDouble>> tmp(spacedim);
109  for (int i = 0; i < spacedim; i++)
110  {
111  tmp[i] = Array<OneD, NekDouble>(npoints);
112  }
113 
114  // Get mapping
116 
117  // Get velocity and convert to Cartesian system,
118  // if it is still in transformed system
119  Array<OneD, Array<OneD, NekDouble>> vel(spacedim);
120  if (m_f->m_fieldMetaDataMap.count("MappingCartesianVel"))
121  {
122  if (m_f->m_fieldMetaDataMap["MappingCartesianVel"] == "False")
123  {
124  // Initialize arrays and copy velocity
125  for (int i = 0; i < spacedim; ++i)
126  {
127  vel[i] = Array<OneD, NekDouble>(npoints);
128  if (m_f->m_exp[0]->GetWaveSpace())
129  {
130  m_f->m_exp[0]->HomogeneousBwdTrans(m_f->m_exp[i]->GetPhys(),
131  vel[i]);
132  }
133  else
134  {
135  Vmath::Vcopy(npoints, m_f->m_exp[i]->GetPhys(), 1, vel[i],
136  1);
137  }
138  }
139  // Convert velocity to cartesian system
140  mapping->ContravarToCartesian(vel, vel);
141  // Convert back to wavespace if necessary
142  if (m_f->m_exp[0]->GetWaveSpace())
143  {
144  for (int i = 0; i < spacedim; ++i)
145  {
146  m_f->m_exp[0]->HomogeneousFwdTrans(vel[i], vel[i]);
147  }
148  }
149  }
150  else
151  {
152  for (int i = 0; i < spacedim; ++i)
153  {
154  vel[i] = Array<OneD, NekDouble>(npoints);
155  Vmath::Vcopy(npoints, m_f->m_exp[i]->GetPhys(), 1, vel[i], 1);
156  }
157  }
158  }
159  else
160  {
161  for (int i = 0; i < spacedim && i < nfields; ++i)
162  {
163  vel[i] = Array<OneD, NekDouble>(npoints);
164  Vmath::Vcopy(npoints, m_f->m_exp[i]->GetPhys(), 1, vel[i], 1);
165  }
166  }
167 
168  // Calculate Gradient
169  for (i = 0; i < nfields; ++i)
170  {
171  for (j = 0; j < spacedim; ++j)
172  {
173  if (i < spacedim)
174  {
175  m_f->m_exp[i]->PhysDeriv(MultiRegions::DirCartesianMap[j],
176  vel[i], tmp[j]);
177  }
178  else
179  {
180  m_f->m_exp[i]->PhysDeriv(MultiRegions::DirCartesianMap[j],
181  m_f->m_exp[i]->GetPhys(), tmp[j]);
182  }
183  }
184  mapping->CovarToCartesian(tmp, tmp);
185  for (int j = 0; j < spacedim; j++)
186  {
187  Vmath::Vcopy(npoints, tmp[j], 1, grad[i * spacedim + j], 1);
188  }
189  }
190 
191  for (i = 0; i < addfields; ++i)
192  {
193  m_f->m_exp[nfields + i] = m_f->AppendExpList(m_f->m_numHomogeneousDir);
194  Vmath::Vcopy(npoints, grad[i], 1, m_f->m_exp[nfields + i]->UpdatePhys(),
195  1);
196  m_f->m_exp[nfields + i]->FwdTransLocalElmt(
197  grad[i], m_f->m_exp[nfields + i]->UpdateCoeffs());
198  }
199 }
200 } // namespace FieldUtils
201 } // namespace Nektar
FieldSharedPtr m_f
Field object.
Definition: Module.h:225
virtual void Process(po::variables_map &vm)
Write mesh to output file.
Definition: ProcessGrad.cpp:64
static GlobalMapping::MappingSharedPtr GetMapping(FieldSharedPtr f)
Abstract base class for processing modules.
Definition: Module.h:260
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:989
std::pair< ModuleType, std::string > ModuleKey
Definition: Module.h:285
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:89
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:1
void Vcopy(int n, const T *x, const int incx, T *y, const int incy)
Definition: Vmath.cpp:1255