Nektar++
Loading...
Searching...
No Matches
ForcingLorentz.cpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: ForcingLorentz.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: Add the Lorentz force, j x B
32///////////////////////////////////////////////////////////////////////////////
33
39
40namespace Nektar::SolverUtils
41{
42
45 "ForcingLorentz", ForcingLorentz::create, "Quasi-static MHD forcing");
46
47/**
48 * @brief
49 * @param pSession
50 * @param pEquation
51 */
54 const std::weak_ptr<EquationSystem> &pEquation)
55 : Forcing(pSession, pEquation)
56{
57}
58
62
63/**
64 * @brief Initialise the forcing module
65 * @param pFields
66 * @param pNumForcingFields
67 * @param pForce
68 */
71 [[maybe_unused]] const unsigned int &pNumForcingFields,
72 const LibUtilities::SessionNode &pForce)
73{
74 // read space dimention
75 bool isH1d, isH2d;
76 m_session->MatchSolverInfo("Homogeneous", "1D", isH1d, false);
77 m_session->MatchSolverInfo("Homogeneous", "2D", isH2d, false);
78 int expdim = isH2d ? 1 : pFields[0]->GetGraph()->GetMeshDimension();
79 m_spacedim = expdim + (isH1d ? 1 : 0) + (isH2d ? 2 : 0);
80 auto equ = m_equ.lock();
81 ASSERTL0(equ, "Weak pointer to the equation system is expired");
82 m_FluidEq = std::dynamic_pointer_cast<VelocityCorrectionScheme>(equ);
83 // read electric conductivity
84 m_session->LoadParameter("ElectricConductivity", m_sigma, -1.);
85 ASSERTL0(m_sigma > 0,
86 "ElectricConductivity should be defined and be positive.");
87 // Read the constant external electric and magnetic fields.
90 std::vector<std::string> elecVars = {"Ex", "Ey", "Ez"};
91 std::vector<std::string> magVars = {"Bx", "By", "Bz"};
92 ASSERTL0(pForce.DefinesChild("ELECTRICMAGNETICFIELDS"),
93 "Requires ELECTRICMAGNETICFIELDS tag specifying the function "
94 "that prescribes the external electric and magnetic fields.");
95 std::string electromagneticFields =
96 pForce.GetValue("ELECTRICMAGNETICFIELDS");
97 ASSERTL0(m_session->DefinesFunction(electromagneticFields),
98 "Function '" + electromagneticFields +
99 "' is not defined in the session.");
100 for (size_t i = 0; i < 3; ++i)
101 {
102 if (m_session->DefinesFunction(electromagneticFields, elecVars[i]))
103 {
104 LibUtilities::EquationSharedPtr fieldEquation =
105 m_session->GetFunction(electromagneticFields, elecVars[i]);
106 m_E0[i] = fieldEquation->Evaluate(0., 0., 0., 0.);
107 }
108 if (m_session->DefinesFunction(electromagneticFields, magVars[i]))
109 {
110 LibUtilities::EquationSharedPtr fieldEquation =
111 m_session->GetFunction(electromagneticFields, magVars[i]);
112 m_B0[i] = fieldEquation->Evaluate(0., 0., 0., 0.);
113 }
114 }
115 m_damping =
116 m_sigma * (m_B0[0] * m_B0[0] + m_B0[1] * m_B0[1] + m_B0[2] * m_B0[2]);
117
118 m_implicitDamping = false;
119 if (pForce.DefinesChild("IMPLICITDAMPING"))
120 {
121 const std::string &text = pForce.GetValue("IMPLICITDAMPING");
122 ASSERTL0(StringUtils::IEquals(text, "true") ||
123 StringUtils::IEquals(text, "false"),
124 "IMPLICITDAMPING must be either True or False.");
126 }
128 {
129 m_FluidEq->SetImplicitLorentzDamping(m_damping);
130 }
131
132 // Keep all three components of u x B and J, including the out-of-plane
133 // component required by a two-dimensional flow with an in-plane field.
135 for (size_t i = 0; i < 3; ++i)
136 {
137 m_Efield[i] = Array<OneD, NekDouble>(pFields[0]->GetTotPoints(), 0.);
138 }
139}
140
141/**
142 * @brief Adds the body force, -Omega X u.
143 * @param fields
144 * @param inarray
145 * @param outarray
146 * @param time
147 */
150 [[maybe_unused]] const Array<OneD, Array<OneD, NekDouble>> &inarray,
152 [[maybe_unused]] const NekDouble &time)
153{
154 size_t physTot = fields[0]->GetTotPoints();
155 if (m_spacedim == 2)
156 {
157 Vmath::Smul(physTot, m_B0[2], inarray[1], 1, m_Efield[0], 1);
158 Vmath::Smul(physTot, -m_B0[2], inarray[0], 1, m_Efield[1], 1);
159 Vmath::Svtsvtp(physTot, m_B0[1], inarray[0], 1, -m_B0[0], inarray[1], 1,
160 m_Efield[2], 1);
161 }
162 else if (m_spacedim == 3)
163 {
164 Vmath::Svtsvtp(physTot, m_B0[2], inarray[1], 1, -m_B0[1], inarray[2], 1,
165 m_Efield[0], 1);
166 Vmath::Svtsvtp(physTot, -m_B0[2], inarray[0], 1, m_B0[0], inarray[2], 1,
167 m_Efield[1], 1);
168 Vmath::Svtsvtp(physTot, m_B0[1], inarray[0], 1, -m_B0[0], inarray[1], 1,
169 m_Efield[2], 1);
170 }
171 for (size_t i = 0; i < 3; ++i)
172 {
173 Vmath::Sadd(physTot, m_E0[i], m_Efield[i], 1, m_Efield[i], 1);
174 }
175 m_FluidEq->SolveEfield(m_Efield, m_Efield);
176 NekDouble SB0[3];
177 SB0[0] = m_sigma * m_B0[0];
178 SB0[1] = m_sigma * m_B0[1];
179 SB0[2] = m_sigma * m_B0[2];
180 if (m_spacedim == 2)
181 {
182 Vmath::Svtvp(physTot, SB0[2], m_Efield[1], 1, outarray[0], 1,
183 outarray[0], 1);
184 Vmath::Svtvp(physTot, -SB0[1], m_Efield[2], 1, outarray[0], 1,
185 outarray[0], 1);
186 Vmath::Svtvp(physTot, -SB0[2], m_Efield[0], 1, outarray[1], 1,
187 outarray[1], 1);
188 Vmath::Svtvp(physTot, SB0[0], m_Efield[2], 1, outarray[1], 1,
189 outarray[1], 1);
190 }
191 else if (m_spacedim == 3)
192 {
193 Vmath::Svtvp(physTot, SB0[2], m_Efield[1], 1, outarray[0], 1,
194 outarray[0], 1);
195 Vmath::Svtvp(physTot, -SB0[1], m_Efield[2], 1, outarray[0], 1,
196 outarray[0], 1);
197 Vmath::Svtvp(physTot, -SB0[2], m_Efield[0], 1, outarray[1], 1,
198 outarray[1], 1);
199 Vmath::Svtvp(physTot, SB0[0], m_Efield[2], 1, outarray[1], 1,
200 outarray[1], 1);
201 Vmath::Svtvp(physTot, SB0[1], m_Efield[0], 1, outarray[2], 1,
202 outarray[2], 1);
203 Vmath::Svtvp(physTot, -SB0[0], m_Efield[1], 1, outarray[2], 1,
204 outarray[2], 1);
205 }
206
208 {
209 for (int i = 0; i < m_spacedim; ++i)
210 {
211 // Remove the -sigma |B|^2 u contribution from (u x B) x B.
212 // This term is instead included in the velocity Helmholtz solve.
213 Vmath::Svtvp(physTot, m_damping, inarray[i], 1, outarray[i], 1,
214 outarray[i], 1);
215 }
216 }
217}
218
219} // namespace Nektar::SolverUtils
#define ASSERTL0(condition, msg)
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, std::string pDesc="")
Register a class with the factory.
A single node of a format-neutral session configuration tree.
const std::string & GetValue(const std::string &name) const
Returns the text of the first child called name.
bool DefinesChild(const std::string &name) const
Returns true if this node has at least one child called name.
Defines a forcing term to be explicitly applied.
Definition Forcing.h:72
const std::weak_ptr< EquationSystem > m_equ
Weak pointer to equation system using this forcing.
Definition Forcing.h:127
LibUtilities::SessionReaderSharedPtr m_session
Session reader.
Definition Forcing.h:125
Array< OneD, Array< OneD, NekDouble > > m_Efield
ForcingLorentz(const LibUtilities::SessionReaderSharedPtr &pSession, const std::weak_ptr< EquationSystem > &pEquation)
VelocityCorrectionSchemeSharedPtr m_FluidEq
static std::string classNameBody
Name of the class.
static ForcingSharedPtr create(const LibUtilities::SessionReaderSharedPtr &pSession, const std::weak_ptr< EquationSystem > &pEquation, const Array< OneD, MultiRegions::ExpListSharedPtr > &pFields, const unsigned int &pNumForcingFields, const LibUtilities::SessionNode &pForce)
Creates an instance of this class.
Array< OneD, NekDouble > m_B0
Array< OneD, NekDouble > m_E0
void v_InitObject(const Array< OneD, MultiRegions::ExpListSharedPtr > &pFields, const unsigned int &pNumForcingFields, const LibUtilities::SessionNode &pForce) override
Initialise the forcing module.
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) override
Adds the body force, -Omega X u.
std::shared_ptr< SessionReader > SessionReaderSharedPtr
std::shared_ptr< Equation > EquationSharedPtr
Definition Equation.h:135
ForcingFactory & GetForcingFactory()
Declaration of the forcing factory singleton.
Definition Forcing.cpp:42
bool IEquals(std::string_view a, std::string_view b)
void Svtsvtp(int n, const T alpha, const T *x, int incx, const T beta, const T *y, int incy, T *z, int incz)
Svtsvtp (scalar times vector plus scalar times vector):
Definition Vmath.hpp:475
void Svtvp(int n, const T alpha, const T *x, const int incx, const T *y, const int incy, T *z, const int incz)
Svtvp (scalar times vector plus vector): z = alpha*x + y.
Definition Vmath.hpp:398
void Smul(int n, const T alpha, const T *x, const int incx, T *y, const int incy)
Scalar multiply y = alpha*x.
Definition Vmath.hpp:102
void Sadd(int n, const T alpha, const T *x, const int incx, T *y, const int incy)
Add vector y = alpha + x.
Definition Vmath.hpp:196