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