Skip to content

Commit ae2814e

Browse files
FoadsfFoadsf
authored andcommitted
new fortran C mixed programing examples added and binary files removed
1 parent f833803 commit ae2814e

File tree

105 files changed

+4511
-27230
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

105 files changed

+4511
-27230
lines changed

A_fortran/readme.txt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,15 @@ mixed language programing
55
call fortran functions/subroutines
66
interface the function calls
77

8-
source:
9-
1. http://www.yolinux.com/TUTORIALS/LinuxTutorialMixingFortranAndC.html
108

119

1210
sources to be looked at:
13-
1. https://msdn.microsoft.com/en-us/library/aa293328(v=vs.60).aspx
14-
2. http://docs.cray.com/books/S-2179-52/html-S-2179-52/ppgzmrwh.html
15-
3. http://arnholm.org/software/cppf77/cppf77.htm
11+
1.
12+
2.
13+
3. http://arnholm.org/software/cppf77/cppf77.htm --> test35
1614
4. http://www.nag.com/lapack-ex/node1.html#sec:Introduction
1715
5. http://physics.oregonstate.edu/~landaur/nacphy/lapack/fortran.html
18-
6. http://www.fortran.com/the-fortran-company-homepage/fortran-tools-libraries-and-application-software/
16+
6. http://www.fortran.com/the-fortran-company-homepage/fortran-tools-libraries-and-application-software/ --> Fortran Tools, Libraries, and Application Software
1917
7. iso_c_binding
2018
8. https://docs.oracle.com/cd/E19059-01/stud.9/817-6694/11_cfort.html
2119
9. https://www.math.utah.edu/software/c-with-fortran.html
@@ -27,6 +25,7 @@ sources to be looked at:
2725
15. http://www.unidata.ucar.edu/software/netcdf/examples/programs/
2826
16. http://people.sc.fsu.edu/~jburkardt/c_src/mixed/mixed.html
2927
17. https://docs.oracle.com/cd/E19422-01/819-3685/11_cfort.html
28+
18. cfortran--> http://www-zeus.desy.de/~burow/cfortran/
3029

3130

3231
points:
@@ -38,3 +37,5 @@ issues:
3837
1. test22 result not correct
3938
2. test19 does not compile
4039
3. test24 not compiling
40+
4. test4 not compiling
41+
5. test34 not compiling

A_fortran/test23/tstfunc1.f

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ subroutine tstfunc()
44
common /test/ delta
55
integer i,j
66
do i = 1, 5
7-
do j = 1, 5
7+
do j = 1, 5
88
if (i.ne.j) then
99
delta(i,j)=0
1010
else

A_fortran/test25/Makefile

Lines changed: 0 additions & 8 deletions
This file was deleted.

A_fortran/test25/readme.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

A_fortran/test26/readme.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

A_fortran/test27/readme.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

A_fortran/test28/readme.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

A_fortran/test29/readme.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

A_fortran/test3/CSUBS.C

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/* File CSUBS.C */
2+
3+
#include <math.h>
4+
5+
int Fact(int n) {
6+
if (n > 1)
7+
return (n * Fact(n - 1));
8+
return 1;
9+
}
10+
11+
void Pythagoras(float a, float b, float *c) { *c = sqrt(a * a + b * b); }

A_fortran/test3/FORMAIN.FOR

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
C File FORMAIN.FOR
2+
C
3+
INTERFACE TO INTEGER*4 FUNCTION Fact [C,ALIAS:'_Fact'] (n)
4+
INTEGER*4 n [VALUE]
5+
END
6+
7+
INTERFACE TO SUBROUTINE Pythagoras [C,ALIAS:'_Pythagoras'] (a,b,c)
8+
REAL*4 a [VALUE]
9+
REAL*4 b [VALUE]
10+
REAL*4 c [REFERENCE]
11+
END
12+
13+
INTEGER*4 Fact
14+
REAL*4 c
15+
WRITE (*,*) 'Factorial of 7 is ', Fact (7)
16+
CALL Pythagoras (30, 40, c)
17+
WRITE (*,*) 'Hypotenuse if sides 30, 40 is ', c
18+
END

A_fortran/test3/Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
all:
2+
gfortran -c FORMAIN.FOR
3+
gcc -c CSUBS.C
4+
gfortran -o result.out FORMAIN.o CSUBS.o
5+
rm -rf *.o
6+
7+
8+
clean :
9+
rm -rf *.out *~ *.bak *.o

A_fortran/test3/readme.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
source: https://msdn.microsoft.com/en-us/library/aa279034(v=vs.60).aspx
2+
3+
isues:
4+
not compiling:
5+
6+
7+
FORMAIN.FOR:3:26:
8+
9+
INTERFACE TO INTEGER*4 FUNCTION Fact [C,ALIAS:'_Fact'] (n)
10+
1
11+
Error: Syntax error: Trailing garbage in INTERFACE statement at (1)
12+
FORMAIN.FOR:4:19:
13+
14+
INTEGER*4 n [VALUE]
15+
1
16+
Fatal Error: Coarrays disabled at (1), use '-fcoarray=' to enable

A_fortran/test30/readme.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

A_fortran/test31/readme.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

A_fortran/test33/cpps.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
#include <cstring>
22
#include <iostream>
3+
34
using namespace std;
5+
46
extern "C" {
57
extern struct {
68
double x;
79
int a, b, c;
810
char s10[10];
911
} abc_;
12+
1013
void cpps_() {
1114
cout << "cpps1: begin" << endl;
1215
abc_.x = 90023.876;

A_fortran/test33/readme.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
http://www.dtic.mil/dtic/tr/fulltext/u2/a567758.pdf
2+
3+
4+
the fortran code calls a C++ function which changes some variables

A_fortran/test34/ftnfctn.f

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
C Fortran subprogram (ftnfctn.f):
2+
3+
4+
FUNCTION FTNFCTN(STR, LOG)
5+
6+
REAL FTNFCTN
7+
CHARACTER*(*) STR
8+
LOGICAL LOG
9+
10+
COMMON /FLOAT1/FLOAT1
11+
COMMON /FLOAT2/FLOAT2
12+
REAL FLOAT1, FLOAT2
13+
DATA FLOAT2/2.4/ ! FLOAT1 INITIALIZED IN MAIN
14+
15+
C PRINT CURRENT STATE OF VARIABLES
16+
PRINT*, ' IN FTNFCTN: FLOAT1 = ', FLOAT1,
17+
1 ';FLOAT2 = ', FLOAT2
18+
PRINT*, ' ARGUMENTS: STR = "', STR, '"; LOG = ', LOG
19+
20+
C CHANGE THE VALUES FOR STR(ING) AND LOG(ICAL)
21+
STR = 'New Fortran String'
22+
LOG = .FALSE.
23+
24+
FTNFCTN = 123.4
25+
PRINT*, ' RETURNING FROM FTNFCTN WITH ', FTNFCTN
26+
PRINT*
27+
RETURN
28+
END

A_fortran/test34/main.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/* C program (main.c): */
2+
3+
// #include <fortran.h>
4+
#include <stdio.h>
5+
#include <string.h>
6+
7+
/* Declare prototype of the Fortran function. Note the last */
8+
/* argument passes the length of the first argument. */
9+
double FTNFCTN_(char *, int *, int);
10+
11+
double FLOAT1 = 1.6;
12+
double FLOAT2; /* Initialized in FTNFCTN */
13+
14+
int main() {
15+
int clogical, ftnlogical, cstringlen;
16+
double rtnval;
17+
char *cstring = "C Character String";
18+
19+
/* Convert clogical to its Fortran equivalent */
20+
clogical = 1;
21+
ftnlogical = _btol(clogical);
22+
23+
/* Print values of variables before call to Fortran function */
24+
printf(" In main: FLOAT1 = %g; FLOAT2 = %g\n", FLOAT1, FLOAT2);
25+
printf(" Calling FTNFCTN with arguments:\n");
26+
printf(" string = \"%s\"; logical = %d\n\n", cstring, clogical);
27+
cstringlen = strlen(cstring);
28+
rtnval = FTNFCTN_(cstring, &ftnlogical, cstringlen);
29+
30+
/* Convert ftnlogical to its C equivalent */
31+
clogical = _ltob(&ftnlogical);
32+
33+
/* Print values of variables after call to Fortran function */
34+
printf(" Back in main: FTNFCTN returned %g\n", rtnval);
35+
printf(" and changed the two arguments:\n");
36+
printf(" string = \"%.*s\"; logical = %d\n", cstringlen, cstring, clogical);
37+
38+
return 0;
39+
}

A_fortran/test34/readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
source: http://docs.cray.com/books/S-2179-52/html-S-2179-52/ppgzmrwh.html

A_fortran/test35/f77char.h

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#include <string.h>
2+
3+
/*
4+
class CHARACTER
5+
===============
6+
A minimal class used when passing string arguments from C++
7+
to FORTRAN 77 (received as FORTRAN 77 CHARACTER strings), and
8+
subsequently returned back to C++ as properly zero terminated
9+
strings.
10+
11+
Method used for zero-termination:
12+
=================================
13+
When the CHARACTER destructor is activated the zero-termination
14+
of the c-string is automatically managed. Zero termination is
15+
also done each time a string array is subscripted using
16+
CHARACTER::operator()(size_t index)
17+
18+
FORTRAN Assumptions:
19+
====================
20+
(1) F77 truncates strings when CHARACTER variable is short
21+
(2) F77 pads variable with blanks when assigned string is short
22+
(3) F77 represents a string as a pointer followed by a length
23+
(4) A string array is stored in contiguous memory
24+
25+
Author: Carsten A. Arnholm, 20-AUG-1995
26+
27+
Updates:
28+
04-MAR-1996 Added features for handling arrays of strings
29+
16-MAR-1996 Tested array features, explicit padding included
30+
29-JUL-1996 Tested portability to SGI/Unix, moved decl. of destructor
31+
04-APR-1997 Using strncpy instead of strcpy in operator=(char* str);
32+
*/
33+
34+
class CHARACTER {
35+
public:
36+
CHARACTER(char *cstring);
37+
CHARACTER(char *cstring, const size_t lstr);
38+
~CHARACTER();
39+
CHARACTER operator()(size_t index);
40+
void pad(size_t first, size_t howmany = 1);
41+
void operator=(char *str);
42+
operator char *();
43+
44+
public:
45+
char *rep; // Actual string
46+
size_t len; // String length
47+
};
48+
49+
inline CHARACTER::CHARACTER(char *cstring)
50+
: rep(cstring), len(strlen(cstring)){};
51+
52+
inline CHARACTER::CHARACTER(char *cstring, const size_t lstr)
53+
: rep(cstring), len(lstr) {
54+
// find position from where to start padding
55+
size_t slen = strlen(rep); // upper limit
56+
size_t actual = (slen < len) ? slen : len; // actual <= len.
57+
for (size_t i = actual; i < len; i++)
58+
rep[i] = ' '; // Do the padding.
59+
}
60+
61+
inline CHARACTER::~CHARACTER() {
62+
if (rep[len] == '\0')
63+
return; // catches string constants
64+
65+
for (int i = len - 1; i >= 0; i--) {
66+
if (rep[i] == '\0')
67+
break; // already zero terminated
68+
69+
if (rep[i] != ' ') { // non-blank discovered, so
70+
rep[i + 1] = '\0'; // zero-terminate and jump out
71+
break;
72+
}
73+
}
74+
}
75+
76+
inline CHARACTER CHARACTER::operator()(size_t index) {
77+
// Construct a temporary CHARACTER object for the array element
78+
// identified by "index" in order to zero-terminate that element
79+
size_t pos = index * len; // start pos of array element
80+
CHARACTER element(rep + pos, len); // construct new CHARACTER.
81+
return element; // destructor called here.
82+
}
83+
84+
inline void CHARACTER::pad(size_t first, size_t howmany) {
85+
86+
size_t pos = 0, i = 0, stop = first + howmany - 1;
87+
for (size_t index = first; index <= stop; index++) {
88+
pos = index * len;
89+
size_t slen = strlen(rep + pos); // upper limit
90+
size_t actual = (slen < len) ? slen : len;
91+
for (i = pos + actual; i < pos + len; i++)
92+
rep[i] = ' '; // Do the padding.
93+
}
94+
}
95+
96+
inline void CHARACTER::operator=(char *str) {
97+
strncpy(rep, str, len); // this will copy a zero if str < rep
98+
rep[len - 1] = '\0'; // zero terminate in case strncpy did not
99+
size_t slen = strlen(rep); // upper limit
100+
size_t actual = (slen < len) ? slen : len; // actual <= len.
101+
for (size_t i = actual; i < len; i++)
102+
rep[i] = ' '; // Do the padding.
103+
}
104+
105+
inline CHARACTER::operator char *() { return rep; }

A_fortran/test35/f77cmplx.h

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
class COMPLEX
3+
=============
4+
A minimal class used when passing complex arithmetic variables
5+
from C++ to FORTRAN 77.
6+
7+
The template parameter is used for specification of precision:
8+
9+
COMPLEX<float> is equivalent to F77 COMPLEX
10+
COMPLEX<double> is equivalent to F77 DOUBLE COMPLEX
11+
12+
Author: Carsten A. Arnholm,
13+
Updates:
14+
04-MAR-1996 initial, non-template version
15+
14-MAY-1996 Template version
16+
29-JUL-1996 Tested portability to SGI/Unix,
17+
corrected operator=(const COMPLEX<T>& )
18+
*/
19+
20+
#ifdef real
21+
// some people define real as a macro
22+
#undef real
23+
#pragma message(__FILE__ " : warning: 'real' macro definition cancelled")
24+
#endif
25+
26+
template <class T> class COMPLEX {
27+
public:
28+
COMPLEX();
29+
COMPLEX(const COMPLEX<T> &);
30+
COMPLEX(const T &re, const T &im);
31+
COMPLEX<T> &operator=(const COMPLEX<T> &);
32+
~COMPLEX();
33+
const T &real();
34+
const T &imag();
35+
36+
private:
37+
T m_re;
38+
T m_im;
39+
};
40+
41+
template <class T> inline COMPLEX<T>::COMPLEX() : m_re(T()), m_im(T()) {}
42+
43+
template <class T>
44+
inline COMPLEX<T>::COMPLEX(const COMPLEX<T> &copy)
45+
: m_re(copy.m_re), m_im(copy.m_im) {}
46+
47+
template <class T>
48+
inline COMPLEX<T>::COMPLEX(const T &re, const T &im) : m_re(re), m_im(im) {}
49+
50+
template <class T>
51+
inline COMPLEX<T> &COMPLEX<T>::operator=(const COMPLEX<T> &copy) {
52+
m_re = copy.m_re;
53+
m_im = copy.m_im;
54+
return *this;
55+
}
56+
57+
template <class T> inline COMPLEX<T>::~COMPLEX() {}
58+
59+
template <class T> inline const T &COMPLEX<T>::real() { return m_re; }
60+
61+
template <class T> inline const T &COMPLEX<T>::imag() { return m_im; }

0 commit comments

Comments
 (0)