Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ProcessAddFld.cpp
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // File: ProcessAddFld.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: Add a field to the intput field
33 //
34 ////////////////////////////////////////////////////////////////////////////////
35 
36 #include <iostream>
37 #include <string>
38 using namespace std;
39 
40 #include "ProcessAddFld.h"
41 
44 
45 namespace Nektar
46 {
47 namespace FieldUtils
48 {
49 
50 ModuleKey ProcessAddFld::className = GetModuleFactory().RegisterCreatorFunction(
51  ModuleKey(eProcessModule, "addfld"),
52  ProcessAddFld::create,
53  "add two fields together with optional scaling. Must specify fromfld and "
54  "scaling is optionally specified with input option scale.");
55 
56 ProcessAddFld::ProcessAddFld(FieldSharedPtr f) : ProcessModule(f)
57 {
58  m_config["scale"] = ConfigOption(false, "1.0", "scale factor");
59 
60  m_config["fromfld"] =
61  ConfigOption(false, "NotSet", "Fld file form which to add field");
62 
63  ASSERTL0(m_config["fromfld"].as<string>().compare("NotSet") != 0,
64  "Need to specify fromfld=file.fld ");
65 }
66 
68 {
69 }
70 
71 void ProcessAddFld::Process(po::variables_map &vm)
72 {
73  if (m_f->m_verbose)
74  {
75  if (m_f->m_comm->TreatAsRankZero())
76  {
77  cout << "ProcessAddFld: Adding new fld to input fld..." << endl;
78  }
79  }
80 
81  ASSERTL0(m_f->m_data.size() != 0, "No input data defined");
82 
83  string scalestr = m_config["scale"].as<string>();
84  NekDouble scale = boost::lexical_cast<NekDouble>(scalestr);
85 
86  string fromfld = m_config["fromfld"].as<string>();
87  FieldSharedPtr fromField = boost::shared_ptr<Field>(new Field());
88 
89  if (m_f->m_exp.size())
90  {
91  // Set up ElementGIDs in case of parallel processing
92  Array<OneD, int> ElementGIDs(m_f->m_exp[0]->GetExpSize());
93  for (int i = 0; i < m_f->m_exp[0]->GetExpSize(); ++i)
94  {
95  ElementGIDs[i] = m_f->m_exp[0]->GetExp(i)->GetGeom()->GetGlobalID();
96  }
97  m_f->FieldIOForFile(fromfld)->Import(
98  fromfld, fromField->m_fielddef, fromField->m_data,
100  }
101  else
102  {
103  m_f->FieldIOForFile(fromfld)->Import(
104  fromfld, fromField->m_fielddef, fromField->m_data,
106  }
107 
108  bool samelength = true;
109  if (fromField->m_data.size() != m_f->m_data.size())
110  {
111  samelength = false;
112  }
113 
114  // scale input field
115  for (int i = 0; i < fromField->m_data.size(); ++i)
116  {
117  int datalen = fromField->m_data[i].size();
118 
119  Vmath::Smul(datalen, scale, &(fromField->m_data[i][0]), 1,
120  &(fromField->m_data[i][0]), 1);
121 
122  if (samelength)
123  {
124  if (datalen != m_f->m_data[i].size())
125  {
126  samelength = false;
127  }
128  }
129  }
130 
131  if (samelength == true)
132  {
133  for (int i = 0; i < m_f->m_data.size(); ++i)
134  {
135  int datalen = m_f->m_data[i].size();
136 
137  Vmath::Vadd(datalen, &(m_f->m_data[i][0]), 1,
138  &(fromField->m_data[i][0]), 1, &(m_f->m_data[i][0]), 1);
139  }
140  }
141  else
142  {
143  ASSERTL0(m_f->m_exp.size() != 0,
144  "Input fields have partitions of different length and so xml "
145  "file needs to be specified");
146 
147  int nfields = m_f->m_fielddef[0]->m_fields.size();
148  int ncoeffs = m_f->m_exp[0]->GetNcoeffs();
149  Array<OneD, NekDouble> SaveFld(ncoeffs);
150 
151  for (int j = 0; j < nfields; ++j)
152  {
153  Vmath::Vcopy(ncoeffs, m_f->m_exp[j]->GetCoeffs(), 1, SaveFld, 1);
154 
155  // since expansion is set up according to m_f search for same
156  // variable in new field
157  int nfield;
158  for (nfield = 0; nfield < fromField->m_fielddef[0]->m_fields.size();
159  ++nfield)
160  {
161  if (fromField->m_fielddef[0]->m_fields[nfield] ==
162  m_f->m_fielddef[0]->m_fields[j])
163  {
164  break;
165  }
166  }
167 
168  ASSERTL0(nfield != fromField->m_fielddef[0]->m_fields.size(),
169  "Could not find field " + m_f->m_fielddef[0]->m_fields[j] +
170  " in from field");
171 
172  // load new field
173  for (int i = 0; i < fromField->m_data.size(); ++i)
174  {
175  m_f->m_exp[j]->ExtractDataToCoeffs(
176  fromField->m_fielddef[i], fromField->m_data[i],
177  fromField->m_fielddef[i]->m_fields[nfield],
178  m_f->m_exp[j]->UpdateCoeffs());
179  }
180 
181  Vmath::Vadd(ncoeffs, m_f->m_exp[j]->GetCoeffs(), 1, SaveFld, 1,
182  m_f->m_exp[j]->UpdateCoeffs(), 1);
183  }
184 
185  std::vector<LibUtilities::FieldDefinitionsSharedPtr> FieldDef =
186  m_f->m_exp[0]->GetFieldDefinitions();
187  std::vector<std::vector<NekDouble> > FieldData(FieldDef.size());
188 
189  for (int i = 0; i < nfields; ++i)
190  {
191  for (int j = 0; j < FieldDef.size(); ++j)
192  {
193  FieldDef[j]->m_fields.push_back(
194  m_f->m_fielddef[0]->m_fields[i]);
195  m_f->m_exp[i]->AppendFieldData(FieldDef[j], FieldData[j]);
196  }
197  }
198 
199  m_f->m_fielddef = FieldDef;
200  m_f->m_data = FieldData;
201  }
202 }
203 }
204 }
map< string, ConfigOption > m_config
List of configuration values.
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:198
Represents a command-line configuration option.
STL namespace.
pair< ModuleType, string > ModuleKey
void Smul(int n, const T alpha, const T *x, const int incx, T *y, const int incy)
Scalar multiply y = alpha*y.
Definition: Vmath.cpp:213
boost::shared_ptr< Field > FieldSharedPtr
Definition: Field.hpp:740
double NekDouble
Abstract base class for processing modules.
virtual void Process(po::variables_map &vm)
Write mesh to output file.
void Vcopy(int n, const T *x, const int incx, T *y, const int incy)
Definition: Vmath.cpp:1061
static FieldMetaDataMap NullFieldMetaDataMap
Definition: FieldIO.h:55
void Vadd(int n, const T *x, const int incx, const T *y, const int incy, T *z, const int incz)
Add vector z = x+y.
Definition: Vmath.cpp:299
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