program corr_shell_buckling_bench use fem_precision use fem_error use fem_buckling_solver use fem_cyclic_symmetry use fem_modal_analysis use fem_modal_output implicit none type shell_input integer :: sectors = 16 integer :: wave = 4 integer :: mode_count = 6 real(rk) :: bottom_radius = 0.031_rk real(rk) :: top_radius = 0.0155_rk real(rk) :: height = 0.050_rk real(rk) :: thickness = 5.0e-4_rk real(rk) :: young = 7.0e10_rk real(rk) :: poisson = 0.30_rk real(rk) :: stiffness_coupling = 0.18_rk real(rk) :: geometric_coupling = 0.42_rk real(rk) :: corrugation_factor = 1.0_rk end type shell_input type(shell_input) :: cfg real(rk), allocatable :: kfull(:,:) real(rk), allocatable :: kgfull(:,:) real(rk), allocatable :: modes(:,:) real(rk), allocatable :: output_modes(:,:) real(rk), allocatable :: output_values(:) real(rk), allocatable :: output_residuals(:) real(rk), allocatable :: factors(:) real(rk), allocatable :: residuals(:) real(rk) :: base_k real(rk) :: couple_k real(rk) :: base_g real(rk) :: couple_g real(rk) :: selected_load real(rk) :: closest_load real(rk) :: closest_rel_error real(rk) :: cyclic_residual real(rk) :: max_offdiag real(rk) :: max_diag_error real(rk) :: first_value real(rk) :: last_value real(rk) :: max_residual real(rk) :: max_component real(rk) :: critical_load character(len=512) :: input_file character(len=512) :: out_dir character(len=512) :: path integer :: argc integer :: ierr integer :: output_count integer :: critical_wave integer :: closest_index intrinsic iargc intrinsic getarg argc = iargc() if (argc .lt. 2) then write(*,'(A)') 'Usage: corrugated_shell_buckling_benchmark INPUT.inp OUTPUT_DIR' stop 2 end if call getarg(1, input_file) call getarg(2, out_dir) call read_shell_input(trim(input_file), cfg, ierr) call exit_if_error(ierr) allocate(kfull(cfg%sectors,cfg%sectors), kgfull(cfg%sectors,cfg%sectors), & modes(cfg%sectors,cfg%sectors), factors(cfg%sectors), & residuals(cfg%sectors), stat=ierr) if (ierr .ne. 0) then call error_set(FEM_ERR_INTERNAL, 'failed to allocate corrugated buckling arrays', ierr) call exit_if_error(ierr) end if call reduced_shell_constants(cfg, base_k, couple_k, base_g, couple_g, ierr) call exit_if_error(ierr) call assemble_periodic_ring(cfg%sectors, base_k, couple_k, base_g, & couple_g, kfull, kgfull) call buckling_dense_solve(cfg%sectors, kfull, kgfull, cfg%sectors, factors, & modes, residuals, ierr) call exit_if_error(ierr) call selected_wave_data(cfg, base_k, couple_k, base_g, couple_g, & selected_load, closest_load, closest_index, & closest_rel_error, critical_wave, critical_load, ierr) call exit_if_error(ierr) call cyclic_boundary_check(cfg, cyclic_residual, ierr) call exit_if_error(ierr) output_count = min(cfg%mode_count, cfg%sectors) allocate(output_modes(cfg%sectors,output_count), output_values(output_count), & output_residuals(output_count), stat=ierr) if (ierr .ne. 0) then call error_set(FEM_ERR_INTERNAL, 'failed to allocate modal output arrays', ierr) call exit_if_error(ierr) end if output_modes = modes(:,1:output_count) output_values = factors(1:output_count) output_residuals = residuals(1:output_count) call modal_orthogonality(cfg%sectors, kgfull, output_count, output_modes, & max_offdiag, max_diag_error, ierr) call exit_if_error(ierr) call modal_scale_to_max(cfg%sectors, output_count, output_modes, ONE, ierr) call exit_if_error(ierr) call modal_summary(cfg%sectors, output_count, output_values, output_residuals, & output_modes, first_value, last_value, max_residual, & max_component, ierr) call exit_if_error(ierr) path = join_path(out_dir, 'corrugated_buckling_summary.csv') call write_summary(trim(path), cfg, base_k, couple_k, base_g, couple_g, & factors(1), selected_load, closest_load, closest_index, & closest_rel_error, cyclic_residual, max_offdiag, & max_diag_error, first_value, last_value, max_residual, & max_component, critical_wave, critical_load, ierr) call exit_if_error(ierr) path = join_path(out_dir, 'corrugated_buckling_waves.csv') call write_waves(trim(path), cfg, base_k, couple_k, base_g, couple_g, ierr) call exit_if_error(ierr) path = join_path(out_dir, 'corrugated_buckling_modes.csv') call modal_write_csv(trim(path), cfg%sectors, output_count, output_values, & output_residuals, output_modes, ierr) call exit_if_error(ierr) path = join_path(out_dir, 'corrugated_buckling_profile.csv') call write_profile(trim(path), cfg, output_modes(:,1), ierr) call exit_if_error(ierr) write(*,'(A,ES12.5,A,I0,A,ES12.5,A,ES12.5)') & 'corrugated shell critical=', critical_load, ' wave=', critical_wave, & ' selected=', selected_load, ' rel_diff=', closest_rel_error contains subroutine read_shell_input(filename, cfg, ierr) character(len=*), intent(in) :: filename type(shell_input), intent(out) :: cfg integer, intent(out) :: ierr character(len=512) :: line character(len=512) :: upper integer :: unit integer :: ios integer :: data_state call set_shell_defaults(cfg) data_state = 0 unit = 91 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 corrugated buckling 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 corrugated buckling input', ierr) return end if if (is_blank_or_comment(line)) cycle call uppercase(line, upper) if (upper(1:1) .eq. '*') then if (index(upper, '*CORRUGATED SHELL BUCKLING') .eq. 1) data_state = 1 else if (data_state .eq. 1) then read(line,*,iostat=ios) cfg%sectors, cfg%wave, cfg%mode_count, & cfg%bottom_radius, cfg%top_radius, cfg%height, cfg%thickness if (ios .ne. 0) exit data_state = 2 else if (data_state .eq. 2) then read(line,*,iostat=ios) cfg%young, cfg%poisson, cfg%stiffness_coupling, & cfg%geometric_coupling, cfg%corrugation_factor close(unit) if (ios .ne. 0) then call error_set(FEM_ERR_INPUT, 'failed to parse corrugated buckling data', ierr) return end if call validate_shell_input(cfg, ierr) return end if end do close(unit) call validate_shell_input(cfg, ierr) end subroutine read_shell_input subroutine set_shell_defaults(cfg) type(shell_input), intent(out) :: cfg cfg%sectors = 16 cfg%wave = 4 cfg%mode_count = 6 cfg%bottom_radius = 0.031_rk cfg%top_radius = 0.0155_rk cfg%height = 0.050_rk cfg%thickness = 5.0e-4_rk cfg%young = 7.0e10_rk cfg%poisson = 0.30_rk cfg%stiffness_coupling = 0.18_rk cfg%geometric_coupling = 0.42_rk cfg%corrugation_factor = ONE end subroutine set_shell_defaults subroutine validate_shell_input(cfg, ierr) type(shell_input), intent(in) :: cfg integer, intent(out) :: ierr if (cfg%sectors .lt. 3 .or. cfg%sectors .gt. 64 .or. & cfg%mode_count .le. 0 .or. cfg%mode_count .gt. cfg%sectors) then call error_set(FEM_ERR_INPUT, 'invalid corrugated buckling sector or mode count', ierr) return end if call cyclic_validate_wave(cfg%sectors, cfg%wave, ierr) if (ierr .ne. FEM_SUCCESS) return if (cfg%bottom_radius .le. ZERO .or. cfg%top_radius .le. ZERO .or. & cfg%height .le. ZERO .or. cfg%thickness .le. ZERO .or. & cfg%young .le. ZERO) then call error_set(FEM_ERR_INPUT, 'invalid corrugated shell dimensions or stiffness', ierr) return end if if (cfg%poisson .le. -0.95_rk .or. cfg%poisson .ge. 0.49_rk .or. & cfg%stiffness_coupling .lt. ZERO .or. cfg%geometric_coupling .lt. ZERO .or. & cfg%corrugation_factor .lt. ZERO) then call error_set(FEM_ERR_INPUT, 'invalid corrugated buckling material ratios', ierr) return end if call error_clear(ierr) end subroutine validate_shell_input subroutine reduced_shell_constants(cfg, base_k, couple_k, base_g, couple_g, ierr) type(shell_input), intent(in) :: cfg real(rk), intent(out) :: base_k real(rk), intent(out) :: couple_k real(rk), intent(out) :: base_g real(rk), intent(out) :: couple_g integer, intent(out) :: ierr real(rk) :: pi_val real(rk) :: ravg real(rk) :: width real(rk) :: slope real(rk) :: bending pi_val = acos(-ONE) ravg = HALF * (cfg%bottom_radius + cfg%top_radius) width = 2.0_rk * pi_val * ravg / real(cfg%sectors, rk) slope = (cfg%bottom_radius - cfg%top_radius) / cfg%height bending = cfg%young * cfg%thickness ** 3 / & (12.0_rk * (ONE - cfg%poisson * cfg%poisson)) base_k = bending * width * (ONE + slope * slope + cfg%corrugation_factor) / & (cfg%height ** 3) couple_k = cfg%stiffness_coupling * base_k base_g = width * (ONE + slope * slope) / cfg%height couple_g = cfg%geometric_coupling * base_g if (base_k .le. ZERO .or. base_g .le. ZERO) then call error_set(FEM_ERR_SOLVER, 'corrugated reduced matrices are invalid', ierr) return end if call error_clear(ierr) end subroutine reduced_shell_constants subroutine assemble_periodic_ring(n, base_k, couple_k, base_g, couple_g, k, kg) integer, intent(in) :: n real(rk), intent(in) :: base_k real(rk), intent(in) :: couple_k real(rk), intent(in) :: base_g real(rk), intent(in) :: couple_g real(rk), intent(out) :: k(n,n) real(rk), intent(out) :: kg(n,n) integer :: i integer :: ip k = ZERO kg = ZERO do i = 1, n ip = i + 1 if (ip .gt. n) ip = 1 k(i,i) = base_k + 2.0_rk * couple_k kg(i,i) = base_g + 2.0_rk * couple_g k(i,ip) = k(i,ip) - couple_k k(ip,i) = k(ip,i) - couple_k kg(i,ip) = kg(i,ip) - couple_g kg(ip,i) = kg(ip,i) - couple_g end do end subroutine assemble_periodic_ring subroutine selected_wave_data(cfg, base_k, couple_k, base_g, couple_g, & selected_load, closest_load, closest_index, & closest_rel_error, critical_wave, & critical_load, ierr) type(shell_input), intent(in) :: cfg real(rk), intent(in) :: base_k real(rk), intent(in) :: couple_k real(rk), intent(in) :: base_g real(rk), intent(in) :: couple_g real(rk), intent(out) :: selected_load real(rk), intent(out) :: closest_load integer, intent(out) :: closest_index real(rk), intent(out) :: closest_rel_error integer, intent(out) :: critical_wave real(rk), intent(out) :: critical_load integer, intent(out) :: ierr integer :: i real(rk) :: trial real(rk) :: rel selected_load = wave_load(cfg%sectors, cfg%wave, base_k, couple_k, & base_g, couple_g) closest_index = 1 closest_load = factors(1) closest_rel_error = abs(closest_load - selected_load) / selected_load do i = 2, cfg%sectors rel = abs(factors(i) - selected_load) / selected_load if (rel .lt. closest_rel_error) then closest_rel_error = rel closest_load = factors(i) closest_index = i end if end do critical_wave = 0 critical_load = wave_load(cfg%sectors, 0, base_k, couple_k, base_g, couple_g) do i = 1, cfg%sectors / 2 trial = wave_load(cfg%sectors, i, base_k, couple_k, base_g, couple_g) if (trial .lt. critical_load) then critical_load = trial critical_wave = i end if end do call error_clear(ierr) end subroutine selected_wave_data real(rk) function wave_load(n, wave, base_k, couple_k, base_g, couple_g) integer, intent(in) :: n integer, intent(in) :: wave real(rk), intent(in) :: base_k real(rk), intent(in) :: couple_k real(rk), intent(in) :: base_g real(rk), intent(in) :: couple_g real(rk) :: theta real(rk) :: shape theta = 2.0_rk * acos(-ONE) * real(wave, rk) / real(n, rk) shape = ONE - cos(theta) wave_load = (base_k + 2.0_rk * couple_k * shape) / & (base_g + 2.0_rk * couple_g * shape) end function wave_load subroutine cyclic_boundary_check(cfg, residual, ierr) type(shell_input), intent(in) :: cfg real(rk), intent(out) :: residual integer, intent(out) :: ierr real(rk) :: angle real(rk) :: cblock(6,6) real(rk) :: sblock(6,6) real(rk) :: master_r(6) real(rk) :: master_i(6) real(rk) :: slave_r(6) real(rk) :: slave_i(6) call cyclic_sector_angle(cfg%sectors, angle, ierr) if (ierr .ne. FEM_SUCCESS) return call cyclic_harmonic_blocks(6, angle, cfg%wave, cblock, sblock, ierr) if (ierr .ne. FEM_SUCCESS) return master_r = (/ ONE, ZERO, 0.25_rk, 0.10_rk, ZERO, 0.20_rk /) master_i = ZERO call cyclic_apply_harmonic_pair(6, cblock, sblock, master_r, master_i, & slave_r, slave_i, ierr) if (ierr .ne. FEM_SUCCESS) return call cyclic_pair_residual_norm(6, cblock, sblock, master_r, master_i, & slave_r, slave_i, residual, ierr) end subroutine cyclic_boundary_check subroutine write_summary(filename, cfg, base_k, couple_k, base_g, couple_g, & full_first, selected_load, closest_load, & closest_index, closest_rel_error, cyclic_residual, & max_offdiag, max_diag_error, first_value, & last_value, max_residual, max_component, & critical_wave, critical_load, ierr) character(len=*), intent(in) :: filename type(shell_input), intent(in) :: cfg real(rk), intent(in) :: base_k, couple_k, base_g, couple_g real(rk), intent(in) :: full_first, selected_load, closest_load integer, intent(in) :: closest_index real(rk), intent(in) :: closest_rel_error, cyclic_residual real(rk), intent(in) :: max_offdiag, max_diag_error real(rk), intent(in) :: first_value, last_value, max_residual, max_component integer, intent(in) :: critical_wave real(rk), intent(in) :: critical_load integer, intent(out) :: ierr integer :: unit integer :: ios unit = 92 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 corrugated buckling summary', ierr) return end if write(unit,'(A)') 'key,value' write(unit,'(A,I0)') 'sectors,', cfg%sectors write(unit,'(A,I0)') 'selected_wave,', cfg%wave write(unit,'(A,I0)') 'mode_count,', cfg%mode_count write(unit,'(A,ES16.8)') 'base_stiffness,', base_k write(unit,'(A,ES16.8)') 'coupling_stiffness,', couple_k write(unit,'(A,ES16.8)') 'base_geometric,', base_g write(unit,'(A,ES16.8)') 'coupling_geometric,', couple_g write(unit,'(A,ES16.8)') 'full_first_load,', full_first write(unit,'(A,I0)') 'critical_wave,', critical_wave write(unit,'(A,ES16.8)') 'critical_load,', critical_load write(unit,'(A,ES16.8)') 'selected_cyclic_load,', selected_load write(unit,'(A,ES16.8)') 'closest_full_load,', closest_load write(unit,'(A,I0)') 'closest_full_mode_index,', closest_index write(unit,'(A,ES16.8)') 'cyclic_full_relative_error,', closest_rel_error write(unit,'(A,ES16.8)') 'cyclic_boundary_residual,', cyclic_residual write(unit,'(A,ES16.8)') 'modal_orthogonality_offdiag,', max_offdiag write(unit,'(A,ES16.8)') 'modal_orthogonality_diag_error,', max_diag_error write(unit,'(A,ES16.8)') 'modal_first_value,', first_value write(unit,'(A,ES16.8)') 'modal_last_value,', last_value write(unit,'(A,ES16.8)') 'modal_max_residual,', max_residual write(unit,'(A,ES16.8)') 'modal_max_component,', max_component close(unit) call error_clear(ierr) end subroutine write_summary subroutine write_waves(filename, cfg, base_k, couple_k, base_g, couple_g, ierr) character(len=*), intent(in) :: filename type(shell_input), intent(in) :: cfg real(rk), intent(in) :: base_k, couple_k, base_g, couple_g integer, intent(out) :: ierr integer :: unit integer :: ios integer :: wave real(rk) :: load 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 corrugated wave output', ierr) return end if write(unit,'(A)') 'wave,cyclic_load' do wave = 0, cfg%sectors / 2 load = wave_load(cfg%sectors, wave, base_k, couple_k, base_g, couple_g) write(unit,'(I0,",",ES16.8)') wave, load end do close(unit) call error_clear(ierr) end subroutine write_waves subroutine write_profile(filename, cfg, mode, ierr) character(len=*), intent(in) :: filename type(shell_input), intent(in) :: cfg real(rk), intent(in) :: mode(cfg%sectors) integer, intent(out) :: ierr integer :: unit integer :: ios integer :: sector real(rk) :: theta real(rk) :: radius unit = 94 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 corrugated profile output', ierr) return end if radius = HALF * (cfg%bottom_radius + cfg%top_radius) write(unit,'(A)') 'sector,theta,x,y,mode_1' do sector = 1, cfg%sectors theta = 2.0_rk * acos(-ONE) * real(sector - 1, rk) / real(cfg%sectors, rk) write(unit,'(I0,",",ES16.8,",",ES16.8,",",ES16.8,",",ES16.8)') & sector, theta, radius * cos(theta), radius * sin(theta), mode(sector) 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 corr_shell_buckling_bench