def mod_arrays(tmp): # Working directory cwd = os.path.dirname(__file__) copy(os.path.join(cwd, 'Makefile'), tmp) copy(os.path.join(cwd, 'mod_arrays.f90'), tmp) copy(os.path.join(cwd, 'test_arrays.f90'), tmp) os.mkdir(os.path.join(tmp, 'static')) os.mkdir(os.path.join(tmp, 'shared')) os.chdir(tmp) os.system('make') fort_mod = FortranModule('test_arrays', 'mod_arrays', path=tmp) fort_mod.fdef(""" subroutine test_vector(vec) double precision, dimension(:) :: vec end subroutine subroutine test_array_2d(arr) double precision, dimension(:,:) :: arr end subroutine """) fort_mod.compile() # recreate module to check if it works independently now fort_mod = FortranModule('test_arrays', 'mod_arrays', path=tmp) fort_mod.load() return fort_mod
def uqp(): cwd = os.path.dirname(__file__) os.chdir(cwd) fort_mod = FortranModule('uqp', 'mod_unqu') fort_mod.fdef(""" integer :: np, nall, npar, nt, iflag_run, iflag_mod, iflag_pol subroutine allocate_params end subroutine init_uq end subroutine set_legendre_borders(kpar, lower, upper) integer, intent(in) :: kpar double precision, intent(in) :: lower, upper end subroutine set_hermite_mean_std(kpar, mean0, std) integer, intent(in) :: kpar double precision, intent(in) :: mean0, std end subroutine pre_uq(axi) double precision, intent(inout) :: axi(:,:) end subroutine run_uq end """) fort_mod.compile(verbose=1) fort_mod.load() return fort_mod
def setup(tmp): # Working directory cwd = os.path.dirname(__file__) copy(os.path.join(cwd, 'Makefile'), tmp) copy(os.path.join(cwd, 'ex02_pseudoclass.f90'), tmp) os.chdir(tmp) os.system('make') lib = FortranLibrary('pseudoclass', path=tmp) classmod = FortranModule(lib, 'class_circle') classmod.fdef(""" type Circle double precision :: radius complex(8), dimension(:), allocatable :: alloc_array end type subroutine circle_print(self) type(Circle), intent(in) :: self end subroutine subroutine circle_alloc_member(self) type(Circle), intent(inout) :: self end subroutine """) lib.compile()
Created: Thu Jul 25 12:29:27 2019 @author: Christopher Albert <*****@*****.**> """ # %% Import, compile and load from numpy import array, linspace from fffi import FortranLibrary, FortranModule libfortmod = FortranLibrary('fortmod') fortmod = FortranModule(libfortmod, 'fortmod') # member variable and subroutine definition stub # TODO: parse fortmod.f90 automatically and strip away implementation with open('fortmod.f90', 'r') as f: code = f.read() fortmod.fdef(code) libfortmod.compile() # only required when Fortran library has changed fortmod.load() # %% Try some stuff print('Before init(): member = {}'.format(fortmod.member)) fortmod.init() print('After init(): member = {}'.format(fortmod.member)) a = fortmod.member_array # this is a mutable reference print('Before side_effects(): member_array = {}'.format(a)) fortmod.side_effects() print('After side_effects(): member_array = {}'.format(a)) z = linspace(1, 10, 4)
mean2 = 2.0 std2 = 0.2 uqp = FortranModule('uqp', 'mod_unqu') uqp.fdef(""" integer :: nporder, nall, npar, nt, iflag_run, iflag_mod, iflag_pol subroutine allocate_params end subroutine init_uq end subroutine set_legendre_borders(kpar, lower, upper) integer, intent(in) :: kpar double precision, intent(in) :: lower, upper end subroutine set_hermite_mean_std(kpar, mean0, std) integer, intent(in) :: kpar double precision, intent(in) :: mean0, std end subroutine pre_uq(axi) double precision, intent(inout) :: axi(:,:) end subroutine run_uq end """) uqp.compile(verbose=1) uqp.load() #%% print('=== Hermite 2D quadrature points ===')
from fffi import FortranLibrary, FortranModule lib = FortranLibrary('pseudoclass') classmod = FortranModule(lib, 'class_circle') classmod.fdef(""" type Circle double precision :: radius end type subroutine circle_print(self) type(Circle), intent(in) :: self end """) lib.compile() classmod.load() cir = classmod.new('Circle') cir.radius = 3.0 print('Radius: {}'.format(cir.radius)) print('Fortran output:') classmod.circle_print(cir) # # To print Fortran stdout in Jupyter/IPython REPL: # # from wurlitzer import sys_pipes() # with sys_pipes(): # required to print Fortran stdout in Jupyter # classmod.circle_print(cir)