Nektar++
Dummy.cpp
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // File Dummy.cpp
4 //
5 // For more information, please see: http://www.nektar.info
6 //
7 // The MIT License
8 //
9 // Copyright (c) 2017 Kilian Lackhove
10 // Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
11 // Department of Aeronautics, Imperial College London (UK), and Scientific
12 // Computing and Imaging Institute, University of Utah (USA).
13 //
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: Dummy Equation System that only sends/receives fields
33 //
34 ///////////////////////////////////////////////////////////////////////////////
35 
36 #include <iostream>
37 
38 #include <boost/core/ignore_unused.hpp>
39 
42 
43 using namespace std;
44 
45 namespace Nektar
46 {
47 string Dummy::className = GetEquationSystemFactory().RegisterCreatorFunction(
48  "Dummy",
49  Dummy::create,
50  "Dummy Equation System that only sends/receives fields");
51 
52 Dummy::Dummy(const LibUtilities::SessionReaderSharedPtr &pSession,
54  : UnsteadySystem(pSession, pGraph)
55 {
56 }
57 
58 /**
59  * @brief Initialization object for the Dummy class.
60  */
62 {
64 
65  m_nanSteps = 0;
66 
69 
71  m_session, shared_from_this(), m_fields, m_fields.size());
72 
73  if (m_session->DefinesElement("Nektar/Coupling"))
74  {
75  TiXmlElement *vCoupling = m_session->GetElement("Nektar/Coupling");
76 
77  ASSERTL0(vCoupling->Attribute("TYPE"),
78  "Missing TYPE attribute in Coupling");
79  string vType = vCoupling->Attribute("TYPE");
80  ASSERTL0(!vType.empty(),
81  "TYPE attribute must be non-empty in Coupling");
82 
83  m_coupling = GetCouplingFactory().CreateInstance(vType, m_fields[0]);
84 
85  auto sV = m_session->GetVariables();
86  for (auto const &sendVar : m_coupling->GetSendFieldNames())
87  {
88  auto match = find(sV.begin(), sV.end(), sendVar);
89  if (match != sV.end())
90  {
91  int id = distance(sV.begin(), match);
92  m_intVariables.push_back(id);
93  }
94  }
95  }
96 }
97 
98 /**
99  * @brief Destructor for Dummy class.
100  */
102 {
103 }
104 
105 /**
106  * @brief v_PreIntegrate
107  */
108 bool Dummy::v_PreIntegrate(int step)
109 {
110  if (m_coupling)
111  {
112  int numForceFields = 0;
113  for (auto &x : m_forcing)
114  {
115  numForceFields += x->GetForces().size();
116  }
117  vector<string> varNames;
119  numForceFields);
120  for (int i = 0; i < m_fields.size(); ++i)
121  {
122  varNames.push_back(m_session->GetVariable(i));
123  phys[i] = m_fields[i]->UpdatePhys();
124  }
125 
126  int f = 0;
127  for (auto &x : m_forcing)
128  {
129  for (int i = 0; i < x->GetForces().size(); ++i)
130  {
131  phys[m_fields.size() + f + i] = x->GetForces()[i];
132  varNames.push_back("F_" + boost::lexical_cast<string>(f) + "_" +
133  m_session->GetVariable(i));
134  }
135  f++;
136  }
137 
138  m_coupling->Send(step, m_time, phys, varNames);
139  m_coupling->Receive(step, m_time, phys, varNames);
140  }
141 
142  return UnsteadySystem::v_PreIntegrate(step);
143 }
144 
145 /**
146  * @brief v_PostIntegrate
147  */
149 {
150  if (m_coupling && m_coupling->GetSendFieldNames().size() > 0)
151  {
152  LibUtilities::Timer timer1;
153  timer1.Start();
154 
155  auto sV = m_session->GetVariables();
156  for (auto const &sendVar : m_coupling->GetSendFieldNames())
157  {
158  auto match = find(sV.begin(), sV.end(), sendVar);
159  if (match != sV.end())
160  {
161  int id = distance(sV.begin(), match);
162  GetFunction("SendFields", m_fields[id])
163  ->Evaluate(sendVar, m_fields[id]->UpdatePhys(), m_time);
164  }
165  }
166 
167  timer1.Stop();
168  if (m_session->DefinesCmdLineArgument("verbose"))
169  {
170  cout << "Field evaluation time: " << timer1.TimePerTest(1) << endl;
171  }
172  }
173 
174  for (int i = 0; i < m_session->GetVariables().size(); ++i)
175  {
176 
177  m_fields[i]->FwdTrans_IterPerExp(m_fields[i]->UpdatePhys(),
178  m_fields[i]->UpdateCoeffs());
179  m_fields[i]->SetPhysState(false);
180  }
181 
182  return UnsteadySystem::v_PostIntegrate(step);
183 }
184 
186 {
187  if (m_coupling)
188  {
189  m_coupling->Finalize();
190  }
191 
193 
194  int f = 0;
195  for (auto &x : m_forcing)
196  {
197  for (int i = 0; i < x->GetForces().size(); ++i)
198  {
199  int npts = GetTotPoints();
200 
201  NekDouble l2err = 0.0;
202  NekDouble linferr = 0.0;
203  for (int j = 0; j < npts; ++j)
204  {
205  l2err += x->GetForces()[i][j] * x->GetForces()[i][j];
206  linferr = max(linferr, fabs(x->GetForces()[i][j]));
207  }
208 
209  m_comm->AllReduce(l2err, LibUtilities::ReduceSum);
210  m_comm->AllReduce(npts, LibUtilities::ReduceSum);
211  m_comm->AllReduce(linferr, LibUtilities::ReduceMax);
212 
213  l2err /= npts;
214  l2err = sqrt(l2err);
215 
216  if (m_comm->TreatAsRankZero())
217  {
218  cout << "L 2 error (variable "
219  << "F_" + boost::lexical_cast<string>(f) + "_" +
220  m_session->GetVariable(i)
221  << ") : " << l2err << endl;
222 
223  cout << "L inf error (variable "
224  << "F_" + boost::lexical_cast<string>(f) + "_" +
225  m_session->GetVariable(i)
226  << ") : " << linferr << endl;
227  }
228  }
229  f++;
230  }
231 }
232 
233 /**
234  * @brief Compute the right-hand side.
235  */
236 void Dummy::DoOdeRhs(const Array<OneD, const Array<OneD, NekDouble> > &inarray,
237  Array<OneD, Array<OneD, NekDouble> > &outarray,
238  const NekDouble time)
239 {
240  boost::ignore_unused(time);
241 
242  int nVariables = inarray.size();
243  int nq = GetTotPoints();
244 
245  for (int i = 0; i < nVariables; ++i)
246  {
247  Vmath::Zero(nq, outarray[i], 1);
248  }
249 }
250 
251 /**
252  * @brief Compute the projection and call the method for imposing the
253  * boundary conditions in case of discontinuous projection.
254  */
256  const Array<OneD, const Array<OneD, NekDouble> > &inarray,
257  Array<OneD, Array<OneD, NekDouble> > &outarray,
258  const NekDouble time)
259 {
260  boost::ignore_unused(time);
261 
262  int nvariables = inarray.size();
263  int nq = m_fields[0]->GetNpoints();
264 
265  // deep copy
266  for (int i = 0; i < nvariables; ++i)
267  {
268  Vmath::Vcopy(nq, inarray[i], 1, outarray[i], 1);
269  }
270 }
271 
272 } // namespace Nektar
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:216
SolverUtils::CouplingSharedPtr m_coupling
Definition: Dummy.h:70
virtual bool v_PostIntegrate(int step)
v_PostIntegrate
Definition: Dummy.cpp:148
virtual void v_Output()
Definition: Dummy.cpp:185
virtual ~Dummy()
Destructor.
Definition: Dummy.cpp:101
std::vector< SolverUtils::ForcingSharedPtr > m_forcing
Definition: Dummy.h:71
virtual bool v_PreIntegrate(int step)
v_PreIntegrate
Definition: Dummy.cpp:108
virtual void v_InitObject()
Initialization object for the Dummy class.
Definition: Dummy.cpp:61
void DoOdeProjection(const Array< OneD, const Array< OneD, NekDouble > > &inarray, Array< OneD, Array< OneD, NekDouble > > &outarray, const NekDouble time)
Compute the projection and call the method for imposing the boundary conditions in case of discontinu...
Definition: Dummy.cpp:255
void DoOdeRhs(const Array< OneD, const Array< OneD, NekDouble > > &inarray, Array< OneD, Array< OneD, NekDouble > > &outarray, const NekDouble time)
Compute the right-hand side.
Definition: Dummy.cpp:236
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, std::string pDesc="")
Register a class with the factory.
Definition: NekFactory.hpp:200
void DefineProjection(FuncPointerT func, ObjectPointerT obj)
void DefineOdeRhs(FuncPointerT func, ObjectPointerT obj)
NekDouble TimePerTest(unsigned int n)
Returns amount of seconds per iteration in a test with n iterations.
Definition: Timer.cpp:62
LibUtilities::CommSharedPtr m_comm
Communicator.
NekDouble m_time
Current time of simulation.
Array< OneD, MultiRegions::ExpListSharedPtr > m_fields
Array holding all dependent variables.
LibUtilities::SessionReaderSharedPtr m_session
The session reader.
SOLVER_UTILS_EXPORT SessionFunctionSharedPtr GetFunction(std::string name, const MultiRegions::ExpListSharedPtr &field=MultiRegions::NullExpListSharedPtr, bool cache=false)
Get a SessionFunction by name.
SOLVER_UTILS_EXPORT int GetTotPoints()
virtual SOLVER_UTILS_EXPORT void v_Output(void)
static SOLVER_UTILS_EXPORT std::vector< ForcingSharedPtr > Load(const LibUtilities::SessionReaderSharedPtr &pSession, const std::weak_ptr< EquationSystem > &pEquation, const Array< OneD, MultiRegions::ExpListSharedPtr > &pFields, const unsigned int &pNumForcingFields=0)
Definition: Forcing.cpp:128
Base class for unsteady solvers.
LibUtilities::TimeIntegrationSchemeOperators m_ode
The time integration scheme operators to use.
virtual SOLVER_UTILS_EXPORT bool v_PostIntegrate(int step)
virtual SOLVER_UTILS_EXPORT bool v_PreIntegrate(int step)
virtual SOLVER_UTILS_EXPORT void v_InitObject()
Init object for UnsteadySystem class.
std::shared_ptr< SessionReader > SessionReaderSharedPtr
CouplingFactory & GetCouplingFactory()
Declaration of the Coupling factory singleton.
Definition: Coupling.cpp:44
EquationSystemFactory & GetEquationSystemFactory()
std::shared_ptr< MeshGraph > MeshGraphSharedPtr
Definition: MeshGraph.h:174
InputIterator find(InputIterator first, InputIterator last, InputIterator startingpoint, const EqualityComparable &value)
Definition: StdRegions.hpp:362
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:1
double NekDouble
void Zero(int n, T *x, const int incx)
Zero vector.
Definition: Vmath.cpp:436
void Vcopy(int n, const T *x, const int incx, T *y, const int incy)
Definition: Vmath.cpp:1199
scalarT< T > sqrt(scalarT< T > in)
Definition: scalar.hpp:267