tchlux / fmodpy Goto Github PK
View Code? Open in Web Editor NEWA lightweight, efficient, highly automated, fortran wrapper for python.
License: MIT License
A lightweight, efficient, highly automated, fortran wrapper for python.
License: MIT License
Hi,
Currently I am getting the following error:
Traceback (most recent call last):
File "/home/johannes/anaconda3/lib/python3.8/runpy.py", line 194, in _run_module_as_main
return _run_code(code, main_globals, None,
File "/home/johannes/anaconda3/lib/python3.8/runpy.py", line 87, in _run_code
exec(code, run_globals)
File "/home/johannes/anaconda3/lib/python3.8/site-packages/fmodpy/__main__.py", line 33, in <module>
fimport(file_path, **command_line_args)
File "/home/johannes/anaconda3/lib/python3.8/site-packages/fmodpy/fmodpy.py", line 178, in fimport
fortran_wrapper, python_wrapper = make_wrapper(
File "/home/johannes/anaconda3/lib/python3.8/site-packages/fmodpy/fmodpy.py", line 277, in make_wrapper
python_file = abstraction.generate_python()
File "/home/johannes/anaconda3/lib/python3.8/site-packages/fmodpy/parsing/file.py", line 97, in generate_python
lines += super().generate_python()
File "/home/johannes/anaconda3/lib/python3.8/site-packages/fmodpy/parsing/code.py", line 154, in generate_python
lines += instance.generate_python(*args) + ['']
File "/home/johannes/anaconda3/lib/python3.8/site-packages/fmodpy/parsing/module.py", line 126, in generate_python
lines += [' '+l for l in code.generate_python(*args)]
File "/home/johannes/anaconda3/lib/python3.8/site-packages/fmodpy/parsing/subroutine.py", line 306, in generate_python
arg_declare = arg.py_declare()
File "/home/johannes/anaconda3/lib/python3.8/site-packages/fmodpy/parsing/argument.py", line 205, in py_declare
default_shape = ', '.join(self._default_size()).lower()
File "/home/johannes/anaconda3/lib/python3.8/site-packages/fmodpy/parsing/argument.py", line 586, in _default_size
dim = str(int(dim) - 1)
ValueError: invalid literal for int() with base 10: '1)'
when running python3 -m fmodpy test.f90
.
test.f90 has the following code:
module tmm
use iso_fortran_env, only: int32, real64
implicit none
contains
pure function calc_angle(incidence_angle,n) result(angle)
real(real64), intent(in) :: incidence_angle
real(real64), intent(in) :: n(:,:)
real(real64) :: angle(size(n(:,1)),size(n(1,:)))
end function calc_angle
end module tmm
This piece of code compiles just fine using gfortran -c test.f90
.
I suspect it has to do with the dimensions in the definition of the array angle
.
Currently, I am using fmodpy
version 1.3.3
. Do you know what causes this behaviour?
koshMer
Hi,
I am not familiar with github, so I don't know whether this is the right place to ask:
Encountered unrecognized line while parsing Module:
'PURE FUNCTION MATINV2 ( A ) RESULT ( B )'
are pure functions not supported by your module?
pure
i run into the following exception - could you tell me why?:fmodpy.exceptions.ParseError: Finished parsing FUNCTION MATINV2, but never declared A, B.
Attached you can find the minimum working example which i ran with python3 fmodpy minimum_working_example.f90 name=mwe
:
minimum_working_example.txt
end module tmm
statement the first time i ran python3 fmodpy minimum_working_example.f90 name=mwe
and i had to CTRL-C
the terminal because it wouldn't stop ^^ :)I really like this project of yours, keep it going! ๐ฅ
Hi! thank you for this great tool. After wrestling with f2py for way too long this is indeed great to have.
For simple tasks I've managed to make it working so far and then I wanted to step up my game with linking to OpenBLAS on windows. It has its own complications obviously but what I am trying to do can be described as;
1- Let's say I downloaded OpenBLAS from Anaconda https://anaconda.org/conda-forge/libopenblas/files
2- My target is to wrap the FORTRAN library https://github.com/SLICOT/SLICOT-Reference but in it let's pick a very simple file /src/MB01OE.f
that has only BLAS dependencies but the rest is standalone; a good candidate for a linking exercise.
3- Now I know somehow I have to use fmodpy.config.{something}
to arrive at the command
gfortran -shared .\MB01OE.f -o .\MB01OE.o -L"C:\opt\64\lib" -lopenblas
(or I completely misunderstood the configuration mechanism).
I have not been able to do this step go forward yet. Could you point me to any direction ? Or is this even possible with openblas both having blas and lapack symbols.
If you want a slightly more complicated example https://github.com/SLICOT/SLICOT-Reference/blob/main/src/MB01OD.f depends on both BLAS and MB01OE.f
mentioned above which is my second mini-goal if I can get this working.
Hi :)
I am currently trying to build an anaconda package from the source code you uploaded on pypi.org. Since it is on pypi it is supposedly quite easy to do so ^^.
For reference, i followed this official guide: conda-build skeleton.
During testing the build scripts expects the following path in the folder og.fmodpy
: /about/version.txt
What is the purpose of og.fmodpy
? You mentioned somewhere that this folder contains old code. Is it necessary to make your module work?
I managed to uninstall and reinstall fmodpy
. Now, all examples from last time work great -- via command line and my python editor.
However, when I try to run the following piece of code tmm.txt
with this python file:
import fmodpy
import numpy as np
code = fmodpy.fimport("tmm.f90")
n = np.array([1,2,1,2,1,2],dtype = float, order = "F")
print(code.calc_k(630e-9,n,n))
i get the following error message:
Traceback (most recent call last):
File "/home/johannes/Dokumente/Masterarbeit/Fortran/TMM/tmm_test/python_test_f90.py", line 38, in <module>
print(code.calc_k(630e-9,n,n))
File "/home/johannes/Dokumente/Masterarbeit/Fortran/TMM/tmm_test/tmm/__init__.py", line 85, in calc_k
clib.c_calc_k(ctypes.byref(wavelength), ctypes.byref(n_dim_1), ctypes.c_void_p(n.ctypes.data), ctypes.byref(angle_dim_1), ctypes.c_void_p(angle.ctypes.data), ctypes.byref(k_dim_1), ctypes.c_void_p(k.ctypes.data))
File "/home/johannes/anaconda3/lib/python3.8/ctypes/__init__.py", line 394, in __getattr__
func = self.__getitem__(name)
File "/home/johannes/anaconda3/lib/python3.8/ctypes/__init__.py", line 399, in __getitem__
func = self._FuncPtr((name_or_ordinal, self))
AttributeError: /home/johannes/Dokumente/Masterarbeit/Fortran/TMM/tmm_test/tmm/tmm.so: undefined symbol: c_calc_k
Do you have a clue what's going on?
Hi, its me again :)
I tried to put several functions in one file test2.f90
and use it in python. However, the compilation of this small program fails.
The code in question:
pure function add(val1, val2) result(val3)
use iso_fortran_env, only: real32
implicit none
real(real32), intent(in) :: val1,val2
real(real32) :: val3
val3 = val1+val2
end function add
pure function triple_add(val1,val2) result(val3)
use iso_fortran_env, only: real32
implicit none
real(real32), intent(in) :: val1,val2
real(real32) :: val3
val3 = add(val1,val2)*3.0
end function triple_add
The python code i use to run it:
import fmodpy
import numpy as np
code = fmodpy.fimport("test.f90",verbose = True)
val1 = np.float32(1)
val2 = np.float32(2)
code.add(1,2)
code.triple_add(1,2)
And the error output:
______________________________________________________________________
fimport
fmodpy configuration:
autocompile = True
blas = False
config_file = '.fmodpy.py'
debug_line_numbers = False
end_is_named = True
f_compiler = 'gfortran'
f_compiler_args = ['-fPIC', '-shared', '-O3']
home_directory = '/home/johannes'
implicit_typing = False
lapack = False
link_blas = ['-lblas']
link_lapack = ['-lblas', '-llapack']
link_omp = ['-fopenmp']
log_file = <ipykernel.iostream.OutStream object at 0x7f6acd706bb0>
omp = False
rebuild = False
show_warnings = True
verbose = True
wrap = True
=================================================================================
Input file directory: /home/johannes/Dokumente/Masterarbeit/Fortran/TMM/tmm_test
Input file name: test2.f90
Base module name: test2
Using build dir: temporary directory
fortran wrapper: test2_c_wrapper.f90
python wrapper: test2_python_wrapper.py
Output module path: /home/johannes/Dokumente/Masterarbeit/Fortran/TMM/tmm_test
=================================================================================
Using temporary build directory at '/tmp/tmp2t6pmdpk'.
Build directory is different from source directory..
sym-linking '/home/johannes/Dokumente/Masterarbeit/Fortran/TMM/tmm_test/test2.f90' to '/tmp/tmp2t6pmdpk/test2.f90'
sym-linking '/home/johannes/Dokumente/Masterarbeit/Fortran/TMM/tmm_test/test.txt' to '/tmp/tmp2t6pmdpk/test.txt'
sym-linking '/home/johannes/Dokumente/Masterarbeit/Fortran/TMM/tmm_test/test' to '/tmp/tmp2t6pmdpk/test'
Attempting to autocompile..
reading 'test2.f90' to check if it can be autocompiled.. yes.
skipping '/tmp/tmp2t6pmdpk/test.txt'
skipping '/tmp/tmp2t6pmdpk/test'
Compiling 'test2.f90'..
gfortran -fPIC -shared -O3 -o ./fmodpy_get_size test2.f90
failed.
----------------------------------------------------------------------
STANDARD ERROR:
test2.f90:26:11:
26 | val3 = add(val1,val2)*3.0
| 1
Error: Return type mismatch of function โaddโ at (1) (UNKNOWN/REAL(4))
test2.f90:26:11:
26 | val3 = add(val1,val2)*3.0
| 1
Error: Function โaddโ at (1) has no IMPLICIT type
test2.f90:26:11:
26 | val3 = add(val1,val2)*3.0
| 1
Error: Reference to impure function โaddโ at (1) within a PURE procedure
----------------------------------------------------------------------
Compiling 'test2.f90'..
gfortran -fPIC -shared -O3 -o ./fmodpy_get_size test2.f90
failed.
----------------------------------------------------------------------
STANDARD ERROR:
test2.f90:26:11:
26 | val3 = add(val1,val2)*3.0
| 1
Error: Return type mismatch of function โaddโ at (1) (UNKNOWN/REAL(4))
test2.f90:26:11:
26 | val3 = add(val1,val2)*3.0
| 1
Error: Function โaddโ at (1) has no IMPLICIT type
test2.f90:26:11:
26 | val3 = add(val1,val2)*3.0
| 1
Error: Reference to impure function โaddโ at (1) within a PURE procedure
----------------------------------------------------------------------
Failed to compile 'test2.f90'.
Failed to compile 'test2.f90'.
File.parse
Function.parse ADD
Function.parse done.
Function.parse TRIPLE_ADD
Function.parse done.
File.parse done.
----------------------------------------------------------------------
FILE
FUNCTION ADD(VAL1, VAL2) RESULT(VAL3)
USE ISO_FORTRAN_ENV , ONLY : REAL32
IMPLICIT NONE
REAL(KIND=REAL32), INTENT(IN) :: VAL1
REAL(KIND=REAL32), INTENT(IN) :: VAL2
REAL(KIND=REAL32) :: VAL3
END FUNCTION ADD
FUNCTION TRIPLE_ADD(VAL1, VAL2) RESULT(VAL3)
USE ISO_FORTRAN_ENV , ONLY : REAL32
IMPLICIT NONE
REAL(KIND=REAL32), INTENT(IN) :: VAL1
REAL(KIND=REAL32), INTENT(IN) :: VAL2
REAL(KIND=REAL32) :: VAL3
END FUNCTION TRIPLE_ADD
----------------------------------------------------------------------
Making symlink from '__init__.py' to 'test2_python_wrapper.py'
Moving from:
/tmp/tmp2t6pmdpk
to
/home/johannes/Dokumente/Masterarbeit/Fortran/TMM/tmm_test/test2
Finished making module 'test2'.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Running system command with arguments
gfortran -fPIC -shared -O3 -o test2.so test2.f90 test2_c_wrapper.f90
test2.f90:26:11:
26 | val3 = add(val1,val2)*3.0
| 1
Error: Return type mismatch of function โaddโ at (1) (UNKNOWN/REAL(4))
test2.f90:26:11:
26 | val3 = add(val1,val2)*3.0
| 1
Error: Function โaddโ at (1) has no IMPLICIT type
test2.f90:26:11:
26 | val3 = add(val1,val2)*3.0
| 1
Error: Reference to impure function โaddโ at (1) within a PURE procedure
Traceback (most recent call last):
File "/home/johannes/Dokumente/Masterarbeit/Fortran/TMM/tmm_test/test2/__init__.py", line 28, in <module>
clib = ctypes.CDLL(_path_to_lib)
File "/home/johannes/anaconda3/lib/python3.8/ctypes/__init__.py", line 381, in __init__
self._handle = _dlopen(self._name, mode)
OSError: /home/johannes/Dokumente/Masterarbeit/Fortran/TMM/tmm_test/test2/test2.so: cannot open shared object file: No such file or directory
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/johannes/Dokumente/Masterarbeit/Fortran/TMM/tmm_test/python_test_f90.py", line 11, in <module>
code = fmodpy.fimport("test2.f90",verbose = True)
File "/home/johannes/anaconda3/lib/python3.8/site-packages/fmodpy/fmodpy.py", line 240, in fimport
module = importlib.import_module(name)
File "/home/johannes/anaconda3/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 783, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/johannes/Dokumente/Masterarbeit/Fortran/TMM/tmm_test/test2/__init__.py", line 42, in <module>
clib = ctypes.CDLL(_path_to_lib)
File "/home/johannes/anaconda3/lib/python3.8/ctypes/__init__.py", line 381, in __init__
self._handle = _dlopen(self._name, mode)
OSError: /home/johannes/Dokumente/Masterarbeit/Fortran/TMM/tmm_test/test2/test2.so: cannot open shared object file: No such file or directory
I am not really sure why I am getting any of those errors when compiling the fortran code. It seems to me that both functions are pure and i state the type for all variables and only use real32....
It works well if I use only the function add(val1,val2)
.
Do I have to structure programs differently? Should I define functions in a fortran module?
Hi.
when i write fortran file like this
subroutine some(array_real64)
! use iso_c_binding
implicit none
INTEGER :: i
REAL(kind=8), DIMENSION(:), ALLOCATABLE, INTENT(OUT) :: array_real64
ALLOCATE (array_real64(4))
do i = 1, size(array_real64)
array_real64(i) = 3.14*i
end do
end subroutine some
and python file like this:
import os
dir_name = os.path.dirname(os.path.abspath(__file__))
test_name = os.path.basename(dir_name)
fort_file = os.path.join(dir_name, "real64.f90")
build_dir = os.path.join(dir_name, "fmodpy_real64")
import fmodpy
import numpy as np
RWD = fmodpy.fimport(fort_file, build_dir=build_dir,
output_dir=dir_name, rebuild=True)
# input = np.zeros((4),dtype=np.float64,order='F')
out = RWD.some()
print(out)
print(out.dtype)
the results is wrong and its type is float32
If I remove the ALLOCATABLE attribute from the real64 array.
REAL(kind=8), DIMENSION(:), INTENT(OUT) :: array_real64
and python like this
input = np.zeros((4),dtype=np.float64,order='F')
out = RWD.some(input)
Hi,
I was wondering whether your wrapper has support for openMP or co-array fortran?
i tried running the .f90
program:
module hello_images
implicit none
contains
subroutine hello()
print *, "I am iamge", this_image(), "of", num_images()
end subroutine hello
end module hello_images
with the following python program
import fmodpy
code = fmodpy.fimport("hello_images.f90",f_compiler = "caf")
code.hello_images.hello()
This gives me an output of I am iamge 0 of 0
Usually, when using co-array fortran, one would supply the number of images when running the program.
For Example: cafrun -n program
Is something like this possible with your wrapper?
Hello,
Your project looks interesting.
I am wondering if this legacy optical design code could be converted to python:
https://github.com/dinosauria123/Koko
original: www.ecalculations.com KDP-2
Kind regards
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.