Nektar++
Python/ExpList.cpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: ExpList.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: Python wrapper for ExpList.
32//
33///////////////////////////////////////////////////////////////////////////////
34
37
40
41#include <fstream>
42#include <sstream>
43#include <string>
44
45using namespace Nektar;
46using namespace Nektar::StdRegions;
47using namespace Nektar::LocalRegions;
48using namespace Nektar::SpatialDomains;
49using namespace Nektar::MultiRegions;
50
52{
53 return exp->GetExp(i);
54}
55
56void ExpList_WriteVTK(ExpListSharedPtr exp, std::string filename)
57{
58 std::ofstream out(filename.c_str());
59 exp->WriteVtkHeader(out);
60 size_t nExp = exp->GetExpSize();
61 for (size_t i = 0; i < nExp; ++i)
62 {
63 exp->WriteVtkPieceHeader(out, i);
64 exp->WriteVtkPieceFooter(out, i);
65 }
66 exp->WriteVtkFooter(out);
67}
68
71{
72 Array<OneD, NekDouble> out(exp->GetNcoeffs());
73 exp->FwdTrans(in, out);
74 return out;
75}
76
79{
80 Array<OneD, NekDouble> out(exp->GetNpoints());
81 exp->BwdTrans(in, out);
82 return out;
83}
84
87{
88 Array<OneD, NekDouble> out(exp->GetNcoeffs());
89 exp->IProductWRTBase(in, out);
90 return out;
91}
92
95{
96 Array<OneD, NekDouble> out(exp->GetNcoeffs(), 0.0);
97 exp->MultiplyByInvMassMatrix(in, out);
98 return out;
99}
100
103 const py::object constFactorMap,
104 const py::object varCoeffMap)
105{
106 Array<OneD, NekDouble> out(exp->GetNcoeffs(), 0.0);
107
110
111 if (!constFactorMap.is_none())
112 {
113 facMap = py::extract<StdRegions::ConstFactorMap>(constFactorMap);
114 }
115 if (!varCoeffMap.is_none())
116 {
117 coeffMap = py::extract<StdRegions::VarCoeffMap>(varCoeffMap);
118 }
119
120 exp->HelmSolve(in, out, facMap, coeffMap);
121 return out;
122}
123
126{
127 return exp->L2(in);
128}
129
133{
134 return exp->L2(in, err);
135}
136
139{
140 return exp->Linf(in);
141}
142
146{
147 return exp->Linf(in, err);
148}
149
151{
152 size_t nPhys = exp->GetNpoints();
153 size_t coordim = exp->GetCoordim(0);
154
155 std::vector<Array<OneD, NekDouble>> coords(coordim);
156 for (size_t i = 0; i < coordim; ++i)
157 {
158 coords[i] = Array<OneD, NekDouble>(nPhys);
159 }
160
161 switch (coordim)
162 {
163 case 1:
164 exp->GetCoords(coords[0]);
165 return py::make_tuple(coords[0]);
166 break;
167 case 2:
168 exp->GetCoords(coords[0], coords[1]);
169 return py::make_tuple(coords[0], coords[1]);
170 break;
171 case 3:
172 exp->GetCoords(coords[0], coords[1], coords[2]);
173 return py::make_tuple(coords[0], coords[1], coords[2]);
174 break;
175 }
176
177 return py::tuple();
178}
179
181{
182 exp->SetPhysArray(inarray);
183}
184
186 const Array<OneD, const NekDouble> &inarray)
187{
188 exp->SetPhys(inarray);
189}
190
192{
193 return exp->GetPhys();
194}
195
198{
199 exp->SetCoeffsArray(inarray);
200}
201
203{
204 return exp->GetCoeffs();
205}
206
208{
209 return exp->Integral();
210}
211
213{
214 std::stringstream ss;
215 ss << static_cast<const void *>(&(exp->GetPhys()[0]));
216 return ss.str();
217}
218
220{
221 exp->ClearGlobalLinSysManager();
223 LocalRegions::MatrixKey::opLess>::ClearManager();
225 LocalRegions::MatrixKey::opLess>::ClearManager();
226}
227
228void ExpList_LoadField(ExpListSharedPtr exp, std::string filename,
229 std::string varName)
230{
231 size_t nExp = exp->GetExpSize();
232 Array<OneD, int> elementGIDs(nExp);
233
234 // Construct a map from element locations to global IDs
235 for (size_t i = 0; i < nExp; ++i)
236 {
237 elementGIDs[i] = exp->GetExp(i)->GetGeom()->GetGlobalID();
238 }
239
240 std::vector<LibUtilities::FieldDefinitionsSharedPtr> def;
241 std::vector<std::vector<NekDouble>> data;
243 LibUtilities::FieldIO::CreateForFile(exp->GetSession(), filename);
244 fldIO->Import(filename, def, data, LibUtilities::NullFieldMetaDataMap,
245 elementGIDs);
246
247 int idx = -1;
248
249 Vmath::Zero(exp->GetNcoeffs(), exp->UpdateCoeffs(), 1);
250
251 // Loop over all the expansions
252 for (size_t i = 0; i < def.size(); ++i)
253 {
254 // Find the index of the required field in the expansion segment
255 for (size_t j = 0; j < def[i]->m_fields.size(); ++j)
256 {
257 if (def[i]->m_fields[j] == varName)
258 {
259 idx = j;
260 }
261 }
262
263 if (idx >= 0)
264 {
265 exp->ExtractDataToCoeffs(def[i], data[i], def[i]->m_fields[idx],
266 exp->UpdateCoeffs());
267 }
268 else
269 {
270 std::cout << "Field " + varName + " not found." << std::endl;
271 }
272 }
273
274 exp->BwdTrans(exp->GetCoeffs(), exp->UpdatePhys());
275}
276
278{
279 int (ExpList::*GetNcoeffs)() const = &ExpList::GetNcoeffs;
280
281 py::class_<ExpList, std::shared_ptr<ExpList>, boost::noncopyable>(
282 "ExpList", py::init<const LibUtilities::SessionReaderSharedPtr &,
284
285 // Query points and offset information
286 .def("GetExp", &ExpList_GetExp)
287 .def("GetExpSize", &ExpList::GetExpSize)
288 .def("GetNpoints", &ExpList::GetNpoints)
289 .def("GetNcoeffs", GetNcoeffs)
290 .def("GetCoords", &ExpList_GetCoords)
291 .def("GetPhys_Offset", &ExpList::GetPhys_Offset)
292 .def("GetCoeff_Offset", &ExpList::GetCoeff_Offset)
293
294 // Evaluations
295 .def("PhysEvaluate", &ExpList::PhysEvaluate)
296 .def("LoadField", &ExpList_LoadField)
297
298 // Operators
299 .def("FwdTrans", &ExpList_FwdTrans)
300 .def("BwdTrans", &ExpList_BwdTrans)
301 .def("IProductWRTBase", &ExpList_IProductWRTBase)
302 .def("MultiplyByInvMassMatrix", &ExpList_MultiplyByInvMassMatrix)
303 .def("HelmSolve", &ExpList_HelmSolve,
304 (py::arg("in"), py::arg("constFactorMap") = py::object(),
305 py::arg("varCoeffMap") = py::object()))
306
307 // Error norms
308 .def("L2", &ExpList_L2)
309 .def("L2", &ExpList_L2_Error)
310 .def("Linf", &ExpList_Linf)
311 .def("Linf", &ExpList_Linf_Error)
312
313 // Storage setups
314 .def("SetPhysArray", &ExpList_SetPhysArray)
315 .def("SetPhys", &ExpList_SetPhys)
316 .def("GetPhys", &ExpList_GetPhys)
317 .def("SetCoeffsArray", &ExpList_SetCoeffsArray)
318 .def("GetCoeffs", &ExpList_GetCoeffs)
319 .def("SetPhysState", &ExpList::SetPhysState)
320 .def("GetPhysState", &ExpList::GetPhysState)
321 .def("Integral", &ExpList_Integral)
322 .def("GetPhysAddress", &ExpList_GetPhysAddress)
323
324 .add_property("phys", &ExpList_GetPhys, &ExpList_SetPhys)
325 .add_property("coeffs", &ExpList_GetCoeffs, &ExpList_SetCoeffsArray)
326
327 // Misc functions
328 .def("WriteVTK", &ExpList_WriteVTK)
329 .def("ResetManagers", &ExpList_ResetManagers);
330
331 // Export Array<OneD, ExpListSharedPtr>
332 export_SharedArray<std::shared_ptr<ExpList>>();
333}
const Array< OneD, const NekDouble > ExpList_GetCoeffs(ExpListSharedPtr exp)
NekDouble ExpList_Linf_Error(ExpListSharedPtr exp, const Array< OneD, const NekDouble > &in, const Array< OneD, const NekDouble > &err)
Array< OneD, NekDouble > ExpList_MultiplyByInvMassMatrix(ExpListSharedPtr exp, const Array< OneD, const NekDouble > &in)
void ExpList_ResetManagers(ExpListSharedPtr exp)
NekDouble ExpList_Linf(ExpListSharedPtr exp, const Array< OneD, const NekDouble > &in)
const Array< OneD, const NekDouble > ExpList_GetPhys(ExpListSharedPtr exp)
Array< OneD, NekDouble > ExpList_HelmSolve(ExpListSharedPtr exp, const Array< OneD, const NekDouble > &in, const py::object constFactorMap, const py::object varCoeffMap)
NekDouble ExpList_Integral(ExpListSharedPtr exp)
void export_ExpList()
Array< OneD, NekDouble > ExpList_IProductWRTBase(ExpListSharedPtr exp, const Array< OneD, const NekDouble > &in)
ExpansionSharedPtr ExpList_GetExp(ExpListSharedPtr exp, int i)
void ExpList_SetPhysArray(ExpListSharedPtr exp, Array< OneD, NekDouble > inarray)
Array< OneD, NekDouble > ExpList_BwdTrans(ExpListSharedPtr exp, const Array< OneD, const NekDouble > &in)
void ExpList_LoadField(ExpListSharedPtr exp, std::string filename, std::string varName)
void ExpList_WriteVTK(ExpListSharedPtr exp, std::string filename)
void ExpList_SetPhys(ExpListSharedPtr exp, const Array< OneD, const NekDouble > &inarray)
py::tuple ExpList_GetCoords(ExpListSharedPtr exp)
void ExpList_SetCoeffsArray(ExpListSharedPtr exp, Array< OneD, NekDouble > inarray)
NekDouble ExpList_L2(ExpListSharedPtr exp, const Array< OneD, const NekDouble > &in)
std::string ExpList_GetPhysAddress(ExpListSharedPtr exp)
Array< OneD, NekDouble > ExpList_FwdTrans(ExpListSharedPtr exp, const Array< OneD, const NekDouble > &in)
NekDouble ExpList_L2_Error(ExpListSharedPtr exp, const Array< OneD, const NekDouble > &in, const Array< OneD, const NekDouble > &err)
Base class for all multi-elemental spectral/hp expansions.
Definition: ExpList.h:99
std::shared_ptr< FieldIO > FieldIOSharedPtr
Definition: FieldIO.h:322
std::shared_ptr< SessionReader > SessionReaderSharedPtr
static FieldMetaDataMap NullFieldMetaDataMap
Definition: FieldIO.h:51
std::shared_ptr< Expansion > ExpansionSharedPtr
Definition: Expansion.h:66
std::shared_ptr< ExpList > ExpListSharedPtr
Shared pointer to an ExpList object.
std::shared_ptr< MeshGraph > MeshGraphSharedPtr
Definition: MeshGraph.h:174
std::map< ConstFactorType, NekDouble > ConstFactorMap
Definition: StdRegions.hpp:430
static ConstFactorMap NullConstFactorMap
Definition: StdRegions.hpp:431
static VarCoeffMap NullVarCoeffMap
Definition: StdRegions.hpp:376
std::map< StdRegions::VarCoeffType, VarCoeffEntry > VarCoeffMap
Definition: StdRegions.hpp:375
NekMatrix< NekMatrix< NekMatrix< NekDouble, StandardMatrixTag >, ScaledMatrixTag >, BlockMatrixTag > DNekScalBlkMat
Definition: NekTypeDefs.hpp:68
NekMatrix< NekMatrix< NekDouble, StandardMatrixTag >, ScaledMatrixTag > DNekScalMat
double NekDouble
void Zero(int n, T *x, const int incx)
Zero vector.
Definition: Vmath.hpp:273
Used to lookup the create function in NekManager.
Definition: MatrixKey.h:69