# Perform integrals in func

**URL:** <https://community.freefem.org/t/perform-integrals-in-func/563>\
**Category:** General Discussion\
**Created:** [September 2, 2020, 10:44am UTC](https://community.freefem.org/t/perform-integrals-in-func/563 "2020-09-02T10:44:50Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![nicoloabrate](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/nicoloabrate/32/1242_2.png) [@nicoloabrate](https://community.freefem.org/u/nicoloabrate)\
**Post date:** [September 2, 2020, 10:44am UTC](https://community.freefem.org/t/perform-integrals-in-func/563/1 "2020-09-02T10:44:50Z")

</div>

Dear FF developers,  
I would like to ask you how to correct the following error,

```
2 : {
3 : Vh w;
4 : w[] = u;
5 : real tI;
6 : tI = int2d(core) (w);
7 : cout<<tI<<endl;
8 : return tI;Impossible to cast <Pd> in <3KN_IdE>
     ( <3KN_IdE> : <P3KNMIdE> )
     ( <3KN_IdE> : <P2KNIdE> )

```

` Error line number 8, in file FFutils/tilt.idp, before token ;`

which comes out when I call this function inside my script,

```
func real[int] tilt(real[int] & u)
  {
    Vh w;
    w[] = u;
    real tI;
    tI = int2d(core) (w);
    cout<<tI<<endl;
    return tI;
  }

```

The variable `u` is defined in the main script as

```
fespace Vh(core,P1);
Vh u;

```

My goal is to perform some integrations over the mesh involving different `fespace` arrays. Since I have to perform many integrations over the domain for each `fespace` array I define, I need to use an external function.

Thank you so much for any help  
Nicolo’

---

<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:** [September 2, 2020, 8:13pm UTC](https://community.freefem.org/t/perform-integrals-in-func/563/2 "2020-09-02T20:13:14Z")

</div>

`tI` is a `real`, your function is supposed to return a `real[int]`, you either need to fix the function definition or the declaration of `tI`.

---

<div class="post-metadata">

**Author:** ![nicoloabrate](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/nicoloabrate/32/1242_2.png) [@nicoloabrate](https://community.freefem.org/u/nicoloabrate)\
**Post date:** [September 3, 2020, 5:47am UTC](https://community.freefem.org/t/perform-integrals-in-func/563/3 "2020-09-03T05:47:20Z")

</div>

Thank you prj,  
I am sorry for having posted an incorrect version of the code. I tried many things so I posted the wrong version…You are absolutely right, the error message that worries me is the following one, obtained when I declare that the output will be a `real`:

```
  247 : real t1 = tilt(phi1) error operator ( <St4pairIP6FEbaseId5v_fesEiE>
 List of choices
         ( <d> : <P2KNIdE> )

```

I cannot understand the variable types `<St4pairIP6FEbaseId5v_fesEiE>`, `<d>` and `<P2KNIdE>`, so it is quite challenging to find out the syntax error…  
Thank you for your attention  
Nicolo

---

<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:** [September 3, 2020, 5:48am UTC](https://community.freefem.org/t/perform-integrals-in-func/563/4 "2020-09-03T05:48:55Z")

</div>

What is `phi1`? Post your full (minimalist) example, otherwise we can’t help you.

---

<div class="post-metadata">

**Author:** ![nicoloabrate](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/nicoloabrate/32/1242_2.png) [@nicoloabrate](https://community.freefem.org/u/nicoloabrate)\
**Post date:** [September 3, 2020, 6:14am UTC](https://community.freefem.org/t/perform-integrals-in-func/563/5 "2020-09-03T06:14:51Z")

</div>

I am sorry. `phi1` is defined as  
`fespace Vh(core,P1);`  
`Vh phi1`  
The minimal example (excluding the mesh generation) is

```
func Pk = [P1, P1];

fespace Vh(core,P1);
fespace VhV(core,Pk);
Vh phi1, phi2, phic1, phic2, v1, v2, pow, phiTOT;

// define variational forms
varf leakage([phi1,phi2],[v1,v2]) = int2d(core) (div(DFC[0], phi1, v1) + RXS[0]*phi1*v1 - S1[2]*phi2*v1
                                                 +div(DFC[1], phi2, v2) + RXS[1]*phi2*v2 - S1[1]*phi1*v2)
                                                 +on(1,2,3,4, phi1=0, phi2=0);

varf fission([phi1,phi2],[v1,v2]) = int2d(core) (CHIT[0]*NSF[0]*phi1*v1+CHIT[0]*NSF[1]*phi2*v1
                                                 +CHIT[1]*NSF[0]*phi1*v2+CHIT[1]*NSF[1]*phi2*v2);

// build operators
matrix<real> L = leakage(VhV, VhV, tgv = tgv),
             F = fission(VhV, VhV, tgv = tgv);

Mat DistL(L, clean = true);
Mat DistF(DistL, F, clean = true);

real sigma=0;
int nev=6;
real[int] ev(nev), evc(nev); // to store nev eigenvalues
VhV<real>[int] [e1,e2](nev), [ec1,ec2](nev); // to store nev eigenvectors

// Parameters for the distributed EigenValue solver
string ssparams = " -eps_nev " + nev + // Number of eigenvalues
                  " -eps_type krylovschur" +
                  " -eps_target "+ sigma + // Shift value
                  " -st_type sinvert " +
                  " -st_pc_type lu " +
                  " -st_pc_factor_mat_solver_type mumps" +
                  " -eps_view"; // coree problem is symmetric

int k = EPSSolve(DistL, // matrix OP = A − sigma*B
                 DistF, //
                 vectors = e1, // Array to store the FEM-EigenFunctions
                 values = ev, // Array to store the EigenValues
                 sparams = ssparams // Parameters for the distributed EigenValue solver
                 );

k=min(k,nev); // some time the number of converged eigen value can be greater than nev;

// sort eigenvalues
int[int] ind = 0:1:k-1;
phi1 = e1[ind[0]];
phi2 = e2[ind[0]];
real t1 = tilt(phi1);
```

---

<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:** [September 3, 2020, 6:19am UTC](https://community.freefem.org/t/perform-integrals-in-func/563/6 "2020-09-03T06:19:48Z")

</div>

No definition of `tilt` in your snippet: impossible to run the code =\> (almost) impossible to help… I guess you need to change `real t1 = tilt(phi1)` to `real t1 = tilt(phi1[])`, but I’m not sure since I can’t run the code.

---

<div class="post-metadata">

**Author:** ![nicoloabrate](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/nicoloabrate/32/1242_2.png) [@nicoloabrate](https://community.freefem.org/u/nicoloabrate)\
**Post date:** [September 3, 2020, 6:45am UTC](https://community.freefem.org/t/perform-integrals-in-func/563/7 "2020-09-03T06:45:03Z")

</div>

I am sorry for taking it for granted but `tilt` is, of course, the function defined in the first post!

```auto
func real tilt(real[int] & u)
  {
    Vh w;
    w[] = u;
    real tI;
    tI = int2d(core) (w);
    cout<<tI<<endl;
    return tI;
  }

```

With you suggestion, now the code works fine, thank you so much for your patience and sorry for not having provided you a complete example from beginning.  
If I may, I would suggest you to add a section in the docs where the variable types are specified ( `<d>`, `<P2KNIdE>` and so on). This would help a lot to fill some gaps in the docs and to help users to understand their syntax errors…  
Thank you again,  
Nicolo

---

<div class="post-metadata">

**Author:** ![julienG](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/julieng/32/222_2.png) [@julienG](https://community.freefem.org/u/julienG)\
**Post date:** [September 3, 2020, 9:40am UTC](https://community.freefem.org/t/perform-integrals-in-func/563/8 "2020-09-03T09:40:50Z")

</div>

I guess it should be `real t1 = tilt(phi1[]);` in the last line, since the function expects array…
