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>
37 using namespace std;
38 
39 #include <boost/core/ignore_unused.hpp>
40 
43 
44 #include "ProcessInnerProduct.h"
45 
46 namespace Nektar
47 {
48 namespace FieldUtils
49 {
50 
51 ModuleKey ProcessInnerProduct::className =
53  ModuleKey(eProcessModule, "innerproduct"),
54  ProcessInnerProduct::create,
55  "take inner product between two fields and return value.");
56 
57 ProcessInnerProduct::ProcessInnerProduct(FieldSharedPtr f) : ProcessModule(f)
58 {
59  m_config["fromfld"] = ConfigOption(
60  false, "NotSet", "Fld file form which to interpolate field");
61  m_config["fields"] =
62  ConfigOption(false, "All", "field id's to be used in inner product");
63  m_config["multifldids"] = ConfigOption(
64  false, "NotSet", "Take inner product of multiple field fields with "
65  "ids given in string. i.e. file_0.chk file_1.chk ...");
66  m_config["allfromflds"] =
67  ConfigOption(true, "0", "Take inner product between all fromflds, "
68  "requires multifldids to be set");
69 }
70 
72 {
73 }
74 
75 void ProcessInnerProduct::Process(po::variables_map &vm)
76 {
77  boost::ignore_unused(vm);
78 
79  // Skip in case of empty partition
80  if (m_f->m_exp[0]->GetNumElmts() == 0)
81  {
82  return;
83  }
84 
85  string fromfld = m_config["fromfld"].as<string>();
86  FieldSharedPtr fromField = std::shared_ptr<Field>(new Field());
87 
88  ASSERTL0(m_config["fromfld"].as<string>() != "NotSet",
89  "The config parameter "
90  "fromfld needs to be defined");
91 
92  // Set up ElementGIDs in case of parallel processing
93  Array<OneD, int> ElementGIDs(m_f->m_exp[0]->GetExpSize());
94  for (int i = 0; i < m_f->m_exp[0]->GetExpSize(); ++i)
95  {
96  ElementGIDs[i] = m_f->m_exp[0]->GetExp(i)->GetGeom()->GetGlobalID();
97  }
98 
99  int nfields = m_f->m_variables.size();
100  int nphys = m_f->m_exp[0]->GetTotPoints();
101  NekDouble totiprod;
102  string fields = m_config["fields"].as<string>();
103  vector<unsigned int> processFields;
104  string multifldidsstr = m_config["multifldids"].as<string>();
105  vector<unsigned int> multiFldIds;
106  vector<string> fromfiles;
107  bool allfromflds = m_config["allfromflds"].as<bool>();
108 
109  if (fields.compare("All") == 0)
110  {
111  for (int i = 0; i < nfields; ++i)
112  {
113  processFields.push_back(i);
114  }
115  }
116  else
117  {
118  ASSERTL0(ParseUtils::GenerateVector(fields, processFields),
119  "Failed to interpret field string in module innerproduct");
120  }
121 
122  if (multifldidsstr.compare("NotSet") == 0)
123  {
124  fromfiles.push_back(fromfld);
125  }
126  else
127  {
128  ASSERTL0(
129  ParseUtils::GenerateSeqVector(multifldidsstr, multiFldIds),
130  "Failed to interpret multifldids string in module innerproduct");
131  int end = fromfld.find_first_of('.', 0);
132  string endstr = fromfld.substr(end, fromfld.size());
133  string bodystr = fromfld.substr(0, end);
134  for (int i = 0; i < multiFldIds.size(); ++i)
135  {
136  string infile = bodystr + "_" +
137  boost::lexical_cast<string>(multiFldIds[i]) +
138  endstr;
139  fromfiles.push_back(infile);
140  }
141  }
142 
143  Array<OneD, Array<OneD, NekDouble> > SaveFld(processFields.size());
144  for (int j = 0; j < processFields.size(); ++j)
145  {
146  int fid = processFields[j];
147  SaveFld[j] = Array<OneD, NekDouble>(nphys);
148  m_f->m_exp[fid]->BwdTrans(m_f->m_exp[fid]->GetCoeffs(), SaveFld[j]);
149  }
150 
151  if (allfromflds == false)
152  {
153 
154  for (int f = 0; f < fromfiles.size(); ++f)
155  {
156  m_f->FieldIOForFile(fromfiles[f])->Import(
157  fromfiles[f], fromField->m_fielddef, fromField->m_data,
159 
160  totiprod = IProduct(processFields, fromField, SaveFld);
161 
162  if (m_f->m_comm->GetRank() == 0)
163  {
164  cout << "Inner Product WRT " << fromfiles[f] << " : "
165  << totiprod << endl;
166  }
167  }
168  }
169  else // evaluate all from fields, first by loading them all up and then
170  // calling IProduct
171  {
172 
173  // Load all from fields.
174  Array<OneD, FieldSharedPtr> allFromField(fromfiles.size());
175  for (int i = 0; i < fromfiles.size(); ++i)
176  {
177  allFromField[i] = std::shared_ptr<Field>(new Field());
178 
179  m_f->FieldIOForFile(fromfiles[i])->Import(
180  fromfiles[i], allFromField[i]->m_fielddef,
181  allFromField[i]->m_data, LibUtilities::NullFieldMetaDataMap,
182  ElementGIDs);
183  }
184 
185  for (int g = 0; g < fromfiles.size(); ++g)
186  {
187  for (int j = 0; j < processFields.size(); ++j)
188  {
189  int fid = processFields[j];
190 
191  // load new field
192  for (int i = 0; i < allFromField[g]->m_data.size(); ++i)
193  {
194  m_f->m_exp[fid]->ExtractDataToCoeffs(
195  allFromField[g]->m_fielddef[i],
196  allFromField[g]->m_data[i],
197  m_f->m_variables[fid],
198  m_f->m_exp[fid]->UpdateCoeffs());
199  }
200 
201  m_f->m_exp[fid]->BwdTrans(m_f->m_exp[fid]->GetCoeffs(),
202  SaveFld[j]);
203  }
204 
205  // take inner product from this g field with all other above
206  for (int f = g; f < fromfiles.size(); ++f)
207  {
208  totiprod = IProduct(processFields, allFromField[f], SaveFld);
209 
210  if (m_f->m_comm->GetRank() == 0)
211  {
212  cout << "Inner Product of " << fromfiles[g] << " WRT "
213  << fromfiles[f] << " : " << totiprod << endl;
214  }
215  }
216  }
217  }
218 }
219 
221  vector<unsigned int> &processFields,
222  FieldSharedPtr &fromField,
223  Array<OneD, const Array<OneD, NekDouble> > &SaveFld)
224 {
225  int nphys = m_f->m_exp[0]->GetTotPoints();
226  NekDouble totiprod = 0.0;
227 
228  for (int j = 0; j < processFields.size(); ++j)
229  {
230  int fid = processFields[j];
231 
232  // load new field
233  for (int i = 0; i < fromField->m_data.size(); ++i)
234  {
235  m_f->m_exp[fid]->ExtractDataToCoeffs(
236  fromField->m_fielddef[i], fromField->m_data[i],
237  m_f->m_variables[fid],
238  m_f->m_exp[fid]->UpdateCoeffs());
239  }
240 
241  m_f->m_exp[fid]->BwdTrans(m_f->m_exp[fid]->GetCoeffs(),
242  m_f->m_exp[fid]->UpdatePhys());
243 
244  Vmath::Vmul(nphys, SaveFld[j], 1, m_f->m_exp[fid]->GetPhys(), 1,
245  m_f->m_exp[fid]->UpdatePhys(), 1);
246 
247  NekDouble iprod =
248  m_f->m_exp[fid]->PhysIntegral(m_f->m_exp[fid]->UpdatePhys());
249 
250  // put in parallel summation
251  m_f->m_comm->AllReduce(iprod, Nektar::LibUtilities::ReduceSum);
252 
253  totiprod += iprod;
254  }
255  return totiprod;
256 }
257 }
258 }
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:216
std::map< std::string, ConfigOption > m_config
List of configuration values.
virtual void Process(po::variables_map &vm)
Write mesh to output file.
Represents a command-line configuration option.
STL namespace.
std::shared_ptr< Field > FieldSharedPtr
Definition: Field.hpp:762
std::pair< ModuleType, std::string > ModuleKey
double NekDouble
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:135
NekDouble IProduct(vector< unsigned int > &processFields, FieldSharedPtr &fromField, Array< OneD, const Array< OneD, NekDouble > > &SaveFld)
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, std::string pDesc="")
Register a class with the factory.
Definition: NekFactory.hpp:199
Abstract base class for processing modules.
static FieldMetaDataMap NullFieldMetaDataMap
Definition: FieldIO.h:53
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:186
ModuleFactory & GetModuleFactory()
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:108
FieldSharedPtr m_f
Field object.