# Behavior of varf with rhs and on()

**URL:** <https://community.freefem.org/t/behavior-of-varf-with-rhs-and-on/3248>\
**Category:** General Discussion\
**Created:** [May 14, 2024, 10:27am UTC](https://community.freefem.org/t/behavior-of-varf-with-rhs-and-on/3248 "2024-05-14T10:27:21Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![fb77](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/fb77/32/3796_2.png) [@fb77](https://community.freefem.org/u/fb77)\
**Post date:** [May 14, 2024, 10:27am UTC](https://community.freefem.org/t/behavior-of-varf-with-rhs-and-on/3248/1 "2024-05-14T10:27:21Z")

</div>

Can anyone comment on the extraction of the linear part in varf() ?  
There is an issue of sign, it seems that it is treated differently depending if it is an integral source or an on() boundary condition. The simple code

```auto
int n=3;
mesh Th=square(n,n);
//plot(Th);

real a=1.,b=1.;

fespace Vh(Th,P1);
Vh u,v;
real[int] tab(Vh.ndof),sol(Vh.ndof),solprob(Vh.ndof),diff(Vh.ndof);

solve prob(u,v)
=int2d(Th)(dx(u)*dx(v)+dy(u)*dy(v))
-int2d(Th)(a*v)
+on(1,2,3,4,u=b)
;
solprob=u[];
//cout << "solprob = " << solprob << endl;

varf test(u,v)
=int2d(Th)(dx(u)*dx(v)+dy(u)*dy(v))
-int2d(Th)(a*v)
+on(1,2,3,4,u=b)
;
matrix A=test(Vh,Vh);
tab=test(0,Vh);
tab=-tab;// it seems necessary for the source "a", but not for boundary "b"
sol=A^-1*tab;
//cout << "sol = " << sol << endl;

diff=solprob-sol;
cout << "error = " << sqrt(diff'*diff) << endl;

```

considers either solve() or varf() with the same problem, but they do not give the same solution.  
??

---

<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:** [May 14, 2024, 12:04pm UTC](https://community.freefem.org/t/behavior-of-varf-with-rhs-and-on/3248/2 "2024-05-14T12:04:50Z")

</div>

`solve` supposes the linear and bilinear form represent the LHS and RHS of an equation system. `varf` defines a linear or bilinear form that is not necessarily part of an equation.

Since you impose inhomogeneous BC, the sign flip in the latter is changing the BC, not just the forcing.

```auto
varf test(u,v)
=int2d(Th)(dx(u)*dx(v)+dy(u)*dy(v))
+int2d(Th)(a*v) // note: sign flip is here now
+on(1,2,3,4,u=b)
;
matrix A=test(Vh,Vh);
tab=test(0,Vh);
// line removed here
sol=A^-1*tab;

```

Another equivalent option would be:

```auto
varf test(u,v)
=int2d(Th)(dx(u)*dx(v)+dy(u)*dy(v))
-int2d(Th)(a*v)
+on(1,2,3,4,u=-b) // note: sign flip is here now
;
matrix A=test(Vh,Vh);
tab=test(0,Vh);
tab=-tab;// it seems necessary for the source "a", but not for boundary "b"
sol=A^-1*tab;

```

---

<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:** [May 14, 2024, 12:25pm UTC](https://community.freefem.org/t/behavior-of-varf-with-rhs-and-on/3248/3 "2024-05-14T12:25:51Z")

</div>

Duplicate of [Change of sign in linear part of variational problem (varf vs problem)](https://community.freefem.org/t/change-of-sign-in-linear-part-of-variational-problem-varf-vs-problem/2984).

---

<div class="post-metadata">

**Author:** ![fb77](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/fb77/32/3796_2.png) [@fb77](https://community.freefem.org/u/fb77)\
**Post date:** [May 14, 2024, 12:57pm UTC](https://community.freefem.org/t/behavior-of-varf-with-rhs-and-on/3248/4 "2024-05-14T12:57:01Z")

</div>

Thank you. It is quite puzzling that `solve` and `varf` do not use the same conventions. Additionally in `varf` the convention for `int2d` or `int1d` is opposite to that of `on()`. Then I have to remind of it when changing from one formulation to the other. Setting `u=-b` instead of `u=b` is very much counterintuitive (your second option). The first option is also counterintuitive since it looks like we are not solving the right equation (-\Delta u+a=0 instead of -\Delta u-a=0).

---

<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:** [May 14, 2024, 2:03pm UTC](https://community.freefem.org/t/behavior-of-varf-with-rhs-and-on/3248/5 "2024-05-14T14:03:17Z")

</div>

> Additionally in `varf` the convention for `int2d` or `int1d` is opposite to that of `on()`

You are thinking that there is a convention, but there is none. `int2d` or `int1d` are used to discretise linear or bilinear form (which may be related or not, as Chris highlighted). `on()` is used to impose Dirichlet boundary conditions. That’s two different things, hence there is no convention between one and the other.

---

<div class="post-metadata">

**Author:** ![quentin](https://avatars.discourse-cdn.com/v4/letter/q/e47c2d/32.png) [@quentin](https://community.freefem.org/u/quentin)\
**Post date:** [May 15, 2024, 3:35am UTC](https://community.freefem.org/t/behavior-of-varf-with-rhs-and-on/3248/6 "2024-05-15T03:35:01Z")

</div>

Hi Chris,  
I noticed that A and tab are constructed from same varf, but A won’t include the linear part `a*v`, right?

---

<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:** [May 15, 2024, 8:38am UTC](https://community.freefem.org/t/behavior-of-varf-with-rhs-and-on/3248/7 "2024-05-15T08:38:46Z")

</div>

Yes, That is correct.

---

<div class="post-metadata">

**Author:** ![fb77](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/fb77/32/3796_2.png) [@fb77](https://community.freefem.org/u/fb77)\
**Post date:** [May 15, 2024, 11:07am UTC](https://community.freefem.org/t/behavior-of-varf-with-rhs-and-on/3248/8 "2024-05-15T11:07:27Z")

</div>

To complete the post of Chris, a third option is to use

```auto
varf test(u,v)
=int2d(Th)(dx(u)*dx(v)+dy(u)*dy(v))
-int2d(Th)(a*v)
+int1d(Th)(tgv*u*v)
-int1d(Th)(tgv*b*v)
;

```

This formulation is valid for `solve` and `varf` without change (the latter must use the line `tab=-tab;`). This formulation avoids the use of on().

---

<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:** [May 15, 2024, 2:01pm UTC](https://community.freefem.org/t/behavior-of-varf-with-rhs-and-on/3248/9 "2024-05-15T14:01:09Z")

</div>

Why would you want to avoid the use of `on()`? That allows more flexibility in terms of how Dirichlet BC’s are enforced via penalization `tgv>0`, or elimination (asymmetric `tgv=-1` or symmetric `tgv=-2`). It also allows you to ignore BC with `tgv=0`.

```auto
matrix A=test(Vh,Vh,tgv=...);
tab=test(0,Vh,tgv=...);

```

---

<div class="post-metadata">

**Author:** ![fb77](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/fb77/32/3796_2.png) [@fb77](https://community.freefem.org/u/fb77)\
**Post date:** [May 15, 2024, 2:38pm UTC](https://community.freefem.org/t/behavior-of-varf-with-rhs-and-on/3248/10 "2024-05-15T14:38:48Z")

</div>

It depends of course of what you want to do.

---

<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:** [May 15, 2024, 4:49pm UTC](https://community.freefem.org/t/behavior-of-varf-with-rhs-and-on/3248/11 "2024-05-15T16:49:10Z")

</div>

You are not answering to the question “Why would you want to avoid the use of `on()` ?”

---

<div class="post-metadata">

**Author:** ![fb77](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/fb77/32/3796_2.png) [@fb77](https://community.freefem.org/u/fb77)\
**Post date:** [May 15, 2024, 5:04pm UTC](https://community.freefem.org/t/behavior-of-varf-with-rhs-and-on/3248/12 "2024-05-15T17:04:12Z")

</div>

On the generic problems of the type I mentioned on my first post, it is possible to use the third option, which does the job in any type of formulation (solve, problem, varf).  
This does not mean I “want” to avoid on().
