Esempio n. 1
0
def msdPlot(m=10.0, k=10.0, c=1.0, x=0.0, xD=1.0):

    mass = m
    springK = k
    dampingC = c
    xInitial = x
    xDotInitial = xD
    smd = MassSpringDamper(m=mass, k=springK, c=dampingC)

    a_state, a_time = smd.simulate(xInitial, xDotInitial)

    fig, a_axes = plt.subplots(2, sharex=True)
    a_axes[0].plot(a_time, a_state[:, 0])
    a_axes[0].set_ylabel('Position')
    a_axes[0].grid()
    a_axes[1].plot(a_time, a_state[:, 1])
    a_axes[1].set_ylabel('Velocity')
    a_axes[1].grid()

    plt.xlabel('Time')
    title = 'Mass Spring Damper with m={0}, k={1}, c={2}'
    subtitle = 'Initial position of {3} and velocity of {4}'
    plt.suptitle((title + '\n' + subtitle).format(mass, springK, dampingC,
                                                  xInitial, xDotInitial))
    plt.show()
    def simulate_sys(self):

        # Print settings to console
        print('\nSimulation Values')
        print('mass: ' + str(self.MassSlider.value()))
        print('spring: ' + str(self.SpringSlider.value()))
        print('damper: ' + str(self.DamperSlider.value()))
        print('time: ' + str(self.TimeSlider.value()))
        print('timestep: ' + str(self.TimeStepSlider.value()))

        # Simulate system
        # First check for mass and spring constant equal to zero, which will
        # break the program
        if self.MassSlider.value() != 0 and self.SpringSlider.value() != 0:
            smd = MassSpringDamper(m=self.MassSlider.value(),
                                   k=self.SpringSlider.value(),
                                   c=self.DamperSlider.value())
            self.state, self.t = smd.simulate(self.InitialX.value(),
                                              self.InitialDX.value(),
                                              self.TimeSlider.value(),
                                              self.TimeStepSlider.value())
            self.change_sys_label()
            self.draw([self.t, self.state[:, 0]], text=None)

        # If mass or spring constant are equal to zero, deliver a dissappointed
        # message from Dr. Smart
        else:
            systemLabel = 'Type: ' + 'inconceivable!'
            self.MiddleBoxLabel.setText(systemLabel)
            self.draw()
    def _simulate_button_pressed(self):
        """
        Called when a user presses the button.
        
        Runs the MassSpringDamper() code fed with the slider values for simulator parameters then calls graph()
        """
        # slider values from sidebar
        self.slider_values = self.sidebar.get_slider_values()
        svs = self.slider_values
        if svs['mass'] == 0:
            svs['mass'] = 0.01

        # create a simulator from class found in 'msd.py'
        msd = MassSpringDamper(svs['mass'], svs['spring'], svs['damper'])

        # rename some variables for clarity with msd.simulate() arg names
        x0 = svs['initial_x']
        x_dot0 = svs['initial_x_dot']
        t = svs['time']
        t_step = svs['time_step']

        # invoke the simulator()
        states, times = msd.simulate(x0, x_dot0, t, t_step)
        # print(states, times)

        self.graph(states, times)
Esempio n. 4
0
#!usr/bin/env python

# Derek Bean
# ME 599
# 1/31/2017


from msd import MassSpringDamper as msd
import matplotlib.pyplot as plt

mass = 1.0
spring_const = 200.0
damper_const = 3.0

smd = msd(mass, spring_const, damper_const)
state, t = msd.simulate(smd, mass, spring_const, damper_const)


plt.figure(1)
plt.plot(t, [x[0] for x in state], 'k')
plt.xlabel('time (s)')
plt.ylabel('displacement')
plt.show()
Esempio n. 5
0
#        return data
#    else:
#        #count = count + 10
#        #print(str(count))
#        print('max: ' + str(max(data)))
#        print('min: ' + str(min(data)))
#        print(len(data))
#
#        time_chop(data[1:],upper,lower)

filename = 'data1.csv'
print(filename)
test_df = pd.read_csv(filename)

time = list(test_df.Time)
position = list(test_df.Position)

c_initial, c_final, overshoot, t_10, t_90, pseudo_pos, p_final = analyze_data(
    position, time)
crop_time, crop_data, t_s = time_chop(time, position, 2)

zeta = -np.log(overshoot / 100) / np.sqrt(np.pi**2 +
                                          (np.log(overshoot / 100))**2)
omega_n = 4 * zeta / t_s
c = 2 * zeta * omega_n
k = omega_n**2

#check some things
from msd import MassSpringDamper
system = MassSpringDamper(m=1.0, k=k, c=c)
state, t = system.simulate(c_initial, 0)
Esempio n. 6
0
def SMD():
    smd = MassSpringDamper(m=10.0, k=10.0, c=1.0)
    state, t = smd.simulate(0.0, 1.0)

    return t, state[:, 0], [min(t), max(t)],\
            [min(state[:, 0]), max(state[:, 0])]
Esempio n. 7
0
File: lab4.py Progetto: jjvang/me599
#!/usr/bin/env python3

from sine import sample_me_sine, plot_me
from sample_histo import random_histogram
from sort_sum_time import plot_me2
from msd import MassSpringDamper
from math import pi

if __name__ == '__main__':
    # plot a sine wave from 0 to 4 pi
    sample_me_sine(0, 4 * pi)
    # plot histogram of 10,000 samples
    # Each sample is the sum of 10 samples from a uniform distribution from 0 to 1
    random_histogram(10000)
    # Plot displacement of the mass over time
    smd = MassSpringDamper(m=10.0, k=10.0, c=1.0)  # spring, mass, damper
    state, t = smd.simulate(0.0, 1.0)  # time = 100 dt= 0.01
    plot_me(t, state[:, 0], 'Mass Spring Damper', 'Time(s)', 'Position')
    # list of lengths
    list_length = [10, 100, 1000, 10000, 10000, 1000000]
    # Plot how long it takes to sort a length of list
    plot_me2(list_length)
    # sorted_time(list_length)
    # # Plot how long it takes to sum a length of list
    # sum_time(list_length)
Esempio n. 8
0
#!/usr/bin/env python3

# ME499-S20 Python Lab 4
# Programmer: Jacob Gray
# Last Edit: 4/30/2020

import time
import numpy as np
from msd import MassSpringDamper
from matplotlib import pyplot as plt
from utils import simulate_gachapon
from utils import random_list
from counter import get_element_counts

smd = MassSpringDamper(m=1.0, k=5.0, c=2.5)
state, times = smd.simulate(1.0, -1.0, 40.0)
"""plt.plot(times, state[:, 0])
plt.title('Position vs. Time')
plt.xlabel('Time (s)')
plt.ylabel('Displacement (m)')
plt.savefig('problem_1_plot.png')"""

results = []

for i in range(1000):
    results.append(simulate_gachapon(15))

q75, q25 = np.percentile(results, [75, 25])
iqr = q75 - q25
h = 2 * iqr * len(results)**(-1 / 3)
bins = int((max(results) - min(results)) / h)
Esempio n. 9
0
from exercise_1 import plot_1
plot_1(x)

#exercise 2
from rand_dist_sample import sum_10
sums = sum_10(10000)
mpl.figure()
mpl.hist(sums, 100)
mpl.xlabel('value')
mpl.ylabel('occurrence')
mpl.title('Exercise 2: Histogram of Randomly Sampled Uniform Dist.')

#exercise 3
from msd import MassSpringDamper

system = MassSpringDamper(m=1.0, k=2.0, c=2.0)
state, t = system.simulate(0.0, -1.0)

mpl.figure()
mpl.plot(t, state[:, 1])
mpl.xlabel('time (s)')
mpl.ylabel('position (m)')
mpl.title('Exercise 3: Simulated SMD System')

#exercise 4
import timeit
from math import pow
from exercise_4 import random_list
from exercise_4 import how_long

#increment = [1,10,100,1000,10000,100000,1000000]