| 1 |
"""This demo demonstrates the DOLFIN parameter system. |
| 2 |
|
| 3 |
Try running this demo with |
| 4 |
|
| 5 |
python demo.py --bar 1 --solver_parameters.max_iterations 1000 |
| 6 |
""" |
| 7 |
|
| 8 |
# Copyright (C) 2009 Johan Hake and Anders Logg |
| 9 |
# |
| 10 |
# This file is part of DOLFIN. |
| 11 |
# |
| 12 |
# DOLFIN is free software: you can redistribute it and/or modify |
| 13 |
# it under the terms of the GNU Lesser General Public License as published by |
| 14 |
# the Free Software Foundation, either version 3 of the License, or |
| 15 |
# (at your option) any later version. |
| 16 |
# |
| 17 |
# DOLFIN is distributed in the hope that it will be useful, |
| 18 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 |
# GNU Lesser General Public License for more details. |
| 21 |
# |
| 22 |
# You should have received a copy of the GNU Lesser General Public License |
| 23 |
# along with DOLFIN. If not, see <http://www.gnu.org/licenses/>. |
| 24 |
# |
| 25 |
# First added: 2009-09-06 |
| 26 |
# Last changed: 2009-12-08 |
| 27 |
|
| 28 |
|
| 29 |
from dolfin import * |
| 30 |
|
| 31 |
#--- Demo of global DOLFIN parameters --- |
| 32 |
|
| 33 |
MPI.init() |
| 34 |
|
| 35 |
# Set some global DOLFIN parameters |
| 36 |
parameters["linear_algebra_backend"] = "Eigen" |
| 37 |
|
| 38 |
# Print global DOLFIN parameters |
| 39 |
info(parameters, True) |
| 40 |
print() |
| 41 |
|
| 42 |
# Read parameters from file |
| 43 |
file = File("parameters.xml") |
| 44 |
parameters = Parameters("parameters") |
| 45 |
file >> parameters |
| 46 |
info(parameters, True) |
| 47 |
print() |
| 48 |
|
| 49 |
#--- Demo of nested parameter sets --- |
| 50 |
|
| 51 |
# Create an application parameter set |
| 52 |
application_parameters = Parameters("application_parameters") |
| 53 |
|
| 54 |
# Create application parameters |
| 55 |
application_parameters.add("foo", 1.0) |
| 56 |
application_parameters.add("bar", 100) |
| 57 |
application_parameters.add("baz", False) |
| 58 |
application_parameters.add("pc", "amg") |
| 59 |
|
| 60 |
# Create a solver parameter set |
| 61 |
solver_parameters = Parameters("solver_parameters") |
| 62 |
|
| 63 |
# Create solver parameters |
| 64 |
solver_parameters.add("max_iterations", 100) |
| 65 |
solver_parameters.add("tolerance", 1e-16) |
| 66 |
solver_parameters.add("relative_tolerance", 1e-16, 1e-16, 1.0) |
| 67 |
|
| 68 |
# Set range for parameter |
| 69 |
solver_parameters.set_range("max_iterations", 0, 1000) |
| 70 |
|
| 71 |
# Set some parameter values |
| 72 |
solver_parameters["max_iterations"] = 500 |
| 73 |
solver_parameters["relative_tolerance"] = 0.1 |
| 74 |
|
| 75 |
# Set solver parameters as nested parameters of application parameters |
| 76 |
application_parameters.add(solver_parameters) |
| 77 |
|
| 78 |
# Parse command-line options |
| 79 |
application_parameters.parse() |
| 80 |
|
| 81 |
# Access parameter values |
| 82 |
foo = application_parameters["foo"] |
| 83 |
bar = application_parameters["bar"] |
| 84 |
tol = application_parameters["solver_parameters"]["tolerance"] |
| 85 |
|
| 86 |
# Print parameter values |
| 87 |
print("foo =", foo) |
| 88 |
print("bar =", bar) |
| 89 |
print("tol =", tol) |
| 90 |
print() |
| 91 |
|
| 92 |
# Print application parameters |
| 93 |
info(application_parameters, True) |
| 94 |
print() |
| 95 |
|
| 96 |
#--- Demo of Krylov solver parameters --- |
| 97 |
|
| 98 |
# Set a parameter for the Krylov solver |
| 99 |
solver = KrylovSolver() |
| 100 |
solver.parameters["relative_tolerance"] = 1e-20 |
| 101 |
|
| 102 |
# Print Krylov solver parameters |
| 103 |
info(solver.parameters, True) |
| 104 |
print() |
| 105 |
|
| 106 |
#--- Demo of updating a parameter set --- |
| 107 |
|
| 108 |
# Create a subset of the application parameter set |
| 109 |
parameter_subset = Parameters("parameter_subset") |
| 110 |
parameter_subset.add("foo", 3.0) |
| 111 |
nested_subset = Parameters("solver_parameters") |
| 112 |
nested_subset.add("max_iterations", 850) |
| 113 |
parameter_subset.add(nested_subset) |
| 114 |
|
| 115 |
# Update application parameters |
| 116 |
application_parameters.update(parameter_subset) |
| 117 |
info(application_parameters, True) |
| 118 |
|
| 119 |
# Can also update using a dictionary |
| 120 |
parameter_subset = dict(foo = 1.5, solver_parameters = dict(max_iterations = 50)) |
| 121 |
application_parameters.update(parameter_subset) |
| 122 |
|
| 123 |
# Or like this |
| 124 |
parameter_subset = {"foo": 1.5, "solver_parameters": {"max_iterations": 50}} |
| 125 |
application_parameters.update(parameter_subset) |
| 126 |
|
| 127 |
print("Features below not implemented in pybind11") |
| 128 |
exit() |
| 129 |
|
| 130 |
# Print command-line option string |
| 131 |
print("\nCommand-line option string") |
| 132 |
print(application_parameters.option_string()) |
| 133 |
|
| 134 |
# Demostrate access to full info of parameters |
| 135 |
def print_data(par, indent=""): |
| 136 |
print("\n" + indent + "Content of:", par.name()) |
| 137 |
for key, par_info in par.iterdata(): |
| 138 |
if isinstance(par_info,Parameters): |
| 139 |
print_data(par_info, indent + " ") |
| 140 |
else: |
| 141 |
print(indent + key, par_info) |
| 142 |
|
| 143 |
print_data(application_parameters) |
| 144 |
|
| 145 |
# Direct creation of application parameter set |
| 146 |
new_application_parameters = Parameters( |
| 147 |
"application_parameters", |
| 148 |
foo = 1.0, |
| 149 |
bar = 100, |
| 150 |
pc = "amg", |
| 151 |
solver_parameters = Parameters( |
| 152 |
"solver", |
| 153 |
max_iterations = 100, |
| 154 |
tolerance = 1e-16, |
| 155 |
relative_tolerance = (1e-16, 1e-16, 1.0), |
| 156 |
pcs = ("ilu", ["ilu","amg","icc","sor"]) |
| 157 |
) |
| 158 |
) |