# Nonlinear PDE + scalar constraint with PETSc?

**URL:** <https://community.freefem.org/t/nonlinear-pde-scalar-constraint-with-petsc/2810>\
**Category:** General Discussion\
**Created:** [November 22, 2023, 8:49pm UTC](https://community.freefem.org/t/nonlinear-pde-scalar-constraint-with-petsc/2810 "2023-11-22T20:49:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![etj](https://avatars.discourse-cdn.com/v4/letter/e/a698b9/32.png) [@etj](https://community.freefem.org/u/etj)\
**Post date:** [November 22, 2023, 8:49pm UTC](https://community.freefem.org/t/nonlinear-pde-scalar-constraint-with-petsc/2810/1 "2023-11-22T20:49:01Z")

</div>

Hello,

I would like to solve a nonlinear PDE together with an additional scalar constraint (involving some integral of the PDE solution).

I have a working serial code, where I assemble a block matrix and use a “hand-coded” Newton method (iteratively solving the linearized problem with something like `x=A^-1*rhs`).  
Now I would like to go parallel. I see two methods and, in case both are possible, I am wondering which one would be easier and/or more efficient:

1. use a “hand-coded” Newton method (with iterations of the linear solver `KSPSolve`, somewhat similar to the constrained but linear example in laplace-lagrange-PETSc.edp),
2. use directly the nonlinear solver `SNESSolve` (similar to the nonlinear but non-constrained example in navier-stokes-2d-PETSc.edp).

Is method 1 much worse than method 2?

For method 2, I have some trouble defining the arguments to be given to `SNESSolve` (the equivalent of `J`, `funcJ` and `funcRes` in navier-stokes-2d-PETSc.edp). Specifically, how should one properly resize, change numbering, and assemble/extract the blocks?  
Is there by chance a working example somewhere?

Thank you in advance for your help!

---

<div class="post-metadata">

**Author:** ![cmd](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/cmd/32/67_2.png) [@cmd](https://community.freefem.org/u/cmd)\
**Post date:** [November 23, 2023, 12:41pm UTC](https://community.freefem.org/t/nonlinear-pde-scalar-constraint-with-petsc/2810/2 "2023-11-23T12:41:58Z")

</div>

Hi there! Check out [this thread](https://community.freefem.org/t/snes-for-bordered-block-matrix/2193/16).
