Add CCLite 0.2.1 contact input parsing

This commit is contained in:
Codex
2026-07-05 23:45:25 +08:00
parent 4a2d30d81d
commit a234e33248
9 changed files with 567 additions and 26 deletions
+35
View File
@@ -5,6 +5,8 @@ module ccl_case_types
integer, parameter :: CCL_TEXT_LEN = 256
integer, parameter :: CCL_SOLVER_SPH = 1
integer, parameter :: CCL_SOLVER_FEM = 2
integer, parameter :: CCL_MAX_SURFACE_INTERACTIONS = 16
integer, parameter :: CCL_MAX_CONTACT_PAIRS = 16
type :: CCL_Solver_Config
logical :: is_set = .false.
@@ -43,6 +45,28 @@ module ccl_case_types
logical :: log = .true.
end type CCL_Output_Config
type :: CCL_Surface_Interaction_Config
logical :: is_set = .false.
integer :: line_no = 0
character(len=CCL_TEXT_LEN) :: name = ""
character(len=32) :: normal_model = ""
real(8) :: kn = 0.0d0
real(8) :: cn = 0.0d0
real(8) :: friction = 0.0d0
logical :: has_kn = .false.
logical :: has_cn = .false.
logical :: has_friction = .false.
end type CCL_Surface_Interaction_Config
type :: CCL_Contact_Pair_Config
logical :: is_set = .false.
integer :: line_no = 0
character(len=CCL_TEXT_LEN) :: name = ""
character(len=CCL_TEXT_LEN) :: interface_name = ""
character(len=CCL_TEXT_LEN) :: interaction_name = ""
character(len=64) :: contact_type = ""
end type CCL_Contact_Pair_Config
type :: CCL_Case_Type
logical :: has_control = .false.
character(len=CCL_TEXT_LEN) :: name = ""
@@ -53,6 +77,10 @@ module ccl_case_types
type(CCL_Time_Config) :: time
type(CCL_Interface_Config) :: interface
type(CCL_Output_Config) :: output
integer :: num_surface_interactions = 0
integer :: num_contact_pairs = 0
type(CCL_Surface_Interaction_Config) :: surface_interaction(CCL_MAX_SURFACE_INTERACTIONS)
type(CCL_Contact_Pair_Config) :: contact_pair(CCL_MAX_CONTACT_PAIRS)
end type CCL_Case_Type
contains
@@ -91,6 +119,13 @@ contains
call ccl_log_info("case", trim(msg))
call ccl_log_info("case", "interface=" // trim(ccl_case%interface%name))
call ccl_log_info("case", "output=" // trim(ccl_case%output%directory))
write(msg, '(a,i0)') "surface interactions=", ccl_case%num_surface_interactions
call ccl_log_info("case", trim(msg))
write(msg, '(a,i0)') "contact pairs=", ccl_case%num_contact_pairs
call ccl_log_info("case", trim(msg))
if (ccl_case%num_contact_pairs > 0) then
call ccl_log_info("case", "0.2.x contact model=NORMAL:PENALTY, friction disabled")
end if
end subroutine ccl_case_print_summary
end module ccl_case_types
+304 -14
View File
@@ -1,5 +1,7 @@
module ccl_inp_parser
use ccl_case_types, only: CCL_Case_Type, CCL_SOLVER_FEM, CCL_SOLVER_SPH, ccl_case_init, ccl_solver_slot
use, intrinsic :: ieee_arithmetic, only: ieee_is_finite
use ccl_case_types, only: CCL_Case_Type, CCL_MAX_CONTACT_PAIRS, CCL_MAX_SURFACE_INTERACTIONS, &
CCL_SOLVER_FEM, CCL_SOLVER_SPH, ccl_case_init, ccl_solver_slot
use ccl_errors, only: CCL_SUCCESS, CCL_ERROR_INPUT_INVALID_VALUE, &
CCL_ERROR_INPUT_MISSING_ARGUMENT, CCL_ERROR_INPUT_PARSE_FAILED
use ccl_string_utils, only: ccl_parse_bool, ccl_strip_quotes, ccl_to_upper
@@ -17,6 +19,8 @@ contains
integer :: ios
integer :: line_no
integer :: current_solver
integer :: current_interaction
integer :: current_pair
character(len=1024) :: line
character(len=64) :: section
@@ -25,6 +29,8 @@ contains
errmsg = ""
section = ""
current_solver = 0
current_interaction = 0
current_pair = 0
line_no = 0
open(newunit=unit_id, file=trim(filename), status="old", action="read", iostat=ios)
@@ -38,7 +44,8 @@ contains
read(unit_id, '(a)', iostat=ios) line
if (ios /= 0) exit
line_no = line_no + 1
call ccl_parse_line(line, line_no, ccl_case, section, current_solver, ierr, errmsg)
call ccl_parse_line(line, line_no, ccl_case, section, current_solver, current_interaction, &
current_pair, ierr, errmsg)
if (ierr /= CCL_SUCCESS) exit
end do
@@ -48,12 +55,15 @@ contains
call ccl_validate_case(ccl_case, ierr, errmsg)
end subroutine ccl_case_read
subroutine ccl_parse_line(line, line_no, ccl_case, section, current_solver, ierr, errmsg)
subroutine ccl_parse_line(line, line_no, ccl_case, section, current_solver, current_interaction, &
current_pair, ierr, errmsg)
character(len=*), intent(in) :: line
integer, intent(in) :: line_no
type(CCL_Case_Type), intent(inout) :: ccl_case
character(len=*), intent(inout) :: section
integer, intent(inout) :: current_solver
integer, intent(inout) :: current_interaction
integer, intent(inout) :: current_pair
integer, intent(out) :: ierr
character(len=*), intent(out) :: errmsg
@@ -71,18 +81,23 @@ contains
first_nonblank = 1
if (clean_line(first_nonblank:first_nonblank) == "*") then
call parse_keyword_line(clean_line, line_no, ccl_case, section, current_solver, ierr, errmsg)
call parse_keyword_line(clean_line, line_no, ccl_case, section, current_solver, current_interaction, &
current_pair, ierr, errmsg)
else
call parse_param_list(clean_line, line_no, section, ccl_case, current_solver, ierr, errmsg)
call parse_param_list(clean_line, line_no, section, ccl_case, current_solver, current_interaction, &
current_pair, ierr, errmsg)
end if
end subroutine ccl_parse_line
subroutine parse_keyword_line(line, line_no, ccl_case, section, current_solver, ierr, errmsg)
subroutine parse_keyword_line(line, line_no, ccl_case, section, current_solver, current_interaction, &
current_pair, ierr, errmsg)
character(len=*), intent(in) :: line
integer, intent(in) :: line_no
type(CCL_Case_Type), intent(inout) :: ccl_case
character(len=*), intent(inout) :: section
integer, intent(inout) :: current_solver
integer, intent(inout) :: current_interaction
integer, intent(inout) :: current_pair
integer, intent(out) :: ierr
character(len=*), intent(out) :: errmsg
@@ -103,6 +118,8 @@ contains
section = trim(keyword)
current_solver = 0
current_interaction = 0
current_pair = 0
select case (trim(section))
case ("CCLITE CONTROL")
@@ -117,8 +134,12 @@ contains
ccl_case%interface%is_set = .true.
case ("CCLITE OUTPUT")
ccl_case%output%is_set = .true.
case ("CCLITE SURFACE INTERACTION", "CCLITE CONTACT PAIR")
continue
case ("CCLITE SURFACE INTERACTION")
call begin_surface_interaction(ccl_case, line_no, current_interaction, ierr, errmsg)
if (ierr /= CCL_SUCCESS) return
case ("CCLITE CONTACT PAIR")
call begin_contact_pair(ccl_case, line_no, current_pair, ierr, errmsg)
if (ierr /= CCL_SUCCESS) return
case default
ierr = CCL_ERROR_INPUT_PARSE_FAILED
write(errmsg, '(a,i0,a,a)') "line ", line_no, ": unsupported keyword *", trim(section)
@@ -126,16 +147,20 @@ contains
end select
if (len_trim(params) > 0) then
call parse_param_list(params, line_no, section, ccl_case, current_solver, ierr, errmsg)
call parse_param_list(params, line_no, section, ccl_case, current_solver, current_interaction, &
current_pair, ierr, errmsg)
end if
end subroutine parse_keyword_line
subroutine parse_param_list(text, line_no, section, ccl_case, current_solver, ierr, errmsg)
subroutine parse_param_list(text, line_no, section, ccl_case, current_solver, current_interaction, &
current_pair, ierr, errmsg)
character(len=*), intent(in) :: text
integer, intent(in) :: line_no
character(len=*), intent(in) :: section
type(CCL_Case_Type), intent(inout) :: ccl_case
integer, intent(inout) :: current_solver
integer, intent(inout) :: current_interaction
integer, intent(inout) :: current_pair
integer, intent(out) :: ierr
character(len=*), intent(out) :: errmsg
@@ -172,18 +197,22 @@ contains
key = ccl_to_upper(token(:eq_pos-1))
value = ccl_strip_quotes(adjustl(token(eq_pos+1:)))
call apply_param(section, trim(key), trim(value), line_no, ccl_case, current_solver, ierr, errmsg)
call apply_param(section, trim(key), trim(value), line_no, ccl_case, current_solver, &
current_interaction, current_pair, ierr, errmsg)
if (ierr /= CCL_SUCCESS) return
end do
end subroutine parse_param_list
subroutine apply_param(section, key, value, line_no, ccl_case, current_solver, ierr, errmsg)
subroutine apply_param(section, key, value, line_no, ccl_case, current_solver, current_interaction, &
current_pair, ierr, errmsg)
character(len=*), intent(in) :: section
character(len=*), intent(in) :: key
character(len=*), intent(in) :: value
integer, intent(in) :: line_no
type(CCL_Case_Type), intent(inout) :: ccl_case
integer, intent(inout) :: current_solver
integer, intent(inout) :: current_interaction
integer, intent(inout) :: current_pair
integer, intent(out) :: ierr
character(len=*), intent(out) :: errmsg
@@ -222,8 +251,20 @@ contains
call apply_interface_param(key, value, ccl_case, ierr, errmsg)
case ("CCLITE OUTPUT")
call apply_output_param(key, value, ccl_case, ierr, errmsg)
case ("CCLITE SURFACE INTERACTION", "CCLITE CONTACT PAIR")
continue
case ("CCLITE SURFACE INTERACTION")
if (current_interaction <= 0) then
ierr = CCL_ERROR_INPUT_PARSE_FAILED
errmsg = "SURFACE INTERACTION data must follow *CCLITE SURFACE INTERACTION"
else
call apply_surface_interaction_param(key, value, ccl_case, current_interaction, ierr, errmsg)
end if
case ("CCLITE CONTACT PAIR")
if (current_pair <= 0) then
ierr = CCL_ERROR_INPUT_PARSE_FAILED
errmsg = "CONTACT PAIR data must follow *CCLITE CONTACT PAIR"
else
call apply_contact_pair_param(key, value, ccl_case, current_pair, ierr, errmsg)
end if
case default
ierr = CCL_ERROR_INPUT_PARSE_FAILED
errmsg = "parameters found before a supported CCLite keyword"
@@ -234,6 +275,50 @@ contains
end if
end subroutine apply_param
subroutine begin_surface_interaction(ccl_case, line_no, current_interaction, ierr, errmsg)
type(CCL_Case_Type), intent(inout) :: ccl_case
integer, intent(in) :: line_no
integer, intent(out) :: current_interaction
integer, intent(out) :: ierr
character(len=*), intent(out) :: errmsg
ierr = CCL_SUCCESS
errmsg = ""
current_interaction = 0
if (ccl_case%num_surface_interactions >= CCL_MAX_SURFACE_INTERACTIONS) then
ierr = CCL_ERROR_INPUT_INVALID_VALUE
write(errmsg, '(a,i0,a,i0)') "line ", line_no, ": too many surface interactions, max=", &
CCL_MAX_SURFACE_INTERACTIONS
return
end if
ccl_case%num_surface_interactions = ccl_case%num_surface_interactions + 1
current_interaction = ccl_case%num_surface_interactions
ccl_case%surface_interaction(current_interaction)%is_set = .true.
ccl_case%surface_interaction(current_interaction)%line_no = line_no
end subroutine begin_surface_interaction
subroutine begin_contact_pair(ccl_case, line_no, current_pair, ierr, errmsg)
type(CCL_Case_Type), intent(inout) :: ccl_case
integer, intent(in) :: line_no
integer, intent(out) :: current_pair
integer, intent(out) :: ierr
character(len=*), intent(out) :: errmsg
ierr = CCL_SUCCESS
errmsg = ""
current_pair = 0
if (ccl_case%num_contact_pairs >= CCL_MAX_CONTACT_PAIRS) then
ierr = CCL_ERROR_INPUT_INVALID_VALUE
write(errmsg, '(a,i0,a,i0)') "line ", line_no, ": too many contact pairs, max=", &
CCL_MAX_CONTACT_PAIRS
return
end if
ccl_case%num_contact_pairs = ccl_case%num_contact_pairs + 1
current_pair = ccl_case%num_contact_pairs
ccl_case%contact_pair(current_pair)%is_set = .true.
ccl_case%contact_pair(current_pair)%line_no = line_no
end subroutine begin_contact_pair
subroutine apply_control_param(key, value, ccl_case, ierr, errmsg)
character(len=*), intent(in) :: key
character(len=*), intent(in) :: value
@@ -378,6 +463,73 @@ contains
if (ierr /= CCL_SUCCESS) errmsg = "OUTPUT boolean must be YES or NO"
end subroutine apply_output_param
subroutine apply_surface_interaction_param(key, value, ccl_case, interaction_id, ierr, errmsg)
character(len=*), intent(in) :: key
character(len=*), intent(in) :: value
type(CCL_Case_Type), intent(inout) :: ccl_case
integer, intent(in) :: interaction_id
integer, intent(out) :: ierr
character(len=*), intent(out) :: errmsg
ierr = CCL_SUCCESS
errmsg = ""
select case (key)
case ("NAME")
ccl_case%surface_interaction(interaction_id)%name = value
case ("NORMAL")
ccl_case%surface_interaction(interaction_id)%normal_model = ccl_to_upper(value)
case ("KN")
read(value, *, iostat=ierr) ccl_case%surface_interaction(interaction_id)%kn
if (ierr == 0) then
ccl_case%surface_interaction(interaction_id)%has_kn = .true.
else
errmsg = "KN must be numeric"
end if
case ("CN")
read(value, *, iostat=ierr) ccl_case%surface_interaction(interaction_id)%cn
if (ierr == 0) then
ccl_case%surface_interaction(interaction_id)%has_cn = .true.
else
errmsg = "CN must be numeric"
end if
case ("FRICTION")
read(value, *, iostat=ierr) ccl_case%surface_interaction(interaction_id)%friction
if (ierr == 0) then
ccl_case%surface_interaction(interaction_id)%has_friction = .true.
else
errmsg = "FRICTION must be numeric"
end if
case default
ierr = CCL_ERROR_INPUT_INVALID_VALUE
errmsg = "unsupported SURFACE INTERACTION parameter: " // trim(key)
end select
end subroutine apply_surface_interaction_param
subroutine apply_contact_pair_param(key, value, ccl_case, pair_id, ierr, errmsg)
character(len=*), intent(in) :: key
character(len=*), intent(in) :: value
type(CCL_Case_Type), intent(inout) :: ccl_case
integer, intent(in) :: pair_id
integer, intent(out) :: ierr
character(len=*), intent(out) :: errmsg
ierr = CCL_SUCCESS
errmsg = ""
select case (key)
case ("NAME")
ccl_case%contact_pair(pair_id)%name = value
case ("INTERFACE")
ccl_case%contact_pair(pair_id)%interface_name = value
case ("INTERACTION")
ccl_case%contact_pair(pair_id)%interaction_name = value
case ("TYPE")
ccl_case%contact_pair(pair_id)%contact_type = ccl_to_upper(value)
case default
ierr = CCL_ERROR_INPUT_INVALID_VALUE
errmsg = "unsupported CONTACT PAIR parameter: " // trim(key)
end select
end subroutine apply_contact_pair_param
subroutine ccl_validate_case(ccl_case, ierr, errmsg)
type(CCL_Case_Type), intent(in) :: ccl_case
integer, intent(out) :: ierr
@@ -453,9 +605,127 @@ contains
if (len_trim(ccl_case%output%directory) == 0) then
ierr = CCL_ERROR_INPUT_MISSING_ARGUMENT
errmsg = "OUTPUT DIRECTORY is required"
return
end if
call validate_contact_definitions(ccl_case, ierr, errmsg)
end subroutine ccl_validate_case
subroutine validate_contact_definitions(ccl_case, ierr, errmsg)
type(CCL_Case_Type), intent(in) :: ccl_case
integer, intent(out) :: ierr
character(len=*), intent(out) :: errmsg
integer :: i
integer :: j
integer :: interaction_id
ierr = CCL_SUCCESS
errmsg = ""
do i = 1, ccl_case%num_surface_interactions
if (len_trim(ccl_case%surface_interaction(i)%name) == 0) then
ierr = CCL_ERROR_INPUT_MISSING_ARGUMENT
write(errmsg, '(a,i0,a)') "line ", ccl_case%surface_interaction(i)%line_no, &
": SURFACE INTERACTION NAME is required"
return
end if
do j = i + 1, ccl_case%num_surface_interactions
if (same_name(ccl_case%surface_interaction(i)%name, ccl_case%surface_interaction(j)%name)) then
ierr = CCL_ERROR_INPUT_INVALID_VALUE
write(errmsg, '(a,i0,a,a)') "line ", ccl_case%surface_interaction(j)%line_no, &
": duplicate SURFACE INTERACTION name: ", trim(ccl_case%surface_interaction(j)%name)
return
end if
end do
if (trim(ccl_case%surface_interaction(i)%normal_model) /= "PENALTY") then
ierr = CCL_ERROR_INPUT_INVALID_VALUE
write(errmsg, '(a,i0,a)') "line ", ccl_case%surface_interaction(i)%line_no, &
": SURFACE INTERACTION requires NORMAL=PENALTY in 0.2.x"
return
end if
if (.not. ccl_case%surface_interaction(i)%has_kn) then
ierr = CCL_ERROR_INPUT_MISSING_ARGUMENT
write(errmsg, '(a,i0,a)') "line ", ccl_case%surface_interaction(i)%line_no, &
": SURFACE INTERACTION requires KN"
return
end if
if (.not. ieee_is_finite(ccl_case%surface_interaction(i)%kn) .or. &
ccl_case%surface_interaction(i)%kn <= 0.0d0) then
ierr = CCL_ERROR_INPUT_INVALID_VALUE
write(errmsg, '(a,i0,a)') "line ", ccl_case%surface_interaction(i)%line_no, &
": KN must be finite and positive"
return
end if
if (.not. ieee_is_finite(ccl_case%surface_interaction(i)%cn) .or. &
ccl_case%surface_interaction(i)%cn < 0.0d0) then
ierr = CCL_ERROR_INPUT_INVALID_VALUE
write(errmsg, '(a,i0,a)') "line ", ccl_case%surface_interaction(i)%line_no, &
": CN must be finite and non-negative"
return
end if
if (.not. ieee_is_finite(ccl_case%surface_interaction(i)%friction)) then
ierr = CCL_ERROR_INPUT_INVALID_VALUE
write(errmsg, '(a,i0,a)') "line ", ccl_case%surface_interaction(i)%line_no, &
": FRICTION must be finite"
return
end if
if (abs(ccl_case%surface_interaction(i)%friction) > 0.0d0) then
ierr = CCL_ERROR_INPUT_INVALID_VALUE
write(errmsg, '(a,i0,a)') "line ", ccl_case%surface_interaction(i)%line_no, &
": FRICTION is reserved in 0.2.x and must be 0.0"
return
end if
end do
do i = 1, ccl_case%num_contact_pairs
if (len_trim(ccl_case%contact_pair(i)%name) == 0) then
ierr = CCL_ERROR_INPUT_MISSING_ARGUMENT
write(errmsg, '(a,i0,a)') "line ", ccl_case%contact_pair(i)%line_no, &
": CONTACT PAIR NAME is required"
return
end if
do j = i + 1, ccl_case%num_contact_pairs
if (same_name(ccl_case%contact_pair(i)%name, ccl_case%contact_pair(j)%name)) then
ierr = CCL_ERROR_INPUT_INVALID_VALUE
write(errmsg, '(a,i0,a,a)') "line ", ccl_case%contact_pair(j)%line_no, &
": duplicate CONTACT PAIR name: ", trim(ccl_case%contact_pair(j)%name)
return
end if
end do
if (len_trim(ccl_case%contact_pair(i)%interface_name) == 0) then
ierr = CCL_ERROR_INPUT_MISSING_ARGUMENT
write(errmsg, '(a,i0,a)') "line ", ccl_case%contact_pair(i)%line_no, &
": CONTACT PAIR requires INTERFACE"
return
end if
if (.not. same_name(ccl_case%contact_pair(i)%interface_name, ccl_case%interface%name)) then
ierr = CCL_ERROR_INPUT_INVALID_VALUE
write(errmsg, '(a,i0,a,a)') "line ", ccl_case%contact_pair(i)%line_no, &
": unknown CONTACT PAIR INTERFACE: ", trim(ccl_case%contact_pair(i)%interface_name)
return
end if
if (len_trim(ccl_case%contact_pair(i)%interaction_name) == 0) then
ierr = CCL_ERROR_INPUT_MISSING_ARGUMENT
write(errmsg, '(a,i0,a)') "line ", ccl_case%contact_pair(i)%line_no, &
": CONTACT PAIR requires INTERACTION"
return
end if
interaction_id = find_surface_interaction(ccl_case, ccl_case%contact_pair(i)%interaction_name)
if (interaction_id == 0) then
ierr = CCL_ERROR_INPUT_INVALID_VALUE
write(errmsg, '(a,i0,a,a)') "line ", ccl_case%contact_pair(i)%line_no, &
": unknown CONTACT PAIR INTERACTION: ", trim(ccl_case%contact_pair(i)%interaction_name)
return
end if
if (trim(ccl_case%contact_pair(i)%contact_type) /= "SPH_PARTICLE_TO_FEM_FACE") then
ierr = CCL_ERROR_INPUT_INVALID_VALUE
write(errmsg, '(a,i0,a)') "line ", ccl_case%contact_pair(i)%line_no, &
": CONTACT PAIR requires TYPE=SPH_PARTICLE_TO_FEM_FACE in 0.2.x"
return
end if
end do
end subroutine validate_contact_definitions
subroutine validate_solver(ccl_case, slot, label, ierr, errmsg)
type(CCL_Case_Type), intent(in) :: ccl_case
integer, intent(in) :: slot
@@ -486,4 +756,24 @@ contains
write(text, '(i0)') value
end function int_to_text
integer function find_surface_interaction(ccl_case, name) result(index_value)
type(CCL_Case_Type), intent(in) :: ccl_case
character(len=*), intent(in) :: name
integer :: i
index_value = 0
do i = 1, ccl_case%num_surface_interactions
if (same_name(ccl_case%surface_interaction(i)%name, name)) then
index_value = i
return
end if
end do
end function find_surface_interaction
logical function same_name(left, right) result(is_same)
character(len=*), intent(in) :: left
character(len=*), intent(in) :: right
is_same = trim(ccl_to_upper(left)) == trim(ccl_to_upper(right))
end function same_name
end module ccl_inp_parser