Files
FemLite/tools/benchmarks/ball_adhesion_cohesive_benchmark.f90
shenw 4800e19b19
FemLite CI / build-test (push) Has been cancelled
Complete V5 multiplatform verification
2026-06-26 13:55:12 +08:00

600 lines
20 KiB
Fortran

program ball_adhesion_bench
use fem_precision
use fem_error
use fem_cohesive_law
use fem_cohesive_state
use fem_cohesive_metrics
implicit none
type ball_cfg
real(rk) :: ball_radius = 0.050_rk
real(rk) :: contact_radius = 0.020_rk
integer :: rings = 12
integer :: samples = 41
real(rk) :: duration = ONE
real(rk) :: max_indentation = 0.0016_rk
real(rk) :: max_opening = 0.010_rk
real(rk) :: normal_stiffness = 8.0e7_rk
real(rk) :: tangent_stiffness = 4.0e7_rk
real(rk) :: normal_strength = 1.0e5_rk
real(rk) :: tangent_strength = 6.0e4_rk
real(rk) :: fracture_energy_i = 350.0_rk
real(rk) :: fracture_energy_ii = 520.0_rk
real(rk) :: mode_mix_eta = ONE
real(rk) :: compression_factor = 2.5_rk
real(rk) :: slip_ratio = 0.35_rk
end type ball_cfg
type ball_stats
real(rk) :: energy_total = ZERO
real(rk) :: hysteresis_area = ZERO
real(rk) :: peak_pull_force = ZERO
real(rk) :: min_compression_force = ZERO
real(rk) :: final_force = ZERO
real(rk) :: final_displacement = ZERO
real(rk) :: max_damage = ZERO
real(rk) :: max_opening = ZERO
real(rk) :: failed_area = ZERO
real(rk) :: damaged_area = ZERO
real(rk) :: failed_fraction = ZERO
integer :: failed_count = 0
end type ball_stats
type(ball_cfg) :: cfg
type(ball_stats) :: stats
type(cohesive_law_params) :: params
character(len=512) :: input_file
character(len=512) :: out_dir
integer :: argc
integer :: ierr
intrinsic iargc
intrinsic getarg
argc = iargc()
if (argc .lt. 2) then
write(*,'(A)') 'Usage: ball_adhesion_cohesive_benchmark INPUT.inp OUTPUT_DIR'
stop 2
end if
call getarg(1, input_file)
call getarg(2, out_dir)
call read_ball_input(trim(input_file), cfg, ierr)
call exit_if_error(ierr)
call cohesive_params_init(cfg%normal_stiffness, cfg%tangent_stiffness, &
cfg%normal_strength, cfg%tangent_strength, &
cfg%fracture_energy_i, cfg%fracture_energy_ii, &
cfg%mode_mix_eta, cfg%compression_factor, params, &
ierr)
call exit_if_error(ierr)
call run_benchmark(cfg, params, trim(out_dir), stats, ierr)
call exit_if_error(ierr)
call write_summary(join_path(out_dir, 'adhesion_summary.csv'), cfg, stats, &
ierr)
call exit_if_error(ierr)
call write_profile(join_path(out_dir, 'decohesion_profile.csv'), cfg, ierr)
call exit_if_error(ierr)
call cohesive_state_reset()
write(*,'(A,ES12.5,A,ES12.5,A,ES12.5)') &
'peak_pull_force=', stats%peak_pull_force, ' dissipated=', &
stats%energy_total, ' failed_fraction=', stats%failed_fraction
contains
subroutine read_ball_input(filename, cfg, ierr)
character(len=*), intent(in) :: filename
type(ball_cfg), intent(out) :: cfg
integer, intent(out) :: ierr
character(len=512) :: line
character(len=512) :: upper
integer :: unit
integer :: ios
logical :: want_data
call set_defaults(cfg)
want_data = .false.
unit = 93
open(unit=unit, file=trim(filename), status='old', action='read', &
iostat=ios)
if (ios .ne. 0) then
call error_set(FEM_ERR_INPUT, 'failed to open ball adhesion input', ierr)
return
end if
do
read(unit,'(A)',iostat=ios) line
if (ios .lt. 0) exit
if (ios .gt. 0) then
close(unit)
call error_set(FEM_ERR_INPUT, 'failed to read ball adhesion input', ierr)
return
end if
if (is_blank_or_comment(line)) cycle
call uppercase(line, upper)
if (upper(1:1) .eq. '*') then
want_data = index(upper, '*BALL ADHESION COHESIVE') .eq. 1
else if (want_data) then
call parse_ball_line(line, cfg, ios)
close(unit)
if (ios .ne. 0) then
call error_set(FEM_ERR_INPUT, 'failed to parse ball adhesion data', &
ierr)
return
end if
call validate_cfg(cfg, ierr)
return
end if
end do
close(unit)
call validate_cfg(cfg, ierr)
end subroutine read_ball_input
subroutine parse_ball_line(line, cfg, ios)
character(len=*), intent(in) :: line
type(ball_cfg), intent(inout) :: cfg
integer, intent(out) :: ios
read(line,*,iostat=ios) cfg%ball_radius, cfg%contact_radius, cfg%rings, &
cfg%samples, cfg%duration, cfg%max_indentation, cfg%max_opening, &
cfg%normal_stiffness, cfg%tangent_stiffness, cfg%normal_strength, &
cfg%tangent_strength, cfg%fracture_energy_i, cfg%fracture_energy_ii, &
cfg%mode_mix_eta, cfg%compression_factor, cfg%slip_ratio
end subroutine parse_ball_line
subroutine set_defaults(cfg)
type(ball_cfg), intent(out) :: cfg
cfg%ball_radius = 0.050_rk
cfg%contact_radius = 0.020_rk
cfg%rings = 12
cfg%samples = 41
cfg%duration = ONE
cfg%max_indentation = 0.0016_rk
cfg%max_opening = 0.010_rk
cfg%normal_stiffness = 8.0e7_rk
cfg%tangent_stiffness = 4.0e7_rk
cfg%normal_strength = 1.0e5_rk
cfg%tangent_strength = 6.0e4_rk
cfg%fracture_energy_i = 350.0_rk
cfg%fracture_energy_ii = 520.0_rk
cfg%mode_mix_eta = ONE
cfg%compression_factor = 2.5_rk
cfg%slip_ratio = 0.35_rk
end subroutine set_defaults
subroutine validate_cfg(cfg, ierr)
type(ball_cfg), intent(in) :: cfg
integer, intent(out) :: ierr
if (cfg%ball_radius .le. ZERO .or. cfg%contact_radius .le. ZERO .or. &
cfg%rings .lt. 2 .or. cfg%samples .lt. 5 .or. &
cfg%duration .le. ZERO .or. cfg%max_indentation .le. ZERO .or. &
cfg%max_opening .le. ZERO .or. cfg%slip_ratio .lt. ZERO) then
call error_set(FEM_ERR_INPUT, 'invalid ball adhesion benchmark data', &
ierr)
return
end if
if (cfg%contact_radius .gt. cfg%ball_radius) then
call error_set(FEM_ERR_INPUT, 'contact radius exceeds ball radius', ierr)
return
end if
call error_clear(ierr)
end subroutine validate_cfg
subroutine reset_stats(stats)
type(ball_stats), intent(out) :: stats
stats%energy_total = ZERO
stats%hysteresis_area = ZERO
stats%peak_pull_force = ZERO
stats%min_compression_force = ZERO
stats%final_force = ZERO
stats%final_displacement = ZERO
stats%max_damage = ZERO
stats%max_opening = ZERO
stats%failed_area = ZERO
stats%damaged_area = ZERO
stats%failed_fraction = ZERO
stats%failed_count = 0
end subroutine reset_stats
subroutine run_benchmark(cfg, params, out_dir, stats, ierr)
type(ball_cfg), intent(in) :: cfg
type(cohesive_law_params), intent(in) :: params
character(len=*), intent(in) :: out_dir
type(ball_stats), intent(out) :: stats
integer, intent(out) :: ierr
integer :: unit
integer :: ios
integer :: i
real(rk) :: time
real(rk) :: disp
real(rk) :: force
real(rk) :: energy_step
real(rk) :: prev_force
real(rk) :: prev_disp
real(rk) :: damage_max
real(rk) :: opening_max
real(rk) :: damaged_area
call reset_stats(stats)
call cohesive_state_reset()
unit = 94
open(unit=unit, file=join_path(out_dir, 'adhesion_history.csv'), &
status='replace', action='write', iostat=ios)
if (ios .ne. 0) then
call error_set(FEM_ERR_OUTPUT, 'failed to open adhesion history', ierr)
return
end if
call write_history_header(unit)
prev_force = ZERO
prev_disp = ZERO
do i = 1, cfg%samples
call sample_kinematics(cfg, i, time, disp)
call evaluate_sample(cfg, params, disp, force, energy_step, damage_max, &
opening_max, damaged_area, stats, ierr)
if (ierr .ne. FEM_SUCCESS) then
close(unit)
return
end if
stats%energy_total = stats%energy_total + energy_step
if (i .gt. 1) then
stats%hysteresis_area = stats%hysteresis_area + &
abs(0.5_rk * (force + prev_force) * (disp - prev_disp))
end if
call update_stats(stats, disp, force, damage_max, opening_max, &
damaged_area)
call write_history_row(unit, i, time, disp, force, energy_step, stats, &
ierr)
if (ierr .ne. FEM_SUCCESS) then
close(unit)
return
end if
prev_force = force
prev_disp = disp
end do
close(unit)
call error_clear(ierr)
end subroutine run_benchmark
subroutine sample_kinematics(cfg, sample, time, disp)
type(ball_cfg), intent(in) :: cfg
integer, intent(in) :: sample
real(rk), intent(out) :: time
real(rk), intent(out) :: disp
real(rk) :: tau
tau = real(sample - 1, rk) / real(cfg%samples - 1, rk)
time = cfg%duration * tau
if (tau .le. 0.35_rk) then
disp = -cfg%max_indentation * tau / 0.35_rk
else if (tau .le. 0.55_rk) then
disp = -cfg%max_indentation * (ONE - (tau - 0.35_rk) / 0.20_rk)
else
disp = cfg%max_opening * (tau - 0.55_rk) / 0.45_rk
end if
end subroutine sample_kinematics
subroutine evaluate_sample(cfg, params, disp, force, energy_step, &
damage_max, opening_max, damaged_area, stats, &
ierr)
type(ball_cfg), intent(in) :: cfg
type(cohesive_law_params), intent(in) :: params
real(rk), intent(in) :: disp
real(rk), intent(out) :: force
real(rk), intent(out) :: energy_step
real(rk), intent(out) :: damage_max
real(rk), intent(out) :: opening_max
real(rk), intent(out) :: damaged_area
type(ball_stats), intent(inout) :: stats
integer, intent(out) :: ierr
type(cohesive_metrics_state) :: metrics
integer :: ring
integer :: sample_count
real(rk) :: failed_area
real(rk) :: failed_fraction
real(rk) :: total_energy
force = ZERO
energy_step = ZERO
damaged_area = ZERO
call cohesive_metrics_reset(metrics)
call cohesive_state_begin_trial()
do ring = 1, cfg%rings
call evaluate_ring(cfg, params, disp, ring, force, energy_step, &
damaged_area, metrics, ierr)
if (ierr .ne. FEM_SUCCESS) return
end do
call cohesive_state_commit()
call cohesive_metrics_summary(metrics, sample_count, stats%failed_count, &
damage_max, opening_max, failed_area, &
failed_fraction, total_energy, ierr)
if (ierr .ne. FEM_SUCCESS) return
stats%failed_area = failed_area
stats%failed_fraction = failed_fraction
call error_clear(ierr)
end subroutine evaluate_sample
subroutine evaluate_ring(cfg, params, disp, ring, force, energy_step, &
damaged_area, metrics, ierr)
type(ball_cfg), intent(in) :: cfg
type(cohesive_law_params), intent(in) :: params
real(rk), intent(in) :: disp
integer, intent(in) :: ring
real(rk), intent(inout) :: force
real(rk), intent(inout) :: energy_step
real(rk), intent(inout) :: damaged_area
type(cohesive_metrics_state), intent(inout) :: metrics
integer, intent(out) :: ierr
real(rk) :: area
real(rk) :: rfrac
real(rk) :: opening
real(rk) :: max_opening
real(rk) :: old_damage
real(rk) :: old_opening
real(rk) :: old_eff
real(rk) :: old_energy
real(rk) :: traction_n
real(rk) :: traction_t(3)
real(rk) :: tangent_n
real(rk) :: tangent_t
real(rk) :: damage
real(rk) :: max_eff
real(rk) :: dissipated
real(rk) :: energy_amount
real(rk) :: slip(3)
integer :: status
call ring_geometry(cfg, ring, area, rfrac)
opening = ring_opening(cfg, disp, rfrac)
slip = (/ cfg%slip_ratio * max(ZERO, opening) * rfrac, ZERO, ZERO /)
call cohesive_state_old(1, 1, 1, 1, 1, ring, old_damage, old_opening, &
old_eff, old_energy, status)
call cohesive_trial_update(params, opening, slip, old_damage, old_eff, &
traction_n, traction_t, tangent_n, tangent_t, &
damage, max_eff, dissipated, status, ierr)
if (ierr .ne. FEM_SUCCESS) return
max_opening = max(old_opening, max(ZERO, opening))
energy_amount = dissipated * area
call cohesive_state_store_trial(1, 1, 1, 1, 1, ring, damage, max_opening, &
max_eff, energy_amount, status, ierr)
if (ierr .ne. FEM_SUCCESS) return
call cohesive_metrics_add(metrics, status, damage, max_opening, max_eff, &
area, energy_amount, ZERO, ierr)
if (ierr .ne. FEM_SUCCESS) return
force = force + traction_n * area
energy_step = energy_step + energy_amount
if (damage .gt. 1.0e-8_rk) damaged_area = damaged_area + area
call error_clear(ierr)
end subroutine evaluate_ring
subroutine ring_geometry(cfg, ring, area, rfrac)
type(ball_cfg), intent(in) :: cfg
integer, intent(in) :: ring
real(rk), intent(out) :: area
real(rk), intent(out) :: rfrac
real(rk) :: rin
real(rk) :: rout
real(rk) :: rmid
rin = cfg%contact_radius * real(ring - 1, rk) / real(cfg%rings, rk)
rout = cfg%contact_radius * real(ring, rk) / real(cfg%rings, rk)
rmid = 0.5_rk * (rin + rout)
area = acos(-ONE) * (rout * rout - rin * rin)
rfrac = rmid / cfg%contact_radius
end subroutine ring_geometry
real(rk) function ring_opening(cfg, disp, rfrac)
type(ball_cfg), intent(in) :: cfg
real(rk), intent(in) :: disp
real(rk), intent(in) :: rfrac
if (disp .lt. ZERO) then
ring_opening = disp + 0.20_rk * cfg%max_indentation * rfrac * rfrac
else
ring_opening = disp * (ONE + 0.35_rk * rfrac * rfrac)
end if
end function ring_opening
subroutine update_stats(stats, disp, force, damage_max, opening_max, &
damaged_area)
type(ball_stats), intent(inout) :: stats
real(rk), intent(in) :: disp
real(rk), intent(in) :: force
real(rk), intent(in) :: damage_max
real(rk), intent(in) :: opening_max
real(rk), intent(in) :: damaged_area
if (force .gt. stats%peak_pull_force) stats%peak_pull_force = force
if (force .lt. stats%min_compression_force) stats%min_compression_force = force
stats%final_force = force
stats%final_displacement = disp
stats%max_damage = max(stats%max_damage, damage_max)
stats%max_opening = max(stats%max_opening, opening_max)
stats%damaged_area = damaged_area
end subroutine update_stats
subroutine write_history_header(unit)
integer, intent(in) :: unit
write(unit,'(A)') 'sample,time,displacement,normal_force,pull_force,' // &
'compression_force,energy_step,energy_total,max_damage,max_opening,' // &
'failed_fraction,failed_count,damaged_area'
end subroutine write_history_header
subroutine write_history_row(unit, sample, time, disp, force, energy_step, &
stats, ierr)
integer, intent(in) :: unit
integer, intent(in) :: sample
real(rk), intent(in) :: time
real(rk), intent(in) :: disp
real(rk), intent(in) :: force
real(rk), intent(in) :: energy_step
type(ball_stats), intent(in) :: stats
integer, intent(out) :: ierr
integer :: ios
write(unit,'(I0,10(",",ES16.8),",",I0,",",ES16.8)',iostat=ios) &
sample, time, disp, force, max(ZERO, force), min(ZERO, force), &
energy_step, stats%energy_total, stats%max_damage, stats%max_opening, &
stats%failed_fraction, stats%failed_count, stats%damaged_area
if (ios .ne. 0) then
call error_set(FEM_ERR_OUTPUT, 'failed to write adhesion history', ierr)
return
end if
call error_clear(ierr)
end subroutine write_history_row
subroutine write_summary(filename, cfg, stats, ierr)
character(len=*), intent(in) :: filename
type(ball_cfg), intent(in) :: cfg
type(ball_stats), intent(in) :: stats
integer, intent(out) :: ierr
integer :: unit
integer :: ios
unit = 95
open(unit=unit, file=trim(filename), status='replace', action='write', &
iostat=ios)
if (ios .ne. 0) then
call error_set(FEM_ERR_OUTPUT, 'failed to open adhesion summary', ierr)
return
end if
write(unit,'(A)') 'key,value'
write(unit,'(A,I0)') 'samples,', cfg%samples
write(unit,'(A,I0)') 'rings,', cfg%rings
write(unit,'(A,ES16.8)') 'ball_radius,', cfg%ball_radius
write(unit,'(A,ES16.8)') 'contact_radius,', cfg%contact_radius
write(unit,'(A,ES16.8)') 'contact_area,', acos(-ONE) * &
cfg%contact_radius * cfg%contact_radius
write(unit,'(A,ES16.8)') 'max_indentation,', cfg%max_indentation
write(unit,'(A,ES16.8)') 'max_opening,', cfg%max_opening
write(unit,'(A,ES16.8)') 'peak_pull_force,', stats%peak_pull_force
write(unit,'(A,ES16.8)') 'min_compression_force,', stats%min_compression_force
write(unit,'(A,ES16.8)') 'dissipated_energy,', stats%energy_total
write(unit,'(A,ES16.8)') 'hysteresis_area,', stats%hysteresis_area
write(unit,'(A,ES16.8)') 'max_damage,', stats%max_damage
write(unit,'(A,ES16.8)') 'max_cohesive_opening,', stats%max_opening
write(unit,'(A,ES16.8)') 'decohesion_area,', stats%failed_area
write(unit,'(A,ES16.8)') 'damaged_area,', stats%damaged_area
write(unit,'(A,ES16.8)') 'failed_fraction,', stats%failed_fraction
write(unit,'(A,I0)') 'failed_count,', stats%failed_count
write(unit,'(A,ES16.8)') 'final_displacement,', stats%final_displacement
write(unit,'(A,ES16.8)') 'final_force,', stats%final_force
close(unit)
call error_clear(ierr)
end subroutine write_summary
subroutine write_profile(filename, cfg, ierr)
character(len=*), intent(in) :: filename
type(ball_cfg), intent(in) :: cfg
integer, intent(out) :: ierr
integer :: unit
integer :: ios
integer :: ring
integer :: status
real(rk) :: area
real(rk) :: rfrac
real(rk) :: rin
real(rk) :: rout
real(rk) :: damage
real(rk) :: max_opening
real(rk) :: max_eff
real(rk) :: energy
unit = 96
open(unit=unit, file=trim(filename), status='replace', action='write', &
iostat=ios)
if (ios .ne. 0) then
call error_set(FEM_ERR_OUTPUT, 'failed to open decohesion profile', ierr)
return
end if
write(unit,'(A)') 'ring,r_inner,r_outer,area,damage,max_opening,' // &
'max_effective,energy,status'
do ring = 1, cfg%rings
call ring_geometry(cfg, ring, area, rfrac)
rin = cfg%contact_radius * real(ring - 1, rk) / real(cfg%rings, rk)
rout = cfg%contact_radius * real(ring, rk) / real(cfg%rings, rk)
call cohesive_state_old(1, 1, 1, 1, 1, ring, damage, max_opening, &
max_eff, energy, status)
write(unit,'(I0,7(",",ES16.8),",",I0)',iostat=ios) ring, rin, rout, &
area, damage, max_opening, max_eff, energy, status
if (ios .ne. 0) then
close(unit)
call error_set(FEM_ERR_OUTPUT, 'failed to write decohesion profile', &
ierr)
return
end if
end do
close(unit)
call error_clear(ierr)
end subroutine write_profile
logical function is_blank_or_comment(line)
character(len=*), intent(in) :: line
integer :: n
n = len_trim(line)
is_blank_or_comment = n .eq. 0
if (.not. is_blank_or_comment .and. n .ge. 2) then
is_blank_or_comment = line(1:2) .eq. '**'
end if
end function is_blank_or_comment
subroutine uppercase(line, upper)
character(len=*), intent(in) :: line
character(len=*), intent(out) :: upper
integer :: i
integer :: code
upper = line
do i = 1, len_trim(upper)
code = iachar(upper(i:i))
if (code .ge. iachar('a') .and. code .le. iachar('z')) then
upper(i:i) = achar(code - iachar('a') + iachar('A'))
end if
end do
end subroutine uppercase
function join_path(dir, name) result(path)
character(len=*), intent(in) :: dir
character(len=*), intent(in) :: name
character(len=512) :: path
integer :: n
n = len_trim(dir)
if (n .le. 0) then
path = trim(name)
else if (dir(n:n) .eq. '\' .or. dir(n:n) .eq. '/') then
path = trim(dir)//trim(name)
else
path = trim(dir)//'/'//trim(name)
end if
end function join_path
subroutine exit_if_error(ierr)
integer, intent(in) :: ierr
integer :: code
integer :: ierr2
character(len=256) :: msg
if (ierr .eq. FEM_SUCCESS) return
call fem_get_last_error(code, msg, ierr2)
if (ierr2 .eq. FEM_SUCCESS) then
write(*,'(A,I0,2A)') 'Error ', code, ': ', trim(msg)
else
write(*,'(A,I0)') 'Error: ', ierr
end if
stop 1
end subroutine exit_if_error
end program ball_adhesion_bench