diff --git a/CMakeLists.txt b/CMakeLists.txt index daeff50..6d4f4c4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -89,6 +89,13 @@ add_executable(test_element_infrastructure target_link_libraries(test_element_infrastructure PRIVATE femlite_core) femlite_apply_fortran_options(test_element_infrastructure) +add_executable(test_facet_topology + tests/module/test_facet_topology.f90 +) + +target_link_libraries(test_facet_topology PRIVATE femlite_core) +femlite_apply_fortran_options(test_facet_topology) + add_executable(perf_p1 tests/performance/perf_p1.f90 ) @@ -110,3 +117,8 @@ add_test(NAME element_infrastructure COMMAND test_element_infrastructure) set_tests_properties(element_infrastructure PROPERTIES WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" ) + +add_test(NAME facet_topology COMMAND test_facet_topology) +set_tests_properties(facet_topology PROPERTIES + WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" +) diff --git a/src/coupling/fem_coupling.f90 b/src/coupling/fem_coupling.f90 index 719089a..d1eaa10 100644 --- a/src/coupling/fem_coupling.f90 +++ b/src/coupling/fem_coupling.f90 @@ -2,6 +2,7 @@ module fem_coupling use fem_precision use fem_error use fem_database + use fem_element_topology implicit none private @@ -27,6 +28,9 @@ contains integer :: table_size integer :: stat integer :: elem + integer :: face + integer :: num_faces + integer :: num_face_nodes integer :: face_count integer :: out_count integer :: local_nodes(MAX_FACE_NODES) @@ -70,120 +74,32 @@ contains do elem = 1, nelem if (elem_status(elem) .ne. 0) cycle - select case (trim(adjustl(elem_type(elem)))) - case ('C3D4') - local_nodes = 0 - local_nodes(1) = 1 - local_nodes(2) = 2 - local_nodes(3) = 3 - call add_solid_face(elem, 1, 3, local_nodes, face_count, hash, & - table_size, cand_elem, cand_face, cand_nnode, & - cand_count, cand_conn, cand_key, ierr) - if (ierr .ne. FEM_SUCCESS) return - - local_nodes = 0 - local_nodes(1) = 1 - local_nodes(2) = 2 - local_nodes(3) = 4 - call add_solid_face(elem, 2, 3, local_nodes, face_count, hash, & - table_size, cand_elem, cand_face, cand_nnode, & - cand_count, cand_conn, cand_key, ierr) - if (ierr .ne. FEM_SUCCESS) return - - local_nodes = 0 - local_nodes(1) = 1 - local_nodes(2) = 3 - local_nodes(3) = 4 - call add_solid_face(elem, 3, 3, local_nodes, face_count, hash, & - table_size, cand_elem, cand_face, cand_nnode, & - cand_count, cand_conn, cand_key, ierr) - if (ierr .ne. FEM_SUCCESS) return - - local_nodes = 0 - local_nodes(1) = 2 - local_nodes(2) = 3 - local_nodes(3) = 4 - call add_solid_face(elem, 4, 3, local_nodes, face_count, hash, & - table_size, cand_elem, cand_face, cand_nnode, & - cand_count, cand_conn, cand_key, ierr) - if (ierr .ne. FEM_SUCCESS) return - - case ('C3D8') - local_nodes = 0 - local_nodes(1) = 1 - local_nodes(2) = 2 - local_nodes(3) = 3 - local_nodes(4) = 4 - call add_solid_face(elem, 1, 4, local_nodes, face_count, hash, & - table_size, cand_elem, cand_face, cand_nnode, & - cand_count, cand_conn, cand_key, ierr) - if (ierr .ne. FEM_SUCCESS) return - - local_nodes = 0 - local_nodes(1) = 5 - local_nodes(2) = 6 - local_nodes(3) = 7 - local_nodes(4) = 8 - call add_solid_face(elem, 2, 4, local_nodes, face_count, hash, & - table_size, cand_elem, cand_face, cand_nnode, & - cand_count, cand_conn, cand_key, ierr) - if (ierr .ne. FEM_SUCCESS) return - - local_nodes = 0 - local_nodes(1) = 1 - local_nodes(2) = 2 - local_nodes(3) = 6 - local_nodes(4) = 5 - call add_solid_face(elem, 3, 4, local_nodes, face_count, hash, & - table_size, cand_elem, cand_face, cand_nnode, & - cand_count, cand_conn, cand_key, ierr) - if (ierr .ne. FEM_SUCCESS) return - - local_nodes = 0 - local_nodes(1) = 2 - local_nodes(2) = 3 - local_nodes(3) = 7 - local_nodes(4) = 6 - call add_solid_face(elem, 4, 4, local_nodes, face_count, hash, & - table_size, cand_elem, cand_face, cand_nnode, & - cand_count, cand_conn, cand_key, ierr) - if (ierr .ne. FEM_SUCCESS) return - - local_nodes = 0 - local_nodes(1) = 3 - local_nodes(2) = 4 - local_nodes(3) = 8 - local_nodes(4) = 7 - call add_solid_face(elem, 5, 4, local_nodes, face_count, hash, & - table_size, cand_elem, cand_face, cand_nnode, & - cand_count, cand_conn, cand_key, ierr) - if (ierr .ne. FEM_SUCCESS) return - - local_nodes = 0 - local_nodes(1) = 4 - local_nodes(2) = 1 - local_nodes(3) = 5 - local_nodes(4) = 8 - call add_solid_face(elem, 6, 4, local_nodes, face_count, hash, & - table_size, cand_elem, cand_face, cand_nnode, & - cand_count, cand_conn, cand_key, ierr) - if (ierr .ne. FEM_SUCCESS) return - - case ('S4') - local_nodes = 0 - local_nodes(1) = 1 - local_nodes(2) = 2 - local_nodes(3) = 3 - local_nodes(4) = 4 - call add_shell_face(elem, 1, 4, local_nodes, face_count, cand_elem, & - cand_face, cand_nnode, cand_count, cand_conn, & - cand_key, ierr) - if (ierr .ne. FEM_SUCCESS) return - - case default + num_faces = element_num_faces(elem_type(elem)) + if (num_faces .le. 0) then call error_set(FEM_ERR_MESH, 'unsupported element type for boundary facets', ierr) return - end select + end if + + do face = 1, num_faces + call element_face_nodes(elem_type(elem), face, num_face_nodes, & + local_nodes, ierr) + if (ierr .ne. FEM_SUCCESS) return + + if (element_is_solid(elem_type(elem))) then + call add_solid_face(elem, face, num_face_nodes, local_nodes, & + face_count, hash, table_size, cand_elem, & + cand_face, cand_nnode, cand_count, cand_conn, & + cand_key, ierr) + else if (element_is_shell(elem_type(elem))) then + call add_shell_face(elem, face, num_face_nodes, local_nodes, & + face_count, cand_elem, cand_face, cand_nnode, & + cand_count, cand_conn, cand_key, ierr) + else + call error_set(FEM_ERR_MESH, 'unsupported element kind for boundary facets', ierr) + return + end if + if (ierr .ne. FEM_SUCCESS) return + end do end do out_count = 0 @@ -395,21 +311,17 @@ contains integer, intent(out) :: max_faces integer, intent(out) :: ierr integer :: elem + integer :: faces max_faces = 0 do elem = 1, nelem if (elem_status(elem) .ne. 0) cycle - select case (trim(adjustl(elem_type(elem)))) - case ('C3D4') - max_faces = max_faces + 4 - case ('C3D8') - max_faces = max_faces + 6 - case ('S4') - max_faces = max_faces + 1 - case default + faces = element_num_faces(elem_type(elem)) + if (faces .le. 0) then call error_set(FEM_ERR_MESH, 'unsupported element type for boundary facets', ierr) return - end select + end if + max_faces = max_faces + faces end do ierr = FEM_SUCCESS @@ -603,11 +515,11 @@ contains real(rk) :: total_normal(NDIM) real(rk) :: tri_area - if (num_face_nodes .eq. 3) then + if (num_face_nodes .eq. 3 .or. num_face_nodes .eq. 6) then call triangle_normal_area(coords(:,1), coords(:,2), coords(:,3), & normal, area, ierr) return - else if (num_face_nodes .eq. 4) then + else if (num_face_nodes .eq. 4 .or. num_face_nodes .eq. 8) then call triangle_normal_area(coords(:,1), coords(:,2), coords(:,3), & total_normal, area, ierr) if (ierr .ne. FEM_SUCCESS) return @@ -672,9 +584,21 @@ contains integer :: local_node tmp = nodes - do local_node = 1, num_face_nodes - nodes(local_node) = tmp(num_face_nodes - local_node + 1) - end do + select case (num_face_nodes) + case (3) + nodes(1:3) = (/ tmp(3), tmp(2), tmp(1) /) + case (4) + nodes(1:4) = (/ tmp(4), tmp(3), tmp(2), tmp(1) /) + case (6) + nodes(1:6) = (/ tmp(3), tmp(2), tmp(1), tmp(5), tmp(4), tmp(6) /) + case (8) + nodes(1:8) = (/ tmp(4), tmp(3), tmp(2), tmp(1), & + tmp(7), tmp(6), tmp(5), tmp(8) /) + case default + do local_node = 1, num_face_nodes + nodes(local_node) = tmp(num_face_nodes - local_node + 1) + end do + end select end subroutine reverse_face_nodes subroutine sort_face_key(key, num_face_nodes) diff --git a/src/element/fem_element_internal.f90 b/src/element/fem_element_internal.f90 index d42c98e..723043e 100644 --- a/src/element/fem_element_internal.f90 +++ b/src/element/fem_element_internal.f90 @@ -3,6 +3,8 @@ module fem_element_internal use fem_error use fem_database use fem_element_topology + use fem_quadrature + use fem_shape_functions use fem_material_j2 implicit none @@ -193,10 +195,8 @@ contains integer, intent(in) :: elem real(rk), intent(in) :: dt integer, intent(out) :: ierr - real(rk) :: xi_node(8) - real(rk) :: eta_node(8) - real(rk) :: zeta_node(8) - real(rk) :: gp_loc(2) + real(rk) :: gp_nat(NDIM,8) + real(rk) :: gp_weight(8) real(rk) :: dndr(NDIM,8) real(rk) :: dndx(NDIM,8) real(rk) :: detj @@ -213,9 +213,8 @@ contains real(rk) :: s real(rk) :: t real(rk) :: weight - integer :: i - integer :: j - integer :: k + integer :: q + integer :: npoint integer :: local_node integer :: node integer :: gp @@ -228,92 +227,86 @@ contains return end if - call hex8_natural_coords(xi_node, eta_node, zeta_node) - gp_loc(1) = -ONE / sqrt(3.0_rk) - gp_loc(2) = ONE / sqrt(3.0_rk) + call quadrature_get_rule('C3D8', 8, npoint, gp_nat, gp_weight, ierr) + if (ierr .ne. FEM_SUCCESS) return gp_start = elem_gp_start(elem) gp = gp_start failed_count = 0 local_force = ZERO - do i = 1, 2 - r = gp_loc(i) - do j = 1, 2 - s = gp_loc(j) - do k = 1, 2 - t = gp_loc(k) - weight = ONE + do q = 1, npoint + r = gp_nat(1,q) + s = gp_nat(2,q) + t = gp_nat(3,q) + weight = gp_weight(q) - call hex8_shape_gradients(elem, r, s, t, xi_node, eta_node, & - zeta_node, dndr, dndx, detj, ierr) - if (ierr .ne. FEM_SUCCESS) return + call hex8_shape_gradients(elem, r, s, t, dndr, dndx, detj, ierr) + if (ierr .ne. FEM_SUCCESS) return - eps = ZERO - do local_node = 1, 8 - node = elem_conn(local_node, elem) - eps(1) = eps(1) + dndx(1,local_node) * get_dof_value(u, 1, node) - eps(2) = eps(2) + dndx(2,local_node) * get_dof_value(u, 2, node) - eps(3) = eps(3) + dndx(3,local_node) * get_dof_value(u, 3, node) - eps(4) = eps(4) + dndx(2,local_node) * get_dof_value(u, 1, node) + & - dndx(1,local_node) * get_dof_value(u, 2, node) - eps(5) = eps(5) + dndx(3,local_node) * get_dof_value(u, 1, node) + & - dndx(1,local_node) * get_dof_value(u, 3, node) - eps(6) = eps(6) + dndx(3,local_node) * get_dof_value(u, 2, node) + & - dndx(2,local_node) * get_dof_value(u, 3, node) - end do - - deps = eps - stress_old = ZERO - state_old = ZERO - if (gp .gt. 0) then - deps = eps - strain(:,gp) - stress_old = stress(:,gp) - state_old = statev(:,gp) - end if - call solid_current_spin(elem, 8, dndx, spin_current) - spin_inc = spin_current - state_old(14:16) - - call material_update_small_strain_dt(elem_mat(elem), dt, deps, & - state_old, stress_old, sig, & - state_new, ierr, spin_inc) - if (ierr .ne. FEM_SUCCESS) return - state_new(14:16) = spin_current - - if (gp .gt. 0) then - strain(:,gp) = eps - stress(:,gp) = sig - statev(:,gp) = state_new - if (state_new(4) .ge. HALF) failed_count = failed_count + 1 - end if - - do local_node = 1, 8 - node = elem_conn(local_node, elem) - - dof = dof_id(1,node) - if (dof .gt. 0) then - local_force(1,local_node) = local_force(1,local_node) + weight * detj * & - (dndx(1,local_node) * sig(1) + dndx(2,local_node) * sig(4) + & - dndx(3,local_node) * sig(5)) - end if - - dof = dof_id(2,node) - if (dof .gt. 0) then - local_force(2,local_node) = local_force(2,local_node) + weight * detj * & - (dndx(1,local_node) * sig(4) + dndx(2,local_node) * sig(2) + & - dndx(3,local_node) * sig(6)) - end if - - dof = dof_id(3,node) - if (dof .gt. 0) then - local_force(3,local_node) = local_force(3,local_node) + weight * detj * & - (dndx(1,local_node) * sig(5) + dndx(2,local_node) * sig(6) + & - dndx(3,local_node) * sig(3)) - end if - end do - - gp = gp + 1 - end do + eps = ZERO + do local_node = 1, 8 + node = elem_conn(local_node, elem) + eps(1) = eps(1) + dndx(1,local_node) * get_dof_value(u, 1, node) + eps(2) = eps(2) + dndx(2,local_node) * get_dof_value(u, 2, node) + eps(3) = eps(3) + dndx(3,local_node) * get_dof_value(u, 3, node) + eps(4) = eps(4) + dndx(2,local_node) * get_dof_value(u, 1, node) + & + dndx(1,local_node) * get_dof_value(u, 2, node) + eps(5) = eps(5) + dndx(3,local_node) * get_dof_value(u, 1, node) + & + dndx(1,local_node) * get_dof_value(u, 3, node) + eps(6) = eps(6) + dndx(3,local_node) * get_dof_value(u, 2, node) + & + dndx(2,local_node) * get_dof_value(u, 3, node) end do + + deps = eps + stress_old = ZERO + state_old = ZERO + if (gp .gt. 0) then + deps = eps - strain(:,gp) + stress_old = stress(:,gp) + state_old = statev(:,gp) + end if + call solid_current_spin(elem, 8, dndx, spin_current) + spin_inc = spin_current - state_old(14:16) + + call material_update_small_strain_dt(elem_mat(elem), dt, deps, & + state_old, stress_old, sig, & + state_new, ierr, spin_inc) + if (ierr .ne. FEM_SUCCESS) return + state_new(14:16) = spin_current + + if (gp .gt. 0) then + strain(:,gp) = eps + stress(:,gp) = sig + statev(:,gp) = state_new + if (state_new(4) .ge. HALF) failed_count = failed_count + 1 + end if + + do local_node = 1, 8 + node = elem_conn(local_node, elem) + + dof = dof_id(1,node) + if (dof .gt. 0) then + local_force(1,local_node) = local_force(1,local_node) + weight * detj * & + (dndx(1,local_node) * sig(1) + dndx(2,local_node) * sig(4) + & + dndx(3,local_node) * sig(5)) + end if + + dof = dof_id(2,node) + if (dof .gt. 0) then + local_force(2,local_node) = local_force(2,local_node) + weight * detj * & + (dndx(1,local_node) * sig(4) + dndx(2,local_node) * sig(2) + & + dndx(3,local_node) * sig(6)) + end if + + dof = dof_id(3,node) + if (dof .gt. 0) then + local_force(3,local_node) = local_force(3,local_node) + weight * detj * & + (dndx(1,local_node) * sig(5) + dndx(2,local_node) * sig(6) + & + dndx(3,local_node) * sig(3)) + end if + end do + + gp = gp + 1 end do if (failed_count .ge. 4) then @@ -341,7 +334,8 @@ contains real(rk) :: xloc(2,4) real(rk) :: qloc(5,4) real(rk) :: floc(5,4) - real(rk) :: gp_loc(2) + real(rk) :: gp_nat(NDIM,4) + real(rk) :: gp_weight(4) real(rk) :: nshape(4) real(rk) :: dndx(4) real(rk) :: dndy(4) @@ -362,8 +356,8 @@ contains real(rk) :: weight real(rk) :: r real(rk) :: s - integer :: i - integer :: j + integer :: q + integer :: npoint integer :: gp integer :: gp_start integer :: local_node @@ -412,66 +406,64 @@ contains (12.0_rk * (ONE - poisson * poisson)) coeff_shr = (5.0_rk / 6.0_rk) * shear_mod * thickness - gp_loc(1) = -ONE / sqrt(3.0_rk) - gp_loc(2) = ONE / sqrt(3.0_rk) + call quadrature_get_rule('S4', 4, npoint, gp_nat, gp_weight, ierr) + if (ierr .ne. FEM_SUCCESS) return gp_start = elem_gp_start(elem) gp = gp_start floc = ZERO - do i = 1, 2 - r = gp_loc(i) - do j = 1, 2 - s = gp_loc(j) - call shell4_shape_xy(xloc, r, s, nshape, dndx, dndy, detj, ierr) - if (ierr .ne. FEM_SUCCESS) return - weight = detj + do q = 1, npoint + r = gp_nat(1,q) + s = gp_nat(2,q) + call shell4_shape_xy(xloc, r, s, nshape, dndx, dndy, detj, ierr) + if (ierr .ne. FEM_SUCCESS) return + weight = gp_weight(q) * detj - call shell4_strains(nshape, dndx, dndy, qloc, strain_mem, & - strain_ben, strain_shr) - call shell4_resultants(coeff_mem, coeff_ben, coeff_shr, poisson, & - strain_mem, strain_ben, strain_shr, & - force_mem, force_ben, force_shr) + call shell4_strains(nshape, dndx, dndy, qloc, strain_mem, & + strain_ben, strain_shr) + call shell4_resultants(coeff_mem, coeff_ben, coeff_shr, poisson, & + strain_mem, strain_ben, strain_shr, & + force_mem, force_ben, force_shr) - do local_node = 1, 4 - floc(1,local_node) = floc(1,local_node) + weight * & - (dndx(local_node) * force_mem(1) + & - dndy(local_node) * force_mem(3)) - floc(2,local_node) = floc(2,local_node) + weight * & - (dndy(local_node) * force_mem(2) + & - dndx(local_node) * force_mem(3)) - floc(3,local_node) = floc(3,local_node) + weight * & - (dndx(local_node) * force_shr(1) + & - dndy(local_node) * force_shr(2)) - floc(4,local_node) = floc(4,local_node) + weight * & - (-dndy(local_node) * force_ben(2) - & - dndx(local_node) * force_ben(3) - & - nshape(local_node) * force_shr(2)) - floc(5,local_node) = floc(5,local_node) + weight * & - (dndx(local_node) * force_ben(1) + & - dndy(local_node) * force_ben(3) + & - nshape(local_node) * force_shr(1)) - end do - - if (gp .gt. 0) then - strain(:,gp) = ZERO - stress(:,gp) = ZERO - statev(:,gp) = ZERO - strain(1,gp) = strain_mem(1) - strain(2,gp) = strain_mem(2) - strain(4,gp) = strain_mem(3) - strain(5,gp) = strain_shr(1) - strain(6,gp) = strain_shr(2) - stress(1,gp) = force_mem(1) / thickness - stress(2,gp) = force_mem(2) / thickness - stress(4,gp) = force_mem(3) / thickness - stress(5,gp) = force_shr(1) / thickness - stress(6,gp) = force_shr(2) / thickness - statev(10,gp) = strain_ben(1) - statev(11,gp) = strain_ben(2) - statev(12,gp) = strain_ben(3) - end if - gp = gp + 1 + do local_node = 1, 4 + floc(1,local_node) = floc(1,local_node) + weight * & + (dndx(local_node) * force_mem(1) + & + dndy(local_node) * force_mem(3)) + floc(2,local_node) = floc(2,local_node) + weight * & + (dndy(local_node) * force_mem(2) + & + dndx(local_node) * force_mem(3)) + floc(3,local_node) = floc(3,local_node) + weight * & + (dndx(local_node) * force_shr(1) + & + dndy(local_node) * force_shr(2)) + floc(4,local_node) = floc(4,local_node) + weight * & + (-dndy(local_node) * force_ben(2) - & + dndx(local_node) * force_ben(3) - & + nshape(local_node) * force_shr(2)) + floc(5,local_node) = floc(5,local_node) + weight * & + (dndx(local_node) * force_ben(1) + & + dndy(local_node) * force_ben(3) + & + nshape(local_node) * force_shr(1)) end do + + if (gp .gt. 0) then + strain(:,gp) = ZERO + stress(:,gp) = ZERO + statev(:,gp) = ZERO + strain(1,gp) = strain_mem(1) + strain(2,gp) = strain_mem(2) + strain(4,gp) = strain_mem(3) + strain(5,gp) = strain_shr(1) + strain(6,gp) = strain_shr(2) + stress(1,gp) = force_mem(1) / thickness + stress(2,gp) = force_mem(2) / thickness + stress(4,gp) = force_mem(3) / thickness + stress(5,gp) = force_shr(1) / thickness + stress(6,gp) = force_shr(2) / thickness + statev(10,gp) = strain_ben(1) + statev(11,gp) = strain_ben(2) + statev(12,gp) = strain_ben(3) + end if + gp = gp + 1 end do call shell4_add_global(elem, basis, floc) @@ -837,52 +829,18 @@ contains ierr = FEM_SUCCESS end subroutine c3d4_shape_gradients - subroutine hex8_natural_coords(xi, eta, zeta) - real(rk), intent(out) :: xi(8) - real(rk), intent(out) :: eta(8) - real(rk), intent(out) :: zeta(8) - - xi(1) = -ONE - xi(2) = ONE - xi(3) = ONE - xi(4) = -ONE - xi(5) = -ONE - xi(6) = ONE - xi(7) = ONE - xi(8) = -ONE - - eta(1) = -ONE - eta(2) = -ONE - eta(3) = ONE - eta(4) = ONE - eta(5) = -ONE - eta(6) = -ONE - eta(7) = ONE - eta(8) = ONE - - zeta(1) = -ONE - zeta(2) = -ONE - zeta(3) = -ONE - zeta(4) = -ONE - zeta(5) = ONE - zeta(6) = ONE - zeta(7) = ONE - zeta(8) = ONE - end subroutine hex8_natural_coords - - subroutine hex8_shape_gradients(elem, r, s, t, xi_node, eta_node, & - zeta_node, dndr, dndx, detj, ierr) + subroutine hex8_shape_gradients(elem, r, s, t, dndr, dndx, detj, ierr) integer, intent(in) :: elem real(rk), intent(in) :: r real(rk), intent(in) :: s real(rk), intent(in) :: t - real(rk), intent(in) :: xi_node(8) - real(rk), intent(in) :: eta_node(8) - real(rk), intent(in) :: zeta_node(8) real(rk), intent(out) :: dndr(NDIM,8) real(rk), intent(out) :: dndx(NDIM,8) real(rk), intent(out) :: detj integer, intent(out) :: ierr + real(rk) :: natural(NDIM) + real(rk) :: nshape(MAX_NEN) + real(rk) :: dndr_all(NDIM,MAX_NEN) real(rk) :: jac(3,3) real(rk) :: invj(3,3) integer :: local_node @@ -890,14 +848,10 @@ contains integer :: i integer :: j - do local_node = 1, 8 - dndr(1,local_node) = 0.125_rk * xi_node(local_node) * & - (ONE + s * eta_node(local_node)) * (ONE + t * zeta_node(local_node)) - dndr(2,local_node) = 0.125_rk * eta_node(local_node) * & - (ONE + r * xi_node(local_node)) * (ONE + t * zeta_node(local_node)) - dndr(3,local_node) = 0.125_rk * zeta_node(local_node) * & - (ONE + r * xi_node(local_node)) * (ONE + s * eta_node(local_node)) - end do + natural = (/ r, s, t /) + call shape_evaluate('C3D8', natural, nshape, dndr_all, ierr) + if (ierr .ne. FEM_SUCCESS) return + dndr = dndr_all(:,1:8) jac = ZERO do local_node = 1, 8 diff --git a/src/element/fem_element_mass.f90 b/src/element/fem_element_mass.f90 index 7309979..84726eb 100644 --- a/src/element/fem_element_mass.f90 +++ b/src/element/fem_element_mass.f90 @@ -2,6 +2,9 @@ module fem_element_mass use fem_precision use fem_error use fem_database + use fem_element_topology + use fem_quadrature + use fem_shape_functions implicit none contains @@ -53,11 +56,10 @@ contains select case (trim(etype)) case ('C3D4') - if (nlocal .ne. 4) then - call error_set(FEM_ERR_MESH, 'C3D4 element must have 4 nodes', ierr) - return - end if - volume = tetra4_volume(elem) + call element_validate_node_count(etype, nlocal, ierr) + if (ierr .ne. FEM_SUCCESS) return + call solid_volume_by_quadrature(elem, volume, ierr) + if (ierr .ne. FEM_SUCCESS) return if (volume .le. ZERO) then call error_set(FEM_ERR_MESH, 'C3D4 element has non-positive volume', ierr) return @@ -67,21 +69,17 @@ contains call add_translational_mass(elem, nodal_mass) case ('C3D8') - if (nlocal .ne. 8) then - call error_set(FEM_ERR_MESH, 'C3D8 element must have 8 nodes', ierr) - return - end if - call hex8_volume(elem, volume, ierr) + call element_validate_node_count(etype, nlocal, ierr) + if (ierr .ne. FEM_SUCCESS) return + call solid_volume_by_quadrature(elem, volume, ierr) if (ierr .ne. FEM_SUCCESS) return elem_mass = density * volume nodal_mass = elem_mass / real(nlocal, rk) call add_translational_mass(elem, nodal_mass) case ('S4') - if (nlocal .ne. 4) then - call error_set(FEM_ERR_MESH, 'S4 element must have 4 nodes', ierr) - return - end if + call element_validate_node_count(etype, nlocal, ierr) + if (ierr .ne. FEM_SUCCESS) return thickness = section_thickness(sec) if (thickness .le. ZERO) then call error_set(FEM_ERR_INPUT, 'S4 section thickness must be positive', ierr) @@ -142,118 +140,62 @@ contains end do end subroutine add_rotational_mass - real(rk) function tetra4_volume(elem) - integer, intent(in) :: elem - real(rk) :: a(NDIM) - real(rk) :: b(NDIM) - real(rk) :: c(NDIM) - real(rk) :: d(NDIM) - - a = coord0(:, elem_conn(1, elem)) - b = coord0(:, elem_conn(2, elem)) - c = coord0(:, elem_conn(3, elem)) - d = coord0(:, elem_conn(4, elem)) - tetra4_volume = abs(det3_columns(b - a, c - a, d - a)) / 6.0_rk - end function tetra4_volume - - subroutine hex8_volume(elem, volume, ierr) + subroutine solid_volume_by_quadrature(elem, volume, ierr) integer, intent(in) :: elem real(rk), intent(out) :: volume integer, intent(out) :: ierr - real(rk) :: xi(8) - real(rk) :: eta(8) - real(rk) :: zeta(8) - real(rk) :: gp(2) - real(rk) :: dndr(3,8) + integer, parameter :: MAX_QP = 32 + real(rk) :: xi(NDIM,MAX_QP) + real(rk) :: weight(MAX_QP) + real(rk) :: nshape(MAX_NEN) + real(rk) :: dndr(NDIM,MAX_NEN) real(rk) :: jac(3,3) real(rk) :: detj - real(rk) :: r - real(rk) :: s - real(rk) :: t - integer :: i - integer :: j - integer :: k + character(len=ELEM_TYPE_LEN) :: etype + integer :: gp + integer :: npoint integer :: node integer :: local_node - xi(1) = -ONE - xi(2) = ONE - xi(3) = ONE - xi(4) = -ONE - xi(5) = -ONE - xi(6) = ONE - xi(7) = ONE - xi(8) = -ONE + etype = adjustl(elem_type(elem)) + call quadrature_get_rule(etype, MAX_QP, npoint, xi, weight, ierr) + if (ierr .ne. FEM_SUCCESS) return - eta(1) = -ONE - eta(2) = -ONE - eta(3) = ONE - eta(4) = ONE - eta(5) = -ONE - eta(6) = -ONE - eta(7) = ONE - eta(8) = ONE - - zeta(1) = -ONE - zeta(2) = -ONE - zeta(3) = -ONE - zeta(4) = -ONE - zeta(5) = ONE - zeta(6) = ONE - zeta(7) = ONE - zeta(8) = ONE - - gp(1) = -ONE / sqrt(3.0_rk) - gp(2) = ONE / sqrt(3.0_rk) volume = ZERO - do i = 1, 2 - r = gp(i) - do j = 1, 2 - s = gp(j) - do k = 1, 2 - t = gp(k) + do gp = 1, npoint + call shape_evaluate(etype, xi(:,gp), nshape, dndr, ierr) + if (ierr .ne. FEM_SUCCESS) return - do local_node = 1, 8 - dndr(1,local_node) = 0.125_rk * xi(local_node) * & - (ONE + s * eta(local_node)) * (ONE + t * zeta(local_node)) - dndr(2,local_node) = 0.125_rk * eta(local_node) * & - (ONE + r * xi(local_node)) * (ONE + t * zeta(local_node)) - dndr(3,local_node) = 0.125_rk * zeta(local_node) * & - (ONE + r * xi(local_node)) * (ONE + s * eta(local_node)) - end do - - jac = ZERO - do local_node = 1, 8 - node = elem_conn(local_node, elem) - jac(1,1) = jac(1,1) + coord0(1,node) * dndr(1,local_node) - jac(1,2) = jac(1,2) + coord0(1,node) * dndr(2,local_node) - jac(1,3) = jac(1,3) + coord0(1,node) * dndr(3,local_node) - jac(2,1) = jac(2,1) + coord0(2,node) * dndr(1,local_node) - jac(2,2) = jac(2,2) + coord0(2,node) * dndr(2,local_node) - jac(2,3) = jac(2,3) + coord0(2,node) * dndr(3,local_node) - jac(3,1) = jac(3,1) + coord0(3,node) * dndr(1,local_node) - jac(3,2) = jac(3,2) + coord0(3,node) * dndr(2,local_node) - jac(3,3) = jac(3,3) + coord0(3,node) * dndr(3,local_node) - end do - - detj = det3_matrix(jac) - if (detj .le. ZERO) then - call error_set(FEM_ERR_MESH, 'C3D8 element has non-positive Jacobian', ierr) - return - end if - volume = volume + detj - end do + jac = ZERO + do local_node = 1, elem_nnode(elem) + node = elem_conn(local_node, elem) + jac(1,1) = jac(1,1) + coord0(1,node) * dndr(1,local_node) + jac(1,2) = jac(1,2) + coord0(1,node) * dndr(2,local_node) + jac(1,3) = jac(1,3) + coord0(1,node) * dndr(3,local_node) + jac(2,1) = jac(2,1) + coord0(2,node) * dndr(1,local_node) + jac(2,2) = jac(2,2) + coord0(2,node) * dndr(2,local_node) + jac(2,3) = jac(2,3) + coord0(2,node) * dndr(3,local_node) + jac(3,1) = jac(3,1) + coord0(3,node) * dndr(1,local_node) + jac(3,2) = jac(3,2) + coord0(3,node) * dndr(2,local_node) + jac(3,3) = jac(3,3) + coord0(3,node) * dndr(3,local_node) end do + + detj = det3_matrix(jac) + if (detj .le. ZERO) then + call error_set(FEM_ERR_MESH, 'solid element has non-positive Jacobian', ierr) + return + end if + volume = volume + detj * weight(gp) end do if (volume .le. ZERO) then - call error_set(FEM_ERR_MESH, 'C3D8 element has non-positive volume', ierr) + call error_set(FEM_ERR_MESH, 'solid element has non-positive volume', ierr) return end if ierr = FEM_SUCCESS - end subroutine hex8_volume + end subroutine solid_volume_by_quadrature real(rk) function shell4_area(elem) integer, intent(in) :: elem @@ -293,16 +235,6 @@ contains c(3) = a(1) * b(2) - a(2) * b(1) end subroutine cross3 - real(rk) function det3_columns(a, b, c) - real(rk), intent(in) :: a(NDIM) - real(rk), intent(in) :: b(NDIM) - real(rk), intent(in) :: c(NDIM) - - det3_columns = a(1) * (b(2) * c(3) - b(3) * c(2)) - & - b(1) * (a(2) * c(3) - a(3) * c(2)) + & - c(1) * (a(2) * b(3) - a(3) * b(2)) - end function det3_columns - real(rk) function det3_matrix(a) real(rk), intent(in) :: a(3,3) diff --git a/tests/module/test_facet_topology.f90 b/tests/module/test_facet_topology.f90 new file mode 100644 index 0000000..8c898a5 --- /dev/null +++ b/tests/module/test_facet_topology.f90 @@ -0,0 +1,179 @@ +program test_facet_topology + use fem_precision + use fem_error + use fem_database + use fem_coupling + implicit none + + real(rk), parameter :: TOL = 1.0e-10_rk + integer, parameter :: MAX_TEST_FACETS = 8 + + call check_c3d10() + call check_c3d20() + call check_s6() + call check_s8() + +contains + + subroutine check_c3d10() + integer :: ierr + + call database_allocate_mesh(10, 1, ierr) + if (ierr .ne. FEM_SUCCESS) call fail(1) + call fill_tet10() + call check_facets(4, 6, 1.5_rk + sqrt(3.0_rk) * HALF, 10) + end subroutine check_c3d10 + + subroutine check_c3d20() + integer :: ierr + + call database_allocate_mesh(20, 1, ierr) + if (ierr .ne. FEM_SUCCESS) call fail(20) + call fill_hex20() + call check_facets(6, 8, 6.0_rk, 30) + end subroutine check_c3d20 + + subroutine check_s6() + integer :: ierr + + call database_allocate_mesh(6, 1, ierr) + if (ierr .ne. FEM_SUCCESS) call fail(40) + call fill_tri6() + call check_facets(1, 6, HALF, 50) + end subroutine check_s6 + + subroutine check_s8() + integer :: ierr + + call database_allocate_mesh(8, 1, ierr) + if (ierr .ne. FEM_SUCCESS) call fail(60) + call fill_quad8() + call check_facets(1, 8, ONE, 70) + end subroutine check_s8 + + subroutine check_facets(expected_count, expected_max_nodes, expected_area, code) + integer, intent(in) :: expected_count + integer, intent(in) :: expected_max_nodes + real(rk), intent(in) :: expected_area + integer, intent(in) :: code + integer :: ierr + integer :: nfacet + integer :: max_nodes + integer :: facet_id(MAX_TEST_FACETS) + integer :: elem_id(MAX_TEST_FACETS) + integer :: face_id(MAX_TEST_FACETS) + integer :: facet_nnode(MAX_TEST_FACETS) + integer :: facet_conn(MAX_FACE_NODES,MAX_TEST_FACETS) + real(rk) :: x(NDIM,MAX_FACE_NODES,MAX_TEST_FACETS) + real(rk) :: vnod(NDIM,MAX_FACE_NODES,MAX_TEST_FACETS) + real(rk) :: normal(NDIM,MAX_TEST_FACETS) + real(rk) :: area(MAX_TEST_FACETS) + + call database_assign_dofs(ierr) + if (ierr .ne. FEM_SUCCESS) call fail(code) + call cpl_build_facets(ierr) + if (ierr .ne. FEM_SUCCESS) call fail(code + 1) + call cpl_get_facet_count(nfacet, max_nodes, ierr) + if (ierr .ne. FEM_SUCCESS) call fail(code + 2) + if (nfacet .ne. expected_count) call fail(code + 3) + if (max_nodes .ne. expected_max_nodes) call fail(code + 4) + call cpl_get_facets(MAX_TEST_FACETS, MAX_FACE_NODES, facet_id, elem_id, & + face_id, facet_nnode, facet_conn, x, vnod, normal, & + area, ierr) + if (ierr .ne. FEM_SUCCESS) call fail(code + 5) + if (abs(sum(area(1:nfacet)) - expected_area) .gt. TOL) call fail(code + 6) + + call cpl_finalize_facets() + call database_finalize(ierr) + if (ierr .ne. FEM_SUCCESS) call fail(code + 7) + end subroutine check_facets + + subroutine fill_tet10() + call set_common_element('C3D10', 10) + coord0(:,1) = (/ ZERO, ZERO, ZERO /) + coord0(:,2) = (/ ONE, ZERO, ZERO /) + coord0(:,3) = (/ ZERO, ONE, ZERO /) + coord0(:,4) = (/ ZERO, ZERO, ONE /) + coord0(:,5) = (/ HALF, ZERO, ZERO /) + coord0(:,6) = (/ HALF, HALF, ZERO /) + coord0(:,7) = (/ ZERO, HALF, ZERO /) + coord0(:,8) = (/ ZERO, ZERO, HALF /) + coord0(:,9) = (/ HALF, ZERO, HALF /) + coord0(:,10) = (/ ZERO, HALF, HALF /) + end subroutine fill_tet10 + + subroutine fill_hex20() + call set_common_element('C3D20', 20) + coord0(:,1) = (/ ZERO, ZERO, ZERO /) + coord0(:,2) = (/ ONE, ZERO, ZERO /) + coord0(:,3) = (/ ONE, ONE, ZERO /) + coord0(:,4) = (/ ZERO, ONE, ZERO /) + coord0(:,5) = (/ ZERO, ZERO, ONE /) + coord0(:,6) = (/ ONE, ZERO, ONE /) + coord0(:,7) = (/ ONE, ONE, ONE /) + coord0(:,8) = (/ ZERO, ONE, ONE /) + coord0(:,9) = (/ HALF, ZERO, ZERO /) + coord0(:,10) = (/ ONE, HALF, ZERO /) + coord0(:,11) = (/ HALF, ONE, ZERO /) + coord0(:,12) = (/ ZERO, HALF, ZERO /) + coord0(:,13) = (/ HALF, ZERO, ONE /) + coord0(:,14) = (/ ONE, HALF, ONE /) + coord0(:,15) = (/ HALF, ONE, ONE /) + coord0(:,16) = (/ ZERO, HALF, ONE /) + coord0(:,17) = (/ ZERO, ZERO, HALF /) + coord0(:,18) = (/ ONE, ZERO, HALF /) + coord0(:,19) = (/ ONE, ONE, HALF /) + coord0(:,20) = (/ ZERO, ONE, HALF /) + end subroutine fill_hex20 + + subroutine fill_tri6() + call set_common_element('S6', 6) + coord0(:,1) = (/ ZERO, ZERO, ZERO /) + coord0(:,2) = (/ ONE, ZERO, ZERO /) + coord0(:,3) = (/ ZERO, ONE, ZERO /) + coord0(:,4) = (/ HALF, ZERO, ZERO /) + coord0(:,5) = (/ HALF, HALF, ZERO /) + coord0(:,6) = (/ ZERO, HALF, ZERO /) + end subroutine fill_tri6 + + subroutine fill_quad8() + call set_common_element('S8', 8) + coord0(:,1) = (/ ZERO, ZERO, ZERO /) + coord0(:,2) = (/ ONE, ZERO, ZERO /) + coord0(:,3) = (/ ONE, ONE, ZERO /) + coord0(:,4) = (/ ZERO, ONE, ZERO /) + coord0(:,5) = (/ HALF, ZERO, ZERO /) + coord0(:,6) = (/ ONE, HALF, ZERO /) + coord0(:,7) = (/ HALF, ONE, ZERO /) + coord0(:,8) = (/ ZERO, HALF, ZERO /) + end subroutine fill_quad8 + + subroutine set_common_element(type_name, nlocal) + character(len=*), intent(in) :: type_name + integer, intent(in) :: nlocal + integer :: i + + coord = ZERO + node_id_ext = 0 + do i = 1, nnode + node_id_ext(i) = i + end do + + elem_id_ext(1) = 1 + elem_type(1) = type_name + elem_nnode(1) = nlocal + elem_conn(:,1) = 0 + do i = 1, nlocal + elem_conn(i,1) = i + end do + elem_status(1) = 0 + end subroutine set_common_element + + subroutine fail(code) + integer, intent(in) :: code + + write(*,*) 'test_facet_topology failed:', code + stop 1 + end subroutine fail + +end program test_facet_topology