# Unable to assign expression in userexpression

**URL:** <https://fenicsproject.discourse.group/t/unable-to-assign-expression-in-userexpression/3700>\
**Category:** Uncategorized\
**Created:** [July 4, 2020, 5:15am UTC](https://fenicsproject.discourse.group/t/unable-to-assign-expression-in-userexpression/3700 "2020-07-04T05:15:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hirshikesh\_Raj](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/hirshikesh_raj/32/192_2.png) [@Hirshikesh\_Raj](https://fenicsproject.discourse.group/u/Hirshikesh_Raj)\
**Post date:** [July 4, 2020, 5:15am UTC](https://fenicsproject.discourse.group/t/unable-to-assign-expression-in-userexpression/3700/1 "2020-07-04T05:15:42Z")

</div>

Hi, I am updating my code from 2017.2 to 2019.1. I am unable to assign expression in user-defined expression. The same works in 2017.2. Now sure how can i resolve this issue. please find MWC.

```
from fenics import *

mesh = Mesh('./Mesh/mesh_bimat_int5t_half.xml')
materials = MeshFunction("size_t", mesh, './Mesh/mesh_bimat_int5t_half_physical_region.xml')

Ep=1e3;
EI=2e3;
Ec = Expression('pow(x[0],2)', degree=1)

# define material properties
class K(UserExpression):
    def __init__ (self, materials, k_0, k_1, k_2, **kwargs):
        super(). __init__ (**kwargs)
        self.materials = materials
        self.k_0 = k_0
        self.k_1 = k_1
        self.k_2 = k_2

    def eval_cell(self, values, x, cell):
            if self.materials[cell.index] ==3:
                self.k_2.eval_cell(values,x,cell)
            elif self.materials[cell.index] == 1:
                values[0] = self.k_0
            else:
                values[0] = self.k_1

E = K(materials, Ep, EI, Ec, degree=1)
Gc = K(materials, Gcp, GcI, Gcc, degree=1)
nu = K(materials, nup, nuI, nuc, degree=1)

Vp = FunctionSpace(mesh,'DG',0)
E = project(E,Vp)
Gc = project(Gc,Vp)
nu = project(nu,Vp)
#plot(kappa,interactive=True)
#ph_f= File('./Results/materials.pvd')

phiE = File('E.pvd')
phiE << project(E, FunctionSpace(mesh, 'DG', 0))
```

---

<div class="post-metadata">

**Author:** ![bhaveshshrimali](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/bhaveshshrimali/32/124_2.png) [@bhaveshshrimali](https://fenicsproject.discourse.group/u/bhaveshshrimali)\
**Post date:** [July 4, 2020, 7:12am UTC](https://fenicsproject.discourse.group/t/unable-to-assign-expression-in-userexpression/3700/2 "2020-07-04T07:12:32Z")

</div>

How about `subclass`-ing your `k_2` as well? Much like how you are doing for `K`. Of course, there may be a smarter way to do this, but, for instance

```python
from dolfin import *
class k2(UserExpression):
    def __init__ (self, **kwargs):
        super(). __init__ (**kwargs)

    def eval_cell(self, values, x, cell):
        values[0] = x[0]**2

class K(UserExpression):
    def __init__ (self, materials, k_0, k_1, k_2, **kwargs):
        super(). __init__ (**kwargs)
        self.materials = materials
        self.k_0 = k_0
        self.k_1 = k_1
        self.k_2 = k_2

    def eval_cell(self, values, x, cell):
            if self.materials.array()[cell.index] == 3:
                self.k_2.eval_cell(values,x,cell)
            elif self.materials.array()[cell.index] == 1:
                values[0] = self.k_0
            else:
                values[0] = self.k_1
mesh = UnitSquareMesh(10, 10)
top = CompiledSubDomain("x[1] >= 0.7")
bottom = CompiledSubDomain("x[1] < 0.5")
middle = CompiledSubDomain("x[1] >=0.5 && x[1] < 0.7")
materials = cpp.mesh.MeshFunctionSizet(mesh, mesh.topology().dim())
top.mark(materials, 1)
middle.mark(materials, 2)
bottom.mark(materials, 3)
V = FunctionSpace(mesh, "DG", 2)
E = K(materials, 10., 20., k2(element=V.ufl_element()), degree=1, element=V.ufl_element()) 
u = Function(V)
u.interpolate(E)

```

should work too. Alternatively you can directly use functions in `DG` spaces to assign different material parameters. See [this](https://fenicsproject.discourse.group/t/how-to-define-different-materials-importet-3d-geometry-gmsh/2709/2) post for an example
