Nektar++
ProcessInnerProduct.cpp
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////////////
2//
3// File: ProcessInnerProduct.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: Compute inner product between two fields.
32//
33////////////////////////////////////////////////////////////////////////////////
34
35#include <iostream>
36#include <string>
37using namespace std;
38
41
42#include "ProcessInnerProduct.h"
43
44namespace Nektar::FieldUtils
45{
46
50 "take inner product between two fields and return value.");
51
53{
54 m_config["fromfld"] = ConfigOption(
55 false, "NotSet", "Fld file form which to interpolate field");
56 m_config["fields"] =
57 ConfigOption(false, "All", "field id's to be used in inner product");
58 m_config["multifldids"] =
59 ConfigOption(false, "NotSet",
60 "Take inner product of multiple field fields with "
61 "ids given in string. i.e. file_0.chk file_1.chk ...");
62 m_config["allfromflds"] =
63 ConfigOption(true, "0",
64 "Take inner product between all fromflds, "
65 "requires multifldids to be set");
66}
67
69{
70}
71
72void ProcessInnerProduct::v_Process(po::variables_map &vm)
73{
74 m_f->SetUpExp(vm);
75
76 // Skip in case of empty partition
77 if (m_f->m_exp[0]->GetNumElmts() == 0)
78 {
79 return;
80 }
81
82 string fromfld = m_config["fromfld"].as<string>();
83 FieldSharedPtr fromField = std::shared_ptr<Field>(new Field());
84
85 ASSERTL0(m_config["fromfld"].as<string>() != "NotSet",
86 "The config parameter "
87 "fromfld needs to be defined");
88
89 // Set up ElementGIDs in case of parallel processing
90 Array<OneD, int> ElementGIDs(m_f->m_exp[0]->GetExpSize());
91 for (int i = 0; i < m_f->m_exp[0]->GetExpSize(); ++i)
92 {
93 ElementGIDs[i] = m_f->m_exp[0]->GetExp(i)->GetGeom()->GetGlobalID();
94 }
95
96 int nfields = m_f->m_variables.size();
97 int nphys = m_f->m_exp[0]->GetTotPoints();
98 NekDouble totiprod;
99 string fields = m_config["fields"].as<string>();
100 vector<unsigned int> processFields;
101 string multifldidsstr = m_config["multifldids"].as<string>();
102 vector<unsigned int> multiFldIds;
103 vector<string> fromfiles;
104 bool allfromflds = m_config["allfromflds"].as<bool>();
105
106 if (fields.compare("All") == 0)
107 {
108 for (int i = 0; i < nfields; ++i)
109 {
110 processFields.push_back(i);
111 }
112 }
113 else
114 {
115 ASSERTL0(ParseUtils::GenerateVector(fields, processFields),
116 "Failed to interpret field string in module innerproduct");
117 }
118
119 if (multifldidsstr.compare("NotSet") == 0)
120 {
121 fromfiles.push_back(fromfld);
122 }
123 else
124 {
125 ASSERTL0(
126 ParseUtils::GenerateSeqVector(multifldidsstr, multiFldIds),
127 "Failed to interpret multifldids string in module innerproduct");
128 int end = fromfld.find_first_of('.', 0);
129 string endstr = fromfld.substr(end, fromfld.size());
130 string bodystr = fromfld.substr(0, end);
131 for (int i = 0; i < multiFldIds.size(); ++i)
132 {
133 string infile = bodystr + "_" +
134 boost::lexical_cast<string>(multiFldIds[i]) +
135 endstr;
136 fromfiles.push_back(infile);
137 }
138 }
139
140 Array<OneD, Array<OneD, NekDouble>> SaveFld(processFields.size());
141 for (int j = 0; j < processFields.size(); ++j)
142 {
143 int fid = processFields[j];
144 SaveFld[j] = Array<OneD, NekDouble>(nphys);
145 m_f->m_exp[fid]->BwdTrans(m_f->m_exp[fid]->GetCoeffs(), SaveFld[j]);
146 }
147
148 if (allfromflds == false)
149 {
150
151 for (int f = 0; f < fromfiles.size(); ++f)
152 {
153 m_f->FieldIOForFile(fromfiles[f])
154 ->Import(fromfiles[f], fromField->m_fielddef, fromField->m_data,
156
157 totiprod = IProduct(processFields, fromField, SaveFld);
158
159 if (m_f->m_comm->GetSpaceComm()->GetRank() == 0)
160 {
161 cout << "Inner Product WRT " << fromfiles[f] << " : "
162 << totiprod << endl;
163 }
164 }
165 }
166 else // evaluate all from fields, first by loading them all up and then
167 // calling IProduct
168 {
169
170 // Load all from fields.
171 Array<OneD, FieldSharedPtr> allFromField(fromfiles.size());
172 for (int i = 0; i < fromfiles.size(); ++i)
173 {
174 allFromField[i] = std::shared_ptr<Field>(new Field());
175
176 m_f->FieldIOForFile(fromfiles[i])
177 ->Import(fromfiles[i], allFromField[i]->m_fielddef,
178 allFromField[i]->m_data,
180 }
181
182 for (int g = 0; g < fromfiles.size(); ++g)
183 {
184 for (int j = 0; j < processFields.size(); ++j)
185 {
186 int fid = processFields[j];
187 Array<OneD, NekDouble> coeffs(m_f->m_exp[fid]->GetNcoeffs());
188 // load new field
189 for (int i = 0; i < allFromField[g]->m_data.size(); ++i)
190 {
191 m_f->m_exp[fid]->ExtractDataToCoeffs(
192 allFromField[g]->m_fielddef[i],
193 allFromField[g]->m_data[i], m_f->m_variables[fid],
194 coeffs);
195 }
196
197 m_f->m_exp[fid]->BwdTrans(coeffs, SaveFld[j]);
198 }
199
200 // take inner product from this g field with all other above
201 for (int f = g; f < fromfiles.size(); ++f)
202 {
203 totiprod = IProduct(processFields, allFromField[f], SaveFld);
204
205 if (m_f->m_comm->GetSpaceComm()->GetRank() == 0)
206 {
207 cout << "Inner Product of " << fromfiles[g] << " WRT "
208 << fromfiles[f] << " : " << totiprod << endl;
209 }
210 }
211 }
212 }
213}
214
216 vector<unsigned int> &processFields, FieldSharedPtr &fromField,
217 Array<OneD, const Array<OneD, NekDouble>> &SaveFld)
218{
219 int nphys = m_f->m_exp[0]->GetTotPoints();
220 NekDouble totiprod = 0.0;
221
222 for (int j = 0; j < processFields.size(); ++j)
223 {
224 int fid = processFields[j];
225
226 Array<OneD, NekDouble> coeffs(m_f->m_exp[fid]->GetNcoeffs());
227 Array<OneD, NekDouble> phys(m_f->m_exp[fid]->GetTotPoints());
228
229 // load new field
230 for (int i = 0; i < fromField->m_data.size(); ++i)
231 {
232 m_f->m_exp[fid]->ExtractDataToCoeffs(fromField->m_fielddef[i],
233 fromField->m_data[i],
234 m_f->m_variables[fid], coeffs);
235 }
236
237 m_f->m_exp[fid]->BwdTrans(coeffs, phys);
238
239 Vmath::Vmul(nphys, SaveFld[j], 1, phys, 1, phys, 1);
240
241 NekDouble iprod = m_f->m_exp[fid]->Integral(phys);
242
243 totiprod += iprod;
244 }
245 return totiprod;
246}
247} // 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.
NekDouble IProduct(std::vector< unsigned int > &processFields, FieldSharedPtr &fromField, Array< OneD, const Array< OneD, NekDouble > > &SaveFld)
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.
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
static bool GenerateSeqVector(const std::string &str, std::vector< unsigned int > &out)
Takes a comma-separated compressed string and converts it to entries in a vector.
Definition: ParseUtils.cpp:104
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
static FieldMetaDataMap NullFieldMetaDataMap
Definition: FieldIO.h:51
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
STL namespace.
Represents a command-line configuration option.
Definition: Module.h:129