# Matrix-vector multiplication in weak formulation

**URL:** https://community.freefem.org/t/matrix-vector-multiplication-in-weak-formulation/449
**Category:** General Discussion
**Created:** [May 31, 2020, 3:14pm UTC](https://community.freefem.org/t/matrix-vector-multiplication-in-weak-formulation/449 "2020-05-31T15:14:04Z")
**Posts on this page:** 20
**Page:** 2

<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 27, 2020, 11:37am UTC](https://community.freefem.org/t/matrix-vector-multiplication-in-weak-formulation/449/21 "2020-07-27T11:37:11Z")

</div>

I’m not a TAO expert, you’ll have to try different methods. Also, I still don’t know what you are trying to solve exactly, so it’s difficult for me to give you precise pointers. [SNESSolve](https://www.mcs.anl.gov/petsc/petsc-current/docs/manualpages/SNES/SNESSolve.html) either solves F(x) = 0, or F(x) = b. You are free to put what you want in the output vector, but this acts as an initial guess for nonlinear solves, so it’s best to use something not too far from the solution otherwise your Newton may never converge.

---

<div class="post-metadata">

### Author: ![Lee](https://avatars.discourse-cdn.com/v4/letter/l/bc8723/32.png) [@Lee](https://community.freefem.org/u/Lee)
#### Post date: [July 27, 2020, 6:21pm UTC](https://community.freefem.org/t/matrix-vector-multiplication-in-weak-formulation/449/22 "2020-07-27T18:21:04Z")

</div>

Thank you for your reply, prj.

Here I uploaded my problem using SNESSolve, all the macro and constants are deleted to reduce the program from 2300 lines to 270+ lines. This is a nonlinear problem F(x)=0, the F is given in funcRes part and the correct Jacobian matrix is obtained in funcJ part. Their forms have been proved in my original IPOPT program so don’t worry about that. The solution vector is [u1,u2,u3,eta,eta0], boundary condition is given for u1, u2 and u3, bound Inequality constrain [0,1] is given for eta and eta0. Also, the eta and eta0 are time dependent so their values of previous time step is defined as etaOLD and eta0OLD.[PETSc\_Example.edp](https://community.freefem.org/uploads/short-url/8o382q7ebD2BaoFdGP3KLwtaB3k.edp) (18.0 KB)

Would you please help me point out the incorrect part in this program? Thanke you very much. I’m nont sure whether I transform the code in a correct way.

---

<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 27, 2020, 6:54pm UTC](https://community.freefem.org/t/matrix-vector-multiplication-in-weak-formulation/449/23 "2020-07-27T18:54:33Z")

</div>

Your script doesn’t even run.

---

<div class="post-metadata">

### Author: ![Lee](https://avatars.discourse-cdn.com/v4/letter/l/bc8723/32.png) [@Lee](https://community.freefem.org/u/Lee)
#### Post date: [July 28, 2020, 3:24pm UTC](https://community.freefem.org/t/matrix-vector-multiplication-in-weak-formulation/449/24 "2020-07-28T15:24:56Z")

</div>

Sorry, I don’t have a simplified version that can run, sorry.

---

<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 28, 2020, 3:30pm UTC](https://community.freefem.org/t/matrix-vector-multiplication-in-weak-formulation/449/25 "2020-07-28T15:30:22Z")

</div>

No, I mean that your script is wrong, I can’t even launch it on my machine.

---

<div class="post-metadata">

### Author: ![Lee](https://avatars.discourse-cdn.com/v4/letter/l/bc8723/32.png) [@Lee](https://community.freefem.org/u/Lee)
#### Post date: [July 28, 2020, 3:37pm UTC](https://community.freefem.org/t/matrix-vector-multiplication-in-weak-formulation/449/26 "2020-07-28T15:37:59Z")

</div>

Is the way of adding boundary condition in SNESSolve the same with TAO? If the initial value of boundary nodes are set to meet the requirements of boundary condition, can it be maintained using the constrain of upper and lower bound or using the form as on(1,2,3,4, du1=0,du2=0,du3=0) in varf function?

Thank you for your reply.

---

<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 28, 2020, 7:10pm UTC](https://community.freefem.org/t/matrix-vector-multiplication-in-weak-formulation/449/27 "2020-07-28T19:10:50Z")

</div>

The boundary conditions have to be set in your initial guess.

---

<div class="post-metadata">

### Author: ![Lee](https://avatars.discourse-cdn.com/v4/letter/l/bc8723/32.png) [@Lee](https://community.freefem.org/u/Lee)
#### Post date: [July 29, 2020, 1:29am UTC](https://community.freefem.org/t/matrix-vector-multiplication-in-weak-formulation/449/28 "2020-07-29T01:29:47Z")

</div>

Thans for your timely reply.

I have found that in the definition of funcRes and funcJ, “tgv” is used in the vector and matrix forming such as “Loc = vPb(Wh, Wh, tgv = -2)” in SNESSolve. The tgv in FreeFem++ is always represent 1e30 to implement Dirichlet boundary conditions and in most situation can be neglected.

So is it necessary to define funcRes and funcJ with the form “Loc = vPb(Wh, Wh, tgv = -2)” or with the form in which tgv can be neglected as “Loc = vPb(Wh, Wh)”?

If correct form is Loc = vPb(Wh, Wh, tgv = -2), which value of tgv should be used, -1, -2 or 1e30 and why?

Thank you.

---

<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 29, 2020, 6:23am UTC](https://community.freefem.org/t/matrix-vector-multiplication-in-weak-formulation/449/29 "2020-07-29T06:23:12Z")

</div>

I always advise against using the default value of 10e+30. -1 (or -2 if your problem has homogeneous boundary conditions and is symmetric) is always better when you do iterative algorithms, such as a Newton loop in SNESSolve/TaoSolve.

---

<div class="post-metadata">

### Author: ![Lee](https://avatars.discourse-cdn.com/v4/letter/l/bc8723/32.png) [@Lee](https://community.freefem.org/u/Lee)
#### Post date: [July 29, 2020, 7:05am UTC](https://community.freefem.org/t/matrix-vector-multiplication-in-weak-formulation/449/30 "2020-07-29T07:05:25Z")

</div>

Thanke you. I’m now using tgv=-2 in a SNESSolve iteration.

---

<div class="post-metadata">

### Author: ![Lee](https://avatars.discourse-cdn.com/v4/letter/l/bc8723/32.png) [@Lee](https://community.freefem.org/u/Lee)
#### Post date: [July 31, 2020, 5:53am UTC](https://community.freefem.org/t/matrix-vector-multiplication-in-weak-formulation/449/31 "2020-07-31T05:53:52Z")

</div>

Dear prj

Here I have uploaded a runable simplified version of my problem.

This is a nonlinear problem F(x)=0, the F is given in funcRes part and the correct Jacobian matrix is obtained in funcJ part. Their forms have been proved in my original IPOPT program so don’t worry about that. The solution vector is [u1,u2,u3,eta,eta0], boundary condition is given for u1, u2 and u3, bound Inequality constrain [0,1] is given for eta and eta0. Also, the eta and eta0 are time dependent so their values of previous time step is defined as etaOLD and eta0OLD.

Could you please point out the errors it contains and give me some suggestions on how to correct it? My own version does not run well and can not get the correct results as IPOPT does,the boundary constrain are some times violated in the line search iterations in Newton computation with the default solver option as " _-snes\_monitor -ksp\_converged\_reason -snes\_view -snes\_vi\_monitor -snes\_type vinewtonrsls -snes\_max\_it 1000 -snes\_rtol 1.0e-6 -pc\_type lu_ ". And moreover, if I turn off the line search using **-snes\_linesearch\_type basec** or change the preconditioner, the results converge would fail.

[PETSc\_exa.edp](https://community.freefem.org/uploads/short-url/zIlsMt4WFbyhVmqPYNM7MyVtY3K.edp) (103.6 KB)

Thank you very much.

---

<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 31, 2020, 6:18am UTC](https://community.freefem.org/t/matrix-vector-multiplication-in-weak-formulation/449/32 "2020-07-31T06:18:49Z")

</div>

Your script has 1.5k source lines of code. Start with something small, if it doesn’t give you what you want, I’ll help you debug. Then, incrementally add complexity on top of functioning code.

---

<div class="post-metadata">

### Author: ![Lee](https://avatars.discourse-cdn.com/v4/letter/l/bc8723/32.png) [@Lee](https://community.freefem.org/u/Lee)
#### Post date: [July 31, 2020, 4:01pm UTC](https://community.freefem.org/t/matrix-vector-multiplication-in-weak-formulation/449/33 "2020-07-31T16:01:53Z")

</div>

Thank you. I will try that.

Do you know how to use the PDIPM solver (-tao type pdipm) in TAO? I found it may be the same solver used in IPOPT, but there is not much information about it. I have try several time with the command  
“_TaoSolve(H, J, DJ, uPETSc, xl=lbPETSc, xu=ubPETSc, sparams="-tao\_monitor -tao\_view -tao\_type pdipm -tao\_max\_it 1000 -tao\_gatol 1e-4 -pc\_type lu", HessianRoutine=HJ)_”  
but failed, the iteration can not even start.

---

<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 31, 2020, 5:25pm UTC](https://community.freefem.org/t/matrix-vector-multiplication-in-weak-formulation/449/34 "2020-07-31T17:25:04Z")

</div>

Does it fail on a complicated problem, or on a very basic one? If it’s again on your 1.5k lines of code solver, then it’s not surprise, I guess you have other issues that you need to figure out first. And that’s why I recommend that you start with a much simpler version of your solver and gradually transition into the complete one.

---

<div class="post-metadata">

### Author: ![Lee](https://avatars.discourse-cdn.com/v4/letter/l/bc8723/32.png) [@Lee](https://community.freefem.org/u/Lee)
#### Post date: [August 4, 2020, 4:24pm UTC](https://community.freefem.org/t/matrix-vector-multiplication-in-weak-formulation/449/35 "2020-08-04T16:24:42Z")

</div>

Thanks for your reply.

Do you have a example using Taosolve with Interior-point Newton’s method(_-tao type pdipm_). I couldn’t find a correct form to use Interior-point Newton’s method. I have uploaded a small example use _-tao type pdipm_ but failed. This one is correct use _-tao type bnls_.

Moreover, beside [SNESVINEWTONRSLS](https://www.mcs.anl.gov/petsc/petsc-3.13/docs/manualpages/SNES/SNESVINEWTONRSLS.html#SNESVINEWTONRSLS) and [SNESVINEWTONSSLS](https://www.mcs.anl.gov/petsc/petsc-3.13/docs/manualpages/SNES/SNESVINEWTONSSLS.html#SNESVINEWTONSSLS), there any other SNES solvers can be used for variational equility and bond-constrained problem?

[TAOexample\_use\_pdipm.edp](https://community.freefem.org/uploads/short-url/hZdDvuoK0AtsAQfeAWdZ2FHV6Ah.edp) (5.1 KB)

Thanks for your kindly help.

---

<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: [August 4, 2020, 5:19pm UTC](https://community.freefem.org/t/matrix-vector-multiplication-in-weak-formulation/449/36 "2020-08-04T17:19:09Z")

</div>

[Here](https://github.com/FreeFem/FreeFem-sources/blob/develop/examples/hpddm/toy-Tao-PETSc.edp) is an example that should work with PDIPM.

---

<div class="post-metadata">

### Author: ![Lee](https://avatars.discourse-cdn.com/v4/letter/l/bc8723/32.png) [@Lee](https://community.freefem.org/u/Lee)
#### Post date: [August 4, 2020, 5:38pm UTC](https://community.freefem.org/t/matrix-vector-multiplication-in-weak-formulation/449/37 "2020-08-04T17:38:11Z")

</div>

I have read that.

TaoSolve(H, J, DJ, x, xl = xl, xu = xu, sparams = “-tao\_monitor -tao\_type ipm -pc\_type lu -pc\_factor\_mat\_solver\_type mumps”, HessianRoutine = funcH, EqualityConstraints = funcE, JacobianEquality = funcJE, JE = JE);

In this TaoSolve, are the options “_EqualityConstraints = funcE, JacobianEquality = funcJE, JE = JE_” necessary for IPM solver? My problem is a simple bound problem without other equality constraints (as the uploaded one). How should I form funcE, funcJE and JE with FF++ function “varf”?

And what about the problem on SNES solvers?

Thank you for your timely reply.

---

<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: [August 4, 2020, 6:36pm UTC](https://community.freefem.org/t/matrix-vector-multiplication-in-weak-formulation/449/38 "2020-08-04T18:36:58Z")

</div>

The options are not mandatory, it all depends on what you want to solve. To use a `varf` instead, you can look instead at [this](https://github.com/FreeFem/FreeFem-sources/blob/develop/examples/hpddm/minimal-surface-Tao-2d-PETSc.edp) other example.  
You cannot provide derivatives of constraints with SNES, so it may converge slower, but this is problem specific.

---

<div class="post-metadata">

### Author: ![Lee](https://avatars.discourse-cdn.com/v4/letter/l/bc8723/32.png) [@Lee](https://community.freefem.org/u/Lee)
#### Post date: [August 5, 2020, 3:07am UTC](https://community.freefem.org/t/matrix-vector-multiplication-in-weak-formulation/449/39 "2020-08-05T03:07:56Z")

</div>

[TAOexample\_use\_ipm.edp](https://community.freefem.org/uploads/short-url/cFqK0sku67lkSxR81ZCFFKvi2js.edp) (5.2 KB)

Here I have uploaded a small example using _ipm_ solver. But the error continue to show in the cmd window. The problem is the one which you have helped me to correct it and it’s runable using _bnls_ solver. Could you lend me a hand on how to correct this example using _ipm_ solver with multiple processors?

Thank you very much.

---

<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: [August 5, 2020, 7:12am UTC](https://community.freefem.org/t/matrix-vector-multiplication-in-weak-formulation/449/40 "2020-08-05T07:12:13Z")

</div>

There are probably some bugs with PDIPM (the solver is rather new), I’m trying to figure this out with the Tao development team. In the meantime, if things work with BNLS, why don’t you use it?

[Previous page](https://community.freefem.org/t/matrix-vector-multiplication-in-weak-formulation/449.md?page=1)

[Next page](https://community.freefem.org/t/matrix-vector-multiplication-in-weak-formulation/449.md?page=3)
