# Non-linear Conjugate Gradient with DDM and PETSc

**URL:** <https://community.freefem.org/t/non-linear-conjugate-gradient-with-ddm-and-petsc/515>\
**Category:** General Discussion\
**Created:** [July 22, 2020, 8:22pm UTC](https://community.freefem.org/t/non-linear-conjugate-gradient-with-ddm-and-petsc/515 "2020-07-22T20:22:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![julienG](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/julieng/32/222_2.png) [@julienG](https://community.freefem.org/u/julienG)\
**Post date:** [July 22, 2020, 8:22pm UTC](https://community.freefem.org/t/non-linear-conjugate-gradient-with-ddm-and-petsc/515/1 "2020-07-22T20:22:47Z")

</div>

Hi all,

I have been interested in using a Non-Linear Conjugate Gradient (NLCG) method for minimizing non-linear problems and using DDM/PETSc tools. As discussed [here](https://github.com/FreeFem/FreeFem-sources/blob/develop/examples/hpddm/minimal-surface-Tao-2d-PETSc.edp), one can use for example TAO using the flags `-tao_type cg`.  
I wrote (some times ago, with the help of @prj ) an alternative NLCG set of macros for hpddm/petsc that more or less reproduce the codes in the file [src/femlib/CGNL.hpp](https://github.com/FreeFem/FreeFem-sources/blob/develop/src/femlib/CGNL.hpp). The main difference with TAO routine is that TAO requires evaluations of the functional to be minimized, while the NLCG here requires only to evaluate the variations… I ran few examples and find that the code I wrote is typically as fast as TAO but with a (slightly) better convergence rate… So I think it is worth sharing with the community.

The solver requires two files:  
[ddm-ARGMIN.idp](https://community.freefem.org/uploads/short-url/9fvmkbNFX1LHAGYDjwufAcUalHh.idp) (2.8 KB) (the macro for the line search)  
[ddm-NLCG.idp](https://community.freefem.org/uploads/short-url/4NB2BujIgi8GbEsOA4JB78Qnxwt.idp) (7.8 KB) (the Non-Linear Conjugate Gradient macros)

I cooked up various examples for non-linear problems. I think a simple example is the static “kink” solution of the [Sine-Gordon equation](https://en.wikipedia.org/wiki/Sine-Gordon_equation) (the solution is called a _topological soliton_, and the analytic solution is known…). The code that constructs the kink is [hpddm-petsc-NLCG.edp](https://community.freefem.org/uploads/short-url/hCbsuiMxVRCsHZfnN0x1KgpNcUn.edp) (4.3 KB) . I tried to make it quite documented.  
Also the code [hpddm-petsc-NLCG-vs-TAO.edp](https://community.freefem.org/uploads/short-url/aV25PPZZe4ghbnlwOjEpUx7zNKK.edp) (5.5 KB) solves the same problem and compares results and execution time with the TAO implementation…

Please let me know if you have some feed back to improve the code, or if some things are not clear. Also, if it is of interest, I can write more detailed documentation on the background problem, or about various other non-linear problems…

---

<div class="post-metadata">

**Author:** ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)\
**Post date:** [July 22, 2020, 8:26pm UTC](https://community.freefem.org/t/non-linear-conjugate-gradient-with-ddm-and-petsc/515/2 "2020-07-22T20:26:19Z")

</div>

Just FYI, NLCG is implemented as a SNES as well in PETSc, see [SNESNCG](https://www.mcs.anl.gov/petsc/petsc-current/docs/manualpages/SNES/SNESNCG.html). Did you try that as well?

---

<div class="post-metadata">

**Author:** ![julienG](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/julieng/32/222_2.png) [@julienG](https://community.freefem.org/u/julienG)\
**Post date:** [July 22, 2020, 8:43pm UTC](https://community.freefem.org/t/non-linear-conjugate-gradient-with-ddm-and-petsc/515/3 "2020-07-22T20:43:37Z")

</div>

I didn’t know about that. I’ll have a look at it. thanks for the suggestion
