我试图从C ++调用Fortran子例程。作为参考,我在Fortran中有一个测试程序调用相同的子程序,该程序为程序提供了正确的值。我的Fortran知识非常有限,所以我无法弄清楚为什么我会获得垃圾值。
Fortran代码如下:
program test_9j
implicit none
integer :: ll1, ll2, ll3, ll4, ll5, ll6, ll7, ll8, llmin, llmax, ndim, ier, i
real(8) :: L1, L2, L3, L4, L5, L6, L7, L8, LMIN, LMAX
real(8), allocatable, dimension(:) :: ninecof
ll1 = 2
ll2 = 2
ll3 = 2
ll4 = 2
ll5 = 2
ll6 = 2
ll7 = 2
ll8 = 2
llmin = 0
llmax = 6
print*, llmin, llmax, llmax-llmin+1
print*, ""
L1 = real(ll1,kind=8)
L2 = real(ll2,kind=8)
L3 = real(ll3,kind=8)
L4 = real(ll4,kind=8)
L5 = real(ll5,kind=8)
L6 = real(ll6,kind=8)
L7 = real(ll7,kind=8)
L8 = real(ll8,kind=8)
LMIN = real(llmin,kind=8)
LMAX = real(llmax,kind=8)
ndim = llmax - llmin + 1
ALLOCATE(ninecof(ndim),stat=ier)
! print*, ier
ninecof = 0.0
print*, "main Hi1"
! print*, "LL", LMIN, LMAX
CALL w9j(ll1,ll2,ll3,ll4,ll5,ll6,ll7,ll8,llmin,llmax,ndim,ninecof)
! print*, "main Hi2"
do i=1,ndim
print*, ninecof(i)
end do
print*, "main Hi3a"
!DEALLOCATE(ninecof)
print*, "main Hi3b"
end program test_9j
这为子程序提供了一系列正确的输出。但是,当我在C ++中尝试这个时,它不起作用:(注意:w9j子例程调用drc6j子例程,这就是我将它包含在extern定义中的原因.drc6j子例程给出了正确的输出。)
using namespace std;
extern "C"
{
double drc6j_ (double*, double*, double*, double*, double*,
double*, double*, double*, int*, int*);
double w9j_ (int*, int*, int*, int*, int*,
int*, int*, int*, int*, int*, int*, double*);
}
int main()
{
int N = 1000;
double* coef9j;
int L1, L2, L3, L4, L5, L6, L7, L8, L9, Lmin=0, Lmax=6;
coef9j = new double [ N ];
L1 = 2, L2 = 2, L3 = 2, L4 = 2, L5 = 2, L6 = 2, L7 = 2, L8 = 2;
w9j_ ( &L2, &L3, &L4, &L5, &L6, &L7, &L8, &L9, &Lmin, &Lmax, &N, coef9j );
cout<<" Lmin, Lmax, ierr = "<<Lmin<<","<<Lmax<<","<<ier<<endl;
for (int i=0;i<Lmax-Lmin+1;i++)
cout<<"coeff = "<<setprecision(7)<<coef9j[i]<<endl;
return 0;
}
这个程序给出Lmin和Lmax的值,如E-315等,这是不可能的,因为我应该得到整数值,而且Lmax也不是零值。在早期的子程序(如drc6j)中,Lmin和Lmax的值由子程序决定,当我通过引用传递时,我使用了这些值。此外,无论输入Li值如何,它始终将coeff =0
作为输出。
请在评论中提及您是否需要drc6j子例程和我用来评估它的C ++代码。我会相应地修改问题。
我得到的C ++代码输出是:
AA 2 2 2 2 2 2 2 256 0 6 1000
L9min, L9max, ierr = 0,6,0
coeff = 0
coeff = 0
coeff = 0
coeff = 0
coeff = 0
coeff = 0
对于Fortran代码:
0 6 7
main Hi1
-8.5714285714285719E-003
1.7347234759768071E-018
1.6734693877551020E-002
1.7347234759768071E-018
1.3877551020408163E-002
0.0000000000000000
0.0000000000000000
main Hi3a
main Hi3b
这是子程序w9j代码:
subroutine w9j(l1,l2,l3,l4,l5,l6,l7,l8,lmin,lmax,ndim,cof9j)
implicit none
!--- in/out variables
integer, intent(in) :: l1, l2, l3, l4, l5, l6, l7, l8, lmin, lmax, ndim
real(8), dimension(ndim), intent(out) :: cof9j
!--- program variables
integer :: l1min, l1max, l2min, l2max, l3min, l3max
integer :: i, j, k, jmin, jmax, ier, dim1, dim2, dim3
real(8) :: y
real(8), allocatable, dimension(:) :: sixj1, sixj2, sixj3
real(8) :: rl1, rl2, rl3, rl4, rl5, rl6, rl7, rl8, ri
real(8) :: rl1min, rl1max, rl2min, rl2max, rl3min, rl3max
!--- --- ---
print*, 'AA', l1,l2,l3,l4,l5,l6,l7,l8,lmin,lmax,ndim
cof9j = 0.0
do k=1,ndim
i=k+lmin-1
! print*, 'i', i
! print*, ""
if((l2+i-l1 < 0) .or. (l2-i+l1 < 0) .or. (-l2+i+l1 < 0) .or. &
& (l2+l5-l8 < 0) .or. (l2-l5+l8 < 0) .or. (-l2+l5+l8 < 0) .or. &
& (l4+l3-l5 < 0) .or. (l4-l3+l5 < 0) .or. (-l4+l3+l5 < 0) .or. &
& (l4+l1-l7 < 0) .or. (l4-l1+l7 < 0) .or. (-l4+l1+l7 < 0) .or. &
& (l6+i-l3 < 0) .or. (l6-i+l3 < 0) .or. (-l6+i+l3 < 0) .or. &
& (l6+l7-l8 < 0) .or. (l6-l7+l8 < 0) .or. (-l6+l7+l8 < 0)) then
! print*, "violation of triangularity"
cof9j(k) = 0.0
cycle
end if
l1min=max(abs(i-l8),abs(l5-l1))
l1max=min(i+l8,l5+l1)
dim1 = l1max -l1min + 1
! print*, 'l1', l1min,l1max,dim1
! print*, ""
l2min=max(abs(l3-l7),abs(l1-l5))
l2max=min(l3+l7,l1+l5)
dim2 = l2max -l2min + 1
! print*, 'l2', l2min,l2max,dim2
! print*, ""
l3min=max(abs(i-l8),abs(l7-l3))
l3max=min(i+l8,l7+l3)
dim3 = l3max -l3min + 1
! print*, 'l3', l3min,l3max,dim3
! print*, ""
if (l1max < l1min .or. l2max < l2min .or. l3max < l3min) then
cof9j(k) = 0.0
cycle
end if
allocate(sixj1(dim1))
allocate(sixj2(dim2))
allocate(sixj3(dim3))
sixj1 = 0.0
sixj2 = 0.0
sixj3 = 0.0
ri = real(i) ! L1
rl1 = real(l1) ! L2
rl2 = real(l2) ! L3
rl3 = real(l3) ! L4
rl4 = real(l4) ! L5
rl5 = real(l5) ! L6
rl6 = real(l6) ! L7
rl7 = real(l7) ! L8
rl8 = real(l8) ! L9
rl1min = 0.0
rl1max = 0.0
rl2min = 0.0
rl2max = 0.0
rl2min = 0.0
rl3max = 0.0
! SUBROUTINE DRC6J (L2, L3, L4, L5, L6, L1MIN, L1MAX, SIXCOF, NDIM, IER)
! L1MAX=MIN(L2+L3,L5+L6) and L1MIN=MAX(ABS(L2-L3),ABS(L5-L6)).
! print*, "Hi1"
! print*, 'BB',ri,rl1,rl2,rl3,rl4,rl5,rl6,rl7,rl8,dim1,dim2,dim3
! L2 L3 L4 L5 L6
call DRC6J(ri,rl8,rl2,rl5,rl1,rl1min,rl1max,sixj1,dim1,ier)
! print*, "6j - 1"
! L2 L3 L4 L5 L6
call DRC6J(rl3,rl7,rl4,rl1,rl5,rl2min,rl2max,sixj2,dim2,ier)
! print*, "6j - 2"
! L2 L3 L4 L5 L6
call DRC6J(ri,rl8,rl6,rl7,rl3,rl3min,rl3max,sixj3,dim3,ier)
! print*, "6j - 3"
! print*, "Hi2"
if (l1min /= int(rl1min) .or. l1max /= int(rl1max) .or. &
& l2min /= int(rl2min) .or. l2max /= int(rl2max) .or. &
& l3min /= int(rl3min) .or. l3max /= int(rl3max)) then
print*, "Declared dimensions of 'sixj' arrays are wrong"
end if
jmin = max(l1min,l2min,l3min)
jmax = min(l1max,l2max,l3max)
print*, 'Jmin Jmax', jmin, jmax
y = 0.0
if(jmin <= jmax) then
do j=jmin,jmax
y = y + (2.0*j+1.0)*sixj1(j-l1min+1)*sixj2(j-l2min+1)*sixj3(j-l3min+1)
end do
end if
cof9j(k) = y
deallocate(sixj1,sixj2,sixj3)
end do
print*, 'end values', l1,l2,l3,l4,l5,l6,l7,l8,lmin,lmax,ndim
return
end subroutine w9j
drc6j.f和依赖项的子程序可以在netlib.org上找到
答案 0 :(得分:2)
w9j
中的任何一点都没有设置lmin
和lmax
并且在c ++代码中没有设置这些变量,因此当您在c ++代码中打印它们时只是在这些变量的位置打印内存中的任何值 - 即它是未定义的。在您的原始fortran代码中,您可以在将这些变量传递给w9j
之前设置它们,这就是为什么它似乎有效。
请注意,您已在Lmin
中将Lmax
和intent(in)
标记为w9j
,但您尝试在c ++代码中将其打印出来,就好像它们是输出一样w9j
。标记为intent(in)
的参数不应在该例程中修改。
更新以下评论
最后一个修复是要注意到w9j
的c ++调用没有通过l1
,但你传递的l9
没有设置为任何东西,导致使用主w9j
例程中的未定义值。