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