The force function
Define the function using keyword def and name it "get_force", like this:Don't forget the ":"!
Since this function takes in one arguement which is the positions. However, in LJ's case, only separation between interacting atoms matter. Since we are only modeling two atoms here, we will use the variable name "r". So now the first line of this function would look like:
Then define the "force" variable, and assign the force calculated with r and the equation given above in the list to the force variable.
Now that what this function needs to return is calculated, we simply return its value with
The acceleration function
Define a function with name "get_acceleration", and take in one arguement: force.Define a variable "acceleration". Assign force devided by mass (1 in this case) to it.
Return the variable "acceleration".
The move calculator function
Define a function with name "get_delta_r_v". It takes in two arguments: velocity, accelerationWe will hard code time increment to be 0.01 by creating a variable called "delta_t" and set it to be 0.01.
Define a variable "delta_r" and assign to it the value calculated using the equation provided previously.
Assign to the variable velocity, the new velocity calculated using the equation provided previously.
Return both delta_r and velocity by returning a tuple like this:
The damper function
Define a function with name "damp_delta_r_v". It takes in two arguments: velocity, position change delta_rHard code the threshold by creating a variable called "threshold_r" and assign to it value 0.1.
Use the if condition. If delta_r is greater than theshold_r, assign threshold_r to delta_r. Assign 0 to velocity.
Else, assign velocity * 0.9 to velocity.
Outside of the if condition block, return delta_r and velocity.
The updater function
Define a function with name "update_r". It takes in two arguments: r and delta_r.It assines the sum of r and delta_r to variable r.
Then it returns r.
The main function
Here is the part of code that uses the above functions and find the optimal bond length.First initialize variables by setting r to 0.3, velocity to 0, delta_r to 0 and acceleration to 0.
Initialize force to be 100.
Use while loop, with condition of the absolute value of force greater than 0.01. In side the while loop:
- Assign get_force return value to force.
- Assign get_acceleration return value to acceleration.
- Assign get_delta_r_v return values to delta_r, velocity.
- Assign damp_delta_r_v return values to delta_r, velocity.
- Assign update_r return value to r.