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