将代码从 FORTRAN 转换为 C
我有以下 FORTRAN 代码,需要将其转换为 C 或 C++。我已经尝试过使用 f2c,但没有成功。它与从兰伯特等形风矢量到正北方向矢量的转换有关。 有没有在 FORTRAN 方面有经验的人可以提供帮助?
PARAMETER ( ROTCON_P = 0.422618 )
PARAMETER ( LON_XX_P = -95.0 )
PARAMETER ( LAT_TAN_P = 25.0 )
do j=1,ny_p
do i=1,nx_p
angle2 = rotcon_p*(olon(i,j)-lon_xx_p)*0.017453
sinx2 = sin(angle2)
cosx2 = cos(angle2)
do k=1,nzp_p
ut = u(i,j,k)
vt = v(i,j,k)
un(i,j,k) = cosx2*ut+sinx2*vt
vn(i,j,k) =-sinx2*ut+cosx2*vt
end if
end do
end do
非常感谢您的任何帮助或提示。
I have the following FORTRAN code which I need to convert to C or C++. I already tried using f2c, but it didn't work out. It has something to do with conversion from Lambert Conformal wind vector to a True-North oriented vector.
Is anyone experienced in FORTRAN who could possibly help?
PARAMETER ( ROTCON_P = 0.422618 )
PARAMETER ( LON_XX_P = -95.0 )
PARAMETER ( LAT_TAN_P = 25.0 )
do j=1,ny_p
do i=1,nx_p
angle2 = rotcon_p*(olon(i,j)-lon_xx_p)*0.017453
sinx2 = sin(angle2)
cosx2 = cos(angle2)
do k=1,nzp_p
ut = u(i,j,k)
vt = v(i,j,k)
un(i,j,k) = cosx2*ut+sinx2*vt
vn(i,j,k) =-sinx2*ut+cosx2*vt
end if
end do
end do
Thanks a lot for any help or tip.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这将帮助您开始 - 我没有尝试编译它,但它接近您将需要的内容。我假设数组 olon、u、v、un 和 vn 作为指针传递给您的函数。
如果你完全停留在 c/c++ 中,这会很好,如果你混合使用 FORTRAN 和 c/c++,你需要知道 FORTRAN 和 c/c++ 向后索引它们的数组,所以你可能必须将索引交换为让它发挥作用
但是我没有足够的背景来解决你的问题来告诉你你需要做什么。
This will get you started - I didn't try to compile it, but it's close to what you're going to need. I assumed that the arrays olon, u, v, un, and vn are passed in to your function as pointers.
If you're staying completely in c/c++ this will be fine, if you're mixing FORTRAN and c/c++, you need to know that FORTRAN and c/c++ index their arrays backwards, so you may have to swap your indices to make it work
But I don't have enough context for your problem to tell you which you need to do.
我说 Fortran,就像 Tarzan 说英语一样,但这应该是 C 语言的要点:
您需要声明
olon
、u
、v、
nx_p
、ny_p
和nzp_p
某处,并在运行此代码之前为它们分配一个值。没有提供足够的上下文信息让我确切地知道它们是什么。I speak Fortran as well as Tarzan speaks English, but this should be the gist of it in C:
You will need to declare
olon
,u
,v
,nx_p
,ny_p
, andnzp_p
somewhere and assign them a value before running this code. There is not enough context info given for me to know exactly what they are.这是一段代码,这可能就是f2c不起作用的原因。另外,正如已经指出的,“结束如果”很可能应该是“结束做”。
如果您有经过测试并执行所需计算的 Fortran 子例程,则可以从 C 调用它们。您使用 Fortran 的 ISO C 绑定声明 Fortran 子例程的参数,然后 Fortran 编译器将使用 C API,以便该例程可从 C 调用。这个简短的代码块很容易翻译;又长又复杂的东西可能更适合重用。
This is a fragment of code, which may be why f2c didn't work. Plus, as already pointed out, most likely the "end if" should be "end do".
If you have Fortran subroutines that are tested and do the calculation that you need, you can call them from C. You declare the arguments of the Fortran subroutine using the ISO C Binding of Fortran, then the Fortran compiler will use the C API so that the routine is callable from C. This short code block is easy to translate; something long and complicated might be better to reuse.