Re: Reading complex multidimensionalarrays from C



On Apr 28, 11:38 pm, "Wade Ward" <inva...@xxxxxxxxxxx> wrote:
"e p chandler" <e...@xxxxxxxx> wrote in messagenews:1177783128.657920.82030@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx



On Apr 28, 12:55 pm, t...@xxxxxxxxxxxxxxxxxx wrote:
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.

I'm still back here, having now just received an opinion from C. Gotta run,
though. Does the fortran part need to be built as a .dll?http://www.billfordx.net/2007-04-28b.htm
--
WW

Yes. The OP needs a Fortran DLL. I've done similar stuff interfacing
Visual Basic / VBA and Fortran for various GUI builders and Fortrans.
If you want to try this yourself, get and install the MinGW version of
gfortran. This includes C.

see: http://quatramaran.ens.fr/~coudert/gfortran/gfortran-windows.exe

Run the program and install in a convenient directory, say \gfortran.
Look back in this thread to see the commands I used to compile the
source files and create the .exe file.

-- Elliot


.



Relevant Pages

  • Re: Reading complex multidimensionalarrays from C
    ... You are passing a pointer to the ... The type mismatch is corrected below. ... In Fortran this is single precision - which is FLOAT in C. ... prototype in C) for your C routine in Fortram, ...
    (comp.lang.fortran)
  • Re: Reading complex multidimensionalarrays from C
    ... You have two different sources of argument mismatch here. ... Fortran you are using single precision, but in C you are using double. ... prototype in C) for your C routine in Fortram, ... Run the program and install in a convenient directory, ...
    (comp.lang.fortran)
  • Re: Reading complex multidimensionalarrays from C
    ... I defined a multidimensional Array in Fortran: ... The type mismatch is corrected below. ... are not guaranteed that a particular compiler supports what might be ...
    (comp.lang.fortran)
  • Re: Reading complex multidimensionalarrays from C
    ... I defined a multidimensional Array in Fortran: ... The type mismatch is corrected below. ... are not guaranteed that a particular compiler supports what might be ...
    (comp.lang.fortran)
  • installing fruit
    ... I would call ruby the next ... FRUIT is a unit testing utility written for FORTRAN. ... Question 1) Where "should" I install it? ... What results are maybe 15 files in 6 folders and additional ...
    (comp.lang.fortran)