Nektar++
Forcing.cpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: Forcing.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// Permission is hereby granted, free of charge, to any person obtaining a
14// copy of this software and associated documentation files (the "Software"),
15// to deal in the Software without restriction, including without limitation
16// the rights to use, copy, modify, merge, publish, distribute, sublicense,
17// and/or sell copies of the Software, and to permit persons to whom the
18// Software is furnished to do so, subject to the following conditions:
19//
20// The above copyright notice and this permission notice shall be included
21// in all copies or substantial portions of the Software.
22//
23// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29// DEALINGS IN THE SOFTWARE.
30//
31// Description: Abstract base class for forcing terms.
32//
33///////////////////////////////////////////////////////////////////////////////
34
37
38using namespace std;
39
40namespace Nektar::SolverUtils
41{
43{
44 static ForcingFactory instance;
45 return instance;
46}
47
49 const std::weak_ptr<EquationSystem> &pEquation)
50 : m_session(pSession), m_equ(pEquation)
51{
52}
53
56 const unsigned int &pNumForcingFields, const TiXmlElement *pForce)
57{
58 v_InitObject(pFields, pNumForcingFields, pForce);
59}
60
61/**
62 * @param fields Expansion lists corresponding to input arrays
63 * @param inarray u^n from previous timestep
64 * @param outarray output array to append forcing to
65 */
67 const Array<OneD, Array<OneD, NekDouble>> &inarray,
69 const NekDouble &time)
70{
71 v_Apply(fields, inarray, outarray, time);
72}
73
76 const Array<OneD, Array<OneD, NekDouble>> &inarray,
77 Array<OneD, Array<OneD, NekDouble>> &outarray, const NekDouble &time)
78{
79 v_PreApply(fields, inarray, outarray, time);
80}
81
83 [[maybe_unused]] const Array<OneD, MultiRegions::ExpListSharedPtr> &fields,
84 const Array<OneD, Array<OneD, NekDouble>> &inarray,
86 [[maybe_unused]] const NekDouble &time)
87{
88 if (&inarray != &outarray)
89 {
90 int nvar = std::min(inarray.size(), outarray.size());
91 for (int i = 0; i < nvar; ++i)
92 {
93 if (&inarray[i] != &outarray[i])
94 {
95 int np = std::min(inarray[i].size(), outarray[i].size());
96 Vmath::Vcopy(np, inarray[i], 1, outarray[i], 1);
97 }
98 }
99 }
100}
101
102/**
103 * @param fields Expansion lists corresponding to input arrays
104 * @param inarray u^n from previous timestep
105 * @param outarray output array to append forcing to
106 */
109 const Array<OneD, Array<OneD, NekDouble>> &inarray,
110 Array<OneD, Array<OneD, NekDouble>> &outarray, const NekDouble &time)
111{
112 v_ApplyCoeff(fields, inarray, outarray, time);
113}
114
115/**
116 *
117 */
118vector<ForcingSharedPtr> Forcing::Load(
120 const std::weak_ptr<EquationSystem> &pEquation,
122 const unsigned int &pNumForcingFields)
123{
124 vector<ForcingSharedPtr> vForceList;
125
126 if (!pSession->DefinesElement("Nektar/Forcing"))
127 {
128 return vForceList;
129 }
130
131 TiXmlElement *vForcing = pSession->GetElement("Nektar/Forcing");
132 if (vForcing)
133 {
134 unsigned int vNumForcingFields = pNumForcingFields;
135 if (!pNumForcingFields)
136 {
137 vNumForcingFields = pFields.size();
138 }
139
140 TiXmlElement *vForce = vForcing->FirstChildElement("FORCE");
141 while (vForce)
142 {
143 string vType = vForce->Attribute("TYPE");
144
145 TiXmlElement *vForceParam = vForce;
147 vForceParam, pSession->GetTimeLevel(), false);
148 vForceList.push_back(GetForcingFactory().CreateInstance(
149 vType, pSession, pEquation, pFields, vNumForcingFields,
150 vForceParam));
151
152 vForce = vForce->NextSiblingElement("FORCE");
153 }
154 }
155 return vForceList;
156}
157
159{
160 return m_Forcing;
161}
162
164{
165 return m_Forcing;
166}
167
169 LibUtilities::SessionReaderSharedPtr pSession, std::string pFieldName,
170 Array<OneD, NekDouble> &pArray, const std::string &pFunctionName,
171 NekDouble pTime)
172{
173 ASSERTL0(pSession->DefinesFunction(pFunctionName),
174 "Function '" + pFunctionName + "' does not exist.");
175
177 pSession->GetFunction(pFunctionName, pFieldName);
178
179 EvaluateTimeFunction(pTime, ffunc, pArray);
180}
181
185{
186 // dummy array of zero pts.
187 Array<OneD, NekDouble> x0(pArray.size(), 0.0);
188
189 pEqn->Evaluate(x0, x0, x0, pTime, pArray);
190}
191
194 const LibUtilities::SessionReaderSharedPtr &pSession, std::string pName,
195 bool pCache)
196{
197 if (pCache)
198 {
199 if ((m_sessionFunctions.find(pName) == m_sessionFunctions.end()) ||
200 (m_sessionFunctions[pName]->GetSession() != pSession) ||
201 (m_sessionFunctions[pName]->GetExpansion() != pFields[0]))
202 {
203 m_sessionFunctions[pName] =
205 pSession, pFields[0], pName, pCache);
206 }
207
208 return m_sessionFunctions[pName];
209 }
210 else
211 {
213 new SessionFunction(pSession, pFields[0], pName, pCache));
214 }
215}
216
218 [[maybe_unused]] const Array<OneD, MultiRegions::ExpListSharedPtr> &fields,
219 [[maybe_unused]] const Array<OneD, Array<OneD, NekDouble>> &inarray,
220 [[maybe_unused]] Array<OneD, Array<OneD, NekDouble>> &outarray,
221 [[maybe_unused]] const NekDouble &time)
222{
223 ASSERTL0(false, "v_ApplyCoeff not defined");
224}
225} // namespace Nektar::SolverUtils
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:208
Provides a generic Factory class.
static void GetXMLElementTimeLevel(TiXmlElement *&element, const size_t timeLevel, const bool enableCheck=true)
Get XML elment time level (Parallel-in-Time)
static std::shared_ptr< DataType > AllocateSharedPtr(const Args &...args)
Allocate a shared pointer from the memory pool.
virtual SOLVER_UTILS_EXPORT void v_Apply(const Array< OneD, MultiRegions::ExpListSharedPtr > &fields, const Array< OneD, Array< OneD, NekDouble > > &inarray, Array< OneD, Array< OneD, NekDouble > > &outarray, const NekDouble &time)=0
virtual SOLVER_UTILS_EXPORT void v_PreApply(const Array< OneD, MultiRegions::ExpListSharedPtr > &fields, const Array< OneD, Array< OneD, NekDouble > > &inarray, Array< OneD, Array< OneD, NekDouble > > &outarray, const NekDouble &time)
Definition: Forcing.cpp:82
Array< OneD, Array< OneD, NekDouble > > m_Forcing
Evaluated forcing function.
Definition: Forcing.h:119
virtual SOLVER_UTILS_EXPORT void v_InitObject(const Array< OneD, MultiRegions::ExpListSharedPtr > &pFields, const unsigned int &pNumForcingFields, const TiXmlElement *pForce)=0
SOLVER_UTILS_EXPORT void Apply(const Array< OneD, MultiRegions::ExpListSharedPtr > &fields, const Array< OneD, Array< OneD, NekDouble > > &inarray, Array< OneD, Array< OneD, NekDouble > > &outarray, const NekDouble &time)
Apply the forcing.
Definition: Forcing.cpp:66
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
SOLVER_UTILS_EXPORT void InitObject(const Array< OneD, MultiRegions::ExpListSharedPtr > &pFields, const unsigned int &pNumForcingFields, const TiXmlElement *pForce)
Initialise the forcing object.
Definition: Forcing.cpp:54
SOLVER_UTILS_EXPORT Array< OneD, Array< OneD, NekDouble > > & UpdateForces()
Definition: Forcing.cpp:158
SOLVER_UTILS_EXPORT void ApplyCoeff(const Array< OneD, MultiRegions::ExpListSharedPtr > &fields, const Array< OneD, Array< OneD, NekDouble > > &inarray, Array< OneD, Array< OneD, NekDouble > > &outarray, const NekDouble &time)
Apply the forcing.
Definition: Forcing.cpp:107
SOLVER_UTILS_EXPORT const Array< OneD, const Array< OneD, NekDouble > > & GetForces()
Definition: Forcing.cpp:163
std::map< std::string, SolverUtils::SessionFunctionSharedPtr > m_sessionFunctions
Map of known SessionFunctions.
Definition: Forcing.h:124
SOLVER_UTILS_EXPORT void EvaluateTimeFunction(LibUtilities::SessionReaderSharedPtr pSession, std::string pFieldName, Array< OneD, NekDouble > &pArray, const std::string &pFunctionName, NekDouble pTime=NekDouble(0))
Definition: Forcing.cpp:168
SOLVER_UTILS_EXPORT void PreApply(const Array< OneD, MultiRegions::ExpListSharedPtr > &fields, const Array< OneD, Array< OneD, NekDouble > > &inarray, Array< OneD, Array< OneD, NekDouble > > &outarray, const NekDouble &time)
Change the advection velocity before applying the forcing. For example, subtracting the frame velocit...
Definition: Forcing.cpp:74
SOLVER_UTILS_EXPORT SessionFunctionSharedPtr GetFunction(const Array< OneD, MultiRegions::ExpListSharedPtr > &pFields, const LibUtilities::SessionReaderSharedPtr &pSession, std::string pName, bool pCache=false)
Get a SessionFunction by name.
Definition: Forcing.cpp:192
SOLVER_UTILS_EXPORT Forcing(const LibUtilities::SessionReaderSharedPtr &pSession, const std::weak_ptr< EquationSystem > &pEquation)
Constructor.
Definition: Forcing.cpp:48
virtual SOLVER_UTILS_EXPORT void v_ApplyCoeff(const Array< OneD, MultiRegions::ExpListSharedPtr > &fields, const Array< OneD, Array< OneD, NekDouble > > &inarray, Array< OneD, Array< OneD, NekDouble > > &outarray, const NekDouble &time)
Definition: Forcing.cpp:217
std::shared_ptr< SessionReader > SessionReaderSharedPtr
std::shared_ptr< Equation > EquationSharedPtr
Definition: Equation.h:125
ForcingFactory & GetForcingFactory()
Declaration of the forcing factory singleton.
Definition: Forcing.cpp:42
std::shared_ptr< SessionFunction > SessionFunctionSharedPtr
double NekDouble
void Vcopy(int n, const T *x, const int incx, T *y, const int incy)
Definition: Vmath.hpp:825
STL namespace.