# How to get values of a pde at every position in the space?

**URL:** <https://community.freefem.org/t/how-to-get-values-of-a-pde-at-every-position-in-the-space/278>\
**Category:** General Discussion\
**Created:** [February 28, 2020, 9:19am UTC](https://community.freefem.org/t/how-to-get-values-of-a-pde-at-every-position-in-the-space/278 "2020-02-28T09:19:30Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mordokkai](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@Mordokkai](https://community.freefem.org/u/Mordokkai)\
**Post date:** [February 28, 2020, 9:19am UTC](https://community.freefem.org/t/how-to-get-values-of-a-pde-at-every-position-in-the-space/278/1 "2020-02-28T09:19:30Z")

</div>

Hi,

I may be asking a stupid question, but how can I get the values of the fespace after the problem was solved. Let’s say, I implemented a diffusion pde. I can plot it with the plot function of freefem++. But how can I get the value of the function for each position in the space ? I would say I need to access every finite elements, its geometry and value. Or maybe there is a function to do that.

Mordokkai

---

<div class="post-metadata">

**Author:** ![frederichecht](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/frederichecht/32/15_2.png) [@frederichecht](https://community.freefem.org/u/frederichecht)\
**Post date:** [February 28, 2020, 10:12am UTC](https://community.freefem.org/t/how-to-get-values-of-a-pde-at-every-position-in-the-space/278/2 "2020-02-28T10:12:28Z")

</div>

HI,

I think, you question is a little be strange because, to get the valeur a point (x0,y0) in 2d of finite element function u  
juste du u(x0,y0) (same in 3d with u(x0,y0,z0) )

Event for basis function like (sin(x)) you do not know the value at each point

this peace of code given a idea:  
// value a each node of any Lagrange Finite element. here P2

mesh Th=square(10,10);  
fespace Vh(Th,P2);

Vh xx=x, yy=x;  
Vh u =sin(x)\*cos(y);

for(int i=0; i\<Vh.ndof; ++i)  
cout \<\< xx[][i] \<\< " " \<\< yy[][i] \<\< " u = "\<\< u[][i] \<\< endl;

---

<div class="post-metadata">

**Author:** ![Mordokkai](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@Mordokkai](https://community.freefem.org/u/Mordokkai)\
**Post date:** [February 28, 2020, 10:39am UTC](https://community.freefem.org/t/how-to-get-values-of-a-pde-at-every-position-in-the-space/278/3 "2020-02-28T10:39:00Z")

</div>

Thanks for your answer.

> I think, you question is a little be strange because, to get the valeur a point (x0,y0) in 2d of finite element function u  
> juste du u(x0,y0) (same in 3d with u(x0,y0,z0) )

That’s the first thing I tried but I got strange results.  
Below you can find the result plot by FreeFem++: plot(u, wait=true, fill=true, dim=2);

 ![im2](https://canada1.discourse-cdn.com/flex030/uploads/freefem/original/1X/55f423a6789ed496fa01a2cf9d6352dbcdbbc2f9.png)

Then I try to write everything in a text file:  
ofstream out (“out.txt” );  
out.fixed;  
for(int i=0; i\<dims[0]; i++){  
for(int j=0; j\<dims[1]; j++){  
out \<\< u(i,j) \<\< " ";  
}  
out \<\< endl;  
}

And then I try to reopen it with python matplotlib:  
import matplotlib.pyplot as plt  
import numpy as np  
mat = np.loadtxt(“out.txt”)  
plt.matshow(mat)  
plt.show()

And I get the following result:

---

<div class="post-metadata">

**Author:** ![Mordokkai](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@Mordokkai](https://community.freefem.org/u/Mordokkai)\
**Post date:** [February 28, 2020, 10:39am UTC](https://community.freefem.org/t/how-to-get-values-of-a-pde-at-every-position-in-the-space/278/4 "2020-02-28T10:39:31Z")

</div>

![im4](https://canada1.discourse-cdn.com/flex030/uploads/freefem/original/1X/44f3e0465d84e307578deb538460d6abaf1c1ed9.png)

---

<div class="post-metadata">

**Author:** ![Mordokkai](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@Mordokkai](https://community.freefem.org/u/Mordokkai)\
**Post date:** [March 2, 2020, 1:08pm UTC](https://community.freefem.org/t/how-to-get-values-of-a-pde-at-every-position-in-the-space/278/5 "2020-03-02T13:08:31Z")

</div>

If I take a basic example:

real[int] dims = [50,50];  
border a(t=0, dims[0]){x=t; y=0; label=1;};  
border b(t=0, dims[1]){x=dims[0]; y=t; label=2;};  
border f(t=0, dims[0]){x=dims[0]-t; y=dims[1]; label=3;};  
border d(t=0, dims[1]){x=0; y=dims[1]-t; label=4;};  
border g(t=10, dims[0]-10){x=t; y=10; label=5;};  
border h(t=10, dims[1]-10){x=dims[0]-10; y=t; label=6;};  
border i(t=10, dims[0]-10){x=dims[0]-t; y=dims[1]-10; label=7;};  
border j(t=10, dims[1]-10){x=10; y=dims[1]-t; label=8;};

int n=3;  
mesh fm = buildmesh(a(n) + b(n) + f(n) + d(n) + g(-n) + h(-n) + i(-n) + j(-n));  
plot(fm);

fespace Vh(fm, P1);

func real heatsource(){  
return 0;  
}

Vh u, v;  
Vh myHeat = heatsource();

problem heat(u,v) = int2d(fm)(dx(u)\*dx(v) + dy(u)_dy(v)) - int2d(fm)(myHeat_v)  
+ on(1, u=0) + on(2, u=0) + on(3, u=0) + on(4, u=0) + on(5, u=0) + on(6, u=0)  
+ on(7, u=10) + on(8, u=0);  
heat;  
plot(u, wait=true, fill=true, dim=2);

ofstream out (“map.txt” );  
out.fixed;  
for(int i=0; i\<dims[0]; i++){  
for(int j=0; j\<dims[1]; j++){  
out \<\< u(i,j) \<\< " ";  
}  
out \<\< endl;  
}

Then freefem display:  
 ![freefem](https://canada1.discourse-cdn.com/flex030/uploads/freefem/original/1X/e32f8ed2a5f96fd067cc399aa2a80f07c69dad88.png)

While matplotlib display:  
 ![matplotlib](https://canada1.discourse-cdn.com/flex030/uploads/freefem/original/1X/4ce365ff6f1b7e715f9b7ca1defa8578c1f49f21.png)

It seems like some values are assigned inside the interior square, while not displayed by freefem

---

<div class="post-metadata">

**Author:** ![denis.crete](https://avatars.discourse-cdn.com/v4/letter/d/9d8465/32.png) [@denis.crete](https://community.freefem.org/u/denis.crete)\
**Post date:** [March 4, 2020, 9:30pm UTC](https://community.freefem.org/t/how-to-get-values-of-a-pde-at-every-position-in-the-space/278/6 "2020-03-04T21:30:11Z")

</div>

Hello  
I often use something like (in the case of magnetic field created by a wire)  
for (int i=0;i\<Th.nt;i++)  
{ for (int j=0; j \<3; j++)  
ff\<\<Th[i][j].x \<\< " "\<\< Th[i][j].y\<\< " "\<\<Bx[][Vh(i,j)]\<\< " "\<\<By[][Vh(i,j)]\<\<endl;  
ff\<\<Th[i][0].x \<\< " "\<\< Th[i][0].y\<\< " “\<\<Bx[][Vh(i,0)]\<\< " “\<\<By[][Vh(i,0)]\<\<”\n\n\n”;  
}  
savemesh(Th,“wire.msh”);

HTH  
Denis

---

<div class="post-metadata">

**Author:** ![Mordokkai](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@Mordokkai](https://community.freefem.org/u/Mordokkai)\
**Post date:** [March 10, 2020, 9:15am UTC](https://community.freefem.org/t/how-to-get-values-of-a-pde-at-every-position-in-the-space/278/7 "2020-03-10T09:15:30Z")

</div>

Thanks for your help. I think my code in FreeFem++ was ok. It was just the way to display it with opencv or matplotlib that was wrong.  
Sorry for that

---

<div class="post-metadata">

**Author:** ![frederichecht](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/frederichecht/32/15_2.png) [@frederichecht](https://community.freefem.org/u/frederichecht)\
**Post date:** [March 12, 2020, 5:51pm UTC](https://community.freefem.org/t/how-to-get-values-of-a-pde-at-every-position-in-the-space/278/8 "2020-03-12T17:51:52Z")

</div>

Dear user  
just a small remark,

freefem dowhen the point is outside the domain depend of the finite element.

2 case if the finite element is continuous then the we get a continuous prolongement  
and the finite is discontinuous then the prolongement is zero.

In all your case you have a hole in the mesh so the finite element is not clearly difined

Remark: you can use the fonction chi(Th)(xx,yy) to know if the point (xx,yy) is in the mesh Th.
