| 1 |
"""Unit tests for parameter library""" |
| 2 |
|
| 3 |
# Copyright (C) 2011 Anders Logg |
| 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 |
import os |
| 22 |
|
| 23 |
from dolfin import * |
| 24 |
from dolfin_utils.test import * |
| 25 |
|
| 26 |
xfail = pytest.mark.xfail(strict=True) |
| 27 |
|
| 28 |
@skip_in_parallel |
| 29 |
def test_simple(tempdir): |
| 30 |
|
| 31 |
# Create some parameters |
| 32 |
p0 = Parameters("test") |
| 33 |
p0.add("filename", "foo.txt") |
| 34 |
p0.add("maxiter", 100) |
| 35 |
p0.add("tolerance", 0.001) |
| 36 |
p0.add("monitor_convergence", True) |
| 37 |
|
| 38 |
# Save to file |
| 39 |
f0 = File(os.path.join(tempdir, "test_parameters.xml")) |
| 40 |
f0 << p0 |
| 41 |
|
| 42 |
# Read from file |
| 43 |
p1 = Parameters() |
| 44 |
f1 = File(os.path.join(tempdir, "test_parameters.xml")) |
| 45 |
f1 >> p1 |
| 46 |
|
| 47 |
# Check values |
| 48 |
assert p1.name() == "test" |
| 49 |
assert p1["filename"] == "foo.txt" |
| 50 |
assert p1["maxiter"] == 100 |
| 51 |
assert p1["tolerance"] == 0.001 |
| 52 |
assert p1["monitor_convergence"] == True |
| 53 |
|
| 54 |
@skip_in_parallel |
| 55 |
def test_gzipped_simple(tempdir): |
| 56 |
|
| 57 |
# Create some parameters |
| 58 |
p0 = Parameters("test") |
| 59 |
p0.add("filename", "foo.txt") |
| 60 |
p0.add("maxiter", 100) |
| 61 |
p0.add("tolerance", 0.001) |
| 62 |
p0.add("monitor_convergence", True) |
| 63 |
|
| 64 |
# Save to file |
| 65 |
f0 = File(os.path.join(tempdir, "test_parameters.xml.gz")) |
| 66 |
f0 << p0 |
| 67 |
|
| 68 |
# Read from file |
| 69 |
p1 = Parameters() |
| 70 |
f1 = File(os.path.join(tempdir, "test_parameters.xml.gz")) |
| 71 |
f1 >> p1 |
| 72 |
|
| 73 |
# Check values |
| 74 |
assert p1.name() == "test" |
| 75 |
assert p1["filename"] == "foo.txt" |
| 76 |
assert p1["maxiter"] == 100 |
| 77 |
assert p1["tolerance"] == 0.001 |
| 78 |
assert p1["monitor_convergence"] == True |
| 79 |
|
| 80 |
@skip_in_parallel |
| 81 |
def test_nested(tempdir): |
| 82 |
|
| 83 |
# Create some nested parameters |
| 84 |
p0 = Parameters("test") |
| 85 |
p00 = Parameters("sub0") |
| 86 |
p00.add("filename", "foo.txt") |
| 87 |
p00.add("maxiter", 100) |
| 88 |
p00.add("tolerance", 0.001) |
| 89 |
p00.add("monitor_convergence", True) |
| 90 |
p0.add("foo", "bar") |
| 91 |
p01 = Parameters(p00); |
| 92 |
p01.rename("sub1"); |
| 93 |
p0.add(p00) |
| 94 |
p0.add(p01) |
| 95 |
|
| 96 |
# Save to file |
| 97 |
f0 = File(os.path.join(tempdir, "test_parameters.xml")) |
| 98 |
f0 << p0 |
| 99 |
|
| 100 |
# Read from file |
| 101 |
p1 = Parameters() |
| 102 |
f1 = File(os.path.join(tempdir, "test_parameters.xml")) |
| 103 |
f1 >> p1 |
| 104 |
|
| 105 |
# Check values |
| 106 |
assert p1.name() == "test" |
| 107 |
assert p1["foo"] == "bar" |
| 108 |
assert p1["sub0"]["filename"] == "foo.txt" |
| 109 |
assert p1["sub0"]["maxiter"] == 100 |
| 110 |
assert p1["sub0"]["tolerance"] == 0.001 |
| 111 |
assert p1["sub0"]["monitor_convergence"] == True |
| 112 |
|
| 113 |
@skip_in_parallel |
| 114 |
def test_parameters_update(tempdir): |
| 115 |
p0 = parameters.copy() |
| 116 |
p1 = parameters.copy() |
| 117 |
p1.update(p0) |
| 118 |
|
| 119 |
@skip_in_parallel |
| 120 |
def test_nested_read_existing(tempdir): |
| 121 |
"""Test that we can read in a nested parameter database into |
| 122 |
an existing (and matching) parameter database""" |
| 123 |
|
| 124 |
file = File(os.path.join(tempdir, "test_parameters.xml")) |
| 125 |
file << parameters |
| 126 |
|
| 127 |
p = Parameters("test") |
| 128 |
file >> p |
| 129 |
file >> p |
| 130 |
|
| 131 |
@skip_in_parallel |
| 132 |
def test_solver_parameters(): |
| 133 |
"Test that global parameters are propagated to solvers" |
| 134 |
|
| 135 |
# Record default values so we can change back |
| 136 |
absolute_tolerance = parameters["krylov_solver"]["absolute_tolerance"] |
| 137 |
|
| 138 |
# Set global parameters |
| 139 |
parameters["krylov_solver"]["absolute_tolerance"] = 1.23456 |
| 140 |
|
| 141 |
# Create solvers |
| 142 |
krylov_solver = KrylovSolver() |
| 143 |
|
| 144 |
# Check that parameters propagate to solvers |
| 145 |
assert krylov_solver.parameters["absolute_tolerance"] == 1.23456 |
| 146 |
|
| 147 |
# Reset parameters so that other tests will continue to work |
| 148 |
if absolute_tolerance is not None: |
| 149 |
parameters["krylov_solver"]["absolute_tolerance"] = absolute_tolerance |
| 150 |
|
| 151 |
def test_parse_parameters(): |
| 152 |
|
| 153 |
def check_pset_and_dict_args(pset, pset_dict): |
| 154 |
for k, v in pset_dict.items(): |
| 155 |
assert pset[k] == v |
| 156 |
|
| 157 |
def create_command_line_args(pset_dict): |
| 158 |
# Construct args like: --a=1 --b=2 --c=3 |
| 159 |
command_line_args1 = ["--" + str(k) + "=" + str(v) for k, v in pset_dict.items()] |
| 160 |
# Construct args like: --a 1 --b 2 --c 3 |
| 161 |
command_line_args2 = [str(element) for k in pset_dict for element in (k, pset_dict[k])] |
| 162 |
return command_line_args1, command_line_args2 |
| 163 |
|
| 164 |
pset_dict = { |
| 165 |
"integer": 1, |
| 166 |
"double": 2.5, |
| 167 |
"string": "string", |
| 168 |
"boolean": False |
| 169 |
} |
| 170 |
|
| 171 |
pset = Parameters("test_parse") |
| 172 |
for k, v in pset_dict.items(): |
| 173 |
pset.add(k, v) |
| 174 |
|
| 175 |
command_line_args = create_command_line_args(pset_dict) |
| 176 |
pset.parse([""] + command_line_args[0]) |
| 177 |
check_pset_and_dict_args(pset, pset_dict) |
| 178 |
pset.parse([""] + command_line_args[1]) |
| 179 |
check_pset_and_dict_args(pset, pset_dict) |
| 180 |
|
| 181 |
changed_pset_dict = { |
| 182 |
"integer": 2, |
| 183 |
"double": 4.5, |
| 184 |
"string": "another string", |
| 185 |
"boolean": True |
| 186 |
} |
| 187 |
|
| 188 |
changed_command_line_args = create_command_line_args(changed_pset_dict) |
| 189 |
pset.parse([""] + changed_command_line_args[0]) |
| 190 |
check_pset_and_dict_args(pset, changed_pset_dict) |
| 191 |
pset.parse([""] + changed_command_line_args[1]) |
| 192 |
check_pset_and_dict_args(pset, changed_pset_dict) |
| 193 |
|
| 194 |
@pytest.mark.parametrize('arg_factory', [pytest.param(True, marks=xfail), |
| 195 |
pytest.param(1, marks=xfail), |
| 196 |
pytest.param(1., marks=xfail), |
| 197 |
pytest.param("string", marks=xfail), |
| 198 |
pytest.param("none", marks=xfail), |
| 199 |
pytest.param(parameters, marks=xfail) |
| 200 |
]) |
| 201 |
def test_wrong_parameters_setitem(arg_factory): |
| 202 |
value = arg_factory |
| 203 |
parameters["misplelled"] = arg_factory |
| 204 |
|
| 205 |
@xfail |
| 206 |
def test_wrong_parameters_getitem(arg_factory): |
| 207 |
parameters["misplelled"] |