Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

""" 

Fortran compiler 

""" 

from __future__ import absolute_import 

from six import iteritems 

 

import os 

import imp 

import shutil 

import sys 

 

from sage.misc.temporary_file import tmp_dir 

 

 

class InlineFortran: 

def __init__(self, globals=None): 

# globals=None means: use user globals from REPL 

self.globs = globals 

self.library_paths=[] 

self.libraries=[] 

self.verbose = False 

 

def __repr__(self): 

return "Interface to Fortran compiler" 

 

def __call__(self, *args, **kwds): 

return self.eval(*args, **kwds) 

 

def eval(self, x, globals=None, locals=None): 

""" 

Compile fortran code ``x`` and adds the functions in it to 

``globals``. 

 

INPUT: 

 

- ``x`` -- Fortran code 

 

- ``globals`` -- a dict to which to add the functions from the 

fortran module 

 

- ``locals`` -- ignored 

 

EXAMPLES:: 

 

sage: code = ''' 

....: C FILE: FIB1.F 

....: SUBROUTINE FIB(A,N) 

....: C 

....: C CALCULATE FIRST N FIBONACCI NUMBERS 

....: C 

....: INTEGER N 

....: REAL*8 A(N) 

....: DO I=1,N 

....: IF (I.EQ.1) THEN 

....: A(I) = 0.0D0 

....: ELSEIF (I.EQ.2) THEN 

....: A(I) = 1.0D0 

....: ELSE 

....: A(I) = A(I-1) + A(I-2) 

....: ENDIF 

....: ENDDO 

....: END 

....: C END FILE FIB1.F 

....: ''' 

sage: fortran(code, globals()) 

sage: import numpy 

sage: a = numpy.array(range(10), dtype=float) 

sage: fib(a, 10) 

sage: a 

array([ 0., 1., 1., 2., 3., 5., 8., 13., 21., 34.]) 

 

TESTS:: 

 

sage: os.chdir(SAGE_ROOT) 

sage: fortran.eval("SYNTAX ERROR !@#$") 

Traceback (most recent call last): 

... 

RuntimeError: failed to compile Fortran code:... 

sage: os.getcwd() == SAGE_ROOT 

True 

""" 

if globals is None: 

globals = self.globs 

if globals is None: 

from sage.repl.user_globals import get_globals 

globals = get_globals() 

 

from numpy import f2py 

 

# Create everything in a temporary directory 

mytmpdir = tmp_dir() 

 

try: 

old_cwd = os.getcwd() 

os.chdir(mytmpdir) 

 

name = "fortran_module" # Python module name 

# if the first line has !f90 as a comment, gfortran will 

# treat it as Fortran 90 code 

if x.startswith('!f90'): 

fortran_file = name + '.f90' 

else: 

fortran_file = name + '.f' 

 

s_lib_path = "" 

s_lib = "" 

for s in self.library_paths: 

s_lib_path = s_lib_path + "-L%s " 

 

for s in self.libraries: 

s_lib = s_lib + "-l%s "%s 

 

log = name + ".log" 

extra_args = '--quiet --f77exec=sage-inline-fortran --f90exec=sage-inline-fortran %s %s >"%s" 2>&1'%( 

s_lib_path, s_lib, log) 

 

f2py.compile(x, name, extra_args = extra_args, source_fn=fortran_file) 

log_string = open(log).read() 

 

# Note that f2py() doesn't raise an exception if it fails. 

# In that case, the import below will fail. 

try: 

file, pathname, description = imp.find_module(name, [mytmpdir]) 

except ImportError: 

raise RuntimeError("failed to compile Fortran code:\n" + log_string) 

try: 

m = imp.load_module(name, file, pathname, description) 

finally: 

file.close() 

 

if self.verbose: 

print(log_string) 

finally: 

os.chdir(old_cwd) 

 

if sys.platform != 'cygwin': 

# Do not delete temporary DLLs on Cygwin; this will cause 

# future forks of this process to fail. Instead temporary DLLs 

# will be cleaned up upon process exit 

try: 

shutil.rmtree(mytmpdir) 

except OSError: 

# This can fail for example over NFS 

pass 

 

for k, x in iteritems(m.__dict__): 

if k[0] != '_': 

globals[k] = x 

 

def add_library(self,s): 

self.libraries.append(s) 

 

def add_library_path(self,s): 

self.library_paths.append(s) 

 

# An instance 

fortran = InlineFortran()