Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 // License for the specific language governing rights and limitations under
14 // Permission is hereby granted, free of charge, to any person obtaining a
15 // copy of this software and associated documentation files (the "Software"),
16 // to deal in the Software without restriction, including without limitation
17 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 // and/or sell copies of the Software, and to permit persons to whom the
19 // Software is furnished to do so, subject to the following conditions:
20 //
21 // The above copyright notice and this permission notice shall be included
22 // in all copies or substantial portions of the Software.
23 //
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
25 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
30 // DEALINGS IN THE SOFTWARE.
31 //
32 // Description: Computes gradient of fields.
33 //
34 ////////////////////////////////////////////////////////////////////////////////
35 
36 #include <iostream>
37 #include <string>
38 using namespace std;
39 
40 #include "ProcessGrad.h"
41 #include "ProcessMapping.h"
42 #include <GlobalMapping/Mapping.h>
43 
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  if (m_f->m_verbose)
68  {
69  if (m_f->m_comm->TreatAsRankZero())
70  {
71  cout << "ProcessGrad: Calculating gradients..." << endl;
72  }
73  }
74 
75  int i, j;
76  int expdim = m_f->m_graph->GetMeshDimension();
77  int spacedim = m_f->m_fielddef[0]->m_numHomogeneousDir + expdim;
78  int nfields = m_f->m_fielddef[0]->m_fields.size();
79  int addfields = nfields * spacedim;
80 
81  int npoints = m_f->m_exp[0]->GetNpoints();
82  Array<OneD, Array<OneD, NekDouble> > grad(addfields);
83  m_f->m_exp.resize(nfields + addfields);
84 
85  for (i = 0; i < addfields; ++i)
86  {
87  grad[i] = Array<OneD, NekDouble>(npoints);
88  }
89 
90  Array<OneD, Array<OneD, NekDouble> > tmp(spacedim);
91  for (int i = 0; i < spacedim; i++)
92  {
93  tmp[i] = Array<OneD, NekDouble>(npoints);
94  }
95 
96  // Get mapping
98 
99  // Get velocity and convert to Cartesian system,
100  // if it is still in transformed system
101  Array<OneD, Array<OneD, NekDouble> > vel(spacedim);
102  if (m_f->m_fieldMetaDataMap.count("MappingCartesianVel"))
103  {
104  if (m_f->m_fieldMetaDataMap["MappingCartesianVel"] == "False")
105  {
106  // Initialize arrays and copy velocity
107  for (int i = 0; i < spacedim; ++i)
108  {
109  vel[i] = Array<OneD, NekDouble>(npoints);
110  if (m_f->m_exp[0]->GetWaveSpace())
111  {
112  m_f->m_exp[0]->HomogeneousBwdTrans(m_f->m_exp[i]->GetPhys(),
113  vel[i]);
114  }
115  else
116  {
117  Vmath::Vcopy(npoints, m_f->m_exp[i]->GetPhys(), 1, vel[i],
118  1);
119  }
120  }
121  // Convert velocity to cartesian system
122  mapping->ContravarToCartesian(vel, vel);
123  // Convert back to wavespace if necessary
124  if (m_f->m_exp[0]->GetWaveSpace())
125  {
126  for (int i = 0; i < spacedim; ++i)
127  {
128  m_f->m_exp[0]->HomogeneousFwdTrans(vel[i], vel[i]);
129  }
130  }
131  }
132  else
133  {
134  for (int i = 0; i < spacedim; ++i)
135  {
136  vel[i] = Array<OneD, NekDouble>(npoints);
137  Vmath::Vcopy(npoints, m_f->m_exp[i]->GetPhys(), 1, vel[i], 1);
138  }
139  }
140  }
141  else
142  {
143  for (int i = 0; i < spacedim; ++i)
144  {
145  vel[i] = Array<OneD, NekDouble>(npoints);
146  Vmath::Vcopy(npoints, m_f->m_exp[i]->GetPhys(), 1, vel[i], 1);
147  }
148  }
149 
150  // Calculate Gradient
151  for (i = 0; i < nfields; ++i)
152  {
153  for (j = 0; j < spacedim; ++j)
154  {
155  if (i < spacedim)
156  {
157  m_f->m_exp[i]->PhysDeriv(MultiRegions::DirCartesianMap[j],
158  vel[i], tmp[j]);
159  }
160  else
161  {
162  m_f->m_exp[i]->PhysDeriv(MultiRegions::DirCartesianMap[j],
163  m_f->m_exp[i]->GetPhys(), tmp[j]);
164  }
165  }
166  mapping->CovarToCartesian(tmp, tmp);
167  for (int j = 0; j < spacedim; j++)
168  {
169  Vmath::Vcopy(npoints, tmp[j], 1, grad[i * spacedim + j], 1);
170  }
171  }
172 
173  for (i = 0; i < addfields; ++i)
174  {
175  m_f->m_exp[nfields + i] =
176  m_f->AppendExpList(m_f->m_fielddef[0]->m_numHomogeneousDir);
177  Vmath::Vcopy(npoints, grad[i], 1, m_f->m_exp[nfields + i]->UpdatePhys(),
178  1);
179  m_f->m_exp[nfields + i]->FwdTrans_IterPerExp(
180  grad[i], m_f->m_exp[nfields + i]->UpdateCoeffs());
181  }
182 
183  vector<string> outname;
184  for (i = 0; i < nfields; ++i)
185  {
186  if (spacedim == 1)
187  {
188  outname.push_back(m_f->m_fielddef[0]->m_fields[i] + "_x");
189  }
190  else if (spacedim == 2)
191  {
192  outname.push_back(m_f->m_fielddef[0]->m_fields[i] + "_x");
193  outname.push_back(m_f->m_fielddef[0]->m_fields[i] + "_y");
194  }
195  else if (spacedim == 3)
196  {
197  outname.push_back(m_f->m_fielddef[0]->m_fields[i] + "_x");
198  outname.push_back(m_f->m_fielddef[0]->m_fields[i] + "_y");
199  outname.push_back(m_f->m_fielddef[0]->m_fields[i] + "_z");
200  }
201  }
202 
203  std::vector<LibUtilities::FieldDefinitionsSharedPtr> FieldDef =
204  m_f->m_exp[0]->GetFieldDefinitions();
205  std::vector<std::vector<NekDouble> > FieldData(FieldDef.size());
206 
207  for (j = 0; j < nfields + addfields; ++j)
208  {
209  for (i = 0; i < FieldDef.size(); ++i)
210  {
211  if (j >= nfields)
212  {
213  FieldDef[i]->m_fields.push_back(outname[j - nfields]);
214  }
215  else
216  {
217  FieldDef[i]->m_fields.push_back(
218  m_f->m_fielddef[0]->m_fields[j]);
219  }
220  m_f->m_exp[j]->AppendFieldData(FieldDef[i], FieldData[i]);
221  }
222  }
223 
224  m_f->m_fielddef = FieldDef;
225  m_f->m_data = FieldData;
226 }
227 }
228 }
STL namespace.
pair< ModuleType, string > ModuleKey
static GlobalMapping::MappingSharedPtr GetMapping(FieldSharedPtr f)
boost::shared_ptr< Field > FieldSharedPtr
Definition: Field.hpp:740
virtual void Process(po::variables_map &vm)
Write mesh to output file.
Definition: ProcessGrad.cpp:65
GLOBAL_MAPPING_EXPORT typedef boost::shared_ptr< Mapping > MappingSharedPtr
A shared pointer to a Mapping object.
Definition: Mapping.h:51
MultiRegions::Direction const DirCartesianMap[]
Definition: ExpList.h:86
Abstract base class for processing modules.
void Vcopy(int n, const T *x, const int incx, T *y, const int incy)
Definition: Vmath.cpp:1061
ModuleFactory & GetModuleFactory()
FieldSharedPtr m_f
Field object.
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, tDescription pDesc="")
Register a class with the factory.
Definition: NekFactory.hpp:215