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  cout << "ProcessGrad: Calculating gradients..." << endl;
70  }
71 
72  int i, j;
73  int expdim = m_f->m_graph->GetMeshDimension();
74  int spacedim = m_f->m_fielddef[0]->m_numHomogeneousDir + expdim;
75  int nfields = m_f->m_fielddef[0]->m_fields.size();
76  int addfields = nfields*spacedim;
77 
78  int npoints = m_f->m_exp[0]->GetNpoints();
79  Array<OneD, Array<OneD, NekDouble> > grad(addfields);
80  m_f->m_exp.resize(nfields+addfields);
81 
82  for (i = 0; i < addfields; ++i)
83  {
84  grad[i] = Array<OneD, NekDouble>(npoints);
85  }
86 
87  Array<OneD, Array<OneD, NekDouble> > tmp(spacedim);
88  for( int i = 0; i<spacedim; i++)
89  {
90  tmp[i] = Array<OneD, NekDouble> (npoints);
91  }
92 
93  // Get mapping
96 
97  // Get velocity and convert to Cartesian system,
98  // if it is still in transformed system
99  Array<OneD, Array<OneD, NekDouble> > vel (spacedim);
100  if (m_f->m_fieldMetaDataMap.count("MappingCartesianVel"))
101  {
102  if(m_f->m_fieldMetaDataMap["MappingCartesianVel"] == "False")
103  {
104  // Initialize arrays and copy velocity
105  for ( int i =0; i<spacedim; ++i )
106  {
107  vel[i] = Array<OneD, NekDouble> (npoints);
108  if (m_f->m_exp[0]->GetWaveSpace())
109  {
110  m_f->m_exp[0]->HomogeneousBwdTrans(
111  m_f->m_exp[i]->GetPhys(),
112  vel[i]);
113  }
114  else
115  {
116  Vmath::Vcopy(npoints, m_f->m_exp[i]->GetPhys(),1,
117  vel[i],1);
118  }
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,
138  vel[i], 1);
139  }
140  }
141  }
142  else
143  {
144  for ( int i =0; i<spacedim; ++i )
145  {
146  vel[i] = Array<OneD, NekDouble> (npoints);
147  Vmath::Vcopy(npoints, m_f->m_exp[i]->GetPhys(), 1,
148  vel[i], 1);
149  }
150  }
151 
152  // Calculate Gradient
153  for (i = 0; i < nfields; ++i)
154  {
155  for (j = 0; j < spacedim; ++j)
156  {
157  if (i<spacedim)
158  {
159  m_f->m_exp[i]->PhysDeriv(MultiRegions::DirCartesianMap[j],
160  vel[i],
161  tmp[j]);
162  }
163  else
164  {
165  m_f->m_exp[i]->PhysDeriv(MultiRegions::DirCartesianMap[j],
166  m_f->m_exp[i]->GetPhys(),
167  tmp[j]);
168  }
169  }
170  mapping->CovarToCartesian(tmp, tmp);
171  for( int j = 0; j<spacedim; j++)
172  {
173  Vmath::Vcopy(npoints, tmp[j], 1, grad[i*spacedim+j], 1 );
174  }
175  }
176 
177  for (i = 0; i < addfields; ++i)
178  {
179  m_f->m_exp[nfields + i] = m_f->AppendExpList(m_f->m_fielddef[0]->m_numHomogeneousDir);
180  m_f->m_exp[nfields + i]->UpdatePhys() = grad[i];
181  m_f->m_exp[nfields + i]->FwdTrans_IterPerExp(grad[i],
182  m_f->m_exp[nfields + i]->UpdateCoeffs());
183  }
184 
185  vector<string > outname;
186  for (i = 0; i<nfields; ++i)
187  {
188  if(spacedim == 1)
189  {
190  outname.push_back(m_f->m_fielddef[0]->m_fields[i]+"_x");
191  }
192  else if (spacedim == 2)
193  {
194  outname.push_back(m_f->m_fielddef[0]->m_fields[i]+"_x");
195  outname.push_back(m_f->m_fielddef[0]->m_fields[i]+"_y");
196  }
197  else if (spacedim == 3)
198  {
199  outname.push_back(m_f->m_fielddef[0]->m_fields[i]+"_x");
200  outname.push_back(m_f->m_fielddef[0]->m_fields[i]+"_y");
201  outname.push_back(m_f->m_fielddef[0]->m_fields[i]+"_z");
202  }
203  }
204 
205  std::vector<LibUtilities::FieldDefinitionsSharedPtr> FieldDef
206  = m_f->m_exp[0]->GetFieldDefinitions();
207  std::vector<std::vector<NekDouble> > FieldData(FieldDef.size());
208 
209  for (j = 0; j < nfields + addfields; ++j)
210  {
211  for (i = 0; i < FieldDef.size(); ++i)
212  {
213  if (j >= nfields)
214  {
215  FieldDef[i]->m_fields.push_back(outname[j-nfields]);
216  }
217  else
218  {
219  FieldDef[i]->m_fields.push_back(m_f->m_fielddef[0]->m_fields[j]);
220  }
221  m_f->m_exp[j]->AppendFieldData(FieldDef[i], FieldData[i]);
222  }
223  }
224 
225  m_f->m_fielddef = FieldDef;
226  m_f->m_data = FieldData;
227 }
228 
229 }
230 }
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