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// Converts Python's sys.argv into C++'s argv.
43char **ConvertCommandLine(py::list &py_argv)
44{
45 int i, argc = py::len(py_argv), bufSize = 0;
46 char **argv = new char *[argc + 1], *p;
47
48 for (i = 0; i < argc; ++i)
49 {
50 std::string tmp = py::extract<std::string>(py_argv[i]);
51 bufSize += tmp.size() + 1;
52 }
53
54 std::vector<char> buf(bufSize);
55 for (i = 0, p = &buf[0]; i < argc; ++i)
56 {
57 std::string tmp = py::extract<std::string>(py_argv[i]);
58 std::copy(tmp.begin(), tmp.end(), p);
59 p[tmp.size()] = '\0';
60 argv[i] = p;
61 p += tmp.size() + 1;
62 }
63
64 argv[argc] = nullptr;
65 return argv;
66}
67
68// Called at the start of every loop over nparts.
69// Args: FieldSharedPtr "f",
70// Python's sys.argv stored as a Python list called "py_argv",
71// and the partition number stored as an integer called "part".
72// Function: clears data stored in f, defines f->m_partComm.
73void NewPartition(FieldSharedPtr f, py::list &py_argv, int part)
74{
75 std::cout << std::endl << "Processing partition: " << part << std::endl;
76 f->ClearField();
77 int argc = py::len(py_argv);
78 char **argv = ConvertCommandLine(py_argv);
79 f->m_partComm = std::shared_ptr<FieldConvertComm>(
80 new FieldConvertComm(argc, argv, f->m_nParts, part));
81}
82
83// Wrapper around the Field constructor
84// Args: FieldConvert command line arguments.
85// Function: constructs a FieldSharedPtr "f", defines f->m_defComm if
86// nparts specified, and stores a line arguments in f->m_vm.
87// Returns: f
88FieldSharedPtr Field_Init(py::list &py_argv, int nparts = 0,
89 int output_points = 0, int output_points_hom_z = 0,
90 bool error = false, bool force_output = false,
91 std::string domain = "", bool no_equispaced = false,
92 int npz = 0, std::string onlyshape = "",
93 int part_only = 0, int part_only_overlapping = 0,
94 bool useSessionVariables = false,
95 bool useSessionExpansion = false,
96 bool verbose = false)
97{
98 // Construct shared pointer to a Field object.
99 std::shared_ptr<Field> f = MemoryManager<Field>::AllocateSharedPtr();
100
101 // Get argc and argv from the Python command line.
102 int argc = py::len(py_argv);
103 char **argv = ConvertCommandLine(py_argv);
104
105 // Define the MPI Communicator.
106 f->m_comm =
107 LibUtilities::GetCommFactory().CreateInstance("Serial", argc, argv);
108
109 if (nparts)
110 {
111 f->m_vm.insert(
112 std::make_pair("nparts", po::variable_value(nparts, false)));
113 if (nparts > 1)
114 {
115 f->m_nParts = nparts;
116 f->m_defComm = f->m_comm;
117 }
118 }
119
120 // Populate m_vm.
121 if (output_points)
122 {
123 f->m_vm.insert(std::make_pair(
124 "output-points", po::variable_value(output_points, false)));
125 }
126
127 if (output_points_hom_z)
128 {
129 f->m_vm.insert(
130 std::make_pair("output-points-hom-z",
131 po::variable_value(output_points_hom_z, false)));
132 }
133
134 if (error)
135 {
136 f->m_vm.insert(std::make_pair("error", po::variable_value()));
137 }
138
139 if (force_output)
140 {
141 f->m_vm.insert(std::make_pair("force-output", po::variable_value()));
142 }
143
144 if (domain.size())
145 {
146 f->m_vm.insert(
147 std::make_pair("range", po::variable_value(domain, false)));
148 }
149
150 if (no_equispaced)
151 {
152 f->m_vm.insert(std::make_pair("no-equispaced", po::variable_value()));
153 }
154
155 if (npz)
156 {
157 f->m_vm.insert(std::make_pair("npz", po::variable_value(npz, false)));
158 }
159
160 if (onlyshape.size())
161 {
162 f->m_vm.insert(
163 std::make_pair("onlyshape", po::variable_value(onlyshape, false)));
164 }
165
166 if (part_only)
167 {
168 f->m_vm.insert(
169 std::make_pair("part-only", po::variable_value(part_only, false)));
170 }
171
172 if (part_only_overlapping)
173 {
174 f->m_vm.insert(
175 std::make_pair("part-only-overlapping",
176 po::variable_value(part_only_overlapping, false)));
177 }
178
179 if (useSessionVariables)
180 {
181 f->m_vm.insert(
182 std::make_pair("use_session_variables", po::variable_value()));
183 }
184
185 if (useSessionExpansion)
186 {
187 f->m_vm.insert(
188 std::make_pair("use_session_expansion", po::variable_value()));
189 }
190
191 if (verbose)
192 {
193 f->m_vm.insert(std::make_pair("verbose", po::variable_value()));
194 }
195
196 return f;
197}
198
199// Get the i-th Pts field
201{
202 return f->m_fieldPts->GetPts(i);
203}
204
205// Set the i-th Pts field
206// TODO: The reference for inarray is unavailable in Python if it's not a const
207// [work] Array<OneD, NekDouble> inarray
208// [fail] Array<OneD, NekDouble> &inarray
209// [work] const Array<OneD, const NekDouble> &inarray
210void Field_SetPts(FieldSharedPtr f, const int i,
211 const Array<OneD, const NekDouble> &inarray)
212{
213 f->m_fieldPts->SetPts(i, inarray);
214}
215
217{
218 py::class_<Field, std::shared_ptr<Field>>("Field", py::no_init)
219 .def("GetPts", &Field_GetPts)
220 .def("SetPts", &Field_SetPts)
221 .def("ClearField", &Field::ClearField)
222 .def("NewPartition", &NewPartition)
223 .def("__init__",
224 py::make_constructor(
225 &Field_Init, py::default_call_policies(),
226 (py::arg("py_argv"), py::arg("nparts") = 0,
227 py::arg("output_points") = 0,
228 py::arg("output_points_hom_z") = 0, py::arg("error") = false,
229 py::arg("force_output") = false, py::arg("domain") = "",
230 py::arg("no_equispaced") = false, py::arg("npz") = 0,
231 py::arg("onlyshape") = "", py::arg("part_only") = 0,
232 py::arg("part_only_overlapping") = 0,
233 py::arg("use_session_variables") = false,
234 py::arg("use_session_expansion") = false,
235 py::arg("verbose") = false)));
236}
void export_Field()
Definition: Field.cpp:216
void NewPartition(FieldSharedPtr f, py::list &py_argv, int part)
Definition: Field.cpp:73
void Field_SetPts(FieldSharedPtr f, const int i, const Array< OneD, const NekDouble > &inarray)
Definition: Field.cpp:210
char ** ConvertCommandLine(py::list &py_argv)
Definition: Field.cpp:43
FieldSharedPtr Field_Init(py::list &py_argv, int nparts=0, int output_points=0, int output_points_hom_z=0, bool error=false, bool force_output=false, std::string domain="", 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)
Definition: Field.cpp:88
const Array< OneD, const NekDouble > Field_GetPts(FieldSharedPtr f, const int i)
Definition: Field.cpp:200
tBaseSharedPtr CreateInstance(tKey idKey, tParam... args)
Create an instance of the class referred to by idKey.
Definition: NekFactory.hpp:143
General purpose memory allocation routines with the ability to allocate from thread specific memory p...
def copy(self)
Definition: pycml.py:2663
std::shared_ptr< Field > FieldSharedPtr
Definition: Field.hpp:990
CommFactory & GetCommFactory()