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