Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 // License for the specific language governing rights and limitations under
14 // Permission is hereby granted, free of charge, to any person obtaining a
15 // copy of this software and associated documentation files (the "Software"),
16 // to deal in the Software without restriction, including without limitation
17 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 // and/or sell copies of the Software, and to permit persons to whom the
19 // Software is furnished to do so, subject to the following conditions:
20 //
21 // The above copyright notice and this permission notice shall be included
22 // in all copies or substantial portions of the Software.
23 //
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
25 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
30 // DEALINGS IN THE SOFTWARE.
31 //
32 // Description: Compute inner product between two fields.
33 //
34 ////////////////////////////////////////////////////////////////////////////////
35 
36 #include <string>
37 #include <iostream>
38 using namespace std;
39 
40 #include "ProcessInnerProduct.h"
41 
44 
45 namespace Nektar
46 {
47 namespace Utilities
48 {
49 
50 ModuleKey ProcessInnerProduct::className =
52  ModuleKey(eProcessModule, "innerproduct"),
53  ProcessInnerProduct::create,
54  "take inner product between two fields and return value.");
55 
56 ProcessInnerProduct::ProcessInnerProduct(FieldSharedPtr f) : ProcessModule(f)
57 {
58  m_config["fromfld"] = ConfigOption(
59  false, "NotSet", "Fld file form which to interpolate field");
60  m_config["fields"] =
61  ConfigOption(false, "All", "field id's to be used in inner product");
62  m_config["multifldids"] =
63  ConfigOption(false,
64  "NotSet",
65  "Take inner product of multiple field fields with "
66  "ids given in string. i.e. file_0.chk file_1.chk ...");
67  m_config["allfromflds"] =
68  ConfigOption(true,
69  "NotSet",
70  "Take inner product between all fromflds, "
71  "requires multifldids to be set");
72 }
73 
75 {
76 }
77 
78 void ProcessInnerProduct::Process(po::variables_map &vm)
79 {
80  if (m_f->m_verbose)
81  {
82  if(m_f->m_comm->GetRank() == 0)
83  {
84  cout << "ProcessInnerProduct: Evaluating inner product..." << endl;
85  }
86  }
87 
88  ASSERTL0(m_f->m_exp.size() != 0, "input xml file needs to be specified");
89  ASSERTL0(m_f->m_data.size() != 0, "No input data has been defined");
90 
91  string fromfld = m_config["fromfld"].as<string>();
92  FieldSharedPtr fromField = boost::shared_ptr<Field>(new Field());
93 
94  ASSERTL0(m_config["fromfld"].as<string>() != "NotSet",
95  "The config parameter "
96  "fromfld needs to be defined");
97 
98  // Set up ElementGIDs in case of parallel processing
99  Array<OneD, int> ElementGIDs(m_f->m_exp[0]->GetExpSize());
100  for (int i = 0; i < m_f->m_exp[0]->GetExpSize(); ++i)
101  {
102  ElementGIDs[i] = m_f->m_exp[0]->GetExp(i)->GetGeom()->GetGlobalID();
103  }
104 
105  int nfields = m_f->m_fielddef[0]->m_fields.size();
106  int nphys = m_f->m_exp[0]->GetTotPoints();
107  NekDouble totiprod;
108  string fields = m_config["fields"].as<string>();
109  vector<unsigned int> processFields;
110  string multifldidsstr = m_config["multifldids"].as<string>();
111  vector<unsigned int> multiFldIds;
112  vector<string> fromfiles;
113  bool allfromflds = m_config["allfromflds"].m_beenSet;
114 
115  if (fields.compare("All") == 0)
116  {
117  for (int i = 0; i < nfields; ++i)
118  {
119  processFields.push_back(i);
120  }
121  }
122  else
123  {
124  ASSERTL0(ParseUtils::GenerateSeqVector(fields.c_str(), processFields),
125  "Failed to interpret field string in module innerproduct");
126  }
127 
128  if (multifldidsstr.compare("NotSet") == 0)
129  {
130  fromfiles.push_back(fromfld);
131  }
132  else
133  {
134  ASSERTL0(
135  ParseUtils::GenerateSeqVector(multifldidsstr.c_str(), multiFldIds),
136  "Failed to interpret multifldids string in module innerproduct");
137  int end = fromfld.find_first_of('.', 0);
138  string endstr = fromfld.substr(end, fromfld.size());
139  string bodystr = fromfld.substr(0, end);
140  for (int i = 0; i < multiFldIds.size(); ++i)
141  {
142  string infile = bodystr + "_" +
143  boost::lexical_cast<string>(multiFldIds[i]) +
144  endstr;
145  fromfiles.push_back(infile);
146  }
147  }
148 
149  Array<OneD, Array<OneD, NekDouble> > SaveFld(processFields.size());
150  for (int j = 0; j < processFields.size(); ++j)
151  {
152  int fid = processFields[j];
153  SaveFld[j] = Array<OneD, NekDouble>(nphys);
154  m_f->m_exp[fid]->BwdTrans(m_f->m_exp[fid]->GetCoeffs(), SaveFld[j]);
155  }
156 
157  if (allfromflds == false)
158  {
159 
160  for (int f = 0; f < fromfiles.size(); ++f)
161  {
162  m_f->m_fld->Import(fromfiles[f],
163  fromField->m_fielddef,
164  fromField->m_data,
166  ElementGIDs);
167 
168  totiprod = IProduct(processFields, fromField, SaveFld);
169 
170  if (m_f->m_comm->GetRank() == 0)
171  {
172  cout << "Inner Product WRT " << fromfiles[f] << " : "
173  << totiprod << endl;
174  }
175  }
176  }
177  else // evaluate all from fields, first by loading them all up and then
178  // calling IProduct
179  {
180 
181  // Load all from fields.
182  Array<OneD, FieldSharedPtr> allFromField(fromfiles.size());
183  for (int i = 0; i < fromfiles.size(); ++i)
184  {
185  allFromField[i] = boost::shared_ptr<Field>(new Field());
186  m_f->m_fld->Import(fromfiles[i],
187  allFromField[i]->m_fielddef,
188  allFromField[i]->m_data,
190  ElementGIDs);
191  }
192 
193  for (int g = 0; g < fromfiles.size(); ++g)
194  {
195  for (int j = 0; j < processFields.size(); ++j)
196  {
197  int fid = processFields[j];
198 
199  // load new field
200  for (int i = 0; i < allFromField[g]->m_data.size(); ++i)
201  {
202  m_f->m_exp[fid]->ExtractDataToCoeffs(
203  allFromField[g]->m_fielddef[i],
204  allFromField[g]->m_data[i],
205  allFromField[g]->m_fielddef[i]->m_fields[fid],
206  m_f->m_exp[fid]->UpdateCoeffs());
207  }
208 
209  m_f->m_exp[fid]->BwdTrans(m_f->m_exp[fid]->GetCoeffs(),
210  SaveFld[j]);
211  }
212 
213  // take inner product from this g field with all other above
214  for (int f = g; f < fromfiles.size(); ++f)
215  {
216  totiprod = IProduct(processFields, allFromField[f], SaveFld);
217 
218  if (m_f->m_comm->GetRank() == 0)
219  {
220  cout << "Inner Product of " << fromfiles[g] << " WRT "
221  << fromfiles[f] << " : " << totiprod << endl;
222  }
223  }
224  }
225  }
226 }
227 
229  vector<unsigned int> &processFields,
230  FieldSharedPtr &fromField,
231  Array<OneD, const Array<OneD, NekDouble> > &SaveFld)
232 {
233  int nphys = m_f->m_exp[0]->GetTotPoints();
234  NekDouble totiprod = 0.0;
235 
236  for (int j = 0; j < processFields.size(); ++j)
237  {
238  int fid = processFields[j];
239 
240  // load new field
241  for (int i = 0; i < fromField->m_data.size(); ++i)
242  {
243  m_f->m_exp[fid]->ExtractDataToCoeffs(
244  fromField->m_fielddef[i],
245  fromField->m_data[i],
246  fromField->m_fielddef[i]->m_fields[fid],
247  m_f->m_exp[fid]->UpdateCoeffs());
248  }
249 
250  m_f->m_exp[fid]->BwdTrans(m_f->m_exp[fid]->GetCoeffs(),
251  m_f->m_exp[fid]->UpdatePhys());
252 
253  Vmath::Vmul(nphys,
254  SaveFld[j],
255  1,
256  m_f->m_exp[fid]->GetPhys(),
257  1,
258  m_f->m_exp[fid]->UpdatePhys(),
259  1);
260 
261  NekDouble iprod =
262  m_f->m_exp[fid]->PhysIntegral(m_f->m_exp[fid]->UpdatePhys());
263 
264  // put in parallel summation
265  m_f->m_comm->AllReduce(iprod, Nektar::LibUtilities::ReduceSum);
266 
267  totiprod += iprod;
268  }
269  return totiprod;
270 }
271 }
272 }
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:188
pair< ModuleType, string > ModuleKey
virtual void Process()=0
map< string, ConfigOption > m_config
List of configuration values.
STL namespace.
NekDouble IProduct(vector< unsigned int > &processFields, FieldSharedPtr &fromField, Array< OneD, const Array< OneD, NekDouble > > &SaveFld)
static bool GenerateSeqVector(const char *const str, std::vector< unsigned int > &vec)
Definition: ParseUtils.hpp:79
FieldSharedPtr m_f
Field object.
double NekDouble
boost::shared_ptr< Field > FieldSharedPtr
Definition: Field.hpp:695
Represents a command-line configuration option.
static FieldMetaDataMap NullFieldMetaDataMap
Definition: FieldIO.h:54
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:169
ModuleFactory & GetModuleFactory()
Abstract base class for processing modules.
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, tDescription pDesc="")
Register a class with the factory.
Definition: NekFactory.hpp:215