Commit 784b14b8 authored by Tristan Renon's avatar Tristan Renon
Browse files

Ooops I maybe did commit the python environment...

parent ec491642
# Default ignored files
/shelf/
/workspace.xml
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.7 (motorPhaser)" project-jdk-type="Python SDK" />
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/motorPhaser.iml" filepath="$PROJECT_DIR$/.idea/motorPhaser.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/venv" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/../../.." vcs="Git" />
</component>
</project>
\ No newline at end of file
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig, ax = plt.subplots(figsize=(8, 8))
ax.set(xlim=(-2, 2), ylim=(-2, 2))
N = 50
xSin = np.linspace(0, 2*np.pi, N) # start,stop,step
sin90 = (np.sin(xSin), np.cos(xSin))
phaseShift = (35, 90)
def animate(i):
ax.cla()
#This one start at 0, 0
ax.arrow(0, 0,
np.sin(phaseShift[0]/180*np.pi)*sin90[0][i], np.cos(phaseShift[0]/180*np.pi)*sin90[0][i],
head_width=.1, head_length=.1, fc='blue', ec='blue')
#This one should start at the end of the first one
ax.arrow(np.sin(phaseShift[0]/180*np.pi)*sin90[0][i], np.cos(phaseShift[0]/180*np.pi)*sin90[0][i],
np.sin(phaseShift[1]/180*np.pi)*sin90[1][i], np.cos(phaseShift[1]/180*np.pi)*sin90[1][i],
head_width=.1, head_length=.1, fc='red', ec='red')
# ax.arrow(0, 0,
# sin90[0][i], sin90[1][i],
# head_width=.1, head_length=.1, fc='green', ec='green')
ax.plot(np.sin(phaseShift[0]/180*np.pi)*sin90[0][:i]+np.sin(phaseShift[1]/180*np.pi)*sin90[1][:i], np.cos(phaseShift[0]/180*np.pi)*sin90[0][:i]+np.cos(phaseShift[1]/180*np.pi)*sin90[1][:i])
ax.set(xlim=(-2, 2), ylim=(-2, 2))
ani = animation.FuncAnimation(fig, animate, interval=20, frames=N, blit=False, save_count=50)
plt.show()
\ No newline at end of file
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig, ax = plt.subplots(figsize=(8, 8))
ax.set(xlim=(-2, 2), ylim=(-2, 2))
N = 50
xSin = np.linspace(0, 2*np.pi, N) # start,stop,step
sin90 = (np.sin(xSin), np.cos(xSin))
def animate(i):
ax.cla()
#This one start at 0, 0
ax.arrow(0, 0,
sin90[0][i], 0,
head_width=.1, head_length=.1, fc='blue', ec='blue')
#This one should start at the end of the first one
ax.arrow(sin90[0][i], 0,
0, sin90[1][i],
head_width=.1, head_length=.1, fc='red', ec='red')
ax.arrow(0, 0,
sin90[0][i], sin90[1][i],
head_width=.1, head_length=.1, fc='green', ec='green')
ax.plot(sin90[0][:i], sin90[1][:i])
ax.set(xlim=(-2, 2), ylim=(-2, 2))
ani = animation.FuncAnimation(fig, animate, interval=20, frames=N, blit=False, save_count=50)
plt.show()
\ No newline at end of file
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig, ax = plt.subplots(figsize=(8, 8))
ax.set(xlim=(-2, 2), ylim=(-2, 2))
N = 50
nSamples = 50
nPhases = 3
phaseShift = (0, 120, 240)
def generateSin(n=2, phaseShift=(0, 90)):
xSin = np.linspace(0, 2 * np.pi, nSamples)
ret = list()
for i in range(n):
ret.append(np.sin(xSin+phaseShift[i]/180*np.pi))
return tuple(ret)
generatedSin = generateSin(nPhases, phaseShift)
def animate(i):
ax.cla()
ax.arrow(0, 0,
np.sin(phaseShift[0]/180*np.pi)*generatedSin[0][i], np.cos(phaseShift[0]/180*np.pi)*generatedSin[0][i],
head_width=.1, head_length=.1, fc='blue', ec='blue')
#This one should start at the end of the first one
ax.arrow(np.sin(phaseShift[0]/180*np.pi)*generatedSin[0][i], np.cos(phaseShift[0]/180*np.pi)*generatedSin[0][i],
np.sin(phaseShift[1]/180*np.pi)*generatedSin[1][i], np.cos(phaseShift[1]/180*np.pi)*generatedSin[1][i],
head_width=.1, head_length=.1, fc='red', ec='red')
#This one should start at the end of the first one
ax.arrow(np.sin(phaseShift[1]/180*np.pi)*generatedSin[1][i]+np.sin(phaseShift[0]/180*np.pi)*generatedSin[0][i], np.cos(phaseShift[1]/180*np.pi)*generatedSin[1][i]+np.cos(phaseShift[0]/180*np.pi)*generatedSin[0][i],
np.sin(phaseShift[2]/180*np.pi)*generatedSin[2][i], np.cos(phaseShift[2]/180*np.pi)*generatedSin[2][i],
head_width=.1, head_length=.1, fc='green', ec='green')
ax.set(xlim=(-2, 2), ylim=(-2, 2))
ani = animation.FuncAnimation(fig, animate, interval=20, frames=N, blit=False, save_count=50)
plt.show()
\ No newline at end of file
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig, ax = plt.subplots(figsize=(8, 8))
ax.set(xlim=(-2, 2), ylim=(-2, 2))
N = 50
xSin = np.linspace(0, 2*np.pi, N) # start,stop,step
sin = np.sin(xSin)
def animate(i):
ax.cla()
#This one start at 0, 0
ax.arrow(0, 0,
sin[i], 0,
head_width=.1, head_length=.1, fc='blue', ec='blue')
ax.set(xlim=(-2, 2), ylim=(-2, 2))
ani = animation.FuncAnimation(fig, animate, interval=20, frames=N, blit=False, save_count=50)
plt.show()
\ No newline at end of file
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig, ax = plt.subplots(figsize=(8, 8))
ax.set(xlim=(-1.5, 1.5), ylim=(-1.5, 1.5))
N = 50
xSin = np.linspace(0, 2*np.pi, N) # start,stop,step
sin90 = (np.sin(xSin), np.cos(xSin))
line, = ax.plot(sin90[0], sin90[1])
def animate(i):
line.set_data(sin90[0][:i], sin90[1][:i])
return line,
ani = animation.FuncAnimation(
fig, animate, interval=20, frames=N, blit=True, save_count=N)
plt.show()
\ No newline at end of file
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib.pyplot import cm
fig, ax = plt.subplots(figsize=(8, 8))
ax.set(xlim=(0, 2*np.pi), ylim=(-2, 2))
for i in range(3):
print(next(ax._get_lines.prop_cycler)['color'])
\ No newline at end of file
"""
General Numerical Solver for the 1D Time-Dependent Schrodinger's equation.
adapted from code at http://matplotlib.sourceforge.net/examples/animation/double_pendulum_animated.py
Double pendulum formula translated from the C code at
http://www.physics.usyd.edu.au/~wheat/dpend_html/solve_dpend.c
author: Jake Vanderplas
email: vanderplas@astro.washington.edu
website: http://jakevdp.github.com
license: BSD
Please feel free to use and modify this, but keep the above information. Thanks!
"""
from numpy import sin, cos
import numpy as np
import matplotlib.pyplot as plt
import scipy.integrate as integrate
import matplotlib.animation as animation
class DoublePendulum:
"""Double Pendulum Class
init_state is [theta1, omega1, theta2, omega2] in degrees,
where theta1, omega1 is the angular position and velocity of the first
pendulum arm, and theta2, omega2 is that of the second pendulum arm
"""
def __init__(self,
init_state=[120, 0, -20, 0],
L1=1.0, # length of pendulum 1 in m
L2=1.0, # length of pendulum 2 in m
M1=1.0, # mass of pendulum 1 in kg
M2=1.0, # mass of pendulum 2 in kg
G=9.8, # acceleration due to gravity, in m/s^2
origin=(0, 0)):
self.init_state = np.asarray(init_state, dtype='float')
self.params = (L1, L2, M1, M2, G)
self.origin = origin
self.time_elapsed = 0
self.state = self.init_state * np.pi / 180.
def position(self):
"""compute the current x,y positions of the pendulum arms"""
(L1, L2, M1, M2, G) = self.params
x = np.cumsum([self.origin[0],
L1 * sin(self.state[0]),
L2 * sin(self.state[2])])
y = np.cumsum([self.origin[1],
-L1 * cos(self.state[0]),
-L2 * cos(self.state[2])])
return (x, y)
def energy(self):
"""compute the energy of the current state"""
(L1, L2, M1, M2, G) = self.params
x = np.cumsum([L1 * sin(self.state[0]),
L2 * sin(self.state[2])])
y = np.cumsum([-L1 * cos(self.state[0]),
-L2 * cos(self.state[2])])
vx = np.cumsum([L1 * self.state[1] * cos(self.state[0]),
L2 * self.state[3] * cos(self.state[2])])
vy = np.cumsum([L1 * self.state[1] * sin(self.state[0]),
L2 * self.state[3] * sin(self.state[2])])
U = G * (M1 * y[0] + M2 * y[1])
K = 0.5 * (M1 * np.dot(vx, vx) + M2 * np.dot(vy, vy))
return U + K
def dstate_dt(self, state, t):
"""compute the derivative of the given state"""
(M1, M2, L1, L2, G) = self.params
dydx = np.zeros_like(state)
dydx[0] = state[1]
dydx[2] = state[3]
cos_delta = cos(state[2] - state[0])
sin_delta = sin(state[2] - state[0])
den1 = (M1 + M2) * L1 - M2 * L1 * cos_delta * cos_delta
dydx[1] = (M2 * L1 * state[1] * state[1] * sin_delta * cos_delta
+ M2 * G * sin(state[2]) * cos_delta
+ M2 * L2 * state[3] * state[3] * sin_delta
- (M1 + M2) * G * sin(state[0])) / den1
den2 = (L2 / L1) * den1
dydx[3] = (-M2 * L2 * state[3] * state[3] * sin_delta * cos_delta
+ (M1 + M2) * G * sin(state[0]) * cos_delta
- (M1 + M2) * L1 * state[1] * state[1] * sin_delta
- (M1 + M2) * G * sin(state[2])) / den2
return dydx
def step(self, dt):
"""execute one time step of length dt and update state"""
self.state = integrate.odeint(self.dstate_dt, self.state, [0, dt])[1]
self.time_elapsed += dt
# ------------------------------------------------------------
# set up initial state and global variables
pendulum = DoublePendulum([180., 0.0, -20., 0.0])
dt = 1. / 30 # 30 fps
# ------------------------------------------------------------
# set up figure and animation
fig = plt.figure()
ax = fig.add_subplot(111, aspect='equal', autoscale_on=False,
xlim=(-2, 2), ylim=(-2, 2))
ax.grid()
line, = ax.plot([], [], 'o-', lw=2)
time_text = ax.text(0.02, 0.95, '', transform=ax.transAxes)
energy_text = ax.text(0.02, 0.90, '', transform=ax.transAxes)
def init():
"""initialize animation"""
line.set_data([], [])
time_text.set_text('')
energy_text.set_text('')
return line, time_text, energy_text
def animate(i):
"""perform animation step"""
global pendulum, dt
pendulum.step(dt)
line.set_data(*pendulum.position())
time_text.set_text('time = %.1f' % pendulum.time_elapsed)
energy_text.set_text('energy = %.3f J' % pendulum.energy())
return line, time_text, energy_text
# choose the interval based on dt and the time to animate one step
from time import time
t0 = time()
animate(0)
t1 = time()
interval = 1000 * dt - (t1 - t0)
ani = animation.FuncAnimation(fig, animate, frames=300,
interval=interval, blit=True, init_func=init)
# save the animation as an mp4. This requires ffmpeg or mencoder to be
# installed. The extra_args ensure that the x264 codec is used, so that
# the video can be embedded in html5. You may need to adjust this for
# your system: for more information, see
# http://matplotlib.sourceforge.net/api/animation_api.html
# ani.save('double_pendulum.mp4', fps=30, extra_args=['-vcodec', 'libx264'])
plt.show()
\ No newline at end of file
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib.pyplot import cm
nSamples = 50
nPhases = 10
# phaseAmplitude = (0, 1.382, 1.382, 1.382, 1.382)
# phaseShiftMec = (0, 72, 72*2, 72*3, 72*4)
# phaseShiftReg = (0, 36, 180-36, 180+36, 360-36)
# phaseAmplitude = (1, 1, 1, 1, 1)
# phaseShiftMec = (0, 72, 72*2, 72*3, 72*4)
# phaseShiftReg = phaseShiftMec
fig, ax = plt.subplots(figsize=(8, 8))
ax.set(xlim=(-2, 2), ylim=(-2, 2))
def generateSin(n=3, phaseShiftReg=None, phaseAmplitude=None, phaseShiftMec=None):
# Generate things if user did not tell about them
if phaseAmplitude is None:
phaseAmplitude = np.ones(n)
if phaseShiftMec is None:
if n == 2:
phaseShiftMec = (0, 90)
else:
phaseShiftMec = np.linspace(0, 360, n, endpoint=False)
if phaseShiftReg is None:
phaseShiftReg = phaseShiftMec
# Make sure people gave valid parameters
assert len(phaseShiftReg) == n, "phaseShiftReg is not the same size as Phases (n)"
assert len(phaseAmplitude) == n, "phaseAmplitude is not the same size as Phases (n)"
assert len(phaseShiftMec) == n, "phaseShiftMec is not the same size as Phases (n)"
# Generate x (0 to 2pi)
xSin = np.linspace(0, 2 * np.pi, nSamples)
ret = list()
# Generate power supply for coils
for i in range(n):
ret.append(phaseAmplitude[i]*np.sin(xSin+phaseShiftReg[i]/180*np.pi))
return tuple(ret), phaseShiftMec, phaseAmplitude
# generatedSin, phaseShiftMec, phaseAmplitude = generateSin(n=nPhases, phaseShiftReg=phaseShiftReg, phaseAmplitude=phaseAmplitude, phaseShiftMec=phaseShiftMec)
generatedSin, phaseShiftMec, phaseAmplitude = generateSin(n=nPhases)
def animate(i):
ax.cla()
sliding = [[0, 0], [0, 0]]
for j in range(nPhases):
colors = next(ax._get_lines.prop_cycler)['color']
sliding[0] += sliding[1]
sliding[1] = [np.sin(phaseShiftMec[j]/180*np.pi)*generatedSin[j][i], np.cos(phaseShiftMec[j]/180*np.pi)*generatedSin[j][i]]
ax.arrow(sliding[0][0], sliding[0][1],
sliding[1][0], sliding[1][1],
head_width=.1, head_length=.1, fc=colors, ec=colors)
limit = max(phaseAmplitude)*1.1
ax.set(xlim=(-limit, limit), ylim=(-limit, limit))
ani = animation.FuncAnimation(fig, animate, interval=20, frames=nSamples, blit=False, save_count=nSamples)
plt.show()
# This is a sample Python script.
# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
def print_hi(name):
# Use a breakpoint in the code line below to debug your script.
print(f'Hi, {name}') # Press Ctrl+F8 to toggle the breakpoint.
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
print_hi('PyCharm')
# See PyCharm help at https://www.jetbrains.com/help/pycharm/
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib.pyplot import cm
nSamples = 50
inputNPhases = 10
#
# inputPhaseAmplitude = (0, 1.382, 1.382, 1.382, 1.382)
# inputPhaseShiftMec = (0, 72, 72*2, 72*3, 72*4)
# inputPhaseShiftReg = (0, 36, 180-36, 180+36, 360-36)
fig, ax = plt.subplots(figsize=(8, 8))
ax.set(xlim=(-(inputNPhases+1)/2, (inputNPhases+1)/2), ylim=(-(inputNPhases+1)/2, (inputNPhases+1)/2))
def generateSin(n=3, phaseShiftReg=None, phaseAmplitude=None, phaseShiftMec=None):
#Generate things if user did not tell about them
if phaseAmplitude is None:
phaseAmplitude = np.ones(n)
if phaseShiftMec is None:
if n == 2:
phaseShiftMec=(0, 90)
else:
phaseShiftMec = np.linspace(0, 360, n, endpoint=False)
if phaseShiftReg is None:
phaseShiftReg = phaseShiftMec
#Make sure people gave valid parameters
assert len(phaseShiftReg) == n, "phaseShiftReg is not the same size as Phases (n)"
assert len(phaseAmplitude) == n, "phaseAmplitude is not the same size as Phases (n)"
assert len(phaseShiftMec) == n, "phaseShiftMec is not the same size as Phases (n)"
# Generate x (0 to 2pi)
xSin = np.linspace(0, 2 * np.pi, nSamples)
ret = list()
# Generate power supply for coils
for i in range(n):
ret.append(phaseAmplitude[i]*np.sin(xSin+phaseShiftReg[i]/180*np.pi))
return tuple(ret), n, phaseShiftReg, phaseAmplitude, phaseShiftMec
# generatedSin, nPhases, phaseShiftReg, phaseAmplitude, phaseShiftMec = generateSin(inputNPhases, inputPhaseShiftReg, inputPhaseAmplitude, inputPhaseShiftMec)
generatedSin, nPhases, phaseShiftReg, phaseAmplitude, phaseShiftMec = generateSin(n=inputNPhases)
def animate(i):
ax.cla()
sliding = [[0, 0], [0, 0]]
plotX = np.zeros(i)
plotY = np.zeros(i)
for j in range(nPhases):
colors = next(ax._get_lines.prop_cycler)['color']
# New start = sum of last starts
sliding[0][0] += sliding[1][0]
sliding[0][1] += sliding[1][1]
# New end = sin-cos (x-y) of mechanical multiplied by power supply
sliding[1] = [np.sin(phaseShiftMec[j]/180*np.pi)*generatedSin[j][i], np.cos(phaseShiftMec[j]/180*np.pi)*generatedSin[j][i]]
ax.arrow(sliding[0][0], sliding[0][1],
sliding[1][0], sliding[1][1],
head_width=.1, head_length=.1, fc=colors, ec=colors)