Convert numpy array to Generic Matrix
Hello,
I have a problem with applying the boundary condition to a matrix. This is a simplified code with the same error:
from dolfin import *
import numpy as np
#Build mesh
nx = 5 #Number of cells in x direction
ny = 5 #Number of cells in y direction
nz = 5 #Number of cells in z direction
domain = UnitCube(nx,ny,nz)
numver = (nx + 1)*(ny + 1)*(nz + 1)
# Define function spaces
V = VectorFunctionS
# Dirichlet boundary
class DirichletBounda
def inside(self, x, on_boundary):
return on_boundary
# Define Dirichlet boundary conditions at boundaries
gD = Expression(
bc = DirichletBC(V, gD, DirichletBounda
# Define test and trial functions
H = TrialFunction(V)
v = TestFunction(V)
F = Expression(
# Define variational problem
a_S = inner(nabla_
a_M = inner(H, v)*dx
M = assemble(a_M, mesh = domain) # mass matrix
S = assemble(a_S, mesh = domain) # stiffness matrix
#pre-built a matrix C
C = np.zeros(
#for instance
for j in range(0,numver):
for i in range(numver):
C[i,j] = 1
C[numver + i,numver + j] = 2
C[2*numver + i, 2*numver+j] = 3
A = M.array()*C + S.array()
f = interpolate(F,V)
b = M*f.vector()
bc.apply(A,b)
#solve problem
H = Function(V)
solve(A, H.vector(),b)
###################
I get the following (expected) error due to the matrix A who is a numpy matrix (C is a numpy array)
bc.apply(A,b)
TypeError: in method 'DirichletBC_
How can I solve this? In short, I want to multiply a pre-built matrix C with the assembled matrix M. Afterwards, I want to apply the boundary conditions on A (not by hand and then not using linalg.solve(A,b) instead of solve). Thank you in advance.
Karel Van Bockstal
Question information
- Language:
- English Edit question
- Status:
- Answered
- For:
- DOLFIN Edit question
- Assignee:
- No assignee Edit question
- Last query:
- Last reply:
Can you help with this problem?
Provide an answer of your own, or ask Karel Van Bockstal for more information if necessary.