# Invalid array size in the code with PETSc for solving the fluid-structure coupling problem

**URL:** https://community.freefem.org/t/invalid-array-size-in-the-code-with-petsc-for-solving-the-fluid-structure-coupling-problem/2094
**Category:** General Discussion
**Created:** [October 30, 2022, 7:30am UTC](https://community.freefem.org/t/invalid-array-size-in-the-code-with-petsc-for-solving-the-fluid-structure-coupling-problem/2094 "2022-10-30T07:30:13Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![SHENG](https://avatars.discourse-cdn.com/v4/letter/s/839c29/32.png) [@SHENG](https://community.freefem.org/u/SHENG)
#### Post date: [October 30, 2022, 7:30am UTC](https://community.freefem.org/t/invalid-array-size-in-the-code-with-petsc-for-solving-the-fluid-structure-coupling-problem/2094/1 "2022-10-30T07:30:13Z")

</div>

Dear all,

I am facing a problem. I want to solve the fluid-structure coupling problem with PETSc. However, the code always report the error, as shown in the below figure,

 ![2abe008f559b27b6c5f073572db228c](https://canada1.discourse-cdn.com/flex030/uploads/freefem/original/2X/1/1bece8880deef27d6224c276ae2dae39523f9975.png)  
I have debugged this code for long time. I realized that the error maybe caused by the difference in the array size between the two fespaces for the coupling problem, while I still did not know how I could solve this problem.  
The code is attached to here.  
[NS\_parallel\_2D.edp](https://community.freefem.org/uploads/short-url/oJkjDew9Tb5ljHxpcqN0xUgr7eD.edp) (5.5 KB)  
Please give me some advice. Thank you in advance.  
Best,  
Liu

---

<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: [October 30, 2022, 8:35am UTC](https://community.freefem.org/t/invalid-array-size-in-the-code-with-petsc-for-solving-the-fluid-structure-coupling-problem/2094/2 "2022-10-30T08:35:55Z")

</div>

You need these lines [FreeFem-sources/elasticity-2d-PETSc.edp at master · FreeFem/FreeFem-sources · GitHub](https://github.com/FreeFem/FreeFem-sources/blob/master/examples/hpddm/elasticity-2d-PETSc.edp#L9-L10) before `createMat()` when you are dealing with vectorial finite element spaces.

---

<div class="post-metadata">

### Author: ![SHENG](https://avatars.discourse-cdn.com/v4/letter/s/839c29/32.png) [@SHENG](https://community.freefem.org/u/SHENG)
#### Post date: [October 30, 2022, 8:42am UTC](https://community.freefem.org/t/invalid-array-size-in-the-code-with-petsc-for-solving-the-fluid-structure-coupling-problem/2094/3 "2022-10-30T08:42:44Z")

</div>

Thank you very much for your reply.  
Actually, I have add these lines, while the code still gave me the same error report.  
I think the reason maybe that the array size of the elastic problem is two (u, uB), while the array size of the fluid problem is three (uu1,uu2,p). How could I solve this problem? I have tried for the below code:  
“…  
macro def(i)[i, i#B]// EOM // vector field definition  
macro init(i)[i, i]// EOM // vector field initialization  
func Pk = [P1, P1]; // finite element space  
fespace Vh(Th,Pk); // for the elastic problem  
Vh [u,uB], [v,vB],…;  
…  
func Pm=[P1,P1,P1];  
fespace Xh(fracture,Pm); // for the fluid problem  
Xh [uu1,uu2,p], [vv1,vv2,q];  
…”  
I have also tried the below code:  
“…  
macro def(i)[i, i#B]// EOM // vector field definition  
macro init(i)[i, i]// EOM // vector field initialization  
func Pk = [P1, P1]; // finite element space  
fespace Vh(Th,Pk); // for the elastic problem  
Vh [u,uB], [v,vB],…;  
…  
func Pm = [P1,P1];  
fespace Xh(fracture, Pm); // for the fluid problem  
Xh [uu1, uu2, p], [vv1, vv2, q];  
fespace Mh (fracture, P1); //  
Mh p,q;  
…”  
The above two codes still gave me the same error reports. Did I miss something?

---

<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: [October 30, 2022, 9:19am UTC](https://community.freefem.org/t/invalid-array-size-in-the-code-with-petsc-for-solving-the-fluid-structure-coupling-problem/2094/4 "2022-10-30T09:19:07Z")

</div>

Look at the examples in the folder `examples/hpddm`.

---

<div class="post-metadata">

### Author: ![SHENG](https://avatars.discourse-cdn.com/v4/letter/s/839c29/32.png) [@SHENG](https://community.freefem.org/u/SHENG)
#### Post date: [October 30, 2022, 9:39am UTC](https://community.freefem.org/t/invalid-array-size-in-the-code-with-petsc-for-solving-the-fluid-structure-coupling-problem/2094/5 "2022-10-30T09:39:03Z")

</div>

Ok, thank you very much, professor.

---

<div class="post-metadata">

### Author: ![SHENG](https://avatars.discourse-cdn.com/v4/letter/s/839c29/32.png) [@SHENG](https://community.freefem.org/u/SHENG)
#### Post date: [November 7, 2022, 7:22am UTC](https://community.freefem.org/t/invalid-array-size-in-the-code-with-petsc-for-solving-the-fluid-structure-coupling-problem/2094/6 "2022-11-07T07:22:38Z")

</div>

Dear professor,

I am facing a problem.  
Now my code could normally run when it only solve the elastic problem.  
However, as you know, I am trying to solve a fluid-structure problem, and the code can not run normally.  
For the fespace describing fluid, the code is as following,  
" …  
fespace Mh(fracture, P1); //definition of the pressure space  
Mh up1=0.,up2=0.;

fespace Xh(fracture, Pm); //definition of the velocity component space  
Xh [uu,uuB,p]=[0.,0.,0.],[vv,vvB,q]=[0.,0.,0.];// //def1(uu),def1(vv);

Mat B;  
//buildMat(fracture, getARGV(“-split”, 1), B, Pk, mpiCommWorld)  
createMat(fracture, B, Pm)

macro grad(uu)[dx(uu),dy(uu)]//EOM  
macro div1(uu)( dx(uu)+dy(uu#B) )//EOM

varf NS ([uu,uuB,p],[vv,vvB,q])=  
int2d(fracture)(idt \* (uu \* vv+uuB \* vvB)+(nu \* grad(uu) ’ \* grad(vv) +grad(uuB) ’ \* grad (vvB)) -div1(uu)_q-div1(vv)p + pq_1e-10)  
+int2d(fracture)(-idt \* convect([up1,up2],-dt,up1) \* vv-idt \* convect([up1,up2],-dt,up2)\* vvB)  
+on(9,10,11,12, uu=(u-u1f) \*idt, uuB=(uB-u2f) \* idt)//boundary condition  
;  
…"  
The code reports that is posted as below,

 ![a0e005196fdbd6bc328b4ac2958ea94](https://canada1.discourse-cdn.com/flex030/uploads/freefem/original/2X/1/13676b9394a30e29b4116a84f0edb3f063deaaa6.png)

When I modify “createMat(fracture, B, Pm)” to “createMat(fracture, B, [P2,P2])”, the code can run normally, although the results are still wrong.  
I do not know the reason. The code file is uploaded to here. Please give me some advice.  
[NS\_parallel\_2D.edp](https://community.freefem.org/uploads/short-url/fptH4raFzdQZmj4eSdHmyoVOgq0.edp) (5.7 KB)  
Thank you very much.

Best,  
Liu

---

<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: [November 7, 2022, 7:24am UTC](https://community.freefem.org/t/invalid-array-size-in-the-code-with-petsc-for-solving-the-fluid-structure-coupling-problem/2094/7 "2022-11-07T07:24:04Z")

</div>

Why are you copy/pasting this question on two different threads?

---

<div class="post-metadata">

### Author: ![SHENG](https://avatars.discourse-cdn.com/v4/letter/s/839c29/32.png) [@SHENG](https://community.freefem.org/u/SHENG)
#### Post date: [November 7, 2022, 7:27am UTC](https://community.freefem.org/t/invalid-array-size-in-the-code-with-petsc-for-solving-the-fluid-structure-coupling-problem/2094/8 "2022-11-07T07:27:09Z")

</div>

Sorry, professor. My remember is wrong. The question is following this thread. I have deleted the question on the another thread.

---

<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: [November 7, 2022, 7:27am UTC](https://community.freefem.org/t/invalid-array-size-in-the-code-with-petsc-for-solving-the-fluid-structure-coupling-problem/2094/9 "2022-11-07T07:27:33Z")

</div>

If you want to mix different vectorial finite element spaces, again, just look at the examples, e.g., [FreeFem-sources/stokes-block-2d-PETSc.edp at master · FreeFem/FreeFem-sources · GitHub](https://github.com/FreeFem/FreeFem-sources/blob/master/examples/hpddm/stokes-block-2d-PETSc.edp#L30-L40).

---

<div class="post-metadata">

### Author: ![SHENG](https://avatars.discourse-cdn.com/v4/letter/s/839c29/32.png) [@SHENG](https://community.freefem.org/u/SHENG)
#### Post date: [November 7, 2022, 7:30am UTC](https://community.freefem.org/t/invalid-array-size-in-the-code-with-petsc-for-solving-the-fluid-structure-coupling-problem/2094/10 "2022-11-07T07:30:02Z")

</div>

Ok, professor, I missed this example. I just focused on the example of stokes-2d-PETSc.edp. Thank you very much for your advice.

---

<div class="post-metadata">

### Author: ![SHENG](https://avatars.discourse-cdn.com/v4/letter/s/839c29/32.png) [@SHENG](https://community.freefem.org/u/SHENG)
#### Post date: [November 7, 2022, 7:33am UTC](https://community.freefem.org/t/invalid-array-size-in-the-code-with-petsc-for-solving-the-fluid-structure-coupling-problem/2094/11 "2022-11-07T07:33:05Z")

</div>

Professor, are there any example for the fluid structure coupling problem with PETSc?

---

<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: [November 7, 2022, 7:40am UTC](https://community.freefem.org/t/invalid-array-size-in-the-code-with-petsc-for-solving-the-fluid-structure-coupling-problem/2094/12 "2022-11-07T07:40:08Z")

</div>

Not openly available, to the best of my knowledge, but it’s doable, cf. slide #20 of [http://joliv.et/FreeFem-tutorial/2019.pdf](http://joliv.et/FreeFem-tutorial/2019.pdf).

---

<div class="post-metadata">

### Author: ![SHENG](https://avatars.discourse-cdn.com/v4/letter/s/839c29/32.png) [@SHENG](https://community.freefem.org/u/SHENG)
#### Post date: [November 7, 2022, 7:43am UTC](https://community.freefem.org/t/invalid-array-size-in-the-code-with-petsc-for-solving-the-fluid-structure-coupling-problem/2094/13 "2022-11-07T07:43:38Z")

</div>

Thank you very much, professor.  
Give you my best wishes.

---

<div class="post-metadata">

### Author: ![SHENG](https://avatars.discourse-cdn.com/v4/letter/s/839c29/32.png) [@SHENG](https://community.freefem.org/u/SHENG)
#### Post date: [November 9, 2022, 8:05am UTC](https://community.freefem.org/t/invalid-array-size-in-the-code-with-petsc-for-solving-the-fluid-structure-coupling-problem/2094/14 "2022-11-09T08:05:15Z")

</div>

Dear Professor,

According to your advice, I have revised the code, and the code could run. However, the code can only provide the first solution, and can not iteratively run. The error reported from the code is as shown below,

 ![1ebf35c10c17bb49693a699ecfe95f5](https://canada1.discourse-cdn.com/flex030/uploads/freefem/original/2X/8/8bd81bd5afe3a7c7d05f485ac6ecb3e518883a1e.png)  
The code is uploaded here.  
[NS\_parallel\_2D.edp](https://community.freefem.org/uploads/short-url/5lSl0lYVsmwPcvPlIt2d6v9OYFt.edp) (5.9 KB)  
Please give me some advice.  
Best,  
Liu

---

<div class="post-metadata">

### Author: ![SHENG](https://avatars.discourse-cdn.com/v4/letter/s/839c29/32.png) [@SHENG](https://community.freefem.org/u/SHENG)
#### Post date: [November 9, 2022, 8:50am UTC](https://community.freefem.org/t/invalid-array-size-in-the-code-with-petsc-for-solving-the-fluid-structure-coupling-problem/2094/15 "2022-11-09T08:50:37Z")

</div>

Dear professor,

I switched the additional parameter in my sparams to set(B, sparams = “-ksp\_converged\_reason -pc\_type lu”, bs=1);. The code can run. While the terminal window has a report in each iteration, as shown below,

 ![bf05e4dfcc51998210aa6fee8d0853c](https://canada1.discourse-cdn.com/flex030/uploads/freefem/original/2X/d/d6a0dcb0c5da83b6fb716ffd7e492bfabc3f9bf1.png)  
 ![54475ca44393c997cb8484e6904c477](https://canada1.discourse-cdn.com/flex030/uploads/freefem/original/2X/5/5699e326e6a1c43c801279afe0c7944dff99510a.png)  
 ![b61b1f8e126e66b81a3945ee34db968](https://canada1.discourse-cdn.com/flex030/uploads/freefem/original/2X/d/d35cc92cb7b68e290aad1ced46988a36df18eba9.png)  
Could you tell me where is the problem?

Best,  
Liu

---

<div class="post-metadata">

### Author: ![SHENG](https://avatars.discourse-cdn.com/v4/letter/s/839c29/32.png) [@SHENG](https://community.freefem.org/u/SHENG)
#### Post date: [November 9, 2022, 9:01am UTC](https://community.freefem.org/t/invalid-array-size-in-the-code-with-petsc-for-solving-the-fluid-structure-coupling-problem/2094/16 "2022-11-09T09:01:25Z")

</div>

The problem is resolved.  
Thank you very much.
