# Obtain Eigenvectors using SLEPc parallely

**URL:** https://community.freefem.org/t/obtain-eigenvectors-using-slepc-parallely/2379
**Category:** General Discussion
**Created:** [April 4, 2023, 5:14pm UTC](https://community.freefem.org/t/obtain-eigenvectors-using-slepc-parallely/2379 "2023-04-04T17:14:53Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![zhugp123](https://avatars.discourse-cdn.com/v4/letter/z/71e660/32.png) [@zhugp123](https://community.freefem.org/u/zhugp123)
#### Post date: [April 4, 2023, 5:14pm UTC](https://community.freefem.org/t/obtain-eigenvectors-using-slepc-parallely/2379/1 "2023-04-04T17:14:53Z")

</div>

Dear FreeFEM Developer,  
I try to solve the following eigenvalue problem using SLPEc parallelly.

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/freefem/original/2X/5/5b376b153597352cfde27a4bc953042116187853.png)

The FE space for `[u, v, p]` is

```auto
fespace Xh(Th, [P2, P2, P1]); 

```

Note that the variables U and V are independent of spatial variables, so they can not be defined through `fespace`.

I solve the eigenvalue using

```auto
EPSSolve(A, B, vectors = vec, values = val, ....);

```

**My question is that** U and V **are not defined in any finite element space, how can we compose** `vec` in `vectors = vec`?  
It seems impossible. I checked all the SLEPc examples provided by FreeFEM ([link](https://doc.freefem.org/documentation/petsc/examples.html)), but I didn’t find similar cases.

Can we store the eigenvector in an array, just like this

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/freefem/original/2X/d/df237ed3ec5e501f5ba70e92ec6e1cd436776775.png)

For example:  
`real[int, int] EigVec(Xh.ndof+2, num);`  
where `num` denotes the number of eigenvalues.

Then we get eigenvectors through

```auto
Xh [eu, ev, ep]; 
eu[] = EigVec[0:Xh.ndof-1][0];

```

where `eu` denotes the leading eigenvector.

---

<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 4, 2023, 5:48pm UTC](https://community.freefem.org/t/obtain-eigenvectors-using-slepc-parallely/2379/2 "2023-04-04T17:48:09Z")

</div>

Yes, you can use the `array` parameter instead of the `vectors` parameter.  
Then, with one column of the `array`, you can use `ChangeNumbering` to go back to the `fespace` function you defined.

---

<div class="post-metadata">

### Author: ![zhugp123](https://avatars.discourse-cdn.com/v4/letter/z/71e660/32.png) [@zhugp123](https://community.freefem.org/u/zhugp123)
#### Post date: [April 5, 2023, 8:02am UTC](https://community.freefem.org/t/obtain-eigenvectors-using-slepc-parallely/2379/3 "2023-04-05T08:02:22Z")

</div>

Dear Dr. Pierre Jolivet,  
Your answer is really helpful. Thanks again.

Best

---

<div class="post-metadata">

### Author: ![zhugp123](https://avatars.discourse-cdn.com/v4/letter/z/71e660/32.png) [@zhugp123](https://community.freefem.org/u/zhugp123)
#### Post date: [April 5, 2023, 9:13am UTC](https://community.freefem.org/t/obtain-eigenvectors-using-slepc-parallely/2379/4 "2023-04-05T09:13:09Z")

</div>

Dear Dr. Pierre Jolivet,  
According to your suggestion, I have replaced `vectors` with `array`:

```auto
real[int, int] EigVec(Xh.ndof+2, nev);
EPSSolve(A, B, array=EigVec, values=EigVal, sparams = params); 

```

The code runs well. Then I try to transfer the data using `ChangeNumbering`

```auto
real[int] EigSig(Xh.ndof+2);
ChangeNumbering(OpJ, EigSig, EigVec(:, 0), inverse=true, exchange = true);
fespace Vh(Th, P2); fespace Ph(Th, P1);
Vh uh, vh; 
Ph ph;
if(mpirank == 0) EigSig.resize(EigSig.n - 2);
Xh [eu, ev, ep] = [uh, vh, ph];
eu[] = EigSig;
uh = eu; vh = ev; ph = ep; 

```

I find that the eigenvalues are correct but the eigenvectors are almost zero (1e-39).  
Is there something wrong with the above codes?

Best,

---

<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 5, 2023, 9:45am UTC](https://community.freefem.org/t/obtain-eigenvectors-using-slepc-parallely/2379/5 "2023-04-05T09:45:57Z")

</div>

I cannot tell for sure without running the actual code, but it looks wrong indeed:

1. `EigVec` should not have the dimension of the local `fespace`, but the local dimension of the `Mat` (without ghost unknowns);
2. `ChangeNumbering` should be called with a vector of dimension `Xh.ndof` first, and `OpJ.n` then. You are calling it with a vector of dimension `Xh.ndof + 2` first, and `Xh.ndof + 2` then;
3. you should not resize `EigSig` after a call to `ChangeNumbering`.

See [FreeFem-sources/laplace-lagrange-PETSc.edp at develop · FreeFem/FreeFem-sources · GitHub](https://github.com/FreeFem/FreeFem-sources/blob/develop/examples/hpddm/laplace-lagrange-PETSc.edp#L50-L54) for an similar example.

---

<div class="post-metadata">

### Author: ![zhugp123](https://avatars.discourse-cdn.com/v4/letter/z/71e660/32.png) [@zhugp123](https://community.freefem.org/u/zhugp123)
#### Post date: [April 5, 2023, 4:10pm UTC](https://community.freefem.org/t/obtain-eigenvectors-using-slepc-parallely/2379/6 "2023-04-05T16:10:12Z")

</div>

Dear Dr. Pierre Jolivet,  
Thanks for your kind reply, I will revise my code according to your suggestion and the attached example.
