### How to get local matrices of each cell of a mesh ?

246

views

0

How to get local matrices of each cell of a mesh ?

Say I have :

mesh = UnitSquareMesh(8, 8)

V = FunctionSpace(mesh, 'P', 1)

u = TrialFunction(V)

v = TestFunction(V)

a = dot(grad(u), grad(v))*dx

How to get the submatrix (extracted from a) that corresponds to each cell of the mesh:

for cell in cells(mesh):

aLoc = a[cell.getdof(), cell.getdof()] # How to grab what's associated to each cell ?

Franck

Say I have :

mesh = UnitSquareMesh(8, 8)

V = FunctionSpace(mesh, 'P', 1)

u = TrialFunction(V)

v = TestFunction(V)

a = dot(grad(u), grad(v))*dx

How to get the submatrix (extracted from a) that corresponds to each cell of the mesh:

for cell in cells(mesh):

aLoc = a[cell.getdof(), cell.getdof()] # How to grab what's associated to each cell ?

Franck

Community: FEniCS Project

### 3 Answers

5

I'm brand new at this, but does assemble_local work?

```
mesh = UnitSquareMesh(8, 8)
V = FunctionSpace(mesh, 'P', 1)
u = TrialFunction(V)
v = TestFunction(V)
a = dot(grad(u), grad(v))*dx
for cell in cells(mesh):
aLoc = assemble_local(a, cell)
```

See https://fenicsproject.org/olddocs/dolfin/2016.2.0/python/programmers-reference/fem/assembling_local/assemble_local.html

Andy

Still need to check... But I would say yes !

written
7 months ago by
FH

It depends what you really want to get.

```
A = assemble(a)
dofmap = V.dofmap()
for cell in cells(mesh):
dofs = dofmap.cell_dofs(cell.index())
aLoc = A.array()[dofs, :][:, dofs]
```

first assembles the form and then extracts the rows/columns for the dofs corresponding to the cell.

`assemble_local(a, cell)`

assembles the form only on the particular cell. For example, for

`mesh = UnitSquareMesh(8, 8)`

you will get 128 same tensors.

Nevertheless, the form is always assembled in one way or another.

written
7 months ago by
Adam Janecka

I believe this is it: first, I need "neumann" matrices (the ones of each element without boundary conditions - they are all the same as all elements are triangles: I must use assemble_local for that), then I need the "dirichlet" matrices (the ones of each element with boundary conditions - they will not be the same: I must use dofmap.cell_dofs for that). At least, that's what I understand so far.

written
7 months ago by
FH

1

```
mesh = UnitSquareMesh(8, 8)
V = FunctionSpace(mesh, 'P', 1)
u = TrialFunction(V)
v = TestFunction(V)
a = dot(grad(u), grad(v))*dx
A = assemble(a)
dofmap = V.dofmap()
for cell in cells(mesh):
dofs = dofmap.cell_dofs(cell.index())
aLoc = A.array()[dofs, :][:, dofs]
```

Does this work for you?

0

Does only half the job !

Your solution is working great

Now still stuck with the other half of the question: same problem for local

I tried this:

aLoc = a[dofs, :][:, dofs]

TypeError: 'Form' object has no attribute '__getitem__'

and this:

aLoc = a.array()[dofs, :][:, dofs]

AttributeError: 'Form' object has no attribute 'array'

... Without success

How to get local matrices of each cell of a mesh

Franck

Your solution is working great

**after**a has been assembled into A: that's the half of my initial need. Thanks for that !Now still stuck with the other half of the question: same problem for local

**un**assembled matrices (that is**before**assembly).I tried this:

aLoc = a[dofs, :][:, dofs]

TypeError: 'Form' object has no attribute '__getitem__'

and this:

aLoc = a.array()[dofs, :][:, dofs]

AttributeError: 'Form' object has no attribute 'array'

... Without success

How to get local matrices of each cell of a mesh

**before**assembly ?Franck

Please login to add an answer/comment or follow this question.