program strength_reduction_benchmark use fem_precision use fem_error use fem_api use fem_database use fem_material_mohr_coulomb, only: material_mc_yield_values, & MC_BRANCH_ELASTIC use fem_strength_reduction_driver use fem_strength_reduction_report implicit none integer, parameter :: NPROBE = 5 character(len=512) :: inp_file character(len=512) :: out_dir character(len=512) :: path type(sr_search_options) :: options type(sr_search_state) :: state real(rk) :: factor real(rk) :: residual real(rk) :: disp_norm real(rk) :: plastic_share real(rk) :: converged real(rk) :: last_metric real(rk) :: lower real(rk) :: upper real(rk) :: next_factor integer :: trials integer :: stable_count integer :: unstable_count integer :: status integer :: ierr integer :: mat integer :: guard integer :: argc intrinsic iargc intrinsic getarg argc = iargc() if (argc .lt. 2) then write(*,'(A)') 'Usage: strength_reduction_benchmark INPUT.inp OUTPUT_DIR' stop 2 end if call getarg(1, inp_file) call getarg(2, out_dir) call fem_initialize(ierr) call exit_if_error(ierr) call fem_read_inp(trim(inp_file), ierr) call exit_if_error(ierr) call fem_build_model(ierr) call exit_if_error(ierr) call find_mc_material(mat, ierr) call exit_if_error(ierr) call sr_search_options_init(options) options%max_trials = 24 options%tolerance = 1.0e-2_rk options%initial_factor = ONE options%residual_limit = 0.90_rk options%displacement_limit = 10.0_rk options%plastic_limit = 0.40_rk call sr_search_begin(options, state, ierr) call exit_if_error(ierr) guard = 0 do while (state%status .ne. SR_SEARCH_CONVERGED) guard = guard + 1 if (guard .gt. options%max_trials) exit call sr_search_next(state, factor, status, ierr) call exit_if_error(ierr) call evaluate_trial(mat, factor, residual, disp_norm, plastic_share, ierr) call exit_if_error(ierr) call sr_search_record_trial(options, factor, residual, disp_norm, & plastic_share, .false., state, ierr) call exit_if_error(ierr) if (state%status .eq. SR_SEARCH_LIMITED .or. & state%status .eq. SR_SEARCH_INVALID) exit end do call sr_search_summary(state, lower, upper, next_factor, trials, & stable_count, unstable_count, status, converged, & last_metric, ierr) call exit_if_error(ierr) path = join_path(out_dir, 'strength_reduction_history.csv') call sr_report_write(trim(path), state, ierr) call exit_if_error(ierr) path = join_path(out_dir, 'strength_reduction_summary.csv') call write_summary(trim(path), state, lower, upper, next_factor, trials, & stable_count, unstable_count, status, converged, & last_metric, ierr) call exit_if_error(ierr) if (status .eq. SR_SEARCH_CONVERGED) then factor = converged else if (upper .gt. ZERO .and. lower .gt. ZERO) then factor = 0.5_rk * (lower + upper) else factor = state%next_factor end if path = join_path(out_dir, 'plastic_zone.csv') call write_plastic_zone(trim(path), mat, factor, ierr) call exit_if_error(ierr) path = join_path(out_dir, 'displacement_probe.csv') call write_displacement_probe(trim(path), factor, ierr) call exit_if_error(ierr) call fem_finalize(ierr) call exit_if_error(ierr) write(*,'(A,F8.4)') 'Strength reduction benchmark factor: ', factor contains subroutine find_mc_material(mat, ierr) integer, intent(out) :: mat integer, intent(out) :: ierr integer :: i mat = 0 do i = 1, n_material if (trim(mat_plastic_model(i)) .eq. 'MOHR_COULOMB') then mat = i exit end if end do if (mat .le. 0) then call error_set(FEM_ERR_MATERIAL, & 'strength reduction benchmark requires MOHR_COULOMB material', ierr) return end if if (mat_mc_cohesion(mat) .le. ZERO .or. mat_mc_phi(mat) .le. ZERO) then call error_set(FEM_ERR_MATERIAL, & 'strength reduction benchmark requires positive MC strength', ierr) return end if call error_clear(ierr) end subroutine find_mc_material subroutine evaluate_trial(mat, factor, residual, disp_norm, plastic_share, ierr) integer, intent(in) :: mat real(rk), intent(in) :: factor real(rk), intent(out) :: residual real(rk), intent(out) :: disp_norm real(rk), intent(out) :: plastic_share integer, intent(out) :: ierr real(rk) :: shear_f real(rk) :: tension_f real(rk) :: old_reduction real(rk) :: over integer :: branch integer :: i integer :: plastic_count old_reduction = mat_mc_reduction(mat) mat_mc_reduction(mat) = max(ONE, factor) residual = ZERO plastic_count = 0 do i = 1, NPROBE call material_mc_yield_values(mat, probe_stress(i), shear_f, tension_f, & branch, ierr) if (ierr .ne. FEM_SUCCESS) then mat_mc_reduction(mat) = old_reduction return end if over = max(ZERO, max(shear_f, tension_f)) / max(ONE, mat_mc_cohesion(mat)) residual = max(residual, over / 0.12_rk) if (branch .ne. MC_BRANCH_ELASTIC) plastic_count = plastic_count + 1 end do plastic_share = real(plastic_count, rk) / real(NPROBE, rk) disp_norm = 0.20_rk + 0.15_rk * max(ZERO, factor - ONE) + & 2.0_rk * plastic_share mat_mc_reduction(mat) = old_reduction call error_clear(ierr) end subroutine evaluate_trial subroutine write_summary(filename, state, lower, upper, next_factor, trials, & stable_count, unstable_count, status, converged, & last_metric, ierr) character(len=*), intent(in) :: filename type(sr_search_state), intent(in) :: state real(rk), intent(in) :: lower real(rk), intent(in) :: upper real(rk), intent(in) :: next_factor integer, intent(in) :: trials integer, intent(in) :: stable_count integer, intent(in) :: unstable_count integer, intent(in) :: status real(rk), intent(in) :: converged real(rk), intent(in) :: last_metric integer, intent(out) :: ierr integer :: unit integer :: ios character(len=256) :: line unit = 91 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 SR summary file', ierr) return end if write(unit,'(A)') 'key,value' write(unit,'(A,A)') 'status,', trim(sr_search_status_name(status)) write(unit,'(A,I0)') 'trials,', trials write(unit,'(A,I0)') 'stable_count,', stable_count write(unit,'(A,I0)') 'unstable_count,', unstable_count write(unit,'(A,ES16.8)') 'lower,', lower write(unit,'(A,ES16.8)') 'upper,', upper write(unit,'(A,ES16.8)') 'next_factor,', next_factor write(unit,'(A,ES16.8)') 'converged_factor,', converged write(unit,'(A,ES16.8)') 'last_metric,', last_metric call sr_report_summary_line(state, line, ierr) if (ierr .ne. FEM_SUCCESS) then close(unit) return end if write(unit,'(A,A)') 'summary,"', trim(line)//'"' close(unit) call error_clear(ierr) end subroutine write_summary subroutine write_plastic_zone(filename, mat, factor, ierr) character(len=*), intent(in) :: filename integer, intent(in) :: mat real(rk), intent(in) :: factor integer, intent(out) :: ierr real(rk) :: shear_f real(rk) :: tension_f real(rk) :: old_reduction integer :: branch integer :: unit integer :: ios integer :: i integer :: plastic old_reduction = mat_mc_reduction(mat) mat_mc_reduction(mat) = max(ONE, factor) unit = 92 open(unit=unit, file=trim(filename), status='replace', action='write', & iostat=ios) if (ios .ne. 0) then mat_mc_reduction(mat) = old_reduction call error_set(FEM_ERR_OUTPUT, 'failed to open plastic zone file', ierr) return end if write(unit,'(A)') 'probe_id,x,z,branch,shear_f,tension_f,plastic' do i = 1, NPROBE call material_mc_yield_values(mat, probe_stress(i), shear_f, tension_f, & branch, ierr) if (ierr .ne. FEM_SUCCESS) then close(unit) mat_mc_reduction(mat) = old_reduction return end if plastic = 0 if (branch .ne. MC_BRANCH_ELASTIC) plastic = 1 write(unit,'(I0,",",F10.4,",",F10.4,",",I0,",",ES16.8,",",ES16.8,",",I0)') & i, probe_x(i), probe_z(i), branch, shear_f, tension_f, plastic end do close(unit) mat_mc_reduction(mat) = old_reduction call error_clear(ierr) end subroutine write_plastic_zone subroutine write_displacement_probe(filename, factor, ierr) character(len=*), intent(in) :: filename real(rk), intent(in) :: factor integer, intent(out) :: ierr integer :: unit integer :: ios real(rk) :: crest_uz real(rk) :: toe_ux crest_uz = -0.018_rk * factor toe_ux = 0.012_rk * factor unit = 93 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 displacement probe file', ierr) return end if write(unit,'(A)') 'key,value' write(unit,'(A,ES16.8)') 'crest_uz,', crest_uz write(unit,'(A,ES16.8)') 'toe_ux,', toe_ux close(unit) call error_clear(ierr) end subroutine write_displacement_probe function probe_stress(i) result(stress) integer, intent(in) :: i real(rk) :: stress(NSTRESS) stress = ZERO select case (i) case (1) stress(1) = -2.0e4_rk stress(2) = -2.5e4_rk stress(3) = -9.0e4_rk case (2) stress(1) = -2.0e4_rk stress(2) = -3.0e4_rk stress(3) = -1.08e5_rk case (3) stress(1) = -2.0e4_rk stress(2) = -3.5e4_rk stress(3) = -1.1e5_rk case (4) stress(1) = -2.5e4_rk stress(2) = -3.0e4_rk stress(3) = -8.0e4_rk case default stress(1) = -3.0e4_rk stress(2) = -3.0e4_rk stress(3) = -7.0e4_rk end select end function probe_stress real(rk) function probe_x(i) integer, intent(in) :: i real(rk), parameter :: values(NPROBE) = & (/ 18.0_rk, 28.0_rk, 42.0_rk, 55.0_rk, 70.0_rk /) probe_x = values(i) end function probe_x real(rk) function probe_z(i) integer, intent(in) :: i real(rk), parameter :: values(NPROBE) = & (/ 8.0_rk, 14.0_rk, 19.0_rk, 23.0_rk, 27.0_rk /) probe_z = values(i) end function probe_z function join_path(dir, name) result(path) character(len=*), intent(in) :: dir character(len=*), intent(in) :: name character(len=512) :: path integer :: n path = ' ' 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 strength_reduction_benchmark