N
NM
Hello all,
I am supposed to do some mixed programming with c++ and fortran.
I was succeeful in exchanging the 2D arrays from fortran to c++ and the
other way, but was unable
to that same with the 3D arrays, the values passed are not all the
same. I am also pasting the fortran and c++ codes so that you could
have a look at them.
////C++ Code
#include <math.h>
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
extern "C" {
void setup_(int *, int *, int *, int *, int *);
void getsetup_( float *,float *);
}
int main()
{
float **testarr,***arr3d;
int rowfor,colfor,i,j,k=1;
int three,two,one,inc;
setup_(&rowfor,&colfor,&three,&two,&one);
// 2D array dynamical allocation
testarr = new float*[colfor];
for (i=0; i < colfor; i++)
testarr= new float[rowfor];
//3d array dynamic allocation
//cout<<"\nThree = "<<three<<"\tTwo = "<<two<<"\tOne= "<<one;
arr3d = new float**[three];
for (i=0;i<three;i++)
arr3d=new float*[two];
for(i=0;i<three;i++)
for(j=0;j<two;j++)
arr3d[j]=new float[one];
// Assigning values to the 2D array of c++
inc=1;
for(i=0;i<colfor;i++)
for(j=0;j<rowfor;j++)
testarr[j]=inc++;
// Assigning values to the 3D array of c++
inc=1;
for(i=0;i<one;i++)
for(j=0;j<two;j++)
for(k=0;k<three;k++)
arr3d[k][j]=inc++;
//Printing the 2D array in c++ before getsetup
cout<<"\nC++ 2D Array IN C++ BEFORE GETSETUP----------";
for(i=0;i<colfor;i++)
{
cout<<endl;
for(j=0;j<rowfor;j++)
cout<<" "<<testarr[j];
}
//Printing the 3D array in c++ before getsetup
cout<<"\nC++ 3D Array IN C++ BEFORE GETSETUP----------";
for(i=0;i<three;i++)
{
cout<<"\n";
for(j=0;j<two;j++)
{
cout<<"\n";
for(k=0;k<one;k++)
cout<<" "<<arr3d[j][k];
}
}
getsetup_(*testarr,&arr3d[0][0][0]);
//Printing the 2D array in c++ after getsetup
cout<<"\n C++ 2D Array IN C++ AFTER GETSETUP ----------";
for(i=0;i<colfor;i++)
{
cout<<endl;
for(j=0;j<rowfor;j++)
cout<<" "<<testarr[j];
}
//Printing the 3D array in c++ after getsetup
cout<<"\nC++ 3D Array IN C++ BEFORE GETSETUP----------";
for(i=0;i<three;i++)
{
cout<<"\n";
for(j=0;j<two;j++)
{
cout<<"\n";
for(k=0;k<one;k++)
cout<<" "<<arr3d[j][k];
}
}
cout<<"\n";
return 0;
}
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Fortran Code
module chek
integer rowfor
integer colfor,onefor,twofor,threefor
real*8, allocatable:: arrfor
,
real*8, allocatable:: arrfor3d
,:,
end module chek
subroutine setup(row, col, three, two, one)
use chek
integer row,col,i,j,k,three,two,one
k=1
rowfor = 3
colfor= 10
three = 4
two = 3
one = 2
allocate(arrfor(rowfor,colfor),stat=ierr)
allocate(arrfor3d(one,two,three),stat=ierr)
! Assinging values to 2D array of Fortran
do i=1,rowfor
do j=1,colfor
arrfor(i,j)= 222
end do
end do
! Assinging values to 3D array of Fortran
do i=1,one
do j=1,two
do k=1,three
arrfor3d(i,j,k)= 333
end do
end do
end do
onefor = one
twofor = two
threefor = three
col=colfor
row=rowfor
end subroutine
subroutine getsetup(arr,arr3d)
use chek
real arr(0:rowfor,0:colfor)
real arr3d(0
nefor,0:twofor,0:threefor)
integer i,j,k
print *,''
print *,'@@@@@ C++ 2D array IN Fortran i.e IN GETSETUP @@@@@@@@@@@@@@'
do i=0,rowfor-1
print *, ' ',(arr(i,j),j=0,colfor-1)
end do
print*,'@@@@@c++ 3D array in fortran in Getsetup@@@@@@@@@@@@@ '
do i=0,onefor-1
do j=0,twofor-1
print *, ' ',(arr3d(i,j,k),k=0,threefor-1)
enddo
enddo
! Assinging values for the 2D array of c++ with 2d array of fortran
do i=0, rowfor-1
do j=0, colfor-1
arr(i,j)=arrfor(i+1,j+1)
end do
end do
! Assinging values to 3D array of c++ withe the 3D array of fortran
do i=1,onefor
do j=1,twofor
do k=1,threefor
arr3d(i-1,j-1,k-1)=arrfor3d(i,j,k)
enddo
enddo
enddo
end subroutine
////////////////////The Output!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
C++ 2D Array IN C++ BEFORE GETSETUP----------
1 2 3
4 5 6
7 8 9
10 11 12
13 14 15
16 17 18
19 20 21
22 23 24
25 26 27
28 29 30
C++ 3D Array IN C++ BEFORE GETSETUP----------
1 13
5 17
9 21
2 14
6 18
10 22
3 15
7 19
11 23
4 16
8 20
@@@@@ C++ 2D array IN Fortran i.e IN GETSETUP @@@@@@@@@@@@@@
1.000000 4.000000 7.000000 10.00000 13.00000 16.00000 19.00000
22.00000 25.00000 28.00000
2.000000 5.000000 8.000000 11.00000 14.00000 17.00000 20.00000
23.00000 26.00000 29.00000
3.000000 6.000000 9.000000 12.00000 15.00000 18.00000 21.00000
24.00000 27.00000 30.00000
@@@@@c++ 3D array in fortran in Getsetup@@@@@@@@@@@@@
1.000000 2.000000 3.000000 4.000000
0.0000000 0.0000000 0.0000000 0.0000000
0.0000000 0.0000000 0.0000000 0.0000000
13.00000 14.00000 15.00000 16.00000
5.000000 6.000000 7.000000 8.000000
0.0000000 0.0000000 0.0000000 0.0000000
12 24
C++ 2D Array IN C++ AFTER GETSETUP ----------
222 222 222
222 222 222
222 222 222
222 222 222
222 222 222
222 222 222
222 222 222
222 222 222
222 222 222
222 222 222
C++ 3D Array IN C++ BEFORE GETSETUP----------
333 333
333 17
9 21
333 333
333 18
10 22
333 333
333 19
11 23
333 333
333 20
12 24
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!////////////////////////////////////////
It can be observed that the 2D arrays are working fine, i.e the changes
done in c++ are reflected in fortran and vice versa, but it doesnt seem
to happen with 3D, some values are not changed as expected. I hope I
have made my point clear.
Any suggestions would be of great help
Thanks in advance
Regards,
NM
I am supposed to do some mixed programming with c++ and fortran.
I was succeeful in exchanging the 2D arrays from fortran to c++ and the
other way, but was unable
to that same with the 3D arrays, the values passed are not all the
same. I am also pasting the fortran and c++ codes so that you could
have a look at them.
////C++ Code
#include <math.h>
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
extern "C" {
void setup_(int *, int *, int *, int *, int *);
void getsetup_( float *,float *);
}
int main()
{
float **testarr,***arr3d;
int rowfor,colfor,i,j,k=1;
int three,two,one,inc;
setup_(&rowfor,&colfor,&three,&two,&one);
// 2D array dynamical allocation
testarr = new float*[colfor];
for (i=0; i < colfor; i++)
testarr= new float[rowfor];
//3d array dynamic allocation
//cout<<"\nThree = "<<three<<"\tTwo = "<<two<<"\tOne= "<<one;
arr3d = new float**[three];
for (i=0;i<three;i++)
arr3d=new float*[two];
for(i=0;i<three;i++)
for(j=0;j<two;j++)
arr3d[j]=new float[one];
// Assigning values to the 2D array of c++
inc=1;
for(i=0;i<colfor;i++)
for(j=0;j<rowfor;j++)
testarr[j]=inc++;
// Assigning values to the 3D array of c++
inc=1;
for(i=0;i<one;i++)
for(j=0;j<two;j++)
for(k=0;k<three;k++)
arr3d[k][j]=inc++;
//Printing the 2D array in c++ before getsetup
cout<<"\nC++ 2D Array IN C++ BEFORE GETSETUP----------";
for(i=0;i<colfor;i++)
{
cout<<endl;
for(j=0;j<rowfor;j++)
cout<<" "<<testarr[j];
}
//Printing the 3D array in c++ before getsetup
cout<<"\nC++ 3D Array IN C++ BEFORE GETSETUP----------";
for(i=0;i<three;i++)
{
cout<<"\n";
for(j=0;j<two;j++)
{
cout<<"\n";
for(k=0;k<one;k++)
cout<<" "<<arr3d[j][k];
}
}
getsetup_(*testarr,&arr3d[0][0][0]);
//Printing the 2D array in c++ after getsetup
cout<<"\n C++ 2D Array IN C++ AFTER GETSETUP ----------";
for(i=0;i<colfor;i++)
{
cout<<endl;
for(j=0;j<rowfor;j++)
cout<<" "<<testarr[j];
}
//Printing the 3D array in c++ after getsetup
cout<<"\nC++ 3D Array IN C++ BEFORE GETSETUP----------";
for(i=0;i<three;i++)
{
cout<<"\n";
for(j=0;j<two;j++)
{
cout<<"\n";
for(k=0;k<one;k++)
cout<<" "<<arr3d[j][k];
}
}
cout<<"\n";
return 0;
}
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Fortran Code
module chek
integer rowfor
integer colfor,onefor,twofor,threefor
real*8, allocatable:: arrfor
real*8, allocatable:: arrfor3d
end module chek
subroutine setup(row, col, three, two, one)
use chek
integer row,col,i,j,k,three,two,one
k=1
rowfor = 3
colfor= 10
three = 4
two = 3
one = 2
allocate(arrfor(rowfor,colfor),stat=ierr)
allocate(arrfor3d(one,two,three),stat=ierr)
! Assinging values to 2D array of Fortran
do i=1,rowfor
do j=1,colfor
arrfor(i,j)= 222
end do
end do
! Assinging values to 3D array of Fortran
do i=1,one
do j=1,two
do k=1,three
arrfor3d(i,j,k)= 333
end do
end do
end do
onefor = one
twofor = two
threefor = three
col=colfor
row=rowfor
end subroutine
subroutine getsetup(arr,arr3d)
use chek
real arr(0:rowfor,0:colfor)
real arr3d(0
integer i,j,k
print *,''
print *,'@@@@@ C++ 2D array IN Fortran i.e IN GETSETUP @@@@@@@@@@@@@@'
do i=0,rowfor-1
print *, ' ',(arr(i,j),j=0,colfor-1)
end do
print*,'@@@@@c++ 3D array in fortran in Getsetup@@@@@@@@@@@@@ '
do i=0,onefor-1
do j=0,twofor-1
print *, ' ',(arr3d(i,j,k),k=0,threefor-1)
enddo
enddo
! Assinging values for the 2D array of c++ with 2d array of fortran
do i=0, rowfor-1
do j=0, colfor-1
arr(i,j)=arrfor(i+1,j+1)
end do
end do
! Assinging values to 3D array of c++ withe the 3D array of fortran
do i=1,onefor
do j=1,twofor
do k=1,threefor
arr3d(i-1,j-1,k-1)=arrfor3d(i,j,k)
enddo
enddo
enddo
end subroutine
////////////////////The Output!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
C++ 2D Array IN C++ BEFORE GETSETUP----------
1 2 3
4 5 6
7 8 9
10 11 12
13 14 15
16 17 18
19 20 21
22 23 24
25 26 27
28 29 30
C++ 3D Array IN C++ BEFORE GETSETUP----------
1 13
5 17
9 21
2 14
6 18
10 22
3 15
7 19
11 23
4 16
8 20
@@@@@ C++ 2D array IN Fortran i.e IN GETSETUP @@@@@@@@@@@@@@
1.000000 4.000000 7.000000 10.00000 13.00000 16.00000 19.00000
22.00000 25.00000 28.00000
2.000000 5.000000 8.000000 11.00000 14.00000 17.00000 20.00000
23.00000 26.00000 29.00000
3.000000 6.000000 9.000000 12.00000 15.00000 18.00000 21.00000
24.00000 27.00000 30.00000
@@@@@c++ 3D array in fortran in Getsetup@@@@@@@@@@@@@
1.000000 2.000000 3.000000 4.000000
0.0000000 0.0000000 0.0000000 0.0000000
0.0000000 0.0000000 0.0000000 0.0000000
13.00000 14.00000 15.00000 16.00000
5.000000 6.000000 7.000000 8.000000
0.0000000 0.0000000 0.0000000 0.0000000
12 24
C++ 2D Array IN C++ AFTER GETSETUP ----------
222 222 222
222 222 222
222 222 222
222 222 222
222 222 222
222 222 222
222 222 222
222 222 222
222 222 222
222 222 222
C++ 3D Array IN C++ BEFORE GETSETUP----------
333 333
333 17
9 21
333 333
333 18
10 22
333 333
333 19
11 23
333 333
333 20
12 24
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!////////////////////////////////////////
It can be observed that the 2D arrays are working fine, i.e the changes
done in c++ are reflected in fortran and vice versa, but it doesnt seem
to happen with 3D, some values are not changed as expected. I hope I
have made my point clear.
Any suggestions would be of great help
Thanks in advance
Regards,
NM