Skip to content

Commit

Permalink
fprettify code formattting
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobwilliams committed Feb 18, 2024
1 parent 234298f commit f6530aa
Show file tree
Hide file tree
Showing 7 changed files with 1,570 additions and 1,546 deletions.
19 changes: 19 additions & 0 deletions .fprettify.rc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
enable-replacements=True
c-relations=True
case=[1,1,1,1]
indent=4
line-length=132
strict-indent=True
strip-comments=True
whitespace-relational=True
whitespace-logical=True
whitespace-plusminus=True
whitespace-multdiv=True
whitespace-comma=True
whitespace-intrinsics=True
whitespace-print=False
whitespace-type=False
whitespace-decl=True
enable-decl=True
disable-fypp=True
disable-indent-mod=False
7 changes: 6 additions & 1 deletion radbelt.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
"files.trimTrailingWhitespace": true,
"editor.insertSpaces": true,
"editor.tabSize": 4,
"editor.trimAutoWhitespace": true
"editor.trimAutoWhitespace": true,
"fortran.linter.includePaths": [
"${workspaceFolder}/src",
"${workspaceFolder}/test"
],
"fortran.linter.initialize": false
}
}
200 changes: 100 additions & 100 deletions src/radbelt_c_module.f90
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
!>
! Experimental C interface to the radbelt module.

module radbelt_c_module
module radbelt_c_module

use iso_c_binding, only: c_double, c_int, c_char, c_null_char, &
c_intptr_t, c_ptr, c_loc, c_f_pointer, &
Expand All @@ -11,194 +11,194 @@ module radbelt_c_module

implicit none

contains
contains
!*****************************************************************************************

!*****************************************************************************************
!>
! Convert C string to Fortran

function c2f_str(cstr) result(fstr)
function c2f_str(cstr) result(fstr)

character(kind=c_char,len=1),dimension(:),intent(in) :: cstr !! string from C
character(len=:),allocatable :: fstr !! fortran string
character(kind=c_char, len=1), dimension(:), intent(in) :: cstr !! string from C
character(len=:), allocatable :: fstr !! fortran string

integer :: i !! counter
integer :: i !! counter

fstr = ''
do i = 1, size(cstr)
fstr = fstr//cstr(i)
end do
fstr = trim(fstr)
fstr = ''
do i = 1, size(cstr)
fstr = fstr//cstr(i)
end do
fstr = trim(fstr)

end function c2f_str
end function c2f_str

!*****************************************************************************************
!>
! Convert an integer pointer to a [[radbelt_type]] pointer.

subroutine int_pointer_to_f_pointer(ipointer, p)
subroutine int_pointer_to_f_pointer(ipointer, p)

integer(c_intptr_t),intent(in) :: ipointer !! integer pointer from C
type(radbelt_type),pointer :: p !! fortran pointer
integer(c_intptr_t), intent(in) :: ipointer !! integer pointer from C
type(radbelt_type), pointer :: p !! fortran pointer

type(c_ptr) :: cp
type(c_ptr) :: cp

cp = transfer(ipointer, c_null_ptr)
if (c_associated(cp)) then
call c_f_pointer(cp, p)
else
p => null()
end if
cp = transfer(ipointer, c_null_ptr)
if (c_associated(cp)) then
call c_f_pointer(cp, p)
else
p => null()
end if

end subroutine int_pointer_to_f_pointer
end subroutine int_pointer_to_f_pointer

!*****************************************************************************************
!>
! create a [[radbelt_type]] from C

subroutine initialize_c(ipointer) bind(C, name="initialize_c")
subroutine initialize_c(ipointer) bind(C, name="initialize_c")

integer(c_intptr_t),intent(out) :: ipointer
type(radbelt_type),pointer :: p
type(c_ptr) :: cp
integer(c_intptr_t), intent(out) :: ipointer
type(radbelt_type), pointer :: p
type(c_ptr) :: cp

allocate(p)
cp = c_loc(p)
ipointer = transfer(cp, 0_c_intptr_t)
allocate (p)
cp = c_loc(p)
ipointer = transfer(cp, 0_c_intptr_t)

end subroutine initialize_c
end subroutine initialize_c

!*****************************************************************************************
!>
! destroy a [[radbelt_type]] from C

subroutine destroy_c(ipointer) bind(C, name="destroy_c")
subroutine destroy_c(ipointer) bind(C, name="destroy_c")

integer(c_intptr_t),intent(in) :: ipointer
type(radbelt_type),pointer :: p
integer(c_intptr_t), intent(in) :: ipointer
type(radbelt_type), pointer :: p

call int_pointer_to_f_pointer(ipointer,p)
if (associated(p)) deallocate(p)
call int_pointer_to_f_pointer(ipointer, p)
if (associated(p)) deallocate (p)

end subroutine destroy_c
end subroutine destroy_c

!*****************************************************************************************
!>
! C interface for setting the `trm` data file path

subroutine set_trm_file_path_c(ipointer, aep8_dir, n) bind(C, name="set_trm_file_path_c")
subroutine set_trm_file_path_c(ipointer, aep8_dir, n) bind(C, name="set_trm_file_path_c")

integer(c_intptr_t),intent(in) :: ipointer
integer(c_int),intent(in) :: n !! size of `aep8_dir`
character(kind=c_char,len=1),dimension(n),intent(in) :: aep8_dir
integer(c_intptr_t), intent(in) :: ipointer
integer(c_int), intent(in) :: n !! size of `aep8_dir`
character(kind=c_char, len=1), dimension(n), intent(in) :: aep8_dir

character(len=:),allocatable :: aep8_dir_
type(radbelt_type),pointer :: p
character(len=:), allocatable :: aep8_dir_
type(radbelt_type), pointer :: p

call int_pointer_to_f_pointer(ipointer, p)
call int_pointer_to_f_pointer(ipointer, p)

if (associated(p)) then
aep8_dir_ = c2f_str(aep8_dir)
call p%set_trm_file_path(aep8_dir_)
else
error stop 'error in set_trm_file_path_c: class is not associated'
end if
if (associated(p)) then
aep8_dir_ = c2f_str(aep8_dir)
call p%set_trm_file_path(aep8_dir_)
else
error stop 'error in set_trm_file_path_c: class is not associated'
end if

end subroutine set_trm_file_path_c
end subroutine set_trm_file_path_c
!*****************************************************************************************

!*****************************************************************************************
!>
! C interface for setting the `igrf` data file path

subroutine set_igrf_file_path_c(ipointer, igrf_dir, n) bind(C, name="set_igrf_file_path")
subroutine set_igrf_file_path_c(ipointer, igrf_dir, n) bind(C, name="set_igrf_file_path")

integer(c_intptr_t),intent(in) :: ipointer
integer(c_int),intent(in) :: n !! size of `igrf_dir`
character(kind=c_char,len=1),dimension(n),intent(in) :: igrf_dir
integer(c_intptr_t), intent(in) :: ipointer
integer(c_int), intent(in) :: n !! size of `igrf_dir`
character(kind=c_char, len=1), dimension(n), intent(in) :: igrf_dir

character(len=:),allocatable :: igrf_dir_
type(radbelt_type),pointer :: p
character(len=:), allocatable :: igrf_dir_
type(radbelt_type), pointer :: p

call int_pointer_to_f_pointer(ipointer, p)
call int_pointer_to_f_pointer(ipointer, p)

if (associated(p)) then
igrf_dir_ = c2f_str(igrf_dir)
call p%set_igrf_file_path(igrf_dir_)
else
error stop 'error in set_igrf_file_path: class is not associated'
end if
if (associated(p)) then
igrf_dir_ = c2f_str(igrf_dir)
call p%set_igrf_file_path(igrf_dir_)
else
error stop 'error in set_igrf_file_path: class is not associated'
end if

end subroutine set_igrf_file_path_c
end subroutine set_igrf_file_path_c
!*****************************************************************************************

!*****************************************************************************************
!>
! C interface for setting the data file paths

subroutine set_data_files_paths_c(ipointer, aep8_dir, igrf_dir, n, m) bind(C, name="set_data_files_paths_c")
subroutine set_data_files_paths_c(ipointer, aep8_dir, igrf_dir, n, m) bind(C, name="set_data_files_paths_c")

integer(c_intptr_t),intent(in) :: ipointer
integer(c_int),intent(in) :: n !! size of `aep8_dir`
character(kind=c_char,len=1),dimension(n),intent(in) :: aep8_dir
integer(c_int),intent(in) :: m !! size of `igrf_dir`
character(kind=c_char,len=1),dimension(m),intent(in) :: igrf_dir
integer(c_intptr_t), intent(in) :: ipointer
integer(c_int), intent(in) :: n !! size of `aep8_dir`
character(kind=c_char, len=1), dimension(n), intent(in) :: aep8_dir
integer(c_int), intent(in) :: m !! size of `igrf_dir`
character(kind=c_char, len=1), dimension(m), intent(in) :: igrf_dir

character(len=:),allocatable :: aep8_dir_, igrf_dir_
type(radbelt_type),pointer :: p
character(len=:), allocatable :: aep8_dir_, igrf_dir_
type(radbelt_type), pointer :: p

call int_pointer_to_f_pointer(ipointer, p)
call int_pointer_to_f_pointer(ipointer, p)

if (associated(p)) then
if (associated(p)) then

aep8_dir_ = c2f_str(aep8_dir)
igrf_dir_ = c2f_str(igrf_dir)
aep8_dir_ = c2f_str(aep8_dir)
igrf_dir_ = c2f_str(igrf_dir)

call p%set_data_files_paths(aep8_dir_, igrf_dir_)
call p%set_data_files_paths(aep8_dir_, igrf_dir_)

else
error stop 'error in set_data_files_paths_c: class is not associated'
end if
else
error stop 'error in set_data_files_paths_c: class is not associated'
end if

end subroutine set_data_files_paths_c
end subroutine set_data_files_paths_c
!*****************************************************************************************

!*****************************************************************************************
!>
! C interface to [[get_flux_g]].

subroutine get_flux_g_c(ipointer,lon,lat,height,year,e,imname,flux) bind(C, name="get_flux_g_c")
subroutine get_flux_g_c(ipointer, lon, lat, height, year, e, imname, flux) bind(C, name="get_flux_g_c")

integer(c_intptr_t),intent(in) :: ipointer
real(c_double),intent(in) :: lon !! geodetic longitude in degrees (east)
real(c_double),intent(in) :: lat !! geodetic latitude in degrees (north)
real(c_double),intent(in) :: height !! altitude in km above sea level
real(c_double),intent(in) :: year !! decimal year for which geomagnetic field is to
integer(c_intptr_t), intent(in) :: ipointer
real(c_double), intent(in) :: lon !! geodetic longitude in degrees (east)
real(c_double), intent(in) :: lat !! geodetic latitude in degrees (north)
real(c_double), intent(in) :: height !! altitude in km above sea level
real(c_double), intent(in) :: year !! decimal year for which geomagnetic field is to
!! be calculated (e.g.:1995.5 for day 185 of 1995)
real(c_double),intent(in) :: e !! minimum energy
integer(c_int),intent(in) :: imname !! which method to use:
real(c_double), intent(in) :: e !! minimum energy
integer(c_int), intent(in) :: imname !! which method to use:
!!
!! * 1 -- particle species: electrons, solar activity: min
!! * 2 -- particle species: electrons, solar activity: max
!! * 3 -- particle species: protons, solar activity: min
!! * 4 -- particle species: protons, solar activity: max
real(c_double),intent(out) :: flux !! The flux of particles above the given energy, in units of cm^-2 s^-1.
real(c_double), intent(out) :: flux !! The flux of particles above the given energy, in units of cm^-2 s^-1.

type(radbelt_type),pointer :: p
type(radbelt_type), pointer :: p

call int_pointer_to_f_pointer(ipointer, p)
call int_pointer_to_f_pointer(ipointer, p)

if (associated(p)) then
if (associated(p)) then

flux = p%get_flux(lon,lat,height,year,e,imname)
flux = p%get_flux(lon, lat, height, year, e, imname)

else
error stop 'error in get_flux_g_c: class is not associated'
end if
else
error stop 'error in get_flux_g_c: class is not associated'
end if

end subroutine get_flux_g_c
end subroutine get_flux_g_c

!*****************************************************************************************
end module radbelt_c_module
end module radbelt_c_module
!*****************************************************************************************
24 changes: 12 additions & 12 deletions src/radbelt_kinds_module.F90
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,36 @@
!>
! Numeric kind definitions for radbelt.

module radbelt_kinds_module
module radbelt_kinds_module

use,intrinsic :: iso_fortran_env
use, intrinsic :: iso_fortran_env

implicit none

private

#ifdef REAL32
integer,parameter,public :: wp = real32 !! Real working precision [4 bytes]
integer, parameter, public :: wp = real32 !! Real working precision [4 bytes]
#elif REAL64
integer,parameter,public :: wp = real64 !! Real working precision [8 bytes]
integer, parameter, public :: wp = real64 !! Real working precision [8 bytes]
#elif REAL128
integer,parameter,public :: wp = real128 !! Real working precision [16 bytes]
integer, parameter, public :: wp = real128 !! Real working precision [16 bytes]
#else
integer,parameter,public :: wp = real64 !! Real working precision if not specified [8 bytes]
integer, parameter, public :: wp = real64 !! Real working precision if not specified [8 bytes]
#endif

#ifdef INT8
integer,parameter,public :: ip = int8 !! Integer working precision [1 byte]
integer, parameter, public :: ip = int8 !! Integer working precision [1 byte]
#elif INT16
integer,parameter,public :: ip = int16 !! Integer working precision [2 bytes]
integer, parameter, public :: ip = int16 !! Integer working precision [2 bytes]
#elif INT32
integer,parameter,public :: ip = int32 !! Integer working precision [4 bytes]
integer, parameter, public :: ip = int32 !! Integer working precision [4 bytes]
#elif INT64
integer,parameter,public :: ip = int64 !! Integer working precision [8 bytes]
integer, parameter, public :: ip = int64 !! Integer working precision [8 bytes]
#else
integer,parameter,public :: ip = int32 !! Integer working precision if not specified [4 bytes]
integer, parameter, public :: ip = int32 !! Integer working precision if not specified [4 bytes]
#endif

!*****************************************************************************************
end module radbelt_kinds_module
end module radbelt_kinds_module
!*****************************************************************************************
Loading

0 comments on commit f6530aa

Please sign in to comment.