Adding a tensor to a constant tensor, creating a 4-tensor from two 2-tensors and contracting tensors

I define the metric tensor as a sum of a constant metric tenor plus another 2-tensor, upon which I minimize. How do I do that?
I tried the following code:

TFS = fem.VectorFunctionSpace(domain, ("CG", 1),dim=2)
q  = fem.Function(TFS)
g0=fem.Constant(domain, ScalarType((((1,0)), (0,1))))
gbar=g0+q

but I get the error

Can’t add expressions with different shapes.

Then, I would like to write the elastic tensor using the metric \bar{g}, namely A^{\alpha\beta\gamma\delta}=\frac{nu Y}{1-\nu^2}\left[\bar{g}^{\alpha\beta}\bar{g}^{\gamma\delta}+\frac{1-\nu}{2\nu}\left(\bar{g}^{\alpha\gamma}\bar{g}^{\beta\delta}+\bar{g}^{\alpha\delta}\bar{g}^{\beta\gamma}\right)\right]
and using is, define the energy which I would like to minimize:
E=\frac{1}{2}A^{\alpha\beta\gamma\delta}\epsilon^{\alpha\beta}\left(u\right)\epsilon^{\gamma\delta}\left(u\right)-\frac{1}{2}A^{\alpha\beta\gamma\delta}\epsilon^{\alpha\beta}\left(u\right)q^{\gamma\delta}\left(u\right)+\frac{1}{8}A^{\alpha\beta\gamma\delta}q^{\alpha\beta}q^{\gamma\delta}
where \epsilon\left(u\right) is defined as

def epsilon(u):
    return sym(grad(u))

How should I define A and E?

I dont understand how q Which consists of (q_0, q_1) should be added to g0, a 2x2 tensor (equivalent to a 2x2 identity matrix).

Thank you for your quick response. I change TFS to be

TFS = fem.TensorFunctionSpace(domain, ("CG",2))

And now I don’t get this error.

I created the following function to create A

def A(q):
    g0=fem.Constant(domain, ScalarType((((1,0)), (0,1))))
    gbar=g0+q
    A=np.zeros((2,2,2,2))
    for i in range (2):
      for j in range (2):
        for k in range (2):
          for k in range(2):
            A[i,j,k,l]=nu(Y/(1-nu**2))*(gbar[i,j]*gbar[k,l]+(1-nu)/(2*nu)(gbar[i,k]*gbar[j,l]+gbar[i,l]*gbar[j,k]))
    return as_tensor(A)

but I get the following error

—> 15 A[i,j,k,l]=nu(Y/(1-nu^2))*(gbar[i,j]gbar[k,l]+(1-nu)/(2nu)(gbar[i,k]*gbar[j,l]+gbar[i,l]*gbar[j,k]))
TypeError: ‘float’ object is not callable

What is the right way to create the 4-tensor A from the 2-tensor gbar?

You are mixing numpy and ufl.
A should be a ufl tensor; see for instance Slicing Tensors using subset of Free Indices or multiple free indices - #2 by dokken

1 Like