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

Existing Solution as One Element of Initial Condition

0 votes

So I have a slightly awkward set of boundaries in a Cahn-Hilliard type problem which makes getting a good initial guess reasonably difficult. I would like to solve Laplace's equation to get an initial condition, which I have done, obtaining a function mu0.

def macro_solver(mesh, boundaries, mu0):

    # Create mesh and build function space
    P1 = FiniteElement("Lagrange", mesh.ufl_cell(), 2)
    ME = FunctionSpace(mesh, P1*P1)

    # Class representing the initial conditions
    class InitialConditions(Expression):
        def eval(self, values, x):
            values[0] = -np.tanh(5*(x[0]-1-0.05*cos(6.28*x[1])))
            values[1] = mu0
        def value_shape(self):
            return (2,) 

This is essentially what I want to do, now a cursory look suggests using the FunctionAssigner class, but mu0 is on a different function space so needs projection and there is no obvious demo or useful documentation as to how this might be accomplished.

Many thanks in advance for any advice

asked Oct 10, 2016 by varnis FEniCS Novice (390 points)

1 Answer

+1 vote

Let V be the finite element space of your Cahn-Hilliard type problem which, if I understood the question correctly, has two components. Let W be the auxiliary space of the laplace problem which, if I understood the question correctly, has one component. Make sure to use the same FE for both V and W.

Please consider defining an Expression for "-np.tanh(5(x[0]-1-0.05cos(6.28*x[1]))", project it to W and then assign it to the initial guess. In a similar way, assign also mu_0 to the initial guess.

Pseudo code:

expression = Expression("tanh(....)", element=W.ufl_element())
initial_guess_0 = project(expression, W)
initial_guess_1 = mu_0

initial_guess = Function(V)
assign(initial_guess.sub(0), initial_guess_0)
assign(initial_guess.sub(1), initial_guess_1)

Best regards,
Francesco

answered Oct 15, 2016 by francesco.ballarin FEniCS User (4,070 points)

Hi Francesco,

That answers half of the question (the technical half!), my understanding is that to use the NonlinearProblem approach we need to set up the class InitialConditions? This is then evaluated by the solver, but there doesn't seem to be provision for passing the guess mu to the eval method?

Apologies if this is an obvious question, but my understanding of python is somewhat rudimentary and this seems like a fairly standard thing to want to do?

if F is the residual of your equation, a simple

  solve(F==0, initial_guess, boundary_conditions)

should suffice. Note that at the end of the nonlinear iterations the problem solution will be stored in the initial_guess function. In a similar way for NonlinearProblem, it shoud be enough for you to provide the function where the initial guess is stored.

...