Skip to content

Commit b4d6039

Browse files
author
daniel
committed
Change f90wrap generated variable names to enable use of n1, n2 variable names
1 parent 5fc2dcf commit b4d6039

File tree

8 files changed

+106
-6
lines changed

8 files changed

+106
-6
lines changed

examples/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ list(APPEND tests
2828
kind_map_default
2929
docstring
3030
return_array
31+
intent_out_size
3132
)
3233

3334
foreach(test ${tests})
@@ -38,5 +39,3 @@ foreach(test ${tests})
3839
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}/${test}"
3940
)
4041
endforeach()
41-
42-

examples/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ EXAMPLES = arrayderivedtypes \
2626
return_array \
2727
optional_string \
2828
long_subroutine_name \
29-
kind_map_default
29+
kind_map_default \
30+
intent_out_size
3031

3132
PYTHON = python
3233

examples/intent_out_size/Makefile

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#=======================================================================
2+
# define the compiler names
3+
#=======================================================================
4+
5+
CC = gcc
6+
F90 = gfortran
7+
PYTHON = python
8+
CFLAGS = -fPIC
9+
F90FLAGS = -fPIC
10+
PY_MOD = pywrapper
11+
F90_SRC = main.f90
12+
OBJ = $(F90_SRC:.f90=.o)
13+
F90WRAP_SRC = $(addprefix f90wrap_,${F90_SRC})
14+
WRAPFLAGS = -v
15+
F2PYFLAGS = --build-dir build
16+
F90WRAP = f90wrap
17+
F2PY = f2py-f90wrap
18+
.PHONY: all clean
19+
20+
all: test
21+
22+
clean:
23+
rm -rf *.mod *.smod *.o f90wrap*.f90 ${PY_MOD}.py _${PY_MOD}*.so __pycache__/ .f2py_f2cmap build ${PY_MOD}/
24+
25+
main.o: ${F90_SRC}
26+
${F90} ${F90FLAGS} -c $< -o $@
27+
28+
%.o: %.f90
29+
${F90} ${F90FLAGS} -c $< -o $@
30+
31+
${F90WRAP_SRC}: ${OBJ}
32+
${F90WRAP} -m ${PY_MOD} ${WRAPFLAGS} ${F90_SRC}
33+
34+
f2py: ${F90WRAP_SRC}
35+
CFLAGS="${CFLAGS}" ${F2PY} -c -m _${PY_MOD} ${F2PYFLAGS} f90wrap_*.f90 *.o
36+
37+
test: f2py
38+
${PYTHON} tests.py
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
include ../make.meson.inc
2+
3+
NAME := pywrapper
4+
5+
test: build
6+
$(PYTHON) tests.py

examples/intent_out_size/main.f90

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
module m_intent_out
2+
3+
implicit none
4+
public
5+
6+
contains
7+
8+
subroutine interpolation(n1,n2,a1,a2,output)
9+
!
10+
integer, intent(in) :: n1,n2
11+
real,dimension(n1,n2), intent(in) :: a1,a2
12+
real,dimension(n1,n2), intent(out) :: output
13+
14+
integer :: i,j
15+
16+
do j=1,n2
17+
do i=1,n1
18+
output(i,j)=(a1(i,j)+a2(i,j))/2
19+
enddo
20+
enddo
21+
22+
end subroutine interpolation
23+
24+
end module m_intent_out
25+
26+

examples/intent_out_size/tests.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import unittest
2+
import numpy as np
3+
4+
from pywrapper import m_intent_out
5+
6+
class TestIntentOut(unittest.TestCase):
7+
8+
def test_intent_out_size(self):
9+
10+
a1 = np.array([[1,2], [3,4]], dtype=np.float32, order='F')
11+
a2 = np.array([[2,4], [6,8]], dtype=np.float32, order='F')
12+
output = np.zeros((2,2), dtype=np.float32, order='F')
13+
n1 = 2
14+
n2 = 2
15+
16+
m_intent_out.interpolation(n1,n2,a1,a2,output)
17+
18+
ref_out = np.array([[1.5,3.], [4.5,6.]], dtype=np.float32, order='F')
19+
20+
np.testing.assert_array_equal(output, ref_out)
21+
22+
if __name__ == '__main__':
23+
24+
unittest.main()

f90wrap/pywrapgen.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ def f902py_name(node, f90_name):
500500
log.info("Adding dimension argument to '%s'" % node.name)
501501
dct["f90_arg_names"] = "%s, %s" % (
502502
dct["f90_arg_names"],
503-
"n%d=%s" % (offset, out_dim),
503+
"f90wrap_n%d=%s" % (offset, out_dim),
504504
)
505505
offset += 1
506506

f90wrap/transform.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,7 @@ class ArrayDimensionConverter(ft.FortranVisitor):
580580
def visit_Procedure(self, node):
581581

582582
n_dummy = 0
583+
all_new_dummy_args = []
583584
for arg in node.arguments:
584585
dims = [attr for attr in arg.attributes if attr.startswith('dimension')]
585586
if dims == []:
@@ -601,7 +602,7 @@ def visit_Procedure(self, node):
601602
d.replace('len', 'slen'), arg.name))
602603
new_ds.append(d)
603604
continue
604-
dummy_arg = ft.Argument(name='n%d' % n_dummy, type='integer', attributes=['intent(hide)'])
605+
dummy_arg = ft.Argument(name='f90wrap_n%d' % n_dummy, type='integer', attributes=['intent(hide)'])
605606

606607
if 'intent(out)' not in arg.attributes:
607608
dummy_arg.f2py_line = ('!f2py intent(hide), depend(%s) :: %s = shape(%s,%d)' %
@@ -614,7 +615,12 @@ def visit_Procedure(self, node):
614615
log.debug('adding dummy arguments %r to %s' % (new_dummy_args, node.name))
615616
arg.attributes = ([attr for attr in arg.attributes if not attr.startswith('dimension')] +
616617
['dimension(%s)' % ','.join(new_ds)])
617-
node.arguments.extend(new_dummy_args)
618+
all_new_dummy_args.extend(new_dummy_args)
619+
620+
# New dummy args are prepended so that they are defined before being used as array dimensions
621+
# This avoids implicit declaration
622+
if all_new_dummy_args != []:
623+
node.arguments = all_new_dummy_args + node.arguments
618624

619625

620626
class MethodFinder(ft.FortranTransformer):

0 commit comments

Comments
 (0)