# For loop in a varf/therm

**URL:** <https://community.freefem.org/t/for-loop-in-a-varf-therm/2146>\
**Category:** General Discussion\
**Created:** [November 28, 2022, 9:32pm UTC](https://community.freefem.org/t/for-loop-in-a-varf-therm/2146 "2022-11-28T21:32:44Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrbenson](https://avatars.discourse-cdn.com/v4/letter/m/d07c76/32.png) [@mrbenson](https://community.freefem.org/u/mrbenson)\
**Post date:** [November 28, 2022, 9:32pm UTC](https://community.freefem.org/t/for-loop-in-a-varf-therm/2146/1 "2022-11-28T21:32:44Z")

</div>

Hi,

I have a lot (hundred to thoundand) of boundary conditions (each one depends on some calculated array values) and want to factorize my code using a loop.

Is there a way to use a for loop inside a varf/therm definition ?

Best regards.

---

<div class="post-metadata">

**Author:** ![henkel](https://avatars.discourse-cdn.com/v4/letter/h/b77776/32.png) [@henkel](https://community.freefem.org/u/henkel)\
**Post date:** [November 29, 2022, 11:16am UTC](https://community.freefem.org/t/for-loop-in-a-varf-therm/2146/2 "2022-11-29T11:16:56Z")

</div>

I am assuming you mean Dirichlet boundary conditions? There is afaik no way to have a loop in your varf, but it should be possible to find a workaround. Instead of using `on()` you can assemble the system matrix / rhs vector without specifying Dirichlet BC and impose them later on. Use the command setBC (FreeFEM version \>= 4.11 required, [example code](https://github.com/FreeFem/FreeFem-sources/blob/master/examples/3d/Elasticity-simple-support-BC.edp#L46-L48)) to modify the matrix and modify the rhs vector manually according to your boundary data.  
This requires a bit of bookkeeping (Which degree of freedom (dof) belongs to which boundary and what is the boundary value to be imposed?). For a single boundary you can get a vector (length: number of dofs) which entries are equal to 1, if the dof belongs to the boundary, else 0:

```auto
fespace Vh(Th, P1);
varf vGamma(u, v) = on(gamma, u = 1);
real[int] marker = vGamma(0, Vh, tgv = -1);

```

To generalize for multilple boundaries, I would iterate over gamma and set up two vectors. One vector that indicates if a dof belongs to any part of the boundary with a Dirichlet BC (1: on boundary, else 0) and one vector that contains the dirichlet boundary data.
