Skip to content
Snippets Groups Projects
Commit a6f0474d authored by David Preiss's avatar David Preiss
Browse files

ffmpeg file size

parent 7868090c
Branches
No related tags found
No related merge requests found
# NMM
## Getting started
To make it easy for you to get started with GitLab, here's a list of recommended next steps.
Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)!
## Add your files
- [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files
- [ ] [Add files using the command line](https://docs.gitlab.com/ee/gitlab-basics/add-file.html#add-a-file-using-the-command-line) or push an existing Git repository with the following command:
```
cd existing_repo
git remote add origin https://gitlab.cba.mit.edu/davepreiss/nmm.git
git branch -M main
git push -uf origin main
```
## Integrate with your tools
- [ ] [Set up project integrations](https://gitlab.cba.mit.edu/davepreiss/nmm/-/settings/integrations)
## Collaborate with your team
- [ ] [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/)
- [ ] [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html)
- [ ] [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically)
- [ ] [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/)
- [ ] [Automatically merge when pipeline succeeds](https://docs.gitlab.com/ee/user/project/merge_requests/merge_when_pipeline_succeeds.html)
## Test and Deploy
Use the built-in continuous integration in GitLab.
- [ ] [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/index.html)
- [ ] [Analyze your code for known vulnerabilities with Static Application Security Testing(SAST)](https://docs.gitlab.com/ee/user/application_security/sast/)
- [ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html)
- [ ] [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/)
- [ ] [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html)
***
# Editing this README
When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thank you to [makeareadme.com](https://www.makeareadme.com/) for this template.
## Suggestions for a good README
Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information.
## Name
Choose a self-explaining name for your project.
## Description
Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors.
## Badges
On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge.
## Visuals
Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method.
## Installation
Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection.
## Usage
Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README.
## Support
Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc.
## Roadmap
If you have ideas for releases in the future, it is a good idea to list them in the README.
## Contributing
State if you are open to contributions and what your requirements are for accepting them.
For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self.
You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser.
## Authors and acknowledgment
Show your appreciation to those who have contributed to the project.
## License
For open source projects, say how it is licensed.
## Project status
If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers.
Top level of NMM code - see weeks as linked from ___.
\ No newline at end of file
# Maxwell's Demon
This week I wanted to start with a "normal" approach that felt pythonic with a particle class and a bunch of for loops / if statements, but still using numpy arrays. Then rewrite it with everything possible vectorized in numpy and see the difference in solve times for each.
Below is a 5000 particle simulation with 2000 time steps that took 0.66 s to solve for (and incidentally much longer to write to a .mp4 file).
![](img/animation_fast.mp4)
[](maxwells_demon_fast.py)
[](maxwells_demon.py)
Interestingly Numba resulted in a ~4.5 s solve time compared to a 0.17 s time without it. From a very helpful conversation with Erik - for low particle counts
Erik
gpu has overhead to move from cpu to gpu
gpu is good at multithreading
optimized for throughput over latency
might be slower on gpu for small numbers of particles
jax and numba will both
\ No newline at end of file
week1/img/animation_fast.gif

9.83 MiB

from matplotlib import rc
# rc('animation', html='jshtml')
from math import *
import random
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import time
from numba import jit
class Particle:
def __init__(self): # initialize X and Y to zero if none is given
self.x = random.uniform(0, width)
self.y = random.uniform(0, height)
self.dx = random.uniform(-velocity_max*.707, velocity_max*.707)
self.dy = random.uniform(-velocity_max*.707, velocity_max*.707)
self.dm = (self.dx**2 + self.dy**2)**0.5 # velocity magnitude
self.color = np.array([0, 0, 1]) if self.dm < velocity_max/2 else np.array([1, 0, 0])
def move(self):
self.side = 0 if self.x < width/2 else 1 # 0 is left (hot) and 1 is right (cold)
self.left_bound = 0 if self.side == 0 else width/2
self.right_bound = width if self.side == 1 else width/2
self.x_new = self.x + self.dx * dt
self.y_new = self.y + self.dy * dt
# check if the door is open
if height/2-door_height/2 <= self.y_new <= height/2+door_height/2: # if we are at door height
if self.dm > velocity_max/2 and self.side == 1: # if we are a hot particle on the right side we pass
self.left_bound = 0
if self.dm < velocity_max/2 and self.side == 0: # if we are a cold particle on the left side we pass
self.right_bound = width
# check if we are bouncing off of a boundary
if not 0 <= self.y_new <= height:
self.dy = -self.dy
self.y_new = self.y + self.dy*dt
if not self.left_bound <= self.x_new <= self.right_bound:
self.dx = -self.dx
self.x_new = self.x + self.dx*dt
self.x = self.x_new
self.y = self.y_new
def update_state(self,t):
self.states[int(t/dt),:] = [self.x,self.y,self.dx,self.dy]
def increment(self,t):
self.move()
# self.update_state(t)
# Simulation code
num_particles = 100
width = 1000
height = 500
door_height = height/2
dt = 0.1 # time interval in seconds
t_final = 200
velocity_max = 50
fig = plt.figure(figsize=(10,5))
ax = plt.axes(xlim=(0,width), ylim=(0,height))
# Here we will just run the whole thing once to get the run time
time1 = time.time()
particles = []
for _ in range(num_particles):
particles.append(Particle())
xs = []
ys = []
colors = []
for s in range(int(t_final/dt)):
for p in particles:
p.increment(s*dt)
xs.append(p.x)
ys.append(p.y)
colors.append(p.color)
run_time = time.time() - time1
print("Run time: " + str(run_time))
# Now we will animate it
def frame(w):
ax.clear()
xs = []
ys = []
colors = []
for p in particles:
p.increment(w)
xs.append(p.x)
ys.append(p.y)
colors.append(p.color)
plt.title("Maxwell's Demon")
ax.set_xlim(0,width)
ax.set_ylim(0,height)
plt.plot([width/2, width/2], [0, height/2-door_height/2], color = 'black')
plt.plot([width/2, width/2], [height, height/2+door_height/2], color = 'black')
plt.plot([width/2, width/2], [height/2+door_height/2, height/2-door_height/2], color = 'yellow')
plot = ax.scatter(xs, ys, c=colors, marker='o')
return plot
anim = animation.FuncAnimation(fig, frame, frames=int(t_final/dt), blit=False, repeat=False, interval=1)
plt.show()
\ No newline at end of file
from math import *
import random
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import time
from numba import jit
import subprocess
def velocity_magnitude(dx,dy):
return np.hypot(dx, dy) # return (dx**2 + dy**2)**0.5
def calc_energy(sides, dms):
''' Calculate the energy of the system by summing the kinetic energy of the particles on each side.'''
on_left = len(np.where(sides == 0)[0])
on_right = num_particles - on_left
return np.sum(dms[np.where(sides == 0)])/on_left, np.sum(dms[np.where(sides == 1)])/on_right
# @jit(nopython=True)
def time_step(time, xs, ys, dxs, dys, dms, sides, energy):
''' Time_step accepts pointers to the whole array, but will frequently use [time,:] or [time-1,:]
to access all the current or previous values.
'''
sides[time-1,:] = np.where(xs[time-1,:] < width/2, 0, 1) # 0 is left (hot) and 1 is right (cold)
left_bound = np.where(sides[time-1] == 0, 0, width/2) # if we are on the left side, left bound is 0, if we are on the right side it's width/2
right_bound = np.where(sides[time-1] == 1, width, width/2) # if we are on the right side, right bound is width, if we are on the left it's width/2
xs[time,:] = xs[time-1,:] + dxs[time-1,:] * dt # update the current times from previous velocities
ys[time,:] = ys[time-1,:] + dys[time-1,:] * dt
# If we are a hot particle on the right side, open the door to pass left (first two lines check if we are door height)
left_bound = np.where( (height/2-door_height/2 <= ys[time,:])\
& (ys[time,:] <= height/2 + door_height/2)\
& (dms[0,:] > velocity_max/2)\
& (sides[time-1,:] == 1),\
0, left_bound) # if we are a hot particle on the right side we pass to the left
# If we are a cold particle on the left side open the door to pass right
right_bound = np.where( (height/2-door_height/2 <= ys[time,:])\
& (ys[time,:] <= height/2 + door_height/2)\
& (dms[0,:] < velocity_max/2)\
& (sides[time-1,:] == 0),\
width, right_bound) # if we are a cold particle on the left side we pass to the right
# Check if we are bouncing off of a boundary and update velocity and position in y then x if so
dys[time,:] = np.where((ys[time,:] < 0) | (ys[time,:] > height), -dys[time-1,:], dys[time-1,:]) # if we are outside in -y or +y, reverse the velocity
ys[time,:] = np.where((ys[time,:] < 0) | (ys[time,:] > height), ys[time,:] + dt*dys[time,:], ys[time,:]) # then update the position to be bounced with the new velocity
dxs[time,:] = np.where((xs[time,:] < left_bound) | (xs[time,:] > right_bound), -1*dxs[time-1,:], dxs[time-1,:]) # if we are outside in -x or +x, reverse the velocity
xs[time,:] = np.where((xs[time,:] < left_bound) | (xs[time,:] > right_bound), xs[time,:] + dt*dxs[time,:], xs[time,:]) # same for x
energy[time,:] = calc_energy(sides[time-1,:], dms[0,:])
def run_sim():
# initialize numpy arrays of x,y,dx,dy for each particle - arrays have a column for every particle and a row for every time step
times = np.arange(0,t_final,dt)
xs = np.zeros((int(t_final/dt),num_particles))
ys = np.zeros((int(t_final/dt),num_particles))
dxs = np.zeros((int(t_final/dt),num_particles))
dys = np.zeros((int(t_final/dt),num_particles))
dms = np.zeros((int(t_final/dt),num_particles)) # velocity magnitude
sides = np.zeros((int(t_final/dt),num_particles)) # 0 is left (hot) and 1 is right (cold)
# these two are constant for each particle and so just need two or one rows respectively
energy = np.zeros((int(t_final/dt), 2)) # total energy of particles on the left (0) and right (1) side
colors = np.zeros(num_particles) # RGB colors which are constant for each particle
# Assign the first row of each array (t=0) to a random or calculated value
xs[0,:] = np.random.uniform(0,width,num_particles)
ys[0,:] = np.random.uniform(0,height,num_particles)
dxs[0,:] = np.random.uniform(-velocity_max*.707,velocity_max*.707,num_particles)
dys[0,:] = np.random.uniform(-velocity_max*.707,velocity_max*.707,num_particles)
dms[0,:] = velocity_magnitude(dxs[0,:],dys[0,:])
sides[0,:] = np.where(xs[0,:] < width/2, 0, 1)
energy[0,:] = calc_energy(sides[0,:], dms[0,:])
colors = np.where(dms[0,:] < velocity_max/2, 0, 1) # 0 is hot, 1 is cold
# Update each time step
for t in range(1,int(t_final/dt)):
time_step(t, xs, ys, dxs, dys, dms, sides, energy)
return xs, ys, colors, energy
# Simulation Constants
num_particles = 5000
width = 1000
height = 500
door_height = height/2
dt = 0.06 # time interval in seconds
t_final = 50
velocity_max = 100
time1 = time.time()
xs, ys, colors, energy = run_sim() # run the simulation
solve_time = time.time() - time1
# Plotting the Simualtion
my_dpi = 96
fig = plt.figure(figsize=(800/my_dpi, 400/my_dpi), dpi=my_dpi)
ax = fig.add_axes([0, 0, 1, 1], frameon=False)
ax.set_xlim(0,width), ax.set_xticks([])
ax.set_ylim(0,height), ax.set_yticks([])
# Hot side label and energy
plt.text(10, 480, 'Hot Side', fontdict=None)
hot_text = plt.text(10, 465, f'{energy[0][0]: .2f}', fontdict=None)
# Cold Side label and energy
plt.text(510, 480, 'Cold Side', fontdict=None)
cold_text = plt.text(510, 465, f'{energy[0][1]: .2f}', fontdict=None)
# Total time to simulate
plt.text(10, 10, f'Total Time to calculate: {solve_time: .4f} s', fontdict=None)
plt.plot([width/2, width/2], [0, height/2-door_height/2], color = 'black')
plt.plot([width/2, width/2], [height, height/2+door_height/2], color = 'black')
plt.plot([width/2, width/2], [height/2+door_height/2, height/2-door_height/2], color = 'yellow')
plot = ax.scatter(xs[0,:], y = ys[0,:], # initial positions in X and Y
marker = 'o',
s = 10,
color = np.where(colors == 0, 'b', 'r'))
def animate(w):
# In animate you ideally just want to be updating positions / size / colors etc and not re-plotting everything with clear.
x = xs[w,:]
y = ys[w,:]
plot.set_offsets(np.c_[x,y])
hot_text.set_text(f'Avg. Velocity: {energy[w,0]: .2f}')
cold_text.set_text(f'Avg. Velocity: {energy[w,1]: .2f}')
anim = animation.FuncAnimation(fig, animate, frames=int(t_final/dt), blit=False, repeat=False, interval=1/30*1000)
# plt.show()
f = r"c://Users/david/Desktop/NMM/week1/img/animation_fast.gif"
writervideo = animation.FFMpegWriter(fps=30, extra_args=['-vcodec', 'libx264'])
# anim.save(f, writer=writervideo, dpi=30)
anim.save(f, writer='imagemagick', fps=30, dpi=my_dpi)
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment