My Project
plotPadList.py
Go to the documentation of this file.
1 import os
2 import sys
3 import numpy as np
4 from matplotlib import pyplot as plt
5 
6 import matplotlib as mpl
7 
8 mpl.rcParams["svg.fonttype"] = "none"
9 mpl.rcParams["font.size"] = 16
10 
11 mpl.rcParams["savefig.directory"] = "../../Figures"
12 plt.rcParams["savefig.format"] = "svg"
13 
14 mpl.rc('font', **{'family': 'serif', 'serif': ['Computer Modern']})
15 mpl.rc('text', usetex=True)
16 
17 import yaml
18 
19 try:
20  from yaml import CLoader as Loader, CDumper as Dumper
21 except ImportError:
22  from yaml import Loader, Dumper
23 
24 class PlotPads:
25  def __init__(self, run_name = [""], numeric_method = "_midpoint_", time_steps_plotted = 300000):
26  self.loadFiles(run_name, numeric_method)
27  self.setUpPlot(run_name)
28  self.time_steps_plotted = time_steps_plotted
29 
30  def loadYaml(
31  self,
32  run_name
33  ):
34  stream = open(self.results_path + run_name + "/doc/yaml/parameters.yaml", 'r')
35  run_parameters = yaml.load(stream, Loader = Loader)
36  return run_parameters
37 
38 
39  def loadFiles(self, run_name, numeric_method):
40  self.results_path = os.path.dirname(os.path.realpath(__file__)) + "/../../../results/"
41 
42  results_folder = "/results/"
43 
44  self.run_parameters = self.loadYaml(run_name[0])
45 
46  shared_names = self.run_parameters["Parameters"]["file_name"]
47 
48 
49  self.pad_position = [np.loadtxt(self.results_path + run_name[0] + results_folder + shared_names + numeric_method + "pad_position.csv", delimiter = ',')]
50  self.pad_velocity = [np.loadtxt(self.results_path + run_name[0] + results_folder + shared_names + numeric_method + "pad_velocity.csv", delimiter = ',')]
51 
52  self.label_names = self.getPadLabels(run_name, use_dictionary_names= False)
53 
54  if(len(run_name) > 1):
55  for i in range(1,len(run_name)):
56  new_pad_position = np.loadtxt(self.results_path + run_name[i] + results_folder + shared_names + numeric_method + "pad_position.csv", delimiter = ',')
57  new_pad_velocity = np.loadtxt(self.results_path + run_name[i] + results_folder + shared_names + numeric_method + "pad_velocity.csv", delimiter = ',')
58 
59  self.pad_position = np.vstack((self.pad_position, new_pad_position))
60  self.pad_velocity = np.vstack((self.pad_velocity, new_pad_velocity))
61 
62  def getPadLabels(self, run_name, use_dictionary_names = False):
63  if(use_dictionary_names):
64  short_names = {'blocks_3_no_damper_no_neighbor_test': 'ND,NN', 'blocks_3_no_damper_test': 'ND', \
65  'blocks_3_no_friction_test': 'NF', 'blocks_3_no_debug_test': 'BKP', \
66  'blocks_3_no_friction_no_damper_no_neighbor_test': 'NF,ND,NN', \
67  'blocks_3_no_debug_long_test': 'BKP Long'}
68 
69  label_names = [short_names[x] for x in run_name]
70  else:
71  label_names = ["seed: 101", "seed: 101", "seed: 102", "seed: 103"]#["no pad", "inf. weight"]#["$\sigma=0.1$", "$\sigma=0.01$", "$\sigma=0.001$"]
72 
73  return (label_names)
74 
75  def x_values_for_run(
76  self,
77  run_name,
78  number_data_points = 0
79  ):
80  run_parameters = self.loadYaml(run_name)
81  start = 0
82  stop = run_parameters["Parameters"]["max_time"]
83 
84  if( number_data_points == 0 ):
85  dt = run_parameters["Parameters"]["dt"]
86  save_every_interval_dt = run_parameters["Parameters"]["save_interval_dt"]
87 
88  number_data_points = (stop - start)/(dt*save_every_interval_dt)
89 
90 
91  x_values = np.linspace(start = start, stop = stop, num = number_data_points)
92  return x_values
93 
94 
95  def setUpPlot(self, run_name):
96  self.one_block_boolean = False
97 
98  self.linewidth = 0.9
99 
100  start = 0
101  stop = self.run_parameters["Parameters"]["max_time"]
102 
103  self.x = np.array([self.x_values_for_run(file) for file in run_name])
104 
105 
106  def plot_pad_position(self):
107 
108  if(len(self.x.shape) > 1):
109  plot_untill = int(np.ceil(self.time_steps_plotted/(self.x[0][-1])*len(self.x[0])))
110  else:
111  plot_untill = int(np.ceil(5000/(self.x[-1])*len(self.x)))
112 
113  for i in range(len(self.pad_position)):
114  plt.plot(self.x[i][:plot_untill], self.pad_position[i][:plot_untill], label = self.label_names[i], linewidth =self.linewidth)
115 
116  #plt.ylim(-0.42,0.45)
117 
118  plt.ylabel("$y$")
119  plt.xlabel("$t$")
120  plt.legend(loc = 1)
121  plt.show()
122 
123  def plot_pad_velocity(self):
124 
125  if(len(self.x.shape) > 1):
126  plot_untill = int(np.ceil(self.time_steps_plotted/(self.x[0][-1])*len(self.x[0])))
127  else:
128  plot_untill = int(np.ceil(5000/(self.x[-1])*len(self.x)))
129 
130  for i in range(len(self.pad_position)):
131  plt.plot(self.x[i][:plot_untill], self.pad_velocity[i][:plot_untill], label = self.label_names[i], linewidth =self.linewidth -0.2)
132 
133  plt.xlabel("$t$")
134  plt.ylabel("$y$")
135  plt.legend()
136  plt.show()
137 
139  print("hi")
140 
141 
142 
143 def plotastic():
144  if(len(sys.argv) > 2):
145  run_name = sys.argv[1:]
146  plot_pad = PlotPads(run_name)
147  elif(len(sys.argv) == 2):
148  plot_pad = PlotPads([sys.argv[1]])
149  else:
150  print("This will probably not work, have you remembered to give input to the command?")
151  plot_pad = PlotPads()
152  plot_pad.plot_pad_position()
153  plot_pad.plot_pad_velocity()
154 
155 if __name__ == "__main__":
156  plotastic()
def x_values_for_run(self, run_name, number_data_points=0)
Definition: plotPadList.py:79
def loadYaml(self, run_name)
Definition: plotPadList.py:33
def plotastic()
Definition: plotPadList.py:143
def __init__(self, run_name=[""], numeric_method="_midpoint_", time_steps_plotted=300000)
Definition: plotPadList.py:25
def setUpPlot(self, run_name)
Definition: plotPadList.py:95
def getPadLabels(self, run_name, use_dictionary_names=False)
Definition: plotPadList.py:62
def plot_pad_position_different_time_step(self)
Definition: plotPadList.py:138
def loadFiles(self, run_name, numeric_method)
Definition: plotPadList.py:39
def plot_pad_velocity(self)
Definition: plotPadList.py:123
def plot_pad_position(self)
Definition: plotPadList.py:106