0

necesito adaptar el siguiente codigo de matlab a python:

%Holms case properties
E=120.66e9; %Young modulus, E=120.66GPa
alpha=14.4e-6; %Expansion coef, Alpha=10e-6 mm-1K-1
v=0.3; %Poisson modulus
a=101.6/1000; %Tube inner radius 101.6 mm
b=304.8/1000; %Tube outer radius 304.8 mm

%Calculation of internal and external temperature profiles
r=[a:(b-a)/19:b]; %20 divisions in radial coordinate
Theta=[0:360/71:360]; %72 divisions in circunferential coordinates
Theta=Theta*pi/180; %Angle in radians
T=zeros([size(Theta,2),size(r,2)]); %Temperature array, rows angle variation, column radial variation
for i=1:size(r,2) %To create temperature distribution (See Logie et al. Eq. 53)
    for i2=1:size(Theta,2)
        T(i2,i)=((555.56*b)/(b^2-a^2))*((r(i)^2-a^2)/r(i))*cos(Theta(i2))+277.78*(1-log(b/r(i))/log(b/a))+273.15;
    end
end
Tint=T(:,1);
Text=T(:,size(r,2));
%Fourier fitting
Tm_o=550.9;
B1_o=555.6;
Tm_i=273.15;
B1_i=0;

%Stress calculation
K=(Tm_i-Tm_o)/log(b/a);
for i=1:size(r,2)
    for i2=1:size(Theta,2)
        K_theta(i2,i)=(r(i)*a*b)/(b^2-a^2)*(((B1_i*b-B1_o*a)/(a^2+b^2))*cos(Theta(i2)));
        T_theta(i2,i)=T(i2,i)-(Tm_i-Tm_o)*log(b/r(i))/log(b/a)-Tm_o;
        sigma_r(i2,i)=(K*alpha*E/(2*(1-v)))*(-log(b/r(i))-(a^2/(b^2-a^2))*(1-b^2/r(i)^2)*log(b/a))+K_theta(i2,i)*((alpha*E)/(2*(1-v)))*(1-a^2/r(i)^2)*(1-b^2/r(i)^2);
        sigma_theta(i2,i)=(K*alpha*E/(2*(1-v)))*(1-log(b/r(i))-(a^2/(b^2-a^2))*(1+b^2/r(i)^2)*log(b/a))+K_theta(i2,i)*(alpha*E/(2*(1-v)))*(3-(a^2+b^2)/r(i)^2-a^2*b^2/r(i)^4);
        sigma_z(i2,i)=(K*alpha*E/(2*(1-v)))*(1-2*log(b/r(i))-((2*a^2)/(b^2-a^2))*log(b/a))+(K_theta(i2,i)*alpha*E*v/(1-v))*(2-(a^2+b^2)/r(i)^2)-alpha*E*T_theta(i2,i);
        sigma_eq(i2,i)=sqrt(((sigma_r(i2,i)-sigma_theta(i2,i))^2+(sigma_theta(i2,i)-sigma_z(i2,i))^2+(sigma_z(i2,i)-sigma_r(i2,i))^2)/2);
    end
end

Hasta ahora, mi codigo de python es:

import math
from math import pi,cos,sqrt,log
import numpy as np
import matplotlib.pyplot as plt
#Holms case properties
E=120.66e9     ##Young modulus E=120.66GPa
alpha=14.4e-6 ##Expansion coef, Alpha=10e-6 mm-1K-1
v=0.3 ##Poisson modulus
a=101.6/1000 ##Tube inner radius 101.6 mm
b=304.8/1000 ##Tube outer radius 304.8 mm
c=20
w=(b-a)/c
z=72
ang=360/z

##Calculation of internal and external temperature profiles
r=[] 
Theta=[]
index=0
for i in range(c):
    r.append(index)
    r[index]=a+index*w
    index=index+1
index=0
for i in range(0,73):
    Theta.append(index)
    Theta[index]=i*ang
    Theta[index]=Theta[index]*pi/180
    
w=len(r)
h=len(Theta)
 ##Angle in radians
T=np.zeros((w, h))##Temperature array, rows angle variation, column radial variation
for i in range(w):
    ##To create temperature distribution ##(See Logie et al. Eq. 53)
    for i2 in range(h):
        T[i][i2] = ((555.56 * b) / (b ** 2 - a ** 2)) * ((r[i] ** 2 - a ** 2) / r[i]) * cos(Theta[i2]) + 277.78 * (1 - log(b / r[i]) / log(b / a)) + 273.15
Tint=T[:,1]
Text=T[:,w]
##Fourier fitting
Tm_o=550.9
B1_o=555.6
Tm_i=273.15
B1_i=0
K_theta=[[],[]]
T_theta=[[],[]]
sigma_r=[[],[]]
sigma_theta=[[],[]]
sigma_z=[[],[]]
sigma_eq=[[],[]]

##Stress calculation
K=(Tm_i-Tm_o)/log(b/a)
for i in range (w):
    for i2 in range (h):
        K_theta[i2][i] = (r[i]*a*b)/(b**2-a**2)*(((B1_i*b-B1_o*a)/(a**2+b**2))*cos(Theta[i2]))
        T_theta[i2][i]=T[i2][i]-(Tm_i-Tm_o)*log(b/r[i])/log(b/a)-Tm_o
        sigma_r[i2][i]=(K*alpha*E/(2*(1-v)))*(-log(b/r[i])-(a**2/(b**2-a**2))*(1-b**2/r[i]**2)*log(b/a))+K_theta[i2][i]*((alpha*E)/(2*(1-v)))*(1-a**2/r[i]**2)*(1-b**2/r[i]**2)
        sigma_theta[i2][i]=(K*alpha*E/(2*(1-v)))*(1-log(b/r[i])-(a**2/(b**2-a**2))*(1+b**2/r[i]**2)*log(b/a))+K_theta[i2][i]*(alpha*E/(2*(1-v)))*(3-(a**2+b**2)/r[i]**2-a**2*b**2/r[i]**4)
        sigma_z[i2][i]=(K*alpha*E/(2*(1-v)))*(1-2*log(b/r[i])-((2*a**2)/(b**2-a**2))*log(b/a))+(K_theta[i2][i]*alpha*E*v/(1-v))*(2-(a**2+b**2)/r[i]**2)-alpha*E*T_theta[i2][i]
        sigma_eq[i2][i]=sqrt(((sigma_r[i2][i]-sigma_theta([i2][i])**2+(sigma_theta[i2][i]-sigma_z[i2][i])**2+(sigma_z[i2][i]-sigma_r[i2][i])**2)/2)

Al compilar, me salta el error unexpected EOF while parsing al final del todo y no sé porque. Alguien sabe porque me sale este error?

1
  • Que tratas de hacer? Lo más probable es que haya un modulo de python que te simplifique las cosas. Recuerda que en python se opta por la simplicidad y estilo pythonico
    – Christian
    Commented el 4 may. 2022 a las 1:47

1 respuesta 1

0

Tienes errores de sintaxis y además estás creando los vectores con la longitud incorrecta.

Te adjunto tu código con las correcciones correspondientes:

from math import pi, cos, sqrt, log
import numpy as np

E = 120.66e9;
alpha = 14.4e-6;
v = 0.3;
a = 101.6 / 1000;
b = 304.8 / 1000;

##Calculation of internal and external temperature profiles
r = np.arange(a, b + (b - a) / 20, (b - a) / 19)
Theta = np.arange(0, 360 + 360 / 71, 360 / 71)
Theta = Theta * pi / 180

T = np.zeros([len(Theta), len(r)])

for i in range(len(Theta)):
    for j in range(len(r)):
        T[i][j] = ((555.56 * b) / (b ** 2 - a ** 2)) * ((r[j] ** 2 - a ** 2) / r[j]) * cos(Theta[i]) + 277.78 * (
                1 - log(b / r[j]) / log(b / a)) + 273.15

Tint = T[:, 0]
Text = T[:, np.size(r) - 1]

Tm_o = 550.9
B1_o = 555.6
Tm_i = 273.15
B1_i = 0

K = (Tm_i - Tm_o) / log(b / a)

K_theta = np.zeros([len(Theta), len(r)])
T_theta = np.zeros([len(Theta), len(r)])
sigma_r = np.zeros([len(Theta), len(r)])
sigma_theta = np.zeros([len(Theta), len(r)])
sigma_z = np.zeros([len(Theta), len(r)])
sigma_eq = np.zeros([len(Theta), len(r)])
for i in range(np.size(r)):
    for j in range(np.size(Theta)):
        K_theta[j][i] = (r[i] * a * b) / (b ** 2 - a ** 2) * (((B1_i * b - B1_o * a) / (a ** 2 + b ** 2)) * cos(Theta[j]))
        T_theta[j][i] = T[j][i] - (Tm_i - Tm_o) * log(b / r[i]) / log(b / a) - Tm_o
        sigma_r[j][i] = (K * alpha * E / (2 * (1 - v))) * (-log(b / r[i]) - (a ** 2 / (b ** 2 - a ** 2)) * (1 -b ** 2 / r[i] ** 2) * log(b / a)) + K_theta[j][i] * ((alpha * E) / (2 * (1 - v))) * (1 - a ** 2 / r[i] ** 2) * (1 - b ** 2 / r[i] ** 2)
        sigma_theta[j][i] = (K * alpha * E / (2 * (1 - v))) * (1 - log(b / r[i]) - (a ** 2 / (b ** 2 - a ** 2)) *(1 + b ** 2 / r[i] ** 2) * log(b / a)) + K_theta[j][i] * (alpha * E / (2 * (1 - v))) * (3 - (a ** 2 + b ** 2) / r[i] ** 2 - a ** 2 * b ** 2 / r[i] ** 4)
        sigma_z[j][i]= (K * alpha * E / (2 * (1 - v))) * (1 - 2 * log(b / r[i]) - ((2 * a ** 2) / (b ** 2 - a ** 2)) * log(b / a)) + (K_theta[j][i] * alpha * E * v / (1 - v)) * (2 - (a ** 2 + b ** 2) / r[i] ** 2) - alpha * E * T_theta[j][i]
        sigma_eq[j][i] = sqrt(((sigma_r[j][i] - sigma_theta[j][i]) ** 2 + (sigma_theta[j][i] - sigma_z[j][i]) ** 2 + (sigma_z[j][i] - sigma_r[j][i]) ** 2) / 2)

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.