program test_shell_finite_tangent use fem_precision use fem_error use fem_shell_finite_kinematics use fem_shell_finite_tangent implicit none real(rk), parameter :: TANGENT_TOL = 5.0e-5_rk call check_tangent_fd_warped() call check_tangent_fd_spherical() contains subroutine check_tangent_fd_warped() real(rk) :: ref_xyz(NDIM,4), cur_xyz(NDIM,4) real(rk) :: ref_dir(NDIM,4), cur_dir(NDIM,4) real(rk) :: natural(NDIM) real(rk) :: rotvec(NDIM) integer :: node call warped_patch(ref_xyz, ref_dir) cur_xyz = ref_xyz do node = 1, 4 cur_xyz(1,node) = cur_xyz(1,node) + 0.09_rk * ref_xyz(1,node) - & 0.02_rk * ref_xyz(2,node) cur_xyz(2,node) = cur_xyz(2,node) + 0.05_rk * ref_xyz(2,node) cur_xyz(3,node) = cur_xyz(3,node) + 0.03_rk * ref_xyz(1,node) + & 0.04_rk * ref_xyz(1,node) * ref_xyz(2,node) rotvec = (/ 0.02_rk * real(node, rk), & 0.03_rk - 0.01_rk * real(node, rk), & -0.015_rk * real(node - 2, rk) /) call rotate_by_rotvec(rotvec, ref_dir(:,node), cur_dir(:,node)) end do natural = (/ -0.19_rk, 0.33_rk, ZERO /) call assert_tangent_fd(ref_xyz, cur_xyz, ref_dir, cur_dir, natural, 10) end subroutine check_tangent_fd_warped subroutine check_tangent_fd_spherical() real(rk) :: ref_xyz(NDIM,4), cur_xyz(NDIM,4) real(rk) :: ref_dir(NDIM,4), cur_dir(NDIM,4) real(rk) :: natural(NDIM) real(rk) :: rotvec(NDIM) integer :: node call spherical_cap_patch(ref_xyz, ref_dir) cur_xyz = ref_xyz do node = 1, 4 cur_xyz(1,node) = cur_xyz(1,node) - 0.02_rk * real(node, rk) cur_xyz(2,node) = cur_xyz(2,node) + 0.05_rk * real(node - 2, rk) cur_xyz(3,node) = cur_xyz(3,node) - 0.06_rk + & 0.015_rk * ref_xyz(2,node) rotvec = (/ -0.025_rk + 0.004_rk * real(node, rk), & 0.018_rk * real(node - 1, rk), & 0.012_rk * real(2 - node, rk) /) call rotate_by_rotvec(rotvec, ref_dir(:,node), cur_dir(:,node)) end do natural = (/ 0.41_rk, -0.27_rk, ZERO /) call assert_tangent_fd(ref_xyz, cur_xyz, ref_dir, cur_dir, natural, 100) end subroutine check_tangent_fd_spherical subroutine assert_tangent_fd(ref_xyz, cur_xyz, ref_dir, cur_dir, natural, code) real(rk), intent(in) :: ref_xyz(NDIM,4), cur_xyz(NDIM,4) real(rk), intent(in) :: ref_dir(NDIM,4), cur_dir(NDIM,4) real(rk), intent(in) :: natural(NDIM) integer, intent(in) :: code real(rk) :: dgen(8,8) real(rk) :: dmem(3,3), dcpl(3,3), dben(3,3), dshr(2,2) real(rk) :: strain(8) real(rk) :: resultant(8) real(rk) :: fint(24) real(rk) :: ktan(24,24) real(rk) :: fplus(24) real(rk) :: fminus(24) real(rk) :: fd(24) real(rk) :: delta(24) real(rk) :: detj real(rk) :: weight real(rk) :: h real(rk) :: err real(rk) :: scale integer :: ierr integer :: col call build_section_matrix(dmem, dcpl, dben, dshr) call shell4_finite_section_matrix(dmem, dcpl, dben, dshr, dgen) weight = 0.73_rk h = 1.0e-6_rk call compute_force(ref_xyz, cur_xyz, ref_dir, cur_dir, natural, weight, & dgen, fint, strain, resultant, ierr) if (ierr .ne. FEM_SUCCESS) call fail(code) call shell4_finite_tangent_gp(ref_xyz, cur_xyz, ref_dir, cur_dir, & natural, weight, dgen, resultant, & strain, fint, ktan, detj, ierr) if (ierr .ne. FEM_SUCCESS) call fail(code + 1) if (detj .le. ZERO) call fail(code + 2) do col = 1, 24 delta = ZERO delta(col) = h call compute_force_delta(ref_xyz, cur_xyz, ref_dir, cur_dir, natural, & weight, dgen, delta, fplus, ierr) if (ierr .ne. FEM_SUCCESS) call fail(code + 3) delta(col) = -h call compute_force_delta(ref_xyz, cur_xyz, ref_dir, cur_dir, natural, & weight, dgen, delta, fminus, ierr) if (ierr .ne. FEM_SUCCESS) call fail(code + 4) fd = (fplus - fminus) / (2.0_rk * h) err = maxval(abs(fd - ktan(:,col))) scale = ONE + max(maxval(abs(fd)), maxval(abs(ktan(:,col)))) if (err .gt. TANGENT_TOL * scale) then write(*,*) 'finite shell tangent audit failed: code=', code, & ' col=', col, ' err=', err, ' scale=', scale call fail(code + 5) end if end do end subroutine assert_tangent_fd subroutine compute_force(ref_xyz, cur_xyz, ref_dir, cur_dir, natural, & weight, dgen, force, strain, resultant, ierr) real(rk), intent(in) :: ref_xyz(NDIM,4), cur_xyz(NDIM,4) real(rk), intent(in) :: ref_dir(NDIM,4), cur_dir(NDIM,4) real(rk), intent(in) :: natural(NDIM) real(rk), intent(in) :: weight real(rk), intent(in) :: dgen(8,8) real(rk), intent(out) :: force(24) real(rk), intent(out) :: strain(8) real(rk), intent(out) :: resultant(8) integer, intent(out) :: ierr real(rk) :: bmat(8,24) real(rk) :: detj call generalized_strain(ref_xyz, cur_xyz, ref_dir, cur_dir, natural, & strain, ierr) if (ierr .ne. FEM_SUCCESS) return call shell4_finite_bmat_at(ref_xyz, cur_xyz, ref_dir, cur_dir, natural, & bmat, detj, ierr) if (ierr .ne. FEM_SUCCESS) return resultant = matmul(dgen, strain) force = weight * matmul(transpose(bmat), resultant) ierr = FEM_SUCCESS end subroutine compute_force subroutine compute_force_delta(ref_xyz, cur_xyz, ref_dir, cur_dir, natural, & weight, dgen, delta, force, ierr) real(rk), intent(in) :: ref_xyz(NDIM,4), cur_xyz(NDIM,4) real(rk), intent(in) :: ref_dir(NDIM,4), cur_dir(NDIM,4) real(rk), intent(in) :: natural(NDIM) real(rk), intent(in) :: weight real(rk), intent(in) :: dgen(8,8) real(rk), intent(in) :: delta(24) real(rk), intent(out) :: force(24) integer, intent(out) :: ierr real(rk) :: pert_xyz(NDIM,4) real(rk) :: pert_dir(NDIM,4) real(rk) :: strain(8) real(rk) :: resultant(8) call perturb_state(cur_xyz, cur_dir, delta, pert_xyz, pert_dir) call compute_force(ref_xyz, pert_xyz, ref_dir, pert_dir, natural, weight, & dgen, force, strain, resultant, ierr) end subroutine compute_force_delta subroutine generalized_strain(ref_xyz, cur_xyz, ref_dir, cur_dir, natural, & generalized, ierr) real(rk), intent(in) :: ref_xyz(NDIM,4), cur_xyz(NDIM,4) real(rk), intent(in) :: ref_dir(NDIM,4), cur_dir(NDIM,4) real(rk), intent(in) :: natural(NDIM) real(rk), intent(out) :: generalized(8) integer, intent(out) :: ierr real(rk) :: strain_mem(3), strain_ben(3) real(rk) :: shear_direct(2), shear_mitc(2) real(rk) :: ref_cov(NDIM,2), cur_cov(NDIM,2) real(rk) :: ref_metric(3), cur_metric(3), detj call shell4_finite_kinematics_at(ref_xyz, cur_xyz, ref_dir, cur_dir, & natural, ref_cov, cur_cov, ref_metric, cur_metric, strain_mem, & strain_ben, shear_direct, shear_mitc, detj, ierr) if (ierr .ne. FEM_SUCCESS) return generalized(1:3) = strain_mem generalized(4:6) = strain_ben generalized(7:8) = shear_mitc end subroutine generalized_strain subroutine perturb_state(cur_xyz, cur_dir, delta, pert_xyz, pert_dir) real(rk), intent(in) :: cur_xyz(NDIM,4), cur_dir(NDIM,4) real(rk), intent(in) :: delta(24) real(rk), intent(out) :: pert_xyz(NDIM,4), pert_dir(NDIM,4) real(rk) :: rotvec(NDIM) integer :: node integer :: base pert_xyz = cur_xyz pert_dir = cur_dir do node = 1, 4 base = 6 * (node - 1) pert_xyz(1,node) = cur_xyz(1,node) + delta(base + 1) pert_xyz(2,node) = cur_xyz(2,node) + delta(base + 2) pert_xyz(3,node) = cur_xyz(3,node) + delta(base + 3) rotvec = (/ delta(base + 4), delta(base + 5), delta(base + 6) /) call rotate_by_rotvec(rotvec, cur_dir(:,node), pert_dir(:,node)) end do end subroutine perturb_state subroutine build_section_matrix(dmem, dcpl, dben, dshr) real(rk), intent(out) :: dmem(3,3), dcpl(3,3), dben(3,3), dshr(2,2) dmem = ZERO dcpl = ZERO dben = ZERO dshr = ZERO dmem(1,1) = 21.0_rk dmem(1,2) = 4.0_rk dmem(2,1) = 4.0_rk dmem(2,2) = 19.0_rk dmem(3,3) = 7.5_rk dcpl(1,1) = 0.35_rk dcpl(1,2) = 0.08_rk dcpl(2,1) = 0.06_rk dcpl(2,2) = -0.24_rk dcpl(3,3) = 0.11_rk dben(1,1) = 1.35_rk dben(1,2) = 0.18_rk dben(2,1) = 0.18_rk dben(2,2) = 1.18_rk dben(3,3) = 0.52_rk dshr(1,1) = 5.0_rk dshr(1,2) = 0.15_rk dshr(2,1) = 0.15_rk dshr(2,2) = 4.6_rk end subroutine build_section_matrix subroutine warped_patch(ref_xyz, ref_dir) real(rk), intent(out) :: ref_xyz(NDIM,4) real(rk), intent(out) :: ref_dir(NDIM,4) integer :: node ref_xyz(:,1) = (/ ZERO, ZERO, ZERO /) ref_xyz(:,2) = (/ 1.20_rk, 0.10_rk, 0.20_rk /) ref_xyz(:,3) = (/ 1.05_rk, 1.10_rk, 0.45_rk /) ref_xyz(:,4) = (/ -0.10_rk, 0.90_rk, 0.15_rk /) do node = 1, 4 ref_dir(:,node) = (/ 0.05_rk * real(node - 2, rk), & -0.03_rk * real(node - 1, rk), ONE /) call normalize(ref_dir(:,node)) end do end subroutine warped_patch subroutine spherical_cap_patch(ref_xyz, ref_dir) real(rk), intent(out) :: ref_xyz(NDIM,4) real(rk), intent(out) :: ref_dir(NDIM,4) real(rk) :: radius, x, y integer :: node radius = 10.0_rk ref_xyz(:,1) = cap_point(radius, -0.60_rk, -0.40_rk) ref_xyz(:,2) = cap_point(radius, 0.55_rk, -0.35_rk) ref_xyz(:,3) = cap_point(radius, 0.50_rk, 0.45_rk) ref_xyz(:,4) = cap_point(radius, -0.65_rk, 0.40_rk) do node = 1, 4 x = ref_xyz(1,node) y = ref_xyz(2,node) ref_dir(:,node) = (/ x, y, ref_xyz(3,node) - radius /) call normalize(ref_dir(:,node)) end do end subroutine spherical_cap_patch function cap_point(radius, x, y) result(point) real(rk), intent(in) :: radius, x, y real(rk) :: point(NDIM) point(1) = x point(2) = y point(3) = radius - sqrt(radius * radius - x * x - y * y) end function cap_point subroutine rotate_by_rotvec(rotvec, vec, rotated) real(rk), intent(in) :: rotvec(NDIM) real(rk), intent(in) :: vec(NDIM) real(rk), intent(out) :: rotated(NDIM) real(rk) :: axis(NDIM) real(rk) :: cross(NDIM) real(rk) :: angle real(rk) :: c real(rk) :: s angle = sqrt(dot_product(rotvec, rotvec)) if (angle .le. 1.0e-14_rk) then call cross_product(rotvec, vec, cross) rotated = vec + cross return end if axis = rotvec / angle call cross_product(axis, vec, cross) c = cos(angle) s = sin(angle) rotated = c * vec + s * cross + & (ONE - c) * dot_product(axis, vec) * axis end subroutine rotate_by_rotvec subroutine cross_product(a, b, c) real(rk), intent(in) :: a(NDIM) real(rk), intent(in) :: b(NDIM) real(rk), intent(out) :: c(NDIM) c(1) = a(2) * b(3) - a(3) * b(2) c(2) = a(3) * b(1) - a(1) * b(3) c(3) = a(1) * b(2) - a(2) * b(1) end subroutine cross_product subroutine normalize(vec) real(rk), intent(inout) :: vec(NDIM) real(rk) :: length length = sqrt(dot_product(vec, vec)) if (length .le. ZERO) call fail(900) vec = vec / length end subroutine normalize subroutine fail(code) integer, intent(in) :: code write(*,*) 'test_shell_finite_tangent failed:', code stop 1 end subroutine fail end program test_shell_finite_tangent