Skip to content

Commit

Permalink
implemented custom findloc function for gfortran <9.0 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
n-claes committed Aug 21, 2021
1 parent 27a174d commit f862e0b
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 9 deletions.
35 changes: 30 additions & 5 deletions src/eigenfunctions/mod_eigenfunctions.f08
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ module subroutine clean_derived_eigenfunctions(); end subroutine
public :: derived_eigenfunctions
public :: initialise_eigenfunctions
public :: calculate_eigenfunctions
public :: find_name_loc_in_array
public :: retrieve_eigenfunctions
public :: retrieve_eigenfunction_from_index
public :: eigenfunctions_clean
Expand Down Expand Up @@ -121,6 +122,28 @@ subroutine calculate_eigenfunctions(right_eigenvectors)
end subroutine calculate_eigenfunctions


!> Function to locate the index of a given name in a character array.
!! Iterates over the elements and returns on the first hit, if no match
!! was found zero is returned.
function find_name_loc_in_array(name, array) result(match_idx)
!> the name to search for
character(len=*), intent(in) :: name
!> array with the names to search in
character(len=*), intent(in) :: array(:)
!> index of first match
integer :: match_idx
integer :: i

match_idx = 0
do i = 1, size(array)
if (array(i) == name) then
match_idx = i
exit
end if
end do
end function find_name_loc_in_array


!> Returns the full set of eigenfunctions corresponding to the given eigenfunction
!! name.
function retrieve_eigenfunctions(name) result(eigenfunctions)
Expand All @@ -133,17 +156,19 @@ function retrieve_eigenfunctions(name) result(eigenfunctions)
integer :: name_idx

! check if we want a regular eigenfunction
name_idx = findloc(ef_names, name, dim=1)
name_idx = find_name_loc_in_array(name, ef_names)
if (name_idx > 0) then
! found, retrieve and return
eigenfunctions = base_eigenfunctions(name_idx)
return
end if
! not found (= 0), try a derived quantity
name_idx = findloc(derived_ef_names, name, dim=1)
if (name_idx > 0) then
eigenfunctions = derived_eigenfunctions(name_idx)
return
if (derived_efs_initialised) then
name_idx = find_name_loc_in_array(name, derived_ef_names)
if (name_idx > 0) then
eigenfunctions = derived_eigenfunctions(name_idx)
return
end if
end if
! if still not found then something went wrong
call log_message( &
Expand Down
44 changes: 40 additions & 4 deletions tests/unit_tests/mod_test_eigenfunctions.pf
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module mod_test_eigenfunctions
complex(dp) :: eigenvals(25)
complex(dp), allocatable :: eigenvectors(:, :)
type(ef_type) :: ef_test
character(len=10) :: test_names(5) = ["name1", "name2", "name3", "name4", "name5"]

contains

Expand Down Expand Up @@ -117,6 +118,20 @@ contains
end subroutine test_eigenfunction_subset_idxs


@test
subroutine test_find_name_loc()
call set_name("eigenfunctions - retrieving name")
@assertEqual(2, find_name_loc_in_array("name2", test_names))
end subroutine test_find_name_loc


@test
subroutine test_find_name_loc_fail()
call set_name("eigenfunctions - retrieving name (fail)")
@assertEqual(0, find_name_loc_in_array("unknown", test_names))
end subroutine test_find_name_loc_fail


@test
subroutine test_ef_names()
call set_name("eigenfunctions - names")
Expand All @@ -125,10 +140,31 @@ contains


@test
subroutine test_ef_names_derived()
call set_name("eigenfunctions - derived names")
write(*, *) derived_ef_names
end subroutine test_ef_names_derived
subroutine test_ef_names_derived_no_bfield()
call set_name("eigenfunctions - derived names (hydro)")
call enable_derived_efs()
@assertEqual(12, size(derived_ef_names))
@assertTrue(.not. any(derived_ef_names == "B_para"))
end subroutine test_ef_names_derived_no_bfield


@test
subroutine test_ef_names_derived_with_b01_field()
call set_name("eigenfunctions - derived names (with B01)")
logging_level = 0
call enable_derived_efs()
call use_bfield(use_b01=.true.)
@assertEqual(12, size(derived_ef_names))
end subroutine test_ef_names_derived_with_b01_field


@test
subroutine test_ef_names_derived_with_bfield()
call set_name("eigenfunctions - derived names (without B01)")
call enable_derived_efs()
call use_bfield(use_b01=.false.)
@assertEqual(20, size(derived_ef_names))
end subroutine test_ef_names_derived_with_bfield


@test
Expand Down

0 comments on commit f862e0b

Please sign in to comment.