georust / geo

Rust geospatial primitives & algorithms
https://crates.io/crates/geo
Other
1.58k stars 200 forks source link

Add periodic boundary conditions #434

Open seatonullberg opened 4 years ago

seatonullberg commented 4 years ago

Periodic boundary conditions could be a valuable extension of the existing geo::algorithm::euclidean_distance functionality. I am happy to help if this is within the scope of the crate.

urschrei commented 4 years ago

Hi @seatonullberg – thanks for the offer! Could you say a little more the applicability of PBCs in the context of the geo crate, and distance calculations? We're always interested in new functionality that's rooted in a use case, I'm just trying to understand a little more about what that is, and the Wiki article is heavy on detail and somewhat lacking in context…

seatonullberg commented 4 years ago

Description

In my field (materials science) PBCs are often used as a "computational trick" to model a bulk/infinite system using only a small number of particles. This is critical to the usability of many simulation codes where the computational cost increases exponentially with the number of particles. To visualize a simple example, imagine a particle in a square simulation cell traveling towards the top of the cell. Rather than being reflected off of the wall upon impact, the particle "passes through" and re-enters the bottom of the cell (just like classic Pacman). This technique preserves the energetic interactions with neighbors without imposing barriers that could alter the equilibrium of the system.

Applications

PBC's are most often used in Molecular Dynamics simulations, but could be applied to other multi-particle simulations where complexity scales poorly with the system size.

Implementation

I envisioned something as simple as a single function which takes a MultiPoint argument to represent the positions of particles, another MultiPoint argument to represent the trajectory vector of each particle, and lastly a Polygon argument to represent the bounding box. Any points outside of the boundary would be reflected back into the bounding box by tracing their trajectory vector through the polygon and into the opposite side. The function could either mutate the positions or return a new MultiPoint object to represent the positions after the periodicity has been imposed. I think this would only make sense for convex polygons with no inclusions.

@urschrei Thanks for the interest in my suggestion. Again, I understand if this feature is too domain specific. I could implement it in my own code but I think others may find it beneficial so I thought I should offer the idea here.

urschrei commented 4 years ago

@seatonullberg Thanks for the explanation! I agree that it's niche, but the implementation sounds reasonable to me. Feel free to open a draft PR if you'd like to, and we can go from there – I'm happy to provide feedback if you need it.