# Preferred way to construct block matrices with exact Dirichlet BC (tgv = -1)?

**URL:** <https://community.freefem.org/t/preferred-way-to-construct-block-matrices-with-exact-dirichlet-bc-tgv-1/222>\
**Category:** General Discussion\
**Created:** [January 14, 2020, 4:22pm UTC](https://community.freefem.org/t/preferred-way-to-construct-block-matrices-with-exact-dirichlet-bc-tgv-1/222 "2020-01-14T16:22:42Z")\
**Posts on this page:** 4\
**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:** [January 14, 2020, 4:22pm UTC](https://community.freefem.org/t/preferred-way-to-construct-block-matrices-with-exact-dirichlet-bc-tgv-1/222/1 "2020-01-14T16:22:42Z")

</div>

Hello,

I have a composite system with several overlapping FE spaces which I construct in blocks using independent variational forms for each space and then assembling them all into a large block matrix. I would like to enforce exact Dirichlet conditions without penalization (e.g. by using tgv = -1). However, the best way I can think of doing this is after the matrix assembly by “zeroing out” all of the rows associated with boundary DOF’s where Dirichlet conditions are enforced and then setting the diagonal terms to 1.

I would really prefer to use the “on()” function to do this without having to do all the bookkeeping for the boundary DOF’s. Is there a way to use “on()” to enforce zero rows in rectangular matrices? Or is there another preferred way to construct block matrices with Dirichlet BC’s?

Thanks!

---

<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:** [January 14, 2020, 9:14pm UTC](https://community.freefem.org/t/preferred-way-to-construct-block-matrices-with-exact-dirichlet-bc-tgv-1/222/2 "2020-01-14T21:14:31Z")

</div>

What bookkeeping are you talking about? We could add a new `tgv = -3` to handle this (I wanted to do this for a long time), but in the meantime, you can just impose on your off-diagonal blocks something like:

```auto
varf vGamma(u, v) = on(gamma, u = 1);
real[int] marker = vGamma(0, Vh, tgv = -1);
for[i,j,v : offdiagonal] if(marker[i] > 0.1)) v = 0.0;

```

---

<div class="post-metadata">

**Author:** ![arousta](https://avatars.discourse-cdn.com/v4/letter/a/838e76/32.png) [@arousta](https://community.freefem.org/u/arousta)\
**Post date:** [January 15, 2020, 11:14am UTC](https://community.freefem.org/t/preferred-way-to-construct-block-matrices-with-exact-dirichlet-bc-tgv-1/222/3 "2020-01-15T11:14:21Z")

</div>

Also relevant, look at this post:  
[https://community.freefem.org/t/implementation-of-dirichlet-boundary-condition-when-tgv-1/113](https://community.freefem.org/t/implementation-of-dirichlet-boundary-condition-when-tgv-1/113)

---

<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:** [January 16, 2020, 12:44am UTC](https://community.freefem.org/t/preferred-way-to-construct-block-matrices-with-exact-dirichlet-bc-tgv-1/222/4 "2020-01-16T00:44:54Z")

</div>

Thanks @prj! The “bookkeeping” I was referring to is what your solution addresses. Namely, defining extra vectors to flag the row indices associated with Dirichlet BC’s and then looping back through each matrix block to zero out the flagged rows.
