Exchanging array between Fortran and C -
i have following c , fortran code exchange data
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 c" i=1,numbl j=1,windspeedcoordnr write(*, fmt2), global_coord_along_beam(i, j, :) end end cflag = exchange_data(global_coord_along_beam) write(*,*), "received data c" i=1,numbl j=1,windspeedcoordnr write(*, fmt2), global_coord_along_beam(i, j, :) end end
and following test c code:
int exchange_data(double* positions) { printf("received data fortran"); bladepositions = positions; (int = 0; < numbld; i++) { (int j = 0; j < datapointnr; j++) { printf("["); (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 send c"); (int = 0; < numbld; i++) { (int j = 0; j < datapointnr; j++) { printf("["); (int k = 0; k < 3; k++) { printf("%5.4f ", positions[3 * datapointnr * k + 3 * j + i]); } printf("]\r\n"); } } return 0; }
this has following output
sent data c -18.6593 -29.1175 137.0735 -18.8588 -29.1308 137.0803 -19.0582 -29.1441 137.0871 received data fortran [-18.6593 -29.1175 137.0735 ] [-18.8588 -29.1308 137.0803 ] [-19.0582 -29.1441 137.0871 ] data send c [1.0000 1.0000 1.0000 ] [1.0000 1.0000 1.0000 ] [1.0000 1.0000 1.0000 ] received data c -18.6593 -29.1175 137.0735 -18.8588 -29.1308 137.0803 -19.0582 -29.1441 137.0871
i seems can transfer data c function not fortran code. how can achieve that?
the problem following line:
positions = windspeedalongblade;
doesn't permanently assign windspeedalongblade
positions
(see here difference between passing value , reference).
to need positions passed pointer array:
int exchange_data(double** positions) { ... *positions = windspeedalongblade; printf("data send c"); (int = 0; < numbld; i++) { (int j = 0; j < datapointnr; j++) { printf("["); (int k = 0; k < 3; k++) { printf("%5.4f ", *positions[3 * datapointnr * k + 3 * j + i]); } printf("]\r\n"); } } return 0; }
but in case have sure windspeedalongblade
remains persistent until use positions
.
the simpler solution leave function , assign values of positions
array directly:
int exchange_data(double* positions) { printf("received data fortran"); bladepositions = positions; (int = 0; < numbld; i++) { (int j = 0; j < datapointnr; j++) { printf("["); (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 send c"); (int = 0; < numbld; i++) { (int j = 0; j < datapointnr; j++) { printf("["); (int k = 0; k < 3; k++) { printf("%5.4f ", positions[3 * datapointnr * k + 3 * j + i]); } printf("]\r\n"); } } return 0; }
so in end depends whether want positions
array or pointer array. looks of fortran code seems array, in case second solution best.
wiki
Comments
Post a Comment