Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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  {
43 
46  const std::map<std::string, std::string> &pParams) :
47  Filter(pSession)
48  {
49  if (pParams.find("OutputFile") == pParams.end())
50  {
51  m_outputFile = m_session->GetSessionName();
52  }
53  else
54  {
55  ASSERTL0(!(pParams.find("OutputFile")->second.empty()),
56  "Missing parameter 'OutputFile'.");
57  m_outputFile = pParams.find("OutputFile")->second;
58  }
59 
60  if(pParams.find("SampleFrequency") == pParams.end())
61  {
63  }
64  else
65  {
66  m_sampleFrequency = atoi(pParams.find("SampleFrequency")->second.c_str());
67  }
68 
69  if(pParams.find("OutputFrequency") == pParams.end())
70  {
71  m_outputFrequency = m_session->GetParameter("NumSteps");
72  }
73  else
74  {
75  m_outputFrequency = atoi(pParams.find("OutputFrequency")->second.c_str());
76  }
77 
78  m_numAverages = 0;
79  m_index = 0;
80  m_outputIndex = 0;
82 
83  }
84 
86  {
87  }
88 
89  void FilterAverageFields::v_Initialise(const Array<OneD, const MultiRegions::ExpListSharedPtr> &pFields, const NekDouble &time)
90  {
91  m_avgFields = Array<OneD, Array<OneD, NekDouble> >(pFields.num_elements());
92  for(int n =0; n < pFields.num_elements(); ++n)
93  {
94  m_avgFields[n] = Array<OneD, NekDouble>(pFields[n]->GetNcoeffs(),0.0);
95  }
96  m_avgFieldMetaData["InitialTime"] = boost::lexical_cast<std::string>(time);
97  }
98 
99  void FilterAverageFields::v_Update(const Array<OneD, const MultiRegions::ExpListSharedPtr> &pFields, const NekDouble &time)
100  {
101  m_index++;
102  if (m_index % m_sampleFrequency > 0)
103  {
104  return;
105  }
106 
107  for(int n = 0; n < pFields.num_elements(); ++n)
108  {
109  Vmath::Vadd(m_avgFields[n].num_elements(),
110  pFields[n]->GetCoeffs(),1,m_avgFields[n],1,
111  m_avgFields[n],1);
112  }
113  m_numAverages += 1;
114  // update FinalTime here since this is when last field will be added.
115  m_avgFieldMetaData["FinalTime"] = boost::lexical_cast<std::string>(time);
116 
117  if (m_index % m_outputFrequency == 0)
118  {
119  OutputAvgField(pFields,++m_outputIndex);
120  }
121 
122  }
123 
124  void FilterAverageFields::v_Finalise(const Array<OneD, const MultiRegions::ExpListSharedPtr> &pFields, const NekDouble &time)
125  {
126  OutputAvgField(pFields);
127  }
128 
129  void FilterAverageFields::OutputAvgField(const Array<OneD, const MultiRegions::ExpListSharedPtr> &pFields, int dump)
130  {
131  for(int n = 0; n < m_avgFields.num_elements(); ++n)
132  {
133  Vmath::Smul(m_avgFields[n].num_elements(), 1.0/m_numAverages,m_avgFields[n],1,m_avgFields[n],1);
134  }
135 
136  std::vector<LibUtilities::FieldDefinitionsSharedPtr> FieldDef
137  = pFields[0]->GetFieldDefinitions();
138  std::vector<std::vector<NekDouble> > FieldData(FieldDef.size());
139 
140  Array<OneD, NekDouble> fieldcoeffs;
141  int ncoeffs = pFields[0]->GetNcoeffs();
142 
143  // copy Data into FieldData and set variable
144  for(int j = 0; j < pFields.num_elements(); ++j)
145  {
146  // check to see if field is same order a zeroth field
147  if(m_avgFields[j].num_elements() == ncoeffs)
148  {
149  fieldcoeffs = m_avgFields[j];
150  }
151  else
152  {
153  fieldcoeffs = Array<OneD,NekDouble>(ncoeffs);
154  pFields[0]->ExtractCoeffsToCoeffs(pFields[j],m_avgFields[j],fieldcoeffs);
155  }
156 
157  for(int i = 0; i < FieldDef.size(); ++i)
158  {
159  // Could do a search here to find correct variable
160  FieldDef[i]->m_fields.push_back(m_session->GetVariable(j));
161  pFields[0]->AppendFieldData(FieldDef[i], FieldData[i], fieldcoeffs);
162  }
163  }
164 
165  m_avgFieldMetaData["NumberOfFieldDumps"] = boost::lexical_cast<std::string>(m_numAverages);
166 
167  std::stringstream outname;
168  if(dump == -1) // final dump
169  {
170  outname << m_outputFile << "_avg.fld";
171  }
172  else
173  {
174  outname << m_outputFile<< "_" << dump << "_avg.fld";
175  }
176 
177  m_fld->Write(outname.str(),FieldDef,FieldData,m_avgFieldMetaData);
178 
179  if(dump != -1) // not final dump so rescale cummulative average
180  {
181  for(int n = 0; n < m_avgFields.num_elements(); ++n)
182  {
183  Vmath::Smul(m_avgFields[n].num_elements(), (NekDouble) m_numAverages,
184  m_avgFields[n],1,m_avgFields[n],1);
185  }
186  }
187  }
188 
190  {
191  return true;
192  }
193  }
194 }