Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator 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 <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  if((f->m_inputfiles.count("fld") == 0) &&
58  (f->m_inputfiles.count("rst") == 0) &&
59  (f->m_inputfiles.count("chk") == 0))
60  {
61  cout << "A fld, chk or rst input file must be specified for the "
62  "scaleinputfld module" << endl;
63  exit(3);
64  }
65 
66  m_config["scale"] = ConfigOption(false, "1.0", "scale factor");
67 
68  m_config["fromfld"] = ConfigOption(false, "NotSet",
69  "Fld file form which to interpolate field");
70 
71  ASSERTL0(m_config["fromfld"].as<string>().compare("NotSet") != 0,
72  "Need to specify fromfld=file.fld ");
73 
74 }
75 
77 {
78 }
79 
80 void ProcessAddFld::Process(po::variables_map &vm)
81 {
82  if (m_f->m_verbose)
83  {
84  cout << "ProcessAddFld: Adding new fld to input fld" << endl;
85  }
86 
87  ASSERTL0(m_f->m_data.size() != 0,"No input data defined");
88 
89  string scalestr = m_config["scale"].as<string>();
90  NekDouble scale = boost::lexical_cast<NekDouble>(scalestr);
91 
92  string fromfld = m_config["fromfld"].as<string>();
93  m_fromField = boost::shared_ptr<Field>(new Field());
94 
95  if(m_f->m_exp.size())
96  {
97  // Set up ElementGIDs in case of parallel processing
98  Array<OneD,int> ElementGIDs(m_f->m_exp[0]->GetExpSize());
99  for (int i = 0; i < m_f->m_exp[0]->GetExpSize(); ++i)
100  {
101  ElementGIDs[i] = m_f->m_exp[0]->GetExp(i)->GetGeom()->GetGlobalID();
102  }
103  m_f->m_fld->Import(fromfld,m_fromField->m_fielddef,
104  m_fromField->m_data,
106  ElementGIDs);
107  }
108  else
109  {
110  m_f->m_fld->Import(fromfld,m_fromField->m_fielddef,
111  m_fromField->m_data,
113  }
114 
115  bool samelength = true;
116  if(m_fromField->m_data.size() != m_f->m_data.size())
117  {
118  samelength = false;
119  }
120 
121  // scale input field
122  for(int i = 0; i < m_fromField->m_data.size(); ++i)
123  {
124  int datalen = m_fromField->m_data[i].size();
125 
126  Vmath::Smul(datalen, scale, &(m_fromField->m_data[i][0]), 1,
127  &(m_fromField->m_data[i][0]), 1);
128 
129  if(samelength)
130  {
131  if(datalen != m_f->m_data[i].size())
132  {
133  samelength = false;
134  }
135  }
136  }
137 
138  if(samelength == true)
139  {
140 
141  for(int i = 0; i < m_f->m_data.size(); ++i)
142  {
143  int datalen = m_f->m_data[i].size();
144 
145  Vmath::Vadd(datalen, &(m_f->m_data[i][0]), 1,
146  &(m_fromField->m_data[i][0]), 1,
147  &(m_f->m_data[i][0]), 1);
148  }
149  }
150  else
151  {
152  ASSERTL0(m_f->m_exp.size() != 0 ,
153  "Input fields have partitions of different length and so xml "
154  "file needs to be specified");
155 
156  int nfields = m_f->m_fielddef[0]->m_fields.size();
157  int ncoeffs = m_f->m_exp[0]->GetNcoeffs();
158  Array<OneD, NekDouble> SaveFld(ncoeffs);
159 
160  for (int j = 0; j < nfields; ++j)
161  {
162  Vmath::Vcopy(ncoeffs,m_f->m_exp[j]->GetCoeffs(),1, SaveFld,1);
163 
164  // load new field
165  for (int i = 0; i < m_fromField->m_data.size(); ++i)
166  {
167  m_f->m_exp[j]->ExtractDataToCoeffs(
168  m_fromField->m_fielddef[i],
169  m_fromField->m_data[i],
170  m_fromField->m_fielddef[i]->m_fields[j],
171  m_f->m_exp[j]->UpdateCoeffs());
172  }
173 
174  Vmath::Vadd(ncoeffs, m_f->m_exp[j]->GetCoeffs(), 1,
175  SaveFld, 1,
176  m_f->m_exp[j]->UpdateCoeffs(), 1);
177  }
178 
179  std::vector<LibUtilities::FieldDefinitionsSharedPtr> FieldDef
180  = m_f->m_exp[0]->GetFieldDefinitions();
181  std::vector<std::vector<NekDouble> > FieldData(FieldDef.size());
182 
183  for(int i = 0; i < nfields; ++i)
184  {
185  for (int j = 0; j < FieldDef.size(); ++j)
186  {
187  FieldDef[j]->m_fields.push_back(m_f->m_fielddef[0]->m_fields[i]);
188  m_f->m_exp[i]->AppendFieldData(FieldDef[j], FieldData[j]);
189  }
190  }
191 
192  m_f->m_fielddef = FieldDef;
193  m_f->m_data = FieldData;
194 
195  }
196 
197 
198 
199 
200 
201 
202 }
203 
204 }
205 }