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

Define initial conditions with a mixed function space

+1 vote

Hi,
I would like to define three initial conditions p0=1bar, θs0=100K and θg0=40K. I am working on a Taylor-Hood mixed function space, and I have tried to apply to Cahn-hilliard demo, but I get an error.

mesh = Mesh("bonmesh.xml")
V1 = VectorFunctionSpace(mesh, 'CG', 2)
V2 = FunctionSpace(mesh, 'CG', 1)
V3 = FunctionSpace(mesh, 'CG', 1)
V4 = FunctionSpace(mesh, 'CG', 1)
W = MixedFunctionSpace([V1, V2, V3, V4])

# Class representing the intial conditions
class InitialConditions(Expression):
    def eval(self, values, x):
        values[0] = 1.0
        values[1] = 100.0
        values[2] = 40.0
    def value_shape(self):
        return (3,)

# Class for interfacing with the Newton solver
class ECRTCEquation(NonlinearProblem):
    def __init__(self, a, L):
        NonlinearProblem.__init__(self)
        self.L = L
        self.a = a
        self.reset_sparsity = True
    def F(self, b, x):
        assemble(self.L, tensor=b)
    def J(self, A, x):
        assemble(self.a, tensor=A, reset_sparsity=self.reset_sparsity)
        self.reset_sparsity = False

# Define trial and test functions
du = TrialFunction(W)
v, q, tS, tG = TestFunctions(W)

# Define functions
u   = Function(W)  # current solution
u0  = Function(W)  # solution from previous converged step

# Split mixed functions
duS, dpGR, dthetaS, dthetaG = split(du)
uS,  pGR, thetaS, thetaG = split(u)
uS0, pGR0, thetaS0, thetaG0 = split(u0)

# Create intial conditions and interpolate
u_init = InitialConditions()
u.interpolate(u_init)
u0.interpolate(u_init)

And the error:

  File "mesh.py", line 161, in <module>
    u.interpolate(u_init)
RuntimeError: 

*** -------------------------------------------------------------------------
*** DOLFIN encountered an error. If you are not able to resolve this issue
*** using the information listed below, you can ask for help at
***
***     https://answers.launchpad.net/dolfin
***
*** Remember to include the error message listed below and, if possible,
*** include a *minimal* running example to reproduce the error.
***
*** -------------------------------------------------------------------------
*** Error:   Unable to interpolate function into function space.
*** Reason:  Dimension 0 of function (3) does not match dimension 0 of function space (5).
*** Where:   This error was encountered inside FunctionSpace.cpp.
*** Process: 0

Is there another way to define and interpolate initial conditions?

Thanks in advance,

Remi

asked Aug 15, 2013 by Kheldarion FEniCS Novice (230 points)

1 Answer

+3 votes
 
Best answer
*** Reason:  Dimension 0 of function (3) does not match dimension 0 of function space (5).

Hey, this tells everything! Your InitialConditions needs to have a corresponding dimension, i.e. 5.

answered Aug 15, 2013 by Jan Blechta FEniCS Expert (51,420 points)
selected Aug 19, 2013 by Jan Blechta

Thank you for your answer.
Yes, I have tried the following code and it works :

# Class representing the intial conditions
class InitialConditions(Expression):
    def eval(self, values, x):
        values[0] = 0.0
        values[1] = 0.0
        values[2] = 1.0
        values[3] = 100.0
        values[4] = 40.0

    def value_shape(self):
        return (5,)

But I am not sure that the values [0] and [1] are correctly interpolate as uS0=(0,0)?

Yes, it's correct. You can check by

print u(0.0, 0.0)
...