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

subvector assignment

+1 vote

DOLFIN's NonlinearProblem interface passes around vectors instead of functions, so to fill these vectors with meaningful values, one might have to convert them to functions first. No problem:

def F(self, b, x):
    xfun = Function(self.W)
    xfun.vector()[:] = x
    # do something with xfun, get bfun
    b = bfun.vector()

(Are there ways to do this without copying?)

I'm dealing with a block problem now, and it goes like

def F(self, b, x):
    xfun = Function(self.WP)
    xfun.vector()[:] = x
    x0, x1 = xfun.split()
    # do something with x0, x1, get b0, b1

How to appropriately assign b0.vector(), b1.vector() to b?

asked Jan 7, 2014 by nschloe FEniCS User (7,120 points)

If I understand the problem, you may like to encapsulate vector b into Function to be able to use FunctionAssigner.

1 Answer

+2 votes
 
Best answer

If self.WP is a VectorFunctionSpace (with the same type of sub space) you should be able to set up two FunctionAssigner objects inside the constructor of your subclassed NonlinearProblem:

def __init__(self, ...)
    ...
    self.full_func = Function(self.WP)
    self.u0 = Function(self.WP.sub(0).collapse())
    self.u1 = Function(self.WP.sub(1).collapse())
    self.to_sub = FunctionAssigner([self.WP.sub(0), self.WP.sub(1)], self.WP)
    self.from_sub = FunctionAssigner(self.WP, [self.WP.sub(0), self.WP.sub(1)])

def F(self, b, x):
    self.full_func.vector()[:] = x
    self.to_sub.assign([self.u0, self.u1], self.full_func)
    ...
    self.from_sub.assign(self.full_func, [self.u0, self.u1])
    b[:] = self.from_sub.vector()
answered Jan 10, 2014 by johanhake FEniCS Expert (22,480 points)
selected Jan 17, 2014 by nschloe
...