Unable to compile C++ code with dijitso, error in expression

A= Expression(‘0.5*(1+tanh(100*(cos(0.4pi )/sin(0.4pi )- cos(pi0.1phi )/sin(pi0.1phi))))+1’,degree=2,pi=pi, phi=phi, element = V.ufl_element())

This is how phi is defined.
q,dq = Function(V),TestFunction(V)

pii,phi, qe         = split(q)

I am a newbie may be I am asking very basic. The phi in the argument of cos and sin generating this error. phi is a function. I want to know how to insert phi in the expression.

Please add a full minimal working code example that reproduces the error message, and make sure that you use 3x` encapsulation to ensure appropriate formatting.

You can index into a mixed Function in an Expression instead of passing the Function components returned by split, e.g.,

from dolfin import *
mesh = UnitIntervalMesh(1)
V = VectorFunctionSpace(mesh,"CG",1,dim=2)
v = project(Constant((1,2)),V)
v1,v2 = split(v)

# Does not work:
#expr1 = Expression("v1",v1=v1,degree=1)
#expr2 = Expression("v2",v2=v2,degree=1)

# Works:
expr1 = Expression("v[0]",v=v,degree=1)
expr2 = Expression("v[1]",v=v,degree=1)

# Verify (should print 1 and 2):
print(assemble(expr1*dx(domain=mesh)))
print(assemble(expr2*dx(domain=mesh)))
2 Likes

Thank you, this solution worked for me @kamensky!

However I would change it a tiny bit to

expr1 = Expression("v",v=v.sub(0),degree=1)

This has the advantage, that the ‘0’ can easily exchanged by a variable, making a larger code more readable and easier to maintain.

Whether this is more readable is perhaps subjective, but the following eliminates the redundant lines that differ only by index and is easier to generalize to arbitrary dimension:

# Works:
#expr1 = Expression("v[0]",v=v,degree=1)
#expr2 = Expression("v[1]",v=v,degree=1)
d=2
expr = [Expression("v["+str(i)+"]",v=v,degree=1)
        for i in range(0,d)]

# Verify (should print 1 and 2):
#print(assemble(expr1*dx(domain=mesh)))
#print(assemble(expr2*dx(domain=mesh)))
for i in range(0,d):
    print(assemble(expr[i]*dx(domain=mesh)))