Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FldAddFld.cpp
Go to the documentation of this file.
1 #include <cstdio>
2 #include <cstdlib>
3 
4 #include <LibUtilities/BasicUtils/ErrorUtil.hpp> // for ASSERTL0
5 #include <SpatialDomains/MeshGraph.h> // for FieldDefinitions, etc
6 
7 using namespace std;
8 using namespace Nektar;
9 
10 int main(int argc, char *argv[])
11 {
12  NekDouble scal1,scal2;
13 
14  if(argc != 6)
15  {
16  fprintf(stderr,"Usage: FldAddFld scal1 scal2 fieldfile1 fieldfile2 outfield\n"
17  "\t produces scal1*fieldfiel1 + scal2*fieldfile2 in outfield\n" );
18  exit(1);
19  }
20 
21  scal1 = boost::lexical_cast<double>(argv[argc-5]);
22  scal2 = boost::lexical_cast<double>(argv[argc-4]);
23 
24  //default meshgraph
26 
27  //----------------------------------------------
28  // Import fieldfile1.
29  string fieldfile1(argv[argc-3]);
30  vector<LibUtilities::FieldDefinitionsSharedPtr> fielddef1;
31  vector<vector<NekDouble> > fielddata1;
32  LibUtilities::Import(fieldfile1,fielddef1,fielddata1);
33  //----------------------------------------------
34 
35  //----------------------------------------------
36  // Import fieldfile2.
37  string fieldfile2(argv[argc-2]);
38  vector<LibUtilities::FieldDefinitionsSharedPtr> fielddef2;
39  vector<vector<NekDouble> > fielddata2;
40  LibUtilities::Import(fieldfile2,fielddef2,fielddata2);
41  //----------------------------------------------
42 
43 
44  ASSERTL0(fielddata1.size() == fielddata2.size(),"Inner has different size");
45 
46  //----------------------------------------------
47  // Add fielddata2 to fielddata1 using m_fields definition to align data.
48 
49 
50  for(int i = 0; i < fielddata1.size(); ++i)
51  {
52  int j;
53  int datalen1 = fielddata1[i].size()/fielddef1[i]->m_fields.size();
54  int datalen2 = fielddata2[i].size()/fielddef2[i]->m_fields.size();
55 
56  ASSERTL0(datalen1 == datalen2,"Data per field is of different length");
57 
58  for(int k = 0; k < fielddef1[i]->m_fields.size(); ++k)
59  {
60  int offset = 0;
61  for(j = 0; j < fielddef2[i]->m_fields.size(); ++j)
62  {
63  if(fielddef1[i]->m_fields[k] == fielddef2[i]->m_fields[j])
64  {
65  break;
66  }
67  offset += datalen1;
68  }
69 
70  if(j == fielddef2[i]->m_fields.size())
71  {
72  for(j = 0; j < datalen1; ++j)
73  {
74  fielddata1[i][datalen1*k+j] *= scal1;
75  }
76  }
77  else // add fields
78  {
79  for(j = 0; j < datalen1; ++j)
80  {
81  fielddata1[i][datalen1*k+j] *= scal1;
82  fielddata1[i][datalen1*k+j] += scal2*fielddata2[i][offset + j];
83  }
84  }
85 
86  }
87 
88  // now check to see if any field in fielddef2[i]->m_fields is
89  // not defined in fielddef1[i]->m_fields
90  for(int k = 0; k < fielddef2[i]->m_fields.size(); ++k)
91  {
92  for(j = 0; j < fielddef1[i]->m_fields.size(); ++j)
93  {
94  if(fielddef2[i]->m_fields[k] == fielddef1[i]->m_fields[j])
95  {
96  break;
97  }
98  }
99 
100  if(j == fielddef1[i]->m_fields.size())
101  {
102  for(j = 0; j < datalen2; ++j)
103  {
104  fielddata2[i][datalen2*k+j] *= scal2;
105  }
106 
107  // add this field to fielddata1
108  fielddef1[i]->m_fields.push_back(fielddef2[i]->m_fields[k]);
109  fielddata1[i].insert(fielddata1[i].end(),&(fielddata2[i][k*datalen2]),
110  &(fielddata2[i][k*datalen2])+datalen1);
111  }
112 
113  }
114 
115  }
116  //----------------------------------------------
117 
118  //-----------------------------------------------
119  // Write out datafile.
120  LibUtilities::Write(argv[argc-1], fielddef1, fielddata1);
121  //-----------------------------------------------
122 
123  return 0;
124 }
125 
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:188
STL namespace.
void Import(const std::string &infilename, std::vector< FieldDefinitionsSharedPtr > &fielddefs, std::vector< std::vector< NekDouble > > &fielddata, FieldMetaDataMap &fieldinfomap, const Array< OneD, int > ElementiDs)
Imports an FLD file.
Definition: FieldIO.cpp:115
int main(int argc, char *argv[])
Definition: FldAddFld.cpp:10
double NekDouble
Base class for a spectral/hp element mesh.
Definition: MeshGraph.h:186
void Write(const std::string &outFile, std::vector< FieldDefinitionsSharedPtr > &fielddefs, std::vector< std::vector< NekDouble > > &fielddata, const FieldMetaDataMap &fieldinfomap)
Write a field file in serial only.
Definition: FieldIO.cpp:81