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#include <pybind11/stl.h>
38
39using namespace Nektar::LibUtilities;
40
41#ifdef NEKTAR_USE_MPI
44#endif
45
46/*
47 * @brief Thin wrapper around SessionReader to provide a nicer Pythonic
48 * interface.
49 *
50 * This allows us to do, for example
51 *
52 * session = SessionReader.CreateInstance(sys.argv)
53 *
54 * which is more natural in Python.
55 */
56
58{
59 CppCommandLine cpp(ns);
60
61 int argc = cpp.GetArgc();
62 char **argv = cpp.GetArgv();
63
64#ifdef NEKTAR_USE_MPI
65 // In the case we're using MPI, it may already have been initialised. So to
66 // handle this, we'll construct our own CommMpi object and pass through to
67 // the SessionReader. This will persist indefinitely, or at least until the
68 // library is unloaded by Python.
69
70 if (!MPICOMM)
71 {
72 MPICOMM = GetCommFactory().CreateInstance("ParallelMPI", argc, argv);
73 }
74
75 std::vector<std::string> filenames(argc - 1);
76 for (int i = 1; i < argc; ++i)
77 {
78 filenames[i - 1] = std::string(argv[i]);
79 }
80
81 // Create session reader.
83 SessionReader::CreateInstance(argc, argv, filenames, MPICOMM);
84#else
85 // Create session reader.
86 SessionReaderSharedPtr sr = SessionReader::CreateInstance(argc, argv);
87#endif
88
89 return sr;
90}
91
92/**
93 * @brief SessionReader exports.
94 */
95
96void export_SessionReader(py::module &m)
97{
98 py::class_<SessionReader, std::shared_ptr<SessionReader>>(m,
99 "SessionReader")
100
101 .def_static("CreateInstance", SessionReader_CreateInstance)
102
103 .def("GetSessionName", &SessionReader::GetSessionName,
105
106 .def("InitSession", &SessionReader::InitSession,
107 py::arg("filenames") = py::list())
108
109 .def("Finalise", &SessionReader::Finalise)
110
111 .def("DefinesParameter", &SessionReader::DefinesParameter)
112 .def("GetParameter", &SessionReader::GetParameter,
114 .def("GetParameters", &SessionReader::GetParameters)
115
116 .def("SetParameter", py::overload_cast<const std::string &, int &>(
117 &SessionReader::SetParameter))
118 .def("SetParameter", py::overload_cast<const std::string &, double &>(
119 &SessionReader::SetParameter))
120 .def("SetParameter", py::overload_cast<const std::string &, size_t &>(
121 &SessionReader::SetParameter))
122
123 .def("DefinesSolverInfo", &SessionReader::DefinesSolverInfo)
124 .def("GetSolverInfo", &SessionReader::GetSolverInfo,
126 .def("SetSolverInfo", &SessionReader::SetSolverInfo)
127
128 .def("GetVariable", &SessionReader::GetVariable,
130 .def("GetVariables", &SessionReader::GetVariables)
131
132 .def("GetFunction",
133 py::overload_cast<const std::string &, const std::string &,
134 const int>(&SessionReader::GetFunction,
135 py::const_),
136 py::arg("name"), py::arg("var"), py::arg("domain") = 0)
137 .def("GetFunction",
138 py::overload_cast<const std::string &, const unsigned int &,
139 const int>(&SessionReader::GetFunction,
140 py::const_),
141 py::arg("name"), py::arg("var"), py::arg("domain") = 0)
142
143 .def("GetComm", &SessionReader::GetComm)
144
145 .def("GetSharedFilesystem", &SessionReader::GetSharedFilesystem);
146}
void export_SessionReader(py::module &m)
SessionReader exports.
SessionReaderSharedPtr SessionReader_CreateInstance(py::list &ns)
tBaseSharedPtr CreateInstance(tKey idKey, tParam... args)
Create an instance of the class referred to by idKey.
def copy(self)
Definition: pycml.py:2663
static void Finalise(gs_data *pGsh)
Deallocates the GSLib mapping data.
Definition: GsLib.hpp:248
std::shared_ptr< SessionReader > SessionReaderSharedPtr
CommFactory & GetCommFactory()
std::shared_ptr< Comm > CommSharedPtr
Pointer to a Communicator object.
Definition: Comm.h:55
Helper structure to construct C++ command line argc and argv variables from a Python list.
Definition: NekPyConfig.hpp:89
int GetArgc()
Returns the constructed argc.
char ** GetArgv()
Returns the constructed argv.