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", Dummy::create,
49  "Dummy Equation System that only sends/receives fields");
50 
51 Dummy::Dummy(const LibUtilities::SessionReaderSharedPtr &pSession,
53  : UnsteadySystem(pSession, pGraph)
54 {
55 }
56 
57 /**
58  * @brief Initialization object for the Dummy class.
59  */
60 void Dummy::v_InitObject(bool DeclareFields)
61 {
62  UnsteadySystem::v_InitObject(DeclareFields);
63 
64  m_nanSteps = 0;
65 
68 
69  m_forcing = SolverUtils::Forcing::Load(m_session, shared_from_this(),
70  m_fields, m_fields.size());
71 
72  if (m_session->DefinesElement("Nektar/Coupling"))
73  {
74  TiXmlElement *vCoupling = m_session->GetElement("Nektar/Coupling");
75 
76  ASSERTL0(vCoupling->Attribute("TYPE"),
77  "Missing TYPE attribute in Coupling");
78  string vType = vCoupling->Attribute("TYPE");
79  ASSERTL0(!vType.empty(),
80  "TYPE attribute must be non-empty in Coupling");
81 
82  m_coupling = GetCouplingFactory().CreateInstance(vType, m_fields[0]);
83 
84  auto sV = m_session->GetVariables();
85  for (auto const &sendVar : m_coupling->GetSendFieldNames())
86  {
87  auto match = find(sV.begin(), sV.end(), sendVar);
88  if (match != sV.end())
89  {
90  int id = distance(sV.begin(), match);
91  m_intVariables.push_back(id);
92  }
93  }
94  }
95 }
96 
97 /**
98  * @brief Destructor for Dummy class.
99  */
101 {
102 }
103 
104 /**
105  * @brief v_PreIntegrate
106  */
107 bool Dummy::v_PreIntegrate(int step)
108 {
109  if (m_coupling)
110  {
111  int numForceFields = 0;
112  for (auto &x : m_forcing)
113  {
114  numForceFields += x->GetForces().size();
115  }
116  vector<string> varNames;
118  numForceFields);
119  for (int i = 0; i < m_fields.size(); ++i)
120  {
121  varNames.push_back(m_session->GetVariable(i));
122  phys[i] = m_fields[i]->UpdatePhys();
123  }
124 
125  int f = 0;
126  for (auto &x : m_forcing)
127  {
128  for (int i = 0; i < x->GetForces().size(); ++i)
129  {
130  phys[m_fields.size() + f + i] = x->GetForces()[i];
131  varNames.push_back("F_" + boost::lexical_cast<string>(f) + "_" +
132  m_session->GetVariable(i));
133  }
134  f++;
135  }
136 
137  m_coupling->Send(step, m_time, phys, varNames);
138  m_coupling->Receive(step, m_time, phys, varNames);
139  }
140 
141  return UnsteadySystem::v_PreIntegrate(step);
142 }
143 
144 /**
145  * @brief v_PostIntegrate
146  */
148 {
149  if (m_coupling && m_coupling->GetSendFieldNames().size() > 0)
150  {
151  LibUtilities::Timer timer1;
152  timer1.Start();
153 
154  auto sV = m_session->GetVariables();
155  for (auto const &sendVar : m_coupling->GetSendFieldNames())
156  {
157  auto match = find(sV.begin(), sV.end(), sendVar);
158  if (match != sV.end())
159  {
160  int id = distance(sV.begin(), match);
161  GetFunction("SendFields", m_fields[id])
162  ->Evaluate(sendVar, m_fields[id]->UpdatePhys(), m_time);
163  }
164  }
165 
166  timer1.Stop();
167  if (m_session->DefinesCmdLineArgument("verbose"))
168  {
169  cout << "Field evaluation time: " << timer1.TimePerTest(1) << endl;
170  }
171  }
172 
173  for (int i = 0; i < m_session->GetVariables().size(); ++i)
174  {
175 
176  m_fields[i]->FwdTransLocalElmt(m_fields[i]->UpdatePhys(),
177  m_fields[i]->UpdateCoeffs());
178  m_fields[i]->SetPhysState(false);
179  }
180 
181  return UnsteadySystem::v_PostIntegrate(step);
182 }
183 
185 {
186  if (m_coupling)
187  {
188  m_coupling->Finalize();
189  }
190 
192 
193  int f = 0;
194  for (auto &x : m_forcing)
195  {
196  for (int i = 0; i < x->GetForces().size(); ++i)
197  {
198  int npts = GetTotPoints();
199 
200  NekDouble l2err = 0.0;
201  NekDouble linferr = 0.0;
202  for (int j = 0; j < npts; ++j)
203  {
204  l2err += x->GetForces()[i][j] * x->GetForces()[i][j];
205  linferr = max(linferr, fabs(x->GetForces()[i][j]));
206  }
207 
208  m_comm->AllReduce(l2err, LibUtilities::ReduceSum);
209  m_comm->AllReduce(npts, LibUtilities::ReduceSum);
210  m_comm->AllReduce(linferr, LibUtilities::ReduceMax);
211 
212  l2err /= npts;
213  l2err = sqrt(l2err);
214 
215  if (m_comm->TreatAsRankZero())
216  {
217  cout << "L 2 error (variable "
218  << "F_" + boost::lexical_cast<string>(f) + "_" +
219  m_session->GetVariable(i)
220  << ") : " << l2err << endl;
221 
222  cout << "L inf error (variable "
223  << "F_" + boost::lexical_cast<string>(f) + "_" +
224  m_session->GetVariable(i)
225  << ") : " << linferr << endl;
226  }
227  }
228  f++;
229  }
230 }
231 
232 /**
233  * @brief Compute the right-hand side.
234  */
235 void Dummy::DoOdeRhs(const Array<OneD, const Array<OneD, NekDouble>> &inarray,
236  Array<OneD, Array<OneD, NekDouble>> &outarray,
237  const NekDouble time)
238 {
239  boost::ignore_unused(time);
240 
241  int nVariables = inarray.size();
242  int nq = GetTotPoints();
243 
244  for (int i = 0; i < nVariables; ++i)
245  {
246  Vmath::Zero(nq, outarray[i], 1);
247  }
248 }
249 
250 /**
251  * @brief Compute the projection and call the method for imposing the
252  * boundary conditions in case of discontinuous projection.
253  */
255  const Array<OneD, const Array<OneD, NekDouble>> &inarray,
256  Array<OneD, Array<OneD, NekDouble>> &outarray, const NekDouble time)
257 {
258  boost::ignore_unused(time);
259 
260  int nvariables = inarray.size();
261  int nq = m_fields[0]->GetNpoints();
262 
263  // deep copy
264  for (int i = 0; i < nvariables; ++i)
265  {
266  Vmath::Vcopy(nq, inarray[i], 1, outarray[i], 1);
267  }
268 }
269 
270 } // namespace Nektar
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:215
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:254
virtual bool v_PreIntegrate(int step) override
v_PreIntegrate
Definition: Dummy.cpp:107
SolverUtils::CouplingSharedPtr m_coupling
Definition: Dummy.h:70
virtual void v_Output() override
Definition: Dummy.cpp:184
virtual ~Dummy()
Destructor.
Definition: Dummy.cpp:100
virtual bool v_PostIntegrate(int step) override
v_PostIntegrate
Definition: Dummy.cpp:147
std::vector< SolverUtils::ForcingSharedPtr > m_forcing
Definition: Dummy.h:71
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:235
virtual void v_InitObject(bool DeclareFields=true) override
Initialization object for the Dummy class.
Definition: Dummy.cpp:60
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, std::string pDesc="")
Register a class with the factory.
Definition: NekFactory.hpp:198
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:68
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:120
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(bool DeclareField=true) override
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:172
InputIterator find(InputIterator first, InputIterator last, InputIterator startingpoint, const EqualityComparable &value)
Definition: StdRegions.hpp:444
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:2
double NekDouble
void Zero(int n, T *x, const int incx)
Zero vector.
Definition: Vmath.cpp:492
void Vcopy(int n, const T *x, const int incx, T *y, const int incy)
Definition: Vmath.cpp:1255
scalarT< T > sqrt(scalarT< T > in)
Definition: scalar.hpp:294