# Real\[int\] from varf in PETSc

**URL:** <https://community.freefem.org/t/real-int-from-varf-in-petsc/2267>\
**Category:** General Discussion\
**Created:** [February 6, 2023, 9:47pm UTC](https://community.freefem.org/t/real-int-from-varf-in-petsc/2267 "2023-02-06T21:47:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmd](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/cmd/32/67_2.png) [@cmd](https://community.freefem.org/u/cmd)\
**Post date:** [February 6, 2023, 9:47pm UTC](https://community.freefem.org/t/real-int-from-varf-in-petsc/2267/1 "2023-02-06T21:47:06Z")

</div>

Hi FreeFEM community,

I have noticed some strange behavior when I use a `varf` to create a `real[int]` in PETSc, and I’m not sure if this is a bug or my own misunderstanding. Please see that attached MWE (I’ve added some explainer text that will print via cout when the script is run). Note that the discrepancies referred to in this script are tiny, but in my actual application, the discrepancies are resulting in a division by 0. I imagine this must have something to do with ghost cell values? Any insight will be helpful!

Cheers,  
Chris

[test.edp](https://community.freefem.org/uploads/short-url/dxsl3sNpxN5VuKWBvFW7uPOj8zX.edp) (2.2 KB)

---

<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:** [February 7, 2023, 6:10am UTC](https://community.freefem.org/t/real-int-from-varf-in-petsc/2267/2 "2023-02-07T06:10:44Z")

</div>

If I use `-pc_type lu`, I get:

```auto
$ ff-mpirun -n 4 test.edp -v 0
  CASE 1: serial (w/o PETSc)
	value 0 = 0.41000000000000031
	value 1 = 0.41000000000000031
	value 2 = 0.41000000000000031
  All values are equal.   
  CASE 2: with PETSc
	value 0 = 0.41000000000007553
	value 1 = 0.41000000000000031
	value 2 = 0.41000000000000031
  I understand why value 0 and value 1 may not be equal... 
  But why does value 1 not equal value 2 in case 2 (even with 1 proc)? 

```

So the values are equal up to machine epsilon.

---

<div class="post-metadata">

**Author:** ![cmd](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/cmd/32/67_2.png) [@cmd](https://community.freefem.org/u/cmd)\
**Post date:** [February 7, 2023, 6:37am UTC](https://community.freefem.org/t/real-int-from-varf-in-petsc/2267/3 "2023-02-07T06:37:29Z")

</div>

Thanks, Pierre. I will try to come up with a better MWE this afternoon. In my problem, I was using a direct solver.

In the meantime, can you see any reason why a ` real[int]` constructed from `varf1`(where the test function is written into the form AND BC) might not be equal to one built from `varf2` (where the test function only appears in the BC)? Of course, I’m referring to a case where the test function does not change between these two situations.

---

<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:** [February 7, 2023, 8:42am UTC](https://community.freefem.org/t/real-int-from-varf-in-petsc/2267/4 "2023-02-07T08:42:38Z")

</div>

I’m not sure I understand your question, as both `varf` in your code return the same vector.

---

<div class="post-metadata">

**Author:** ![cmd](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/cmd/32/67_2.png) [@cmd](https://community.freefem.org/u/cmd)\
**Post date:** [February 7, 2023, 9:11am UTC](https://community.freefem.org/t/real-int-from-varf-in-petsc/2267/5 "2023-02-07T09:11:45Z")

</div>

My apologies, let me clarify with a better MWE:  
[test2.edp](https://community.freefem.org/uploads/short-url/ue4m9PeHL493v94fA05jKqTeL3P.edp) (1.0 KB)

The main part of interest is here:

```auto
varf varf1( def(u2), def(v) ) = int2d(Th) ( f(u1,u2,v) ) + on(inds, u2 = 0);
varf varf2( def(u3), def(v) ) = int2d(Th) ( f(u1,u2,v) ) + on(inds, u3 = 0);
A = varf1(Vh, Vh, tgv = -10);
real[int] vec0 = A*u2[];
real[int] vec1 = varf1(0, Vh, tgv=-10);
real[int] vec2 = varf2(0, Vh, tgv=-10);

```

I would expect all of these to `vec` to be equal (within machine precision). However, I find that, while `vec0` and `vec2` are equal, `vec1` is different (it is zero). Am I doing something wrong/is that expected?

---

<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:** [February 7, 2023, 9:22am UTC](https://community.freefem.org/t/real-int-from-varf-in-petsc/2267/6 "2023-02-07T09:22:16Z")

</div>

Your `varf1` has no linear part, only bilinear. So if you assemble a vector out of it, you get nothing.

---

<div class="post-metadata">

**Author:** ![cmd](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/cmd/32/67_2.png) [@cmd](https://community.freefem.org/u/cmd)\
**Post date:** [February 7, 2023, 9:32am UTC](https://community.freefem.org/t/real-int-from-varf-in-petsc/2267/7 "2023-02-07T09:32:27Z")

</div>

Indeed, this was a pretty fundamental misunderstanding on my part. Thank you for clarifying!
