Release SPHLite 0.1.0 framework

This commit is contained in:
Codex
2026-07-02 18:47:38 +08:00
commit ad31c2d5cc
54 changed files with 4710 additions and 0 deletions
+258
View File
@@ -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