Debian Codegraph @

python/test/unit/nls/test_PETScTAOSolver.py

raw
1 """Unit test for the PETSc TAO solver"""
2
3 # Copyright (C) 2014 Tianyi Li
4 #
5 # This file is part of DOLFIN.
6 #
7 # DOLFIN is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Lesser General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # DOLFIN is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Lesser General Public License for more details.
16 #
17 # You should have received a copy of the GNU Lesser General Public License
18 # along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
19 #
20 # First added: 2014-07-19
21 # Last changed: 2014-10-14
22
23 """This demo uses PETSc TAO solver for nonlinear (bound-constrained)
24 optimisation problems to solve the same minimization problem for testing
25 TAOLinearBoundSolver."""
26
27 from dolfin import *
28 import pytest
29
30 from dolfin_utils.test import *
31
32 backend = set_parameters_fixture("linear_algebra_backend", ["PETSc"])
33
34 @skip_if_not_PETSc
35 def test_tao_linear_bound_solver(backend):
36 "Test PETScTAOSolver"
37
38 # Create mesh and define function space
39 Lx = 1.0; Ly = 0.1
40 mesh = RectangleMesh(Point(0, 0), Point(Lx, Ly), 100, 10)
41 V = FunctionSpace(mesh, "Lagrange", 1)
42
43 # Define Dirichlet boundaries
44 def left(x,on_boundary):
45 return on_boundary and near(x[0], 0.0)
46
47 def right(x,on_boundary):
48 return on_boundary and near(x[0], Lx)
49
50 # Define boundary conditions
51 zero = Constant(0.0)
52 one = Constant(1.0)
53 bc_l = DirichletBC(V, zero, left)
54 bc_r = DirichletBC(V, one, right)
55 bcs = [bc_l, bc_r]
56
57 # Define the variational problem
58 u = Function(V)
59 du = TrialFunction(V)
60 v = TestFunction(V)
61 cv = Constant(3.0/4.0)
62 ell = Constant(0.5) # This should be smaller than Lx
63 energy = cv*(ell/2.0*inner(grad(u), grad(u))*dx + 2.0/ell*u*dx)
64 grad_energy = derivative(energy, u, v)
65 H_energy = derivative(grad_energy, u, du)
66
67 # Define the lower and upper bounds
68 lb = interpolate(Constant(0.0), V)
69 ub = interpolate(Constant(1.0), V)
70
71 # Apply BC to the lower and upper bounds
72 for bc in bcs:
73 bc.apply(lb.vector())
74 bc.apply(ub.vector())
75
76 # Define the minimisation problem by using OptimisationProblem class
77 class TestProblem(OptimisationProblem):
78
79 def __init__(self):
80 OptimisationProblem.__init__(self)
81
82 # Objective function
83 def f(self, x):
84 u.vector()[:] = x
85 return assemble(energy)
86
87 # Gradient of the objective function
88 def F(self, b, x):
89 u.vector()[:] = x
90 assemble(grad_energy, tensor=b)
91
92 # Hessian of the objective function
93 def J(self, A, x):
94 u.vector()[:] = x
95 assemble(H_energy, tensor=A)
96
97 # Create the PETScTAOSolver
98 solver = PETScTAOSolver()
99
100 # Set some parameters
101 solver.parameters["monitor_convergence"] = True
102 solver.parameters["report"] = True
103
104 solver.solve(TestProblem(), u.vector(), lb.vector(), ub.vector())
105 solver.solve(TestProblem(), u.vector(), lb.vector(), ub.vector())
106
107 # Verify that energy(u) = Ly
108 assert round(assemble(energy) - Ly, 4) == 0