| 你需要在合适的时机,释放 pointchain 数组。 |
| 不需要转换。使用的时候注意顺序即可。 |
kyra 发表于 2017-7-14 15:54 真的是这样啊。。。数据都对,好厉害。 我这个程序其实是一个中间程序,是一个划分网格的程序,也就是说这里面的所有数据都需要导出来 这样看来我需要再做一个函数进行转化一下对吗 顺便问一下这个数据转换有相关的资料吗 我想学习一下 |
kyra 发表于 2017-7-14 08:12 这里最后一行代码,假如想输出 [Fortran] 查看源码 复制源码 module hydromodelUtil
use iso_c_binding
implicit none
double precision, parameter :: pi=3.141593d0
!这部分为输入值
type ,Bind(C)::unitinfo
integer(c_int) :: controlno
character(c_char) :: unitname
integer(c_int) :: unittype
integer(c_int) :: solid
real (c_double):: coearea
real(c_double) ,dimension(3) :: shapepara
integer(c_int):: pn=0
double precision, dimension(:,:), allocatable :: plist
real(c_double), dimension(3) :: rotationpara, tranpara
end type unitinfo
!这部分为输出值
type ,Bind(C):: singleinfo
integer(c_int) :: controlno
integer(c_int) :: unittype
integer(c_int) :: solid
real(c_double) :: coearea
real(c_double):: volume
real(c_double), dimension(3) :: center
integer (c_int):: pointcount, trianglecount
double precision, dimension(:,:), allocatable :: pointchain
double precision, dimension(:,:,:), allocatable :: trianglechain
end type singleinfo
contains
subroutine cylindermeshing(r, h, cylinderinfo) Bind(C,Name="cylindermeshing")
!DEC$ ATTRIBUTES DLLEXPORT::cylindermeshing
implicit none
real(c_double) :: r, h
type(singleinfo) :: cylinderinfo
integer(c_int) :: polyedge=60
double precision, dimension(:,:), allocatable :: point
real(c_double) :: dtheta
integer(c_int) :: meshi
dtheta=2*pi/real(polyedge)
cylinderinfo%volume=(0.5d0*polyedge*sin(dtheta)*r**2)*h
cylinderinfo%center=(/ 0.0d0, 0.0d0, 0.5d0*h /)
cylinderinfo%pointcount=2*polyedge
cylinderinfo%trianglecount=4*polyedge
allocate(point(2,polyedge+1) )
allocate(cylinderinfo%pointchain(3,cylinderinfo%pointcount) )
do meshi=1, polyedge, 1
point(1,meshi)=r*cos( (real(meshi)-1.0d0)*dtheta)
point(2,meshi)=r*sin( (real(meshi)-1.0d0)*dtheta)
end do
point(:, polyedge+1)=point(:, 1)
do meshi=1, polyedge, 1
cylinderinfo%pointchain(1:2,meshi)=point(1:2,meshi)
cylinderinfo%pointchain(3,meshi)=0.0d0
cylinderinfo%pointchain(1:2,polyedge+meshi)=point(1:2,meshi)
cylinderinfo%pointchain(3,polyedge+meshi)=h
end do
end subroutine cylindermeshing
end module hydromodelUtil
|
|
本帖最后由 kyra 于 2017-7-14 08:20 编辑 问题1,无需额外处理。 问题2,不能在type里存放fortran语言的动态分配数组。否则C语言无法用标准化的手段访问。解决办法是,用 C 指针,fortran分配后把地址赋给 C 指针。具体看注释 (这方法适合于fortran的动态分配返回给 C,并不适合 C 分配的数组传递给 fortran) [Fortran] 查看源码 复制源码 real(c_double),dimension(:,:),allocatable,target,save::point [C++] 查看源码 复制源码 cout<<cylinderinfo.pointchain[2][6]<<endl; |
|
在 Fortran 语言里。pointchain 大小是 1:3 * 1:cylinderinfo%pointcount 则 C 语言中正好相反,所以应该视为 0:cylinderinfo%pointcount-1 * 0:2 因此 fortran 中 pointchain(2,6) = 381 ,则 C 中应该输出 cout << cylinderinfo.pointchain[ (6-1)*3+(2-1) ] |
捐赠本站|Archiver|关于我们 About Us|小黑屋|Fcode ( 京ICP备18005632-2号 )
GMT+8, 2025-12-5 10:58