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
41{
42namespace SolverUtils
43{
45{
46 static ForcingFactory instance;
47 return instance;
48}
49
51 const std::weak_ptr<EquationSystem> &pEquation)
52 : m_session(pSession), m_equ(pEquation)
53{
54}
55
58 const unsigned int &pNumForcingFields, const TiXmlElement *pForce)
59{
60 v_InitObject(pFields, pNumForcingFields, pForce);
61}
62
63/**
64 * @param fields Expansion lists corresponding to input arrays
65 * @param inarray u^n from previous timestep
66 * @param outarray output array to append forcing to
67 */
69 const Array<OneD, Array<OneD, NekDouble>> &inarray,
71 const NekDouble &time)
72{
73 v_Apply(fields, inarray, outarray, time);
74}
75
78 const Array<OneD, Array<OneD, NekDouble>> &inarray,
79 Array<OneD, Array<OneD, NekDouble>> &outarray, const NekDouble &time)
80{
81 v_PreApply(fields, inarray, outarray, time);
82}
83
86 const Array<OneD, Array<OneD, NekDouble>> &inarray,
87 Array<OneD, Array<OneD, NekDouble>> &outarray, const NekDouble &time)
88{
89 boost::ignore_unused(fields, time);
90 if (&inarray != &outarray)
91 {
92 int nvar = std::min(inarray.size(), outarray.size());
93 for (int i = 0; i < nvar; ++i)
94 {
95 if (&inarray[i] != &outarray[i])
96 {
97 int np = std::min(inarray[i].size(), outarray[i].size());
98 Vmath::Vcopy(np, inarray[i], 1, outarray[i], 1);
99 }
100 }
101 }
102}
103
104/**
105 * @param fields Expansion lists corresponding to input arrays
106 * @param inarray u^n from previous timestep
107 * @param outarray output array to append forcing to
108 */
111 const Array<OneD, Array<OneD, NekDouble>> &inarray,
112 Array<OneD, Array<OneD, NekDouble>> &outarray, const NekDouble &time)
113{
114 v_ApplyCoeff(fields, inarray, outarray, time);
115}
116
117/**
118 *
119 */
120vector<ForcingSharedPtr> Forcing::Load(
122 const std::weak_ptr<EquationSystem> &pEquation,
124 const unsigned int &pNumForcingFields)
125{
126 vector<ForcingSharedPtr> vForceList;
127
128 if (!pSession->DefinesElement("Nektar/Forcing"))
129 {
130 return vForceList;
131 }
132
133 TiXmlElement *vForcing = pSession->GetElement("Nektar/Forcing");
134 if (vForcing)
135 {
136 unsigned int vNumForcingFields = pNumForcingFields;
137 if (!pNumForcingFields)
138 {
139 vNumForcingFields = pFields.size();
140 }
141
142 TiXmlElement *vForce = vForcing->FirstChildElement("FORCE");
143 while (vForce)
144 {
145 string vType = vForce->Attribute("TYPE");
146
147 TiXmlElement *vForceParam = vForce;
149 vForceParam, pSession->GetTimeLevel(), false);
150 vForceList.push_back(GetForcingFactory().CreateInstance(
151 vType, pSession, pEquation, pFields, vNumForcingFields,
152 vForceParam));
153
154 vForce = vForce->NextSiblingElement("FORCE");
155 }
156 }
157 return vForceList;
158}
159
161{
162 return m_Forcing;
163}
164
166{
167 return m_Forcing;
168}
169
171 LibUtilities::SessionReaderSharedPtr pSession, std::string pFieldName,
172 Array<OneD, NekDouble> &pArray, const std::string &pFunctionName,
173 NekDouble pTime)
174{
175 ASSERTL0(pSession->DefinesFunction(pFunctionName),
176 "Function '" + pFunctionName + "' does not exist.");
177
179 pSession->GetFunction(pFunctionName, pFieldName);
180
181 EvaluateTimeFunction(pTime, ffunc, pArray);
182}
183
187{
188 // dummy array of zero pts.
189 Array<OneD, NekDouble> x0(pArray.size(), 0.0);
190
191 pEqn->Evaluate(x0, x0, x0, pTime, pArray);
192}
193
196 const LibUtilities::SessionReaderSharedPtr &pSession, std::string pName,
197 bool pCache)
198{
199 if (pCache)
200 {
201 if ((m_sessionFunctions.find(pName) == m_sessionFunctions.end()) ||
202 (m_sessionFunctions[pName]->GetSession() != pSession) ||
203 (m_sessionFunctions[pName]->GetExpansion() != pFields[0]))
204 {
205 m_sessionFunctions[pName] =
207 pSession, pFields[0], pName, pCache);
208 }
209
210 return m_sessionFunctions[pName];
211 }
212 else
213 {
215 new SessionFunction(pSession, pFields[0], pName, pCache));
216 }
217}
218
221 const Array<OneD, Array<OneD, NekDouble>> &inarray,
222 Array<OneD, Array<OneD, NekDouble>> &outarray, const NekDouble &time)
223{
224 boost::ignore_unused(fields, inarray, outarray, time);
225 ASSERTL0(false, "v_ApplyCoeff not defined");
226}
227} // namespace SolverUtils
228} // namespace Nektar
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:215
Provides a generic Factory class.
Definition: NekFactory.hpp:105
static void GetXMLElementTimeLevel(TiXmlElement *&element, const size_t timeLevel, const bool disableCheck=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:84
Array< OneD, Array< OneD, NekDouble > > m_Forcing
Evaluated forcing function.
Definition: Forcing.h:121
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:68
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
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:56
SOLVER_UTILS_EXPORT Array< OneD, Array< OneD, NekDouble > > & UpdateForces()
Definition: Forcing.cpp:160
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:109
SOLVER_UTILS_EXPORT const Array< OneD, const Array< OneD, NekDouble > > & GetForces()
Definition: Forcing.cpp:165
std::map< std::string, SolverUtils::SessionFunctionSharedPtr > m_sessionFunctions
Map of known SessionFunctions.
Definition: Forcing.h:126
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:170
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:76
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:194
SOLVER_UTILS_EXPORT Forcing(const LibUtilities::SessionReaderSharedPtr &pSession, const std::weak_ptr< EquationSystem > &pEquation)
Constructor.
Definition: Forcing.cpp:50
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:219
std::shared_ptr< SessionReader > SessionReaderSharedPtr
std::shared_ptr< Equation > EquationSharedPtr
Definition: Equation.h:129
ForcingFactory & GetForcingFactory()
Declaration of the forcing factory singleton.
Definition: Forcing.cpp:44
std::shared_ptr< SessionFunction > SessionFunctionSharedPtr
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:2
double NekDouble
void Vcopy(int n, const T *x, const int incx, T *y, const int incy)
Definition: Vmath.cpp:1191