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 
39 using namespace Nektar;
40 using namespace Nektar::FieldUtils;
41 
42 // Converts Python's sys.argv into C++'s argv.
43 char **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] = NULL;
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.
73 void 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
88 FieldSharedPtr 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 forceoutput = false,
91  std::string domain = "", bool noequispaced = 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 (forceoutput)
140  {
141  f->m_vm.insert(std::make_pair("forceoutput", 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 (noequispaced)
151  {
152  f->m_vm.insert(std::make_pair("noequispaced", 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("useSessionVariables", po::variable_value()));
183  }
184 
185  if (useSessionExpansion)
186  {
187  f->m_vm.insert(
188  std::make_pair("useSessionExpansion", 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 
200 // Get the i-th Pts field
202 {
203  return f->m_fieldPts->GetPts(i);
204 }
205 
206 // Set the i-th Pts field
207 // TODO: The reference for inarray is unavailable in Python if it's not a const
208 // [work] Array<OneD, NekDouble> inarray
209 // [fail] Array<OneD, NekDouble> &inarray
210 // [work] const Array<OneD, const NekDouble> &inarray
212  const int i,
213  const Array<OneD, const NekDouble> &inarray)
214 {
215  f->m_fieldPts->SetPts(i,inarray);
216 }
217 
218 
220 {
221  py::class_<Field, std::shared_ptr<Field>>("Field", py::no_init)
222  .def("GetPts", &Field_GetPts)
223  .def("SetPts", &Field_SetPts)
224  .def("ClearField", &Field::ClearField)
225  .def("NewPartition", &NewPartition)
226  .def("__init__",
227  py::make_constructor(
228  &Field_Init, py::default_call_policies(),
229  (py::arg("py_argv"), py::arg("nparts") = 0,
230  py::arg("output_points") = 0,
231  py::arg("output_points_hom_z") = 0, py::arg("error") = false,
232  py::arg("forceoutput") = false, py::arg("domain") = "",
233  py::arg("noequispaced") = false, py::arg("npz") = 0,
234  py::arg("onlyshape") = "", py::arg("part_only") = 0,
235  py::arg("part_only_overlapping") = 0,
236  py::arg("useSessionVariables") = false,
237  py::arg("useSessionExpansion") = false,
238  py::arg("verbose") = false)));
239 }
FieldSharedPtr Field_Init(py::list &py_argv, int nparts=0, int output_points=0, int output_points_hom_z=0, bool error=false, bool forceoutput=false, std::string domain="", bool noequispaced=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:201
void export_Field()
Definition: Field.cpp:219
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:211
char ** ConvertCommandLine(py::list &py_argv)
Definition: Field.cpp:43
tBaseSharedPtr CreateInstance(tKey idKey, tParam... args)
Create an instance of the class referred to by idKey.
Definition: NekFactory.hpp:145
static std::shared_ptr< DataType > AllocateSharedPtr(const Args &...args)
Allocate a shared pointer from the memory pool.
def copy(self)
Definition: pycml.py:2663
std::shared_ptr< Field > FieldSharedPtr
Definition: Field.hpp:989
CommFactory & GetCommFactory()
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:1
FIELD_UTILS_EXPORT void ClearField()
Definition: Field.hpp:972