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  cout << "ProcessInnerProduct: Evaluating inner product" << endl;
83  }
84 
85  ASSERTL0(m_f->m_exp.size() != 0, "input xml file needs to be specified");
86  ASSERTL0(m_f->m_data.size() != 0, "No input data has been defined");
87 
88  string fromfld = m_config["fromfld"].as<string>();
89  FieldSharedPtr fromField = boost::shared_ptr<Field>(new Field());
90 
91  ASSERTL0(m_config["fromfld"].as<string>() != "NotSet",
92  "The config parameter "
93  "fromfld needs to be defined");
94 
95  // Set up ElementGIDs in case of parallel processing
96  Array<OneD, int> ElementGIDs(m_f->m_exp[0]->GetExpSize());
97  for (int i = 0; i < m_f->m_exp[0]->GetExpSize(); ++i)
98  {
99  ElementGIDs[i] = m_f->m_exp[0]->GetExp(i)->GetGeom()->GetGlobalID();
100  }
101 
102  int nfields = m_f->m_fielddef[0]->m_fields.size();
103  int nphys = m_f->m_exp[0]->GetTotPoints();
104  NekDouble totiprod;
105  string fields = m_config["fields"].as<string>();
106  vector<unsigned int> processFields;
107  string multifldidsstr = m_config["multifldids"].as<string>();
108  vector<unsigned int> multiFldIds;
109  vector<string> fromfiles;
110  bool allfromflds = m_config["allfromflds"].m_beenSet;
111 
112  if (fields.compare("All") == 0)
113  {
114  for (int i = 0; i < nfields; ++i)
115  {
116  processFields.push_back(i);
117  }
118  }
119  else
120  {
121  ASSERTL0(ParseUtils::GenerateSeqVector(fields.c_str(), processFields),
122  "Failed to interpret field string in module innerproduct");
123  }
124 
125  if (multifldidsstr.compare("NotSet") == 0)
126  {
127  fromfiles.push_back(fromfld);
128  }
129  else
130  {
131  ASSERTL0(
132  ParseUtils::GenerateSeqVector(multifldidsstr.c_str(), multiFldIds),
133  "Failed to interpret multifldids string in module innerproduct");
134  int end = fromfld.find_first_of('.', 0);
135  string endstr = fromfld.substr(end, fromfld.size());
136  string bodystr = fromfld.substr(0, end);
137  for (int i = 0; i < multiFldIds.size(); ++i)
138  {
139  string infile = bodystr + "_" +
140  boost::lexical_cast<string>(multiFldIds[i]) +
141  endstr;
142  fromfiles.push_back(infile);
143  }
144  }
145 
146  Array<OneD, Array<OneD, NekDouble> > SaveFld(processFields.size());
147  for (int j = 0; j < processFields.size(); ++j)
148  {
149  int fid = processFields[j];
150  SaveFld[j] = Array<OneD, NekDouble>(nphys);
151  m_f->m_exp[fid]->BwdTrans(m_f->m_exp[fid]->GetCoeffs(), SaveFld[j]);
152  }
153 
154  if (allfromflds == false)
155  {
156 
157  for (int f = 0; f < fromfiles.size(); ++f)
158  {
159  m_f->m_fld->Import(fromfiles[f],
160  fromField->m_fielddef,
161  fromField->m_data,
163  ElementGIDs);
164 
165  totiprod = IProduct(processFields, fromField, SaveFld);
166 
167  if (m_f->m_comm->GetRank() == 0)
168  {
169  cout << "Inner Product WRT " << fromfiles[f] << " : "
170  << totiprod << endl;
171  }
172  }
173  }
174  else // evaluate all from fields, first by loading them all up and then
175  // calling IProduct
176  {
177 
178  // Load all from fields.
179  Array<OneD, FieldSharedPtr> allFromField(fromfiles.size());
180  for (int i = 0; i < fromfiles.size(); ++i)
181  {
182  allFromField[i] = boost::shared_ptr<Field>(new Field());
183  m_f->m_fld->Import(fromfiles[i],
184  allFromField[i]->m_fielddef,
185  allFromField[i]->m_data,
187  ElementGIDs);
188  }
189 
190  for (int g = 0; g < fromfiles.size(); ++g)
191  {
192  for (int j = 0; j < processFields.size(); ++j)
193  {
194  int fid = processFields[j];
195 
196  // load new field
197  for (int i = 0; i < allFromField[g]->m_data.size(); ++i)
198  {
199  m_f->m_exp[fid]->ExtractDataToCoeffs(
200  allFromField[g]->m_fielddef[i],
201  allFromField[g]->m_data[i],
202  allFromField[g]->m_fielddef[i]->m_fields[fid],
203  m_f->m_exp[fid]->UpdateCoeffs());
204  }
205 
206  m_f->m_exp[fid]->BwdTrans(m_f->m_exp[fid]->GetCoeffs(),
207  SaveFld[j]);
208  }
209 
210  // take inner product from this g field with all other above
211  for (int f = g; f < fromfiles.size(); ++f)
212  {
213  totiprod = IProduct(processFields, allFromField[f], SaveFld);
214 
215  if (m_f->m_comm->GetRank() == 0)
216  {
217  cout << "Inner Product of " << fromfiles[g] << " WRT "
218  << fromfiles[f] << " : " << totiprod << endl;
219  }
220  }
221  }
222  }
223 }
224 
226  vector<unsigned int> &processFields,
227  FieldSharedPtr &fromField,
228  Array<OneD, const Array<OneD, NekDouble> > &SaveFld)
229 {
230  int nphys = m_f->m_exp[0]->GetTotPoints();
231  NekDouble totiprod = 0.0;
232 
233  for (int j = 0; j < processFields.size(); ++j)
234  {
235  int fid = processFields[j];
236 
237  // load new field
238  for (int i = 0; i < fromField->m_data.size(); ++i)
239  {
240  m_f->m_exp[fid]->ExtractDataToCoeffs(
241  fromField->m_fielddef[i],
242  fromField->m_data[i],
243  fromField->m_fielddef[i]->m_fields[fid],
244  m_f->m_exp[fid]->UpdateCoeffs());
245  }
246 
247  m_f->m_exp[fid]->BwdTrans(m_f->m_exp[fid]->GetCoeffs(),
248  m_f->m_exp[fid]->UpdatePhys());
249 
250  Vmath::Vmul(nphys,
251  SaveFld[j],
252  1,
253  m_f->m_exp[fid]->GetPhys(),
254  1,
255  m_f->m_exp[fid]->UpdatePhys(),
256  1);
257 
258  NekDouble iprod =
259  m_f->m_exp[fid]->PhysIntegral(m_f->m_exp[fid]->UpdatePhys());
260 
261  // put in parallel summation
262  m_f->m_comm->AllReduce(iprod, Nektar::LibUtilities::ReduceSum);
263 
264  totiprod += iprod;
265  }
266  return totiprod;
267 }
268 }
269 }
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:161
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