-
-
Notifications
You must be signed in to change notification settings - Fork 360
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
Add Python3 code to Verlet #146
Closed
Closed
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ Jeremie Gillet (- Jie -) | |
Salim Khatib | ||
Hitesh C | ||
Jess 3Jane | ||
Hugo Granström |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# submitted by HugoGranstrom | ||
|
||
def verlet_step(pos_prev, pos, acc, dt): | ||
pos_next = 2 * pos - pos_prev + acc * dt * dt | ||
return pos_next | ||
|
||
# calculate velocity at the current timestep | ||
def stormer_verlet_current(pos_prev, pos_next, dt): | ||
vel_current = (pos_next - pos_prev) / (2 * dt) | ||
return vel_current | ||
|
||
#calculate velocity at the next timestep | ||
def stormer_verlet_next(pos_next, pos, dt): | ||
vel_next = (pos_next - pos) / (dt) | ||
return vel_next | ||
|
||
def acc_func(pos): | ||
# write the function for acceleration here, for example acc = F/m | ||
# now acc is constant and independent of pos, thus pos is not used | ||
# for other implentations it can be used though | ||
return -10 | ||
|
||
def velocity_verlet_step(pos, vel, acc, dt): | ||
pos_next = pos + vel * dt + 0.5 * acc * dt * dt | ||
acc_next = acc_func(pos_next) | ||
vel_next = vel + 0.5 * (acc + acc_next) * dt | ||
return pos_next, vel_next, acc_next | ||
|
||
# HugoGranstrom | ||
# example calculating time it takes for an object to fall 5 m with acc = -10 | ||
# because acc is constant, all 0.5 * (acc + acc_next) * dt becomes just acc * dt | ||
|
||
def run_verlet(pos_start, acc, dt): | ||
time = 0 | ||
pos = pos_start | ||
pos_prev = pos_start | ||
|
||
while pos > 0: | ||
# calculate next timestep | ||
pos_next = 2 * pos - pos_prev + acc * dt * dt | ||
|
||
# prepare for next timestep | ||
pos_prev = pos | ||
pos = pos_next | ||
time += dt | ||
|
||
print(f"Classic Verlet took {time} seconds") | ||
|
||
def run_velocity_verlet(pos_start, vel_start, acc, dt): | ||
time = 0 | ||
pos = pos_start | ||
vel = vel_start | ||
|
||
while pos > 0: | ||
# calculate next timestep | ||
pos_next = pos + vel * dt + 0.5 * acc * dt * dt | ||
vel_next = vel + acc * dt | ||
|
||
# prepare for next timestep | ||
pos = pos_next | ||
vel = vel_next | ||
time += dt | ||
|
||
print(f"Velocity Verlet took {time} seconds") | ||
|
||
run_verlet(5, -10, 0.01) | ||
run_velocity_verlet(5, 0, -10, 0.01) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 got rid of Python 2 and
lang="py3"
became justlang="py"
. Just remove the "3" everyone. Don't worry about the leftover Python 2 code imports.