Nektar++
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 <string>
37 #include <iostream>
38 using namespace std;
39 
40 #include "ProcessAddFld.h"
41 
44 
45 namespace Nektar
46 {
47 namespace Utilities
48 {
49 
50 ModuleKey ProcessAddFld::className =
52  ModuleKey(eProcessModule, "addfld"),
53  ProcessAddFld::create, "rescale input field by a constant factor.");
54 
55 ProcessAddFld::ProcessAddFld(FieldSharedPtr f) : ProcessModule(f)
56 {
57  m_config["scale"] = ConfigOption(false, "1.0", "scale factor");
58 
59  m_config["fromfld"] = ConfigOption(false, "NotSet",
60  "Fld file form which to interpolate field");
61 
62  ASSERTL0(m_config["fromfld"].as<string>().compare("NotSet") != 0,
63  "Need to specify fromfld=file.fld ");
64 
65 }
66 
68 {
69 }
70 
71 void ProcessAddFld::Process(po::variables_map &vm)
72 {
73  if (m_f->m_verbose)
74  {
75  cout << "ProcessAddFld: Adding new fld to input fld" << endl;
76  }
77 
78  ASSERTL0(m_f->m_data.size() != 0,"No input data defined");
79 
80  string scalestr = m_config["scale"].as<string>();
81  NekDouble scale = boost::lexical_cast<NekDouble>(scalestr);
82 
83  string fromfld = m_config["fromfld"].as<string>();
84  FieldSharedPtr fromField = boost::shared_ptr<Field>(new Field());
85 
86  if(m_f->m_exp.size())
87  {
88  // Set up ElementGIDs in case of parallel processing
89  Array<OneD,int> ElementGIDs(m_f->m_exp[0]->GetExpSize());
90  for (int i = 0; i < m_f->m_exp[0]->GetExpSize(); ++i)
91  {
92  ElementGIDs[i] = m_f->m_exp[0]->GetExp(i)->GetGeom()->GetGlobalID();
93  }
94  m_f->m_fld->Import(fromfld,fromField->m_fielddef,
95  fromField->m_data,
97  ElementGIDs);
98  }
99  else
100  {
101  m_f->m_fld->Import(fromfld,fromField->m_fielddef,
102  fromField->m_data,
104  }
105 
106  bool samelength = true;
107  if(fromField->m_data.size() != m_f->m_data.size())
108  {
109  samelength = false;
110  }
111 
112  // scale input field
113  for(int i = 0; i < fromField->m_data.size(); ++i)
114  {
115  int datalen = fromField->m_data[i].size();
116 
117  Vmath::Smul(datalen, scale, &(fromField->m_data[i][0]), 1,
118  &(fromField->m_data[i][0]), 1);
119 
120  if(samelength)
121  {
122  if(datalen != m_f->m_data[i].size())
123  {
124  samelength = false;
125  }
126  }
127  }
128 
129  if(samelength == true)
130  {
131  for(int i = 0; i < m_f->m_data.size(); ++i)
132  {
133  int datalen = m_f->m_data[i].size();
134 
135  Vmath::Vadd(datalen, &(m_f->m_data[i][0]), 1,
136  &(fromField->m_data[i][0]), 1,
137  &(m_f->m_data[i][0]), 1);
138  }
139  }
140  else
141  {
142  ASSERTL0(m_f->m_exp.size() != 0 ,
143  "Input fields have partitions of different length and so xml "
144  "file needs to be specified");
145 
146  int nfields = m_f->m_fielddef[0]->m_fields.size();
147  int ncoeffs = m_f->m_exp[0]->GetNcoeffs();
148  Array<OneD, NekDouble> SaveFld(ncoeffs);
149 
150  for (int j = 0; j < nfields; ++j)
151  {
152  Vmath::Vcopy(ncoeffs,m_f->m_exp[j]->GetCoeffs(),1, SaveFld,1);
153 
154  // load new field
155  for (int i = 0; i < fromField->m_data.size(); ++i)
156  {
157  m_f->m_exp[j]->ExtractDataToCoeffs(
158  fromField->m_fielddef[i],
159  fromField->m_data[i],
160  fromField->m_fielddef[i]->m_fields[j],
161  m_f->m_exp[j]->UpdateCoeffs());
162  }
163 
164  Vmath::Vadd(ncoeffs, m_f->m_exp[j]->GetCoeffs(), 1,
165  SaveFld, 1,
166  m_f->m_exp[j]->UpdateCoeffs(), 1);
167  }
168 
169  std::vector<LibUtilities::FieldDefinitionsSharedPtr> FieldDef
170  = m_f->m_exp[0]->GetFieldDefinitions();
171  std::vector<std::vector<NekDouble> > FieldData(FieldDef.size());
172 
173  for(int i = 0; i < nfields; ++i)
174  {
175  for (int j = 0; j < FieldDef.size(); ++j)
176  {
177  FieldDef[j]->m_fields.push_back(m_f->m_fielddef[0]->m_fields[i]);
178  m_f->m_exp[i]->AppendFieldData(FieldDef[j], FieldData[j]);
179  }
180  }
181 
182  m_f->m_fielddef = FieldDef;
183  m_f->m_data = FieldData;
184 
185  }
186 }
187 
188 }
189 }
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:135
pair< ModuleType, string > ModuleKey
virtual void Process()=0
map< string, ConfigOption > m_config
List of configuration values.
STL namespace.
FieldSharedPtr m_f
Field object.
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:199
double NekDouble
boost::shared_ptr< Field > FieldSharedPtr
Definition: Field.hpp:677
Represents a command-line configuration option.
void Vcopy(int n, const T *x, const int incx, T *y, const int incy)
Definition: Vmath.cpp:1016
static FieldMetaDataMap NullFieldMetaDataMap
Definition: FieldIO.h:66
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:285
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