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