# 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:** 12
**Page:** 3

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

</div>

Thank you for timely reply.

When BNLS is used in my original problem, it’s very hard to converge in the second time step iteration. The residual output information indicate the iterations seems stuck in a small interval and repeat again and again. This phenomenon also appears in IPOPT, but once I turn off line search with command _linesearch=false_, the IPOPT can give correct results.

The results for the first time step is almost the same with IPOPT. Since IPOPT uses the primal-dual interior point method, I think maybe _ipm_ or _PDIPM_ may help to retrieve the correct IPOPT results. If _PDIPM_ is not readily available, how about _ipm_?

Meanwhile, I’m trying other Tao solvers, but till now, the problem is not solved.

---

<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, 12:36pm UTC](https://community.freefem.org/t/matrix-vector-multiplication-in-weak-formulation/449/42 "2020-08-05T12:36:11Z")

</div>

You can try both IPM and PDIPM, I was just implying that you may run into a bug. But there are indeed many available options, I don’t really know what you are solving and I’m not really a Tao expert, so I’m not very useful.

---

<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, 1:19pm UTC](https://community.freefem.org/t/matrix-vector-multiplication-in-weak-formulation/449/43 "2020-08-05T13:19:03Z")

</div>

I tried to use IPM in a much smaller bound constrained example, but it can not run. So I uploaded the example (as the TAOexample\_use\_ipm.edp above) if you can help me point out the error.

Thank you very much.

---

<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 7, 2020, 7:42am UTC](https://community.freefem.org/t/matrix-vector-multiplication-in-weak-formulation/449/44 "2020-08-07T07:42:19Z")

</div>

Dear prj

Here I have build a small example using tao\_ipm solver. But the error continue to show in the cmd window as:

“[0]IPETSC ERROR: Caught signal number 11 SEGV: Segementation Violation, probably memory access out of range”

I totally have no idea how to fix it. Could you please help me correct it? The edp is uploaded as follows.  
[ipmExa.edp](https://community.freefem.org/uploads/short-url/a4UHsRf5IUkvFH2hgpZEsNutytp.edp) (5.9 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: [August 7, 2020, 12:37pm UTC](https://community.freefem.org/t/matrix-vector-multiplication-in-weak-formulation/449/45 "2020-08-07T12:37:23Z")

</div>

The equality constraint also appears in the Hessian, so you need to change your `HJ` to something like:

```auto
func int HJ(real[int]& X, real[int]& E) {
[...]
}

```

---

<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 7, 2020, 2:17pm UTC](https://community.freefem.org/t/matrix-vector-multiplication-in-weak-formulation/449/46 "2020-08-07T14:17:24Z")

</div>

The problem only has simple inequality constraint as _xlPETSc_ and _xuPETSc_. But if I delete the funcE and funcJE, how should I write the TaoSolve information?

If I write it as follows:  
_TaoSolve(H, J, DJ, uPETSc, xl = lbPETSc, xu = ubPETSc, sparams = “-tao\_monitor -tao\_view -tao\_type ipm -pc\_type lu -pc\_factor\_mat\_solver\_type mumps -tao\_max\_it 1000 -tao\_gatol 1e-4”, HessianRoutine = HJ);_  
The error I mentioned continues to appear.

---

<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 7, 2020, 2:43pm UTC](https://community.freefem.org/t/matrix-vector-multiplication-in-weak-formulation/449/47 "2020-08-07T14:43:37Z")

</div>

I can’t reproduce the previous error without equality constraint.

---

<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 7, 2020, 2:51pm UTC](https://community.freefem.org/t/matrix-vector-multiplication-in-weak-formulation/449/48 "2020-08-07T14:51:11Z")

</div>

The error message is:  
[0] PETSC EROOR: Out of memory. This could be due to allocating too large an object or bleeding by not properly destroyubg unneeded objects.  
It still can not run properly for multiple processors? How should I correct it?

---

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

</div>

PDIPM and IPM are bugged when using no inequality or equality constraint. This will be fixed in the next PETSc release. I suggest you use other Tao solvers in the meantime.

---

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

</div>

Thank you for your reply.

Could you recommend some Tao solver which has the same function as interior point method?

---

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

</div>

No, I’d suggest you try different alternative. You can tune the line search parameters by looking at the PETSc/Tao documentation that I’ve already pasted above.

---

<div class="post-metadata">

### Author: ![usiu5555](https://avatars.discourse-cdn.com/v4/letter/u/e19adc/32.png) [@usiu5555](https://community.freefem.org/u/usiu5555)
#### Post date: [September 5, 2024, 1:03pm UTC](https://community.freefem.org/t/matrix-vector-multiplication-in-weak-formulation/449/52 "2024-09-05T13:03:51Z")

</div>

I try to reformulate the example in Transmission Problem ([Transmission problem](https://doc.freefem.org/models/transmission-problem.html)) into the type where the diffusion coefficient is a matrix, which depends on the region. When the diffusion coefficient is constant matrix, the results are satisfying. When I add region-dependent formulation the problem diverges, even if the matrix has the same values in both regions. Here is the code I tried (works with line 15, doesn’t work with line 17):

```auto
border a(t=0, 1){x=t; y=0;};
border b(t=0, 1){x=1; y=t;};
border c(t=0, 1){x=1-t; y=1;};
border d(t=0, 1){x=0; y=1-t;};
border i1(t=0, 1){x=t; y=0.5;};
mesh th = buildmesh(a(10) + b(10) + c(10) +d(10)+i1(10));
fespace Ph(th, P2);

Ph reg=region;
int nupper = reg(0.25, 0.75);
int nlower = reg(0.25, 0.25);

matrix A = [[1,0],[0,0.01]];
//this part works
// matrix nu = A;
//this part doesnt
matrix nu = A*(region==nlower) + A*(region==nupper);

Ph u,v;
solve lap (u, v)
    = int2d(th)(
         [dx(v), dy(v)]'*([[nu(0,0), nu(0,1)],[nu(1,0), nu(1,1)]]*[dx(u), dy(u)])
    )
    + int2d(th)(
        -1*v
    )
    + on(a, b, c, d, u=0)
    ;
plot(u);

```

I was able to make it converge by changing line 17 but the results are the same as with constant matrix in both regions :

```auto
matrix B = [[0.01,0],[0,0.01]];
matrix nu = B*(region!=nlower&&region!=nupper)+ A*(region==nlower);

```

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