Files
SPHLite/src/database/sph_database.f90
T

508 lines
18 KiB
Fortran

module sph_database
use sph_precision, only: rk, SPH_NDIM
use sph_error, only: SPH_SUCCESS, SPH_ERROR_ARGUMENT, SPH_ERROR_DATABASE, sph_error_set
implicit none
integer, parameter :: SPH_NAME_LEN = 64
integer, parameter :: SPH_MAX_MATERIALS = 32
integer, parameter :: SPH_MAX_SECTIONS = 32
integer, parameter :: SPH_MAX_SETS = 32
integer, parameter :: SPH_MAX_REGIONS = 32
integer, parameter :: SPH_MAX_ELEMENTS = 256
integer, parameter :: SPH_MAX_ELEMENT_NODES = 8
integer, parameter :: SPH_PHASE_WATER = 1
integer, parameter :: SPH_PHASE_GAS = 2
integer, parameter :: SPH_PHASE_EXPLOSIVE = 3
integer, parameter :: SPH_PHASE_SOLID = 4
integer, parameter :: SPH_PHASE_SHELL = 5
integer, parameter :: SPH_PHASE_BOUNDARY = 6
integer, parameter :: SPH_PHASE_COUPLING = 7
integer, parameter :: SPH_PHASE_SOLID_FAILED = 8
integer, parameter :: SPH_PHASE_SHELL_FAILED = 9
integer, parameter :: SPH_STATUS_ACTIVE = 0
integer, parameter :: SPH_STATUS_INACTIVE = 1
integer, parameter :: SPH_STATUS_FAILED = 2
integer, parameter :: SPH_STATUS_RESERVED = 3
integer, parameter :: SPH_REGION_COUPLING_CONTACT = 1
integer, parameter :: SPH_REGION_COUPLING_CONTINUUM = 2
integer :: npart = 0
integer, allocatable :: particle_id_ext(:)
integer, allocatable :: particle_phase(:)
integer, allocatable :: particle_status(:)
integer, allocatable :: material_id(:)
integer, allocatable :: section_id(:)
real(rk), allocatable :: x0(:,:)
real(rk), allocatable :: x(:,:)
real(rk), allocatable :: v(:,:)
real(rk), allocatable :: a(:,:)
real(rk), allocatable :: vhalf(:,:)
real(rk), allocatable :: mass(:)
real(rk), allocatable :: rho(:)
real(rk), allocatable :: volume(:)
real(rk), allocatable :: pressure(:)
real(rk), allocatable :: internal_energy(:)
real(rk), allocatable :: temperature(:)
real(rk), allocatable :: h(:)
real(rk), allocatable :: drho_dt(:)
real(rk), allocatable :: acc_pressure(:,:)
real(rk), allocatable :: acc_viscosity(:,:)
real(rk), allocatable :: acc_body(:,:)
integer, allocatable :: particle_cell_id(:)
integer, allocatable :: cell_head(:)
integer, allocatable :: cell_next(:)
integer, allocatable :: neighbor_count(:)
integer, allocatable :: neighbor_offset(:)
integer, allocatable :: neighbor_list(:)
integer :: water_particle_count = 0
real(rk) :: water_mass = 0.0_rk
real(rk) :: water_momentum(SPH_NDIM) = 0.0_rk
real(rk) :: water_max_pressure = 0.0_rk
real(rk) :: water_min_density = 0.0_rk
real(rk) :: water_max_density = 0.0_rk
real(rk) :: water_pressure_impulse = 0.0_rk
real(rk) :: water_dt_cfl = 0.0_rk
real(rk) :: water_dt_accel = 0.0_rk
real(rk) :: water_dt_viscous = 0.0_rk
integer :: material_count = 0
character(len=SPH_NAME_LEN) :: material_name(SPH_MAX_MATERIALS)
character(len=SPH_NAME_LEN) :: material_type(SPH_MAX_MATERIALS)
integer :: material_internal_id(SPH_MAX_MATERIALS)
real(rk) :: material_density(SPH_MAX_MATERIALS)
integer :: section_count = 0
character(len=SPH_NAME_LEN) :: section_name(SPH_MAX_SECTIONS)
character(len=SPH_NAME_LEN) :: section_type(SPH_MAX_SECTIONS)
character(len=SPH_NAME_LEN) :: section_material_name(SPH_MAX_SECTIONS)
integer :: section_material_id(SPH_MAX_SECTIONS)
real(rk) :: section_thickness(SPH_MAX_SECTIONS)
integer :: section_integration_points(SPH_MAX_SECTIONS)
integer :: nset_count = 0
character(len=SPH_NAME_LEN) :: nset_name(SPH_MAX_SETS)
integer :: nset_member_count(SPH_MAX_SETS)
integer :: nset_first_member(SPH_MAX_SETS)
integer :: elset_count = 0
character(len=SPH_NAME_LEN) :: elset_name(SPH_MAX_SETS)
integer :: elset_member_count(SPH_MAX_SETS)
integer :: elset_first_member(SPH_MAX_SETS)
integer :: element_count = 0
integer :: element_id_ext(SPH_MAX_ELEMENTS)
character(len=SPH_NAME_LEN) :: element_type(SPH_MAX_ELEMENTS)
integer :: element_conn_count(SPH_MAX_ELEMENTS)
integer :: element_conn(SPH_MAX_ELEMENT_NODES, SPH_MAX_ELEMENTS)
integer :: coupling_region_count = 0
character(len=SPH_NAME_LEN) :: coupling_region_name(SPH_MAX_REGIONS)
character(len=SPH_NAME_LEN) :: coupling_region_set_name(SPH_MAX_REGIONS)
integer :: coupling_region_type(SPH_MAX_REGIONS)
integer :: coupling_region_internal_id(SPH_MAX_REGIONS)
contains
subroutine sph_db_allocate(count, ierr)
integer, intent(in) :: count
integer, intent(out) :: ierr
integer :: stat
ierr = SPH_SUCCESS
if (count < 0) then
call sph_error_set(SPH_ERROR_ARGUMENT, 'particle count must be non-negative')
ierr = SPH_ERROR_ARGUMENT
return
end if
call sph_db_free()
npart = count
if (count == 0) return
allocate(particle_id_ext(count), particle_phase(count), particle_status(count), &
material_id(count), section_id(count), x0(SPH_NDIM,count), &
x(SPH_NDIM,count), v(SPH_NDIM,count), a(SPH_NDIM,count), &
vhalf(SPH_NDIM,count), mass(count), rho(count), volume(count), &
pressure(count), internal_energy(count), temperature(count), &
h(count), drho_dt(count), acc_pressure(SPH_NDIM,count), &
acc_viscosity(SPH_NDIM,count), acc_body(SPH_NDIM,count), &
particle_cell_id(count), cell_next(count), neighbor_count(count), &
neighbor_offset(count), stat=stat)
if (stat /= 0) then
call sph_db_free()
call sph_error_set(SPH_ERROR_DATABASE, 'failed to allocate particle database')
ierr = SPH_ERROR_DATABASE
return
end if
call sph_db_initialize_defaults()
end subroutine sph_db_allocate
subroutine sph_db_initialize_defaults()
integer :: i
if (npart <= 0) return
do i = 1, npart
particle_id_ext(i) = i
particle_phase(i) = SPH_PHASE_SOLID
particle_status(i) = SPH_STATUS_ACTIVE
material_id(i) = 1
section_id(i) = 1
x0(:,i) = 0.0_rk
x(:,i) = 0.0_rk
v(:,i) = 0.0_rk
a(:,i) = 0.0_rk
vhalf(:,i) = 0.0_rk
mass(i) = 1.0_rk
rho(i) = 1000.0_rk
volume(i) = 1.0_rk
pressure(i) = 0.0_rk
internal_energy(i) = 0.0_rk
temperature(i) = 293.15_rk
h(i) = 1.0_rk
drho_dt(i) = 0.0_rk
acc_pressure(:,i) = 0.0_rk
acc_viscosity(:,i) = 0.0_rk
acc_body(:,i) = 0.0_rk
particle_cell_id(i) = 0
cell_next(i) = 0
neighbor_count(i) = 0
neighbor_offset(i) = 0
end do
call sph_db_clear_water_diagnostics()
end subroutine sph_db_initialize_defaults
subroutine sph_db_free()
if (allocated(particle_id_ext)) deallocate(particle_id_ext)
if (allocated(particle_phase)) deallocate(particle_phase)
if (allocated(particle_status)) deallocate(particle_status)
if (allocated(material_id)) deallocate(material_id)
if (allocated(section_id)) deallocate(section_id)
if (allocated(x0)) deallocate(x0)
if (allocated(x)) deallocate(x)
if (allocated(v)) deallocate(v)
if (allocated(a)) deallocate(a)
if (allocated(vhalf)) deallocate(vhalf)
if (allocated(mass)) deallocate(mass)
if (allocated(rho)) deallocate(rho)
if (allocated(volume)) deallocate(volume)
if (allocated(pressure)) deallocate(pressure)
if (allocated(internal_energy)) deallocate(internal_energy)
if (allocated(temperature)) deallocate(temperature)
if (allocated(h)) deallocate(h)
if (allocated(drho_dt)) deallocate(drho_dt)
if (allocated(acc_pressure)) deallocate(acc_pressure)
if (allocated(acc_viscosity)) deallocate(acc_viscosity)
if (allocated(acc_body)) deallocate(acc_body)
if (allocated(particle_cell_id)) deallocate(particle_cell_id)
if (allocated(cell_head)) deallocate(cell_head)
if (allocated(cell_next)) deallocate(cell_next)
if (allocated(neighbor_count)) deallocate(neighbor_count)
if (allocated(neighbor_offset)) deallocate(neighbor_offset)
if (allocated(neighbor_list)) deallocate(neighbor_list)
npart = 0
call sph_db_clear_metadata()
call sph_db_clear_water_diagnostics()
end subroutine sph_db_free
subroutine sph_db_clear_metadata()
material_count = 0
material_name = ' '
material_type = ' '
material_internal_id = 0
material_density = 0.0_rk
section_count = 0
section_name = ' '
section_type = ' '
section_material_name = ' '
section_material_id = 0
section_thickness = 0.0_rk
section_integration_points = 0
nset_count = 0
nset_name = ' '
nset_member_count = 0
nset_first_member = 0
elset_count = 0
elset_name = ' '
elset_member_count = 0
elset_first_member = 0
element_count = 0
element_id_ext = 0
element_type = ' '
element_conn_count = 0
element_conn = 0
coupling_region_count = 0
coupling_region_name = ' '
coupling_region_set_name = ' '
coupling_region_type = 0
coupling_region_internal_id = 0
end subroutine sph_db_clear_metadata
subroutine sph_db_clear_water_diagnostics()
water_particle_count = 0
water_mass = 0.0_rk
water_momentum = 0.0_rk
water_max_pressure = 0.0_rk
water_min_density = 0.0_rk
water_max_density = 0.0_rk
water_pressure_impulse = 0.0_rk
water_dt_cfl = 0.0_rk
water_dt_accel = 0.0_rk
water_dt_viscous = 0.0_rk
end subroutine sph_db_clear_water_diagnostics
subroutine sph_db_allocate_neighbor_storage(max_links, num_cells, ierr)
integer, intent(in) :: max_links
integer, intent(in) :: num_cells
integer, intent(out) :: ierr
integer :: stat
ierr = SPH_SUCCESS
if (max_links < 0 .or. num_cells < 0) then
call sph_error_set(SPH_ERROR_ARGUMENT, 'neighbor storage sizes must be non-negative')
ierr = SPH_ERROR_ARGUMENT
return
end if
if (allocated(neighbor_list)) deallocate(neighbor_list)
if (allocated(cell_head)) deallocate(cell_head)
allocate(neighbor_list(max_links), cell_head(num_cells), stat=stat)
if (stat /= 0) then
call sph_error_set(SPH_ERROR_DATABASE, 'failed to allocate neighbor storage')
ierr = SPH_ERROR_DATABASE
return
end if
if (max_links > 0) neighbor_list = 0
if (num_cells > 0) cell_head = 0
end subroutine sph_db_allocate_neighbor_storage
subroutine sph_db_get_particle_count(count, ierr)
integer, intent(out) :: count
integer, intent(out) :: ierr
count = npart
ierr = SPH_SUCCESS
end subroutine sph_db_get_particle_count
subroutine sph_db_get_particle_state_capacity(count, ndim, ierr)
integer, intent(out) :: count
integer, intent(out) :: ndim
integer, intent(out) :: ierr
count = npart
ndim = SPH_NDIM
ierr = SPH_SUCCESS
end subroutine sph_db_get_particle_state_capacity
subroutine sph_db_get_particle_state(max_particles, particle_id_ext_out, &
phase_out, status_out, material_id_out, section_id_out, x_out, v_out, &
a_out, rho_out, pressure_out, internal_energy_out, temperature_out, &
h_out, count, ierr)
integer, intent(in) :: max_particles
integer, intent(out) :: particle_id_ext_out(max_particles)
integer, intent(out) :: phase_out(max_particles)
integer, intent(out) :: status_out(max_particles)
integer, intent(out) :: material_id_out(max_particles)
integer, intent(out) :: section_id_out(max_particles)
real(rk), intent(out) :: x_out(SPH_NDIM,max_particles)
real(rk), intent(out) :: v_out(SPH_NDIM,max_particles)
real(rk), intent(out) :: a_out(SPH_NDIM,max_particles)
real(rk), intent(out) :: rho_out(max_particles)
real(rk), intent(out) :: pressure_out(max_particles)
real(rk), intent(out) :: internal_energy_out(max_particles)
real(rk), intent(out) :: temperature_out(max_particles)
real(rk), intent(out) :: h_out(max_particles)
integer, intent(out) :: count
integer, intent(out) :: ierr
integer :: i
ierr = SPH_SUCCESS
count = npart
if (max_particles < npart) then
call sph_error_set(SPH_ERROR_ARGUMENT, 'particle state buffer is too small')
ierr = SPH_ERROR_ARGUMENT
return
end if
do i = 1, npart
particle_id_ext_out(i) = particle_id_ext(i)
phase_out(i) = particle_phase(i)
status_out(i) = particle_status(i)
material_id_out(i) = material_id(i)
section_id_out(i) = section_id(i)
x_out(:,i) = x(:,i)
v_out(:,i) = v(:,i)
a_out(:,i) = a(:,i)
rho_out(i) = rho(i)
pressure_out(i) = pressure(i)
internal_energy_out(i) = internal_energy(i)
temperature_out(i) = temperature(i)
h_out(i) = h(i)
end do
end subroutine sph_db_get_particle_state
subroutine sph_db_set_node(index, ext_id, coords, ierr)
integer, intent(in) :: index
integer, intent(in) :: ext_id
real(rk), intent(in) :: coords(SPH_NDIM)
integer, intent(out) :: ierr
ierr = SPH_SUCCESS
if (index < 1 .or. index > npart) then
call sph_error_set(SPH_ERROR_ARGUMENT, 'particle index is out of range')
ierr = SPH_ERROR_ARGUMENT
return
end if
particle_id_ext(index) = ext_id
x0(:,index) = coords(:)
x(:,index) = coords(:)
end subroutine sph_db_set_node
subroutine sph_db_add_material(name, mtype, ierr)
character(len=*), intent(in) :: name
character(len=*), intent(in) :: mtype
integer, intent(out) :: ierr
ierr = SPH_SUCCESS
if (material_count >= SPH_MAX_MATERIALS) then
call sph_error_set(SPH_ERROR_DATABASE, 'too many material records')
ierr = SPH_ERROR_DATABASE
return
end if
material_count = material_count + 1
material_name(material_count) = name
material_type(material_count) = mtype
material_internal_id(material_count) = material_count
material_density(material_count) = 0.0_rk
end subroutine sph_db_add_material
subroutine sph_db_set_material_density(name, density_value, ierr)
character(len=*), intent(in) :: name
real(rk), intent(in) :: density_value
integer, intent(out) :: ierr
integer :: i
ierr = SPH_SUCCESS
if (density_value <= 0.0_rk) then
call sph_error_set(SPH_ERROR_ARGUMENT, 'material density must be positive')
ierr = SPH_ERROR_ARGUMENT
return
end if
do i = 1, material_count
if (trim(material_name(i)) == trim(name)) then
material_density(i) = density_value
return
end if
end do
call sph_error_set(SPH_ERROR_DATABASE, 'density references unknown material')
ierr = SPH_ERROR_DATABASE
end subroutine sph_db_set_material_density
subroutine sph_db_add_section(name, stype, mat_name, thickness, points, ierr)
character(len=*), intent(in) :: name
character(len=*), intent(in) :: stype
character(len=*), intent(in) :: mat_name
real(rk), intent(in) :: thickness
integer, intent(in) :: points
integer, intent(out) :: ierr
ierr = SPH_SUCCESS
if (section_count >= SPH_MAX_SECTIONS) then
call sph_error_set(SPH_ERROR_DATABASE, 'too many section records')
ierr = SPH_ERROR_DATABASE
return
end if
section_count = section_count + 1
section_name(section_count) = name
section_type(section_count) = stype
section_material_name(section_count) = mat_name
section_material_id(section_count) = 1
section_thickness(section_count) = thickness
section_integration_points(section_count) = points
end subroutine sph_db_add_section
subroutine sph_db_add_nset(name, member_count, first_member, ierr)
character(len=*), intent(in) :: name
integer, intent(in) :: member_count
integer, intent(in) :: first_member
integer, intent(out) :: ierr
ierr = SPH_SUCCESS
if (nset_count >= SPH_MAX_SETS) then
call sph_error_set(SPH_ERROR_DATABASE, 'too many node set records')
ierr = SPH_ERROR_DATABASE
return
end if
nset_count = nset_count + 1
nset_name(nset_count) = name
nset_member_count(nset_count) = member_count
nset_first_member(nset_count) = first_member
end subroutine sph_db_add_nset
subroutine sph_db_add_elset(name, member_count, first_member, ierr)
character(len=*), intent(in) :: name
integer, intent(in) :: member_count
integer, intent(in) :: first_member
integer, intent(out) :: ierr
ierr = SPH_SUCCESS
if (elset_count >= SPH_MAX_SETS) then
call sph_error_set(SPH_ERROR_DATABASE, 'too many element set records')
ierr = SPH_ERROR_DATABASE
return
end if
elset_count = elset_count + 1
elset_name(elset_count) = name
elset_member_count(elset_count) = member_count
elset_first_member(elset_count) = first_member
end subroutine sph_db_add_elset
subroutine sph_db_add_element(ext_id, etype, conn_count, conn_values, ierr)
integer, intent(in) :: ext_id
character(len=*), intent(in) :: etype
integer, intent(in) :: conn_count
integer, intent(in) :: conn_values(SPH_MAX_ELEMENT_NODES)
integer, intent(out) :: ierr
integer :: n
ierr = SPH_SUCCESS
if (element_count >= SPH_MAX_ELEMENTS) then
call sph_error_set(SPH_ERROR_DATABASE, 'too many element records')
ierr = SPH_ERROR_DATABASE
return
end if
element_count = element_count + 1
element_id_ext(element_count) = ext_id
element_type(element_count) = etype
n = conn_count
if (n > SPH_MAX_ELEMENT_NODES) n = SPH_MAX_ELEMENT_NODES
if (n < 0) n = 0
element_conn_count(element_count) = n
element_conn(:,element_count) = 0
if (n > 0) element_conn(1:n,element_count) = conn_values(1:n)
end subroutine sph_db_add_element
subroutine sph_db_add_coupling_region(name, set_name, region_type, ierr)
character(len=*), intent(in) :: name
character(len=*), intent(in) :: set_name
integer, intent(in) :: region_type
integer, intent(out) :: ierr
ierr = SPH_SUCCESS
if (coupling_region_count >= SPH_MAX_REGIONS) then
call sph_error_set(SPH_ERROR_DATABASE, 'too many coupling region records')
ierr = SPH_ERROR_DATABASE
return
end if
coupling_region_count = coupling_region_count + 1
coupling_region_name(coupling_region_count) = name
coupling_region_set_name(coupling_region_count) = set_name
coupling_region_type(coupling_region_count) = region_type
coupling_region_internal_id(coupling_region_count) = coupling_region_count
end subroutine sph_db_add_coupling_region
end module sph_database