Find shared atoms between two fragments (for GMBE with overlapping fragments)
This function identifies atoms that appear in both fragments, which is essential for computing intersection-corrected energies in GMBE.
Algorithm: O(n1 * n2) brute-force comparison - Loop through all atoms in fragment 1 - For each atom, check if it appears in fragment 2 - Collect all shared atoms
Returns: .true. if fragments share at least one atom, .false. otherwise
Output: intersection - allocatable array containing shared atom indices n_intersect - number of shared atoms
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer, | intent(in) | :: | frag1_atoms(:) |
Atom indices in fragment 1 (0-indexed) |
||
| integer, | intent(in) | :: | n1 |
Number of atoms in fragment 1 |
||
| integer, | intent(in) | :: | frag2_atoms(:) |
Atom indices in fragment 2 (0-indexed) |
||
| integer, | intent(in) | :: | n2 |
Number of atoms in fragment 2 |
||
| integer, | intent(out), | allocatable | :: | intersection(:) |
Shared atom indices |
|
| integer, | intent(out) | :: | n_intersect |
Number of shared atoms |
| Type | Visibility | Attributes | Name | Initial | |||
|---|---|---|---|---|---|---|---|
| integer, | private | :: | i | ||||
| integer, | private | :: | j | ||||
| integer, | private | :: | temp_count | ||||
| integer, | private, | allocatable | :: | temp_intersection(:) |
function find_fragment_intersection(frag1_atoms, n1, frag2_atoms, n2, & intersection, n_intersect) result(has_intersection) !! Find shared atoms between two fragments (for GMBE with overlapping fragments) !! !! This function identifies atoms that appear in both fragments, which is essential !! for computing intersection-corrected energies in GMBE. !! !! Algorithm: O(n1 * n2) brute-force comparison !! - Loop through all atoms in fragment 1 !! - For each atom, check if it appears in fragment 2 !! - Collect all shared atoms !! !! Returns: !! .true. if fragments share at least one atom, .false. otherwise !! !! Output: !! intersection - allocatable array containing shared atom indices !! n_intersect - number of shared atoms integer, intent(in) :: frag1_atoms(:) !! Atom indices in fragment 1 (0-indexed) integer, intent(in) :: n1 !! Number of atoms in fragment 1 integer, intent(in) :: frag2_atoms(:) !! Atom indices in fragment 2 (0-indexed) integer, intent(in) :: n2 !! Number of atoms in fragment 2 integer, allocatable, intent(out) :: intersection(:) !! Shared atom indices integer, intent(out) :: n_intersect !! Number of shared atoms logical :: has_intersection integer :: i, j integer, allocatable :: temp_intersection(:) integer :: temp_count ! Allocate temporary array (max possible size is min(n1, n2)) allocate (temp_intersection(min(n1, n2))) temp_count = 0 ! Find all shared atoms do i = 1, n1 do j = 1, n2 if (frag1_atoms(i) == frag2_atoms(j)) then ! Found a shared atom temp_count = temp_count + 1 temp_intersection(temp_count) = frag1_atoms(i) exit ! Move to next atom in frag1 end if end do end do ! Set output n_intersect = temp_count has_intersection = (temp_count > 0) ! Allocate and copy result if intersection exists if (has_intersection) then allocate (intersection(n_intersect)) intersection = temp_intersection(1:n_intersect) end if deallocate (temp_intersection) end function find_fragment_intersection