# FFDDM transfert scalar field from complex FE space to real one

**URL:** https://community.freefem.org/t/ffddm-transfert-scalar-field-from-complex-fe-space-to-real-one/2047
**Category:** General Discussion
**Created:** [October 8, 2022, 8:42am UTC](https://community.freefem.org/t/ffddm-transfert-scalar-field-from-complex-fe-space-to-real-one/2047 "2022-10-08T08:42:07Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![altar31](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/altar31/32/910_2.png) [@altar31](https://community.freefem.org/u/altar31)
#### Post date: [October 8, 2022, 8:42am UTC](https://community.freefem.org/t/ffddm-transfert-scalar-field-from-complex-fe-space-to-real-one/2047/1 "2022-10-08T08:42:07Z")

</div>

Dear FreeFEM community,

I’m actually trying to solve a multiphysics problem in parallel using ffddm. I’m struggle to transfer some electromagnetic quantities (modulus) between two distributed finite element space : from a **complex** FE space to a **real** one on the **same mesh**. In order to do that, I’m trying to use **prfe#transferfromVhi(us,Vht,Pkt,rest)** as mentioned in the ffddm documentation but without success.  
Otherwise, I’ve tried using PETCs interfaces without success too…and find some post [Creating Mat from Mat](https://community.freefem.org/t/creating-mat-complex-from-mat-real/502) but this is beyond my actual FreeFEM skills…

How can I perform this kind of interpolation ?  
Otherwise, how can I save the **global real part** of the quantity of interest and load it in a new **real** distributed real finite element space (same mesh) ?

This is the pseudocode of my problem.

```auto

macro dimension 2// EOM           
include "ffddm.idp"
func Pk = P1;

// Domain decomposition
ffddmbuildDmesh( M ,Th , mpiCommWorld )
ffddmbuildDmesh( M2 ,Th , mpiCommWorld )

macro def(u)u// EOM // scalar field definition
macro init(u)u// EOM // scalar field initialization

ffddmbuildDfespace( FE , M , complex , def , init , Pk ) // FE complex space P1
ffddmbuildDfespace( FE2 , M2 , real , def , init , Pk ) // FE real space P1
FEVhi<complex> def(us) = ucomplex; // complex quantities of interest
FE2Vhi def(ut), def(ut2); // real space

FEtransferfromVhi(us,FE2Vhi,Pk,ut)
ut2 = real(ut); // take the real part
ffddmplot(FE,us,"u source");
ffddmplot(FE2,ut2,"u target"); // observe the real part on the new real Fespace 

```

Thanks in advance for your help !  
Best  
Damien

---

<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 8, 2022, 10:15am UTC](https://community.freefem.org/t/ffddm-transfert-scalar-field-from-complex-fe-space-to-real-one/2047/2 "2022-10-08T10:15:07Z")

</div>

What’s your issue with the PETSc code?

---

<div class="post-metadata">

### Author: ![phtournier](https://avatars.discourse-cdn.com/v4/letter/p/3da27b/32.png) [@phtournier](https://community.freefem.org/u/phtournier)
#### Post date: [October 8, 2022, 11:53am UTC](https://community.freefem.org/t/ffddm-transfert-scalar-field-from-complex-fe-space-to-real-one/2047/3 "2022-10-08T11:53:28Z")

</div>

If everything is happening on the same mesh you do not need any fancy transfer function, you can just take the real part of the local variables:

```auto
macro dimension 2// EOM           
include "ffddm.idp"
func Pk = P1;

// Domain decomposition
ffddmbuildDmesh( M ,Th , mpiCommWorld )

macro def(u)u// EOM // scalar field definition
macro init(u)u// EOM // scalar field initialization

ffddmbuildDfespace( FE , M , complex , def , init , Pk ) // FE complex space P1
ffddmbuildDfespace( FE2 , M , real , def , init , Pk ) // FE real space P1
FEVhi<complex> def(us) = ucomplex; // complex quantities of interest
FE2Vhi def(ut2); // real space

ut2 = real(us); // take the real part
// or: ut2[] = us[].re;

ffddmplot(FE,abs(us),"u source");
ffddmplot(FE2,ut2,"u target"); // observe the real part on the new real Fespace 

```

---

<div class="post-metadata">

### Author: ![altar31](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/altar31/32/910_2.png) [@altar31](https://community.freefem.org/u/altar31)
#### Post date: [October 10, 2022, 6:20am UTC](https://community.freefem.org/t/ffddm-transfert-scalar-field-from-complex-fe-space-to-real-one/2047/4 "2022-10-10T06:20:42Z")

</div>

Sorry for my delayed answer.

In fact, I have a problem which involve to first solve in a complex FEspace and after in a real one. So, I’m not able to mix “load PETSc-complex” with “load PETSc” which is apparently a known issue.

Thanks to phtournier answer, I’m able to solve my problem.

But now I have a new problem which is in the same topic. (maybe I have to open a new post).

Can I define a subdomain of my distributed global mesh and attached it to a new **real** FEspace ?  
I have the following constraints :

1. I will use a quantity computed previously in the complex global distributed domain as an input of my variational form in the subdomain.

I have been trying some attempts with the **trunc** function without success.

Please find some pseudocode for illustration of my problem

```auto
// ELECTROMAGNETIC PROBLEM 

ffddmbuildDmesh( M ,Th , mpiCommWorld ) // global distributed mesh
macro def(u)u// EOM // scalar field definition
macro init(u)u// EOM // scalar field initialization

ffddmbuildDfespace( FE , M , complex , def , init , Pk1 ) // complex distributed FE space

ffddmsetupOperator(PB,FE,Varf )
complex[int] bi(0); 
ffddmbuildrhs( PB , Varf , bi )
complex[int] x0(bi.n);
x0 = 0;
// One-lvl preconditionner
ffddmsetupPrecond( PB , Varf ) 
// solve the problem
FEVhi<complex> def(u),def(err),def(E),def(J),def(Q);
u[] = PBfGMRES(x0, bi, 1.e-6, 200, "right"); // iterative

PBwritesummary
ffddmplot(FE,abs(u), "Global solution");

// Compute some quantities of interest inside the distributed domain
E = -iomega*u; // electric field
J = (Sigma*E) ;
ffddmplot(FE,abs(J), "Eddy current ");
func J2 = J*(region==CylinderRegion); // global quantities computed on a specific domain of the mesh
FEVhi<complex> Jr = J2;
ffddmplot(FE,abs(Jr), "Eddy current cylinder");
Q = ((abs(Jr^2))/(2*sigmaCylinder)); // joule losses inside the cylinder
FEVhi Qr = (abs(Q)); 

// THERMAL PROBLEM

// would like to create a subdomain of the global distributed mesh wich i used Qr for solved a new problem
mesh Thbis = trunc(Th, x<=rCylinder);

// if a build a new distributed mesh i lost my actual decomposition... i don't want that
// ffddmbuildDmesh( M2 ,Thbis , mpiCommWorld ) 
ffddmbuildDfespace(FE2, M, real, def, init, P2)

ffddmplot(FE2,Qr, "Heat source restricted"); // doesn't plot on the restricted subdomain

// set up the thermal problem 
FE2Vhi u2, un2,Tp;
ffddmsetupOperator(PB2,FE2,Varf2 )
ffddmsetupPrecond(PB2, Varf2)

real[int] rhs(FE2Vhi.ndof);
un2 = 0; // initial guess

real t = 0; int iter=0; u2 = T0;
while (t <=tmax){    
    iter++; 
    t+=dt;
    Tp=u2; // update temperature
    ffddmbuildrhs(PB2, Varf2, rhs)
    u2[] = PB2fGMRES(un2[], rhs, 1.e-6, 200, "right");
    un2[] = u2[];
    cout<<"Tmax: "<<u2[].max << "Tmin: "<<u2[].min <<endl; 
}

ffddmplot(FE2,u2, "Temperature");
PB2writesummary

```

Thanks for your help !  
Best  
Damien

---

<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 10, 2022, 6:30am UTC](https://community.freefem.org/t/ffddm-transfert-scalar-field-from-complex-fe-space-to-real-one/2047/5 "2022-10-10T06:30:49Z")

</div>

> [@altar31](#):
>
> So, I’m not able to mix `load "PETSc-complex"` with `load "PETSc"` which is apparently a known issue.

Right, you need to stick with only one, e.g., `load "PETSc-complex"`.  
I don’t know how to deal with restricted global meshes in ffddm, FWIW, there is an exemple here using PETSc [https://github.com/FreeFem/FreeFem-sources/blob/develop/examples/hpddm/restriction-2d-PETSc.edp](https://github.com/FreeFem/FreeFem-sources/blob/develop/examples/hpddm/restriction-2d-PETSc.edp).

---

<div class="post-metadata">

### Author: ![altar31](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/altar31/32/910_2.png) [@altar31](https://community.freefem.org/u/altar31)
#### Post date: [October 10, 2022, 3:37pm UTC](https://community.freefem.org/t/ffddm-transfert-scalar-field-from-complex-fe-space-to-real-one/2047/6 "2022-10-10T15:37:01Z")

</div>

Thanks for the reference.  
I will try the “PETSc way” to perform the subdomain restriction.

I keep you informed.

---

<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 10, 2022, 4:12pm UTC](https://community.freefem.org/t/ffddm-transfert-scalar-field-from-complex-fe-space-to-real-one/2047/7 "2022-10-10T16:12:12Z")

</div>

If you provide a full working example, I can help you out. In the current code you pasted, there are a bunch of macro (mostly for the `varf`) that are undefined, and I thus cannot run this on my end. The mesh is also not defined.
