matrix on a mesh

Asked by Tim Baker

Hi,

I have a matrix in matlab, which I can export as text and I would like to load it on dolfin and build a mesh and put the value of the matrix on the mesh. Is it possible having a matrix in dolfin assign those value on the mesh?

Question information

Language:
English Edit question
Status:
Answered
For:
DOLFIN Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Kent-Andre Mardal (kent-and) said :
#1

The following code shows how to use a matrix generated by numpy as a FEniCS
expression:

from dolfin import *
from numpy import random

class Mat2Expression(Expression):
  def __init__(self, A, N, M):
    self.A = A
    self.N = N
    self.M = M
  def eval(self, v, x):
    A = self.A
    N = self.N
    M = self.M

    i = int(x[0]*N) - 1
    j = int(x[1]*N) - 1
    print i, j
    v[0] = A[i,j]

N = 10
A = random.random([N, N])
M = 23
mesh = UnitSquare(M, M)
V = FunctionSpace(mesh, "Lagrange", 1)

AA = Mat2Expression(A, N, M)

A = interpolate(AA, V)
plot(A)
interactive()

On 9 July 2012 21:11, Tim Baker <email address hidden>wrote:

> New question #202679 on DOLFIN:
> https://answers.launchpad.net/dolfin/+question/202679
>
> Hi,
>
> I have a matrix in matlab, which I can export as text and I would like to
> load it on dolfin and build a mesh and put the value of the matrix on the
> mesh. Is it possible having a matrix in dolfin assign those value on the
> mesh?
>
> --
> You received this question notification because you are a member of
> DOLFIN Team, which is an answer contact for DOLFIN.
>

Revision history for this message
Tim Baker (maurolap) said :
#2

thanks, what happens if the matrix is 3D?

Revision history for this message
Kent-Andre Mardal (kent-and) said :
#3

No problem, you just have to add that dimension to the expression,
ie,
  k= int(x[2]*N) - 1

Kent

On 9 July 2012 23:56, Tim Baker <email address hidden>wrote:

> Question #202679 on DOLFIN changed:
> https://answers.launchpad.net/dolfin/+question/202679
>
> Status: Answered => Open
>
> Tim Baker is still having a problem:
> thanks, what happens if the matrix is 3D?
>
> --
> You received this question notification because you are a member of
> DOLFIN Team, which is an answer contact for DOLFIN.
>

Can you help with this problem?

Provide an answer of your own, or ask Tim Baker for more information if necessary.

To post a message you must log in.