Nektar++
Loading...
Searching...
No Matches
ProcessMHDCurrentDensity.cpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: ProcessMHDCurrentDensity.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: Computes quasi-static MHD electric current density.
32//
33///////////////////////////////////////////////////////////////////////////////
34
35#include <array>
36#include <string>
37
39
41
43
44namespace Nektar::FieldUtils
45{
46
49 ModuleKey(eProcessModule, "MHDCurrentDensity"),
51 "Computes quasi-static MHD electric current density.");
52
54 : ProcessModule(f)
55{
56 m_config["function"] = ConfigOption(
57 false, "NotSet",
58 "Override the electric/magnetic field function name (optional)");
59}
60
64
65int ProcessMHDCurrentDensity::FindField(const std::string &name) const
66{
67 for (int i = 0; i < m_f->m_variables.size(); ++i)
68 {
69 if (StringUtils::IEquals(m_f->m_variables[i], name))
70 {
71 return i;
72 }
73 }
74 return -1;
75}
76
78{
79 if (!StringUtils::IEquals(m_config.at("function").as<std::string>(),
80 "NotSet"))
81 {
82 return m_config.at("function").as<std::string>();
83 }
84
85 if (m_f->m_session->DefinesElement("Nektar/Forcing"))
86 {
87 TiXmlElement *forcing = m_f->m_session->GetElement("Nektar/Forcing");
88 TiXmlElement *force = forcing->FirstChildElement("FORCE");
89 while (force)
90 {
91 const char *type = force->Attribute("TYPE");
92 if (type && StringUtils::IEquals(type, "ForcingLorentz"))
93 {
94 TiXmlElement *fields =
95 force->FirstChildElement("ELECTRICMAGNETICFIELDS");
96 ASSERTL0(fields && fields->GetText(),
97 "ForcingLorentz requires an ELECTRICMAGNETICFIELDS "
98 "function for MHDCurrentDensity.");
99 return fields->GetText();
100 }
101 force = force->NextSiblingElement("FORCE");
102 }
103 }
104
105 ASSERTL0(m_f->m_session->DefinesFunction("ElectricMagneticFields"),
106 "MHDCurrentDensity could not find a ForcingLorentz "
107 "ELECTRICMAGNETICFIELDS function. Specify one with "
108 "function=<name>.");
109 return "ElectricMagneticFields";
110}
111
112void ProcessMHDCurrentDensity::v_Process(po::variables_map &vm)
113{
114 m_f->SetUpExp(vm);
115
116 int spacedim = m_f->m_graph->GetMeshDimension() + m_f->m_numHomogeneousDir;
117 ASSERTL0(spacedim == 2 || spacedim == 3,
118 "MHDCurrentDensity only supports two- and three-dimensional "
119 "fields.");
120
121 std::array<int, 3> velocity = {FindField("u"), FindField("v"),
122 FindField("w")};
123 int potential = FindField("phi");
124 ASSERTL0(velocity[0] >= 0 && velocity[1] >= 0,
125 "MHDCurrentDensity requires velocity fields u and v.");
126 ASSERTL0(spacedim == 2 || velocity[2] >= 0,
127 "MHDCurrentDensity requires velocity field w in 3D.");
128 ASSERTL0(potential >= 0,
129 "MHDCurrentDensity requires electric potential field phi.");
130
131 NekDouble sigma = -1.0;
132 m_f->m_session->LoadParameter("ElectricConductivity", sigma, -1.0);
133 ASSERTL0(sigma > 0.0,
134 "ElectricConductivity must be defined and positive for "
135 "MHDCurrentDensity.");
136
137 std::string functionName = GetElectromagneticFieldFunction();
138 ASSERTL0(m_f->m_session->DefinesFunction(functionName),
139 "Function '" + functionName + "' is not defined in the session.");
140
141 std::array<NekDouble, 3> electric = {0.0, 0.0, 0.0};
142 std::array<NekDouble, 3> magnetic = {0.0, 0.0, 0.0};
143 const std::array<std::string, 3> electricVars = {"Ex", "Ey", "Ez"};
144 const std::array<std::string, 3> magneticVars = {"Bx", "By", "Bz"};
145 for (int i = 0; i < 3; ++i)
146 {
147 if (m_f->m_session->DefinesFunction(functionName, electricVars[i]))
148 {
149 electric[i] =
150 m_f->m_session->GetFunction(functionName, electricVars[i])
151 ->Evaluate(0.0, 0.0, 0.0, 0.0);
152 }
153 if (m_f->m_session->DefinesFunction(functionName, magneticVars[i]))
154 {
155 magnetic[i] =
156 m_f->m_session->GetFunction(functionName, magneticVars[i])
157 ->Evaluate(0.0, 0.0, 0.0, 0.0);
158 }
159 }
160
161 int nfields = m_f->m_variables.size();
162 m_f->m_variables.insert(m_f->m_variables.end(), {"Jx", "Jy", "Jz"});
163 m_f->m_exp.resize(nfields + 3);
164
165 // An empty partition still needs the variable list above, but has no
166 // expansions on which to calculate the current.
167 if (m_f->m_exp[0]->GetNumElmts() == 0)
168 {
169 return;
170 }
171
172 int npoints = m_f->m_exp[0]->GetNpoints();
173 for (int i = 0; i < spacedim; ++i)
174 {
175 ASSERTL0(m_f->m_exp[velocity[i]]->GetNpoints() == npoints,
176 "MHDCurrentDensity requires velocity fields on the same "
177 "points.");
178 }
179 ASSERTL0(m_f->m_exp[potential]->GetNpoints() == npoints,
180 "MHDCurrentDensity requires phi on the same points as the "
181 "velocity fields.");
182
185 for (int i = 0; i < 3; ++i)
186 {
187 gradPhi[i] = Array<OneD, NekDouble>(npoints, 0.0);
188 current[i] = Array<OneD, NekDouble>(npoints, 0.0);
189 }
190 for (int i = 0; i < spacedim; ++i)
191 {
192 m_f->m_exp[potential]->PhysDeriv(MultiRegions::DirCartesianMap[i],
193 m_f->m_exp[potential]->GetPhys(),
194 gradPhi[i]);
195 }
196
197 const Array<OneD, const NekDouble> u = m_f->m_exp[velocity[0]]->GetPhys();
198 const Array<OneD, const NekDouble> v = m_f->m_exp[velocity[1]]->GetPhys();
199 Array<OneD, NekDouble> zero(npoints, 0.0);
201 if (spacedim == 3)
202 {
203 w = m_f->m_exp[velocity[2]]->GetPhys();
204 }
205
206 for (int i = 0; i < npoints; ++i)
207 {
208 current[0][i] = sigma * (electric[0] + v[i] * magnetic[2] -
209 w[i] * magnetic[1] - gradPhi[0][i]);
210 current[1][i] = sigma * (electric[1] + w[i] * magnetic[0] -
211 u[i] * magnetic[2] - gradPhi[1][i]);
212 current[2][i] = sigma * (electric[2] + u[i] * magnetic[1] -
213 v[i] * magnetic[0] - gradPhi[2][i]);
214 }
215
216 for (int i = 0; i < 3; ++i)
217 {
218 m_f->m_exp[nfields + i] = m_f->AppendExpList(m_f->m_numHomogeneousDir);
219 Vmath::Vcopy(npoints, current[i], 1,
220 m_f->m_exp[nfields + i]->UpdatePhys(), 1);
221 m_f->m_exp[nfields + i]->FwdTransLocalElmt(
222 current[i], m_f->m_exp[nfields + i]->UpdateCoeffs());
223 }
224}
225
226} // namespace Nektar::FieldUtils
#define ASSERTL0(condition, msg)
FieldSharedPtr m_f
Field object.
Definition Module.h:239
std::map< std::string, ConfigOption > m_config
List of configuration values.
Definition Module.h:272
static std::shared_ptr< Module > create(FieldSharedPtr f)
Abstract base class for processing modules.
Definition Module.h:301
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, std::string pDesc="")
Register a class with the factory.
std::shared_ptr< Field > FieldSharedPtr
std::pair< ModuleType, std::string > ModuleKey
Definition Module.h:180
ModuleFactory & GetModuleFactory()
Definition Module.cpp:48
MultiRegions::Direction const DirCartesianMap[]
Definition ExpList.h:95
bool IEquals(std::string_view a, std::string_view b)
void Vcopy(int n, const T *x, const int incx, T *y, const int incy)
Definition Vmath.hpp:828
Represents a command-line configuration option.
Definition Module.h:129