program roll_forming_kin_bench use fem_precision use fem_error use fem_rigid_roller use fem_roller_schedule use fem_roll_metrics implicit none type roller_input real(rk) :: radius = 0.05_rk real(rk) :: normal_force = 1000.0_rk real(rk) :: mu = 0.30_rk real(rk) :: feed_rate = 0.20_rk real(rk) :: down_rate = -0.01_rk real(rk) :: angular_speed = 4.0_rk real(rk) :: reference_speed = 0.20_rk real(rk) :: duration = ONE integer :: samples = 6 end type roller_input type(roller_input) :: cfg type(roller_step_state) :: step type(roll_metrics_state) :: metrics character(len=512) :: input_file character(len=512) :: out_dir character(len=512) :: path real(rk) :: spin_slip_sum real(rk) :: no_spin_slip_sum real(rk) :: spin_traction_sum real(rk) :: no_spin_traction_sum real(rk) :: spin_energy real(rk) :: no_spin_energy real(rk) :: max_delta_traction real(rk) :: mean_pressure real(rk) :: contact_width real(rk) :: max_penetration real(rk) :: normal_force_sum real(rk) :: friction_power real(rk) :: mean_slip integer :: active_count integer :: argc integer :: ierr intrinsic iargc intrinsic getarg argc = iargc() if (argc .lt. 2) then write(*,'(A)') 'Usage: roll_forming_kinematics_benchmark INPUT.inp OUTPUT_DIR' stop 2 end if call getarg(1, input_file) call getarg(2, out_dir) call read_roller_input(trim(input_file), cfg, ierr) call exit_if_error(ierr) call roller_step_init(ZERO, cfg%duration, ZERO, cfg%feed_rate * cfg%duration, & ZERO, cfg%down_rate * cfg%duration, & cfg%angular_speed, cfg%angular_speed, & cfg%normal_force, cfg%normal_force, step, ierr) call exit_if_error(ierr) call roll_metrics_reset(metrics) path = join_path(out_dir, 'roller_kinematics_history.csv') call write_history(trim(path), cfg, step, metrics, spin_slip_sum, & no_spin_slip_sum, spin_traction_sum, no_spin_traction_sum, & spin_energy, no_spin_energy, max_delta_traction, ierr) call exit_if_error(ierr) call roll_metrics_summary(metrics, active_count, contact_width, & max_penetration, normal_force_sum, & friction_power, mean_slip, mean_pressure, ierr) call exit_if_error(ierr) path = join_path(out_dir, 'roller_kinematics_summary.csv') call write_summary(trim(path), cfg, active_count, contact_width, & max_penetration, normal_force_sum, friction_power, & mean_slip, mean_pressure, spin_slip_sum, no_spin_slip_sum, & spin_traction_sum, no_spin_traction_sum, spin_energy, & no_spin_energy, max_delta_traction, ierr) call exit_if_error(ierr) write(*,'(A,ES12.5,A,ES12.5,A,ES12.5)') & 'roller mean slip with spin=', spin_slip_sum / real(cfg%samples, rk), & ' no_spin=', no_spin_slip_sum / real(cfg%samples, rk), & ' traction_delta=', max_delta_traction contains subroutine read_roller_input(filename, cfg, ierr) character(len=*), intent(in) :: filename type(roller_input), intent(out) :: cfg integer, intent(out) :: ierr character(len=512) :: line character(len=512) :: upper integer :: unit integer :: ios logical :: want_data call set_roller_defaults(cfg) want_data = .false. unit = 88 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 roller kinematics 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 roller kinematics 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, '*ROLLER KINEMATICS') .eq. 1 else if (want_data) then read(line,*,iostat=ios) cfg%radius, cfg%normal_force, cfg%mu, & cfg%feed_rate, cfg%down_rate, cfg%angular_speed, & cfg%reference_speed, cfg%duration, cfg%samples close(unit) if (ios .ne. 0) then call error_set(FEM_ERR_INPUT, 'failed to parse roller kinematics data', ierr) return end if call validate_roller_input(cfg, ierr) return end if end do close(unit) call validate_roller_input(cfg, ierr) end subroutine read_roller_input subroutine set_roller_defaults(cfg) type(roller_input), intent(out) :: cfg cfg%radius = 0.05_rk cfg%normal_force = 1000.0_rk cfg%mu = 0.30_rk cfg%feed_rate = 0.20_rk cfg%down_rate = -0.01_rk cfg%angular_speed = 4.0_rk cfg%reference_speed = 0.20_rk cfg%duration = ONE cfg%samples = 6 end subroutine set_roller_defaults subroutine validate_roller_input(cfg, ierr) type(roller_input), intent(in) :: cfg integer, intent(out) :: ierr if (cfg%radius .le. ZERO .or. cfg%normal_force .lt. ZERO .or. & cfg%mu .lt. ZERO .or. cfg%reference_speed .lt. ZERO .or. & cfg%duration .le. ZERO .or. cfg%samples .lt. 2) then call error_set(FEM_ERR_INPUT, 'invalid roller kinematics benchmark data', ierr) return end if call error_clear(ierr) end subroutine validate_roller_input subroutine write_history(filename, cfg, step, metrics, spin_slip_sum, & no_spin_slip_sum, spin_traction_sum, & no_spin_traction_sum, spin_energy, no_spin_energy, & max_delta_traction, ierr) character(len=*), intent(in) :: filename type(roller_input), intent(in) :: cfg type(roller_step_state), intent(in) :: step type(roll_metrics_state), intent(inout) :: metrics real(rk), intent(out) :: spin_slip_sum real(rk), intent(out) :: no_spin_slip_sum real(rk), intent(out) :: spin_traction_sum real(rk), intent(out) :: no_spin_traction_sum real(rk), intent(out) :: spin_energy real(rk), intent(out) :: no_spin_energy real(rk), intent(out) :: max_delta_traction integer, intent(out) :: ierr integer :: unit integer :: ios integer :: i real(rk) :: dt real(rk) :: time real(rk) :: row(10) spin_slip_sum = ZERO no_spin_slip_sum = ZERO spin_traction_sum = ZERO no_spin_traction_sum = ZERO spin_energy = ZERO no_spin_energy = ZERO max_delta_traction = ZERO dt = cfg%duration / real(cfg%samples - 1, rk) unit = 89 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 roller kinematics history', ierr) return end if write(unit,'(A)') 'sample,time,feed,down,spin,slip_with_spin,' // & 'slip_without_spin,traction_with_spin,traction_without_spin,' // & 'friction_energy_with_spin,friction_energy_without_spin' do i = 1, cfg%samples time = dt * real(i - 1, rk) call evaluate_sample(cfg, step, time, dt, metrics, row, ierr) if (ierr .ne. FEM_SUCCESS) then close(unit) return end if spin_slip_sum = spin_slip_sum + row(4) no_spin_slip_sum = no_spin_slip_sum + row(5) spin_traction_sum = spin_traction_sum + row(6) no_spin_traction_sum = no_spin_traction_sum + row(7) spin_energy = spin_energy + row(8) no_spin_energy = no_spin_energy + row(9) max_delta_traction = max(max_delta_traction, abs(row(6) - row(7))) write(unit,'(I0,10(",",ES16.8))') i, time, row(1), row(2), row(3), & row(4), row(5), row(6), row(7), row(8), row(9) end do close(unit) call error_clear(ierr) end subroutine write_history subroutine evaluate_sample(cfg, step, time, dt, metrics, row, ierr) type(roller_input), intent(in) :: cfg type(roller_step_state), intent(in) :: step real(rk), intent(in) :: time real(rk), intent(in) :: dt type(roll_metrics_state), intent(inout) :: metrics real(rk), intent(out) :: row(10) integer, intent(out) :: ierr real(rk) :: center0(3), center(3), axis(3), feed_dir(3), down_dir(3) real(rk) :: axis_unit(3), velocity(3), point(3), body_velocity(3) real(rk) :: normal(3), roll_dir(3), axial_dir(3) real(rk) :: rel_spin(3), rel_no_spin(3), tangent_spin(3), tangent_no_spin(3) real(rk) :: force_spin(3), force_no_spin(3) real(rk) :: feed, down, spin, pressure, gap, penetration real(rk) :: normal_speed, slip_spin, slip_no_spin, power row = ZERO center0 = ZERO axis = (/ ZERO, ZERO, ONE /) body_velocity = ZERO call roller_build_feed_frame(axis, (/ ZERO, ONE, ZERO /), axis_unit, & feed_dir, down_dir, ierr) if (ierr .ne. FEM_SUCCESS) return call roller_step_eval(step, time, feed, down, spin, pressure, ierr) if (ierr .ne. FEM_SUCCESS) return call roller_pose_linear(center0, feed_dir, down_dir, feed, down, center, ierr) if (ierr .ne. FEM_SUCCESS) return call roller_step_velocity(feed_dir, down_dir, cfg%feed_rate, cfg%down_rate, & velocity, ierr) if (ierr .ne. FEM_SUCCESS) return point = center + cfg%radius * (/ ONE, ZERO, ZERO /) call roller_contact_kin(point, body_velocity, center, axis, cfg%radius, & velocity, spin, gap, penetration, normal, roll_dir, & axial_dir, rel_spin, normal_speed, slip_spin, ierr) if (ierr .ne. FEM_SUCCESS) return tangent_spin = rel_spin - normal_speed * normal call roller_contact_kin(point, body_velocity, center, axis, cfg%radius, & velocity, ZERO, gap, penetration, normal, roll_dir, & axial_dir, rel_no_spin, normal_speed, slip_no_spin, ierr) if (ierr .ne. FEM_SUCCESS) return tangent_no_spin = rel_no_spin - normal_speed * normal call roller_regularized_traction(cfg%normal_force, cfg%mu, tangent_spin, & cfg%reference_speed, force_spin, ierr) if (ierr .ne. FEM_SUCCESS) return call roller_regularized_traction(cfg%normal_force, cfg%mu, tangent_no_spin, & cfg%reference_speed, force_no_spin, ierr) if (ierr .ne. FEM_SUCCESS) return call roller_friction_work_rate(tangent_spin, force_spin, power, ierr) if (ierr .ne. FEM_SUCCESS) return call roll_metrics_add_sample(metrics, .true., penetration, ZERO, & cfg%normal_force, slip_spin, & norm3(force_spin), cfg%radius * cfg%radius, ierr) if (ierr .ne. FEM_SUCCESS) return row(1) = feed row(2) = down row(3) = spin row(4) = slip_spin row(5) = slip_no_spin row(6) = norm3(force_spin) row(7) = norm3(force_no_spin) row(8) = max(ZERO, -power * dt) call roller_friction_work_rate(tangent_no_spin, force_no_spin, power, ierr) if (ierr .ne. FEM_SUCCESS) return row(9) = max(ZERO, -power * dt) row(10) = cfg%radius * row(6) call error_clear(ierr) end subroutine evaluate_sample subroutine write_summary(filename, cfg, active_count, contact_width, & max_penetration, normal_force_sum, friction_power, & mean_slip, mean_pressure, spin_slip_sum, & no_spin_slip_sum, spin_traction_sum, & no_spin_traction_sum, spin_energy, no_spin_energy, & max_delta_traction, ierr) character(len=*), intent(in) :: filename type(roller_input), intent(in) :: cfg integer, intent(in) :: active_count real(rk), intent(in) :: contact_width, max_penetration, normal_force_sum real(rk), intent(in) :: friction_power, mean_slip, mean_pressure real(rk), intent(in) :: spin_slip_sum, no_spin_slip_sum real(rk), intent(in) :: spin_traction_sum, no_spin_traction_sum real(rk), intent(in) :: spin_energy, no_spin_energy, max_delta_traction integer, intent(out) :: ierr integer :: unit integer :: ios unit = 90 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 roller kinematics summary', ierr) return end if write(unit,'(A)') 'key,value' write(unit,'(A,I0)') 'samples,', cfg%samples write(unit,'(A,I0)') 'active_count,', active_count write(unit,'(A,ES16.8)') 'radius,', cfg%radius write(unit,'(A,ES16.8)') 'normal_force,', cfg%normal_force write(unit,'(A,ES16.8)') 'friction_coefficient,', cfg%mu write(unit,'(A,ES16.8)') 'feed_rate,', cfg%feed_rate write(unit,'(A,ES16.8)') 'down_rate,', cfg%down_rate write(unit,'(A,ES16.8)') 'angular_speed,', cfg%angular_speed write(unit,'(A,ES16.8)') 'mean_slip_with_spin,', spin_slip_sum / real(cfg%samples, rk) write(unit,'(A,ES16.8)') 'mean_slip_without_spin,', no_spin_slip_sum / real(cfg%samples, rk) write(unit,'(A,ES16.8)') 'mean_traction_with_spin,', spin_traction_sum / real(cfg%samples, rk) write(unit,'(A,ES16.8)') 'mean_traction_without_spin,', no_spin_traction_sum / real(cfg%samples, rk) write(unit,'(A,ES16.8)') 'max_delta_traction,', max_delta_traction write(unit,'(A,ES16.8)') 'friction_energy_with_spin,', spin_energy write(unit,'(A,ES16.8)') 'friction_energy_without_spin,', no_spin_energy write(unit,'(A,ES16.8)') 'roller_torque_with_spin,', cfg%radius * spin_traction_sum / real(cfg%samples, rk) write(unit,'(A,ES16.8)') 'contact_width,', contact_width write(unit,'(A,ES16.8)') 'max_penetration,', max_penetration write(unit,'(A,ES16.8)') 'normal_force_sum,', normal_force_sum write(unit,'(A,ES16.8)') 'friction_power_metric,', friction_power write(unit,'(A,ES16.8)') 'mean_slip_metric,', mean_slip write(unit,'(A,ES16.8)') 'mean_pressure,', mean_pressure close(unit) call error_clear(ierr) end subroutine write_summary real(rk) function norm3(vector) real(rk), intent(in) :: vector(3) norm3 = sqrt(dot_product(vector, vector)) end function norm3 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 roll_forming_kin_bench