Nektar++
Python/EquationSystem.cpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: EquationSystem.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 the EquationSystem.
32//
33///////////////////////////////////////////////////////////////////////////////
34
37
40
41using namespace Nektar;
42using namespace Nektar::SolverUtils;
43
44/**
45 * @brief Dummy equation system that can be used for Python testing.
46 *
47 * This class simply evaluates a known function as the implementation as part of
48 * the DoInitialise method.
49 */
51{
52public:
54
55 /// Creates an instance of this class
59 {
62 pGraph);
63 p->InitObject();
64 return p;
65 }
66 /// Name of class
67 static std::string className;
68
70 {
71 }
72
73protected:
76 : EquationSystem(pSession, pGraph)
77 {
78 }
79
80 void v_InitObject(bool DeclareField) override
81 {
82 EquationSystem::v_InitObject(DeclareField);
83
84 for (int i = 0; i < m_fields.size(); ++i)
85 {
86 int nq = m_fields[0]->GetNpoints();
87
88 Array<OneD, NekDouble> x(nq), y(nq), z(nq);
89 m_fields[i]->GetCoords(x, y, z);
90
91 for (int j = 0; j < nq; ++j)
92 {
93 m_fields[i]->UpdatePhys()[j] = sin(x[j]) * sin(y[j]);
94 }
95
96 m_fields[i]->FwdTrans(m_fields[i]->GetPhys(),
97 m_fields[i]->UpdateCoeffs());
98 }
99 }
100};
101
104 "Dummy", DummyEquationSystem::create, "Dummy equation system.");
105
107 std::string eqnSysName, LibUtilities::SessionReaderSharedPtr session,
109{
111 GetEquationSystemFactory().CreateInstance(eqnSysName, session, mesh);
112 return ret;
113}
114
117{
118 return eqSys->UpdateFields();
119}
120
122{
123 eqSys->PrintSummary(std::cout);
124}
125
126std::shared_ptr<SessionFunction> EquationSystem_GetFunction1(
127 EquationSystemSharedPtr eqSys, std::string name)
128{
129 return eqSys->GetFunction(name);
130}
131
132std::shared_ptr<SessionFunction> EquationSystem_GetFunction2(
133 EquationSystemSharedPtr eqSys, std::string name,
135{
136 return eqSys->GetFunction(name, field);
137}
138
140{
141 eqSys->WriteFld(name);
142}
143
145{
146 eqSys->Checkpoint_Output(n);
147}
148
149void export_EquationSystem(py::module &m)
150{
151 using EqSysWrap = EquationSystemWrap<EquationSystem>;
153
156
157 py::classh<EquationSystem, EqSysWrap>(m, "EquationSystem")
160
161 // Virtual functions that can be optionally overridden
162 .def("InitObject", &EqSysPub::v_InitObject)
163 .def("DoInitialise", &EqSysPub::v_DoInitialise)
164 .def("DoSolve", &EqSysPub::v_DoSolve)
165 .def("SetInitialConditions", &EqSysPub::v_SetInitialConditions)
166 .def("EvaluateExactSolution", &EqSysWrap::EvaluateExactSolution)
167 .def("LinfError", &EqSysWrap::LinfError)
168 .def("L2Error", &EqSysWrap::L2Error)
169
170 // Fields accessors (read-only)
171 .def("GetFields", &EquationSystem_GetFields)
172 .def_property_readonly("fields", &EquationSystem_GetFields)
173
174 // Various utility functions
175 .def("GetNvariables", &EquationSystem::GetNvariables)
176 .def("GetVariable", &EquationSystem::GetVariable)
177 .def("GetNpoints", &EquationSystem::GetNpoints)
178 .def("SetInitialStep", &EquationSystem::SetInitialStep)
179 .def("ZeroPhysFields", &EquationSystem::ZeroPhysFields)
180
181 // Time accessors/properties
182 .def("GetTime", &EquationSystem::GetTime)
183 .def("SetTime", &EquationSystem::SetTime)
184 .def_property("time", &EquationSystem::GetTime,
185 &EquationSystem::SetTime)
186
187 // Timestep accessors/properties
188 .def("GetTimeStep", &EquationSystem::GetTimeStep)
189 .def("SetTimeStep", &EquationSystem::SetTimeStep)
190 .def_property("timestep", &EquationSystem::GetTimeStep,
191 &EquationSystem::SetTimeStep)
192
193 // Steps accessors/properties
194 .def("GetSteps", &EquationSystem::GetSteps)
195 .def("SetSteps", &EquationSystem::SetSteps)
196 .def_property("steps", &EquationSystem::GetSteps,
197 &EquationSystem::SetSteps)
198
199 // Print a summary
200 .def("PrintSummary", &EquationSystem_PrintSummary)
201
202 // Access functions from the session file
203 .def("GetFunction", &EquationSystem_GetFunction1)
204 .def("GetFunction", &EquationSystem_GetFunction2)
205
206 // I/O utility functions
207 .def("WriteFld", &EquationSystem_WriteFld)
208 .def("Checkpoint_Output", &EquationSystem_Checkpoint_Output)
209
210 // Factory functions.
211 .def_static("Create", &EquationSystem_Create)
212 .def_static("Register", [](std::string &filterName, py::object &obj) {
213 fac(filterName, obj, filterName);
214 });
215}
void EquationSystem_Checkpoint_Output(EquationSystemSharedPtr eqSys, int n)
std::shared_ptr< SessionFunction > EquationSystem_GetFunction1(EquationSystemSharedPtr eqSys, std::string name)
void EquationSystem_WriteFld(EquationSystemSharedPtr eqSys, std::string name)
EquationSystemSharedPtr EquationSystem_Create(std::string eqnSysName, LibUtilities::SessionReaderSharedPtr session, SpatialDomains::MeshGraphSharedPtr mesh)
Array< OneD, MultiRegions::ExpListSharedPtr > EquationSystem_GetFields(EquationSystemSharedPtr eqSys)
void EquationSystem_PrintSummary(EquationSystemSharedPtr eqSys)
void export_EquationSystem(py::module &m)
std::shared_ptr< SessionFunction > EquationSystem_GetFunction2(EquationSystemSharedPtr eqSys, std::string name, MultiRegions::ExpListSharedPtr field)
Dummy equation system that can be used for Python testing.
void v_InitObject(bool DeclareField) override
Initialisation object for EquationSystem.
DummyEquationSystem(const LibUtilities::SessionReaderSharedPtr &pSession, const SpatialDomains::MeshGraphSharedPtr &pGraph)
static std::string className
Name of class.
static SolverUtils::EquationSystemSharedPtr create(const LibUtilities::SessionReaderSharedPtr &pSession, const SpatialDomains::MeshGraphSharedPtr &pGraph)
Creates an instance of this class.
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, std::string pDesc="")
Register a class with the factory.
tBaseSharedPtr CreateInstance(tKey idKey, tParam... args)
Create an instance of the class referred to by idKey.
General purpose memory allocation routines with the ability to allocate from thread specific memory p...
A base class for describing how to solve specific equations.
Array< OneD, MultiRegions::ExpListSharedPtr > m_fields
Array holding all dependent variables.
std::shared_ptr< SessionReader > SessionReaderSharedPtr
std::shared_ptr< ExpList > ExpListSharedPtr
Shared pointer to an ExpList object.
std::shared_ptr< EquationSystem > EquationSystemSharedPtr
A shared pointer to an EquationSystem object.
EquationSystemFactory & GetEquationSystemFactory()
std::shared_ptr< MeshGraph > MeshGraphSharedPtr
Definition: MeshGraph.h:174
std::vector< double > z(NPUPPER)
EquationSystem wrapper to handle virtual function calls in EquationSystem and its subclasses.