module moduleTableTtoZne use constantParameters, only: dp type:: tableTtoZne real(dp) :: Z_min, Z_max real(dp) :: Temp_min, Temp_max real(dp), allocatable, dimension(:):: Temp real(dp), allocatable, dimension(:,:):: Z real(dp), allocatable, dimension(:,:):: Z_k real(dp), allocatable, dimension(:):: ne contains procedure, pass:: init => initTableTtoZne procedure, pass:: get => getValueTableTtoZne end type tableTtoZne contains subroutine initTableTtoZne(self, tableFile) use constantParameters, only: eV_to_K use referenceValues, only: Temp_ref, n_ref implicit none class(tableTtoZne), intent(inout):: self character(:), allocatable, intent(in):: tableFile character(100):: dummy character(len=512) :: line character(len=20), allocatable :: ne_headers(:) integer:: amount, num_ne integer:: i,j integer:: stat integer:: id = 20 num_ne = 0 open(id, file = tableFile) amount = -1 ! Remove header do read(id, '(A)', iostat = stat) dummy !If EOF or error, exit file if (stat /= 0) EXIT ! !Skip comment ! if (index(dummy,'#') /= 0) CYCLE !Add row amount = amount + 1 end do !Go bback to initial point rewind(id) read(id, '(A)', iostat = stat) dummy do i = 1, len_trim(dummy) if (dummy(i:i) == ",") num_ne = num_ne + 1 end do allocate(ne_headers(num_ne)) allocate(self%ne(num_ne)) rewind(id) read(id, '(A)') line ! Read values while skipping the first entry read(line, *) dummy, (self%ne(i), i=1, num_ne) ! read(id, *) ! Skip 'x' ! do i = 1, num_ne ! read(dummy, *) ne_headers(i) ! read(ne_headers(i), *) self%ne(i) ! print *, self%ne(i) ! end do rewind(id) !Allocate table arrays allocate(self%Temp(1:amount)) allocate(self%Z(1:amount, 1:num_ne)) allocate(self%Z_k(1:amount, 1:num_ne)) self%Temp = 0.0_dp self%Z = 0.0_dp self%Z_k = 0.0_dp i = 0 read(id, *) ! Skip header do read(id, '(A)', iostat = stat) dummy !TOdo: Make this a function if (stat /= 0) EXIT !Add data !TODO: substitute with extracting information from dummy backspace(id) i = i + 1 read(id, *, iostat= stat) self%Temp(i), (self%Z(i, j), j = 1, num_ne) end do self%Temp = self%Temp * eV_to_K / Temp_ref self%ne = self%ne / n_ref close(id) self%Temp_min = self%Temp(1) self%Temp_max = self%Temp(amount) self%Z_min = self%Z(1,1) self%Z_max = self%Z(amount,num_ne) do i = 1, amount - 1 do j = 1, num_ne self%Z_k(i,j) = ( self%Z(i+1,j) - self%Z(i,j))/(self%Temp(i+1) - self%Temp(i)) end do end do end subroutine initTableTtoZne subroutine getValueTableTtoZne(self, Temp, ne, Z) implicit none class(tableTtoZne), intent(in):: self real(dp), intent(in):: Temp, ne real(dp), intent(out):: Z real(dp):: delta_Temp integer:: i, j j = minloc(abs(ne - self%ne), 1) ! print *, "ne : ", ne ! print *, "Temp : ", Temp if (Temp <= self%Temp_min) THEN Z = self%Z_min elseif (Temp >= self%Temp_max) THEN Z = self%Z_max else i = minloc(abs(Temp - self%Temp), 1) delta_Temp = Temp - self%Temp(i) if (delta_Temp < 0 ) THEN i = i - 1 delta_Temp = Temp - self%Temp(i) end if Z = self%Z(i,j) + self%Z_k(i,j)*delta_Temp end if end subroutine getValueTableTtoZne end module moduleTableTtoZne