program mc_yield_surface_benchmark use fem_precision use fem_error use fem_database use fem_material_mohr_coulomb use fem_material_mc_envelope implicit none integer, parameter :: SURFACE_SAMPLES = 17 real(rk), parameter :: PI = 3.1415926535897932384626433832795_rk real(rk), parameter :: E_SOIL = 1000.0_rk real(rk), parameter :: NU_SOIL = 0.25_rk real(rk), parameter :: C_SOIL = 10.0_rk real(rk), parameter :: PHI_SOIL = 30.0_rk * PI / 180.0_rk real(rk), parameter :: PSI_SOIL = 5.0_rk * PI / 180.0_rk real(rk), parameter :: T_SOIL = 2.0_rk character(len=512) :: out_dir integer :: argc integer :: ierr intrinsic iargc intrinsic getarg argc = iargc() if (argc .lt. 1) then call write_usage() stop 2 end if call getarg(1, out_dir) call setup_material(ierr) call exit_if_error(ierr) call write_outputs(trim(out_dir), ierr) call exit_if_error(ierr) call database_finalize(ierr) call exit_if_error(ierr) write(*,'(A)') 'Mohr-Coulomb yield surface benchmark completed' contains subroutine write_usage() write(*,'(A)') 'Usage: mc_yield_surface_benchmark OUTPUT_DIR' end subroutine write_usage subroutine setup_material(ierr) integer, intent(out) :: ierr call database_allocate_model(1, 1, 0, 0, 0, 0, 1, 0, 0, & 0, 0, 0, 0, 0, ierr) if (ierr .ne. FEM_SUCCESS) return mat_name(1) = 'MC_YIELD_SURFACE_SOIL' mat_plastic_model(1) = 'MOHR_COULOMB' mat_density(1) = 1800.0_rk mat_e(1) = E_SOIL mat_nu(1) = NU_SOIL mat_mc_phi(1) = PHI_SOIL mat_mc_cohesion(1) = C_SOIL mat_mc_dilation(1) = PSI_SOIL mat_mc_tension(1) = T_SOIL mat_mc_res_phi(1) = ZERO mat_mc_res_cohesion(1) = ZERO mat_mc_res_dilation(1) = ZERO mat_mc_reduction(1) = ONE call error_clear(ierr) end subroutine setup_material subroutine write_outputs(dir, ierr) character(len=*), intent(in) :: dir integer, intent(out) :: ierr integer :: surface_unit integer :: point_unit integer :: summary_unit real(rk) :: max_error integer :: point_count integer :: elastic_count integer :: shear_count integer :: tension_count integer :: mixed_count call ensure_output_directory(dir, ierr) if (ierr .ne. FEM_SUCCESS) return surface_unit = 91 point_unit = 92 summary_unit = 93 call open_csv(surface_unit, join_path(dir, 'yield_surface.csv'), ierr) if (ierr .ne. FEM_SUCCESS) return call open_csv(point_unit, join_path(dir, 'yield_points.csv'), ierr) if (ierr .ne. FEM_SUCCESS) return call open_csv(summary_unit, join_path(dir, 'yield_summary.csv'), ierr) if (ierr .ne. FEM_SUCCESS) return write(surface_unit,'(A)') 'factor,sample,confining,compression,'// & 'extension,tension_cap,compression_formula,extension_formula,'// & 'tension_formula,compression_error,extension_error,tension_error' write(point_unit,'(A)') 'case_name,factor,sxx,syy,szz,sxy,syz,sxz,'// & 'branch,shear_f,tension_f,shear_margin,tension_margin,safety_ratio' max_error = ZERO point_count = 0 elastic_count = 0 shear_count = 0 tension_count = 0 mixed_count = 0 call write_surface_for_factor(surface_unit, ONE, max_error, ierr) if (ierr .ne. FEM_SUCCESS) return call write_surface_for_factor(surface_unit, 2.0_rk, max_error, ierr) if (ierr .ne. FEM_SUCCESS) return call write_points_for_factor(point_unit, ONE, point_count, elastic_count, & shear_count, tension_count, mixed_count, ierr) if (ierr .ne. FEM_SUCCESS) return call write_points_for_factor(point_unit, 2.0_rk, point_count, & elastic_count, shear_count, tension_count, & mixed_count, ierr) if (ierr .ne. FEM_SUCCESS) return call write_summary(summary_unit, max_error, point_count, elastic_count, & shear_count, tension_count, mixed_count) close(surface_unit) close(point_unit) close(summary_unit) call error_clear(ierr) end subroutine write_outputs subroutine write_surface_for_factor(unit_id, factor, max_error, ierr) integer, intent(in) :: unit_id real(rk), intent(in) :: factor real(rk), intent(inout) :: max_error integer, intent(out) :: ierr type(mc_envelope_diag) :: diag real(rk) :: confining(SURFACE_SAMPLES) real(rk) :: compression(SURFACE_SAMPLES) real(rk) :: extension(SURFACE_SAMPLES) real(rk) :: tension(SURFACE_SAMPLES) real(rk) :: comp_ref real(rk) :: ext_ref real(rk) :: ten_ref real(rk) :: comp_err real(rk) :: ext_err real(rk) :: ten_err integer :: i mat_mc_reduction(1) = factor call mc_env_table(1, SURFACE_SAMPLES, ZERO, 80.0_rk, confining, & compression, extension, tension, diag, ierr) if (ierr .ne. FEM_SUCCESS) return do i = 1, SURFACE_SAMPLES call analytic_strengths(confining(i), comp_ref, ext_ref, ten_ref, ierr) if (ierr .ne. FEM_SUCCESS) return comp_err = abs(compression(i) - comp_ref) ext_err = abs(extension(i) - ext_ref) ten_err = abs(tension(i) - ten_ref) max_error = max(max_error, comp_err, ext_err, ten_err) write(unit_id,'(F8.4,",",I0,",",ES16.8,",",ES16.8,",",ES16.8,'// & '",",ES16.8,",",ES16.8,",",ES16.8,",",ES16.8,",",ES16.8,'// & '",",ES16.8,",",ES16.8)') factor, i, confining(i), compression(i), & extension(i), tension(i), comp_ref, ext_ref, ten_ref, comp_err, & ext_err, ten_err end do call error_clear(ierr) end subroutine write_surface_for_factor subroutine write_points_for_factor(unit_id, factor, point_count, & elastic_count, shear_count, & tension_count, mixed_count, ierr) integer, intent(in) :: unit_id real(rk), intent(in) :: factor integer, intent(inout) :: point_count integer, intent(inout) :: elastic_count integer, intent(inout) :: shear_count integer, intent(inout) :: tension_count integer, intent(inout) :: mixed_count integer, intent(out) :: ierr real(rk) :: comp real(rk) :: ext real(rk) :: tension real(rk) :: stress(NSTRESS) mat_mc_reduction(1) = factor comp = ZERO ext = ZERO tension = ZERO stress = ZERO stress(1) = -5.0_rk stress(2) = -5.0_rk stress(3) = -5.0_rk call write_point(unit_id, 'elastic_hydrostatic', factor, stress, & point_count, elastic_count, shear_count, tension_count, & mixed_count, ierr) if (ierr .ne. FEM_SUCCESS) return call analytic_strengths(10.0_rk, comp, ext, tension, ierr) if (ierr .ne. FEM_SUCCESS) return stress = ZERO stress(1) = -10.0_rk stress(2) = -0.5_rk * (10.0_rk + comp) stress(3) = -comp call write_point(unit_id, 'compression_surface', factor, stress, & point_count, elastic_count, shear_count, tension_count, & mixed_count, ierr) if (ierr .ne. FEM_SUCCESS) return stress(3) = -(comp + 5.0_rk) call write_point(unit_id, 'compression_outside', factor, stress, & point_count, elastic_count, shear_count, tension_count, & mixed_count, ierr) if (ierr .ne. FEM_SUCCESS) return stress = ZERO stress(1) = tension stress(2) = ZERO stress(3) = -ONE call write_point(unit_id, 'tension_cap_surface', factor, stress, & point_count, elastic_count, shear_count, tension_count, & mixed_count, ierr) if (ierr .ne. FEM_SUCCESS) return stress(1) = tension + 0.25_rk call write_point(unit_id, 'tension_outside', factor, stress, point_count, & elastic_count, shear_count, tension_count, mixed_count, & ierr) if (ierr .ne. FEM_SUCCESS) return stress = ZERO stress(1) = tension + 0.5_rk stress(2) = -10.0_rk stress(3) = -80.0_rk call write_point(unit_id, 'mixed_corner_outside', factor, stress, & point_count, elastic_count, shear_count, tension_count, & mixed_count, ierr) if (ierr .ne. FEM_SUCCESS) return call error_clear(ierr) end subroutine write_points_for_factor subroutine write_point(unit_id, case_name, factor, stress, point_count, & elastic_count, shear_count, tension_count, & mixed_count, ierr) integer, intent(in) :: unit_id character(len=*), intent(in) :: case_name real(rk), intent(in) :: factor real(rk), intent(in) :: stress(NSTRESS) integer, intent(inout) :: point_count integer, intent(inout) :: elastic_count integer, intent(inout) :: shear_count integer, intent(inout) :: tension_count integer, intent(inout) :: mixed_count integer, intent(out) :: ierr real(rk) :: shear_f real(rk) :: tension_f real(rk) :: shear_margin real(rk) :: tension_margin real(rk) :: ratio integer :: branch integer :: margin_branch integer :: ratio_branch call material_mc_yield_values(1, stress, shear_f, tension_f, branch, ierr) if (ierr .ne. FEM_SUCCESS) return call mc_env_stress_margin(1, stress, shear_margin, tension_margin, & margin_branch, ierr) if (ierr .ne. FEM_SUCCESS) return call mc_env_safety_ratio(1, stress, ratio, ratio_branch, ierr) if (ierr .ne. FEM_SUCCESS) return point_count = point_count + 1 call add_branch_count(branch, elastic_count, shear_count, tension_count, & mixed_count) write(unit_id,'(A,",",F8.4,",",ES16.8,",",ES16.8,",",ES16.8,'// & '",",ES16.8,",",ES16.8,",",ES16.8,",",A,",",ES16.8,",",'// & 'ES16.8,",",ES16.8,",",ES16.8,",",ES16.8)') trim(case_name), & factor, stress(1), stress(2), stress(3), stress(4), stress(5), & stress(6), trim(branch_name(branch)), shear_f, tension_f, & shear_margin, tension_margin, ratio call error_clear(ierr) end subroutine write_point subroutine add_branch_count(branch, elastic_count, shear_count, & tension_count, mixed_count) integer, intent(in) :: branch integer, intent(inout) :: elastic_count integer, intent(inout) :: shear_count integer, intent(inout) :: tension_count integer, intent(inout) :: mixed_count select case (branch) case (MC_BRANCH_ELASTIC) elastic_count = elastic_count + 1 case (MC_BRANCH_SHEAR) shear_count = shear_count + 1 case (MC_BRANCH_TENSION) tension_count = tension_count + 1 case (MC_BRANCH_MIXED) mixed_count = mixed_count + 1 end select end subroutine add_branch_count subroutine write_summary(unit_id, max_error, point_count, elastic_count, & shear_count, tension_count, mixed_count) integer, intent(in) :: unit_id real(rk), intent(in) :: max_error integer, intent(in) :: point_count integer, intent(in) :: elastic_count integer, intent(in) :: shear_count integer, intent(in) :: tension_count integer, intent(in) :: mixed_count write(unit_id,'(A)') 'key,value' write(unit_id,'(A)') 'status,PASS' write(unit_id,'(A,I0)') 'surface_samples_per_factor,', SURFACE_SAMPLES write(unit_id,'(A,I0)') 'factor_count,2' write(unit_id,'(A,I0)') 'point_count,', point_count write(unit_id,'(A,I0)') 'elastic_count,', elastic_count write(unit_id,'(A,I0)') 'shear_count,', shear_count write(unit_id,'(A,I0)') 'tension_count,', tension_count write(unit_id,'(A,I0)') 'mixed_count,', mixed_count write(unit_id,'(A,ES16.8)') 'max_formula_error,', max_error write(unit_id,'(A,ES16.8)') 'phi_deg,', PHI_SOIL * 180.0_rk / PI write(unit_id,'(A,ES16.8)') 'cohesion,', C_SOIL write(unit_id,'(A,ES16.8)') 'tension,', T_SOIL end subroutine write_summary subroutine analytic_strengths(confining, compression, extension, & tension_cap, ierr) real(rk), intent(in) :: confining real(rk), intent(out) :: compression real(rk), intent(out) :: extension real(rk), intent(out) :: tension_cap integer, intent(out) :: ierr real(rk) :: phi real(rk) :: cohesion real(rk) :: dilation real(rk) :: sin_phi real(rk) :: nphi real(rk) :: kphi compression = ZERO extension = ZERO tension_cap = ZERO call material_mc_params(1, phi, cohesion, dilation, tension_cap, ierr) if (ierr .ne. FEM_SUCCESS) return sin_phi = sin(phi) nphi = (ONE + sin_phi) / max(tiny(ONE), ONE - sin_phi) kphi = 2.0_rk * cohesion * sqrt(nphi) compression = nphi * confining + kphi extension = max(ZERO, (confining - kphi) / nphi) call error_clear(ierr) end subroutine analytic_strengths subroutine ensure_output_directory(dir, ierr) character(len=*), intent(in) :: dir integer, intent(out) :: ierr character(len=512) :: command character(len=128) :: os_name integer :: os_len integer :: os_end integer :: exitstat integer :: cmdstat if (len_trim(dir) .le. 0 .or. trim(dir) .eq. '.') then call error_clear(ierr) return end if os_name = ' ' call get_environment_variable('OS', os_name, os_len) os_end = min(len(os_name), max(1, os_len)) if (index(os_name(1:os_end), 'Windows') .gt. 0) then if (index(trim(dir), '"') .gt. 0) then call error_set(FEM_ERR_OUTPUT, & 'MC yield surface output path must not contain double quotes', ierr) return end if command = 'mkdir "'//trim(dir)//'" >nul 2>nul' else if (index(trim(dir), "'") .gt. 0) then call error_set(FEM_ERR_OUTPUT, & 'MC yield surface output path must not contain single quotes', ierr) return end if command = "mkdir -p '"//trim(dir)//"'" end if call execute_command_line(trim(command), exitstat=exitstat, & cmdstat=cmdstat) if (cmdstat .ne. 0) then call error_set(FEM_ERR_OUTPUT, & 'failed to create MC yield surface output directory', ierr) return end if call error_clear(ierr) end subroutine ensure_output_directory subroutine open_csv(unit_id, filename, ierr) integer, intent(in) :: unit_id character(len=*), intent(in) :: filename integer, intent(out) :: ierr integer :: ios open(unit=unit_id, file=trim(filename), status='replace', action='write', & iostat=ios) if (ios .ne. 0) then call error_set(FEM_ERR_OUTPUT, 'failed to open MC yield surface output', & ierr) return end if call error_clear(ierr) end subroutine open_csv function join_path(dir, name) result(out_path) character(len=*), intent(in) :: dir character(len=*), intent(in) :: name character(len=512) :: out_path integer :: n out_path = ' ' n = len_trim(dir) if (n .le. 0) then out_path = trim(name) else if (dir(n:n) .eq. '\' .or. dir(n:n) .eq. '/') then out_path = trim(dir)//trim(name) else out_path = trim(dir)//'/'//trim(name) end if end function join_path character(len=16) function branch_name(branch) integer, intent(in) :: branch select case (branch) case (MC_BRANCH_ELASTIC) branch_name = 'ELASTIC' case (MC_BRANCH_SHEAR) branch_name = 'SHEAR' case (MC_BRANCH_TENSION) branch_name = 'TENSION' case (MC_BRANCH_MIXED) branch_name = 'MIXED' case default branch_name = 'UNKNOWN' end select end function branch_name subroutine exit_if_error(ierr) integer, intent(in) :: ierr if (ierr .ne. FEM_SUCCESS) then write(*,'(A,I0)') 'MC yield surface benchmark failed: ierr=', ierr stop 1 end if end subroutine exit_if_error end program mc_yield_surface_benchmark