将我的java代码转换为python:我不明白为什么这些值不同

问题描述 投票:0回答:1

我在将 Java 代码转换为 Python 时遇到问题。这是我的代码,它可以工作并输出所需的结果:

import java.io.*;
import java.util.*;
import static java.lang.Math.*;
public class Main {
public static void main(String[] args) throws IOException {
int i; // step number
int N =438; // the number of time steps
double t; // the time when all the drops evaporate;
double tmin=0; // the initial moment of time
double dt1=47.25; // the step by time for the value of nopt1
double dt2=10.22; // // the step by time for the value of nopt2
double a1, a2; // intermediate values for the final formula
double s1, s2; // intermediate values for the final formula
double y; // intermediate values for the final formula
double Dp; // coefficient of molecular diffusion of water vapor in air,m^2/s
double El; pressure of saturated water vapor over ice at temperature T, Pa
double Ev; // pressure of saturated water vapor with water at temperature T, Pa
double E0=610.78; // saturation pressure of water vapor at temperature T0 = 273.15, Pa
double S=1; // relative humidity
double rl; // radius of ice particles, m
double r1, r2; // radius of ice particles 
double r0l=2e-6; // initial radius of an ice particle, m
double r0v=4e-6; // initial radius of a drop,m
double t1,t2; // the time when all drops evaporate for nopt1 and nopt2;
double RR1, RR2; // intermediate values for the final formula
double rv1, rv2; //radius of water droplets, m
double L1, L2; // range of visibility for nopt1 and nopt2, m
double nopt1=1.1e+3; //the concentration of ice crystals 
double nopt2=1.1e+4; // concentration of ice crystals 
double T=268.15; // air temperature, K
double b=0.62; // dimensionless coefficient
double nv=1e+9; // volume concentration of a drop, m-3
double pl=917; // ice density, kg/m3
double pv=1000; // water density, kg/m3
double Rp=461; // gas constant of water vapor,
double P=100000;  // pressure, Pa

 PrintStream out1 = new PrintStream(new FileOutputStream("outfile1.txt"));
 PrintStream out2 = new PrintStream(new FileOutputStream("outfile2.txt"));
// The calculation is performed for two values of popt
// calculate the pressure of saturated water vapor above water and above ice at a temperature of T according to Magnus formulas
 Ev=E0*pow(10, ((7.63*(T-273.15))/(T-31.25)));
 El=E0*pow(10, ((9.5*(T-273.15))/(T-7.65)));
// calculate the coefficient of molecular diffusion of water vapor in the air
 Dp=(-1.89725e-5+1.5e-7*T)*(101326/P);
// Calculating part of the formula r1 and r2
 a1=pow(r0v,3)*((pv*nv)/(pl*nopt1))+pow(r0l,3);
 a2=pow(r0v,3)*((pv*nv)/(pl*nopt2))+pow(r0l,3);
// calculate the radius of ice particles corresponding to the moment of complete evaporation of droplets for nopt1 and nopt2
 r1=pow(a1, 1.0/3.0);
 r2=pow(a2, 1.0/3.0);
// Calculating part of the formula t1 and t2
 s1=(pow(r1,2)-pow(r0l,2))*pl*Rp*T;
 s2=(pow(r2,2)-pow(r0l,2))*pl*Rp*T;
// Calculating part of the formula t1 and t2
 y=2*Dp*Ev*(1-(El/Ev));
// Calculating the time when all drops will evaporate for nopt1 and nopt2
 t1=s1/y;
 t2=s2/y;
// Sequentially iterate over the values of "i" from 0 to t1
 for(i=0;i<N;i++) {
// Calculate the value of the time when all the drops evaporate by the value "i"
 t=tmin+dt1*i;

// Calculate the radius of the ice particles for the time interval from 0 to t1
 rl=pow(((r0l*r0l)+((2*Dp*Ev)/(pl*Rp*T))*(S-(El/Ev))*t),0.5);
// Calculating part of the rv1 formula
 RR1=(pow(r0v,3)*pv*nv+pl*nopt1*(pow(r0l,3)-pow(rl,3)))/(pv*nv);
// Calculate the radius of water droplets for the same time interval
 rv1=pow(RR1, 1.0/3.0);
// Calculating the range of visibility for the same time points
 L1=b/(nv*pow(rv1,2)+nopt1*pow(rl,2));

 System.out.printf(Locale.US, "%.2e\t%.2e\t%.2f\t%.2f\r\n", rv1, rl, t, L1);

 out1.printf(Locale.US, "%.2e\t%.2e\t%.2f\t%.2f\r\n", rv1, rl, t, L1);
}

// System out.printf(“n_opt2\r\n”);

// Sequentially iterate over the values of "i" from 0 to t2
 for(i=0;i<N;i++) {

 t=tmin+dt2*i;

 rl=pow(((r0l*r0l)+((2*Dp*Ev)/(pl*Rp*T))*(S-(El/Ev))*t),0.5);

 RR2=(pow(r0v,3)*pv*nv+pl*nopt2*(pow(r0l,3)-pow(rl,3)))/(pv*nv);

 rv2=pow(RR2, 1.0/3.0);

 L2=b/(nv*pow(rv2,2)+nopt2*pow(rl,2));

 System.out.printf(Locale.US, "%.2e\t%.2e\t%.2f\t%.2f\r\n", rv2, rl, t, L2);

 out2.printf(Locale.US, "%.2e\t%.2e\t%.2f\t%.2f\r\n", rv2, rl, t, L2);
}

 out1.close();
 out2.close();
}
}

rv2第一列结果示例(Java): the result of the first column of rv2

我尝试用Python重写这段代码(将结果输出到终端而不保存):


import math
N = 438
tmin = 0
dt1 = 47.25
dt2 = 10.22
E0 = 610.78  
S = 1
r0l = 2e-6
r0v = 4e-6
nopt1 = 1.1e+3
nopt2 = 1.1e+4
T = 268.15 
b = 0.62
nv = 1e+9
pl = 917
pv = 1000
Rp = 461
P = 1000000
pi = 3.14

i = 0

Ev = 610.78*pow(10, ((7.63*(T-273.15))/(T-31.25)))
El = 610.78*pow(10, ((9.5*(T-273.15))/(T-7.65)))
Dp = (-1.89725e-5+1.5e-7*T)*(101326/P)
a1 = pow(r0v, 3)*((pv*nv)/(pl*nopt1))+pow(r0l, 3)
a2 = pow(r0v, 3)*((pv*nv)/(pl*nopt2))+pow(r0l, 3)
r1 = pow(a1, 1.0/3.0)
r2 = pow(a2, 1.0/3.0)
s1 = (pow(r1, 2)-pow(r0l, 2))*pl*Rp*T
s2 = (pow(r2, 2)-pow(r0l, 2))*pl*Rp*T
y = 2*Dp*Ev*(1-(El/Ev))
t1 = s1/y
t2 = s2/y

while i < N:

    i += 1
    t = tmin+dt1*i
    rl = pow(((r0l*r0l)+((2*Dp*Ev)/(pl*Rp*T))*(S-(El/Ev))*t), 0.5)
    RR1 = (pow(r0v, 3)*pv*nv+pl*nopt1*(pow(r0l, 3)-pow(rl, 3)))/(pv*nv)
    rv1 = pow(RR1, 1.0/3.0)
    L1 = b/(nv*pow(rv1, 2)+nopt1*pow(rl, 2))
    print(rv1, rl, t, L1)


while i < N:
    i += 1
    t = tmin+dt2*i
    rl2 = pow(((r0l*r0l)+((2*Dp*Ev)/(pl*Rp*T))*(S-(El/Ev))*t), 0.5)
    RR2 = (pow(r0v, 3)*pv*nv+pl*nopt2*(pow(r0l, 3)-pow(rl, 3)))/(pv*nv)
    rv2 = pow(RR2, 1.0/3.0)
    L2 = b/(nv*pow(rv2, 2)+nopt2*pow(rl, 2))
    print(rv2, rl2, t, L2)

我认为While循环有问题,结果输出的步骤错误(?)并且值没有太大变化。 以下是 rv2 第一列的结果示例(Python): the result of the first column of rv2

python java while-loop
1个回答
1
投票

您可以尝试在第二个 while 循环之前再次设置 i=0 吗? 在 Python 中,局部变量的范围仅限于函数,而不是循环,因此 i 以 i=N

开始第二个循环

此外,您应该将“i += 1”移动到循环末尾。

© www.soinside.com 2019 - 2024. All rights reserved.