Nektar++
Python/BasicUtils/SessionReader.cpp
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // File: SessionReader.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 SessionReader.
32 //
33 ///////////////////////////////////////////////////////////////////////////////
34 
37 
38 using namespace Nektar::LibUtilities;
39 
40 /**
41  * @brief Thin wrapper around SessionReader to provide a nicer Pythonic
42  * interface.
43  *
44  * This allows us to do, for example
45  *
46  * session = SessionReader.CreateInstance(sys.argv)
47  *
48  * which is more natural in Python.
49  */
51 {
52  int i, argc = py::len(ns), bufSize = 0;
53  char **argv = new char *[argc+1], *p;
54 
55  // Create argc, argv to give to the session reader. Note that this needs to
56  // be a contiguous block in memory, otherwise MPI (specifically OpenMPI)
57  // will likely segfault.
58  for (i = 0; i < argc; ++i)
59  {
60  std::string tmp = py::extract<std::string>(ns[i]);
61  bufSize += tmp.size() + 1;
62  }
63 
64  std::vector<char> buf(bufSize);
65  for (i = 0, p = &buf[0]; i < argc; ++i)
66  {
67  std::string tmp = py::extract<std::string>(ns[i]);
68  std::copy(tmp.begin(), tmp.end(), p);
69  p[tmp.size()] = '\0';
70  argv[i] = p;
71  p += tmp.size()+1;
72  }
73 
74  // Also make sure we set argv[argc] = NULL otherwise OpenMPI will also
75  // segfault.
76  argv[argc] = NULL;
77 
78  // Create session reader.
80 
81  // Clean up.
82  delete [] argv;
83 
84  return sr;
85 }
86 
87 /**
88  * @brief SessionReader exports.
89  *
90  * Currently wrapped functions:
91  * - SessionReader::CreateInstance for creating objects
92  * - SessionReader::GetSessionName to return the session name
93  * - SessionReader::Finalise to deal with finalising things
94  */
96 {
97  py::class_<SessionReader,
98  std::shared_ptr<SessionReader>,
99  boost::noncopyable>(
100  "SessionReader", py::no_init)
101 
102  .def("CreateInstance", SessionReader_CreateInstance)
103  .staticmethod("CreateInstance")
104 
105  .def("GetSessionName", &SessionReader::GetSessionName,
106  py::return_value_policy<py::copy_const_reference>())
107 
108  .def("Finalise", &SessionReader::Finalise)
109 
110  .def("DefinesParameter", &SessionReader::DefinesParameter)
111  .def("GetParameter", &SessionReader::GetParameter,
112  py::return_value_policy<py::return_by_value>())
113 
114  .def("GetVariable", &SessionReader::GetVariable,
115  py::return_value_policy<py::copy_const_reference>())
116 
117  .def("GetComm", &SessionReader::GetComm)
118 
119  ;
120 }
SessionReaderSharedPtr SessionReader_CreateInstance(py::list &ns)
Thin wrapper around SessionReader to provide a nicer Pythonic interface.
void export_SessionReader()
SessionReader exports.
Reads and parses information from a Nektar++ XML session file.
const std::string & GetSessionName() const
Returns the session name of the loaded XML document.
static SessionReaderSharedPtr CreateInstance(int argc, char *argv[])
Creates an instance of the SessionReader class.
CommSharedPtr GetComm()
Returns the communication object.
const NekDouble & GetParameter(const std::string &pName) const
Returns the value of the specified parameter.
const std::string & GetVariable(const unsigned int &idx) const
Returns the name of the variable specified by the given index.
bool DefinesParameter(const std::string &name) const
Checks if a parameter is specified in the XML document.
def copy(self)
Definition: pycml.py:2663
std::shared_ptr< SessionReader > SessionReaderSharedPtr