def setUpClass(cls): # Working directory cls.origcwd = os.getcwd() cls.cwd = os.path.dirname(__file__) os.chdir(cls.cwd) try: os.system('make clean && make') # Reference output ref_out = subprocess.check_output('./test_arrays.x') # Reference output for vector refspl = ref_out.replace(b' ', b'').split(b'\n\n\n') cls.refvec = np.fromstring(refspl[0], sep='\n') # Reference output for 2D array refarrspl = refspl[1].split(b'\n\n') # Fortran column outputs cls.refarr = np.empty((cls.m, cls.n)) for kcol in range(cls.n): cls.refarr[:, kcol] = np.fromstring(refarrspl[kcol], sep='\n') mod_arrays = fortran_module('test_arrays', 'mod_arrays', path=cls.cwd) mod_arrays.fdef(""" subroutine test_vector(vec) double precision, dimension(:) :: vec end subroutine test_array_2d(arr) double precision, dimension(:,:) :: arr end """) mod_arrays.compile() # recreate module to check if it works independently now cls.mod_arrays = fortran_module('test_arrays', 'mod_arrays', path=cls.cwd) cls.mod_arrays.load() except BaseException: os.chdir(cls.origcwd) raise
bmod_ref = 5.0e4 Z_charge = 2 m_mass = 4 E_kin = 3.5e6 trace_time = 1.0e-2 # rbig=rlarm*bmod00 # dphi=2.d0*pi/(L1i*npoiper) v0 = np.sqrt(2.0*E_kin*ev/(m_mass*p_mass)) # alpha particle velocity, cm/s rlarm = v0*m_mass*p_mass*c/(Z_charge*e_charge*bmod_ref) # reference gyroradius tau = trace_time*v0 libneo_orb = fortran_library('simple') neo_orb = fortran_module(libneo_orb, 'neo_orb_global') neo_orb.fdef(""" subroutine init_field(ans_s, ans_tp, amultharm, aintegmode) integer :: ans_s, ans_tp, amultharm, aintegmode end subroutine init_params(Z_charge, m_mass, E_kin, adtau, adtaumax, arelerr) integer :: Z_charge, m_mass double precision :: E_kin, adtau, adtaumax double precision :: arelerr end subroutine init_integrator(z0) double precision, dimension(:), intent(in) :: z0 end
""" Created: Tue Aug 20 15:07:28 2019 @author: Christopher Albert <*****@*****.**> """ import numpy as np from fffi import fortran_library, fortran_module libneo_orb = fortran_library('neo_orb', path='/u/calbert/build/NEO-ORB', compiler={ 'name': 'ifort', 'version': '18.0.3' }) neoorb = fortran_module(libneo_orb, 'neo_orb_global') neoorb.fdef(""" subroutine init_field(ans_s, ans_tp, amultharm, aintegmode) integer, intent(in) :: ans_s, ans_tp, amultharm, aintegmode end """) libneo_orb.compile() neoorb.load()
""" Created: Tue Aug 20 15:07:28 2019 @author: Christopher Albert <*****@*****.**> """ import numpy as np from fffi import fortran_library, fortran_module libneo_orb = fortran_library('neo_orb', path='../lib') bench = fortran_module(libneo_orb, 'neo_orb_bench') bench.fdef(""" integer :: npoiper2 double precision :: rbig, dtau, dtaumax integer :: nt double precision :: starttime, endtime logical :: multi logical :: quasi logical :: tok integer :: integ_mode integer :: nlag integer :: nplagr_invar integer :: ncut double precision :: taub double precision :: rtol
bmod_ref = 5.0e4 Z_charge = 2 m_mass = 4 E_kin = 3.5e6 trace_time = 1.0e-2 #rbig=rlarm*bmod00 #dphi=2.d0*pi/(L1i*npoiper) v0 = np.sqrt(2.0*E_kin*ev/(m_mass*p_mass)) # alpha particle velocity, cm/s rlarm=v0*m_mass*p_mass*c/(Z_charge*e_charge*bmod_ref) # reference gyroradius tau=trace_time*v0 libneo_orb = fortran_library('neo_orb', path='../lib') orbit_symplectic = fortran_module(libneo_orb, 'orbit_symplectic') orbit_symplectic.fdef(""" type :: SymplecticIntegrator double precision :: atol double precision :: rtol ! Current phase-space coordinates z and old pth double precision, dimension(4) :: z ! z = (r, th, ph, pphi) double precision :: pthold ! Buffer for Lagrange polynomial interpolation integer :: kbuf integer :: kt integer :: k integer :: bufind(0:nlag) double precision, dimension(4, nbuf) :: zbuf
Created on Thu Sep 20 19:00:57 2018 @author: chral """ from ctypes import CDLL, byref, POINTER, c_double, c_int, c_long, c_short import numpy as np libpath = '/home/calbert/code/uqp/libuq.so' lib = CDLL(libpath) #%% from fffi import fortran_module modname = 'mod_index' mod_index = fortran_module(lib, 'mod_index') npoly = c_int(3) nfterme = c_int(0) npterme = c_int(0) npar = c_int(2) jterme = np.zeros(4, dtype=np.int32) func_name = '__{}_MOD_{}'.format(modname, 'number_of_terms') print(func_name) func = getattr(lib, func_name) func(byref(nfterme), byref(npterme), byref(npoly), byref(npar), jterme.ctypes.data_as(POINTER(c_int))) print(jterme)
Z_charge = 2 m_mass = 4 E_kin = 3.5e6 trace_time = 1.0e-2 #rbig=rlarm*bmod00 #dphi=2.d0*pi/(L1i*npoiper) v0 = np.sqrt(2.0 * E_kin * ev / (m_mass * p_mass)) # alpha particle velocity, cm/s rlarm = v0 * m_mass * p_mass * c / (Z_charge * e_charge * bmod_ref ) # reference gyroradius tau = trace_time * v0 libneo_orb = fortran_library('neo_orb', path='../lib') field_can = fortran_module(libneo_orb, 'field_can_mod') field_can.fdef(""" type :: FieldCan integer :: field_type double precision :: Ath, Aph double precision :: hth, hph double precision :: Bmod double precision, dimension(3) :: dAth, dAph double precision, dimension(3) :: dhth, dhph double precision, dimension(3) :: dBmod double precision, dimension(6) :: d2Ath, d2Aph double precision, dimension(6) :: d2hth, d2hph double precision, dimension(6) :: d2Bmod
import matplotlib.pyplot as plt from fffi import fortran_library, fortran_module from netCDF4 import Dataset from mpl_toolkits.mplot3d import Axes3D pi = np.pi libneo_orb = fortran_library('simple', compiler={ 'name': 'gfortran', 'version': 9 }) # compiler={'name': 'ifort', 'version': 18}) neo_orb = fortran_module(libneo_orb, 'neo_orb_global') neo_orb.fdef(""" subroutine init_field(ans_s, ans_tp, amultharm, aintegmode) integer :: ans_s, ans_tp, amultharm, aintegmode end subroutine init_params(Z_charge, m_mass, E_kin, adtau, adtaumax, arelerr) integer :: Z_charge, m_mass double precision :: E_kin, adtau, adtaumax double precision :: arelerr end subroutine init_integrator(z0) double precision, dimension(:), intent(in) :: z0 end