Skip to content

Add custom Newton's method example #85

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
build:
# The type of runner that the job will run on
runs-on: ubuntu-latest
container: dokken92/dolfinx_custom:16062022
container: dokken92/dolfinx_custom:02072022

env:
HDF5_MPI: "ON"
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/main-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ jobs:
mpirun -n 3 python3 solvers.py
mpirun -n 3 python3 convergence.py
mpirun -n 3 python3 compiler_parameters.py
mpirun -n 4 python3 newton-solver.py

- name: Test building the book
run:
Expand Down
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

## Dev
- Added a section on custom Newton-solvers, see [chapter4/newton-solver].
- Various minor DOLFINx API updates. `dolfinx.mesh.compute_boundary_facets` -> `dolfinx.mesh.exterior_facet_indices` with slightly different functionality. Use `dolfinx.mesh.MeshTagsMetaClass.find` instead of `mt.indices[mt.values==value]`.
- Various numpy updates, use `np.full_like`.
- Change all notebooks to use [jupytext](https://jupytext.readthedocs.io/en/latest/install.html) to automatically sync `.ipynb` with `.py` files.
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM dokken92/dolfinx_custom:16062022
FROM dokken92/dolfinx_custom:02072022

# create user with a home directory
ARG NB_USER
Expand Down
1 change: 1 addition & 0 deletions _toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,4 @@ parts:
- file: chapter4/solvers
- file: chapter4/compiler_parameters
- file: chapter4/convergence
- file: chapter4/newton-solver
58 changes: 29 additions & 29 deletions chapter2/nonlinpoisson_code.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Newton's method\n",
"The next step is to define the non-linear problem. As it is non-linear we will use [Newtons method](https://en.wikipedia.org/wiki/Newton%27s_method).\n",
"For details about how to implement a Newton solver, see [Custom Newton solvers](../chapter4/newton-solver.ipynb).\n",
"Newton's method requires methods for evaluating the residual `F` (including application of boundary conditions), as well as a method for computing the Jacobian matrix. DOLFINx provides the function `NonlinearProblem` that implements these methods. In addition to the boundary conditions, you can supply the variational form for the Jacobian (computed if not supplied), and form and jit parameters, see the [JIT parameters section](../chapter4/compiler_parameters.ipynb)."
]
},
Expand Down Expand Up @@ -161,22 +163,22 @@
"name": "stderr",
"output_type": "stream",
"text": [
"2022-06-16 22:08:51.665 ( 0.569s) [main ] petsc.cpp:677 INFO| PETSc Krylov solver starting to solve system.\n",
"2022-06-16 22:08:51.666 ( 0.570s) [main ] petsc.cpp:677 INFO| PETSc Krylov solver starting to solve system.\n",
"2022-06-16 22:08:51.668 ( 0.571s) [main ] NewtonSolver.cpp:36 INFO| Newton iteration 2: r (abs) = 20.3795 (tol = 1e-10) r (rel) = 0.922547(tol = 1e-06)\n",
"2022-06-16 22:08:51.668 ( 0.572s) [main ] petsc.cpp:677 INFO| PETSc Krylov solver starting to solve system.\n",
"2022-06-16 22:08:51.668 ( 0.572s) [main ] NewtonSolver.cpp:36 INFO| Newton iteration 3: r (abs) = 6.95284 (tol = 1e-10) r (rel) = 0.314744(tol = 1e-06)\n",
"2022-06-16 22:08:51.669 ( 0.572s) [main ] petsc.cpp:677 INFO| PETSc Krylov solver starting to solve system.\n",
"2022-06-16 22:08:51.669 ( 0.573s) [main ] NewtonSolver.cpp:36 INFO| Newton iteration 4: r (abs) = 2.93579 (tol = 1e-10) r (rel) = 0.132899(tol = 1e-06)\n",
"2022-06-16 22:08:51.670 ( 0.573s) [main ] petsc.cpp:677 INFO| PETSc Krylov solver starting to solve system.\n",
"2022-06-16 22:08:51.670 ( 0.574s) [main ] NewtonSolver.cpp:36 INFO| Newton iteration 5: r (abs) = 0.700635 (tol = 1e-10) r (rel) = 0.0317166(tol = 1e-06)\n",
"2022-06-16 22:08:51.670 ( 0.574s) [main ] petsc.cpp:677 INFO| PETSc Krylov solver starting to solve system.\n",
"2022-06-16 22:08:51.671 ( 0.575s) [main ] NewtonSolver.cpp:36 INFO| Newton iteration 6: r (abs) = 0.0490873 (tol = 1e-10) r (rel) = 0.0022221(tol = 1e-06)\n",
"2022-06-16 22:08:51.671 ( 0.575s) [main ] petsc.cpp:677 INFO| PETSc Krylov solver starting to solve system.\n",
"2022-06-16 22:08:51.672 ( 0.576s) [main ] NewtonSolver.cpp:36 INFO| Newton iteration 7: r (abs) = 0.00029971 (tol = 1e-10) r (rel) = 1.35674e-05(tol = 1e-06)\n",
"2022-06-16 22:08:51.672 ( 0.576s) [main ] petsc.cpp:677 INFO| PETSc Krylov solver starting to solve system.\n",
"2022-06-16 22:08:51.673 ( 0.577s) [main ] NewtonSolver.cpp:36 INFO| Newton iteration 8: r (abs) = 1.51622e-08 (tol = 1e-10) r (rel) = 6.86368e-10(tol = 1e-06)\n",
"2022-06-16 22:08:51.673 ( 0.577s) [main ] NewtonSolver.cpp:255 INFO| Newton solver finished in 8 iterations and 90 linear solver iterations.\n"
"2022-06-29 20:29:08.231 ( 0.120s) [main ] petsc.cpp:677 INFO| PETSc Krylov solver starting to solve system.\n",
"2022-06-29 20:29:08.232 ( 0.122s) [main ] petsc.cpp:677 INFO| PETSc Krylov solver starting to solve system.\n",
"2022-06-29 20:29:08.233 ( 0.123s) [main ] NewtonSolver.cpp:36 INFO| Newton iteration 2: r (abs) = 20.3795 (tol = 1e-10) r (rel) = 0.922547(tol = 1e-06)\n",
"2022-06-29 20:29:08.234 ( 0.123s) [main ] petsc.cpp:677 INFO| PETSc Krylov solver starting to solve system.\n",
"2022-06-29 20:29:08.235 ( 0.124s) [main ] NewtonSolver.cpp:36 INFO| Newton iteration 3: r (abs) = 6.95284 (tol = 1e-10) r (rel) = 0.314744(tol = 1e-06)\n",
"2022-06-29 20:29:08.235 ( 0.124s) [main ] petsc.cpp:677 INFO| PETSc Krylov solver starting to solve system.\n",
"2022-06-29 20:29:08.236 ( 0.125s) [main ] NewtonSolver.cpp:36 INFO| Newton iteration 4: r (abs) = 2.93579 (tol = 1e-10) r (rel) = 0.132899(tol = 1e-06)\n",
"2022-06-29 20:29:08.236 ( 0.125s) [main ] petsc.cpp:677 INFO| PETSc Krylov solver starting to solve system.\n",
"2022-06-29 20:29:08.237 ( 0.126s) [main ] NewtonSolver.cpp:36 INFO| Newton iteration 5: r (abs) = 0.700635 (tol = 1e-10) r (rel) = 0.0317166(tol = 1e-06)\n",
"2022-06-29 20:29:08.237 ( 0.126s) [main ] petsc.cpp:677 INFO| PETSc Krylov solver starting to solve system.\n",
"2022-06-29 20:29:08.238 ( 0.127s) [main ] NewtonSolver.cpp:36 INFO| Newton iteration 6: r (abs) = 0.0490873 (tol = 1e-10) r (rel) = 0.0022221(tol = 1e-06)\n",
"2022-06-29 20:29:08.238 ( 0.127s) [main ] petsc.cpp:677 INFO| PETSc Krylov solver starting to solve system.\n",
"2022-06-29 20:29:08.239 ( 0.128s) [main ] NewtonSolver.cpp:36 INFO| Newton iteration 7: r (abs) = 0.00029971 (tol = 1e-10) r (rel) = 1.35674e-05(tol = 1e-06)\n",
"2022-06-29 20:29:08.239 ( 0.128s) [main ] petsc.cpp:677 INFO| PETSc Krylov solver starting to solve system.\n",
"2022-06-29 20:29:08.240 ( 0.129s) [main ] NewtonSolver.cpp:36 INFO| Newton iteration 8: r (abs) = 1.51622e-08 (tol = 1e-10) r (rel) = 6.86368e-10(tol = 1e-06)\n",
"2022-06-29 20:29:08.240 ( 0.129s) [main ] NewtonSolver.cpp:255 INFO| Newton solver finished in 8 iterations and 90 linear solver iterations.\n"
]
}
],
Expand Down Expand Up @@ -214,16 +216,16 @@
"name": "stderr",
"output_type": "stream",
"text": [
"2022-06-16 22:08:51.682 ( 0.586s) [main ]topologycomputation.cpp:746 INFO| Computing mesh entities of dimension 0\n",
"2022-06-16 22:08:51.682 ( 0.586s) [main ]topologycomputation.cpp:746 INFO| Computing mesh entities of dimension 1\n",
"2022-06-16 22:08:51.682 ( 0.586s) [main ] MPI.cpp:154 INFO| Computing communicaton graph edges (using NBX algorithm). Number of input edges: 0\n",
"2022-06-16 22:08:51.682 ( 0.586s) [main ] MPI.cpp:217 INFO| Finished graph edge discovery using NBX algorithm. Number of discovered edges 0\n",
"2022-06-16 22:08:51.682 ( 0.586s) [main ] MPI.cpp:154 INFO| Computing communicaton graph edges (using NBX algorithm). Number of input edges: 0\n",
"2022-06-16 22:08:51.682 ( 0.586s) [main ] MPI.cpp:217 INFO| Finished graph edge discovery using NBX algorithm. Number of discovered edges 0\n",
"2022-06-16 22:08:51.682 ( 0.586s) [main ] MPI.cpp:154 INFO| Computing communicaton graph edges (using NBX algorithm). Number of input edges: 0\n",
"2022-06-16 22:08:51.682 ( 0.586s) [main ] MPI.cpp:217 INFO| Finished graph edge discovery using NBX algorithm. Number of discovered edges 0\n",
"2022-06-16 22:08:51.683 ( 0.586s) [main ] MPI.cpp:154 INFO| Computing communicaton graph edges (using NBX algorithm). Number of input edges: 0\n",
"2022-06-16 22:08:51.683 ( 0.586s) [main ] MPI.cpp:217 INFO| Finished graph edge discovery using NBX algorithm. Number of discovered edges 0\n"
"2022-06-29 20:29:08.252 ( 0.141s) [main ]topologycomputation.cpp:746 INFO| Computing mesh entities of dimension 0\n",
"2022-06-29 20:29:08.252 ( 0.141s) [main ]topologycomputation.cpp:746 INFO| Computing mesh entities of dimension 1\n",
"2022-06-29 20:29:08.252 ( 0.141s) [main ] MPI.cpp:154 INFO| Computing communicaton graph edges (using NBX algorithm). Number of input edges: 0\n",
"2022-06-29 20:29:08.252 ( 0.141s) [main ] MPI.cpp:217 INFO| Finished graph edge discovery using NBX algorithm. Number of discovered edges 0\n",
"2022-06-29 20:29:08.252 ( 0.141s) [main ] MPI.cpp:154 INFO| Computing communicaton graph edges (using NBX algorithm). Number of input edges: 0\n",
"2022-06-29 20:29:08.252 ( 0.141s) [main ] MPI.cpp:217 INFO| Finished graph edge discovery using NBX algorithm. Number of discovered edges 0\n",
"2022-06-29 20:29:08.252 ( 0.141s) [main ] MPI.cpp:154 INFO| Computing communicaton graph edges (using NBX algorithm). Number of input edges: 0\n",
"2022-06-29 20:29:08.252 ( 0.141s) [main ] MPI.cpp:217 INFO| Finished graph edge discovery using NBX algorithm. Number of discovered edges 0\n",
"2022-06-29 20:29:08.252 ( 0.141s) [main ] MPI.cpp:154 INFO| Computing communicaton graph edges (using NBX algorithm). Number of input edges: 0\n",
"2022-06-29 20:29:08.252 ( 0.141s) [main ] MPI.cpp:217 INFO| Finished graph edge discovery using NBX algorithm. Number of discovered edges 0\n"
]
}
],
Expand All @@ -246,9 +248,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"lines_to_next_cell": 2
},
"metadata": {},
"outputs": [],
"source": []
}
Expand Down
3 changes: 2 additions & 1 deletion chapter2/nonlinpoisson_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ def q(u):
v = ufl.TestFunction(V)
F = q(uh)*ufl.dot(ufl.grad(uh), ufl.grad(v))*ufl.dx - f*v*ufl.dx

# ## Newton's method
# The next step is to define the non-linear problem. As it is non-linear we will use [Newtons method](https://en.wikipedia.org/wiki/Newton%27s_method).
# For details about how to implement a Newton solver, see [Custom Newton solvers](../chapter4/newton-solver.ipynb).
# Newton's method requires methods for evaluating the residual `F` (including application of boundary conditions), as well as a method for computing the Jacobian matrix. DOLFINx provides the function `NonlinearProblem` that implements these methods. In addition to the boundary conditions, you can supply the variational form for the Jacobian (computed if not supplied), and form and jit parameters, see the [JIT parameters section](../chapter4/compiler_parameters.ipynb).

problem = fem.petsc.NonlinearProblem(F, uh, bcs=[bc])
Expand Down Expand Up @@ -107,4 +109,3 @@ def q(u):
print(f"Error_max: {error_max:.2e}")
# -


Loading