Tuple index out of range

Hey there.

The following code is implemented to calculate σ in a 2D problem:

from fenics import*
from mshr import*

#dominio

L,R = 1., 0.1
N = 50

domain = Rectangle(Point(0.,0.),Point(L,L))-Circle(Point(0.,0.),R)
mesh = generate_mesh(domain,N)

#ecuaciones material compuesto def=S*sigma
#sigma=C*def con S inversa de C

Ex, Ey, nuxy, Gxy = 100., 10., 0.3, 5.
S = as_matrix([[1./Ex, -nuxy/Ex, 0.],[-nuxy/Ey, 1./Ey, 0.],[0.,0.,1./Gxy]])
C = inv(S)

#definición de funciones
def eps(v):
	return div(v)
def strain2voigt(e):
	return as_vector([e[0,0],e[1,1],2*e[0,1]])
def voigt2stress(s):
	return as_tensor ([[s[0],s[2],s[2],s[1]]])

def sigma(v):
	return voigt2stress(dot(C, strain2voigt(eps(v))))

#problema de posición
class Top(SubDomain):
	def inside(self, x, on_boundary):
		return near(x[1], L) and on_boundary
class Left(SubDomain):
	def inside(self, x, on_boundary):
		return near(x[0], 0) and on_boundary
class Bottom(SubDomain):
	def inside (self, x, on_boundary):
		return near(x[1], 0) and on_boundary

facets = MeshFunction("size_t", mesh, 1)
facets.set_all(0)
Top().mark(facets, 1)
Left().mark(facets, 2)
Bottom().mark(facets, 3)
ds = Measure('ds', subdomain_data=facets)

#resolver
V = VectorFunctionSpace(mesh, 'Lagrange', 2)
du = TrialFunction(V)
u_ = TestFunction(V)
u = Function(V, name='displacements')
a = inner(sigma(du),eps(u_))*dx

#tracción uniforme carasuperior
T = Constant((0, 1e-3))
l = dot(T,u_)*ds(1)

#condiciones de simetría
bc = [DirichletBC(V.sub(0),  Constant(0.), facets, 2), DirichletBC(V.sub(1), Constant(0.), facets, 3)]

#solver
solve (a==l, u, bc)

import matplotlib.pyplot as plt
p = plot(sigma(u)[1,1]/T[1], mode='color')
plt.colorbar(p)
plt.show()
---------------------------------------------
ERROR
----------------------------------------------
```Traceback (most recent call last):
  File "placaconagujero.py", line 53, in <module>
    a = inner(sigma(du),eps(u_))*dx
  File "placaconagujero.py", line 28, in sigma
    return voigt2stress(dot(C, strain2voigt(eps(v))))
  File "placaconagujero.py", line 23, in strain2voigt
    return as_vector([e[0,0],e[1,1],2*e[0,1]])
  File "/usr/lib/python3/dist-packages/ufl/exproperators.py", line 438, in _getitem
    all_indices, slice_indices, repeated_indices = create_slice_indices(component, shape, self.ufl_free_indices)
  File "/usr/lib/python3/dist-packages/ufl/index_combination_utils.py", line 152, in create_slice_indices
    if int(ind) >= shape[len(all_indices)]:
IndexError: tuple index out of range

I already changed sym(grad(v)) to simply div(v) as mentioned on this forum, but I still get this error.

Thanks.

This is definitiely not what you want to do, as sym(grad(v)) is a matrix, div(v) is a scalar value.

Going back to sym(grad(v))
You can

print(sigma(du).ufl_shape)

which gives you

(1,4)

which I guess you want to be (2, 2), thus:

def voigt2stress(s):
	return as_tensor ([[s[0],s[2]],[s[2],s[1]]])

is probably what you want

1 Like