# Macro ThN2O for mixed vectorial and scalar finite element space

**URL:** https://community.freefem.org/t/macro-thn2o-for-mixed-vectorial-and-scalar-finite-element-space/1731
**Category:** General Discussion
**Created:** [May 10, 2022, 9:55pm UTC](https://community.freefem.org/t/macro-thn2o-for-mixed-vectorial-and-scalar-finite-element-space/1731 "2022-05-10T21:55:44Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![jmorvan](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/jmorvan/32/2928_2.png) [@jmorvan](https://community.freefem.org/u/jmorvan)
#### Post date: [May 10, 2022, 9:55pm UTC](https://community.freefem.org/t/macro-thn2o-for-mixed-vectorial-and-scalar-finite-element-space/1731/1 "2022-05-10T21:55:44Z")

</div>

Hello everyone!

I’ve recently watched the tutorials from professor Pierre Jolivet on Youtube about parallel computing in FreeFem. I have a code that works fine sequentially and I intend to swicht it to parallel. In the sequential script I have one scalar and one vectorial finite element space, both associated with an initial mesh Th. I need to go from local to global for both vectorial and scalar fields. Can it be obtained by using macro ThN2O for both cases all in the same code?

Thanks!

---

<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: [May 11, 2022, 5:09am UTC](https://community.freefem.org/t/macro-thn2o-for-mixed-vectorial-and-scalar-finite-element-space/1731/2 "2022-05-11T05:09:17Z")

</div>

Yes, why would that matter?

---

<div class="post-metadata">

### Author: ![jmorvan](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/jmorvan/32/2928_2.png) [@jmorvan](https://community.freefem.org/u/jmorvan)
#### Post date: [May 11, 2022, 12:04pm UTC](https://community.freefem.org/t/macro-thn2o-for-mixed-vectorial-and-scalar-finite-element-space/1731/3 "2022-05-11T12:04:26Z")

</div>

As I’m using macro def(i) [i i#y] and macro init(i) [i i] for the vectorial field, the reduction from local to global works fine for the vectorial field. However, I couldn’t do the same for the scalar field.

---

<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: [May 11, 2022, 12:16pm UTC](https://community.freefem.org/t/macro-thn2o-for-mixed-vectorial-and-scalar-finite-element-space/1731/4 "2022-05-11T12:16:16Z")

</div>

Please share a minimal working example. N2O is a mesh information, it has nothing to do with a finite element space whatsoever, so as long as you use it appropriately afterwards, you can do whatever you want with it.

---

<div class="post-metadata">

### Author: ![jmorvan](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/jmorvan/32/2928_2.png) [@jmorvan](https://community.freefem.org/u/jmorvan)
#### Post date: [May 11, 2022, 12:33pm UTC](https://community.freefem.org/t/macro-thn2o-for-mixed-vectorial-and-scalar-finite-element-space/1731/5 "2022-05-11T12:33:36Z")

</div>

Ok, I see! Thanks for your reply and for the information. I’ll try to make a short code those ideas to share with you. Thanks again!

---

<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: [May 11, 2022, 2:06pm UTC](https://community.freefem.org/t/macro-thn2o-for-mixed-vectorial-and-scalar-finite-element-space/1731/6 "2022-05-11T14:06:17Z")

</div>

Here is an example. Of course, you are free to use any kind of vectorial finite elements, I just used Morley here.

```auto
load "Morley"
include "macro_ddm.idp"
load "PETSc"

mesh ThGlobal = square(100, 100);
mesh Th = square(100, 100);
fespace WhGlobal(ThGlobal, P2Morley);
fespace VhGlobal(ThGlobal, P1);
WhGlobal [uG, uGx, uGy] = [x^2, y^2, x^2+y^2];
VhGlobal pG = x + y;
int[int] n2o;
macro ThN2O()n2o//
buildDmesh(Th);
fespace Wh(Th, P2Morley);
fespace Vh(Th, P1);
Wh [u, ux, uy] = [x^2, y^2, x^2+y^2];
Vh p = x + y;
int[int] restWh = restrict(Wh, WhGlobal, n2o);
int[int] restVh = restrict(Vh, VhGlobal, n2o);
{
    Mat A;
    createMat(Th, A, P2); // trick => use P2, not P2Morley!
    real[int] tmp;
    ChangeNumbering(A, u[], tmp);
    ChangeNumbering(A, u[], tmp, inverse = true);
    WhGlobal [reducex, reducey, reducez];
    WhGlobal [beforex, beforey, beforez];
    for[i, v : restWh] beforex[][v] = u[][i];
    mpiReduce(beforex[], reducex[], processor(0, mpiCommWorld), mpiSUM);
    uG[] -= reducex[];
    if(mpirank == 0) assert(uG[].l2 < 1.0e-12);
}
{
    Mat A;
    createMat(Th, A, P1);
    real[int] tmp;
    ChangeNumbering(A, p[], tmp);
    ChangeNumbering(A, p[], tmp, inverse = true);
    VhGlobal reduce;
    VhGlobal before;
    before[] = 0;
    for[i, v : restVh] before[][v] = p[][i];
    mpiReduce(before[], reduce[], processor(0, mpiCommWorld), mpiSUM);
    pG[] -= reduce[];
    if(mpirank == 0) assert(pG[].l2 < 1.0e-12);
}
```

---

<div class="post-metadata">

### Author: ![jmorvan](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/jmorvan/32/2928_2.png) [@jmorvan](https://community.freefem.org/u/jmorvan)
#### Post date: [May 11, 2022, 9:49pm UTC](https://community.freefem.org/t/macro-thn2o-for-mixed-vectorial-and-scalar-finite-element-space/1731/7 "2022-05-11T21:49:44Z")

</div>

Thank you so much for sharing this example. It’ll certainly help me!

Best regards!

---

<div class="post-metadata">

### Author: ![stergopilot](https://avatars.discourse-cdn.com/v4/letter/s/e36b37/32.png) [@stergopilot](https://community.freefem.org/u/stergopilot)
#### Post date: [July 20, 2023, 1:37pm UTC](https://community.freefem.org/t/macro-thn2o-for-mixed-vectorial-and-scalar-finite-element-space/1731/8 "2023-07-20T13:37:33Z")

</div>

If I change Morley to Pk = [P1,P1,P1], I met Compile error : Invalide array size for vectorial fespace function. How do I solve this problem? Thanks!

````auto
load "PETSc" // PETSc plugin
macro dimension()2// EOM // 2D or 3D
include "macro_ddm.idp" // additional DDM functions
macro def(i)[i, i#B, i#C]//
macro init(i)[i, i, i]//

mesh ThGlobal = square(100, 100);
mesh Th = square(100, 100);
func Pk = [P1,P1,P1];
fespace WhGlobal(ThGlobal, Pk);
fespace VhGlobal(ThGlobal, P1);
WhGlobal [uG, uGx, uGy] = [x^2, y^2, x^2+y^2];
VhGlobal pG = x + y;
int[int] n2o;
macro ThN2O()n2o//
buildDmesh(Th);
fespace Wh(Th, Pk);
fespace Vh(Th, P1);
Wh [u, ux, uy] = [x^2, y^2, x^2+y^2];
Vh p = x + y;
int[int] restWh = restrict(Wh, WhGlobal, n2o);
int[int] restVh = restrict(Vh, VhGlobal, n2o);
{
    Mat A;
    createMat(Th, A, Pk); // trick => use P2, not P2Morley!
    real[int] tmp;
    ChangeNumbering(A, u[], tmp);
    ChangeNumbering(A, u[], tmp, inverse = true);
    WhGlobal [reducex, reducey, reducez];
    WhGlobal [beforex, beforey, beforez];
    for[i, v : restWh] beforex[][v] = u[][i];
    mpiReduce(beforex[], reducex[], processor(0, mpiCommWorld), mpiSUM);
    uG[] -= reducex[];
    if(mpirank == 0) assert(uG[].l2 < 1.0e-12);
}

{
    Mat A;
    createMat(Th, A, P1);
    real[int] tmp;
    ChangeNumbering(A, p[], tmp);
    ChangeNumbering(A, p[], tmp, inverse = true);
    VhGlobal reduce;
    VhGlobal before;
    before[] = 0;
    for[i, v : restVh] before[][v] = p[][i];
    mpiReduce(before[], reduce[], processor(0, mpiCommWorld), mpiSUM);
    pG[] -= reduce[];
    if(mpirank == 0) assert(pG[].l2 < 1.0e-12);
}```

````

402 @ fespace WhPartPrivate(ThTab[1 - 1], ThPkPart P1 );  
403 @ WhPartPrivate def(func2vec) [func2vec, func2vecB, func2vecC]; the array size must be 1 not 3  
Invalid array size for vectorial fespace function  
current line = 403 mpirank 0 / 2  
Compile error : Invalid array size for vectorial fespace function  
line number :403, ;  
error Compile error : Invalid array size for vectorial fespace function  
line number :403, ;  
code = 1 mpirank: 0

```auto

```

---

<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 20, 2023, 1:41pm UTC](https://community.freefem.org/t/macro-thn2o-for-mixed-vectorial-and-scalar-finite-element-space/1731/9 "2023-07-20T13:41:58Z")

</div>

```diff
--- wrong.edp	2023-07-20 15:41:15
+++ right.edp	2023-07-20 15:41:26
@@ -1,8 +1,6 @@
 load "PETSc" // PETSc plugin
 macro dimension()2// EOM // 2D or 3D
 include "macro_ddm.idp" // additional DDM functions
-macro def(i)[i, i#B, i#C]//
-macro init(i)[i, i, i]//
 
 mesh ThGlobal = square(100, 100);
 mesh Th = square(100, 100);
@@ -22,6 +20,8 @@ int[int] restVh = restrict(Vh, VhGlobal, n2o);
 int[int] restVh = restrict(Vh, VhGlobal, n2o);
 {
     Mat A;
+ macro def(i)[i, i#B, i#C]//
+ macro init(i)[i, i, i]//
     createMat(Th, A, Pk); // trick => use P2, not P2Morley!
     real[int] tmp;
     ChangeNumbering(A, u[], tmp);

```

---

<div class="post-metadata">

### Author: ![stergopilot](https://avatars.discourse-cdn.com/v4/letter/s/e36b37/32.png) [@stergopilot](https://community.freefem.org/u/stergopilot)
#### Post date: [July 21, 2023, 6:52am UTC](https://community.freefem.org/t/macro-thn2o-for-mixed-vectorial-and-scalar-finite-element-space/1731/10 "2023-07-21T06:52:59Z")

</div>

It works. Thank you very much!

---

<div class="post-metadata">

### Author: ![stergopilot](https://avatars.discourse-cdn.com/v4/letter/s/e36b37/32.png) [@stergopilot](https://community.freefem.org/u/stergopilot)
#### Post date: [April 10, 2024, 3:21am UTC](https://community.freefem.org/t/macro-thn2o-for-mixed-vectorial-and-scalar-finite-element-space/1731/11 "2024-04-10T03:21:39Z")

</div>

I am writing to ask how to write a parallel solver for a transient problem with more than one finite space. For the case “A large fluid problem” in FreeFem documentation, we have Pk=[P2,P2,P1] for [u1,u2,p] and P1 for T, k. We can use macro def(i)[i, i#B, i#C]//and macro init(i)[i, i, i]// for [u1,u2,p]. It is similar to the above implementation. But now at each time step, we need to update T using u1and u2 in the previous time step, which are invisible in the code delimited by the second pair of curly braces.  
{  
Mat A;  
createMat(Th, A, Pk); //for [u1,u2,p]  
…  
}  
{  
Mat B;  
createMat(Th, B, P1); //for T  
…  
}

Thanks!

---

<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: [April 10, 2024, 5:42am UTC](https://community.freefem.org/t/macro-thn2o-for-mixed-vectorial-and-scalar-finite-element-space/1731/12 "2024-04-10T05:42:36Z")

</div>

Why are you declaring `A` and `B` inside the brackets?

---

<div class="post-metadata">

### Author: ![stergopilot](https://avatars.discourse-cdn.com/v4/letter/s/e36b37/32.png) [@stergopilot](https://community.freefem.org/u/stergopilot)
#### Post date: [April 12, 2024, 3:59am UTC](https://community.freefem.org/t/macro-thn2o-for-mixed-vectorial-and-scalar-finite-element-space/1731/13 "2024-04-12T03:59:57Z")

</div>

Here is the sample code of my simulation.  
[prll.edp](https://community.freefem.org/uploads/short-url/22EGJTUPrfl3cLrblgFhQfuXlw6.edp) (3.4 KB)  
I do not know how to handle the following macros properly.

> macro def(i)[i, i#B, i#C, i#D, i#E, i#F, i#G]//  
> macro init(i)[i, i, i, i, i, i, i]//  
> Mat A;  
> createMat(Th,A,Pk)  
> set(A, sparams = “-pc\_type lu”);  
> A = vSt(Wh, Wh);  
> macro def(i)[i, i#B, i#C]//  
> macro init(i)[i, i, i]//  
> Mat B;  
> createMat(Th,B,Pk)  
> set(B, sparams = “-pc\_type lu”);

---

<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: [April 12, 2024, 5:34am UTC](https://community.freefem.org/t/macro-thn2o-for-mixed-vectorial-and-scalar-finite-element-space/1731/14 "2024-04-12T05:34:17Z")

</div>

Why did you remove all brackets?

---

<div class="post-metadata">

### Author: ![stergopilot](https://avatars.discourse-cdn.com/v4/letter/s/e36b37/32.png) [@stergopilot](https://community.freefem.org/u/stergopilot)
#### Post date: [April 12, 2024, 7:32am UTC](https://community.freefem.org/t/macro-thn2o-for-mixed-vectorial-and-scalar-finite-element-space/1731/15 "2024-04-12T07:32:38Z")

</div>

I removed the brackets, I obtained the following errors.

> Blockquote  
> current line = 96 mpirank 0 / 6  
> Compile error : The macro already exists, sorry  
> line number :96, )  
> error Compile error : The macro already exists, sorry  
> line number :96, )  
> code = 1 mpirank: 0

Then I used the brackets, such as

> {  
> macro def(i)[i, i#B, i#C, i#D, i#E, i#F, i#G]//  
> macro init(i)[i, i, i, i, i, i, i]//  
> Mat A;  
> createMat(Th,A,Pk)  
> set(A, sparams = “-pc\_type lu”);  
> A = vSt(Wh, Wh);  
> }  
> {  
> macro def(i)[i, i#B, i#C]//  
> macro init(i)[i, i, i]//  
> Mat B;  
> createMat(Th,B,Pk)  
> set(B, sparams = “-pc\_type lu”);  
> }

I got the following errors

> 396 @ fespace WhPartPrivate(ThTab[1 - 1], ThPkPart Pk );  
> 397 @ WhPartPrivate def(func2vec) [func2vec, func2vecB, func2vecC]; the array size must be 7 not 3  
> Error line number 397, in file macro: partitionPrivate in /opt/freefem/4.9/mpich3.3rc1-gnu9.4.0/lib/ff++/4.9/idp/macro\_ddm.idp, before token ;  
> Invalide array size for vectorial fespace function  
> current line = 397 mpirank 0 / 6  
> Compile error : Invalide array size for vectorial fespace function  
> line number :397, ;  
> error Compile error : Invalide array size for vectorial fespace function  
> line number :397, ;  
> code = 1 mpirank: 0

---

<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: [April 12, 2024, 7:35am UTC](https://community.freefem.org/t/macro-thn2o-for-mixed-vectorial-and-scalar-finite-element-space/1731/16 "2024-04-12T07:35:14Z")

</div>

Please post a minimal example reproducing the error, not something with 300+ lines of code.

---

<div class="post-metadata">

### Author: ![stergopilot](https://avatars.discourse-cdn.com/v4/letter/s/e36b37/32.png) [@stergopilot](https://community.freefem.org/u/stergopilot)
#### Post date: [April 12, 2024, 7:49am UTC](https://community.freefem.org/t/macro-thn2o-for-mixed-vectorial-and-scalar-finite-element-space/1731/17 "2024-04-12T07:49:19Z")

</div>

Attached is a small example.  
[example.edp](https://community.freefem.org/uploads/short-url/iHJyBNGthyc7uXKGbJ7XXBGozec.edp) (3.0 KB)

---

<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: [April 12, 2024, 8:02am UTC](https://community.freefem.org/t/macro-thn2o-for-mixed-vectorial-and-scalar-finite-element-space/1731/18 "2024-04-12T08:02:38Z")

</div>

You are using the same `Pk` in both calls to `createMat()`, but different `def` macros, why?  
Also, you are still defining `A` and `B` inside brackets, which is wrong.

---

<div class="post-metadata">

### Author: ![stergopilot](https://avatars.discourse-cdn.com/v4/letter/s/e36b37/32.png) [@stergopilot](https://community.freefem.org/u/stergopilot)
#### Post date: [April 12, 2024, 8:17am UTC](https://community.freefem.org/t/macro-thn2o-for-mixed-vectorial-and-scalar-finite-element-space/1731/19 "2024-04-12T08:17:51Z")

</div>

Yes. It should be

> createMat(Th,B,Ps)

I commented

> // macro def(i)[i, i#B, i#C]//  
> // macro init(i)[i, i, i]//

then I met problems

> 396 @ fespace WhPartPrivate(ThTab[1 - 1], ThPkPart Ps );  
> 397 @ WhPartPrivate def(func2vec) [func2vec, func2vecB, func2vecC, func2vecD, func2vecE, func2vecF, func2vecG]; the array size must be 3 not 7  
> Error line number 397, in file macro: partitionPrivate in /opt/freefem/4.9/mpich3.3rc1-gnu9.4.0/lib/ff++/4.9/idp/macro\_ddm.idp, before token ;  
> Invalide array size for vectorial fespace function  
> current line = 397 mpirank 0 / 6  
> Compile error : Invalide array size for vectorial fespace function  
> line number :397, ;  
> error Compile error : Invalide array size for vectorial fespace function  
> line number :397, ;  
> code = 1 mpirank: 0

I tried to use brackets to avoid the duplicate definition of `macro def(i)[i, i#B, i#C]//`and `macro init(i)[i, i, i]//` but it did not work.

---

<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: [April 12, 2024, 8:29am UTC](https://community.freefem.org/t/macro-thn2o-for-mixed-vectorial-and-scalar-finite-element-space/1731/20 "2024-04-12T08:29:43Z")

</div>

Please send the updated code.

[Next page](https://community.freefem.org/t/macro-thn2o-for-mixed-vectorial-and-scalar-finite-element-space/1731.md?page=2)
