# Possible to assemble load vector only for some nodes?

**URL:** <https://fenicsproject.discourse.group/t/possible-to-assemble-load-vector-only-for-some-nodes/10129>\
**Category:** General\
**Created:** [January 10, 2023, 2:47pm UTC](https://fenicsproject.discourse.group/t/possible-to-assemble-load-vector-only-for-some-nodes/10129 "2023-01-10T14:47:29Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![hamedZaker](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/hamedzaker/32/4563_2.png) [@hamedZaker](https://fenicsproject.discourse.group/u/hamedZaker)\
**Post date:** [January 10, 2023, 2:47pm UTC](https://fenicsproject.discourse.group/t/possible-to-assemble-load-vector-only-for-some-nodes/10129/1 "2023-01-10T14:47:29Z")

</div>

Hi 🙂

Consider a Poisson equation u’’ = f with its assembled load vector (the right-hand side) L:

```auto
domain = mesh.create_unit_square(MPI.COMM_WORLD, 10, 10) 
Vh = fem.FunctionSpace(domain, ("CG", 1))
v = ufl.TestFunction(Vh)
u = ufl.TrialFunction(Vh)
a = ufl.dot(ufl.grad(u), ufl.grad(v)) * v
L = fem.assemble_vector(fem.form(f * v * ufl.dx))

```

This vector L has N lines, where N is the number of nodes in the mesh. I want to implement [DEIM](https://epubs.siam.org/doi/abs/10.1137/090766498?journalCode=sjoce3) using dolfinx. I won’t go into details of DEIM, but it requires computing only some lines of the load vector L (to speed up the computation) rather than assembling for all N nodes. Is there any way I can assemble only for some nodes?

Of course, I can construct L and then select some of its rows, but it kills the performance gain the DEIM method is about. I really appreciate your help/ideas.

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [January 10, 2023, 2:53pm UTC](https://fenicsproject.discourse.group/t/possible-to-assemble-load-vector-only-for-some-nodes/10129/2 "2023-01-10T14:53:02Z")

</div>

If you can link the nodes to certain cell indices, you can restrict the integration domain by using a `MeshTag` as input to the `dx`-Measure, as shown in: [Test problem 2: Flow past a cylinder (DFG 2D-3 benchmark) — FEniCSx tutorial](https://jsdokken.com/dolfinx-tutorial/chapter2/ns_code2.html?highlight=subdomain_data#verification-of-the-implementation-compute-known-physical-quantities) (for facet integrals, but the same principle works for cell integrals), see for instance: [dolfinx/test\_assemble\_domains.py at 2be366d20632377e0688df585fcd7e201d5dc86c · FEniCS/dolfinx · GitHub](https://github.com/FEniCS/dolfinx/blob/2be366d20632377e0688df585fcd7e201d5dc86c/python/test/unit/fem/test_assemble_domains.py#L61-L66) or for instance [Add PML demo - GSoC 2022 by mikics · Pull Request #2276 · FEniCS/dolfinx · GitHub](https://github.com/FEniCS/dolfinx/pull/2276/files#diff-b602fe77ea10a133717804e13c6dc2bcb9c7b4f683b3989ba88af1dac3391a87R271-R273)

---

<div class="post-metadata">

**Author:** ![hamedZaker](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/hamedzaker/32/4563_2.png) [@hamedZaker](https://fenicsproject.discourse.group/u/hamedZaker)\
**Post date:** [January 11, 2023, 8:44am UTC](https://fenicsproject.discourse.group/t/possible-to-assemble-load-vector-only-for-some-nodes/10129/3 "2023-01-11T08:44:33Z")

</div>

Thank you! That was so helpful to understand the dolfinx possibilities. I tried to build upon [this one](https://github.com/FEniCS/dolfinx/blob/2be366d20632377e0688df585fcd7e201d5dc86c/python/test/unit/fem/test_assemble_domains.py#L61-L66) that you suggested but I have " incompatible constructor arguments" error for **fem.form**. Do you have any idea?

 ![image](https://global.discourse-cdn.com/free1/uploads/fenicsproject1/original/2X/d/de4bbc16387112095027898e9f64c15eeafec361.png)

```auto
import ufl
import numpy as np
from mpi4py import MPI
from dolfinx import mesh, fem
import pytest
domain = mesh.create_unit_square(MPI.COMM_WORLD, 10, 10)

Vh = fem.FunctionSpace(domain, ("CG", 1))
v = ufl.TestFunction(Vh)
u = ufl.TrialFunction(Vh)

cell_map = domain.topology.index_map(domain.topology.dim)
num_cells = cell_map.size_local
indices = np.arange(0, num_cells)
values = np.full(indices.shape, 3, dtype=np.intc)
values[0] = 1
values[1] = 2

marker = pytest.mark.meshtags_factory(domain, domain.topology.dim, indices, values)
dz = ufl.Measure('dx', domain=domain, subdomain_data=marker)
a = fem.form(v * dz(3))

```

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [January 11, 2023, 9:01am UTC](https://fenicsproject.discourse.group/t/possible-to-assemble-load-vector-only-for-some-nodes/10129/4 "2023-01-11T09:01:39Z")

</div>

Try:

```python
import ufl
import numpy as np
from mpi4py import MPI
from dolfinx import mesh, fem
import pytest
domain = mesh.create_unit_square(MPI.COMM_WORLD, 10, 10)

Vh = fem.FunctionSpace(domain, ("CG", 1))
v = ufl.TestFunction(Vh)
u = ufl.TrialFunction(Vh)

cell_map = domain.topology.index_map(domain.topology.dim)
num_cells = cell_map.size_local
indices = np.arange(0, num_cells)
values = np.full(indices.shape, 3, dtype=np.intc)
values[0] = 1
values[1] = 2

marker = mesh.meshtags(domain, domain.topology.dim, indices, values)
dz = ufl.Measure('dx', domain=domain, subdomain_data=marker)
a = fem.form(v * dz(3))

```

---

<div class="post-metadata">

**Author:** ![hamedZaker](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/hamedzaker/32/4563_2.png) [@hamedZaker](https://fenicsproject.discourse.group/u/hamedZaker)\
**Post date:** [January 11, 2023, 3:21pm UTC](https://fenicsproject.discourse.group/t/possible-to-assemble-load-vector-only-for-some-nodes/10129/5 "2023-01-11T15:21:54Z")

</div>

Great! Thanks a lot. It is working well now. My only surprise is that the assembling even got slower using this marker (and dz(3) rather than dz), and I am wondering why.

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [January 11, 2023, 3:41pm UTC](https://fenicsproject.discourse.group/t/possible-to-assemble-load-vector-only-for-some-nodes/10129/6 "2023-01-11T15:41:11Z")

</div>

Please make a minimal example where you show timings of this claim.

---

<div class="post-metadata">

**Author:** ![hamedZaker](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/hamedzaker/32/4563_2.png) [@hamedZaker](https://fenicsproject.discourse.group/u/hamedZaker)\
**Post date:** [January 12, 2023, 9:43am UTC](https://fenicsproject.discourse.group/t/possible-to-assemble-load-vector-only-for-some-nodes/10129/8 "2023-01-12T09:43:13Z")

</div>

I take back what I said, I was not precise. The issue is not about assembling time, I used it inside a nonlinear solver and the timing I have is not directly related to this dz measure, but lots of other stuff in convergence for example. Anyway, thank you, my code is at least working 🙂

---

<div class="post-metadata">

**Author:** ![Thomas996](https://avatars.discourse-cdn.com/v4/letter/t/b9e5f3/32.png) [@Thomas996](https://fenicsproject.discourse.group/u/Thomas996)\
**Post date:** [March 8, 2023, 5:12pm UTC](https://fenicsproject.discourse.group/t/possible-to-assemble-load-vector-only-for-some-nodes/10129/9 "2023-03-08T17:12:41Z")

</div>

Hi, I have a following question here: how to define L and solver in this case? I followed your steps and it reports the error by petsc-solver: object is in wrong state/Matrix is missing diagonal entry

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [March 8, 2023, 6:33pm UTC](https://fenicsproject.discourse.group/t/possible-to-assemble-load-vector-only-for-some-nodes/10129/10 "2023-03-08T18:33:17Z")

</div>

If you want to solve a problem on only a part of your mesh, you can either:

1. add Dirichlet BCs on all remaining dofs (say, set then to 0).
2. use a sub mesh and solve the problem with a function space defined on the sub mesh.

---

<div class="post-metadata">

**Author:** ![Thomas996](https://avatars.discourse-cdn.com/v4/letter/t/b9e5f3/32.png) [@Thomas996](https://fenicsproject.discourse.group/u/Thomas996)\
**Post date:** [March 8, 2023, 8:06pm UTC](https://fenicsproject.discourse.group/t/possible-to-assemble-load-vector-only-for-some-nodes/10129/11 "2023-03-08T20:06:14Z")

</div>

Hi dokken, thank you for your prompt answer.  
To 2: Do you have a example of how to define a sub mesh?

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [March 8, 2023, 8:07pm UTC](https://fenicsproject.discourse.group/t/possible-to-assemble-load-vector-only-for-some-nodes/10129/12 "2023-03-08T20:07:27Z")

</div>

See:

> <https://github.com/FEniCS/dolfinx/blob/d376b20b4b7bf5b85bd3a26582ec9683864bd23e/python/test/unit/fem/test_assemble_submesh.py#L77-L78>

---

<div class="post-metadata">

**Author:** ![Thomas996](https://avatars.discourse-cdn.com/v4/letter/t/b9e5f3/32.png) [@Thomas996](https://fenicsproject.discourse.group/u/Thomas996)\
**Post date:** [March 8, 2023, 8:20pm UTC](https://fenicsproject.discourse.group/t/possible-to-assemble-load-vector-only-for-some-nodes/10129/13 "2023-03-08T20:20:58Z")

</div>

Thanks. I’ll look through it. I would like to ask one more question if it’s relevant: I now have a 3D beam mesh and I want to give it a torsion. What may be the most efficient way to do this in fenicsx?

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [March 8, 2023, 8:25pm UTC](https://fenicsproject.discourse.group/t/possible-to-assemble-load-vector-only-for-some-nodes/10129/14 "2023-03-08T20:25:04Z")

</div>

See for instance: [dolfinx/main.cpp at main · FEniCS/dolfinx · GitHub](https://github.com/FEniCS/dolfinx/blob/main/cpp/demo/hyperelasticity/main.cpp)  
which is similar to: [https://fenicsproject.org/olddocs/dolfin/latest/python/demos/hyperelasticity/demo\_hyperelasticity.py.html](https://fenicsproject.org/olddocs/dolfin/latest/python/demos/hyperelasticity/demo_hyperelasticity.py.html) in legacy FEniCS.

---

<div class="post-metadata">

**Author:** ![Thomas996](https://avatars.discourse-cdn.com/v4/letter/t/b9e5f3/32.png) [@Thomas996](https://fenicsproject.discourse.group/u/Thomas996)\
**Post date:** [March 8, 2023, 8:40pm UTC](https://fenicsproject.discourse.group/t/possible-to-assemble-load-vector-only-for-some-nodes/10129/15 "2023-03-08T20:40:07Z")

</div>

Much Thanks. I think it will also help other people. C++ is for now too hard for me to understand. But firslty I will try to implement the old code from Fenics.
