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