-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP]Tutorial for pair potential implementation #196
Conversation
doc/src/advanced/potential.md
Outdated
|
||
In this tutorial, we will implement a potential to describe non-bonded pair interactions, namely the [**Mie potential**](http://www.sklogwiki.org/SklogWiki/index.php/Mie_potential). We will have to implement both the `Potential` as well as the `PairPotential` traits. If we wanted to implement a function that can be used as non-bonded as well as bond-length potential, we'd have to implement `Potential`, `PairPotential` as well as `BondPotential`. "Implementing a trait" means that we will define a [structure](https://doc.rust-lang.org/book/second-edition/ch05-00-structs.html) (a `struct`) for which we will add functions to satisfy the traits' requirements. | ||
|
||
Let's start by having a look at the API documentation for `Potential`. Open the [API documentation](http://lumol.org/lumol/latest/lumol/index.html) and navigate to the `energy` module which is listed under the `modules` section at the bottom of the page. Traits are listed at the very bottom of the module documentation. Open the documentation for the `Potential` trait. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll give a direct link to the trait here, instead of making the people navigate
doc/src/advanced/potential.md
Outdated
- $\epsilon$ the energetic paramater, | ||
- $\sigma$ the particle diameter or structural parameter. | ||
|
||
We start by defining the `struct` for our potential. We will add our code to the `functions.rs` file located in `lumol/src/core/src/energy`. Add the following lines: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need to add new potentials in the same module as the other ones. Actually, we don't even need to add it in the same crate. See my suggestion below!
doc/src/advanced/potential.md
Outdated
|
||
```rust | ||
impl Mie { | ||
fn new(sigma: f64, epsilon: f64, n: f64, m: f64) -> Mie { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the Mie::new
function is enforcing some constraint, maybe the fields should not be pub
lic
doc/src/advanced/potential.md
Outdated
|
||
fn tail_virial(&self, cutoff: f64) -> f64 { | ||
if self.m <= 3.0 { | ||
panic!("No tail correction possible for Mie potential with an exponent lower than 3.0") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In other long ranged potentials, we return from tail_energy/tail_virial instead of panicking.
This looks very nice! I have a suggestion about the organisation of this tutorial. Lumol is written such as it can be extended from the outside; which means the users do not need to write code in this repository to have it work with the rest of the engine. The tutorial could be written to use this property: just create a new cargo project, make it depend on the lumol crate, and implement the potential here. This can have two benefits:
Your tutorial outline could then be splitted in two parts: the basics with
and then the extra, only needed to include code in the main repository
The only issue is until we get a plugin system (#45), users needs to write there own |
Thanks for the feedback!
That's a good idea, I reorganize the tutorial accordingly. Would you still have this as part of the manual (I would, see the 2nd part about docs and input) or would we treat tutorials of this kind differently? Where would you put the tutorial repo (create a I'll keep on pushing to this PR - it already includes all codes necessary to put them into a separate repo so this will be a quick change in the end. |
Concerning organisation, I think we can have everything in this repo for now, so the text in the manual, and the code in a tutorials directory. If it gets annoying, we can still move it to a separated repo later. |
1ce4527
to
86e9752
Compare
Added first part of the tutorial. I also added a I somehow screwed up the git history (sorry). Not sure how long it will take me to write the second part. Maybe it makes sense to merge this as is. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have a few comment, take them or leave them as you wish!
In this tutorial we will teach you how to use your own potential function with Lumol. | ||
This tutorial consists of two parts. | ||
In the first part, we will implement the logic in a separate project using Lumol as an external library. | ||
The second part describes the neccessary steps to implement the logic into Lumol `core`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
into Lumol core
=> into Lumol core package called lumol-core
|
||
A potential is a function that describes the energy and force between interaction sites. | ||
In Lumol we differentiate between two types of potentials: First, there are potential functions that need the global state of the system, *i.e.* all positions, as input. The Ewald summation which is used to compute electrostatic interactions is an example for this kind of potential. | ||
In Lumol we call them `GlobalPotential`s. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In Lumol we call them ... => We call them ...
More specific, we will implement a potential to compute van-der-Waals interactions between pairs of particles. | ||
We will make liberal use of the API documentation of both the Rust standard library as well as the Lumol API. | ||
Please note that we will point to other references, such as the Rust book, concerning general Rust concepts to keep this tutorial brief. | ||
If you have questions concerning Rust or Lumol, please don't hesitate to file an issue on github or join the discussion on our gitter. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe add the links to gitter and github here
tutorials/.gitignore
Outdated
@@ -0,0 +1,3 @@ | |||
/target/ | |||
**/*.rs.bk |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this the standard gitignore created by cargo new?
tutorials/mie_potential/Cargo.toml
Outdated
@@ -0,0 +1,7 @@ | |||
[package] | |||
name = "mie_potential" | |||
version = "0.1.0" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would use version = "0.0.0" here, and add a publish = false
to ensure this is never published to crates.io
We start by creating a new package using `cargo`: | ||
|
||
``` | ||
cargo new mie_potential |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as below, I would call the crate lumol-tutorial-potential
|
||
We start by defining the `struct` for our potential. Add the following lines to `lib.rs`: | ||
|
||
```rust |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think it would be possible to directly include the relevant part of the file in /tutorials
? This would make it easier to prevent the code from bit rotting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm note sure what you mean here. How would you directly include that part of the file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I checked and this is not possible with markdown. So let's keep it this way for now.
I do this with sphinx: you have example files somewhere, and then you include them in your documentation. (see here for example). This allow to ensure that the documentation and the actual files stays in sync.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I see, yes i do that in sphinx too.
/// Exponent of attractive contribution | ||
m: f64, | ||
/// Energetic prefactor computed from the exponents and epsilon | ||
prefac: f64, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not prefactor
?
} | ||
``` | ||
|
||
First, we can see that `PairPotential` enforces the implementation of `Potential` which is denoted by `pub trait PairPotential: Potential ...` (we ignore `BoxClonePair` for now). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we can add a comment saying that BoxClonePair
is automatically implemented for us if we implement PairPotential
manually?
In fact, the potential has to be a *short ranged* potential. | ||
For our Mie potential, both the exponents have to be larger than 3.0 else our potential will be *long ranged* and | ||
the integral that has to be solved to compute the tail corrections diverges. | ||
For now, we will simply return zero in this case. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The "for now" looks strange here: returning 0 is the right thing to do if the potential is not short-ranged
No, this does not look like so =)
Yeah, we can merge this as it is, and keep the advanced part of the tutorial for later. Can you also add a test in .travis.yml that check that the code compiles (you don't need to run the simulation, just build everything with |
Renamed project and make use of lumol_input Suggested changes and exclude tutorial from workspace Add script to build tutorials Added suggestions from reviewer
.travis.yml
Outdated
@@ -29,8 +29,12 @@ script: | |||
# Run unit tests and doc tests in debug mode | |||
- cargo test -p lumol-core | |||
- cargo test -p lumol-input | |||
# Check if tutorials compile | |||
- cd |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like a previous attempt leftover, but this might break the build, as the code is not in $HOME.
.travis.yml
Outdated
# Check that benchmarks still compile | ||
- cargo bench --no-run | ||
# Check if tutorials compile | ||
- ./scripts/ci/compile-tutorials.py |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would rather explicitly do cd tutorial/lumol_potential_tutorial && cargo build
here. We can craft a script for this later!
You might need to do a cd $TRAVIS_BUILD_DIR
after that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, that was my first attempt :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is another way to do this, in one line: cargo build --manifest-path=tutorial/lumol_potential_tutorial/Cargo.toml
.
But if you prefer the python script, you'll need to chmod +x
it. There is a permission denied error on Travis.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good to me. If you are done with it, I'll merge =)
I'm done with this part. |
And thank you again for doing this: having a good documentation is essential, especially for open source software! |
EDIT: New structure
This PR will teach users how to add a
PairPotential
to Lumol.These are the steps discussed in the tutorial:
Part 1:
Potential
PairPotential
Part 2 (moving the implementation into lumol core):
You can clone this PR and locally build the manual using
from within the
lumol/doc/
directory (you can install mdbook using cargo).