utils.f90 4.16 KB
Newer Older
Luca's avatar
init  
Luca committed
1 2 3

! Contains some functions that might be useful later

4 5 6 7
! Write your own print function with ability to suppress print
! Muss immer alle prints und warnings ausschalten können
! Test Programm schreiben mit exit codes -> gfortran 'test.f90' und dann 'echo $?'

Luca's avatar
init  
Luca committed
8
MODULE utils
9
  use globals
Luca's avatar
init  
Luca committed
10
  implicit none
11 12
  ! logical :: print_enabled = .true.
  ! logical :: warnings_enabled = .true.
13

14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
  INTERFACE print_matrix
    ! prints 2d array as matrix. For complex it takes absolutes
    
    SUBROUTINE print_integer_matrix(m) 
      integer :: m(:,:)
      integer :: s(2)
    END SUBROUTINE print_integer_matrix

  SUBROUTINE print_complex_matrix(m) 
    complex :: m(:,:)
    integer :: s(2)
  END SUBROUTINE print_complex_matrix

  END INTERFACE print_matrix

Luca's avatar
init  
Luca committed
29
CONTAINS
30 31 32

  FUNCTION  get_condensed_m(z) result(m)
    ! returns condensed m where the ones not needed are filled with 0
33 34
    complex(kind=prec), intent(in) :: z(:)
    integer :: m(size(z)), pos, i 
35
    m = 1
36
    pos = 1
37
    do i = 1,size(z)
38
      if(abs(z(i)) < zero) then
39 40 41 42 43 44 45 46 47 48 49 50
        if(i == size(z)) then
          pos = pos + 1
        else 
          m(pos) = m(pos) + 1
        end if
      else 
        pos = pos + 1
      end if
    end do
    m(pos:) = 0
  END FUNCTION get_condensed_m

51
  FUNCTION get_condensed_z(m, z_in) result(z_out)
52 53 54 55 56 57 58 59
    ! returns condensed z vector
    integer :: m(:), i, pos
    complex(kind=prec) :: z_in(:), z_out(size(m)) 
    pos = 0
    do i=1,size(m)
      pos = pos + m(i)
      z_out(i) = z_in(pos)
    end do
60
  END FUNCTION get_condensed_z
61 62 63 64 65 66 67 68 69 70 71 72 73

  FUNCTION  get_flattened_z(m,z_in) result(z_out)
    ! returns flattened version of z based on m and z
    integer :: m(:), i, pos
    complex(kind=prec) :: z_in(:), z_out(sum(m))
    z_out = 0
    pos = 0
    do i=1,size(m)
      pos = pos + m(i)
      z_out(pos) = z_in(i)
    end do
  END FUNCTION get_flattened_z

74 75 76 77 78
  FUNCTION find_amount_trailing_zeros(z) result(res)
    complex(kind=prec) :: z(:)
    integer :: res, i
    res = 0
    do i = size(z), 1, -1
79
      if( abs(z(i)) < zero ) then
80 81 82 83 84 85 86
        res = res + 1
      else
        exit
      end if
    end do
  END FUNCTION find_amount_trailing_zeros

87 88 89 90 91 92 93 94 95 96 97 98
  FUNCTION find_first_zero(v) result(res)
    ! returns index of first zero, or -1 if there is no zero
    integer :: v(:), i, res
    res = -1
    do i = 1,size(v)
      if(v(i) == 0) then
        res = i
        return
      end if
    end do
  END FUNCTION find_first_zero

99 100 101 102 103 104
  FUNCTION zero_array(n) result(res)
    integer :: n
    complex(kind=prec) :: res(n)
    res = 0
  END FUNCTION zero_array

105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
  FUNCTION shuffle_with_zero(a) result(res)
    ! rows of result are shuffles of a with 0
    complex :: a(:)
    complex :: res(size(a)+1,size(a)+1)
    integer :: i,j, N
    N = size(a)+1
    do i = 1,N
      ! i is the index of the row
      ! j is the index of the zero
      j  = N+1-i
      res(i,j) = 0
      res(i,1:j-1) = a(1:j-1)
      res(i,j+1:N) = a(j:)
    end do
  END FUNCTION shuffle_with_zero

  ! subroutine print(s1,s2,s3,s4,s5)
  !   character(len = *), intent(in), optional :: s1, s2, s3, s4, s5
  !   if(print_enabled) then
  !     print*, s1, s2, s3, s4, s5
  !   end if
  ! end subroutine print

  ! subroutine warn(s1,s2,s3,s4,s5)
  !   character(len = *), intent(in), optional :: s1, s2, s3, s4, s5
  !   if(warnings_enabled) then
  !     print*, 'Warning: ', s1, s2, s3, s4, s5
  !   end if
  ! end subroutine warn
Luca's avatar
init  
Luca committed
134 135 136

END MODULE utils

137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155

SUBROUTINE print_integer_matrix(m) 
  integer :: m(:,:)
  integer :: s(2), i
  s = shape(m)
  do i = 1,s(1)
    print*, m(i,:)
  end do
END SUBROUTINE print_integer_matrix

SUBROUTINE print_complex_matrix(m) 
  complex :: m(:,:)
  integer :: s(2), i
  s = shape(m)
  do i = 1,s(1)
    print*, abs(m(i,:))
  end do
END SUBROUTINE print_complex_matrix

156 157 158
! PROGRAM test
!   use  utils
!   implicit none
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
  
!   complex(kind=prec) :: z(4)
!   integer :: m_prime(4), condensed_size
!   z = cmplx((/0.0,1.7,0.0,0.0/))

!   ! transform to condensed notation
!   m_prime = get_condensed_m(z)
!   print*, abs(z)
!   m_prime = get_condensed_m(z)
!   print*, abs(z)
!   if(find_first_zero(m_prime) == -1) then
!     condensed_size = size(m_prime)
!   else
!     condensed_size = find_first_zero(m_prime)-1 
!   end if
!   print*, condensed_size
175 176

! END  PROGRAM test