Commit c41581a3 authored by Gao, Xiang's avatar Gao, Xiang Committed by Farhad Ramezanghorbani
Browse files

Use type inference of arange to clean up code (#357)

* Use type inference of arange to clean up code

* commit
parent 1a2b4504
...@@ -98,9 +98,9 @@ def compute_shifts(cell: Tensor, pbc: Tensor, cutoff: float) -> Tensor: ...@@ -98,9 +98,9 @@ def compute_shifts(cell: Tensor, pbc: Tensor, cutoff: float) -> Tensor:
inv_distances = reciprocal_cell.norm(2, -1) inv_distances = reciprocal_cell.norm(2, -1)
num_repeats = torch.ceil(cutoff * inv_distances).to(torch.long) num_repeats = torch.ceil(cutoff * inv_distances).to(torch.long)
num_repeats = torch.where(pbc, num_repeats, torch.zeros_like(num_repeats)) num_repeats = torch.where(pbc, num_repeats, torch.zeros_like(num_repeats))
r1 = torch.arange(1, num_repeats[0] + 1, device=cell.device, dtype=torch.long) r1 = torch.arange(1, num_repeats[0] + 1, device=cell.device)
r2 = torch.arange(1, num_repeats[1] + 1, device=cell.device, dtype=torch.long) r2 = torch.arange(1, num_repeats[1] + 1, device=cell.device)
r3 = torch.arange(1, num_repeats[2] + 1, device=cell.device, dtype=torch.long) r3 = torch.arange(1, num_repeats[2] + 1, device=cell.device)
o = torch.zeros(1, dtype=torch.long, device=cell.device) o = torch.zeros(1, dtype=torch.long, device=cell.device)
return torch.cat([ return torch.cat([
torch.cartesian_prod(r1, r2, r3), torch.cartesian_prod(r1, r2, r3),
...@@ -136,7 +136,7 @@ def neighbor_pairs(padding_mask: Tensor, coordinates: Tensor, cell: Tensor, ...@@ -136,7 +136,7 @@ def neighbor_pairs(padding_mask: Tensor, coordinates: Tensor, cell: Tensor,
coordinates = coordinates.detach() coordinates = coordinates.detach()
cell = cell.detach() cell = cell.detach()
num_atoms = padding_mask.shape[1] num_atoms = padding_mask.shape[1]
all_atoms = torch.arange(num_atoms, device=cell.device, dtype=torch.long) all_atoms = torch.arange(num_atoms, device=cell.device)
# Step 2: center cell # Step 2: center cell
p1_center, p2_center = torch.combinations(all_atoms).unbind(-1) p1_center, p2_center = torch.combinations(all_atoms).unbind(-1)
...@@ -145,7 +145,7 @@ def neighbor_pairs(padding_mask: Tensor, coordinates: Tensor, cell: Tensor, ...@@ -145,7 +145,7 @@ def neighbor_pairs(padding_mask: Tensor, coordinates: Tensor, cell: Tensor,
# Step 3: cells with shifts # Step 3: cells with shifts
# shape convention (shift index, molecule index, atom index, 3) # shape convention (shift index, molecule index, atom index, 3)
num_shifts = shifts.shape[0] num_shifts = shifts.shape[0]
all_shifts = torch.arange(num_shifts, device=cell.device, dtype=torch.long) all_shifts = torch.arange(num_shifts, device=cell.device)
shift_index, p1, p2 = torch.cartesian_prod(all_shifts, all_atoms, all_atoms).unbind(-1) shift_index, p1, p2 = torch.cartesian_prod(all_shifts, all_atoms, all_atoms).unbind(-1)
shifts_outide = shifts.index_select(0, shift_index) shifts_outide = shifts.index_select(0, shift_index)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment