| 1 |
"""Unit tests for LocalSolver""" |
| 2 |
|
| 3 |
# Copyright (C) 2013 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 |
# Modified by Steven Vandekerckhove, 2014 |
| 21 |
# Modified by Tormod Landet, 2015 |
| 22 |
|
| 23 |
import pytest |
| 24 |
import numpy |
| 25 |
from dolfin import * |
| 26 |
from dolfin_utils.test import skip_in_parallel |
| 27 |
from dolfin_utils.test import set_parameters_fixture |
| 28 |
ghost_mode = set_parameters_fixture("ghost_mode", ["shared_facet"]) |
| 29 |
|
| 30 |
|
| 31 |
def test_solve_global_rhs(): |
| 32 |
mesh = UnitCubeMesh(2, 3, 3) |
| 33 |
V = FunctionSpace(mesh, "Discontinuous Lagrange", 2) |
| 34 |
W = FunctionSpace(mesh, "Lagrange", 2) |
| 35 |
|
| 36 |
u, v = TrialFunction(V), TestFunction(V) |
| 37 |
f = Expression("x[0]*x[0] + x[0]*x[1] + x[1]*x[1]", element=W.ufl_element()) |
| 38 |
|
| 39 |
# Forms for projection |
| 40 |
a, L = inner(v, u)*dx, inner(v, f)*dx |
| 41 |
|
| 42 |
solvers = [LocalSolver.SolverType.LU, LocalSolver.SolverType.Cholesky] |
| 43 |
for solver_type in solvers: |
| 44 |
|
| 45 |
# First solve |
| 46 |
u = Function(V) |
| 47 |
local_solver = LocalSolver(a, L, solver_type) |
| 48 |
local_solver.solve_global_rhs(u) |
| 49 |
error = assemble((u - f)*(u - f)*dx) |
| 50 |
assert round(error, 10) == 0 |
| 51 |
|
| 52 |
# Test cached factorization |
| 53 |
u.vector().zero() |
| 54 |
local_solver.factorize() |
| 55 |
local_solver.solve_global_rhs(u) |
| 56 |
error = assemble((u - f)*(u - f)*dx) |
| 57 |
assert round(error, 10) == 0 |
| 58 |
|
| 59 |
# Clear cache and re-compute |
| 60 |
u.vector().zero() |
| 61 |
local_solver.clear_factorization() |
| 62 |
local_solver.solve_global_rhs(u) |
| 63 |
error = assemble((u - f)*(u - f)*dx) |
| 64 |
assert round(error, 10) == 0 |
| 65 |
|
| 66 |
|
| 67 |
def test_solve_local_rhs(ghost_mode): |
| 68 |
mesh = UnitCubeMesh(1, 5, 1) |
| 69 |
V = FunctionSpace(mesh, "Lagrange", 2) |
| 70 |
W = FunctionSpace(mesh, "Lagrange", 2) |
| 71 |
|
| 72 |
u, v = TrialFunction(V), TestFunction(V) |
| 73 |
f = Constant(10.0) |
| 74 |
|
| 75 |
# Forms for projection |
| 76 |
a, L = inner(v, u)*dx, inner(v, f)*dx |
| 77 |
|
| 78 |
solvers = [LocalSolver.SolverType.LU, LocalSolver.SolverType.Cholesky] |
| 79 |
for solver_type in solvers: |
| 80 |
|
| 81 |
# First solve |
| 82 |
u = Function(V) |
| 83 |
local_solver = LocalSolver(a, L, solver_type) |
| 84 |
local_solver.solve_local_rhs(u) |
| 85 |
x = u.vector().copy() |
| 86 |
x[:] = 10.0 |
| 87 |
assert round((u.vector() - x).norm("l2") - 0.0, 10) == 0 |
| 88 |
|
| 89 |
u.vector().zero() |
| 90 |
local_solver.factorize() |
| 91 |
local_solver.solve_local_rhs(u) |
| 92 |
assert round((u.vector() - x).norm("l2") - 0.0, 10) == 0 |
| 93 |
|
| 94 |
u.vector().zero() |
| 95 |
local_solver.clear_factorization() |
| 96 |
local_solver.solve_local_rhs(u) |
| 97 |
assert round((u.vector() - x).norm("l2") - 0.0, 10) == 0 |
| 98 |
|
| 99 |
|
| 100 |
def test_solve_local_rhs_facet_integrals(ghost_mode): |
| 101 |
mesh = UnitSquareMesh(4, 4) |
| 102 |
|
| 103 |
# Facet function is used here to verify that the proper domains |
| 104 |
# of the rhs are used unlike before where the rhs domains were |
| 105 |
# taken to be the same as the lhs domains |
| 106 |
marker = MeshFunction("size_t", mesh, mesh.topology().dim()-1, 0) |
| 107 |
ds0 = Measure("ds", domain=mesh, subdomain_data=marker, subdomain_id=0) |
| 108 |
|
| 109 |
Vu = VectorFunctionSpace(mesh, 'DG', 1) |
| 110 |
Vv = FunctionSpace(mesh, 'DGT', 1) |
| 111 |
u = TrialFunction(Vu) |
| 112 |
v = TestFunction(Vv) |
| 113 |
|
| 114 |
n = FacetNormal(mesh) |
| 115 |
w = Constant([1, 1]) |
| 116 |
|
| 117 |
a = dot(u, n)*v*ds |
| 118 |
L = dot(w, n)*v*ds0 |
| 119 |
|
| 120 |
for R in '+-': |
| 121 |
a += dot(u(R), n(R))*v(R)*dS |
| 122 |
L += dot(w(R), n(R))*v(R)*dS |
| 123 |
|
| 124 |
u = Function(Vu) |
| 125 |
local_solver = LocalSolver(a, L) |
| 126 |
local_solver.solve_local_rhs(u) |
| 127 |
|
| 128 |
x = u.vector().copy() |
| 129 |
x[:] = 1 |
| 130 |
assert round((u.vector() - x).norm('l2'), 10) == 0 |
| 131 |
|
| 132 |
|
| 133 |
def test_local_solver_dg(ghost_mode): |
| 134 |
mesh = UnitIntervalMesh(50) |
| 135 |
U = FunctionSpace(mesh, "DG", 2) |
| 136 |
|
| 137 |
# Set initial values |
| 138 |
u0 = interpolate(Expression("cos(pi*x[0])", degree=2), U) |
| 139 |
|
| 140 |
# Define test and trial functions |
| 141 |
v, u = TestFunction(U), TrialFunction(U) |
| 142 |
|
| 143 |
# Set time step size |
| 144 |
dt = Constant(2.0e-4) |
| 145 |
|
| 146 |
# Define fluxes on interior and exterior facets |
| 147 |
u_hat = avg(u0) + 0.25*jump(u0) |
| 148 |
u_hatbnd = -u0 + 0.25*(u0 - 1.0) |
| 149 |
|
| 150 |
# Define variational formulation |
| 151 |
a = u*v*dx |
| 152 |
L = (u0*v + dt*u0*v.dx(0))*dx - dt*u_hat*jump(v)*dS - dt*u_hatbnd*v*ds |
| 153 |
|
| 154 |
# Compute reference solution with global LU solver |
| 155 |
u_lu = Function(U) |
| 156 |
solve(a == L, u_lu, solver_parameters = {"linear_solver" : "lu"}) |
| 157 |
|
| 158 |
# Compute solution with local solver and compare |
| 159 |
local_solver = LocalSolver(a, L) |
| 160 |
u_ls = Function(U) |
| 161 |
local_solver.solve_global_rhs(u_ls) |
| 162 |
assert round((u_lu.vector() - u_ls.vector()).norm("l2"), 12) == 0 |
| 163 |
|
| 164 |
# Compute solution with local solver (Cholesky) and compare |
| 165 |
local_solver = LocalSolver(a, L, LocalSolver.SolverType.Cholesky) |
| 166 |
u_ls = Function(U) |
| 167 |
local_solver.solve_global_rhs(u_ls) |
| 168 |
assert round((u_lu.vector() - u_ls.vector()).norm("l2"), 12) == 0 |
| 169 |
|
| 170 |
|
| 171 |
def test_solve_local(ghost_mode): |
| 172 |
mesh = UnitIntervalMesh(50) |
| 173 |
U = FunctionSpace(mesh, "DG", 2) |
| 174 |
|
| 175 |
# Set initial values |
| 176 |
u0 = interpolate(Expression("cos(pi*x[0])", degree=2), U) |
| 177 |
|
| 178 |
# Define test and trial functions |
| 179 |
v, u = TestFunction(U), TrialFunction(U) |
| 180 |
|
| 181 |
# Set time step size |
| 182 |
dt = Constant(2.0e-4) |
| 183 |
|
| 184 |
# Define fluxes on interior and exterior facets |
| 185 |
u_hat = avg(u0) + 0.25*jump(u0) |
| 186 |
u_hatbnd = -u0 + 0.25*(u0 - 1.0) |
| 187 |
|
| 188 |
# Define variational formulation |
| 189 |
a = u*v*dx |
| 190 |
L = (u0*v + dt*u0*v.dx(0))*dx - dt*u_hat*jump(v)*dS - dt*u_hatbnd*v*ds |
| 191 |
b = assemble(L) |
| 192 |
|
| 193 |
# Compute reference solution with global LU solver |
| 194 |
u_lu = Function(U) |
| 195 |
solve(a == L, u_lu, solver_parameters = {"linear_solver" : "lu"}) |
| 196 |
|
| 197 |
# Compute solution with local solver and compare |
| 198 |
local_solver = LocalSolver(a) |
| 199 |
u_ls = Function(U) |
| 200 |
local_solver.solve_local(u_ls.vector(), b, U.dofmap()) |
| 201 |
assert round((u_lu.vector() - u_ls.vector()).norm("l2"), 12) == 0 |
| 202 |
|
| 203 |
# Compute solution with local solver (Cholesky) and compare |
| 204 |
local_solver = LocalSolver(a, solver_type=LocalSolver.SolverType.Cholesky) |
| 205 |
u_ls = Function(U) |
| 206 |
local_solver.solve_local(u_ls.vector(), b, U.dofmap()) |
| 207 |
assert round((u_lu.vector() - u_ls.vector()).norm("l2"), 12) == 0 |