### How to define an expression depending on the solution?

554
views
1
11 months ago by
I am solving the heat function on a 3D domain. Materials property(let say thermal conductivity, K) should be defined based on the temperature at each point.(ie T< T0, k=k1, else k2)
I am trying following code:
u = TrialFunction(V)
v = TestFunction(V)
f = Constant(10)
u = Function(V)
#####Drichlet boundary and initial values and timesteps are given
tol = 1E-6
Tempr = interpolate(Constant(10), V)
Tempr = Tempr.compute_vertex_values(mesh)
Temp = u.compute_vertex_values(mesh)
k1=10
k2=20
k = Expression('Temp < Tempr+tol ? k1 : k2', degree=0, k1=k1, k2=k2, Temp=Temp, Tempr=Tempr,  tol=tol)

a, L = lhs(F), rhs(F)
for n in range(tsteps+1)
t += dt
solve(a == L, u, bc)
vertex_values_kr = kr.compute_vertex_values(mesh)
vtkfile << (u, t)
u_n.assign(u)
################################################################################################################3
it gives this error
File "/usr/lib/python2.7/dist-packages/dolfin/functions/expression.py", line 686, in __new__
_check_kwargs(members, kwargs, not_allowed, valid_kws)
File "/usr/lib/python2.7/dist-packages/dolfin/functions/expression.py", line 932, in _check_kwargs
raise TypeError("expected default arguments for member variables "
TypeError: expected default arguments for member variables to be scalars or GenericFunctions.
Aborted (core dumped)

Community: FEniCS Project
how about some code formatting?  my sensitive eyes don't like the picture you have painted.
written 10 months ago by pf4d
your right sorry...it was my first time asking some question....I will use some formatting.
written 9 months ago by Nasibeh Hassanjanikhoshkroud

0
11 months ago by
Hi, may be you can use something like this:
kk = conditional(lt(temp),tempr,k1,k2)​

Thanks

Thanks for the answer. yes the conditional will work. But the problem is I need to define k based on temp and also the coordinate..As I found till now expression is great for coordinate and conditional for the the problem I mentioned above. I though there might be a way to combine these two in expression which I guess is not possible. So I decided to write my own function to define k value for each vertex.
written 10 months ago by Nasibeh Hassanjanikhoshkroud
0
11 months ago by
As the error message says, the arguments are supposed to be scalars or generic functions but you feed it with NumPy arrays (Temp, Tempr). Have a look here: https://www.allanswered.com/post/wxgz/.