Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FilterSampler.cpp
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // File FilterSampler.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: Base clase for filters performing operations on samples
33 // of the field.
34 //
35 ///////////////////////////////////////////////////////////////////////////////
36 
38 
39 namespace Nektar
40 {
41 namespace SolverUtils
42 {
43 
46  const ParamMap &pParams)
47  : Filter(pSession)
48 {
49  ParamMap::const_iterator it;
50 
51  // OutputFile
52  it = pParams.find("OutputFile");
53  if (it == pParams.end())
54  {
55  m_outputFile = m_session->GetSessionName();
56  }
57  else
58  {
59  ASSERTL0(it->second.length() > 0, "Missing parameter 'OutputFile'.");
60  m_outputFile = it->second;
61  }
62 
63  // SampleFrequency
64  it = pParams.find("SampleFrequency");
65  if (it == pParams.end())
66  {
68  }
69  else
70  {
71  LibUtilities::Equation equ(m_session, it->second);
72  m_sampleFrequency = floor(equ.Evaluate());
73  }
74 
75  // OutputFrequency
76  it = pParams.find("OutputFrequency");
77  if (it == pParams.end())
78  {
79  m_outputFrequency = m_session->GetParameter("NumSteps");
80  }
81  else
82  {
83  LibUtilities::Equation equ(m_session, it->second);
84  m_outputFrequency = floor(equ.Evaluate());
85  }
86 
87  m_scale = 1.0;
88  m_numSamples = 0;
89  m_index = 0;
90  m_outputIndex = 0;
92  pSession->GetComm());
93 }
94 
96 {
97 }
98 
101  const NekDouble &time)
102 {
103  int ncoeff = pFields[0]->GetNcoeffs();
104  // m_variables need to be filled by a derived class
105  m_outFields.resize(m_variables.size());
106 
107  for (int n = 0; n < m_variables.size(); ++n)
108  {
109  m_outFields[n] = Array<OneD, NekDouble>(ncoeff, 0.0);
110  }
111 
112  m_fieldMetaData["InitialTime"] = boost::lexical_cast<std::string>(time);
113 }
114 
117  const NekDouble &time)
118 {
119  m_index++;
120  if (m_index % m_sampleFrequency > 0)
121  {
122  return;
123  }
124 
125  m_numSamples++;
126  v_ProcessSample(pFields, time);
127 
128  if (m_index % m_outputFrequency == 0)
129  {
130  m_fieldMetaData["FinalTime"] = boost::lexical_cast<std::string>(time);
131  v_PrepareOutput(pFields, time);
132  OutputField(pFields, ++m_outputIndex);
133  }
134 }
135 
138  const NekDouble &time)
139 {
140  m_fieldMetaData["FinalTime"] = boost::lexical_cast<std::string>(time);
141  v_PrepareOutput(pFields, time);
142  OutputField(pFields);
143 }
144 
146  const Array<OneD, const MultiRegions::ExpListSharedPtr> &pFields, int dump)
147 {
148  for (int n = 0; n < m_outFields.size(); ++n)
149  {
150  Vmath::Smul(m_outFields[n].num_elements(),
151  m_scale,
152  m_outFields[n],
153  1,
154  m_outFields[n],
155  1);
156  }
157 
158  std::vector<LibUtilities::FieldDefinitionsSharedPtr> FieldDef =
159  pFields[0]->GetFieldDefinitions();
160  std::vector<std::vector<NekDouble> > FieldData(FieldDef.size());
161 
162  Array<OneD, NekDouble> fieldcoeffs;
163  int ncoeffs = pFields[0]->GetNcoeffs();
164 
165  // copy Data into FieldData and set variable
166  for (int j = 0; j < m_outFields.size(); ++j)
167  {
168  // check to see if field is same order a zeroth field
169  if (m_outFields[j].num_elements() == ncoeffs)
170  {
171  fieldcoeffs = m_outFields[j];
172  }
173  else
174  {
175  fieldcoeffs = Array<OneD, NekDouble>(ncoeffs);
176  pFields[0]->ExtractCoeffsToCoeffs(
177  pFields[j], m_outFields[j], fieldcoeffs);
178  }
179 
180  for (int i = 0; i < FieldDef.size(); ++i)
181  {
182  FieldDef[i]->m_fields.push_back(m_variables[j]);
183  pFields[0]->AppendFieldData(FieldDef[i], FieldData[i], fieldcoeffs);
184  }
185  }
186 
187  m_fieldMetaData["NumberOfFieldDumps"] =
188  boost::lexical_cast<std::string>(m_numSamples);
189 
190  std::stringstream outname;
191  std::string suffix = v_GetFileSuffix();
192  if (dump == -1) // final dump
193  {
194  outname << m_outputFile << suffix << ".fld";
195  }
196  else
197  {
198  outname << m_outputFile << "_" << dump << suffix << ".fld";
199  }
200 
201  m_fld->Write(outname.str(), FieldDef, FieldData, m_fieldMetaData);
202 
203  if (dump != -1) // not final dump so rescale
204  {
205  for (int n = 0; n < m_outFields.size(); ++n)
206  {
207  Vmath::Smul(m_outFields[n].num_elements(),
208  1.0 / m_scale,
209  m_outFields[n],
210  1,
211  m_outFields[n],
212  1);
213  }
214  }
215 }
216 }
217 }
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:188
std::vector< Array< OneD, NekDouble > > m_outFields
Definition: FilterSampler.h:90
virtual SOLVER_UTILS_EXPORT void v_Update(const Array< OneD, const MultiRegions::ExpListSharedPtr > &pFields, const NekDouble &time)
static boost::shared_ptr< DataType > AllocateSharedPtr()
Allocate a shared pointer from the memory pool.
virtual SOLVER_UTILS_EXPORT std::string v_GetFileSuffix()=0
boost::shared_ptr< SessionReader > SessionReaderSharedPtr
Definition: MeshPartition.h:51
void OutputField(const Array< OneD, const MultiRegions::ExpListSharedPtr > &pFields, int dump=-1)
LibUtilities::FieldMetaDataMap m_fieldMetaData
Definition: FilterSampler.h:89
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
std::vector< std::string > m_variables
Definition: FilterSampler.h:91
NekDouble Evaluate() const
Definition: Equation.h:102
virtual SOLVER_UTILS_EXPORT void v_ProcessSample(const Array< OneD, const MultiRegions::ExpListSharedPtr > &pFields, const NekDouble &time)=0
virtual SOLVER_UTILS_EXPORT void v_Initialise(const Array< OneD, const MultiRegions::ExpListSharedPtr > &pFields, const NekDouble &time)
LibUtilities::FieldIOSharedPtr m_fld
Definition: FilterSampler.h:88
double NekDouble
std::map< std::string, std::string > ParamMap
Definition: Filter.h:67
LibUtilities::SessionReaderSharedPtr m_session
Definition: Filter.h:84
SOLVER_UTILS_EXPORT FilterSampler(const LibUtilities::SessionReaderSharedPtr &pSession, const ParamMap &pParams)
virtual SOLVER_UTILS_EXPORT ~FilterSampler()
virtual SOLVER_UTILS_EXPORT void v_PrepareOutput(const Array< OneD, const MultiRegions::ExpListSharedPtr > &pFields, const NekDouble &time)
Definition: FilterSampler.h:69
virtual SOLVER_UTILS_EXPORT void v_Finalise(const Array< OneD, const MultiRegions::ExpListSharedPtr > &pFields, const NekDouble &time)