# Subdomains with different coefficient dependent on solution

**URL:** <https://fenicsproject.discourse.group/t/subdomains-with-different-coefficient-dependent-on-solution/976>\
**Category:** Uncategorized\
**Created:** [June 11, 2019, 7:25am UTC](https://fenicsproject.discourse.group/t/subdomains-with-different-coefficient-dependent-on-solution/976 "2019-06-11T07:25:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hyesung\_Shin](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/hyesung_shin/32/299_2.png) [@Hyesung\_Shin](https://fenicsproject.discourse.group/u/Hyesung_Shin)\
**Post date:** [June 11, 2019, 7:25am UTC](https://fenicsproject.discourse.group/t/subdomains-with-different-coefficient-dependent-on-solution/976/1 "2019-06-11T07:25:29Z")

</div>

Hi, guys.  
I am currently stuck at the following problem.

I want to define a coefficient dependent on solution and subdomains. But it produced an error: setting an array element with a sequence.  
If i don’t use different coefficient on subdomains, it works. Also, if the coefficient doesn’t dependent on the solution, it works.

Here is part of my code related with the error:

> from fenics import \*
> 
> sigmaK\_g=2.6e-39;  
> sigmaK\_a=3.7e-108;
> 
> mesh = UnitSquareMesh(5,5);  
> U = FunctionSpace(mesh, “Lagrange”, 1);
> 
> class Air(SubDomain):  
> def inside(self, x, on\_boundary):  
> return x[0] \<= 0 + DOLFIN\_EPS
> 
> class Glass(SubDomain):  
> def inside(self, x, on\_boundary):  
> return x[0] \>= 0 - DOLFIN\_EPS
> 
> subdomains = MeshFunction(“size\_t”, mesh, mesh.topology().dim(),0)
> 
> air = Air().mark(subdomains,0)  
> glass = Glass().mark(subdomains, 1)
> 
> u = Function(U);  
> v =TestFunction(U);
> 
> class KK(UserExpression):  
> def **init** (self, subdomains, k\_0, k\_1, \*\*kwargs):  
> super(). **init** (\*\*kwargs)  
> self.subdomains = subdomains  
> self.k\_0 = k\_0  
> self.k\_1 = k\_1  
> def eval\_cell(self, values, x, cell):  
> if self.subdomains[cell.index] == 0:  
> values[0] = self.k\_0  
> else:  
> values[0] = self.k\_1  
> def value\_shape(self):  
> return ()
> 
> Wpii\_g=sigmaK\_g \* inner(u,u)\*\*2  
> Wpii\_a=sigmaK\_a \* inner(u,u)\*\*7  
> Wpii = KK(subdomains=subdomains, k\_0 = Wpii\_a, k\_1 = Wpii\_g, degree=1)
> 
> F=Wpii \* v \* u \* dx;
> 
> solve(F == 0, u)

And Here is my error code:

> ValueErrorTraceback (most recent call last)  
> in   
> 46 F=Wpii \* v \* u \* dx;  
> 47  
> —\> 48 solve(F == 0, u)
> 
> /usr/local/lib/python3.6/dist-packages/dolfin/fem/solving.py in solve(\*args, \*\*kwargs)  
> 218 # tolerance)  
> 219 elif isinstance(args[0], ufl.classes.Equation):  
> → 220 \_solve\_varproblem(\*args, \*\*kwargs)  
> 221  
> 222 # Default case, just call the wrapped C++ solve function
> 
> /usr/local/lib/python3.6/dist-packages/dolfin/fem/solving.py in \_solve\_varproblem(\*args, \*\*kwargs)  
> 264 solver = NonlinearVariationalSolver(problem)  
> 265 solver.parameters.update(solver\_parameters)  
> → 266 solver.solve()  
> 267  
> 268
> 
> /usr/local/lib/python3.6/dist-packages/dolfin/function/expression.py in wrapped\_eval\_cell(self, values, x, cell)  
> 54  
> 55 def wrapped\_eval\_cell(self, values, x, cell):  
> —\> 56 self.user\_expression.eval\_cell(values, x, cell)  
> 57  
> 58 # Attach user-provided Python eval functions (if they exist in
> 
> in eval\_cell(self, values, x, cell)  
> 36 values[0] = self.k\_0  
> 37 else:  
> —\> 38 values[0] = self.k\_1  
> 39 def value\_shape(self):  
> 40 return ()
> 
> ValueError: setting an array element with a sequence.

I found this error is related with shape of np array. But i can’t understand why it happened.  
Is there any way to define the different nonlinear coefficient in different subdomains?

Thank you for taking the time to help me out!

---

<div class="post-metadata">

**Author:** ![plugged](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/plugged/32/5115_2.png) [@plugged](https://fenicsproject.discourse.group/u/plugged)\
**Post date:** [June 11, 2019, 7:55am UTC](https://fenicsproject.discourse.group/t/subdomains-with-different-coefficient-dependent-on-solution/976/2 "2019-06-11T07:55:28Z")

</div>

Hey,

I assume that k0 and k1 are just fenics Functions. What you can do, to make it work, is to actually evaluate these functions, i.e., in your eval\_cell routine of KK, instead of

```
values[0] = self.k_0

```

use

```
values[0] = self.k_0(x)
```
