Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ProcessCombineAvg.cpp
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // File: ProcessCombineAvg.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: Combines two fld files containing average fields.
33 //
34 ////////////////////////////////////////////////////////////////////////////////
35 
36 #include <string>
37 #include <iostream>
38 using namespace std;
39 
40 #include "ProcessCombineAvg.h"
41 
44 
45 namespace Nektar
46 {
47 namespace Utilities
48 {
49 
50 ModuleKey ProcessCombineAvg::className =
52  ModuleKey(eProcessModule, "combineAvg"),
53  ProcessCombineAvg::create, "combine two fields containing averages (and possibly Reynolds stresses). Must specify fromfld.");
54 
55 ProcessCombineAvg::ProcessCombineAvg(FieldSharedPtr f) : ProcessModule(f)
56 {
57  m_config["fromfld"] = ConfigOption(false, "NotSet",
58  "Fld file form which to add field");
59 
60  ASSERTL0(m_config["fromfld"].as<string>().compare("NotSet") != 0,
61  "Need to specify fromfld=file.fld ");
62 
63 }
64 
66 {
67 }
68 
69 void ProcessCombineAvg::Process(po::variables_map &vm)
70 {
71  if (m_f->m_verbose)
72  {
73  if(m_f->m_comm->TreatAsRankZero())
74  {
75  cout << "ProcessCombineAvg: Combining new fld into input avg fld..."
76  << endl;
77  }
78  }
79 
80  ASSERTL0(m_f->m_exp.size() != 0,"No input expansion defined");
81 
82  int nfields = m_f->m_fielddef[0]->m_fields.size();
83  int nq = m_f->m_exp[0]->GetTotPoints();
84  int expdim = m_f->m_graph->GetMeshDimension();
85  int spacedim = expdim;
86  if ((m_f->m_fielddef[0]->m_numHomogeneousDir) == 1 ||
87  (m_f->m_fielddef[0]->m_numHomogeneousDir) == 2)
88  {
89  spacedim += m_f->m_fielddef[0]->m_numHomogeneousDir;
90  }
91 
92  // Allocate storage for new field and correction (for Reynolds stress)
93  Array<OneD, Array<OneD, NekDouble> > fromPhys(nfields);
94  Array<OneD, Array<OneD, NekDouble> > correction(nfields);
95  for (int j = 0; j < nfields; ++j)
96  {
97  fromPhys[j] = Array<OneD, NekDouble> (nq, 0.0);
98  correction[j] = Array<OneD, NekDouble> (nq, 0.0);
99  }
100 
101  string fromfld = m_config["fromfld"].as<string>();
102  FieldSharedPtr fromField = boost::shared_ptr<Field>(new Field());
103  LibUtilities::FieldMetaDataMap fromFieldMetaDataMap;
104 
105  // Set up ElementGIDs in case of parallel processing
106  Array<OneD,int> ElementGIDs(m_f->m_exp[0]->GetExpSize());
107  for (int i = 0; i < m_f->m_exp[0]->GetExpSize(); ++i)
108  {
109  ElementGIDs[i] = m_f->m_exp[0]->GetExp(i)->GetGeom()->GetGlobalID();
110  }
111  // Import fromfld file
112  m_f->m_fld->Import(fromfld,
113  fromField->m_fielddef,
114  fromField->m_data,
115  fromFieldMetaDataMap,
116  ElementGIDs);
117  ASSERTL0(fromField->m_fielddef[0]->m_fields.size() == nfields,
118  "Mismatch in number of fields");
119  // Extract data to fromPhys
120  for (int j = 0; j < nfields; ++j)
121  {
122  ASSERTL0(fromField->m_fielddef[0]->m_fields[j] ==
123  m_f->m_fielddef[0]->m_fields[j], "Field names do not match.");
124 
125  // load new field (overwrite m_f->m_exp coeffs for now)
126  for (int i = 0; i < fromField->m_data.size(); ++i)
127  {
128  m_f->m_exp[j]->ExtractDataToCoeffs(
129  fromField->m_fielddef[i],
130  fromField->m_data[i],
131  fromField->m_fielddef[i]->m_fields[j],
132  m_f->m_exp[j]->UpdateCoeffs());
133  }
134  m_f->m_exp[j]->BwdTrans(m_f->m_exp[j]->GetCoeffs(), fromPhys[j]);
135  }
136 
137  // Load number of samples in each file
138  ASSERTL0(m_f->m_fieldMetaDataMap.count("NumberOfFieldDumps") != 0,
139  "Missing NumberOfFieldDumps metadata.");
140  ASSERTL0(fromFieldMetaDataMap.count("NumberOfFieldDumps") != 0,
141  "Missing NumberOfFieldDumps metadata.");
142  string s_num;
143  s_num = m_f->m_fieldMetaDataMap["NumberOfFieldDumps"];
144  int na = atoi(s_num.c_str());
145  s_num = fromFieldMetaDataMap["NumberOfFieldDumps"];
146  int nb = atoi(s_num.c_str());
147 
148  // Look for Reynolds stresses
149  int stress = -1;
150  for (int j = 0; j < nfields; ++j)
151  {
152  if (m_f->m_fielddef[0]->m_fields[j] == "uu")
153  {
154  stress = j;
155  break;
156  }
157  }
158 
159  // Calculate correction for Reynolds stresses
160  if (stress != -1)
161  {
162  Array<OneD, NekDouble> tmp (nq, 0.0);
163  int n = stress;
164  // Follow same numbering as FilterReynoldsStresses
165  for (int i = 0; i < spacedim; ++i)
166  {
167  for (int j = i; j < spacedim; ++j, ++n)
168  {
169  // correction is zero for averages and
170  // = (\bar{x_a}-\bar{x_b})*(\bar{y_a}-\bar{y_b})*na*nb/N
171  // for Reynolds stresses
172  NekDouble fac = ((NekDouble) (na*nb)) / ((NekDouble) (na+nb));
173  Vmath::Vsub(nq, m_f->m_exp[i]->GetPhys(), 1,
174  fromPhys[i], 1, correction[n], 1);
175  Vmath::Vsub(nq, m_f->m_exp[j]->GetPhys(), 1,
176  fromPhys[j], 1, tmp, 1);
177  Vmath::Vmul(nq, correction[n], 1, tmp, 1, correction[n], 1);
178  Vmath::Smul(nq, fac, correction[n], 1, correction[n], 1);
179  }
180  }
181  }
182  // Combine fields
183  for (int j = 0; j < nfields; ++j)
184  {
185  // The new value is: (x_a*na + x_b*nb + correction)/N
186  Vmath::Smul(nq, 1.0*na, m_f->m_exp[j]->GetPhys(), 1,
187  m_f->m_exp[j]->UpdatePhys(), 1);
188  Vmath::Svtvp(nq, 1.0*nb, fromPhys[j], 1,
189  m_f->m_exp[j]->GetPhys(), 1,
190  m_f->m_exp[j]->UpdatePhys(), 1);
191  Vmath::Vadd(nq, m_f->m_exp[j]->GetPhys(), 1,
192  correction[j], 1,
193  m_f->m_exp[j]->UpdatePhys(), 1);
194  Vmath::Smul(nq, 1.0/(na+nb),
195  m_f->m_exp[j]->GetPhys(), 1,
196  m_f->m_exp[j]->UpdatePhys(), 1);
197 
198  m_f->m_exp[j]->FwdTrans_IterPerExp(m_f->m_exp[j]->GetPhys(),
199  m_f->m_exp[j]->UpdateCoeffs());
200  }
201 
202  // Update metadata
203  m_f->m_fieldMetaDataMap["NumberOfFieldDumps"] =
204  boost::lexical_cast<std::string>(na+nb);
205  NekDouble t0 = -1;
206  NekDouble finTime = -1;
207  if(m_f->m_fieldMetaDataMap.count("InitialTime"))
208  {
209  string s_t = m_f->m_fieldMetaDataMap["InitialTime"];
210  NekDouble t = atof(s_t.c_str());
211 
212  t0 = t;
213  }
214  if(fromFieldMetaDataMap.count("InitialTime"))
215  {
216  string s_t = fromFieldMetaDataMap["InitialTime"];
217  NekDouble t = atof(s_t.c_str());
218 
219  if (t0 == -1)
220  {
221  t0 = t;
222  }
223  else
224  {
225  t0 = std::min(t0, t);
226  }
227  }
228  if(m_f->m_fieldMetaDataMap.count("FinalTime"))
229  {
230  string s_t = m_f->m_fieldMetaDataMap["FinalTime"];
231  NekDouble t = atof(s_t.c_str());
232 
233  finTime = std::max(t0, t);
234  }
235  if(fromFieldMetaDataMap.count("FinalTime"))
236  {
237  string s_t = fromFieldMetaDataMap["FinalTime"];
238  NekDouble t = atof(s_t.c_str());
239 
240  finTime = std::max(t0, t);
241  }
242  if (t0 != -1)
243  {
244  m_f->m_fieldMetaDataMap["InitialTime"] =
245  boost::lexical_cast<std::string>(t0);
246  }
247  if (finTime != -1)
248  {
249  m_f->m_fieldMetaDataMap["FinalTime"] =
250  boost::lexical_cast<std::string>(finTime);
251  }
252 
253  // Update field def and data
254  std::vector<LibUtilities::FieldDefinitionsSharedPtr> FieldDef
255  = m_f->m_exp[0]->GetFieldDefinitions();
256  std::vector<std::vector<NekDouble> > FieldData(FieldDef.size());
257  for(int i = 0; i < nfields; ++i)
258  {
259  for (int j = 0; j < FieldDef.size(); ++j)
260  {
261  FieldDef[j]->m_fields.push_back(m_f->m_fielddef[0]->m_fields[i]);
262  m_f->m_exp[i]->AppendFieldData(FieldDef[j], FieldData[j]);
263  }
264  }
265 
266  m_f->m_fielddef = FieldDef;
267  m_f->m_data = FieldData;
268 }
269 
270 }
271 }
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:188
pair< ModuleType, string > ModuleKey
virtual void Process()=0
map< string, ConfigOption > m_config
List of configuration values.
void Svtvp(int n, const T alpha, const T *x, const int incx, const T *y, const int incy, T *z, const int incz)
svtvp (scalar times vector plus vector): z = alpha*x + y
Definition: Vmath.cpp:471
STL namespace.
std::map< std::string, std::string > FieldMetaDataMap
Definition: FieldIO.h:53
FieldSharedPtr m_f
Field object.
void Smul(int n, const T alpha, const T *x, const int incx, T *y, const int incy)
Scalar multiply y = alpha*y.
Definition: Vmath.cpp:199
double NekDouble
boost::shared_ptr< Field > FieldSharedPtr
Definition: Field.hpp:698
void Vsub(int n, const T *x, const int incx, const T *y, const int incy, T *z, const int incz)
Subtract vector z = x-y.
Definition: Vmath.cpp:329
Represents a command-line configuration option.
void Vadd(int n, const T *x, const int incx, const T *y, const int incy, T *z, const int incz)
Add vector z = x+y.
Definition: Vmath.cpp:285
void Vmul(int n, const T *x, const int incx, const T *y, const int incy, T *z, const int incz)
Multiply vector z = x*y.
Definition: Vmath.cpp:169
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