3
"""Unit tests for the RKSolver interface"""
5
# Copyright (C) 2013 Johan Hake
7
# This file is part of DOLFIN.
9
# DOLFIN is free software: you can redistribute it and/or modify
10
# it under the terms of the GNU Lesser General Public License as published by
11
# the Free Software Foundation, either version 3 of the License, or
12
# (at your option) any later version.
14
# DOLFIN is distributed in the hope that it will be useful,
15
# but WITHOUT ANY WARRANTY; without even the implied warranty of
16
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
# GNU Lesser General Public License for more details.
19
# You should have received a copy of the GNU Lesser General Public License
20
# along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
22
from __future__ import print_function
28
from dolfin_utils.test import *
31
def convergence_order(errors, base = 2):
33
orders = [0.0] * (len(errors)-1)
34
for i in range(len(errors)-1):
36
orders[i] = math.log(errors[i]/errors[i+1], base)
37
except ZeroDivisionError:
43
@skip_64bit_int # The linear solver can fail wit 64-bit indices
45
def test_butcher_schemes_scalar():
47
LEVEL = cpp.get_log_level()
48
cpp.set_log_level(cpp.WARNING)
49
mesh = UnitSquareMesh(4, 4)
51
V = FunctionSpace(mesh, "R", 0)
57
u_true = Expression("exp(t)", t=tstop)
59
for Scheme in [ForwardEuler, ExplicitMidPoint, RK4,
60
BackwardEuler, CN2, ESDIRK3, ESDIRK4]:
61
scheme = Scheme(form, u)
62
solver = RKSolver(scheme)
64
for dt in [0.05, 0.025, 0.0125]:
65
u.interpolate(Constant(1.0))
66
solver.step_interval(0., tstop, dt)
67
u_errors.append(u_true(0.0, 0.0) - u(0.0, 0.0))
69
assert scheme.order()-min(convergence_order(u_errors))<0.1
71
cpp.set_log_level(LEVEL)
76
def test_butcher_schemes_vector():
78
LEVEL = cpp.get_log_level()
79
cpp.set_log_level(cpp.WARNING)
80
mesh = UnitSquareMesh(4, 4)
82
V = VectorFunctionSpace(mesh, "R", 0, dim=2)
85
form = inner(as_vector((-u[1], u[0])), v)*dx
88
u_true = Expression(("cos(t)", "sin(t)"), t=tstop)
90
for Scheme in [ForwardEuler, ExplicitMidPoint, RK4,
91
BackwardEuler, CN2, ESDIRK3, ESDIRK4]:
92
scheme = Scheme(form, u)
93
solver = RKSolver(scheme)
96
for dt in [0.05, 0.025, 0.0125]:
97
u.interpolate(Constant((1.0, 0.0)))
98
solver.step_interval(0., tstop, dt)
99
u_errors_0.append(u_true(0.0, 0.0)[0] - u(0.0, 0.0)[0])
100
u_errors_1.append(u_true(0.0, 0.0)[1] - u(0.0, 0.0)[1])
102
assert scheme.order()-min(convergence_order(u_errors_0))<0.1
103
assert scheme.order()-min(convergence_order(u_errors_1))<0.1
105
cpp.set_log_level(LEVEL)