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 // Permission is hereby granted, free of charge, to any person obtaining a
14 // copy of this software and associated documentation files (the "Software"),
15 // to deal in the Software without restriction, including without limitation
16 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
17 // and/or sell copies of the Software, and to permit persons to whom the
18 // Software is furnished to do so, subject to the following conditions:
19 //
20 // The above copyright notice and this permission notice shall be included
21 // in all copies or substantial portions of the Software.
22 //
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29 // DEALINGS IN THE SOFTWARE.
30 //
31 // Description: Add a field to the intput field
32 //
33 ////////////////////////////////////////////////////////////////////////////////
34 
35 #include <iostream>
36 #include <string>
37 using namespace std;
38 
39 #include <boost/core/ignore_unused.hpp>
40 
42 
43 #include "ProcessAddFld.h"
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  if(f->m_inputfiles.count("xml"))
64  {
66  }
67  else
68  {
70  }
71 }
72 
74 {
75 }
76 
77 void ProcessAddFld::Process(po::variables_map &vm)
78 {
79  boost::ignore_unused(vm);
80 
81  string scalestr = m_config["scale"].as<string>();
82  NekDouble scale = boost::lexical_cast<NekDouble>(scalestr);
83 
84  ASSERTL0(m_config["fromfld"].as<string>().compare("NotSet") != 0,
85  "Need to specify fromfld=file.fld ");
86  string fromfld = m_config["fromfld"].as<string>();
87 
88  vector<LibUtilities::FieldDefinitionsSharedPtr> fromFieldDef;
89  vector<vector<double> > fromFieldData;
90 
91  if (m_f->m_graph)
92  {
93  const SpatialDomains::ExpansionInfoMap &expansions =
94  m_f->m_graph->GetExpansionInfo();
95 
96  // if Range has been speficied it is possible to have a
97  // partition which is empty so check this and return if
98  // no elements present.
99 
100  if (!expansions.size())
101  {
102  return;
103  }
104 
105  Array<OneD, int> ElementGIDs(expansions.size());
106 
107  int i = 0;
108  for (auto &expIt : expansions)
109  {
110  ElementGIDs[i++] = expIt.second->m_geomShPtr->GetGlobalID();
111  }
112  m_f->FieldIOForFile(fromfld)->Import(
113  fromfld, fromFieldDef, fromFieldData,
115  }
116  else
117  {
118  m_f->FieldIOForFile(fromfld)->Import(
119  fromfld, fromFieldDef, fromFieldData,
121  }
122 
123  bool samelength = true;
124  if (fromFieldData.size() != m_f->m_data.size())
125  {
126  samelength = false;
127  }
128 
129  // scale input field
130  for (int i = 0; i < fromFieldData.size(); ++i)
131  {
132  int datalen = fromFieldData[i].size();
133 
134  Vmath::Smul(datalen, scale, &(fromFieldData[i][0]), 1,
135  &(fromFieldData[i][0]), 1);
136 
137  if (samelength)
138  {
139  if (datalen != m_f->m_data[i].size())
140  {
141  samelength = false;
142  }
143  }
144  }
145 
147  {
148  ASSERTL0(samelength == true,
149  "Input fields have partitions of different length and so xml "
150  "file needs to be specified");
151  for (int i = 0; i < m_f->m_data.size(); ++i)
152  {
153  int datalen = m_f->m_data[i].size();
154 
155  Vmath::Vadd(datalen, &(m_f->m_data[i][0]), 1,
156  &(fromFieldData[i][0]), 1, &(m_f->m_data[i][0]), 1);
157  }
158 
159  }
160  else
161  {
162  // Skip in case of empty partition
163  if (m_f->m_exp[0]->GetNumElmts() == 0)
164  {
165  return;
166  }
167 
168  int nfields = m_f->m_variables.size();
169  int ncoeffs = m_f->m_exp[0]->GetNcoeffs();
170  Array<OneD, NekDouble> SaveFld(ncoeffs);
171 
172  for (int j = 0; j < nfields; ++j)
173  {
174  Vmath::Vcopy(ncoeffs, m_f->m_exp[j]->GetCoeffs(), 1, SaveFld, 1);
175 
176  // Check if new field has this variable
177  auto it = find (fromFieldDef[0]->m_fields.begin(),
178  fromFieldDef[0]->m_fields.end(),
179  m_f->m_variables[j]);
180 
181  ASSERTL0(it != fromFieldDef[0]->m_fields.end(),
182  "Could not find field " + m_f->m_variables[j] + " in from field");
183 
184  // load new field
185  for (int i = 0; i < fromFieldData.size(); ++i)
186  {
187  m_f->m_exp[j]->ExtractDataToCoeffs(
188  fromFieldDef[i], fromFieldData[i],
189  m_f->m_variables[j],
190  m_f->m_exp[j]->UpdateCoeffs());
191  }
192 
193  Vmath::Vadd(ncoeffs, m_f->m_exp[j]->GetCoeffs(), 1, SaveFld, 1,
194  m_f->m_exp[j]->UpdateCoeffs(), 1);
195  m_f->m_exp[j]->BwdTrans(
196  m_f->m_exp[j]->GetCoeffs(),
197  m_f->m_exp[j]->UpdatePhys());
198  }
199  }
200 }
201 }
202 }
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:216
FieldSharedPtr m_f
Field object.
Definition: Module.h:230
std::map< std::string, ConfigOption > m_config
List of configuration values.
Definition: Module.h:233
virtual void Process(po::variables_map &vm)
Write mesh to output file.
Abstract base class for processing modules.
Definition: Module.h:265
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, std::string pDesc="")
Register a class with the factory.
Definition: NekFactory.hpp:200
std::shared_ptr< Field > FieldSharedPtr
Definition: Field.hpp:989
std::pair< ModuleType, std::string > ModuleKey
Definition: Module.h:290
ModuleFactory & GetModuleFactory()
Definition: Module.cpp:49
static FieldMetaDataMap NullFieldMetaDataMap
Definition: FieldIO.h:53
std::map< int, ExpansionInfoShPtr > ExpansionInfoMap
Definition: MeshGraph.h:143
InputIterator find(InputIterator first, InputIterator last, InputIterator startingpoint, const EqualityComparable &value)
Definition: StdRegions.hpp:362
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:1
double NekDouble
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:322
void Smul(int n, const T alpha, const T *x, const int incx, T *y, const int incy)
Scalar multiply y = alpha*x.
Definition: Vmath.cpp:225
void Vcopy(int n, const T *x, const int incx, T *y, const int incy)
Definition: Vmath.cpp:1199
Represents a command-line configuration option.
Definition: Module.h:134