Nektar++
ProcessGrad.cpp
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////////////
2//
3// File: ProcessGrad.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 gradient of fields.
32//
33////////////////////////////////////////////////////////////////////////////////
34
35#include <iostream>
36#include <string>
37using namespace std;
38
42
43#include "ProcessGrad.h"
44#include "ProcessMapping.h"
45
46namespace Nektar::FieldUtils
47{
48
51 "Computes gradient of fields.");
52
54{
55 m_config["vars"] = ConfigOption(false, "NotSet", "Select variables");
56 m_config["dirs"] = ConfigOption(false, "NotSet", "Select directions");
57}
58
60{
61}
62
63void ProcessGrad::ParserOptions(std::set<int> &variables,
64 std::set<int> &directions)
65{
66 if (m_config["vars"].as<string>().compare("NotSet"))
67 {
68 ParseUtils::GenerateVariableSet(m_config["vars"].as<string>(),
69 m_f->m_variables, variables);
70 }
71 else
72 {
73 for (int v = 0; v < m_f->m_variables.size(); ++v)
74 {
75 variables.insert(v);
76 }
77 }
78 vector<string> coords = {"x", "y", "z"};
79 int spacedim = m_f->m_numHomogeneousDir + m_f->m_graph->GetMeshDimension();
80 coords.resize(spacedim);
81 if (m_config["dirs"].as<string>().compare("NotSet"))
82 {
83 ParseUtils::GenerateVariableSet(m_config["dirs"].as<string>(), coords,
84 directions);
85 }
86 else
87 {
88 for (int d = 0; d < spacedim; ++d)
89 {
90 directions.insert(d);
91 }
92 }
93}
94
96{
97 // Calculate Gradient
98 int n = 0;
99 for (int i : m_selectedVars)
100 {
101 for (int j : m_directions)
102 {
103 m_f->m_exp[i]->PhysDeriv(MultiRegions::DirCartesianMap[j],
104 m_f->m_exp[i]->GetPhys(), grad[n]);
105 ++n;
106 }
107 }
108}
109
111{
112 int expdim = m_f->m_graph->GetMeshDimension();
113 int spacedim = m_f->m_numHomogeneousDir + expdim;
114 int nfields = m_f->m_variables.size();
115 bool hasvel = m_selectedVars.size() && (*m_selectedVars.begin() < spacedim);
116
117 int npoints = m_f->m_exp[0]->GetNpoints();
118
120 for (int i = 0; i < spacedim; i++)
121 {
122 tmp[i] = Array<OneD, NekDouble>(npoints);
123 }
124
125 // Get mapping
127
128 // Get velocity and convert to Cartesian system,
129 // if it is still in transformed system
131 if (hasvel && m_f->m_fieldMetaDataMap.count("MappingCartesianVel"))
132 {
133 if (m_f->m_fieldMetaDataMap["MappingCartesianVel"] == "False")
134 {
135 // Initialize arrays and copy velocity
136 for (int i = 0; i < spacedim; ++i)
137 {
138 vel[i] = Array<OneD, NekDouble>(npoints);
139 if (m_f->m_exp[0]->GetWaveSpace())
140 {
141 m_f->m_exp[0]->HomogeneousBwdTrans(
142 npoints, m_f->m_exp[i]->GetPhys(), vel[i]);
143 }
144 else
145 {
146 Vmath::Vcopy(npoints, m_f->m_exp[i]->GetPhys(), 1, vel[i],
147 1);
148 }
149 }
150 // Convert velocity to cartesian system
151 mapping->ContravarToCartesian(vel, vel);
152 // Convert back to wavespace if necessary
153 if (m_f->m_exp[0]->GetWaveSpace())
154 {
155 for (int i = 0; i < spacedim; ++i)
156 {
157 m_f->m_exp[0]->HomogeneousFwdTrans(npoints, vel[i], vel[i]);
158 }
159 }
160 }
161 else
162 {
163 for (int i = 0; i < spacedim; ++i)
164 {
165 vel[i] = Array<OneD, NekDouble>(npoints);
166 Vmath::Vcopy(npoints, m_f->m_exp[i]->GetPhys(), 1, vel[i], 1);
167 }
168 }
169 }
170 else if (hasvel)
171 {
172 for (int i = 0; i < spacedim && i < nfields; ++i)
173 {
174 vel[i] = Array<OneD, NekDouble>(npoints);
175 Vmath::Vcopy(npoints, m_f->m_exp[i]->GetPhys(), 1, vel[i], 1);
176 }
177 }
178
179 // Calculate Gradient
180 int n = 0;
181 for (int i : m_selectedVars)
182 {
183 for (int j = 0; j < spacedim; ++j)
184 {
185 if (i < spacedim)
186 {
187 m_f->m_exp[i]->PhysDeriv(MultiRegions::DirCartesianMap[j],
188 vel[i], tmp[j]);
189 }
190 else
191 {
192 m_f->m_exp[i]->PhysDeriv(MultiRegions::DirCartesianMap[j],
193 m_f->m_exp[i]->GetPhys(), tmp[j]);
194 }
195 }
196 mapping->CovarToCartesian(tmp, tmp);
197 for (int j : m_directions)
198 {
199 Vmath::Vcopy(npoints, tmp[j], 1, grad[n], 1);
200 ++n;
201 }
202 }
203}
204
205void ProcessGrad::v_Process(po::variables_map &vm)
206{
207 m_f->SetUpExp(vm);
209
210 int nfields = m_f->m_variables.size();
211 int addfields = m_selectedVars.size() * m_directions.size();
212 m_f->m_exp.resize(nfields + addfields);
213
214 vector<string> coords = {"x", "y", "z"};
215 for (int i : m_selectedVars)
216 {
217 for (int j : m_directions)
218 {
219 m_f->m_variables.push_back(m_f->m_variables[i] + "_" + coords[j]);
220 }
221 }
222
223 // Skip in case of empty partition
224 if (m_f->m_exp[0]->GetNumElmts() == 0)
225 {
226 return;
227 }
228
229 int npoints = m_f->m_exp[0]->GetNpoints();
230 Array<OneD, Array<OneD, NekDouble>> grad(addfields);
231 for (int i = 0; i < addfields; ++i)
232 {
233 grad[i] = Array<OneD, NekDouble>(npoints);
234 }
235
236 if (m_f->m_fieldMetaDataMap.count("MappingType"))
237 {
238 ProcessMappingFld(grad);
239 }
240 else
241 {
243 }
244
245 for (int i = 0; i < addfields; ++i)
246 {
247 m_f->m_exp[nfields + i] = m_f->AppendExpList(m_f->m_numHomogeneousDir);
248
249 Vmath::Vcopy(npoints, grad[i], 1, m_f->m_exp[nfields + i]->UpdatePhys(),
250 1);
251 m_f->m_exp[nfields + i]->FwdTransLocalElmt(
252 grad[i], m_f->m_exp[nfields + i]->UpdateCoeffs());
253 }
254}
255} // namespace Nektar::FieldUtils
FieldSharedPtr m_f
Field object.
Definition: Module.h:239
std::map< std::string, ConfigOption > m_config
List of configuration values.
Definition: Module.h:272
void ProcessMappingFld(Array< OneD, Array< OneD, NekDouble > > &grad)
std::set< int > m_selectedVars
Definition: ProcessGrad.h:83
static std::shared_ptr< Module > create(FieldSharedPtr f)
Creates an instance of this class.
Definition: ProcessGrad.h:51
void ParserOptions(std::set< int > &variables, std::set< int > &directions)
Definition: ProcessGrad.cpp:63
void ProcessCartesianFld(Array< OneD, Array< OneD, NekDouble > > &grad)
Definition: ProcessGrad.cpp:95
void v_Process(po::variables_map &vm) override
Write mesh to output file.
static GlobalMapping::MappingSharedPtr GetMapping(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.
Definition: NekFactory.hpp:197
static bool GenerateVariableSet(const std::string &str, const std::vector< std::string > &variables, std::set< int > &out)
Generate a set of variable locations.
Definition: ParseUtils.cpp:168
std::shared_ptr< Field > FieldSharedPtr
Definition: Field.hpp:990
std::pair< ModuleType, std::string > ModuleKey
Definition: Module.h:180
ModuleFactory & GetModuleFactory()
Definition: Module.cpp:47
GLOBAL_MAPPING_EXPORT typedef std::shared_ptr< Mapping > MappingSharedPtr
A shared pointer to a Mapping object.
Definition: Mapping.h:51
MultiRegions::Direction const DirCartesianMap[]
Definition: ExpList.h:86
std::vector< double > d(NPUPPER *NPUPPER)
void Vcopy(int n, const T *x, const int incx, T *y, const int incy)
Definition: Vmath.hpp:825
Represents a command-line configuration option.
Definition: Module.h:129