Error in assigning variable material properties


78
views
0
3 months ago by
I have 3 three materail inside the domain. Out of three two (1 and 3) has constant material propertFile attached: bimat.msh (1.4 MB)

y but one (2) has variable material (E1*x[0]).
While assigning I am getting error:  ValueError: setting an array element with a sequence.

How can I resolve this error. 

from dolfin import *
mesh = Mesh('./mesh/bimat.xml')
materials = MeshFunction("size_t", mesh, './mesh/bimat_physical_region.xml')

W = VectorFunctionSpace(mesh,'CG',1)
x = SpatialCoordinate(mesh)

E1 = 210e3
E2 = 180e3
E_int = Expression('E1*x[0]', E1=E1)

#---------------------------------------------------
# Material definition..
#---------------------------------------------------
plot(materials, interactive = True)
class K(Expression):
    def __init__(self, materials, k_0, k_1,k_2, **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] == 1 :
                values[0] = self.k_1
            elif self.materials[cell.index] == 2:
                values[0] = self.k_2
            else:
                values[0] = self.k_0

Ec = K(materials, E1, E2, E_int, degree = 1)
ZZ = FunctionSpace(mesh,'DG',0)
E = project(Ec, ZZ)

plot(E, interactive = True)
​
Community: FEniCS Project

1 Answer


2
3 months ago by
The following modified code runs for me in 2017.2 (although it looks like you may be using an earlier version, based on the plotting):

from dolfin import *
#mesh = Mesh('./mesh/bimat.xml')
mesh = UnitSquareMesh(10,10)
materials = MeshFunction("size_t", mesh, mesh.topology().dim())#, './mesh/bimat_physical_region.xml')
materials.set_all(2)

W = VectorFunctionSpace(mesh,'CG',1)
x = SpatialCoordinate(mesh)

E1 = 210e3
E2 = 180e3
E_int = Expression('E1*x[0]', E1=E1,degree=1)

#---------------------------------------------------
# Material definition..
#---------------------------------------------------
#plot(materials, interactive = True)
class K(Expression):
    def __init__(self, materials, k_0, k_1,k_2, **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] == 1 :
                values[0] = self.k_1
            elif self.materials[cell.index] == 2:
                #values[0] = self.k_2
                self.k_2.eval_cell(values,x,cell)
            else:
                values[0] = self.k_0

Ec = K(materials, E1, E2, E_int, degree = 1)
ZZ = FunctionSpace(mesh,'DG',0)
E = project(Ec, ZZ)

#plot(E, interactive = True)
​
Thanks for the help. It works for 1.6.0 also
written 3 months ago by hirshikesh  
Please login to add an answer/comment or follow this question.

Similar posts:
Search »