# Is it possible to access values of a P1 element at the nodes?

**URL:** <https://community.freefem.org/t/is-it-possible-to-access-values-of-a-p1-element-at-the-nodes/51>\
**Category:** General Discussion\
**Created:** [May 16, 2019, 12:22am UTC](https://community.freefem.org/t/is-it-possible-to-access-values-of-a-p1-element-at-the-nodes/51 "2019-05-16T00:22:31Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Neutrino](https://avatars.discourse-cdn.com/v4/letter/n/bb73d2/32.png) [@Neutrino](https://community.freefem.org/u/Neutrino)\
**Post date:** [May 16, 2019, 12:22am UTC](https://community.freefem.org/t/is-it-possible-to-access-values-of-a-p1-element-at-the-nodes/51/1 "2019-05-16T00:22:31Z")

</div>

Hi there!  
Is it possible to access values of a P1 function at the nodes and put them in an array?

Thank you.

---

<div class="post-metadata">

**Author:** ![simon.garnotel](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/simon.garnotel/32/11_2.png) [@simon.garnotel](https://community.freefem.org/u/simon.garnotel)\
**Post date:** [May 16, 2019, 5:55am UTC](https://community.freefem.org/t/is-it-possible-to-access-values-of-a-p1-element-at-the-nodes/51/2 "2019-05-16T05:55:47Z")

</div>

Finite element function can be automatically converted in array, you have to write:

```auto
fespace Uh(Th, P1);
Uh u = ...;

cout << u[] << endl;
cout << u[][5] << endl;

```

`u[]` is the array of `u`, `u[][5]` is the 6th element of the array.

---

<div class="post-metadata">

**Author:** ![Neutrino](https://avatars.discourse-cdn.com/v4/letter/n/bb73d2/32.png) [@Neutrino](https://community.freefem.org/u/Neutrino)\
**Post date:** [May 16, 2019, 9:42am UTC](https://community.freefem.org/t/is-it-possible-to-access-values-of-a-p1-element-at-the-nodes/51/3 "2019-05-16T09:42:05Z")

</div>

Thank you for the quick answer.

I have another question. Given a mesh Th and a function u in Vh, how do I get the value of u at the node Th[i][j]?

---

<div class="post-metadata">

**Author:** ![simon.garnotel](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/simon.garnotel/32/11_2.png) [@simon.garnotel](https://community.freefem.org/u/simon.garnotel)\
**Post date:** [May 16, 2019, 8:25pm UTC](https://community.freefem.org/t/is-it-possible-to-access-values-of-a-p1-element-at-the-nodes/51/4 "2019-05-16T20:25:35Z")

</div>

The mesh node is obtained using `Th(i)` (not `Th[i][j]`, this is triangle `i`, vertex `j`, see [documentation](https://doc.freefem.org/documentation/mesh-generation.html#mesh-connectivity-and-data)).

You have access to the value at node `i` using `u[][i]`, or alternatively `u(Th(i).x, Th(i).y)` (using interpolation).

---

<div class="post-metadata">

**Author:** ![franco](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/franco/32/30_2.png) [@franco](https://community.freefem.org/u/franco)\
**Post date:** [May 30, 2019, 3:20pm UTC](https://community.freefem.org/t/is-it-possible-to-access-values-of-a-p1-element-at-the-nodes/51/5 "2019-05-30T15:20:21Z")

</div>

How about in the case of a vector

```
fespace Vh(Th,[P1,P1]);
Vh [ux, uy];

```

I gues it should be the same but when I try to output this to file like the following code:

```
{
            ofstream cupf("file.txt");
            cupf.precision(8);
            cupf.scientific;
            for (int i=0; i < Th.nv; i++)
            {
                cupf << Th(i).x << " " << Th(i).y << " " << ux[][i] << " " << uy[][i] << endl;
            }
            cupf.flush; //to flush the buffer of file
}

```

It gives me the exact same values for ux than for uy, which I know is not true because I also plot this vector field and it looks OK. Maybe I’m doing something wrong with the way I access the values since it is a vector? Any ideas are highly appreciated!

---

<div class="post-metadata">

**Author:** ![simon.garnotel](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/simon.garnotel/32/11_2.png) [@simon.garnotel](https://community.freefem.org/u/simon.garnotel)\
**Post date:** [May 30, 2019, 4:34pm UTC](https://community.freefem.org/t/is-it-possible-to-access-values-of-a-p1-element-at-the-nodes/51/6 "2019-05-30T16:34:18Z")

</div>

> [@franco](#):
>
> cupf \<\< Th(i).x \<\< " " \<\< Th(i).y \<\< " " \<\< ux[i] \<\< " " \<\< uy[i] \<\< endl; }

You have to write:

```freefem
ux[][2*i] << " " << ux[][2*i+1]

```

Remark: `ux[]` and `uy[]` are the same objects

---

<div class="post-metadata">

**Author:** ![franco](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/franco/32/30_2.png) [@franco](https://community.freefem.org/u/franco)\
**Post date:** [May 30, 2019, 5:31pm UTC](https://community.freefem.org/t/is-it-possible-to-access-values-of-a-p1-element-at-the-nodes/51/7 "2019-05-30T17:31:24Z")

</div>

Thanks a lot! It worked. I think this should be in the manual since it is quite confusing that you can define the vectors using component names for the variables but accessing the elements has a different format.

---

<div class="post-metadata">

**Author:** ![simon.garnotel](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/simon.garnotel/32/11_2.png) [@simon.garnotel](https://community.freefem.org/u/simon.garnotel)\
**Post date:** [May 30, 2019, 5:55pm UTC](https://community.freefem.org/t/is-it-possible-to-access-values-of-a-p1-element-at-the-nodes/51/8 "2019-05-30T17:55:50Z")

</div>

Thanks for your feedback.  
I will add a clearer description of vectorial finite elements soon in the documentation

---

<div class="post-metadata">

**Author:** ![franco](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/franco/32/30_2.png) [@franco](https://community.freefem.org/u/franco)\
**Post date:** [May 30, 2019, 6:58pm UTC](https://community.freefem.org/t/is-it-possible-to-access-values-of-a-p1-element-at-the-nodes/51/9 "2019-05-30T18:58:35Z")

</div>

Your welcome and thanks to you.

Another related question, now for P2 elements. In the same script along with the vectors I also have P2 scalar variables:

```
fespace Vh(Th,[P1,P1]);
fespace Sh2(Th, P2);
Vh [ux, uy];
Sh2 c;

```

But when I try to write the values to file in the same way and together with the vector values like this, using your suggestion

```
{
                ofstream cupf("file.txt");
                cupf.precision(16);
                cupf.scientific;
                for (int i=0; i < Th.nv; i++)
                {
                    cupf << Th(i).x << " " << Th(i).y << " " << c[][i] << " " << ux[][2*i] << " " << ux[][2*i+1] << endl;
                }
                cupf.flush; //to flush the buffer of file
}

```

The vector component values seem OK now but the scalar value, c variable, does not. The plot directly from FreeFem built in command looks fine but if I plot the scalar c from the data file (I’m working with Matplotlib in Python) it looks like the same values but different distribution, like scrambled…

 ![Figure_2](https://canada1.discourse-cdn.com/flex030/uploads/freefem/original/1X/731851a6cd9148bfc0b1b273976b8bb018823004.png)  
maybe the order is different because it’s a different type of element? even if the mesh is the same?

---

<div class="post-metadata">

**Author:** ![simon.garnotel](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/simon.garnotel/32/11_2.png) [@simon.garnotel](https://community.freefem.org/u/simon.garnotel)\
**Post date:** [May 30, 2019, 7:03pm UTC](https://community.freefem.org/t/is-it-possible-to-access-values-of-a-p1-element-at-the-nodes/51/10 "2019-05-30T19:03:50Z")

</div>

It’s more complicated with a P2 finite element: your degrees of freedom are not only the mesh nodes.  
A simple way to do that is to write:

```auto
c(Th(i).x, Th(i).y)

```

(That use interpolation)

---

<div class="post-metadata">

**Author:** ![Yanma](https://avatars.discourse-cdn.com/v4/letter/y/ee59a6/32.png) [@Yanma](https://community.freefem.org/u/Yanma)\
**Post date:** [August 29, 2024, 5:42am UTC](https://community.freefem.org/t/is-it-possible-to-access-values-of-a-p1-element-at-the-nodes/51/11 "2024-08-29T05:42:02Z")

</div>

It didn’t work. I have the table value in txt file written in format “x \t y \t fxy”, and read the data as per following code:  
mesh Th2 = triangulate(tempFileName);  
fespace Vh2(Th2,P1);  
Vh2 fxy;  
{  
ifstream temp(tempFileName);  
for(int i = 0; i \< fxy.n; i++)  
{  
real x1,y1;  
temp \>\> x1 \>\> y1 \>\> fxy[i]; //to read third column only  
}  
}  
Looking at plot of the fxy, the file is read normally and right. Then, I needed function values in vertices of a triangle containing the described coordinate, say, (0, -39):  
int N = Th2(0,-39).nuTriangle;  
cout \<\< "Number of triangle containing (0,-39) is "\<\< N \<\< endl;  
for (int i = 0; i\<3; ++i)  
{  
int xi = Th2[N][i].x;  
int yi = Th2[N][i].y;  
cout \<\< “Vertix” \<\< i \<\< "x = " \<\< xi \<\< ", y = " \<\< yi \<\< endl;  
cout \<\< "Function value is " \<\< fxy(Th2(N).x, Th2(N).y \<\< endl;  
}  
I received result  
Vertix0x = 1, y = -39  
Function value is 0.919753  
while the function in stated coordinates (x y fxy) is 1 -39 0.6419752836227417.  
I tried also fxy[N] or fxy[i], it gave me N-th and i-th values of function in the table (I mean, values in N-th and i-th row of the table) regardless the vertix coordinate.  
Attempt fxy[N][i] failed: Error line number 131, in file c:\projects\FreeFEM\Bubble-1.edp, before token ]. Seems like such syntax is not allowed.

---

<div class="post-metadata">

**Author:** ![Yanma](https://avatars.discourse-cdn.com/v4/letter/y/ee59a6/32.png) [@Yanma](https://community.freefem.org/u/Yanma)\
**Post date:** [August 29, 2024, 6:00am UTC](https://community.freefem.org/t/is-it-possible-to-access-values-of-a-p1-element-at-the-nodes/51/12 "2024-08-29T06:00:33Z")

</div>

But the following appeared as working:  
cout \<\< "Function value is " \<\< fxy(Th2[N][i].x,Th2[N][i].y) \<\< endl;
