Note that the input to the eval_cell
function in user-expression is an numpy array of shape (9,). Thus, you need to convert the input to this shape.
Additionally, you are not assigning values to values, but overwriting the variable. Below I’ve created a functioning version:
Ex, Ey, nuxy, Gxy = 100., 10., 0.3, 5.
mport numpy as np
C_numpy = np.linalg.inv([[1./Ex,nuxy/Ex,0.],[nuxy/Ex,1./Ey,0.],[0.,0.,1./Gxy]])
class K(UserExpression):
def __init__(self,materials,d1,d2,**kwargs):
super().__init__(**kwargs)
self.materials=materials
self.d1= d1.reshape(-1)
self.d2=d2.reshape(-1)
def value_shape(self):
return (3,3)
def eval_cell(self,values,x,cell):
if self.materials[cell.index] == 1:
values[:]= self.d1
else:
values[:] = self.d2
C = K(materials,C_numpy,C_numpy,degree=0)