# Periodic boundary conditions with PETSc and vectorial FEspace

**URL:** https://community.freefem.org/t/periodic-boundary-conditions-with-petsc-and-vectorial-fespace/1849
**Category:** General Discussion
**Created:** [June 25, 2022, 12:23pm UTC](https://community.freefem.org/t/periodic-boundary-conditions-with-petsc-and-vectorial-fespace/1849 "2022-06-25T12:23:11Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![AzizTakhirov](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/aziztakhirov/32/26_2.png) [@AzizTakhirov](https://community.freefem.org/u/AzizTakhirov)
#### Post date: [June 25, 2022, 12:23pm UTC](https://community.freefem.org/t/periodic-boundary-conditions-with-petsc-and-vectorial-fespace/1849/1 "2022-06-25T12:23:11Z")

</div>

Hello,  
I’m trying to figure out the correct syntax for generating the matrices with  
periodic boundary conditions with PETSc and vectorial FEspace, such as in the case of Stokes equations with periodic boundary conditions.  
I looked at diffusion-periodic-2d-PETSc.edp and stokes-2D-PETSc.edp, and came up with this code, but I get an error:  
load “PETSc” //  
include “macro\_ddm.idp” //  
macro dimension()2// EOM // 2D or 3D

int[int] labPeriodic = [1,2,3,4];  
macro Pk() [P2,P2,P1], periodic=[[2,y],[4,y]] //EOM  
mesh Th = square(40,40);  
Mat A;  
int[int] n2o;  
macro ThPeriodicity()labPeriodic//  
macro ThN2O()n2o//  
buildDmesh(Th)  
//createMat(Th, A, Pk) //\<--------------- This didn’t work either  
buildMatPeriodic(Th, getARGV(“-split”, 1), A, Pk, piCommWorld,labPeriodic)

Thanks

---

<div class="post-metadata">

### Author: ![zhaog6](https://avatars.discourse-cdn.com/v4/letter/z/4af34b/32.png) [@zhaog6](https://community.freefem.org/u/zhaog6)
#### Post date: [June 26, 2022, 2:38am UTC](https://community.freefem.org/t/periodic-boundary-conditions-with-petsc-and-vectorial-fespace/1849/2 "2022-06-26T02:38:26Z")

</div>

The correct code should be

```nohighlight
load “PETSc” //
include “macro_ddm.idp” //
macro dimension() 2 // EOM // 2D or 3D

int[int] labPeriodic = [2, 4];
macro Pk() [P2, P2, P1], periodic=[[labPeriodic[0], y], [labPeriodic[1], y]] //EOM
mesh Th = square(40, 40);
Mat A;
int[int] n2o;
macro ThPeriodicity() labPeriodic //EOM
macro ThN2O() n2o //EOM
buildDmesh(Th);
{
    macro def(u) [u, u#B, u#C] //
    macro init(u) [u, u, u] //
    createMat(Th, A, Pk);
}

```

---

<div class="post-metadata">

### Author: ![AzizTakhirov](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/aziztakhirov/32/26_2.png) [@AzizTakhirov](https://community.freefem.org/u/AzizTakhirov)
#### Post date: [June 26, 2022, 7:19pm UTC](https://community.freefem.org/t/periodic-boundary-conditions-with-petsc-and-vectorial-fespace/1849/3 "2022-06-26T19:19:25Z")

</div>

Thank you, it worked.  
Just one correction, I think you meant  
macro Pk() [P2, P2, P1], periodic=[[labPeriodic[1], y], [labPeriodic[3], y]] //EOM

---

<div class="post-metadata">

### Author: ![zhaog6](https://avatars.discourse-cdn.com/v4/letter/z/4af34b/32.png) [@zhaog6](https://community.freefem.org/u/zhaog6)
#### Post date: [June 27, 2022, 12:37am UTC](https://community.freefem.org/t/periodic-boundary-conditions-with-petsc-and-vectorial-fespace/1849/4 "2022-06-27T00:37:50Z")

</div>

> I think you meant macro Pk() [P2, P2, P1], periodic=[[labPeriodic[1], y], [labPeriodic[3], y]] //EOM

No. If your boundaries of label=2, 4 is a pair of periodic boundaries, then

```c++
int[int] labPeriodic = [2, 4];
macro ThPeriodicity() labPeriodic //EOM

```

and the FE macro is

```c++
macro Pk() [P2, P2, P1] periodic=[[labPeriodic[0], y], [labPeriodic[1], y]] //

```
