0

I want to add the following lists as elements into a NumPy matrix so that later on, I could eliminate some rows and columns.

ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 2 dimensions. The detected shape was (5, 5) + inhomogeneous part.

import numpy as np
import math
import sympy as sp


def FSDT(a,b,E11,E22,Nu12,Nu21,G12,G13,G23,theta,PlyNumber,K,h):

    #----------------------------------------------------------
    i=0    
    h  = PlyNumber*LaminaThickness
    Z = []
    Z.append(-h/2.0)
    
    for j in range(1,PlyNumber+1,1):
        Z.append(Z[j-1]+LaminaThickness)
    #Matrix Q

    Q11=[]
    Q12=[]
    Q22=[]
    Q66=[]
    Q44=[]
    Q55=[]

    Q11_B = []
    Q12_B = []
    Q16_B = []
    Q22_B = []
    Q26_B = []
    Q66_B = []
    Q44_B = []
    Q45_B = []
    Q55_B = []
    

    for i in range(0,1,1):
        Q11.append(E11/(1-Nu12*Nu21))
        Q12.append((Nu12*E22)/(1-Nu12*Nu21))
        Q22.append((E22)/(1-Nu12*Nu21))
        Q44.append(G23)
        Q66.append(G12)
        Q55.append(G13)

        i=0
        for j in range(0,PlyNumber,1):
            Q11_B.append(Q11[i]*(np.cos(theta[j]*np.pi/180.0)**4)+2*(Q12[i]+2*Q66[i])*  (np.sin(theta[j]*np.pi/180.0))**2*(np.cos(theta[j]*np.pi/180.0))**2+Q22[i]*(np.sin(theta[j]*np.pi/180.0))**4)
            Q12_B.append((Q11[i]+Q22[i]-4*Q66[i])*(np.sin(theta[j]*np.pi/180.0))**2*(np.cos(theta[j]*np.pi/180.0))**2+Q12[i]*((np.sin(theta[j]*np.pi/180.0))**4+((np.cos(theta[j]*np.pi/180.0))**4)))
            Q16_B.append((Q11[i]-Q12[i]-2*Q66[i])*np.sin(theta[j]*np.pi/180.0)*np.cos(theta[j]*np.pi/180.0)**3+(Q12[i]-Q22[i]+2*Q66[i])*np.cos(theta[j]*np.pi/180.0)*np.sin(theta[j]*np.pi/180.0)**3)
            Q22_B.append(Q11[i]*np.sin(theta[j]*np.pi/180.0)**4+2*(Q12[i]+2*Q66[i])*np.sin(theta[j]*np.pi/180.0)**2*np.cos(theta[j]*np.pi/180.0)**2+Q22[i]*np.cos(theta[j]*np.pi/180.0)**4)
            Q26_B.append((Q11[i]-Q12[i]-2*Q12[i])*np.sin(theta[j]*np.pi/180.0)**3*np.cos(theta[j]*np.pi/180.0)+(Q12[i]-Q22[i]+2*Q66[i])*np.cos(theta[j]*np.pi/180.0)**3*np.sin(theta[j]*np.pi/180.0))
            Q66_B.append((Q11[i]+Q22[i]-2*Q12[i]-2*Q66[i])**np.sin(theta[j]*np.pi/180.0)**2**np.cos(theta[j]*np.pi/180.0)**2+Q66[i]*(np.sin(theta[j]*np.pi/180.0)**4+np.cos(theta[j]*np.pi/180.0)**4))
            Q44_B.append(Q44[i]*np.cos(theta[j]*np.pi/180.0)**2 + Q55[i]*np.sin(theta[j]*np.pi/180.0)**2)
            Q55_B.append(Q44[i]*np.sin(theta[j]*np.pi/180.0)**2 + Q55[i]*np.cos(theta[j]*np.pi/180.0)**2)
            Q45_B.append((Q55[i]-Q44[i])*np.cos(theta[j]*np.pi/180.0)*np.sin(theta[j]*np.pi/180.0))

            Q = np.matrix([[Q11_B, Q12_B, Q16_B, 0, 0],[Q12_B, Q22_B, Q26_B, 0, 0],[Q16_B, Q26_B, Q66_B, 0, 0],[0, 0, 0, Q44_B, Q45_B],[0, 0, 0, Q45_B, Q55_B]])
            return Q
#-----------------------------------------------------------------------------------------------

# def FSDT(E11,E22,G12,G13,G23,Nu12,Nu21,LaminaThickness,PlyNumber):

a = 10
b = 5
E11 = 131000
E22 = 32750
Nu12 = 0.25
Nu21 = 0.33
G12 = 19785
G13 = 16854
G23 = 19785
rho = 1650
K= np.pi**2/12

theta = [0,90,0,90,0]
PlyNumber = len(theta)
LaminaThickness = 0.01
h = 0.5
Q = FSDT(a,b,E11,E22,Nu12,Nu21,G12,G13,G23,theta,PlyNumber,K,h)

Or there is another way I could write the Q matrix??

Thank you for replying

I have tried to use np. array np. mat to define Q, but the same problem keeps occurring when running the code, it seems like the Q matrix is inhomogeneous.

2
  • 1
    Q = np.matrix(..., dtype=object) no error and no warning for me Commented Jun 15, 2023 at 15:57
  • 1
    I do get the ValueError like you described. Do you know what your expected output array shape should be? Also, could you share more context on what you're trying to accomplish? That may help decipher your code and properly refactor. Commented Jun 15, 2023 at 16:05

1 Answer 1

1

I tried to sort out what exactly what you were going for, and based on my best guess I don't think you need most of the looping and lists. Your return statement was also in the wrong spot, so overall your code was trying to return some kind of Frankenstein matrix. Let me know if this gives you what you're looking for, and if not, give some more explanation on your use case so this could be altered:

import numpy as np
import math


def FSDT(a,b,E11,E22,Nu12,Nu21,G12,G13,G23,theta,PlyNumber,K,h):

    #----------------------------------------------------------
    ## removed values aren't being used below

    ## instantiated an array of zeros to hold information
    Q = np.zeros((5,5,PlyNumber))
    

    Q11 = (E11/(1-Nu12*Nu21))
    Q12 = ((Nu12*E22)/(1-Nu12*Nu21))
    Q22 = ((E22)/(1-Nu12*Nu21))
    Q44 = (G23)
    Q66 = (G12)
    Q55 = (G13)

    for j in range(0,PlyNumber,1):
        Q11_B = (Q11*(np.cos(theta[j]*np.pi/180.0)**4)+2*(Q12+2*Q66)*  (np.sin(theta[j]*np.pi/180.0))**2*(np.cos(theta[j]*np.pi/180.0))**2+Q22*(np.sin(theta[j]*np.pi/180.0))**4)
        Q12_B = ((Q11+Q22-4*Q66)*(np.sin(theta[j]*np.pi/180.0))**2*(np.cos(theta[j]*np.pi/180.0))**2+Q12*((np.sin(theta[j]*np.pi/180.0))**4+((np.cos(theta[j]*np.pi/180.0))**4)))
        Q16_B = ((Q11-Q12-2*Q66)*np.sin(theta[j]*np.pi/180.0)*np.cos(theta[j]*np.pi/180.0)**3+(Q12-Q22+2*Q66)*np.cos(theta[j]*np.pi/180.0)*np.sin(theta[j]*np.pi/180.0)**3)
        Q22_B = (Q11*np.sin(theta[j]*np.pi/180.0)**4+2*(Q12+2*Q66)*np.sin(theta[j]*np.pi/180.0)**2*np.cos(theta[j]*np.pi/180.0)**2+Q22*np.cos(theta[j]*np.pi/180.0)**4)
        Q26_B = ((Q11-Q12-2*Q12)*np.sin(theta[j]*np.pi/180.0)**3*np.cos(theta[j]*np.pi/180.0)+(Q12-Q22+2*Q66)*np.cos(theta[j]*np.pi/180.0)**3*np.sin(theta[j]*np.pi/180.0))
        Q66_B = ((Q11+Q22-2*Q12-2*Q66)**np.sin(theta[j]*np.pi/180.0)**2**np.cos(theta[j]*np.pi/180.0)**2+Q66*(np.sin(theta[j]*np.pi/180.0)**4+np.cos(theta[j]*np.pi/180.0)**4))
        Q44_B = (Q44*np.cos(theta[j]*np.pi/180.0)**2 + Q55*np.sin(theta[j]*np.pi/180.0)**2)
        Q55_B = (Q44*np.sin(theta[j]*np.pi/180.0)**2 + Q55*np.cos(theta[j]*np.pi/180.0)**2)
        Q45_B = ((Q55-Q44)*np.cos(theta[j]*np.pi/180.0)*np.sin(theta[j]*np.pi/180.0))

        Q[:,:,j] = np.array([[Q11_B, Q12_B, Q16_B, 0, 0],[Q12_B, Q22_B, Q26_B, 0, 0],[Q16_B, Q26_B, Q66_B, 0, 0],[0, 0, 0, Q44_B, Q45_B],[0, 0, 0, Q45_B, Q55_B]])
    return Q
#-----------------------------------------------------------------------------------------------


a = 10
b = 5
E11 = 131000
E22 = 32750
Nu12 = 0.25
Nu21 = 0.33
G12 = 19785
G13 = 16854
G23 = 19785
rho = 1650
K= np.pi**2/12

theta = [0,90,0,90,0]
PlyNumber = len(theta)
LaminaThickness = 0.01
h = 0.5
Q = FSDT(a,b,E11,E22,Nu12,Nu21,G12,G13,G23,theta,PlyNumber,K,h)

print(Q.shape)


(5, 5, 5)
Sign up to request clarification or add additional context in comments.

1 Comment

I am trying to get a matrix Q of a dimension that depends on the ply number which is in my example a 5x5 inside each element of the matrix a list of the Q.._B

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.