Nektar++
ProcessAverageFld.cpp
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////////////
2//
3// File: ProcessAverageFld.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: Take averaging of input fields
32//
33////////////////////////////////////////////////////////////////////////////////
34
35#include <iostream>
36#include <string>
37using namespace std;
40#include <boost/format.hpp>
41
42#include "ProcessAverageFld.h"
43
44namespace Nektar::FieldUtils
45{
46
50 "averaging of several field files. Must specify inputfld and range of "
51 "file numbers.");
52
54{
55 m_config["inputfld"] =
56 ConfigOption(false, "NotSet", "Fld file form which to average");
57 m_config["range"] = ConfigOption(false, "NotSet", "range of file numbers");
58}
59
61{
62}
63
64void ProcessAverageFld::v_Process(po::variables_map &vm)
65{
66 m_f->SetUpExp(vm);
67 ASSERTL0(m_config["inputfld"].as<string>().compare("NotSet") != 0,
68 "Need to specify inputfld=file_%.fld ");
69 ASSERTL0(m_config["range"].as<string>().compare("NotSet") != 0,
70 "Need to specify range=ns,dn,ne[include]");
71
72 std::string infilename = m_config["inputfld"].as<string>();
73 std::vector<int> range;
74 ASSERTL0(ParseUtils::GenerateVector(m_config["range"].as<string>(), range),
75 "Failed to interpret range string");
77 range.size() == 3,
78 "range string should contain 3 values nstart, deltan, nend[include].");
79
80 vector<LibUtilities::FieldDefinitionsSharedPtr> FieldDef;
81 vector<vector<double>> FieldData;
82 NekDouble scale = 0.;
83 for (int i = range[0]; i <= range[2]; i += range[1])
84 {
85 boost::format filenameformat(infilename);
86 filenameformat % i;
87 string inputfldname = filenameformat.str();
88 vector<LibUtilities::FieldDefinitionsSharedPtr> tempFieldDef;
89 vector<vector<double>> tempFieldData;
90
91 if (m_f->m_graph)
92 {
93 const SpatialDomains::ExpansionInfoMap &expansions =
94 m_f->m_graph->GetExpansionInfo();
95
96 // if Range has been speficied it is possible to have a
97 // partition which is empty so check this and return if
98 // no elements present.
99
100 if (!expansions.size())
101 {
102 return;
103 }
104
105 Array<OneD, int> ElementGIDs(expansions.size());
106
107 int i = 0;
108 for (auto &expIt : expansions)
109 {
110 ElementGIDs[i++] = expIt.second->m_geomShPtr->GetGlobalID();
111 }
112 m_f->FieldIOForFile(inputfldname)
113 ->Import(inputfldname, tempFieldDef, tempFieldData,
115 }
116 else
117 {
118 m_f->FieldIOForFile(inputfldname)
119 ->Import(inputfldname, tempFieldDef, tempFieldData,
121 }
122
123 if (FieldDef.size() == 0)
124 {
125 FieldDef = tempFieldDef;
126 }
127
128 if (FieldData.size() < tempFieldData.size())
129 {
130 FieldData.resize(tempFieldData.size());
131 }
132
133 for (size_t i = 0; i < tempFieldData.size(); ++i)
134 {
135 if (FieldData[i].size() < tempFieldData[i].size())
136 {
137 FieldData[i].resize(tempFieldData[i].size(), 0.);
138 }
139 Vmath::Vadd(FieldData[i].size(), tempFieldData[i].data(), 1,
140 FieldData[i].data(), 1, FieldData[i].data(), 1);
141 }
142 if (m_f->m_comm->GetSpaceComm()->GetRank() == 0 && vm.count("verbose"))
143 {
144 std::cout << "File " << inputfldname << " processed." << std::endl;
145 }
146 scale += 1.;
147 }
148 if (scale < 0.5)
149 {
150 return; // no input file
151 }
152 scale = 1. / scale;
153
154 // Filling expansion
155 int numHomoDir = m_f->m_numHomogeneousDir;
156 int nfields = FieldDef[0]->m_fields.size();
157 m_f->m_exp.resize(nfields);
158 for (int i = 1; i < nfields; ++i)
159 {
160 m_f->m_exp[i] = m_f->AppendExpList(numHomoDir);
161 }
162 m_f->m_variables = FieldDef[0]->m_fields;
163
164 for (int j = 0; j < nfields; ++j)
165 {
166 // load new field
167 for (int i = 0; i < FieldDef.size(); ++i)
168 {
169 m_f->m_exp[j]->ExtractDataToCoeffs(FieldDef[i], FieldData[i],
170 m_f->m_variables[j],
171 m_f->m_exp[j]->UpdateCoeffs());
172 }
173 Vmath::Smul(m_f->m_exp[j]->GetNcoeffs(), scale,
174 m_f->m_exp[j]->UpdateCoeffs(), 1,
175 m_f->m_exp[j]->UpdateCoeffs(), 1);
176 m_f->m_exp[j]->BwdTrans(m_f->m_exp[j]->GetCoeffs(),
177 m_f->m_exp[j]->UpdatePhys());
178 }
179}
180} // 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
void v_Process(po::variables_map &vm) override
Write mesh to output file.
static std::shared_ptr< Module > create(FieldSharedPtr f)
Creates an instance of this class.
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.
Definition: NekFactory.hpp:197
static bool GenerateVector(const std::string &str, std::vector< T > &out)
Takes a comma-separated string and converts it to entries in a vector.
Definition: ParseUtils.cpp:130
std::shared_ptr< Field > FieldSharedPtr
Definition: Field.hpp:990
std::pair< ModuleType, std::string > ModuleKey
Definition: Module.h:180
ModuleFactory & GetModuleFactory()
Definition: Module.cpp:47
static FieldMetaDataMap NullFieldMetaDataMap
Definition: FieldIO.h:51
std::map< int, ExpansionInfoShPtr > ExpansionInfoMap
Definition: MeshGraph.h:141
double NekDouble
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
Represents a command-line configuration option.
Definition: Module.h:129