# ArityMismatch: Adding expressions with non-matching form arguments ('v\_0',) vs ()

**URL:** <https://fenicsproject.discourse.group/t/aritymismatch-adding-expressions-with-non-matching-form-arguments-v-0-vs/11516>\
**Category:** General\
**Created:** [June 15, 2023, 12:11am UTC](https://fenicsproject.discourse.group/t/aritymismatch-adding-expressions-with-non-matching-form-arguments-v-0-vs/11516 "2023-06-15T00:11:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sinhon2030](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/sinhon2030/32/7442_2.png) [@sinhon2030](https://fenicsproject.discourse.group/u/sinhon2030)\
**Post date:** [June 15, 2023, 12:11am UTC](https://fenicsproject.discourse.group/t/aritymismatch-adding-expressions-with-non-matching-form-arguments-v-0-vs/11516/1 "2023-06-15T00:11:39Z")

</div>

Hi,

I am new to fenics and I was trying to put advection to Cahn-Hilliard equation as I wanted to couple it to hydrodynamics. For the time being, I am using constant velocity just to see if the code works. But it gives me error. Here is the code.

import random  
from dolfin import \*

# Class representing the intial conditions

class InitialConditions(UserExpression):  
def **init** (self, \*\*kwargs):  
random.seed(2 + MPI.rank(MPI.comm\_world))  
super(). **init** (\*_kwargs)  
def eval(self, values, x):  
values[0] = 0.63 + 0.02_(0.5 - random.random())  
values[1] = 0.0  
def value\_shape(self):  
return (2,)

# Class for interfacing with the Newton solver

class CahnHilliardEquation(NonlinearProblem):  
def **init** (self, a, L):  
NonlinearProblem. **init** (self)  
self.L = L  
self.a = a  
def F(self, b, x):  
assemble(self.L, tensor=b)  
def J(self, A, x):  
assemble(self.a, tensor=A)

# Sub domain for Periodic boundary condition (periodic in x)

class PeriodicBoundary(SubDomain):

```
# Left boundary is "target domain" G
def inside(self, x, on_boundary):
    return bool(x[0] < DOLFIN_EPS and x[0] > -DOLFIN_EPS and on_boundary)

# Map right boundary (H) to left boundary (G)
def map(self, x, y):
    y[0] = x[0] - 1.0
    y[1] = x[1]

```

# Create periodic boundary condition

pbc = PeriodicBoundary()

# Model parameters

lmbda = 1.0e-02 # surface parameter  
dt = 5.0e-06 # time step  
theta = 0.5 # time stepping family, e.g. theta=1 → backward Euler, theta=0.5 → Crank-Nicolson

# Form compiler options

parameters[“form\_compiler”][“optimize”] = True  
parameters[“form\_compiler”][“cpp\_optimize”] = True

# Create mesh and build function space

mesh = UnitSquareMesh.create(100, 100, CellType.Type.quadrilateral)  
P1 = FiniteElement(“Lagrange”, mesh.ufl\_cell(), 1)  
ME = FunctionSpace(mesh, P1\*P1,constrained\_domain=pbc)

# Define trial and test functions

du = TrialFunction(ME)  
q, v = TestFunctions(ME)

# Define functions

u = Function(ME) # current solution  
u0 = Function(ME) # solution from previous converged step

# Split mixed functions

dc, dmu = split(du)  
c, mu = split(u)  
c0, mu0 = split(u0)

# Create intial conditions and interpolate

u\_init = InitialConditions(degree=1)  
#u\_init=Expression((‘tanh(1000\*(x[1]-0.5)+1.0)_0.01’,‘0’), degree=2)  
#u\_init=Expression(('tanh(1000_(x[1]-0.5)+1.0)’,‘0’), degree=2)  
u.interpolate(u\_init)  
u0.interpolate(u\_init)

# Compute the chemical potential df/dc

c = variable(c)  
f = 100_c\*\*2_(1-c)\*\*2  
dfdc = diff(f, c)

# mu\_(n+theta)

mu\_mid = (1.0-theta)_mu0 + theta_mu

#add advection (advection is causing problems)  
vel=Constant((0.0,0.1))  
vel\_=interpolate(vel,ME)

# Weak statement of the equations

L0 = c_q_dx - c0_q_dx + dt_dot(grad(mu\_mid), grad(q))dx + dtdot(grad(c),vel\_)dx  
L1 = muv_dx - dfdc_v_dx - lmbda\*dot(grad(c), grad(v))\*dx  
L = L0 + L1

# Compute directional derivative about u in the direction of du (Jacobian)

a = derivative(L, u, du)

# Create nonlinear problem and Newton solver

problem = CahnHilliardEquation(a, L)  
solver = NewtonSolver()  
solver.parameters[“linear\_solver”] = “lu”  
solver.parameters[“convergence\_criterion”] = “incremental”  
solver.parameters[“relative\_tolerance”] = 1e-6

# Output file

file = File(“output.pvd”, “compressed”)

# Step in time

t = 0.0  
T = 50\*dt  
while (t \< T):  
t += dt  
u0.vector()[:] = u.vector()  
solver.solve(problem, u.vector())  
file \<\< (u.split()[0], t)

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [June 15, 2023, 8:02am UTC](https://fenicsproject.discourse.group/t/aritymismatch-adding-expressions-with-non-matching-form-arguments-v-0-vs/11516/2 "2023-06-15T08:02:37Z")

</div>

Please use 3x` formatting to make your code properly formatted, ie.

````python
```python
#add code here
```

````

---

<div class="post-metadata">

**Author:** ![sinhon2030](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/sinhon2030/32/7442_2.png) [@sinhon2030](https://fenicsproject.discourse.group/u/sinhon2030)\
**Post date:** [June 16, 2023, 2:16am UTC](https://fenicsproject.discourse.group/t/aritymismatch-adding-expressions-with-non-matching-form-arguments-v-0-vs/11516/3 "2023-06-16T02:16:18Z")

</div>

I fixed the issue. There was a typo. Thank anyway. I will take care of your suggestion next time.
