Nektar++
Field.cpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: Field.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 Field class.
32//
33///////////////////////////////////////////////////////////////////////////////
34
35#include <FieldUtils/Field.hpp>
38
39using namespace Nektar;
40using namespace Nektar::FieldUtils;
41
42// Called at the start of every loop over nparts.
43// Args: FieldSharedPtr "f",
44// Python's sys.argv stored as a Python list called "py_argv",
45// and the partition number stored as an integer called "part".
46// Function: clears data stored in f, defines f->m_partComm.
47void NewPartition(FieldSharedPtr f, py::list &py_argv, int part)
48{
49 std::cout << std::endl << "Processing partition: " << part << std::endl;
50 f->ClearField();
51
52 CppCommandLine cpp(py_argv);
53
54 f->m_partComm = std::shared_ptr<FieldConvertComm>(
55 new FieldConvertComm(cpp.GetArgc(), cpp.GetArgv(), f->m_nParts, part));
56}
57
58// Wrapper around the Field constructor
59// Args: FieldConvert command line arguments.
60// Function: constructs a FieldSharedPtr "f", defines f->m_defComm if
61// nparts specified, and stores a line arguments in f->m_vm.
62// Returns: f
63FieldSharedPtr Field_Init(py::list &argv, int nparts = 0, int output_points = 0,
64 int output_points_hom_z = 0, bool error = false,
65 bool force_output = false, bool no_equispaced = false,
66 int npz = 0, std::string onlyshape = "",
67 int part_only = 0, int part_only_overlapping = 0,
68 bool useSessionVariables = false,
69 bool useSessionExpansion = false,
70 bool verbose = false, std::string domain = "")
71{
72 // Construct shared pointer to a Field object.
73 std::shared_ptr<Field> f = MemoryManager<Field>::AllocateSharedPtr();
74
75 if (py::len(argv) > 0)
76 {
77 // Get argc and argv from the Python command line.
78 CppCommandLine cpp(argv);
79
80 // Define the MPI Communicator.
82 "Serial", cpp.GetArgc(), cpp.GetArgv());
83
84 if (nparts)
85 {
86 f->m_vm.insert(
87 std::make_pair("nparts", po::variable_value(nparts, false)));
88 if (nparts > 1)
89 {
90 f->m_nParts = nparts;
91 f->m_defComm = f->m_comm;
92 }
93 }
94 }
95
96 // Populate m_vm.
97 if (output_points)
98 {
99 f->m_vm.insert(std::make_pair(
100 "output-points", po::variable_value(output_points, false)));
101 }
102
103 if (output_points_hom_z)
104 {
105 f->m_vm.insert(
106 std::make_pair("output-points-hom-z",
107 po::variable_value(output_points_hom_z, false)));
108 }
109
110 if (error)
111 {
112 f->m_vm.insert(std::make_pair("error", po::variable_value()));
113 }
114
115 if (force_output)
116 {
117 f->m_vm.insert(std::make_pair("force-output", po::variable_value()));
118 }
119
120 if (domain.size())
121 {
122 NEKERROR(ErrorUtil::efatal,
123 "The doamin option in field is deprecated. Please use "
124 "the xml option range=\"xmax,xmin,ymax,ymin\", \n\t i.e."
125 "InputModule.Create(\"xml\", field, \"myfile.xml\", "
126 "range=\"-1,1,-1,1\").Run()");
127 }
128
129 if (no_equispaced)
130 {
131 f->m_vm.insert(std::make_pair("no-equispaced", po::variable_value()));
132 }
133
134 if (npz)
135 {
136 f->m_vm.insert(std::make_pair("npz", po::variable_value(npz, false)));
137 }
138
139 if (onlyshape.size())
140 {
141 f->m_vm.insert(
142 std::make_pair("onlyshape", po::variable_value(onlyshape, false)));
143 }
144
145 if (part_only)
146 {
147 f->m_vm.insert(
148 std::make_pair("part-only", po::variable_value(part_only, false)));
149 }
150
151 if (part_only_overlapping)
152 {
153 f->m_vm.insert(
154 std::make_pair("part-only-overlapping",
155 po::variable_value(part_only_overlapping, false)));
156 }
157
158 if (useSessionVariables)
159 {
160 f->m_vm.insert(
161 std::make_pair("use_session_variables", po::variable_value()));
162 }
163
164 if (useSessionExpansion)
165 {
166 f->m_vm.insert(
167 std::make_pair("use_session_expansion", po::variable_value()));
168 }
169
170 if (verbose)
171 {
172 f->m_vm.insert(std::make_pair("verbose", po::variable_value()));
173 }
174
175 return f;
176}
177
178// Get the i-th Pts field
180{
181 return f->m_fieldPts->GetPts(i);
182}
183
184// Set the i-th Pts field
185// TODO: The reference for inarray is unavailable in Python if it's not a const
186// [work] Array<OneD, NekDouble> inarray
187// [fail] Array<OneD, NekDouble> &inarray
188// [work] const Array<OneD, const NekDouble> &inarray
189void Field_SetPts(FieldSharedPtr f, const int i,
190 const Array<OneD, const NekDouble> &inarray)
191{
192 f->m_fieldPts->SetPts(i, inarray);
193}
194
196 py::list &pyExpList)
197{
199 Array<OneD, MultiRegions::ExpListSharedPtr> expLists(py::len(pyExpList));
200
201 for (int i = 0; i < expLists.size(); ++i)
202 {
203 if (!py::extract<MultiRegions::ExpListSharedPtr>(pyExpList[i]).check())
204 {
205 throw NekError("List should contain only ExpList objects.");
206 }
207
208 expLists[i] = py::extract<MultiRegions::ExpListSharedPtr>(pyExpList[i]);
209 }
210
211 return expLists;
212}
213
214void Field_SetupFromExpList(FieldSharedPtr f, py::list &explists)
215{
217 PyListToOneDArray(explists);
218 f->SetupFromExpList(exp);
219}
220
222{
223 py::class_<Field, std::shared_ptr<Field>>("Field", py::no_init)
224 .def("__init__",
225 py::make_constructor(
226 &Field_Init, py::default_call_policies(),
227 (py::arg("argv") = py::list(), py::arg("nparts") = 0,
228 py::arg("output_points") = 0,
229 py::arg("output_points_hom_z") = 0, py::arg("error") = false,
230 py::arg("force_output") = false,
231 py::arg("no_equispaced") = false, py::arg("npz") = 0,
232 py::arg("onlyshape") = "", py::arg("part_only") = 0,
233 py::arg("part_only_overlapping") = 0,
234 py::arg("use_session_variables") = false,
235 py::arg("use_session_expansion") = false,
236 py::arg("verbose") = false, py::arg("domain") = "")))
237
238 .def("GetPts", &Field_GetPts)
239 .def("SetPts", &Field_SetPts)
240 .def("ClearField", &Field::ClearField)
241 .def("NewPartition", &NewPartition)
242 .def("ReadFieldDefs", &Field::ReadFieldDefs)
243
244 .def("SetupFromExpList", Field_SetupFromExpList)
245
246 .def_readwrite("graph", &Field::m_graph)
247 .def_readwrite("session", &Field::m_session)
248 .def_readwrite("verbose", &Field::m_verbose)
249 .def_readwrite("comm", &Field::m_comm);
250}
#define NEKERROR(type, msg)
Assert Level 0 – Fundamental assert which is used whether in FULLDEBUG, DEBUG or OPT compilation mode...
Definition: ErrorUtil.hpp:202
void Field_SetupFromExpList(FieldSharedPtr f, py::list &explists)
Definition: Field.cpp:214
void export_Field()
Definition: Field.cpp:221
void NewPartition(FieldSharedPtr f, py::list &py_argv, int part)
Definition: Field.cpp:47
void Field_SetPts(FieldSharedPtr f, const int i, const Array< OneD, const NekDouble > &inarray)
Definition: Field.cpp:189
const Array< OneD, const NekDouble > Field_GetPts(FieldSharedPtr f, const int i)
Definition: Field.cpp:179
Array< OneD, MultiRegions::ExpListSharedPtr > PyListToOneDArray(py::list &pyExpList)
Definition: Field.cpp:195
FieldSharedPtr Field_Init(py::list &argv, int nparts=0, int output_points=0, int output_points_hom_z=0, bool error=false, bool force_output=false, bool no_equispaced=false, int npz=0, std::string onlyshape="", int part_only=0, int part_only_overlapping=0, bool useSessionVariables=false, bool useSessionExpansion=false, bool verbose=false, std::string domain="")
Definition: Field.cpp:63
Nektar::ErrorUtil::NekError NekError
tBaseSharedPtr CreateInstance(tKey idKey, tParam... args)
Create an instance of the class referred to by idKey.
General purpose memory allocation routines with the ability to allocate from thread specific memory p...
std::shared_ptr< Field > FieldSharedPtr
Definition: Field.hpp:1026
CommFactory & GetCommFactory()
Helper structure to construct C++ command line argc and argv variables from a Python list.
int GetArgc()
Returns the constructed argc.
char ** GetArgv()
Returns the constructed argv.