Nektar++
FilterAverageFields.cpp
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // File FilterAverageField.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: Average solution fields during time-stepping.
33 //
34 ///////////////////////////////////////////////////////////////////////////////
35 
37 
38 namespace Nektar
39 {
40 namespace SolverUtils
41 {
44  "AverageFields", FilterAverageFields::create);
45 
48  const ParamMap &pParams) :
49  Filter(pSession)
50 {
51  ParamMap::const_iterator it;
52 
53  // OutputFile
54  it = pParams.find("OutputFile");
55  if (it == pParams.end())
56  {
57  m_outputFile = m_session->GetSessionName();
58  }
59  else
60  {
61  ASSERTL0(it->second.length() > 0, "Missing parameter 'OutputFile'.");
62  m_outputFile = it->second;
63  }
64 
65  // SampleFrequency
66  it = pParams.find("SampleFrequency");
67  if(it == pParams.end())
68  {
70  }
71  else
72  {
73  LibUtilities::Equation equ(m_session, it->second);
74  m_sampleFrequency = floor(equ.Evaluate());
75  }
76 
77  // OutputFrequency
78  it = pParams.find("OutputFrequency");
79  if(it == pParams.end())
80  {
81  m_outputFrequency = m_session->GetParameter("NumSteps");
82  }
83  else
84  {
85  LibUtilities::Equation equ(m_session, it->second);
86  m_outputFrequency = floor(equ.Evaluate());
87  }
88 
89  m_numAverages = 0;
90  m_index = 0;
91  m_outputIndex = 0;
93  ::AllocateSharedPtr(pSession->GetComm());
94 
95 }
96 
98 {
99 }
100 
103  const NekDouble &time)
104 {
105  int nfield = pFields.num_elements();
106  int ncoeff = pFields[0]->GetNcoeffs();
108  for(int n =0; n < nfield; ++n)
109  {
110  m_avgFields[n] = Array<OneD, NekDouble>(ncoeff, 0.0);
111  }
112  m_avgFieldMetaData["InitialTime"]
113  = boost::lexical_cast<std::string>(time);
114 }
115 
118  const NekDouble &time)
119 {
120  m_index++;
121  if (m_index % m_sampleFrequency > 0)
122  {
123  return;
124  }
125 
126  for(int n = 0; n < pFields.num_elements(); ++n)
127  {
128  Vmath::Vadd(m_avgFields[n].num_elements(),
129  pFields[n]->GetCoeffs(),1,m_avgFields[n],1,
130  m_avgFields[n],1);
131  }
132  m_numAverages += 1;
133 
134  // update FinalTime here since this is when last field will be added.
135  m_avgFieldMetaData["FinalTime"]
136  = boost::lexical_cast<std::string>(time);
137 
138  if (m_index % m_outputFrequency == 0)
139  {
140  OutputAvgField(pFields,++m_outputIndex);
141  }
142 
143 }
144 
147  const NekDouble &time)
148 {
149  OutputAvgField(pFields);
150 }
151 
154  int dump)
155 {
156  for(int n = 0; n < m_avgFields.num_elements(); ++n)
157  {
158  Vmath::Smul(m_avgFields[n].num_elements(), 1.0/m_numAverages,
159  m_avgFields[n], 1,
160  m_avgFields[n], 1);
161  }
162 
163  std::vector<LibUtilities::FieldDefinitionsSharedPtr> FieldDef
164  = pFields[0]->GetFieldDefinitions();
165  std::vector<std::vector<NekDouble> > FieldData(FieldDef.size());
166 
167  Array<OneD, NekDouble> fieldcoeffs;
168  int ncoeffs = pFields[0]->GetNcoeffs();
169 
170  // copy Data into FieldData and set variable
171  for(int j = 0; j < pFields.num_elements(); ++j)
172  {
173  // check to see if field is same order a zeroth field
174  if(m_avgFields[j].num_elements() == ncoeffs)
175  {
176  fieldcoeffs = m_avgFields[j];
177  }
178  else
179  {
180  fieldcoeffs = Array<OneD,NekDouble>(ncoeffs);
181  pFields[0]->ExtractCoeffsToCoeffs(pFields[j],
182  m_avgFields[j],
183  fieldcoeffs);
184  }
185 
186  for(int i = 0; i < FieldDef.size(); ++i)
187  {
188  // Could do a search here to find correct variable
189  FieldDef[i]->m_fields.push_back(m_session->GetVariable(j));
190  pFields[0]->AppendFieldData(FieldDef[i],
191  FieldData[i],
192  fieldcoeffs);
193  }
194  }
195 
196  m_avgFieldMetaData["NumberOfFieldDumps"]
197  = boost::lexical_cast<std::string>(m_numAverages);
198 
199  std::stringstream outname;
200  if(dump == -1) // final dump
201  {
202  outname << m_outputFile << "_avg.fld";
203  }
204  else
205  {
206  outname << m_outputFile<< "_" << dump << "_avg.fld";
207  }
208 
209  m_fld->Write(outname.str(),FieldDef,FieldData,m_avgFieldMetaData);
210 
211  if(dump != -1) // not final dump so rescale cummulative average
212  {
213  for(int n = 0; n < m_avgFields.num_elements(); ++n)
214  {
215  Vmath::Smul(m_avgFields[n].num_elements(),
217  m_avgFields[n], 1,
218  m_avgFields[n], 1);
219  }
220  }
221 }
222 
224 {
225  return true;
226 }
227 }
228 }
virtual void v_Finalise(const Array< OneD, const MultiRegions::ExpListSharedPtr > &pFields, const NekDouble &time)
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:161
LibUtilities::FieldMetaDataMap m_avgFieldMetaData
static boost::shared_ptr< DataType > AllocateSharedPtr()
Allocate a shared pointer from the memory pool.
virtual void v_Initialise(const Array< OneD, const MultiRegions::ExpListSharedPtr > &pFields, const NekDouble &time)
static std::string className
Name of the class.
Array< OneD, Array< OneD, NekDouble > > m_avgFields
void OutputAvgField(const Array< OneD, const MultiRegions::ExpListSharedPtr > &pFields, int dump=-1)
boost::shared_ptr< SessionReader > SessionReaderSharedPtr
Definition: MeshPartition.h:50
virtual void v_Update(const Array< OneD, const MultiRegions::ExpListSharedPtr > &pFields, const NekDouble &time)
void Smul(int n, const T alpha, const T *x, const int incx, T *y, const int incy)
Scalar multiply y = alpha*y.
Definition: Vmath.cpp:199
static FilterSharedPtr create(const LibUtilities::SessionReaderSharedPtr &pSession, const std::map< std::string, std::string > &pParams)
Creates an instance of this class.
NekDouble Evaluate() const
Definition: Equation.h:102
double NekDouble
std::map< std::string, std::string > ParamMap
Definition: Filter.h:67
LibUtilities::SessionReaderSharedPtr m_session
Definition: Filter.h:84
SOLVER_UTILS_EXPORT FilterAverageFields(const LibUtilities::SessionReaderSharedPtr &pSession, const ParamMap &pParams)
FilterFactory & GetFilterFactory()
Definition: Filter.cpp:42
LibUtilities::FieldIOSharedPtr m_fld
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:285
virtual SOLVER_UTILS_EXPORT ~FilterAverageFields()
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, tDescription pDesc="")
Register a class with the factory.
Definition: NekFactory.hpp:215