This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

How to calculate an external force vector as the product of a stiffness matrix K and a prescribed vector u_p

0 votes

Hi, again.

As known, the external force vector can be obtained from the following codes:

dU_ext = dot(load, u_test)*ds(1) # reference external energy functional due to external load imposed on the boundary ds(1)
f_ext = assemble(dU_ext) # assembled external forces

Now, I want to calculate a similar external force vector as the product of a stiffness matrix K and a vector u_p containing prescribed displacements.

K = assemble(a) # stiffness matrix
bc.apply(u_p.vector()) # u_p contains the prescribed displacements

# the reference external forces
f_ext = inner(K, u_p) # does not work
f_ext = numpy.matmul(K.array(), u_p.vector.array()) # works, but yields an array rather than a Vector as the external forces given above

I want to arrive at an external force vector f_ext of the same type obtained by assemble().
Can someone give me any idea? Thank you!

asked Mar 22, 2017 by jywu FEniCS Novice (510 points)
edited Mar 22, 2017 by jywu

2 Answers

0 votes
 
Best answer

I guess that you are looking for something similar to K*u_p.vector(). See the minimal example below

from dolfin import *

MyMesh = UnitSquareMesh(3,3)
imposed_displacement = Constant(1)

V = FunctionSpace(MyMesh, "Lagrange", 1)
u_p = Function(V)

u = TrialFunction(V)
v = TestFunction(V)
K = assemble(inner(grad(u), grad(v))*dx)
f = K*u_p.vector()

print type(f)

# Now, if you wish, you can use f as right-hand side of a linear system
solution = Function(V)
solve(K, solution.vector(), f)
answered Mar 22, 2017 by francesco.ballarin FEniCS User (4,070 points)
selected Mar 23, 2017 by jywu

Yes, Dr. Ballarin. Any better idea?

I modified the answer to show you an example.

Thanks a lot, Dr. Ballarin. I cannot imagine that it is so simple.
You really helped me!

0 votes

It seems that the following codes work:

f_ext = Vector(N)
f_ext[:] = numpy.matmul(K.array(), u_p.vector.array())

Am I right?

answered Mar 22, 2017 by jywu FEniCS Novice (510 points)
...