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