<commit_before>module PPP_electron
    use constants_def, only: dp
    use conductivity_def

    implicit none
    ! error codes
    integer, parameter :: unable_to_load_table = -1
    integer, parameter :: unable_to_compute_interpolation = -2
    integer, parameter :: unable_to_write_cache = -3
    integer, parameter :: unable_to_evaluate_conductivity = -4
    integer, parameter :: table_is_not_loaded = 1
    integer, parameter, private :: NZ = 15, Nrho = 64, NT = 19
    integer, parameter, private :: conductivity_filename_length = 256
    character(len=conductivity_filename_length) :: conductivity_datadir    
    
    character(len=*), parameter :: tablename = 'condall06'
    
    type electron_conductivity_tbl
        logical :: is_loaded = .FALSE.
        integer :: linear_T
        integer :: linear_rho
        real(dp) :: Zmin
        real(dp) :: Zmax
        real(dp) :: rhomin
        real(dp) :: rhomax
        real(dp) :: Tmin
        real(dp) :: Tmax
        real(dp), dimension(NZ) :: lgZs
        real(dp), dimension(Nrho) :: lgrhos
        real(dp), dimension(NT) :: lgTs
        real(dp), dimension(NT,Nrho,NZ) :: lgK    ! table and coeff.
    end type electron_conductivity_tbl

    type(electron_conductivity_tbl), target, save :: PPP_tbl
    ! can't put this in the table because of the target attribute
    real(dp), dimension(4,NT,Nrho,NZ), target, private, save :: workspace

contains
    
    subroutine load_PPP_electron_table(datadir,ierr)
        use exceptions_lib
        implicit none
        character(len=*), intent(in) :: datadir
        integer, intent(out) :: ierr
        type(electron_conductivity_tbl), pointer :: tab
        character(len=*), parameter :: this_routine = 'load_PPP_electron_table'
        character(len=256) :: data_filename, cache_filename
        logical :: have_cache
        type(alert) :: status=alert(scope=this_routine)
        type(failure) :: read_table_error=failure(scope=this_routine, &
        &   message='unable to load table')
        type(failure) :: write_cache_error=failure(scope=this_routine, &
        &   message='unable to write cache')
        
        ierr = 0
        tab => PPP_tbl
        if (tab% is_loaded) then
            call status% report('table is already loaded')
            return
        end if
        
        cache_filename = &
        &   trim(datadir)//'/cache/'//trim(tablename)//'.bin'
        inquire(file=cache_filename,exist=have_cache)
        if (have_cache) then
            call read_PPP_electron_table_cache(cache_filename,tab,ierr)
            if (ierr == 0) then
                tab% is_loaded = .TRUE.
                return
            end if
        end if
        
        ! If we don't have the table cached, or cannot load it, then read the 
        ! datafile and write to cache
        ierr = 0
        data_filename =  &
        &   trim(datadir)//'/'//trim(tablename)//'.dat'
        call read_PPP_electron_table(data_filename,tab,ierr)
        if (read_table_error% raised(ierr)) then
            ierr = unable_to_load_table
            return
        end if
        tab% is_loaded = .TRUE.

        call write_PPP_electron_table_cache(cache_filename,tab,ierr)
        if (write_cache_error% raised(ierr)) then
            ierr = unable_to_write_cache
            return
        end if
    end subroutine load_PPP_electron_table
    
    subroutine free_PPP_electron_table()
        PPP_tbl% is_loaded = .FALSE.
    end subroutine free_PPP_electron_table

    subroutine construct_interpolation_coefficients(ierr)
        use interp_2d_lib_db, only: interp_mkbicub_db
        use exceptions_lib<commit_msg>Update PPP_electron.f<commit_after>module PPP_electron
    use constants_def, only: dp
    use conductivity_def

    implicit none
    ! error codes
    integer, parameter :: unable_to_load_table = -1
    integer, parameter :: unable_to_compute_interpolation = -2
    integer, parameter :: unable_to_write_cache = -3
    integer, parameter :: unable_to_evaluate_conductivity = -4
    integer, parameter :: table_is_not_loaded = 1
    integer, parameter, private :: NZ = 15, Nrho = 64, NT = 19
    integer, parameter, private :: conductivity_filename_length = 256
    character(len=conductivity_filename_length) :: conductivity_datadir    
    
    character(len=*), parameter :: tablename = 'condall06'
    
    type electron_conductivity_tbl
        logical :: is_loaded = .FALSE.
        integer :: linear_T
        integer :: linear_rho
        real(dp) :: Zmin
        real(dp) :: Zmax
        real(dp) :: rhomin
        real(dp) :: rhomax
        real(dp) :: Tmin
        real(dp) :: Tmax
        real(dp), dimension(NZ) :: lgZs
        real(dp), dimension(Nrho) :: lgrhos
        real(dp), dimension(NT) :: lgTs
        real(dp), dimension(NT,Nrho,NZ) :: lgK    ! table and coeff.
    end type electron_conductivity_tbl

    type(electron_conductivity_tbl), target, save :: PPP_tbl
    ! can't put this in the table because of the target attribute
    real(dp), dimension(4,NT,Nrho,NZ), target, private, save :: workspace

contains
    
    subroutine load_PPP_electron_table(datadir,ierr)
        use exceptions_lib
        implicit none
        character(len=*), intent(in) :: datadir
        integer, intent(out) :: ierr
        type(electron_conductivity_tbl), pointer :: tab
        character(len=*), parameter :: this_routine = 'load_PPP_electron_table'
        character(len=256) :: data_filename, cache_filename
        logical :: have_cache
        type(alert) :: status=alert(scope=this_routine)
        type(failure) :: read_table_error=failure(scope=this_routine, &
        &   message='unable to load table')
        type(failure) :: write_cache_error=failure(scope=this_routine, &
        &   message='unable to write cache')
        
        ierr = 0
        tab => PPP_tbl
        if (tab% is_loaded) then
            call status% report('table is already loaded')
            return
        end if
        
        cache_filename = trim(datadir)//'/cache/'//trim(tablename)//'.bin'
        inquire(file=cache_filename,exist=have_cache)
        if (have_cache) then
            call read_PPP_electron_table_cache(cache_filename,tab,ierr)
            if (ierr == 0) then
                tab% is_loaded = .TRUE.
                return
            end if
        end if
        
        ! If we don't have the table cached, or cannot load it, then read the 
        ! datafile and write to cache
        ierr = 0
        data_filename = trim(datadir)//'/'//trim(tablename)//'.dat'
        call read_PPP_electron_table(data_filename,tab,ierr)
        if (read_table_error% raised(ierr)) then
            ierr = unable_to_load_table
            return
        end if
        tab% is_loaded = .TRUE.

        call write_PPP_electron_table_cache(cache_filename,tab,ierr)
        if (write_cache_error% raised(ierr)) then
            ierr = unable_to_write_cache
            return
        end if
    end subroutine load_PPP_electron_table
    
    subroutine free_PPP_electron_table()
        PPP_tbl% is_loaded = .FALSE.
    end subroutine free_PPP_electron_table

    subroutine construct_interpolation_coefficients(ierr)
        use interp_2d_lib_db, only: interp_mkbicub_db
        use exceptions_lib
{'fraction_non_alphanumeric': 0.09050833448133351, 'fraction_numerical': 0.006612481057996969, 'mean_word_length': 2.4803451581975073, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3500156', 'n_tokens_mistral': 2277, 'n_tokens_neox': 2197, 'n_words': 512}