Re: Reading complex multidimensionalarrays from C
- From: "Wade Ward" <invalid@xxxxxxxxxxx>
- Date: Sat, 28 Apr 2007 23:38:29 -0400
"e p chandler" <epc8@xxxxxxxx> wrote in message
news:1177783128.657920.82030@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
On Apr 28, 12:55 pm, t...@xxxxxxxxxxxxxxxxxx wrote:I'm still back here, having now just received an opinion from C. Gotta run,
Hi,
I defined a multidimensional Array (with complex values) in Fortran:
PROGRAM Hello
complex, dimension(2,2) :: A
A(1,1) = cmplx(3, 2)
A(1,2) = cmplx(3, 7)
A(2,1) = cmplx(9, 9)
A(2,2) = cmplx(2, 7)
CALL test(A,2,2)
You have an argument mismatch here. You are passing a pointer to the
start of A, then two integers. Your routine expects a pointer to an
array of two doubles. The type mismatch is corrected below.
The proper syntax is:
CALL test(A(2,2))
END
I tried to read values with the folowing C function:
#include <stdio.h>
typedef struct {
double dr;
double di;
The components of a Fortran COMPLEX are two variables of type default
REAL. In Fortran this is single precision - which is FLOAT in C. You
are not guaranteed that a particular compiler supports what might be
declared as (non-standard) DOUBLE COMPLEX or COMPLEX*16 or a type of
complex using kinds).
float dr;
float di;
} fcomplex;
int test_ (fcomplex *fc, int *n, int *m) {
printf("YAY: %f + %fi\n", fc->dr, fc->di);
return(0);
}
I compiled it like this:
gfortran -c -g testF2.f
You have posted free form source but gfortran expects fixed format
with a file extension of .f:
gfortran -c -g -ffree-form testF2.f
gcc -c -g testC.c
On my system I need to add a path to the include files. You may not
need this:
gcc -c -g -I\gfortran\include testC.c
gfortran -g -o test testC.o testF2.o
OK
It compiles well, but when I execute ./test I get:
YAY: 2.000000 + 262144.063538i
YAY: 2.000000 + 7.000000i
So could anyone please give me some hints how to do it right?
See comments interspersed with your code and output.
Regards,
Timo
You have fallen into several traps common in mixed language
programming. You have two different sources of argument mismatch here.
First, your incorrect syntax gives you a mismatch in the NUMBER of
arguments. Second, the TYPES of your arguments do not correspond. In
Fortran you are using single precision, but in C you are using double.
Worse, your compilers will not catch this type of error. If you had
included an explicit interface (somewhat analagous to a function
prototype in C) for your C routine in Fortram, you might have caught
the syntax error, but not the type misatch error.
though. Does the fortran part need to be built as a .dll?
http://www.billfordx.net/2007-04-28b.htm
--
WW
.
- Follow-Ups:
- Re: Reading complex multidimensionalarrays from C
- From: e p chandler
- Re: Reading complex multidimensionalarrays from C
- References:
- Reading complex multidimensionalarrays from C
- From: timos
- Re: Reading complex multidimensionalarrays from C
- From: e p chandler
- Reading complex multidimensionalarrays from C
- Prev by Date: Re: utility to replace "end" with "end subroutine foo"
- Next by Date: Re: Reading complex multidimensionalarrays from C
- Previous by thread: Re: Reading complex multidimensionalarrays from C
- Next by thread: Re: Reading complex multidimensionalarrays from C
- Index(es):
Relevant Pages
|
|