| 1 |
"""Unit tests for the Nonlinear- and Linear-VariationalProblem classes""" |
| 2 |
|
| 3 |
# Copyright (C) 2016 Garth N. Wells |
| 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 |
import pytest |
| 21 |
from dolfin import * |
| 22 |
from dolfin_utils.test import * |
| 23 |
|
| 24 |
|
| 25 |
def test_linear_construction(): |
| 26 |
"Test construction of LinearVariationalProblem" |
| 27 |
|
| 28 |
mesh = UnitSquareMesh(4, 4) |
| 29 |
V = FunctionSpace(mesh, "Lagrange", 1) |
| 30 |
u = TrialFunction(V) |
| 31 |
v = TestFunction(V) |
| 32 |
f = Constant(100.0) |
| 33 |
|
| 34 |
a = dot(grad(u), grad(v))*dx + u*v*dx |
| 35 |
L = f*v*dx |
| 36 |
|
| 37 |
bc = DirichletBC(V, 0.0, DomainBoundary()) |
| 38 |
|
| 39 |
w = Function(V) |
| 40 |
with pytest.raises(TypeError): |
| 41 |
problem = LinearVariationalProblem(a, L) |
| 42 |
with pytest.raises(Exception): |
| 43 |
problem = LinearVariationalProblem(a, L, [bc]) |
| 44 |
with pytest.raises(Exception): |
| 45 |
problem = LinearVariationalProblem(a, L, [bc], w) |
| 46 |
problem = LinearVariationalProblem(a, L, w, []) |
| 47 |
problem = LinearVariationalProblem(a, L, w, [bc]) |
| 48 |
problem = LinearVariationalProblem(a, L, w, [bc, bc]) |
| 49 |
|
| 50 |
|
| 51 |
def test_nonlinear_construction(): |
| 52 |
"Test construction of NonlinearVariationalProblem" |
| 53 |
|
| 54 |
mesh = UnitSquareMesh(4, 4) |
| 55 |
V = FunctionSpace(mesh, "Lagrange", 1) |
| 56 |
u = Function(V) |
| 57 |
du = TrialFunction(V) |
| 58 |
v = TestFunction(V) |
| 59 |
f = Constant(100.0) |
| 60 |
|
| 61 |
F = dot(grad(u), grad(v))*dx + u*v*dx - f*v*dx |
| 62 |
J = derivative(F, u, du) |
| 63 |
bc = DirichletBC(V, 0.0, DomainBoundary()) |
| 64 |
|
| 65 |
with pytest.raises(Exception): |
| 66 |
problem = NonlinearVariationalProblem(F, u, J) |
| 67 |
problem = NonlinearVariationalProblem(F, u) |
| 68 |
problem = NonlinearVariationalProblem(F, u, []) |
| 69 |
problem = NonlinearVariationalProblem(F, u, [], J) |
| 70 |
problem = NonlinearVariationalProblem(F, u, J=J, bcs=[]) |