Extract element state allocation module

This commit is contained in:
shenw
2026-06-22 20:53:30 +08:00
parent 30c2525bc7
commit da3bdc691b
6 changed files with 192 additions and 56 deletions
+2 -2
View File
@@ -6,6 +6,7 @@ module fem_api
use fem_database
use fem_inp_reader
use fem_element_topology
use fem_element_state
use fem_element_mass
use fem_element_internal
use fem_coupling
@@ -467,8 +468,7 @@ contains
call error_set(FEM_ERR_INTERNAL, 'fem_get_element_average_state called before fem_initialize', ierr)
return
end if
if (.not. allocated(elem_gp_start) .or. .not. allocated(stress) .or. &
.not. allocated(strain)) then
if (.not. element_state_is_available()) then
call error_set(FEM_ERR_INTERNAL, 'element state is not available', ierr)
return
end if
+2 -50
View File
@@ -3,6 +3,7 @@ module fem_element_internal
use fem_error
use fem_database
use fem_element_topology
use fem_element_state
use fem_quadrature
use fem_shape_functions
use fem_material_j2
@@ -21,7 +22,7 @@ contains
return
end if
call ensure_element_state(ierr)
call element_state_ensure(ierr)
if (ierr .ne. FEM_SUCCESS) return
dt_local = ZERO
@@ -50,55 +51,6 @@ contains
ierr = FEM_SUCCESS
end subroutine element_compute_internal_force
subroutine ensure_element_state(ierr)
integer, intent(out) :: ierr
integer :: elem
integer :: gp
integer :: stat
if (allocated(elem_gp_start)) then
ierr = FEM_SUCCESS
return
end if
allocate(elem_gp_start(nelem), elem_ngp(nelem), stat=stat)
if (stat .ne. 0) then
call error_set(FEM_ERR_INTERNAL, 'failed to allocate element state map', ierr)
return
end if
elem_gp_start = 0
elem_ngp = 0
ngauss_total = 0
do elem = 1, nelem
elem_ngp(elem) = element_num_integration_points(elem_type(elem))
if (elem_ngp(elem) .gt. 0) then
elem_gp_start(elem) = ngauss_total + 1
ngauss_total = ngauss_total + elem_ngp(elem)
end if
end do
if (ngauss_total .gt. 0) then
allocate(stress(NSTRESS,ngauss_total), strain(NSTRESS,ngauss_total), &
statev(NSTATEV_MAX,ngauss_total), stat=stat)
if (stat .ne. 0) then
call error_set(FEM_ERR_INTERNAL, 'failed to allocate element state arrays', ierr)
return
end if
stress = ZERO
strain = ZERO
statev = ZERO
end if
do gp = 1, ngauss_total
statev(1,gp) = ZERO
end do
ierr = FEM_SUCCESS
end subroutine ensure_element_state
subroutine c3d4_internal_force(elem, dt, ierr)
integer, intent(in) :: elem
real(rk), intent(in) :: dt
+93
View File
@@ -0,0 +1,93 @@
module fem_element_state
use fem_precision
use fem_error
use fem_database
use fem_element_topology
implicit none
private
public :: element_state_ensure
public :: element_state_reset_values
public :: element_state_is_available
contains
logical function element_state_is_available()
element_state_is_available = .false.
if (.not. allocated(elem_gp_start)) return
if (.not. allocated(elem_ngp)) return
if (ngauss_total .le. 0) then
element_state_is_available = .true.
return
end if
if (.not. allocated(stress)) return
if (.not. allocated(strain)) return
if (.not. allocated(statev)) return
element_state_is_available = .true.
end function element_state_is_available
subroutine element_state_ensure(ierr)
integer, intent(out) :: ierr
integer :: elem
integer :: stat
if (allocated(elem_gp_start)) then
if (.not. element_state_is_available()) then
call error_set(FEM_ERR_INTERNAL, 'element state map exists but arrays are incomplete', ierr)
return
end if
ierr = FEM_SUCCESS
return
end if
if (nelem .lt. 0) then
call error_set(FEM_ERR_INTERNAL, 'negative element count in state allocation', ierr)
return
end if
allocate(elem_gp_start(nelem), elem_ngp(nelem), stat=stat)
if (stat .ne. 0) then
call error_set(FEM_ERR_INTERNAL, 'failed to allocate element state map', ierr)
return
end if
elem_gp_start = 0
elem_ngp = 0
ngauss_total = 0
do elem = 1, nelem
elem_ngp(elem) = element_num_integration_points(elem_type(elem))
if (elem_ngp(elem) .le. 0) then
call error_set(FEM_ERR_MESH, 'unsupported element type in state allocation', ierr)
return
end if
elem_gp_start(elem) = ngauss_total + 1
ngauss_total = ngauss_total + elem_ngp(elem)
end do
if (ngauss_total .gt. 0) then
allocate(stress(NSTRESS,ngauss_total), strain(NSTRESS,ngauss_total), &
statev(NSTATEV_MAX,ngauss_total), stat=stat)
if (stat .ne. 0) then
call error_set(FEM_ERR_INTERNAL, 'failed to allocate element state arrays', ierr)
return
end if
call element_state_reset_values(ierr)
if (ierr .ne. FEM_SUCCESS) return
end if
ierr = FEM_SUCCESS
end subroutine element_state_ensure
subroutine element_state_reset_values(ierr)
integer, intent(out) :: ierr
if (allocated(stress)) stress = ZERO
if (allocated(strain)) strain = ZERO
if (allocated(statev)) statev = ZERO
ierr = FEM_SUCCESS
end subroutine element_state_reset_values
end module fem_element_state
+2 -4
View File
@@ -3,6 +3,7 @@ module fem_output_csv
use fem_error
use fem_control
use fem_database
use fem_element_state
implicit none
character(len=256) :: csv_output_directory = '.'
@@ -261,10 +262,7 @@ contains
eqps = ZERO
damage = ZERO
if (.not. allocated(elem_gp_start)) return
if (.not. allocated(stress)) return
if (.not. allocated(strain)) return
if (.not. allocated(statev)) return
if (.not. element_state_is_available()) return
if (elem .lt. 1 .or. elem .gt. nelem) return
first_gp = elem_gp_start(elem)