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
150{
151 using EqSysWrap = EquationSystemWrap<EquationSystem>;
152
155
156 py::class_<EqSysWrap, std::shared_ptr<EqSysWrap>, boost::noncopyable>(
157 "EquationSystem", py::init<LibUtilities::SessionReaderSharedPtr,
159
160 // Virtual functions that can be optionally overridden
161 .def("InitObject", &EquationSystem::InitObject,
162 &EqSysWrap::Default_v_InitObject)
163 .def("DoInitialise", &EquationSystem::DoInitialise,
164 &EqSysWrap::Default_v_DoInitialise)
165 .def("DoSolve", &EquationSystem::DoSolve, &EqSysWrap::Default_v_DoSolve)
166 .def("SetInitialConditions", &EquationSystem::SetInitialConditions,
167 &EqSysWrap::Default_v_SetInitialConditions)
168 .def("EvaluateExactSolution", &EqSysWrap::v_EvaluateExactSolution,
169 &EqSysWrap::Default_v_EvaluateExactSolution)
170 .def("LinfError", &EqSysWrap::v_LinfError,
171 &EqSysWrap::Default_v_LinfError)
172 .def("L2Error", &EqSysWrap::v_L2Error, &EqSysWrap::Default_v_L2Error)
173
174 // Fields accessors (read-only)
175 .def("GetFields", &EquationSystem_GetFields)
176 .add_property("fields", &EquationSystem_GetFields)
177
178 // Various utility functions
179 .def("GetNvariables", &EquationSystem::GetNvariables)
180 .def("GetVariable", &EquationSystem::GetVariable)
181 .def("GetNpoints", &EquationSystem::GetNpoints)
182 .def("SetInitialStep", &EquationSystem::SetInitialStep)
183 .def("ZeroPhysFields", &EquationSystem::ZeroPhysFields)
184
185 // Time accessors/properties
186 .def("GetTime", &EquationSystem::GetTime)
187 .def("SetTime", &EquationSystem::SetTime)
188 .add_property("time", &EquationSystem::GetTime,
189 &EquationSystem::SetTime)
190
191 // Timestep accessors/properties
192 .def("GetTimeStep", &EquationSystem::GetTimeStep)
193 .def("SetTimeStep", &EquationSystem::SetTimeStep)
194 .add_property("timestep", &EquationSystem::GetTimeStep,
195 &EquationSystem::SetTimeStep)
196
197 // Steps accessors/properties
198 .def("GetSteps", &EquationSystem::GetSteps)
199 .def("SetSteps", &EquationSystem::SetSteps)
200 .add_property("steps", &EquationSystem::GetSteps,
201 &EquationSystem::SetSteps)
202
203 // Print a summary
204 .def("PrintSummary", &EquationSystem_PrintSummary)
205
206 // Access functions from the session file
207 .def("GetFunction", &EquationSystem_GetFunction1)
208 .def("GetFunction", &EquationSystem_GetFunction2)
209
210 // I/O utility functions
211 .def("WriteFld", &EquationSystem_WriteFld)
212 .def("Checkpoint_Output", &EquationSystem_Checkpoint_Output)
213
214 // Factory functions.
215 .def("Create", &EquationSystem_Create)
216 .staticmethod("Create")
217 .def("Register", [](std::string const &filterName,
218 py::object &obj) { fac(filterName, obj); })
219 .staticmethod("Register");
220
222}
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 export_EquationSystem()
void EquationSystem_PrintSummary(EquationSystemSharedPtr eqSys)
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.
A helper class that for factory-based classes allows std::shared_ptr<T> as something that boost::pyth...