# How to use correctly split with MixedElements?

**URL:** <https://fenicsproject.discourse.group/t/how-to-use-correctly-split-with-mixedelements/5281>\
**Category:** variational formulation\
**Created:** [March 11, 2021, 8:51am UTC](https://fenicsproject.discourse.group/t/how-to-use-correctly-split-with-mixedelements/5281 "2021-03-11T08:51:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jesus\_Amador](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/jesus_amador/32/2894_2.png) [@Jesus\_Amador](https://fenicsproject.discourse.group/u/Jesus_Amador)\
**Post date:** [March 11, 2021, 8:51am UTC](https://fenicsproject.discourse.group/t/how-to-use-correctly-split-with-mixedelements/5281/1 "2021-03-11T08:51:46Z")

</div>

Hello everyone,  
Recently I have encountered a problem when using the split function with MixedElements. When I use w0.split() the result is very similar to the initial condition but the solver gives an error “Unable to successfully call PETSc function ‘MatSetValuesLocal’.” and when I use split(w0) the solver works but there are perturbations with respect to the initial condition when I plot it.

Attached is a snippet of the code:

```
mesh = IntervalMesh(900, 0, 1.5e-4) 
V1 = FiniteElement("CG", mesh.ufl_cell(), 2)
V2 = FiniteElement("CG", mesh.ufl_cell(), 2)
ME = FunctionSpace(mesh,MixedElement([V1, V2]))
dw = TrialFunction(ME)
q, p = TestFunctions(ME)
w = Function(ME)
w0 = Function(ME)
class InitialConditions(UserExpression):
    def __init__ (self, **kwargs):
        super(). __init__ (**kwargs)
    def eval(self, values, x): #Returns values for a function of dimension two
        if x[0]<xi=4*1.5e-6/6:
            values[0]=1.0
            values[1]=1.0
        else:
            values[0]=0.0
            values[1]=0.0
    def value_shape(self): 
         return (2,)
u_init = InitialConditions()
w0.interpolate(u_init)
c, eta = split(w)
c0, eta0 = split(w0)
fig, ax = pl.subplots()
figure()
plot(eta0)
ax.set_xlim(0.0000975, 0.0001025)
c0, eta0 = w0.split()
figure()
plot(eta0)
ax.set_xlim(0.0000975, 0.0001025)

```

I would be very grateful if anyone knows why this is happening.

Thanks,  
Jesús

---

<div class="post-metadata">

**Author:** ![kamensky](https://avatars.discourse-cdn.com/v4/letter/k/e95f7d/32.png) [@kamensky](https://fenicsproject.discourse.group/u/kamensky)\
**Post date:** [March 11, 2021, 3:58pm UTC](https://fenicsproject.discourse.group/t/how-to-use-correctly-split-with-mixedelements/5281/2 "2021-03-11T15:58:42Z")

</div>

So, in general, you want to use `split(w)` to get components for defining a variational form, then `w.split()` after solving, to write output for visualization. For example, you might follow a workflow like

```python
# (Define mixed function space W)

w = Function(W)
q,p = split(w)

# (Define nonlinear residual F in terms of q and p)

solve(F==0,w,...)

q_viz, p_viz = w.split()
File("q.pvd") << q_viz
File("p.pvd") << p_viz

```

The variables `q` and `p` from `split(w)` are essentially just convenient labels for `w[0]` and `w[1]`, while `q_viz` and `p_viz` from `w.split()` are separate `Function` objects. (The fact that this is a potential source of confusion has been a known [issue](https://bitbucket.org/fenics-project/dolfin/issues/194/split-u-versus-usplit) for some time.)

---

<div class="post-metadata">

**Author:** ![mapengfei-nwpu](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/mapengfei-nwpu/32/117_2.png) [@mapengfei-nwpu](https://fenicsproject.discourse.group/u/mapengfei-nwpu)\
**Post date:** [June 16, 2021, 7:17am UTC](https://fenicsproject.discourse.group/t/how-to-use-correctly-split-with-mixedelements/5281/3 "2021-06-16T07:17:49Z")

</div>

I tried it. It seems that w.split() and split(w) retrun the same results. It is w.split(True) who returns the different result.
