cavemanloverboy / FNNTW

15 stars 0 forks source link

[Feature Request] Can FNNTW support the periodic boundary for triclinic box? #7

Open mushroomfire opened 11 months ago

mushroomfire commented 11 months ago

Hi, FNNTW now supports periodic boundary conditions for a rectangular box, where the angles between the two line vectors are 90 degrees. Is it possible to make it more generalizable for a triclinic box? This feature proves useful in certain simulation domains. Let's consider the box below, where each row represents a box vector:

box = np.array([[a1, a2, a3], [b1, b2, b3], [c1, c2, c3]])

The rectangular box is like below:

box = np.array([[a, 0, 0], [0, b, 0], [0, 0, c]])

A distance vector rij = np.array([x, y, z]) can be wrapped as below:

def pbc(rij, box):
      nz = rij[2] / box[2][2]
      ny = (rij[1] - nz * box[2][1]) / box[1][1]
      nx = (rij[0] - ny * box[1][0] - nz * box[2][0]) / box[0][0]
      n =[nx, ny, nz]
      for i in range(3):
            if n[i] > 0.5:
                n[i] -= 1
            elif n[i] < -0.5:
                n[i] += 1
      return n[0] * box[0] + n[1] * box[1] + n[2] * box[2]

Maybe change the distance equation in codes can achieve this? Thank you very much!

cavemanloverboy commented 7 months ago

just noticed this issue, sorry. i have a new library now bosque that is faster.

triclinic is interesting. best i can come up with off the top of my head is coordinate transformation + weighted squared euclidean

cavemanloverboy commented 7 months ago

or, since it's periodic, you should be able to just displace things until it's a box. here's a 2D example. it can be done in 3d as well

image

mushroomfire commented 7 months ago

Thanks for your reply. I am unsure if this approach can maintain the calculation speed. In molecular dynamics (MD) simulation, there is a code that is capable of handling both triclinic and rectangular boxes with periodic or free boundaries, and it also offers high computational efficiency. I have provided a reference to this code for your convenience: https://gitlab.com/stuko/ovito/-/blob/master/src/ovito/particles/util/NearestNeighborFinder.cpp. Later on, I will also attempt to use Bosque.

cavemanloverboy commented 7 months ago

Yes, definitely not ideal. Best of luck. If I can be of any help, make an issue on bosque and we can discuss there