vendredi 16 octobre 2020

Using an array value in an if else statement

I am trying to run this analysis and plot a series of values over a range of time values t. Therefore, the variable t is an array. Ux and Uy are functions of t. U1 and U2 are functions of Ux and Uy so therefore they are functions of t. This relationship continues for Ep1 and Ep2. The problem arises at the location of the first if statement. It doesn't like me using an array value and provides the error: "The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()." I do not want to use either of those functions because that forces the statement to be either true all the time or false all the time whereas I need it to be conditional based on the value of t. I have also looked into a for loop for this but I am not very good with those so I could not find the right place to put the sequence. Please let me know the best course of action. Thank you.

Using Spyder(Python 3.8):

import numpy as np
import matplotlib.pyplot as plt

# Specified Parameters
E = 211400000 # KPa
Yo = 252600 # KPa
Ep = 16900000 # KPa
Atop = 0.000645 # m^2
Abot = 0.000750 # m^2
h = 1.8 # m
b = 2.4 # m

# Calculated Parameters
theta = np.arctan(h/b)
L = h/np.sin(theta)
Ey = Yo/E

# Displacement Time History
t = np.arange(0,2,0.01)
Ux = 0.01*np.sin(2*np.pi*t)
Uy = 0.01*np.sin(4*np.pi*t)

# Compatibility Equations
U1 = Ux*np.cos(theta) - Uy*np.sin(theta)
U2 = Ux*np.cos(theta) + Uy*np.sin(theta)

# Strain Displacement Equations
Ep1 = U1/L
Ep2 = U2/L

# Constituitive Relations
if Ep1 < -Ey:
    S1 = -Yo - E*(abs(Ep1)-Ey)
elif -Ey <= Ep1 <= Ey:
    S1 = E*Ep1
elif Ep1 > Ey:
    S1 = Yo + E*(Ep1-Ey)
    
if Ep2 < -Ey:
    S2 = -Yo - E*(abs(Ep2)-Ey)
elif -Ey <= Ep2 <= Ey:
    S2 = E*Ep2
elif Ep2 > Ey:
    S2 = Yo + E*(Ep2-Ey)

# Force-Stress Relations (Axial Forces)
F1 = S1*Atop
F2 = S2*Abot

# Equilibrium Equations
Px = (F1 + F2)*np.cos(theta)
Py = F1*np.sin(theta) - F2*np.sin(theta)

# Plot Values
plt.figure(1)
plt.title('Px vs Ux')
plt.xlabel('Ux [m]')
plt.ylabel('Px [kips]')
plt.plot(Ux, Px)
plt.grid(True)
plt.show()

plt.figure(2)
plt.title('Py vs Uy')
plt.xlabel('Uy [m]')
plt.ylabel('Py [kips]')
plt.plot(Uy, Py)
plt.grid(True)
plt.show()

plt.figure(3)
plt.title('Ux vs t')
plt.xlabel('t [sec]')
plt.ylabel('Ux [m]')
plt.plot(t, Ux)
plt.grid(True)
plt.show()
    
plt.figure(4)
plt.title('Uy vs t')
plt.xlabel('t [sec]')
plt.ylabel('Uy [m]')
plt.plot(t, Uy)
plt.grid(True)
plt.show()

plt.figure(5)
plt.title('Ux vs Uy')
plt.xlabel('Uy [m]')
plt.ylabel('Ux [m]')
plt.plot(Uy, Ux)
plt.grid(True)
plt.show()

plt.figure(6)
plt.title('F1 vs U1')
plt.xlabel('U1 [m]')
plt.ylabel('F1 [kips]')
plt.plot(U1, F1)
plt.grid(True)
plt.show()

plt.figure(7)
plt.title('F2 vs U2')
plt.xlabel('U2 [m]')
plt.ylabel('F2 [kips]')
plt.plot(U2, F2)
plt.grid(True)
plt.show()

Aucun commentaire:

Enregistrer un commentaire