### Define initial guess as vector

193
views
0
7 months ago by
Hi, I want to define initial guess for the solution explicitilyin vector form and use this as old_solution. But it throwing error:  Unable to compute projection. I have attached MWE.
from dolfin import *
mesh = UnitIntervalMesh(3)

p = FunctionSpace(mesh, 'DG',0)

pold = as_vector([1,1.5,1.5,1.5])
print shape(Function(p))
print shape(pold)
pold = project(pold,Function(p))
plot(pold)
interactive()​

Thanks for the help.

Community: FEniCS Project

1
7 months ago by
I'm guessing you want to define the values at the nodes (because you supply four values in pold and your mesh has four nodes).
Then you have to change the function space to CG:
Consider this:
from dolfin import *
import numpy as np
mesh = UnitIntervalMesh(3)

p = FunctionSpace(mesh, 'CG',1)
pold = Function(p)

inds = vertex_to_dof_map(p)
pold.vector()[inds] =np.array([1,1.5,1.5,1.5])

plot(pold)
interactive()​
1
7 months ago by
Looks like you have a number of confusing elements here...
Use project() on an expression and a function space, not a function:
pold = project(pold,p)​

but this will fail anyway for your code. Project() projects an expression onto your function space, whereas I think you are actually trying to give it nodal answers like klunkean mentioned. Your function space is 1D, as your first shape() confirms (the empty bracket means 1). The dimension of 'pold' is 4 since it is a vector of 4 elements. Therefore you are trying to project a 4 dimensional vector onto a 1 dimensional function space and it fails (clearly).
If you do:

pold = Constant(1.5)

then it will work, and assign 1.5 to everywhere.

I think you might be confusing the 'vector' expression with the solution vector...