[英]Need to convert following FORTRAN code to C++
我是一个非常糟糕的程序员,并且给了我一个程序,可以帮助我进行空气动力学方面的研究。 但它在fortran中,并且我试图使用MATLAB运行该程序。 将其转换为Matlab可以理解的语言有什么帮助吗? (最好是c ++)
program joukow
c
c computes joukowski airfoil and finds pressure coefficient
c currently set up for symmetric airfoil with sharp trailing edge
c and chord length equal to one.
c profile is written onto prof.dat and cp onto cp.dat
c implicit real*8(a-h,o-z)
complex z,zeta,cw
dimension uz(100),vz(100),xi(100),eta(100),cp(100)
dimension xout(100),yout(100)
open(unit=8,file='prof.dat',status='unknown')
open(unit=9,file='cp.dat',status='unknown')
b=1.d0
write(6,98)
format(2x,'input the radius of the a-circle in z plane')
read(5,99)a
format(f10.0)
xl=2.*a-1.+1./(2.*a-1.)
c xl=a+1./a
c chord=2.*xl
chord=2.+xl
del=a-b
c del =0.1d0
do 50 i=1,100
ri=i
theta=6.2832d0*ri/101.d0
x=-del+a*cos(theta)
y=a*sin(theta)
z=cmplx(x,y)
zeta=z+b**2/z
c
c xi and eta are coordinates of points on airfoil
c
xi(i)=real(zeta)
eta(i)=aimag(zeta)
cw=(1.-a**2/(z+del)**2)/(1.-b**2/z**2)
c
c uz and vz are velocity components on the airfoil assuming the free-stream
c speed is one.
c
uz(i)=real(cw)
vz(i)=-aimag(cw)
c
c xout and yout are airfoil coordinates where the leading edge is at (0,0)
c and the chordlength is one.
c
xout(i)=(xl+xi(i))/chord
yout(i)=eta(i)/chord
write(8,100)xout(i),yout(i)
format(2x,2f10.4)
continue
c
c now calculate the pressure coefficient cp
c
write(6,200)
format(2x,'pressure coefficients')
do 70 i=1,50
cp(i)=1.-(uz(i)**2+vz(i)**2)
write(9,100)xout(i),cp(i)
continue
stop
end
Matlab理解Fortran很好-检查文档。 而且,如果这不满足您的要求,则只需进行很少的修改即可将程序中执行任何计算的大多数行键入到Matlab控制台中。 如果您是一个贫穷的程序员,我建议您花时间将程序修改为Matlab而不是C ++。 如果您没有比我现在有时间的更好的帮助,我会在以后再写更多。
编辑:首先,关于从Matlab 使用Fortran源文件的一些信息。 如果您真的不想(或出于性能原因或不这样做的话)将Fortran重写为Matlab,然后将其转换为MEX文件。 对我来说,使用f2c(或其他任何方式,包括您自己的时间和精力)将Fortran转换为C或C ++似乎毫无意义。
如果您不喜欢该想法,这里有一些将Fortran转换为Matlab的想法。
首先,所有以C或c开头的行都是注释,因此您无需翻译它们。 从您的代码开始:
complex z,zeta,cw
dimension uz(100),vz(100),xi(100),eta(100),cp(100)
dimension xout(100),yout(100)
这些行声明了许多变量。 您不必在Matlab中使用变量之前就声明变量,但是有时有充分的理由这么做。 您也不必在Fortran中使用,尽管如今普遍认为这是个坏主意。 您可以使用以下语句在Matlab中“声明”这些变量:
uz = zeros(100,1);
vz = zeros(100,1);
通过在Matlab中预先声明它们,可以为它们分配一次内存,并避免一些降低性能的问题。
接下来的2行:
open(unit=8,file='prof.dat',status='unknown')
open(unit=9,file='cp.dat',status='unknown')
打开几个文件进行输出。 它们稍后将在write
语句中使用-忘记它们,而改写Matlab语句,例如save xout
。
下一行是Fortran,但在Matlab中相同:
b=1.d0
接下来的几行从控制台获取半径值:
write(6,98)
format(2x,'input the radius of the a-circle in z plane')
read(5,99)a
format(f10.0)
再次,我建议你忘记了这些,只需要使用Matlab的控制台设置的值a
。 更多不需要翻译的Fortran(尽管我建议您要么舍弃小数点后不跟0,要么在它们之间加上空格,随后的*-。*是Matlab中的特定运算符):
xl=2.*a-1.+1./(2.*a-1.)
chord=2.+xl
del=a-b
Fortran do循环与Matlab for循环相同。 改写:
do 50 i=1,100
如
for i = 1:100
正如其他受访者中的一位指出的那样,匹配的结束语句不明确,您必须弄清楚这一点。 请注意,我只是提供Fortran到Matlab的逐行翻译。 它不是写得很好的Fortran,而且我也没有提供写得很好的Matlab,我会留给您。
这很多不需要翻译:
ri=i
theta=6.2832d0*ri/101.d0
x=-del+a*cos(theta)
y=a*sin(theta)
cmplx是一个Fortran函数,它返回一个复数,该复数具有实部x和虚部y:
z=cmplx(x,y)
在Matlab中,这将是z = x + y * i。 Fortran使用**进行幂运算,Matlab使用^
zeta=z+b**2/z
等等等等。
希望能有所帮助。
我使用了f2matlab,然后稍加修饰。 这是经过整理和编译的fortran90代码:
program joukow
!
! computes joukowski airfoil and finds pressure coefficient
! currently set up for symmetric airfoil with sharp trailing edge
! and chord length equal to one.
! profile is written onto prof.dat and cp onto cp.dat
! implicit real*8(a-h,o-z)
complex z,zeta,cw
dimension uz(100),vz(100),xi(100),eta(100),cp(100)
dimension xout(100),yout(100)
open(unit=8,file='prof.dat',status='unknown')
open(unit=9,file='cp.dat',status='unknown')
b=1.d0
write(6,98)
98 format(2x,'input the radius of the a-circle in z plane')
read(5,99)a
99 format(f10.0)
xl=2.*a-1.+1./(2.*a-1.)
! xl=a+1./a
! chord=2.*xl
chord=2.+xl
del=a-b
! del =0.1d0
do i=1,100
ri=i
theta=6.2832d0*ri/101.d0
x=-del+a*cos(theta)
y=a*sin(theta)
z=cmplx(x,y)
zeta=z+b**2/z
!
! xi and eta are coordinates of points on airfoil
!
xi(i)=real(zeta)
eta(i)=aimag(zeta)
cw=(1.-a**2/(z+del)**2)/(1.-b**2/z**2)
!
! uz and vz are velocity components on the airfoil assuming the free-stream
! speed is one.
!
uz(i)=real(cw)
vz(i)=-aimag(cw)
!
! xout and yout are airfoil coordinates where the leading edge is at (0,0)
! and the chordlength is one.
!
xout(i)=(xl+xi(i))/chord
yout(i)=eta(i)/chord
write(8,100)xout(i),yout(i)
100 format(2x,2f10.4)
end do
!
! now calculate the pressure coefficient cp
!
write(6,200)
200 format(2x,'pressure coefficients')
do i=1,50
cp(i)=1.-(uz(i)**2+vz(i)**2)
write(9,100) xout(i),cp(i)
end do
stop
end program joukow
这是生成的matlab代码:
function hw1(varargin)
%
% computes joukowski airfoil and finds pressure coefficient
% currently set up for symmetric airfoil with sharp trailing edge
% and chord length equal to one.
% profile is written onto prof.dat and cp onto cp.dat
% implicit real*8(a-h,o-z)
format_99=['%10.0f'];
format_100=[repmat(' ',1,2),repmat('%10.4f',1,2),'\n'];
format_200=[repmat(' ',1,2),'pressure coefficients \n'];
fid_8=fopen('prof.dat','w+');
fid_9=fopen('cp.dat','w+');
b=1.0d0;
a=input('input the radius of the a-circle in z plane');
xl=2..*a-1.+1../(2..*a-1.);
% xl=a+1./a
% chord=2.*xl
chord=2.+xl;
del=a-b;
% del =0.1d0
for i=1:100;
ri=i;
theta=6.2832d0.*ri./101.0d0;
x=-del+a.*cos(theta);
y=a.*sin(theta);
z=complex(x,y);
zeta=z+b.^2./z;
%
% xi and eta are coordinates of points on airfoil
%
xi(i)=real(zeta);
eta(i)=imag(zeta);
cw=(1.-a.^2./(z+del).^2)./(1.-b.^2./z.^2);
%
% uz and vz are velocity components on the airfoil assuming the free-stream
% speed is one.
%
uz(i)=real(cw);
vz(i)=-imag(cw);
%
% xout and yout are airfoil coordinates where the leading edge is at (0,0)
% and the chordlength is one.
%
xout(i)=(xl+xi(i))./chord;
yout(i)=eta(i)./chord;
fprintf(fid_8,format_100,xout(i),yout(i));
end; i=100+1;
%
% now calculate the pressure coefficient cp
%
fprintf(1,format_200);
for i=1:50;
cp(i)=1.-(uz(i).^2+vz(i).^2);
fprintf(fid_9,format_100, xout(i),cp(i));
end; i=50+1;
end %program joukow
他们都给我相同的结果。 我没有检查算法的正确性,只是转换了代码。
我不知道它的支持程度如何--但是以前最简单的方法是f2c,它可以将fortran直接转换为c代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.