Debian Codegraph @

python/test/unit/nls/test_TAO_linear_bound_solver.py

raw
1 """Unit tests for the TAOLinearBoundSolver interface"""
2
3 # Copyright (C) 2013 Corrado Maurini
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: 03/09/2012
21 # Last changed: 2013-04-04
22
23 # Begin demo
24 # Corrado Maurini
25 #
26 # This example solve the bound constrained minimization problem
27 # in the domain (x,y) in [0,Lx]x[0,Ly]
28 #
29 # min F(u) with 0<=u<=1 and u(0,y)= 0, u(Lx,y) = 1
30 #
31 # where F(u) is the quadratic functionaldefined by the form
32 #
33 # F(u) = 3./4.*(ell/2.*inner(grad(u), grad(u))+ 2./ell*usol)*dx
34 #
35 # An analytical is available:
36 # u(x,y) = 0 for 0<x<1-ell, u(x,y) = (x-(1-ell))^2 for 1-ell<x<Lx
37 # and the value of the functional at the solution usol is F(usol)=Ly
38 # for any value of ell, with 0<ell<Lx.
39
40 from dolfin import *
41 import pytest
42
43 from dolfin_utils.test import *
44
45 backend = set_parameters_fixture("linear_algebra_backend", ["PETSc"])
46
47 @skip_if_not_PETSc
48 def test_tao_linear_bound_solver(backend):
49 "Test TAOLinearBoundSolver"
50
51 # Create mesh and define function space
52 Lx = 1.0; Ly = 0.1
53 mesh = RectangleMesh(Point(0, 0), Point(Lx, Ly), 100, 10)
54 V = FunctionSpace(mesh, "Lagrange", 1)
55
56 # Define Dirichlet boundaries
57 def left(x,on_boundary):
58 return on_boundary and near(x[0], 0.0)
59
60 def right(x,on_boundary):
61 return on_boundary and near(x[0], Lx)
62
63 # Define boundary conditions
64 zero = Constant(0.0)
65 one = Constant(1.0)
66 bc_l = DirichletBC(V, zero, left)
67 bc_r = DirichletBC(V, one, right)
68 bc = [bc_l, bc_r]
69
70 # Define variational problem
71 usol = Function(V)
72 u = TrialFunction(V)
73 v = TestFunction(V)
74 cv = Constant(3.0/4.0)
75 ell = Constant(0.5) # This should be smaller than Lx
76 F = cv*(ell/2.0*inner(grad(usol), grad(usol))*dx + 2.0/ell*usol*dx)
77 # Weak form
78 a = cv*ell*inner(grad(u), grad(v))*dx
79 L = -cv*2*v/ell*dx
80
81 # Assemble the linear system
82 A, b = assemble_system(a, L, bc)
83
84 # Define the upper and lower bounds
85 upperbound = interpolate(Constant(1.), V)
86 lowerbound = interpolate(Constant(0.), V)
87 xu = upperbound.vector()
88 xl = lowerbound.vector()
89
90 # Take the PETScVector of the solution function
91 xsol = usol.vector()
92
93 solver = TAOLinearBoundSolver("tron", "cg")
94 solver.solve(A,xsol,b,xl,xu)
95 solver.solve(A,xsol,b,xl,xu)
96
97 # Test that F(usol) = Ly
98 assert round(assemble(F) - Ly, 4) == 0