My Project
demo_pos_pos_spr_friction.py
Go to the documentation of this file.
1 from run_animation_layout_support_functions import *
2 
3 
5  run_name,
6  shown_indexes,
7  shift_amount_per_frame=1,
8  frames=800,
9  live_plotting=True,
10  sample_range=[0, 2*100],
11  velocity_index=40
12 ):
13  phase_data_block = load_friction_phase_data(run_name,
14  "block_phase_" + str(velocity_index))
15  phase_data_pad = load_friction_phase_data(run_name,
16  "pad_phase_" + str(velocity_index))
17  frames_per_velocity = phase_data_block.shape[1]
18  velocities = loadYaml(run_name,
19  "/results/friction_phase/increment_run.yaml")["velocities"]
20  for i in range(velocity_index + 1, len(velocities)):
21  phase_data_block = np.concatenate((phase_data_block, load_friction_phase_data(run_name, "block_phase_" + str(i))), axis=1)
22  phase_data_pad = np.concatenate((phase_data_pad, load_friction_phase_data(run_name, "pad_phase_" + str(i))), axis=1)
23 
24  position_data, velocity_data = seperate_pos_vel_data(phase_data_block)
25  if("--nopad" in sys.argv):
26  phase_shape_block = phase_data_block.shape
27  block_average = np.zeros(phase_shape_block[1])
28  number_of_blocks = int(phase_shape_block[0]/2)
29  for i in range(0, number_of_blocks):
30  block_average += phase_data_block[2*i]
31  position_data_pad = block_average/number_of_blocks
32 
33  position_data_pad_title = "Block avg. position"
34  position_data_pad_y_label = "$\\frac{\sum_1^n u_i}{n}$"
35  show_pad = False
36  index_first_spring = 1
37  else:
38  position_data_pad = phase_data_pad[0]
39  position_data_pad_title = "Pad position"
40  position_data_pad_y_label = "$x$"
41  show_pad = True
42  index_first_spring = 3
43 
44  number_of_data_points = len(position_data[0])
45  if(frames > number_of_data_points):
46  frames = number_of_data_points
47 
48  position_plot = make_single_line_line_plot(position_data,
49  shown_indexes,
50  ax_number=0,
51  y_min=-3.0,
52  y_max=2.0,
53  x_label="Time steps shown",
54  y_label="$u_i$")
55 
56  bar_plot = make_single_line_bar_plot(position_plot,
57  ax_number=1,
58  line_number=0,
59  x_label="Block",
60  y_label="$u_i$")
61 
62  position_pad_plot = make_single_line_line_plot([position_data_pad],
63  shown_indexes,
64  ax_number=2,
65  y_min=-3.0,
66  y_max=2.0,
67  x_label="Time steps shown",
68  y_label=position_data_pad_y_label)#, x_values=np.arange(len(position_data_pad)))
69 
70  spring_plot = make_single_line_spring_plot(position_plot,
71  ax_number=3,
72  line_number=0,
73  common_indexes=shown_indexes,
74  pad_position=position_data_pad,
75  x_label="$x$",
76  y_label="Model",
77  show_pad=show_pad)
78  animation_plots = [position_plot, bar_plot, position_pad_plot, spring_plot]
79 
80  bk_animation = BKAnimation(animation_plots, sleep_amount=0.000, max_lim_change=0.3)
81  bk_animation.init_live_plot((231, 232, 233, 212),
82  (20, 10),
83  ["Position", "Bar", position_data_pad_title, "Model"],
84  show_plot=live_plotting,
85  suptitle="Velocity: " + str(velocities[velocity_index]))
86 
87 
88  plot_shape = [len(animation_plots), len(position_plot.single_line_list)]
89  only_shift_first = False # used for data points after the remaining data points are fewer than the showed ones
90  only_shift_first_index = number_of_data_points - shown_indexes[1] - 1 # 0 indexation
91  def init_plot_frames():
92  bk_animation.update_subplots(update_lims=False)
93  def plot_frames(n):
94  nonlocal velocity_index, only_shift_first
95  bk_animation.update_suptitle("Velocity: %(velocity)1.3f |\
96  Frame: %(frame)i" % {'velocity': velocities[velocity_index],
97  'frame': n})
98  if(n % frames_per_velocity == 0 and n > 0):
99  velocity_index += 1
100 
101  if(n % only_shift_first_index == 0 and n > 0):
102  only_shift_first = True
103  shift_z_indexes_of_lines_in_ax(bk_animation,
104  shift_amount_per_frame,
105  ax_number=0,
106  n_line_number=plot_shape[1],
107  index_type='y',
108  only_shift_first=only_shift_first)
109  shift_z_indexes_of_lines_in_ax(bk_animation,
110  shift_amount_per_frame,
111  ax_number=2,
112  n_line_number=1,
113  index_type='y',
114  only_shift_first=only_shift_first)
115  #bk_animation.shift_x_indexes(ax_number=3, line_number=2, shift_amount=shift_amount_per_frame)
116  bk_animation.change_y(1, 0)
117  bk_animation.change_x(3, 0, get_axis="y")
118  if(show_pad):
119  bk_animation.change_x(3, 1)
120  bk_animation.update_upper_springs(3, index_first_spring=index_first_spring)
121 
122  bk_animation.update_subplots()
123  sleep(bk_animation.sleep_amount)
124 
125  if(live_plotting):
126  init_plot_frames()
127  for i in range(0, frames):
128  plot_frames(i)
129  # if(i % 10 == 0):
130  # input()
131  # uncomment to stop animation every 10th frame to take snapshots of single frames
132  else:
133  ani = animation.FuncAnimation(bk_animation.fig,
134  plot_frames,
135  init_func=init_plot_frames,
136  frames=frames,
137  interval=30)
138  writer = animation.writers['ffmpeg'](fps=20)
139  simulation_path = loader.get_simulation_path(run_name)
140  ani.save(simulation_path + '/animation_vid_7.mp4',
141  writer=writer,
142  dpi=100)
def make_single_line_spring_plot(single_plot, ax_number, line_number, pad_position=[], common_indexes=[], height_of_plot=0.1, x_label="", y_label="", show_pad=False)
def make_single_line_bar_plot(single_plot, ax_number, line_number, x_label="", y_label="")
def make_single_line_line_plot(y_data, common_indexes, ax_number, x_values=[], y_min=0, y_max=0, x_label="", y_label="")
def shift_z_indexes_of_lines_in_ax(bk_animation, shift_amount_per_frame, ax_number, n_line_number, index_type, only_shift_first=False)
def pos_pos_spr_friction(run_name, shown_indexes, shift_amount_per_frame=1, frames=800, live_plotting=True, sample_range=[0, velocity_index=40)