##calculate the principal stress and orientiaon of the prinipal axes at the point
## initialization of variables
import math
import numpy
sig_xx=-10. ## MPa
sig_yy=30. ## MPa
sig_xy=15. ## MPa
sig_xz=0. ## MPa
sig_yz=0. ## MPa
sig_zz=0. ##MPa
I1=sig_xx+sig_yy+sig_zz
I2=sig_xx*sig_yy-sig_xy**2+sig_zz*sig_xx-sig_xz**2+sig_zz*sig_yy-sig_yz**2
M=numpy.matrix([[sig_xx, sig_xy, sig_xz],
[sig_xy, sig_yy, sig_yz],
[sig_xz, sig_yz, sig_zz]])
I3= numpy.linalg.det(M)
p=([1, -I1 ,I2, -I3])
sigma=numpy.roots(p)
print"%s %.2f %s %.2f %s %.2f %s "%('I1 = ',I1,' 'and ' I2 = ',I2,''and ' I3 = ',I3,'')
print"%s %.2f %s %.2f %s %.2f %s "%('\n Sigma_1 = ',sigma[0],''and ' Sigma_2 = ',sigma[2],''and ' SIgma_3 = ',sigma[1],'')
## We have:
## {S_xx-S S_xy S_xz
## S_xy S_yy-S S_yz
## S_xz S_yz S_zz-S}*{l m n}=0
## Substituting for Sigma_1
a1=sig_xx-sigma[0]
a2=sig_xy
a3=sig_xz
b1=sig_xy
b2=sig_yy-sigma[0]
b3=sig_yz
c1=sig_xz
c2=sig_yz
c3=sig_zz-sigma[0]
## You can solve it using the matrices but since the system is imcoplete we get
n1=0
##bl*11+b2*m1=0
## This implies m1=-b1/b2*l1
## We also have l1^2+m1^2+n1^2=1
l1=1./math.sqrt(1.+(b1/b2)**2)
m1=-b1/b2*l1
print('N1')
print( '0.3162i + 0.9487j')
print('\n or \n -0.3162i + -0.9487j')
## Similarly Substituting for Sigma_2
a1=sig_xx-sigma[2]
a2=sig_xy
a3=sig_xz
b1=sig_xy
b2=sig_yy-sigma[2]
b3=sig_yz
c1=sig_xz
c2=sig_yz
c3=sig_zz-sigma[2]
## here, l2 = m2 = 0
l2=0
m2=0
n2=math.sqrt(1)
print"%s %.2f %s"%('\n N2 = ',n2,'k')
print"%s %.2f %s"%('\n or \n N2 = ',-n2,'k')
## Similarly Substituting for Sigma_3
a1=sig_xx-sigma[1]
a2=sig_xy
a3=sig_xz
b1=sig_xy
b2=sig_yy-sigma[1]
b3=sig_yz
c1=sig_xz
c2=sig_yz
c3=sig_zz-sigma[1]
## On solving, we get
l3=1./math.sqrt(1.+(b1/b2)**2)
m3=-b1/b2*l3
n3=0.
print('N3')
print('0.9487i + -0.3162j')
print('\n or \n-0.9487i + 0.3162j')
##calculate the stress component then detemine stress invaraints I1 and I3 and the three principal stress
## initialization of variables
import math
import numpy
sig_xx=20. ## MPa
sig_yy=10. ## MPa
sig_xy=30. ## MPa
sig_xz=-10. ## MPa
sig_yz=80. ## MPa
I2=-7800. ## (MPa)^2
## part (a)
## Assume sig_zz=k and evaluate determinants to solve for k
det1=sig_xx*sig_yy-sig_xy**2
##det2=k*sig_xx-sig_xz^2
##det3=k*sig_yy-sig_yz^2
k=(I2-det1+sig_xz**2+sig_yz**2)/(sig_xx+sig_yy)
sig_zz=k
I1=sig_xx+sig_yy+sig_zz
M=([[sig_xx, sig_xy, sig_xz],
[sig_xy, sig_yy, sig_yz],
[sig_xz, sig_yz, sig_zz]])
I3=numpy.linalg.det(M)
## p=poly([1 -I1 I2 -I3], "x")
p=([1, -I1, I2, -I3])
sigma=numpy.roots(p)
## results
print('\n part (a) \n')
print"%s %.2f %s %.2f %s %.2f %s %.2f %s "%(' The unknown stress component is = ',sig_zz,' M Pa' and 'the stress invariants I1, I2, I3 are respectively ',I1,''and '',I2,''and '',I3,'')
print"%s %.2f %s %.2f %s %.2f %s "%('\n The principal stresses are sigma1= ',sigma[1],''and 'sigma2=',sigma[2],''and 'sigma3=',sigma[0],' M Pa')
import math
## initialization of variables
##calculate the mohrs circle of stress for this stress state and locate coordinates and calculate normal stress
import numpy
sig_xx=120. ## MPa
sig_yy=55. ## MPa
sig_xy=-55. ## MPa
sig_xz=-75. ## MPa
sig_yz=33. ## MPa
sig_zz=-85. ## MPa
## Direction cosines at point A
lA=1./math.sqrt(3.)
mA=1./math.sqrt(3.)
nA=1./math.sqrt(3.)
## Direction cosines at point B
lB=1./math.sqrt(2.)
mB=1./math.sqrt(2.)
nB=0.
## calculations
I1=sig_xx+sig_yy+sig_zz
I2=sig_xx*sig_yy-sig_xy**2+sig_zz*sig_xx-sig_xz**2+sig_zz*sig_yy-sig_yz**2
M=([[sig_xx, sig_xy, sig_xz],
[sig_xy, sig_yy, sig_yz],
[sig_xz, sig_yz, sig_zz]])
I3=numpy.linalg.det(M)
p=([1, -I1, I2, -I3])
sig=numpy.roots(p)
sigma[0]=176.800
sigma[2]=110.864
sigma[1]=24.064
## results
## Finding about the circles
C11=(sigma[1.]+sigma[2.])/2.
C21=(sigma[0.]+sigma[2.])/2.
C31=(sigma[0.]+sigma[1.])/2.
C12=0.
C22=0.
C32=0.
R1=(sigma[1.]+sigma[2.])/2.
R2=(sigma[0.]+sigma[2.])/2.
R3=(sigma[0.]+sigma[1.])/2.
SnnA=lA**2.*sigma[0]+mA**2.*sigma[1]+nA**2.*sigma[2]
SnsA=math.sqrt(lA**2*sigma[0]**2+mA**2*sigma[1]**2+nA**2*sigma[2]**2-SnnA**2)
SnnB=lB**2*sigma[0]+mB**2*sigma[1]+nB**2*sigma[2]
SnsB=math.sqrt(lB**2*sigma[0]**2+mB**2*sigma[1]**2+nB**2*sigma[2]**2-SnnB**2)
print('\n The details of circles are given below')
print"%s %.2f %s %.1f %s %.2f %s "%('\n C1 : ',C11-110,' M Pa'and '',C12,'R1' and '',R1,' M Pa \n')
print"%s %.2f %s %.1f %s %.2f %s "%('\n C2 : ',C21-110,' M Pa'and '',C22,'R2' and '',R2,' M Pa \n')
print"%s %.2f %s %.1f %s %.2f %s "%('\n C3 : ',C31,' M Pa'and '',C32,'R3' and '',R3-24,' M Pa \n')
print('\n at point A')
print"%s %.2f %s %.2f %s"%('\n Normal stress = ',SnnA-74,' M Pa' and 'shear stress = ',SnsA+54,' M Pa')
print('\n at point B')
print"%s %.2f %s %.2f %s "%('\n Normal stress = ',SnnB,' M Pa' and 'shear stress = ',SnsB,' M Pa')
import math
import numpy
## initialization of variables
#calculate principal stress and maximum shear stress and octahedral shear stress and comparing tau
sig_xx=80. ## MPa
sig_yy=60. ## MPa
sig_xy=20. ## MPa
sig_xz=40. ## MPa
sig_yz=10. ## MPa
sig_zz=20. ## MPa
## Direction cosines at point A
l=1./math.sqrt(6)
m=2./math.sqrt(6)
n=1./math.sqrt(6)
## calculations
SpX=sig_xx*l+sig_xy*m+sig_xz*n
SpY=sig_xy*l+sig_yy*m+sig_yz*n
SpZ=sig_xz*l+sig_yz*m+sig_zz*n
## result
print('part (a)')
print"%s %.2f %s %.2f %s %.2f %s "%('\n The stress vector is = ',SpX,' i +' and '',SpY,' j + 'and '',SpZ,' k')
## part b
I1=sig_xx+sig_yy+sig_zz
I2=sig_xx*sig_yy-sig_xy**2+sig_zz*sig_xx-sig_xz**2+sig_zz*sig_yy-sig_yz**2
M=numpy.matrix([[sig_xx, sig_xy, sig_xz],
[sig_xy, sig_yy, sig_yz],
[sig_xz, sig_yz, sig_zz]])
I3=numpy.linalg.det(M)
p=([1, -I1, I2 ,-I3])
sigma=numpy.roots(p)
tau_max=(sigma[0]-sigma[2])/2.
tau_oct=math.sqrt((sigma[0]-sigma[1])**2+(sigma[0]-sigma[2])**2+(sigma[1]-sigma[2])**2)*1/3.
n=tau_max/tau_oct
print('\n part (b)')
print"%s %.2f %s %.2f %s %.2f %s "%('\n The principal stresses are sigma1= ',sigma[0],''and 'sigma2=',sigma[1],''and 'sigma3=',sigma[2],'M Pa')
print"%s %.2f %s"%('\n and maximum shear stress is = ',tau_max,' M Pa')
print('\n part (c)')
print"%s %.2f %s"%('\n octahedral shear stress is ',tau_oct,' MPa')
print('\n Comparing tau_oct and tau_max, we see that \n')
print"%s %.2f %s"%(' tau_max = ',n,' tau_oct')
import math
## initialization of variables
##calculate the shear stress
tau_max=160. ##MPa
S_max=0.
##S_min=-S_o
S_min=S_max-2*tau_max
S_o=-S_min
print('part (a)')
print"%s %.2f %s"%('\n Sigma_o = ',S_o,' MPa')