| 1 |
!----------------------------------------------------------------- |
| 2 |
! |
| 3 |
! This file is (or was) part of SPLASH, a visualisation tool |
| 4 |
! for Smoothed Particle Hydrodynamics written by Daniel Price: |
| 5 |
! |
| 6 |
! http://users.monash.edu.au/~dprice/splash |
| 7 |
! |
| 8 |
! SPLASH comes with ABSOLUTELY NO WARRANTY. |
| 9 |
! This is free software; and you are welcome to redistribute |
| 10 |
! it under the terms of the GNU General Public License |
| 11 |
! (see LICENSE file for details) and the provision that |
| 12 |
! this notice remains intact. If you modify this file, please |
| 13 |
! note section 2a) of the GPLv2 states that: |
| 14 |
! |
| 15 |
! a) You must cause the modified files to carry prominent notices |
| 16 |
! stating that you changed the files and the date of any change. |
| 17 |
! |
| 18 |
! Copyright (C) 2005-2020 Daniel Price. All rights reserved. |
| 19 |
! Contact: [email protected] |
| 20 |
! |
| 21 |
!----------------------------------------------------------------- |
| 22 |
|
| 23 |
!------------------------------------------------------------------------- |
| 24 |
! Module providing library version of splash read_data routines |
| 25 |
! specifies c interfaces to corresponding Fortran subroutines |
| 26 |
!------------------------------------------------------------------------- |
| 27 |
module libreaddata |
| 28 |
|
| 29 |
use readdata, only:select_data_format |
| 30 |
use getdata, only:get_data,get_labels |
| 31 |
use labels, only:label,unitslabel,headertags,shortstring,lenlabel,lenunitslabel |
| 32 |
use params, only:ltag |
| 33 |
use initialise, only:defaults_set_initial |
| 34 |
use iso_c_binding, only:c_float,c_int,c_bool,c_char,c_double |
| 35 |
use asciiutils, only:fstring,cstring |
| 36 |
use filenames, only:rootname, tagline, nfiles |
| 37 |
use particle_data, only:dat,maxpart,maxcol,maxstep,npartoftype,iamtype,headervals |
| 38 |
use settings_data, only:ncolumns,ivegotdata,required,iverbose |
| 39 |
use libutils, only:ctypes_to_fstring,check_argcv |
| 40 |
|
| 41 |
implicit none |
| 42 |
|
| 43 |
public |
| 44 |
|
| 45 |
contains |
| 46 |
subroutine check_argcv_c() bind(c, name='check_argcv') |
| 47 |
call check_argcv() |
| 48 |
end subroutine check_argcv_c |
| 49 |
|
| 50 |
subroutine get_labels_c(labels_out, ncol) bind(c) |
| 51 |
integer(c_int), intent(in) :: ncol |
| 52 |
character(kind=c_char), intent(out) :: labels_out(lenlabel, ncol) |
| 53 |
character(len=lenlabel) :: temp_string |
| 54 |
integer :: i,j |
| 55 |
|
| 56 |
call get_labels |
| 57 |
|
| 58 |
do i = 1, ncol |
| 59 |
temp_string = shortstring(label(i), unitslabel(i)) |
| 60 |
do j = 1, lenlabel |
| 61 |
if (j .le. len(temp_string)) then |
| 62 |
labels_out(j, i) = temp_string(j:j) |
| 63 |
else |
| 64 |
labels_out(j, i) = ' ' |
| 65 |
endif |
| 66 |
enddo |
| 67 |
end do |
| 68 |
|
| 69 |
end subroutine get_labels_c |
| 70 |
|
| 71 |
subroutine get_header_vals_size(taglength, vallength) bind(c) |
| 72 |
! Need to get the correct size of the header array to allocate memory in Python |
| 73 |
integer(c_int), intent(out) :: taglength, vallength |
| 74 |
! taglength is the size of the tag array |
| 75 |
! vallength is the length of the headerval array |
| 76 |
|
| 77 |
taglength = size(headertags) |
| 78 |
vallength = size(headervals) |
| 79 |
|
| 80 |
end subroutine get_header_vals_size |
| 81 |
|
| 82 |
subroutine get_headers(headertags_out, headervals_out, taglength, vallength) bind(c) |
| 83 |
integer(c_int), intent(in) :: taglength, vallength |
| 84 |
character(kind=c_char), intent(out) :: headertags_out(ltag, taglength) |
| 85 |
real(c_double), intent(out) :: headervals_out(vallength) |
| 86 |
integer :: i,j |
| 87 |
|
| 88 |
do i = 1, taglength |
| 89 |
do j = 1, ltag |
| 90 |
if (j .le. len(headertags(i))) then |
| 91 |
headertags_out(j, i) = headertags(i)(j:j) |
| 92 |
else |
| 93 |
headertags_out(j, i) = ' ' |
| 94 |
endif |
| 95 |
enddo |
| 96 |
enddo |
| 97 |
|
| 98 |
headervals_out(1:vallength) = headervals(1:vallength, 1) |
| 99 |
|
| 100 |
end subroutine get_headers |
| 101 |
|
| 102 |
subroutine read_data_c(filename,fileformat,f_length, ff_length,& |
| 103 |
sph_dat,npart,ncol,read_header,iverbose,ierr) bind(c, name='read_data') |
| 104 |
integer(c_int), intent(in) :: f_length, ff_length |
| 105 |
character(kind=c_char), intent(in) :: filename(f_length), fileformat(ff_length) |
| 106 |
integer(c_int), intent(inout) :: ncol, npart |
| 107 |
real(c_double), intent(out) :: sph_dat(npart,ncol) |
| 108 |
integer(c_int), intent(in) :: read_header, iverbose |
| 109 |
integer(c_int), intent(out) :: ierr |
| 110 |
|
| 111 |
character(len=ff_length) :: format_f |
| 112 |
|
| 113 |
integer :: ncolr,npartr |
| 114 |
|
| 115 |
ierr = 0 |
| 116 |
|
| 117 |
call defaults_set_initial |
| 118 |
|
| 119 |
! iverbose = verbose |
| 120 |
|
| 121 |
if (iverbose==1) print*, tagline |
| 122 |
|
| 123 |
if (read_header==1) required = .false. |
| 124 |
|
| 125 |
nfiles = 1 |
| 126 |
rootname(1) = ctypes_to_fstring(filename) |
| 127 |
format_f = ctypes_to_fstring(fileformat) |
| 128 |
|
| 129 |
if (iverbose==1) then |
| 130 |
print*, "Received file format f ", format_f |
| 131 |
print*, "Size of sph_dat is ", size(sph_dat) |
| 132 |
endif |
| 133 |
|
| 134 |
call select_data_format(format_f,ierr) |
| 135 |
|
| 136 |
if (ierr == 0) then |
| 137 |
call get_data(1,.true.,.true.,1) |
| 138 |
if (ivegotdata .and. maxpart>0) then |
| 139 |
npartr = sum(npartoftype(:,1)) |
| 140 |
ncolr = ncolumns |
| 141 |
if (ncol/=0 .or. npart/=0) then |
| 142 |
if (ncol/=ncolr+1 .or. npart/=npartr) then |
| 143 |
print*, "WARNING: Array size given in libread is not equal to read array." |
| 144 |
endif |
| 145 |
else if (ncol==0 .or. npart==0) then |
| 146 |
ncol = ncolr |
| 147 |
npart = npartr |
| 148 |
endif |
| 149 |
|
| 150 |
if (ncol > 0 .and. read_header/=1) then |
| 151 |
sph_dat(1:npart,1:ncol-1) = dat(1:npart,1:ncol-1,1) |
| 152 |
sph_dat(1:npart,ncol) = iamtype(1:npart,1) |
| 153 |
else |
| 154 |
if (iverbose==1) print*, "Updating values for npart and ncol." |
| 155 |
npart = sum(npartoftype(:,1)) |
| 156 |
ncol = ncolumns + 1 |
| 157 |
endif |
| 158 |
endif |
| 159 |
else |
| 160 |
print*, "Error in selecting the data format" |
| 161 |
endif |
| 162 |
|
| 163 |
end subroutine read_data_c |
| 164 |
|
| 165 |
end module libreaddata |