Skip to content

Commit

Permalink
Allow setting a non-zero initial guess for nonlinear solver in tutori…
Browse files Browse the repository at this point in the history
…al 17
  • Loading branch information
francesco-ballarin committed Jul 25, 2023
1 parent c59a611 commit aa96358
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion tutorials/17_navier_stokes.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,20 @@
" \"\"\"Return the boundary conditions for the problem.\"\"\"\n",
" return self._bcs\n",
"\n",
" def create_snes_solution(self) -> petsc4py.PETSc.Vec: # type: ignore[no-any-unimported]\n",
" \"\"\"\n",
" Create a petsc4py.PETSc.Vec to be passed to petsc4py.PETSc.SNES.solve.\n",
"\n",
" The returned vector will be initialized with the initial guesses provided in `self._solutions`,\n",
" properly stacked together in a single block vector.\n",
" \"\"\"\n",
" x = dolfinx.fem.petsc.create_vector_block(self._residual_cpp)\n",
" with multiphenicsx.fem.petsc.BlockVecSubVectorWrapper(x, [self._VQ[0].dofmap, self._VQ[1].dofmap]) as x_wrapper:\n",
" for x_wrapper_local, sub_solution in zip(x_wrapper, self._solutions):\n",
" with sub_solution.vector.localForm() as sub_solution_local:\n",
" x_wrapper_local[:] = sub_solution_local\n",
" return x\n",
"\n",
" def update_solutions(self, x: petsc4py.PETSc.Vec) -> None: # type: ignore[no-any-unimported]\n",
" \"\"\"Update `self._solutions` with data in `x`.\"\"\"\n",
" x.ghostUpdate(addv=petsc4py.PETSc.InsertMode.INSERT, mode=petsc4py.PETSc.ScatterMode.FORWARD)\n",
Expand Down Expand Up @@ -292,7 +306,7 @@
" jacobian_mat = dolfinx.fem.petsc.create_matrix_block(self._jacobian_cpp)\n",
" snes.setJacobian(self._assemble_jacobian, J=jacobian_mat, P=None)\n",
" snes.setMonitor(lambda _, it, residual: print(it, residual))\n",
" solution = dolfinx.fem.petsc.create_vector_block(self._residual_cpp)\n",
" solution = self.create_snes_solution()\n",
" snes.solve(None, solution)\n",
" self.update_solutions(solution)\n",
" residual_vec.destroy()\n",
Expand Down

0 comments on commit aa96358

Please sign in to comment.