将代码从 FORTRAN 转换为 C

发布于 2024-08-25 22:23:42 字数 600 浏览 5 评论 0原文

我有以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ζ澈沫 2024-09-01 22:23:42

这将帮助您开始 - 我没有尝试编译它,但它接近您将需要的内容。我假设数组 olon、u、v、un 和 vn 作为指针传递给您的函数。

const double rotcon_p   =  0.422618;
const double lon_xx_p   = -95.0;
const double lat_tan_p  =  25.0;

for (j=0;j<ny_p;++j)
{
  for (i=0,i<nx_p;++i)
  {
    double angle2 = rotcon_p*(olon[i][j]-lon_xx_p)*0.017453;
    double sinx2 = sin(angle2);
    double cosx2 = cos(angle2);
    for (k=0;k<nsp_p;++k)
    {
      double ut = u[i][j][k]
      double vt = v[i][j][k]
      un[i][j][k] = cosx2*ut+sinx2*vt
      vn[i][j][k] =-sinx2*ut+cosx2*vt
   }
  }
}

如果你完全停留在 c/c++ 中,这会很好,如果你混合使用 FORTRAN 和 c/c++,你需要知道 FORTRAN 和 c/c++ 向后索引它们的数组,所以你可能必须将索引交换为让它发挥作用

const double rotcon_p   =  0.422618;
const double lon_xx_p   = -95.0;
const double lat_tan_p  =  25.0;

for (j=0;j<ny_p;++j)
{
  for (i=0,i<nx_p;++i)
  {
    double angle2 = rotcon_p*(olon[j][i]-lon_xx_p)*0.017453;
    double sinx2 = sin(angle2);
    double cosx2 = cos(angle2);
    for (k=0;k<nsp_p;++k)
    {
      double ut = u[k][j][i]
      double vt = v[k][j][i]
      un[k][j][i] = cosx2*ut+sinx2*vt
      vn[k][j][i] =-sinx2*ut+cosx2*vt
   }
  }
}

但是我没有足够的背景来解决你的问题来告诉你你需要做什么。

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.

const double rotcon_p   =  0.422618;
const double lon_xx_p   = -95.0;
const double lat_tan_p  =  25.0;

for (j=0;j<ny_p;++j)
{
  for (i=0,i<nx_p;++i)
  {
    double angle2 = rotcon_p*(olon[i][j]-lon_xx_p)*0.017453;
    double sinx2 = sin(angle2);
    double cosx2 = cos(angle2);
    for (k=0;k<nsp_p;++k)
    {
      double ut = u[i][j][k]
      double vt = v[i][j][k]
      un[i][j][k] = cosx2*ut+sinx2*vt
      vn[i][j][k] =-sinx2*ut+cosx2*vt
   }
  }
}

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

const double rotcon_p   =  0.422618;
const double lon_xx_p   = -95.0;
const double lat_tan_p  =  25.0;

for (j=0;j<ny_p;++j)
{
  for (i=0,i<nx_p;++i)
  {
    double angle2 = rotcon_p*(olon[j][i]-lon_xx_p)*0.017453;
    double sinx2 = sin(angle2);
    double cosx2 = cos(angle2);
    for (k=0;k<nsp_p;++k)
    {
      double ut = u[k][j][i]
      double vt = v[k][j][i]
      un[k][j][i] = cosx2*ut+sinx2*vt
      vn[k][j][i] =-sinx2*ut+cosx2*vt
   }
  }
}

But I don't have enough context for your problem to tell you which you need to do.

━╋う一瞬間旳綻放 2024-09-01 22:23:42

我说 Fortran,就像 Tarzan 说英语一样,但这应该是 C 语言的要点:

#include <math.h>

const double ROTCON_P = 0.422618;
const double LON_XX_P = -95.0;
const double LAT_TAN_P = 25.0;

int i, j, k;
double angle2, sinx2, cosx2, ut, vt;
double un[nzp_p][ny_p][nx_p];
double vn[nzp_p][ny_p][nx_p];

for (j=0; j<ny_p; ++j) {
    for (i=0; i<nx_p; ++i) {
        angle2 = ROTCON_P * (olon[j][i] - LON_XX_P) * 0.017453;
        sinx2 = sin(angle2);
        cosx2 = cos(angle2);
        for (k=0; k<nzp_p; ++k) {
            ut = u[k][j][i];
            vt = v[k][j][i];
            un[k][j][i] = (cosx2 * ut) + (sinx2 * vt);
            vn[k][j][i] = (-1 * sinx2 * ut) + (cosx2 * vt);
        }
    }
}

您需要声明 olonuv、nx_pny_pnzp_p 某处,并在运行此代码之前为它们分配一个值。没有提供足够的上下文信息让我确切地知道它们是什么。

I speak Fortran as well as Tarzan speaks English, but this should be the gist of it in C:

#include <math.h>

const double ROTCON_P = 0.422618;
const double LON_XX_P = -95.0;
const double LAT_TAN_P = 25.0;

int i, j, k;
double angle2, sinx2, cosx2, ut, vt;
double un[nzp_p][ny_p][nx_p];
double vn[nzp_p][ny_p][nx_p];

for (j=0; j<ny_p; ++j) {
    for (i=0; i<nx_p; ++i) {
        angle2 = ROTCON_P * (olon[j][i] - LON_XX_P) * 0.017453;
        sinx2 = sin(angle2);
        cosx2 = cos(angle2);
        for (k=0; k<nzp_p; ++k) {
            ut = u[k][j][i];
            vt = v[k][j][i];
            un[k][j][i] = (cosx2 * ut) + (sinx2 * vt);
            vn[k][j][i] = (-1 * sinx2 * ut) + (cosx2 * vt);
        }
    }
}

You will need to declare olon, u, v, nx_p, ny_p, and nzp_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.

迷爱 2024-09-01 22:23:42

这是一段代码,这可能就是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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文