Nektar++
AddModeTo2DFld.cpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: AddModeTo2DFld.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:
32//
33///////////////////////////////////////////////////////////////////////////////
34
35#include <SpatialDomains/MeshGraph.h> // for FieldDefinitions, etc
37#include <cstdio>
38#include <cstdlib>
39
40using namespace std;
41using namespace Nektar;
42
43int main(int argc, char *argv[])
44{
45 NekDouble scal1, scal2;
46
47 if (argc != 6)
48 {
49 fprintf(
50 stderr,
51 "Usage: AddModeTo2DFld scal1 scal2 2Dfieldfile1 fieldfile2 "
52 "outfield\n"
53 "\t produces scal1*2Dfieldfiel1 + scal2*fieldfile2 in outfield\n");
54 exit(1);
55 }
56
57 scal1 = boost::lexical_cast<double>(argv[argc - 5]);
58 scal2 = boost::lexical_cast<double>(argv[argc - 4]);
59
60 //----------------------------------------------
61 // Import fieldfile1.
62 string fieldfile1(argv[argc - 3]);
63 vector<LibUtilities::FieldDefinitionsSharedPtr> fielddef1;
64 vector<vector<NekDouble>> fielddata1;
65 LibUtilities::Import(fieldfile1, fielddef1, fielddata1);
66 //----------------------------------------------
67
68 //----------------------------------------------
69 // Import fieldfile2.
70 string fieldfile2(argv[argc - 2]);
71 vector<LibUtilities::FieldDefinitionsSharedPtr> fielddef2;
72 vector<vector<NekDouble>> fielddata2;
73 LibUtilities::Import(fieldfile2, fielddef2, fielddata2);
74 //----------------------------------------------
75
76 vector<vector<NekDouble>> combineddata;
77
78 ASSERTL0(fielddata1.size() == fielddata2.size(),
79 "Inner has different size");
80 //----------------------------------------------
81 // Add fielddata2 to fielddata1 using m_fields definition to align data.
82
83 int i = 0;
84 int j = 0;
85 int k = 0;
86 int n = 0;
87
88 for (i = 0; i < fielddata2.size(); ++i)
89 {
90 ASSERTL0(fielddef2[i]->m_numHomogeneousDir == 1,
91 "Expected second fld to have one homogeneous direction");
92 ASSERTL0(fielddef2[i]->m_numModes[2] == 2,
93 "Expected Fourier field to have 2 modes");
94
95 int datalen1 = fielddata1[i].size() / fielddef1[i]->m_fields.size();
96 int datalen2 = fielddata2[i].size() / fielddef2[i]->m_fields.size();
97
98 ASSERTL0(datalen1 * 2 == datalen2,
99 "Data per fields is note compatible");
100
101 // Determine the number of coefficients per element
102 int ncoeffs = 0;
103 switch (fielddef2[i]->m_shapeType)
104 {
107 fielddef2[i]->m_numModes[0], fielddef2[i]->m_numModes[1]);
108 break;
110 ncoeffs =
111 fielddef2[i]->m_numModes[0] * fielddef2[i]->m_numModes[1];
112 break;
113 default:
114 ASSERTL0(false, "Shape not recognised");
115 break;
116 }
117
118 // array for zero packing
119 Array<OneD, NekDouble> Zero(ncoeffs, 0.0);
120
121 // scale first and second fields
122 Vmath::Smul(fielddata1[i].size(), scal1, &fielddata1[i][0], 1,
123 &fielddata1[i][0], 1);
124 Vmath::Smul(fielddata2[i].size(), scal2, &fielddata2[i][0], 1,
125 &fielddata2[i][0], 1);
126
127 vector<NekDouble> newdata;
128 auto vec_iter = fielddata2[i].begin();
129
130 for (k = 0; k < fielddef2[i]->m_fields.size(); ++k)
131 {
132 // get location of 2D field information in order of field2 ordering
133 int offset = 0;
134 for (j = 0; j < fielddef1[i]->m_fields.size(); ++j)
135 {
136 if (fielddef1[i]->m_fields[j] == fielddef2[i]->m_fields[k])
137 {
138 break;
139 }
140 offset += datalen1;
141 }
142
143 if (j != fielddef1[i]->m_fields.size())
144 {
145 for (n = 0; n < fielddef2[i]->m_elementIDs.size(); ++n)
146 {
147 // Real zero component
148 newdata.insert(
149 newdata.end(), &(fielddata1[i][offset + n * ncoeffs]),
150 &(fielddata1[i][offset + n * ncoeffs]) + ncoeffs);
151
152 // Imaginary zero component;
153 newdata.insert(newdata.end(), &Zero[0], &Zero[0] + ncoeffs);
154
155 // Put orginal mode in here.
156 newdata.insert(newdata.end(), vec_iter,
157 vec_iter + 2 * ncoeffs);
158 vec_iter += 2 * ncoeffs;
159 }
160 }
161 else
162 {
163
164 for (n = 0; n < fielddef2[i]->m_elementIDs.size(); ++n)
165 {
166 // Real & Imag zero component
167 newdata.insert(newdata.end(), &Zero[0], &Zero[0] + ncoeffs);
168 newdata.insert(newdata.end(), &Zero[0], &Zero[0] + ncoeffs);
169
170 // Put orginal mode in here.
171 newdata.insert(newdata.end(), vec_iter,
172 vec_iter + 2 * ncoeffs);
173 vec_iter += 2 * ncoeffs;
174 }
175 }
176 }
177 combineddata.push_back(newdata);
178 fielddef2[i]->m_numModes[2] += 2;
179 fielddef2[i]->m_homogeneousZIDs.push_back(2);
180 fielddef2[i]->m_homogeneousZIDs.push_back(3);
181
182 // check to see if any field in fielddef1[i]->m_fields is
183 // not defined in fielddef2[i]->m_fields
184 for (k = 0; k < fielddef1[i]->m_fields.size(); ++k)
185 {
186 for (j = 0; j < fielddef2[i]->m_fields.size(); ++j)
187 {
188 if (fielddef1[i]->m_fields[k] == fielddef2[i]->m_fields[j])
189 {
190 break;
191 }
192 }
193
194 if (j == fielddef2[i]->m_fields.size())
195 {
196 cout << "Warning: Field \'" << fielddef1[i]->m_fields[k]
197 << "\' was not included in output " << endl;
198 }
199 }
200 }
201 //----------------------------------------------
202
203 //-----------------------------------------------
204 // Write out datafile.
205 LibUtilities::Write(argv[argc - 1], fielddef2, combineddata);
206 //-----------------------------------------------
207
208 return 0;
209}
int main(int argc, char *argv[])
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:208
void Import(const std::string &infilename, std::vector< FieldDefinitionsSharedPtr > &fielddefs, std::vector< std::vector< NekDouble > > &fielddata, FieldMetaDataMap &fieldinfomap, const Array< OneD, int > &ElementIDs)
This function allows for data to be imported from an FLD file when a session and/or communicator is n...
Definition: FieldIO.cpp:288
void Write(const std::string &outFile, std::vector< FieldDefinitionsSharedPtr > &fielddefs, std::vector< std::vector< NekDouble > > &fielddata, const FieldMetaDataMap &fieldinfomap, const bool backup)
This function allows for data to be written to an FLD file when a session and/or communicator is not ...
Definition: FieldIO.cpp:245
double NekDouble
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.hpp:100
void Zero(int n, T *x, const int incx)
Zero vector.
Definition: Vmath.hpp:273
STL namespace.