Release SPHLite 0.1.0 framework
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
module sph_inp_defs
|
||||
implicit none
|
||||
|
||||
integer, parameter :: SPH_INPUT_LINE_LEN = 1024
|
||||
integer, parameter :: SPH_INPUT_FIELD_LEN = 256
|
||||
integer, parameter :: SPH_INPUT_MAX_FIELDS = 64
|
||||
|
||||
integer, parameter :: SPH_KEY_NONE = 0
|
||||
integer, parameter :: SPH_KEY_NODE = 1
|
||||
integer, parameter :: SPH_KEY_ELEMENT = 2
|
||||
integer, parameter :: SPH_KEY_NSET = 3
|
||||
integer, parameter :: SPH_KEY_ELSET = 4
|
||||
integer, parameter :: SPH_KEY_MATERIAL = 5
|
||||
integer, parameter :: SPH_KEY_SECTION = 6
|
||||
integer, parameter :: SPH_KEY_SPHLITE = 7
|
||||
integer, parameter :: SPH_KEY_UNKNOWN = 99
|
||||
|
||||
end module sph_inp_defs
|
||||
@@ -0,0 +1,91 @@
|
||||
module sph_inp_helpers
|
||||
use sph_inp_defs, only: SPH_INPUT_LINE_LEN, SPH_KEY_NODE, SPH_KEY_ELEMENT, &
|
||||
SPH_KEY_NSET, SPH_KEY_ELSET, SPH_KEY_MATERIAL, SPH_KEY_SECTION, &
|
||||
SPH_KEY_SPHLITE, SPH_KEY_UNKNOWN
|
||||
implicit none
|
||||
|
||||
contains
|
||||
|
||||
function sph_inp_upper(line) result(out)
|
||||
character(len=*), intent(in) :: line
|
||||
character(len=len(line)) :: out
|
||||
integer :: i
|
||||
integer :: code
|
||||
|
||||
out = line
|
||||
do i = 1, len(out)
|
||||
code = ichar(out(i:i))
|
||||
if (code >= ichar('a') .and. code <= ichar('z')) then
|
||||
out(i:i) = char(code - ichar('a') + ichar('A'))
|
||||
end if
|
||||
end do
|
||||
end function sph_inp_upper
|
||||
|
||||
function sph_inp_clean_line(line) result(clean)
|
||||
character(len=*), intent(in) :: line
|
||||
character(len=SPH_INPUT_LINE_LEN) :: clean
|
||||
|
||||
clean = ' '
|
||||
if (len_trim(line) == 0) return
|
||||
if (line(1:1) == '!') return
|
||||
if (len_trim(line) >= 2) then
|
||||
if (line(1:2) == '**') return
|
||||
end if
|
||||
clean = adjustl(line)
|
||||
end function sph_inp_clean_line
|
||||
|
||||
subroutine sph_inp_split_csv(line, fields, nfields)
|
||||
character(len=*), intent(in) :: line
|
||||
character(len=*), intent(out) :: fields(:)
|
||||
integer, intent(out) :: nfields
|
||||
integer :: i
|
||||
integer :: start_pos
|
||||
integer :: n
|
||||
|
||||
fields = ' '
|
||||
nfields = 0
|
||||
n = len_trim(line)
|
||||
if (n <= 0) return
|
||||
|
||||
start_pos = 1
|
||||
do i = 1, n
|
||||
if (line(i:i) == ',') then
|
||||
if (nfields < size(fields)) then
|
||||
nfields = nfields + 1
|
||||
fields(nfields) = adjustl(line(start_pos:i-1))
|
||||
end if
|
||||
start_pos = i + 1
|
||||
end if
|
||||
end do
|
||||
|
||||
if (nfields < size(fields)) then
|
||||
nfields = nfields + 1
|
||||
fields(nfields) = adjustl(line(start_pos:n))
|
||||
end if
|
||||
end subroutine sph_inp_split_csv
|
||||
|
||||
function sph_inp_keyword_id(line) result(key_id)
|
||||
character(len=*), intent(in) :: line
|
||||
integer :: key_id
|
||||
character(len=len(line)) :: upper
|
||||
|
||||
upper = sph_inp_upper(adjustl(line))
|
||||
key_id = SPH_KEY_UNKNOWN
|
||||
if (index(upper, '*NODE') == 1) then
|
||||
key_id = SPH_KEY_NODE
|
||||
else if (index(upper, '*ELEMENT') == 1) then
|
||||
key_id = SPH_KEY_ELEMENT
|
||||
else if (index(upper, '*NSET') == 1) then
|
||||
key_id = SPH_KEY_NSET
|
||||
else if (index(upper, '*ELSET') == 1) then
|
||||
key_id = SPH_KEY_ELSET
|
||||
else if (index(upper, '*MATERIAL') == 1) then
|
||||
key_id = SPH_KEY_MATERIAL
|
||||
else if (index(upper, '*SPHLITE') == 1) then
|
||||
key_id = SPH_KEY_SPHLITE
|
||||
else if (index(upper, 'SECTION') > 0 .and. upper(1:1) == '*') then
|
||||
key_id = SPH_KEY_SECTION
|
||||
end if
|
||||
end function sph_inp_keyword_id
|
||||
|
||||
end module sph_inp_helpers
|
||||
@@ -0,0 +1,258 @@
|
||||
module sph_input_reader
|
||||
use sph_precision, only: rk, SPH_NDIM
|
||||
use sph_error, only: SPH_SUCCESS, SPH_ERROR_IO, SPH_ERROR_INPUT, sph_error_set
|
||||
use sph_control, only: sph_control_set_time
|
||||
use sph_database, only: npart, sph_db_allocate, sph_db_free, sph_db_set_node
|
||||
use sph_inp_defs, only: SPH_INPUT_LINE_LEN, SPH_INPUT_FIELD_LEN, &
|
||||
SPH_INPUT_MAX_FIELDS, SPH_KEY_NODE, SPH_KEY_ELEMENT, SPH_KEY_NSET, &
|
||||
SPH_KEY_ELSET, SPH_KEY_MATERIAL, SPH_KEY_SECTION, SPH_KEY_SPHLITE, &
|
||||
SPH_KEY_UNKNOWN
|
||||
use sph_inp_helpers, only: sph_inp_clean_line, sph_inp_split_csv, &
|
||||
sph_inp_keyword_id, sph_inp_upper
|
||||
implicit none
|
||||
|
||||
integer, parameter :: SPH_INPUT_UNIT = 31
|
||||
integer :: input_warning_count = 0
|
||||
logical :: input_has_material = .false.
|
||||
logical :: input_has_section = .false.
|
||||
logical :: input_has_time_control = .false.
|
||||
|
||||
contains
|
||||
|
||||
subroutine sph_input_reset()
|
||||
input_warning_count = 0
|
||||
input_has_material = .false.
|
||||
input_has_section = .false.
|
||||
input_has_time_control = .false.
|
||||
end subroutine sph_input_reset
|
||||
|
||||
subroutine sph_input_get_warning_count(count)
|
||||
integer, intent(out) :: count
|
||||
|
||||
count = input_warning_count
|
||||
end subroutine sph_input_get_warning_count
|
||||
|
||||
subroutine sph_input_read(filename, ierr)
|
||||
character(len=*), intent(in) :: filename
|
||||
integer, intent(out) :: ierr
|
||||
integer :: node_count
|
||||
|
||||
call sph_input_reset()
|
||||
call sph_input_scan(filename, node_count, ierr)
|
||||
if (ierr /= SPH_SUCCESS) return
|
||||
call sph_db_allocate(node_count, ierr)
|
||||
if (ierr /= SPH_SUCCESS) return
|
||||
call sph_input_fill_nodes(filename, node_count, ierr)
|
||||
end subroutine sph_input_read
|
||||
|
||||
subroutine sph_input_build_model(ierr)
|
||||
integer, intent(out) :: ierr
|
||||
|
||||
ierr = SPH_SUCCESS
|
||||
if (npart <= 0) then
|
||||
call sph_error_set(SPH_ERROR_INPUT, 'input is missing *NODE particle data')
|
||||
ierr = SPH_ERROR_INPUT
|
||||
return
|
||||
end if
|
||||
if (.not. input_has_material) then
|
||||
call sph_error_set(SPH_ERROR_INPUT, 'input is missing *MATERIAL data')
|
||||
ierr = SPH_ERROR_INPUT
|
||||
return
|
||||
end if
|
||||
if (.not. input_has_section) then
|
||||
call sph_error_set(SPH_ERROR_INPUT, 'input is missing *SECTION data')
|
||||
ierr = SPH_ERROR_INPUT
|
||||
return
|
||||
end if
|
||||
if (.not. input_has_time_control) then
|
||||
call sph_error_set(SPH_ERROR_INPUT, 'input is missing *SPHLITE time control data')
|
||||
ierr = SPH_ERROR_INPUT
|
||||
return
|
||||
end if
|
||||
end subroutine sph_input_build_model
|
||||
|
||||
subroutine sph_input_scan(filename, node_count, ierr)
|
||||
character(len=*), intent(in) :: filename
|
||||
integer, intent(out) :: node_count
|
||||
integer, intent(out) :: ierr
|
||||
character(len=SPH_INPUT_LINE_LEN) :: raw_line
|
||||
character(len=SPH_INPUT_LINE_LEN) :: line
|
||||
integer :: ios
|
||||
integer :: active_key
|
||||
integer :: key_id
|
||||
|
||||
ierr = SPH_SUCCESS
|
||||
node_count = 0
|
||||
active_key = SPH_KEY_UNKNOWN
|
||||
|
||||
open(unit=SPH_INPUT_UNIT, file=trim(filename), status='old', action='read', iostat=ios)
|
||||
if (ios /= 0) then
|
||||
call sph_error_set(SPH_ERROR_IO, 'failed to open input file')
|
||||
ierr = SPH_ERROR_IO
|
||||
return
|
||||
end if
|
||||
|
||||
do
|
||||
read(SPH_INPUT_UNIT, '(A)', iostat=ios) raw_line
|
||||
if (ios /= 0) exit
|
||||
line = sph_inp_clean_line(raw_line)
|
||||
if (len_trim(line) == 0) cycle
|
||||
|
||||
if (line(1:1) == '*') then
|
||||
key_id = sph_inp_keyword_id(line)
|
||||
active_key = key_id
|
||||
call sph_input_record_keyword(key_id, line)
|
||||
else if (active_key == SPH_KEY_NODE) then
|
||||
node_count = node_count + 1
|
||||
end if
|
||||
end do
|
||||
|
||||
close(unit=SPH_INPUT_UNIT)
|
||||
end subroutine sph_input_scan
|
||||
|
||||
subroutine sph_input_fill_nodes(filename, expected_nodes, ierr)
|
||||
character(len=*), intent(in) :: filename
|
||||
integer, intent(in) :: expected_nodes
|
||||
integer, intent(out) :: ierr
|
||||
character(len=SPH_INPUT_LINE_LEN) :: raw_line
|
||||
character(len=SPH_INPUT_LINE_LEN) :: line
|
||||
integer :: ios
|
||||
integer :: active_key
|
||||
integer :: key_id
|
||||
integer :: node_index
|
||||
|
||||
ierr = SPH_SUCCESS
|
||||
active_key = SPH_KEY_UNKNOWN
|
||||
node_index = 0
|
||||
|
||||
if (expected_nodes == 0) then
|
||||
call sph_db_free()
|
||||
return
|
||||
end if
|
||||
|
||||
open(unit=SPH_INPUT_UNIT, file=trim(filename), status='old', action='read', iostat=ios)
|
||||
if (ios /= 0) then
|
||||
call sph_error_set(SPH_ERROR_IO, 'failed to reopen input file')
|
||||
ierr = SPH_ERROR_IO
|
||||
return
|
||||
end if
|
||||
|
||||
do
|
||||
read(SPH_INPUT_UNIT, '(A)', iostat=ios) raw_line
|
||||
if (ios /= 0) exit
|
||||
line = sph_inp_clean_line(raw_line)
|
||||
if (len_trim(line) == 0) cycle
|
||||
|
||||
if (line(1:1) == '*') then
|
||||
key_id = sph_inp_keyword_id(line)
|
||||
active_key = key_id
|
||||
else if (active_key == SPH_KEY_NODE) then
|
||||
node_index = node_index + 1
|
||||
call sph_input_parse_node(line, node_index, ierr)
|
||||
if (ierr /= SPH_SUCCESS) then
|
||||
close(unit=SPH_INPUT_UNIT)
|
||||
return
|
||||
end if
|
||||
end if
|
||||
end do
|
||||
|
||||
close(unit=SPH_INPUT_UNIT)
|
||||
end subroutine sph_input_fill_nodes
|
||||
|
||||
subroutine sph_input_record_keyword(key_id, line)
|
||||
integer, intent(in) :: key_id
|
||||
character(len=*), intent(in) :: line
|
||||
|
||||
if (key_id == SPH_KEY_MATERIAL) then
|
||||
input_has_material = .true.
|
||||
else if (key_id == SPH_KEY_SECTION) then
|
||||
input_has_section = .true.
|
||||
else if (key_id == SPH_KEY_SPHLITE) then
|
||||
input_has_time_control = .true.
|
||||
call sph_input_parse_time_control(line)
|
||||
else if (key_id == SPH_KEY_UNKNOWN) then
|
||||
input_warning_count = input_warning_count + 1
|
||||
end if
|
||||
end subroutine sph_input_record_keyword
|
||||
|
||||
subroutine sph_input_parse_time_control(line)
|
||||
character(len=*), intent(in) :: line
|
||||
character(len=SPH_INPUT_FIELD_LEN) :: fields(SPH_INPUT_MAX_FIELDS)
|
||||
character(len=SPH_INPUT_FIELD_LEN) :: item
|
||||
character(len=SPH_INPUT_FIELD_LEN) :: key
|
||||
character(len=SPH_INPUT_FIELD_LEN) :: value
|
||||
integer :: nfields
|
||||
integer :: i
|
||||
integer :: eq_pos
|
||||
integer :: ios
|
||||
real(rk) :: dt
|
||||
real(rk) :: total_time
|
||||
integer :: steps
|
||||
|
||||
dt = -1.0_rk
|
||||
total_time = -1.0_rk
|
||||
steps = -1
|
||||
call sph_inp_split_csv(line, fields, nfields)
|
||||
do i = 1, nfields
|
||||
item = adjustl(fields(i))
|
||||
eq_pos = index(item, '=')
|
||||
if (eq_pos <= 0) cycle
|
||||
key = sph_inp_upper(adjustl(item(1:eq_pos-1)))
|
||||
value = adjustl(item(eq_pos+1:len_trim(item)))
|
||||
if (index(key, 'DT') == 1) then
|
||||
read(value, *, iostat=ios) dt
|
||||
else if (index(key, 'TIME') == 1 .or. index(key, 'END TIME') == 1) then
|
||||
read(value, *, iostat=ios) total_time
|
||||
else if (index(key, 'STEPS') == 1 .or. index(key, 'MAX STEPS') == 1) then
|
||||
read(value, *, iostat=ios) steps
|
||||
end if
|
||||
end do
|
||||
call sph_control_set_time(dt, total_time, steps)
|
||||
end subroutine sph_input_parse_time_control
|
||||
|
||||
subroutine sph_input_parse_node(line, node_index, ierr)
|
||||
character(len=*), intent(in) :: line
|
||||
integer, intent(in) :: node_index
|
||||
integer, intent(out) :: ierr
|
||||
character(len=SPH_INPUT_FIELD_LEN) :: fields(SPH_INPUT_MAX_FIELDS)
|
||||
integer :: nfields
|
||||
integer :: ext_id
|
||||
integer :: ios
|
||||
real(rk) :: coords(SPH_NDIM)
|
||||
|
||||
ierr = SPH_SUCCESS
|
||||
call sph_inp_split_csv(line, fields, nfields)
|
||||
if (nfields < 4) then
|
||||
call sph_error_set(SPH_ERROR_INPUT, 'invalid *NODE record')
|
||||
ierr = SPH_ERROR_INPUT
|
||||
return
|
||||
end if
|
||||
read(fields(1), *, iostat=ios) ext_id
|
||||
if (ios /= 0) then
|
||||
call sph_error_set(SPH_ERROR_INPUT, 'invalid node id')
|
||||
ierr = SPH_ERROR_INPUT
|
||||
return
|
||||
end if
|
||||
read(fields(2), *, iostat=ios) coords(1)
|
||||
if (ios /= 0) then
|
||||
call sph_error_set(SPH_ERROR_INPUT, 'invalid node x coordinate')
|
||||
ierr = SPH_ERROR_INPUT
|
||||
return
|
||||
end if
|
||||
read(fields(3), *, iostat=ios) coords(2)
|
||||
if (ios /= 0) then
|
||||
call sph_error_set(SPH_ERROR_INPUT, 'invalid node y coordinate')
|
||||
ierr = SPH_ERROR_INPUT
|
||||
return
|
||||
end if
|
||||
read(fields(4), *, iostat=ios) coords(3)
|
||||
if (ios /= 0) then
|
||||
call sph_error_set(SPH_ERROR_INPUT, 'invalid node z coordinate')
|
||||
ierr = SPH_ERROR_INPUT
|
||||
return
|
||||
end if
|
||||
|
||||
call sph_db_set_node(node_index, ext_id, coords, ierr)
|
||||
end subroutine sph_input_parse_node
|
||||
|
||||
end module sph_input_reader
|
||||
Reference in New Issue
Block a user