Skip to content

Instantly share code, notes, and snippets.

@Sleepingwell
Last active January 12, 2023 03:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sleepingwell/df93c55f1598ef0ddcf6f2c61056c5c1 to your computer and use it in GitHub Desktop.
Save Sleepingwell/df93c55f1598ef0ddcf6f2c61056c5c1 to your computer and use it in GitHub Desktop.
A minimal example of calling a python function via a callback passed to a fortran subroutine.
.PHONY: venv
VENV_PATH=venv
TIMESTAMP_FILE=$(VENV_PATH)/.f2pytest-creation-timestamp
test: venv test.f90
. $(VENV_PATH)/bin/activate && \
python -m numpy.f2py -c test.f90 -m test
venv: $(TIMESTAMP_FILE)
$(TIMESTAMP_FILE):
virtualenv -p python3 $(VENV_PATH)
. $(VENV_PATH)/bin/activate && pip install numpy scipy
touch $(TIMESTAMP_FILE)
import test
def func(x):
print('x is {}'.format(x))
return x * 2
test.test(func)
subroutine test(py_func)
integer x,y,py_func
external py_func
x = 42
y = py_func(x)
end subroutine
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment