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