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