在Fortran和C之间交换数组

时间:2017-08-23 08:29:21

标签: c fortran fortran-iso-c-binding

我有以下C和Fortran代码,我想交换一些数据

FUNCTION exchange_data(data) bind(c,name='exchange_data')
    use iso_c_binding
    integer(kind=c_int)                                 :: exchange_data
    real(kind=c_double), intent(inout), dimension(*)    :: data

END FUNCTION exchange_data

....

WRITE(*,*), "Sent data to C"
DO I=1,NumBl
    DO J=1,WindSpeedCoordNr
        WRITE(*, FMT2), GLOBAL_COORD_ALONG_BEAM(I, J, :)
    END DO
END DO
cflag = exchange_data(GLOBAL_COORD_ALONG_BEAM)

WRITE(*,*), "Received data from C"
DO I=1,NumBl
    DO J=1,WindSpeedCoordNr
        WRITE(*, FMT2), GLOBAL_COORD_ALONG_BEAM(I, J, :)
    END DO
END DO

以下测试C代码:

int exchange_data(double* positions)
{

    printf("Received data from Fortran");
    bladepositions = positions;
    for (int i = 0; i < numbld; i++) {
        for (int j = 0; j < datapointnr; j++) {
            printf("[");
            for (int k = 0; k < 3; k++) {

                printf("%5.4f ", bladepositions[3 * datapointnr * k + 3 * j + i]);
                windspeedalongblade[3 * datapointnr * k + 3 * j + i] = 1.0;
            }
            printf("]\r\n");
        }
    }


    positions = windspeedalongblade;

    printf("Data will be send from C");
    for (int i = 0; i < numbld; i++) {
        for (int j = 0; j < datapointnr; j++) {
            printf("[");
            for (int k = 0; k < 3; k++) {
                printf("%5.4f ", positions[3 * datapointnr * k + 3 * j + i]);
            }
            printf("]\r\n");
        }
    }
    return 0;
}

这有以下输出

Sent data to C
  -18.6593  -29.1175  137.0735
  -18.8588  -29.1308  137.0803
  -19.0582  -29.1441  137.0871

Received data from Fortran
[-18.6593 -29.1175 137.0735 ]
[-18.8588 -29.1308 137.0803 ]
[-19.0582 -29.1441 137.0871 ]

Data will be send from C
[1.0000 1.0000 1.0000 ]
[1.0000 1.0000 1.0000 ]
[1.0000 1.0000 1.0000 ]

Received data from C
  -18.6593  -29.1175  137.0735
  -18.8588  -29.1308  137.0803
  -19.0582  -29.1441  137.0871

我似乎可以将数据传输到C函数但不能传回Fortran代码。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:4)

问题是以下一行:

windspeedalongblade

不会永久性地将positions分配给int exchange_data(double** positions) { ... *positions = windspeedalongblade; printf("Data will be send from C"); for (int i = 0; i < numbld; i++) { for (int j = 0; j < datapointnr; j++) { printf("["); for (int k = 0; k < 3; k++) { printf("%5.4f ", *positions[3 * datapointnr * k + 3 * j + i]); } printf("]\r\n"); } } return 0; } (请参阅here传递值和参考值之间的差异。)

要做到这一点,你需要将位置作为指向数组的指针传递:

windspeedalongblade

但在这种情况下,您必须确保positions保持持久,直到您使用positions

更简单的解决方案是保持函数不变并直接指定int exchange_data(double* positions) { printf("Received data from Fortran"); bladepositions = positions; for (int i = 0; i < numbld; i++) { for (int j = 0; j < datapointnr; j++) { printf("["); for (int k = 0; k < 3; k++) { printf("%5.4f ", bladepositions[3 * datapointnr * k + 3 * j + i]); windspeedalongblade[3 * datapointnr * k + 3 * j + i] = positions[3 * datapointnr * k + 3 * j + i] = 1.0; } printf("]\r\n"); } } printf("Data will be send from C"); for (int i = 0; i < numbld; i++) { for (int j = 0; j < datapointnr; j++) { printf("["); for (int k = 0; k < 3; k++) { printf("%5.4f ", positions[3 * datapointnr * k + 3 * j + i]); } printf("]\r\n"); } } return 0; } 数组的值:

positions

所以最后它取决于你是希望@{Html.RenderAction("PartialView", "PartialViewController");} 是一个数组还是只是一个指向数组的指针。从Fortran代码的外观来看,它似乎是一个数组,在这种情况下,第二个解决方案将是最好的。