Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator 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 <string>
37 #include <iostream>
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 Utilities
50 {
51 
52 ModuleKey ProcessGrad::className =
54  ModuleKey(eProcessModule, "gradient"),
55  ProcessGrad::create, "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->GetRank() == 0)
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
99 
100  // Get velocity and convert to Cartesian system,
101  // if it is still in transformed system
102  Array<OneD, Array<OneD, NekDouble> > vel (spacedim);
103  if (m_f->m_fieldMetaDataMap.count("MappingCartesianVel"))
104  {
105  if(m_f->m_fieldMetaDataMap["MappingCartesianVel"] == "False")
106  {
107  // Initialize arrays and copy velocity
108  for ( int i =0; i<spacedim; ++i )
109  {
110  vel[i] = Array<OneD, NekDouble> (npoints);
111  if (m_f->m_exp[0]->GetWaveSpace())
112  {
113  m_f->m_exp[0]->HomogeneousBwdTrans(
114  m_f->m_exp[i]->GetPhys(),
115  vel[i]);
116  }
117  else
118  {
119  Vmath::Vcopy(npoints, m_f->m_exp[i]->GetPhys(),1,
120  vel[i],1);
121  }
122 
123  }
124  // Convert velocity to cartesian system
125  mapping->ContravarToCartesian(vel, vel);
126  // Convert back to wavespace if necessary
127  if (m_f->m_exp[0]->GetWaveSpace())
128  {
129  for ( int i =0; i<spacedim; ++i )
130  {
131  m_f->m_exp[0]->HomogeneousFwdTrans(vel[i], vel[i]);
132  }
133  }
134  }
135  else
136  {
137  for ( int i =0; i<spacedim; ++i )
138  {
139  vel[i] = Array<OneD, NekDouble> (npoints);
140  Vmath::Vcopy(npoints, m_f->m_exp[i]->GetPhys(), 1,
141  vel[i], 1);
142  }
143  }
144  }
145  else
146  {
147  for ( int i =0; i<spacedim; ++i )
148  {
149  vel[i] = Array<OneD, NekDouble> (npoints);
150  Vmath::Vcopy(npoints, m_f->m_exp[i]->GetPhys(), 1,
151  vel[i], 1);
152  }
153  }
154 
155  // Calculate Gradient
156  for (i = 0; i < nfields; ++i)
157  {
158  for (j = 0; j < spacedim; ++j)
159  {
160  if (i<spacedim)
161  {
162  m_f->m_exp[i]->PhysDeriv(MultiRegions::DirCartesianMap[j],
163  vel[i],
164  tmp[j]);
165  }
166  else
167  {
168  m_f->m_exp[i]->PhysDeriv(MultiRegions::DirCartesianMap[j],
169  m_f->m_exp[i]->GetPhys(),
170  tmp[j]);
171  }
172  }
173  mapping->CovarToCartesian(tmp, tmp);
174  for( int j = 0; j<spacedim; j++)
175  {
176  Vmath::Vcopy(npoints, tmp[j], 1, grad[i*spacedim+j], 1 );
177  }
178  }
179 
180  for (i = 0; i < addfields; ++i)
181  {
182  m_f->m_exp[nfields + i] = m_f->AppendExpList(m_f->m_fielddef[0]->m_numHomogeneousDir);
183  m_f->m_exp[nfields + i]->UpdatePhys() = grad[i];
184  m_f->m_exp[nfields + i]->FwdTrans_IterPerExp(grad[i],
185  m_f->m_exp[nfields + i]->UpdateCoeffs());
186  }
187 
188  vector<string > outname;
189  for (i = 0; i<nfields; ++i)
190  {
191  if(spacedim == 1)
192  {
193  outname.push_back(m_f->m_fielddef[0]->m_fields[i]+"_x");
194  }
195  else if (spacedim == 2)
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  }
200  else if (spacedim == 3)
201  {
202  outname.push_back(m_f->m_fielddef[0]->m_fields[i]+"_x");
203  outname.push_back(m_f->m_fielddef[0]->m_fields[i]+"_y");
204  outname.push_back(m_f->m_fielddef[0]->m_fields[i]+"_z");
205  }
206  }
207 
208  std::vector<LibUtilities::FieldDefinitionsSharedPtr> FieldDef
209  = m_f->m_exp[0]->GetFieldDefinitions();
210  std::vector<std::vector<NekDouble> > FieldData(FieldDef.size());
211 
212  for (j = 0; j < nfields + addfields; ++j)
213  {
214  for (i = 0; i < FieldDef.size(); ++i)
215  {
216  if (j >= nfields)
217  {
218  FieldDef[i]->m_fields.push_back(outname[j-nfields]);
219  }
220  else
221  {
222  FieldDef[i]->m_fields.push_back(m_f->m_fielddef[0]->m_fields[j]);
223  }
224  m_f->m_exp[j]->AppendFieldData(FieldDef[i], FieldData[i]);
225  }
226  }
227 
228  m_f->m_fielddef = FieldDef;
229  m_f->m_data = FieldData;
230 }
231 
232 }
233 }
pair< ModuleType, string > ModuleKey
virtual void Process()=0
STL namespace.
FieldSharedPtr m_f
Field object.
static GlobalMapping::MappingSharedPtr GetMapping(FieldSharedPtr f)
boost::shared_ptr< Field > FieldSharedPtr
Definition: Field.hpp:695
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
void Vcopy(int n, const T *x, const int incx, T *y, const int incy)
Definition: Vmath.cpp:1047
ModuleFactory & GetModuleFactory()
Abstract base class for processing modules.
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, tDescription pDesc="")
Register a class with the factory.
Definition: NekFactory.hpp:215