Debian Codegraph @

python/src/nls.cpp

raw
1 // Copyright (C) 2017 Chris Richardson and Garth N. Wells
2 //
3 // This file is part of DOLFIN.
4 //
5 // DOLFIN is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // DOLFIN is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
17
18 #include <memory>
19 #include <pybind11/pybind11.h>
20 #include <pybind11/numpy.h>
21 #include <pybind11/stl.h>
22
23 #include <dolfin/common/Variable.h>
24 #include <dolfin/parameter/Parameters.h>
25 #include <dolfin/la/GenericLinearAlgebraFactory.h>
26 #include <dolfin/la/GenericLinearSolver.h>
27 #include <dolfin/la/GenericMatrix.h>
28 #include <dolfin/la/GenericVector.h>
29 #include <dolfin/la/PETScObject.h>
30 #include <dolfin/nls/NewtonSolver.h>
31 #include <dolfin/nls/PETScSNESSolver.h>
32 #include <dolfin/nls/PETScTAOSolver.h>
33 #include <dolfin/nls/TAOLinearBoundSolver.h>
34 #include <dolfin/nls/NonlinearProblem.h>
35 #include <dolfin/nls/OptimisationProblem.h>
36
37 #include "casters.h"
38
39 namespace py = pybind11;
40
41 namespace dolfin_wrappers
42 {
43 void nls(py::module& m)
44 {
45 // dolfin::NewtonSolver 'trampoline' for overloading virtual
46 // functions from Python
47 class PyNewtonSolver : public dolfin::NewtonSolver
48 {
49 using dolfin::NewtonSolver::NewtonSolver;
50
51 // pybdind11 has some issues when passing by reference (due to
52 // the return value policy), so the below is non-standard. See
53 // https://github.com/pybind/pybind11/issues/250.
54
55 bool converged(const dolfin::GenericVector& r,
56 const dolfin::NonlinearProblem& nonlinear_problem,
57 std::size_t iteration)
58 {
59 PYBIND11_OVERLOAD_INT(bool, dolfin::NewtonSolver, "converged", &r, &nonlinear_problem, iteration);
60 return dolfin::NewtonSolver::converged(r, nonlinear_problem, iteration);
61 }
62
63 void solver_setup(std::shared_ptr<const dolfin::GenericMatrix> A,
64 std::shared_ptr<const dolfin::GenericMatrix> P,
65 const dolfin::NonlinearProblem& nonlinear_problem,
66 std::size_t iteration)
67 {
68 PYBIND11_OVERLOAD_INT(void, dolfin::NewtonSolver, "solver_setup", A, P,
69 &nonlinear_problem, iteration);
70 return dolfin::NewtonSolver::solver_setup(A, P, nonlinear_problem, iteration);
71 }
72
73 void update_solution(dolfin::GenericVector& x, const dolfin::GenericVector& dx,
74 double relaxation_parameter,
75 const dolfin::NonlinearProblem& nonlinear_problem,
76 std::size_t iteration)
77 {
78 PYBIND11_OVERLOAD_INT(void, dolfin::NewtonSolver, "update_solution",
79 &x, &dx, relaxation_parameter, nonlinear_problem,
80 iteration);
81 return dolfin::NewtonSolver::update_solution(x, dx, relaxation_parameter,
82 nonlinear_problem, iteration);
83 }
84 };
85
86 // Class used to expose protected dolfin::NewtonSolver members
87 // (see https://github.com/pybind/pybind11/issues/991)
88 class PyPublicNewtonSolver : public dolfin::NewtonSolver
89 {
90 public:
91 using NewtonSolver::converged;
92 using NewtonSolver::solver_setup;
93 using NewtonSolver::update_solution;
94 };
95
96 // dolfin::NewtonSolver
97 py::class_<dolfin::NewtonSolver, std::shared_ptr<dolfin::NewtonSolver>, PyNewtonSolver,
98 dolfin::Variable>(m, "NewtonSolver")
99 .def(py::init<>())
100 .def(py::init([](const MPICommWrapper comm)
101 { return std::unique_ptr<PyNewtonSolver>(new PyNewtonSolver(comm.get())); }))
102 .def(py::init([](const MPICommWrapper comm, std::shared_ptr<dolfin::GenericLinearSolver> solver,
103 dolfin::GenericLinearAlgebraFactory& factory)
104 { return std::unique_ptr<PyNewtonSolver>(new PyNewtonSolver(comm.get(), solver, factory)); }))
105 .def("solve", &dolfin::NewtonSolver::solve)
106 .def("converged", &PyPublicNewtonSolver::converged)
107 .def("solver_setup", &PyPublicNewtonSolver::solver_setup)
108 .def("update_solution", &PyPublicNewtonSolver::update_solution)
109 .def("linear_solver", &dolfin::NewtonSolver::linear_solver, py::return_value_policy::reference);
110
111 #ifdef HAS_PETSC
112 // dolfin::PETScSNESSolver
113 py::class_<dolfin::PETScSNESSolver, std::shared_ptr<dolfin::PETScSNESSolver>,
114 dolfin::PETScObject>(m, "PETScSNESSolver")
115 .def(py::init<std::string>(), py::arg("nls_type")="default")
116 .def(py::init([](const MPICommWrapper comm, std::string nls_type="default")
117 { return std::unique_ptr<dolfin::PETScSNESSolver>(
118 new dolfin::PETScSNESSolver(comm.get(), nls_type)); }),
119 py::arg("comm"), py::arg("nls_type") = "default")
120 .def_readwrite("parameters", &dolfin::PETScSNESSolver::parameters)
121 .def("snes", &dolfin::PETScSNESSolver::snes)
122 .def("solve", (std::pair<std::size_t, bool> (dolfin::PETScSNESSolver::*)(dolfin::NonlinearProblem&,
123 dolfin::GenericVector&))
124 &dolfin::PETScSNESSolver::solve)
125 .def("set_from_options", &dolfin::PETScSNESSolver::set_from_options);
126
127 // dolfin::TAOLinearBoundSolver
128 py::class_<dolfin::TAOLinearBoundSolver, std::shared_ptr<dolfin::TAOLinearBoundSolver>, dolfin::Variable>
129 (m, "TAOLinearBoundSolver")
130 .def(py::init([](const MPICommWrapper comm)
131 { return std::unique_ptr<dolfin::TAOLinearBoundSolver>(new dolfin::TAOLinearBoundSolver(comm.get())); }))
132 .def(py::init<std::string, std::string, std::string>(), py::arg("method")="default",
133 py::arg("ksp_type")="default", py::arg("pc_type")="default")
134 .def("solve", (std::size_t (dolfin::TAOLinearBoundSolver::*)
135 (const dolfin::GenericMatrix&, dolfin::GenericVector&,
136 const dolfin::GenericVector&, const dolfin::GenericVector&,
137 const dolfin::GenericVector&))
138 &dolfin::TAOLinearBoundSolver::solve);
139
140 // dolfin::PETScTAOSolver
141 py::class_<dolfin::PETScTAOSolver, std::shared_ptr<dolfin::PETScTAOSolver>, dolfin::PETScObject>(m, "PETScTAOSolver")
142 .def(py::init<>())
143 .def(py::init([](const MPICommWrapper comm, std::string tao_type="default",
144 std::string ksp_type="default", std::string pc_type="default")
145 { return std::unique_ptr<dolfin::PETScTAOSolver>(
146 new dolfin::PETScTAOSolver(comm.get(), tao_type, ksp_type, pc_type)); }),
147 py::arg("comm"), py::arg("tao_type")="default",
148 py::arg("ksp_type")="default", py::arg("pc_type")="default")
149 .def_readwrite("parameters", &dolfin::PETScTAOSolver::parameters)
150 .def("solve", (std::pair<std::size_t, bool> (dolfin::PETScTAOSolver::*)(dolfin::OptimisationProblem&, dolfin::GenericVector&))
151 &dolfin::PETScTAOSolver::solve)
152 .def("solve", (std::pair<std::size_t, bool> (dolfin::PETScTAOSolver::*)(dolfin::OptimisationProblem&, dolfin::GenericVector&,
153 const dolfin::GenericVector&, const dolfin::GenericVector&))
154 &dolfin::PETScTAOSolver::solve);
155 #endif
156
157 // dolfin::NonlinearProblem 'trampoline' for overloading from
158 // Python
159 class PyNonlinearProblem : public dolfin::NonlinearProblem
160 {
161 using dolfin::NonlinearProblem::NonlinearProblem;
162
163 // pybdind11 has some issues when passing by reference (due to
164 // the return value policy), so the below is non-standard. See
165 // https://github.com/pybind/pybind11/issues/250.
166
167 void J(dolfin::GenericMatrix& A, const dolfin::GenericVector& x) override
168 {
169 PYBIND11_OVERLOAD_INT(void, dolfin::NonlinearProblem, "J", &A, &x);
170 py::pybind11_fail("Tried to call pure virtual function dolfin::OptimisationProblem::J");
171 }
172
173 void F(dolfin::GenericVector& b, const dolfin::GenericVector& x) override
174 {
175 PYBIND11_OVERLOAD_INT(void, dolfin::NonlinearProblem, "F", &b, &x);
176 py::pybind11_fail("Tried to call pure virtual function dolfin::OptimisationProblem::F");
177 }
178
179 void form(dolfin::GenericMatrix& A, dolfin::GenericMatrix& P,
180 dolfin::GenericVector& b, const dolfin::GenericVector& x) override
181 {
182 PYBIND11_OVERLOAD_INT(void, dolfin::NonlinearProblem, "form", &A, &P, &b, &x);
183 return dolfin::NonlinearProblem::form(A, P, b, x);
184 }
185
186 };
187
188 // dolfin::NonlinearProblem
189 py::class_<dolfin::NonlinearProblem, std::shared_ptr<dolfin::NonlinearProblem>, PyNonlinearProblem>(m, "NonlinearProblem")
190 .def(py::init<>())
191 .def("F", &dolfin::NonlinearProblem::F)
192 .def("J", &dolfin::NonlinearProblem::J)
193 .def("form", (void (dolfin::NonlinearProblem::*)(dolfin::GenericMatrix&, dolfin::GenericMatrix&,
194 dolfin::GenericVector&, const dolfin::GenericVector&))
195 &dolfin::NonlinearProblem::form);
196
197 // dolfin::OptimizationProblem 'trampoline' for overloading from
198 // Python
199 class PyOptimisationProblem : public dolfin::OptimisationProblem
200 {
201 using dolfin::OptimisationProblem::OptimisationProblem;
202
203 // pybdind11 has some issues when passing by reference (due to
204 // the return value policy), so the below is non-standard. See
205 // https://github.com/pybind/pybind11/issues/250.
206
207 double f(const dolfin::GenericVector& x) override
208 {
209 PYBIND11_OVERLOAD_INT(double, dolfin::OptimisationProblem, "f", &x);
210 py::pybind11_fail("Tried to call pure virtual function dolfin::OptimisationProblem::f");
211 }
212
213 void F(dolfin::GenericVector& b, const dolfin::GenericVector& x) override
214 {
215 PYBIND11_OVERLOAD_INT(void, dolfin::OptimisationProblem, "F", &b, &x);
216 py::pybind11_fail("Tried to call pure virtual function dolfin::OptimisationProblem::F");
217 }
218
219 void J(dolfin::GenericMatrix& A, const dolfin::GenericVector& x) override
220 {
221 PYBIND11_OVERLOAD_INT(void, dolfin::OptimisationProblem, "J", &A, &x);
222 py::pybind11_fail("Tried to call pure virtual function dolfin::OptimisationProblem::J");
223 }
224 };
225
226 // dolfin::OptimizationProblem
227 py::class_<dolfin::OptimisationProblem, std::shared_ptr<dolfin::OptimisationProblem>,
228 PyOptimisationProblem>(m, "OptimisationProblem")
229 .def(py::init<>())
230 .def("f", &dolfin::OptimisationProblem::f)
231 .def("F", &dolfin::OptimisationProblem::F)
232 .def("J", &dolfin::OptimisationProblem::J);
233 }
234 }