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
35#include "../NekPyConvertors.hpp"
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
93 std::string paramName, int paramValue)
94{
95 session->SetParameter(paramName, paramValue);
96}
97
99 std::string paramName, double paramValue)
100{
101 session->SetParameter(paramName, paramValue);
102}
103
105 std::string func, int var)
106{
107 return session->GetFunction(func, var);
108}
109
111 std::string func, std::string var)
112{
113 return session->GetFunction(func, var);
114}
115
116/**
117 * @brief Function to wrap SessionReader::GetParameters
118 *
119 * Returns a Python dict containing (parameter name)-> (parameter value)
120 * entries.
121 */
123{
124 return MapToPyDict(s->GetParameters());
125}
126
127/**
128 * @brief Function to wrap SessionReader::GetVariables
129 *
130 * Returns a Python list containing variable names.
131 */
133{
134 return VectorToPyList(s->GetVariables());
135}
136
137/**
138 * @brief SessionReader exports.
139 *
140 * Currently wrapped functions:
141 * - SessionReader::CreateInstance for creating objects
142 * - SessionReader::GetSessionName to return the session name
143 * - SessionReader::Finalise to deal with finalising things
144 */
145
147{
148 py::class_<SessionReader, std::shared_ptr<SessionReader>,
149 boost::noncopyable>("SessionReader", py::no_init)
150
151 .def("CreateInstance", SessionReader_CreateInstance)
152 .staticmethod("CreateInstance")
153
154 .def("GetSessionName", &SessionReader::GetSessionName,
155 py::return_value_policy<py::copy_const_reference>())
156
157 .def("Finalise", &SessionReader::Finalise)
158
159 .def("DefinesParameter", &SessionReader::DefinesParameter)
160 .def("GetParameter", &SessionReader::GetParameter,
161 py::return_value_policy<py::return_by_value>())
162 .def("GetParameters", &SessionReader_GetParameters)
163
164 .def("SetParameter", SessionReader_SetParameterInt)
165 .def("SetParameter", SessionReader_SetParameterDouble)
166
167 .def("DefinesSolverInfo", &SessionReader::DefinesSolverInfo)
168 .def("GetSolverInfo", &SessionReader::GetSolverInfo,
169 py::return_value_policy<py::copy_const_reference>())
170 .def("SetSolverInfo", &SessionReader::SetSolverInfo)
171
172 .def("GetVariable", &SessionReader::GetVariable,
173 py::return_value_policy<py::copy_const_reference>())
174 .def("GetVariables", SessionReader_GetVariables)
175
176 .def("GetFunction", SessionReader_GetFunction1)
177 .def("GetFunction", SessionReader_GetFunction2)
178
179 .def("GetComm", &SessionReader::GetComm)
180
181 .def("GetSharedFilesystem", &SessionReader::GetSharedFilesystem);
182}
py::list VectorToPyList(const std::vector< T > &input)
Converts a std::vector to a Python list.
py::dict MapToPyDict(const std::map< KeyT, ValT > &input)
Converts a std::map to a Python dict.
EquationSharedPtr SessionReader_GetFunction2(SessionReaderSharedPtr session, std::string func, std::string var)
void SessionReader_SetParameterDouble(SessionReaderSharedPtr session, std::string paramName, double paramValue)
EquationSharedPtr SessionReader_GetFunction1(SessionReaderSharedPtr session, std::string func, int var)
py::dict SessionReader_GetParameters(SessionReaderSharedPtr s)
Function to wrap SessionReader::GetParameters.
py::list SessionReader_GetVariables(SessionReaderSharedPtr s)
Function to wrap SessionReader::GetVariables.
SessionReaderSharedPtr SessionReader_CreateInstance(py::list &ns)
void SessionReader_SetParameterInt(SessionReaderSharedPtr session, std::string paramName, int paramValue)
void export_SessionReader()
SessionReader exports.
tBaseSharedPtr CreateInstance(tKey idKey, tParam... args)
Create an instance of the class referred to by idKey.
static void Finalise(gs_data *pGsh)
Deallocates the GSLib mapping data.
Definition: GsLib.hpp:248
std::shared_ptr< SessionReader > SessionReaderSharedPtr
std::shared_ptr< Equation > EquationSharedPtr
Definition: Equation.h:125
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.
int GetArgc()
Returns the constructed argc.
char ** GetArgv()
Returns the constructed argv.