My Project
checkEnergy.py
Go to the documentation of this file.
1 import os
2 import sys
3 from matplotlib import pyplot as plt
4 
5 from matplotlib2tikz import save as tikz_save
6 
7 import matplotlib as mpl
8 
9 mpl.rcParams['svg.fonttype'] = "none"
10 mpl.rcParams['font.size'] = 16
11 
12 
13 mpl.rc('font', **{'family': 'serif', 'serif': ['Computer Modern']})
14 mpl.rc('text', usetex=True)
15 
16 import numpy as np
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 
26  def __init__(self, run_name = "", comparison_name = "", numeric_method = "_midpoint_"):
27  self.loadFiles(run_name, numeric_method)
28  self.setUpPlot()
30 
31  if(comparison_name == ""):
32  self.singleResult()
33  else:
34  self.comparisonResult(comparison_name, numeric_method)
35 
36  def singleResult(self):
37  if(self.only_pulling_springs):
38  print("Only pullings springs in input.")
40  elif(self.only_neighbor_springs):
41  print("Only neighbor springs in input.")
43  else:
44  print("Can only run test 1 if no fiction, neighbor springs, \
45  damper is activated and the pad behaves as it's fastened to the roof surface. ")
46  print("Can only run test 2 if no friction, stationary springs \
47  damper is activated and the pad behaves as it's fastened to the roof surface. ")
48  quit()
49 
50  self.plotEnergyTest(energy, zoom = False)
51  self.plotEnergyTest(energy)
52 
53 
54 
55  def comparisonResult(self, comparison_name, numeric_method):
56  self.loadComparisonFiles(comparison_name, numeric_method)
57  self.setUpPlotComparison()
58  if(self.only_pulling_springs):
61  elif(self.only_neighbor_springs):
64  else:
65  print("Can only run test 1 if no fiction, neighbor springs, \
66  damper is activated and the pad behaves as it's fastened to the roof surface. ")
67  print("Can only run test 2 if no friction, stationary springs \
68  damper is activated and the pad behaves as it's fastened to the roof surface. ")
69  quit()
70 
71  self.plotEnergyTestComparison(energy, energy_comparison, zoom = False)
72  self.plotEnergyTestComparison(energy, energy_comparison)
73 
74  def loadFiles(self, run_name, numeric_method):
75  self.results_path = os.path.dirname(os.path.realpath(__file__)) + "/../../../results/"
76  results_folder = "/results/"
77 
78  stream = open(self.results_path + run_name + "/doc/yaml/parameters.yaml", 'r')
79  self.run_parameters = yaml.load(stream, Loader = Loader)
80 
81  shared_names = self.run_parameters["Parameters"]["file_name"]
82 
83  self.block_position = np.loadtxt(self.results_path + run_name + results_folder + shared_names + numeric_method + "block_position.csv", delimiter=",")
84  self.block_velocity = np.loadtxt(self.results_path + run_name + results_folder + shared_names + numeric_method + "block_velocity.csv", delimiter=",")
85 
86  def loadComparisonFiles(self, comparison_name, numeric_method):
87  print("Remember, this code assumes that the only difference between the two runs is the stepsize.")
88 
89  results_folder = "/results/"
90 
91  shared_names = self.run_parameters["Parameters"]["file_name"]
92 
93  self.block_position_comparison = np.loadtxt(self.results_path + comparison_name + results_folder + shared_names + numeric_method + "block_position.csv", delimiter=",")
94  self.block_velocity_comparison = np.loadtxt(self.results_path + comparison_name + results_folder + shared_names + numeric_method + "block_velocity.csv", delimiter=",")
95 
96  def setUpPlot(self):
97 
98  start = 0
99  stop = self.run_parameters["Parameters"]["max_time"]
100 
101  max_time = self.run_parameters["Parameters"]["max_time"]
102 
103  if(len(self.block_position.shape) == 1):
104  self.one_block_boolean = True
105  self.x = np.linspace(start = start, stop = stop, num = len(self.block_position))
106 
107  self.step_size = max_time/len(self.block_position)
108  else:
109  self.x = np.linspace(start = start, stop = stop, num = len(self.block_position[0]))
110  self.step_size = max_time/len(self.block_position[0])
111 
112 
114  start = 0
115  stop = self.run_parameters["Parameters"]["max_time"]
116 
117  max_time = self.run_parameters["Parameters"]["max_time"]
118 
119  if(len(self.block_position_comparison.shape) == 1):
121  self.x_comparison = np.linspace(start = start, stop = stop, num = len(self.block_position_comparison))
123  else:
124  self.x_comparison = np.linspace(start = start, stop = stop, num = len(self.block_position_comparison[0]))
125  self.step_size_comparison = max_time/len(self.block_position_comparison[0])
126 
127 
131 
133  no_friction = self.run_parameters["Debug"]["debug_no_friction"]
134  no_neighbor_springs = self.run_parameters["Debug"]["debug_no_neighbor_springs"]
135  no_damper = self.run_parameters["Debug"]["debug_no_damper"]
136  no_pad = self.run_parameters["Debug"]["debug_no_pad"]
137 
138  self.only_pulling_springs = all(value == True for value in [no_friction, no_neighbor_springs, no_damper, no_pad])
139 
141  no_friction = self.run_parameters["Debug"]["debug_no_friction"]
142  no_stationary_springs = self.run_parameters["Debug"]["debug_no_stationary_springs"]
143  no_damper = self.run_parameters["Debug"]["debug_no_damper"]
144  no_pad = self.run_parameters["Debug"]["debug_no_pad"]
145 
146  self.only_neighbor_springs = all(value == True for value in [no_friction, no_stationary_springs, no_damper, no_pad])
147 
148 
149  def onlyPullingSpringsEnergyTest(self, block_velocity, block_position):
150 
151  N = self.run_parameters["Parameters"]["N"]
152  m_scale_mass = self.run_parameters["Parameters"]["m_scale_mass"]
153  m_mass_x = self.run_parameters["Parameters"]["m_mass_x"]
154  m_scale_P = self.run_parameters["Parameters"]["m_scale_P"]
155  m_k_P0 = self.run_parameters["Parameters"]["m_k_P0"]
156 
157  m_u = m_scale_mass*m_mass_x/N
158  k_p = m_scale_P*m_k_P0/N
159 
160  energy_only_pulling_springs_test = 1/2 * m_u * np.sum(np.power(block_velocity, 2), axis=0) + \
161  1/2 * k_p * np.sum(np.power(block_position, 2), axis = 0)
162  return(energy_only_pulling_springs_test)
163 
164  def onlyNeighborSpringsEnergyTest(self, block_velocity, block_position):
165  N = self.run_parameters["Parameters"]["N"]
166  m_scale_mass = self.run_parameters["Parameters"]["m_scale_mass"]
167  m_mass_x = self.run_parameters["Parameters"]["m_mass_x"]
168  m_scale_C = self.run_parameters["Parameters"]["m_scale_C"]
169  m_k_P0 = self.run_parameters["Parameters"]["m_k_P0"]
170 
171  m_u = m_scale_mass*m_mass_x/N
172  k_c = m_scale_C*m_k_P0*N
173 
174  last_energy_ledd = block_position[1:,] - block_position[:-1,]
175 
176  energy_only_neighbor_springs_test = 1/2 * m_u * np.sum(np.power(block_velocity, 2), axis=0) + \
177  1/2 * k_c * np.sum(np.power(last_energy_ledd, 2), axis = 0)
178 
179  return(energy_only_neighbor_springs_test)
180 
181  def plotEnergyTest(self, energy_test_result, zoom = True):
182  plt.plot(self.x, energy_test_result, label = "Energy")
183 
184  if(not zoom):
185  plt.ylim(0, np.max(energy_test_result)*(1.10))
186 
187  plt.ylabel("$E$")
188  plt.xlabel("$t$")
189  plt.legend()
190  plt.show()
191 
192  def plotEnergyTestComparison(self, energy_test_result, energy_test_result_comparison, zoom = True):
193  plt.plot(self.x, energy_test_result, label = str(self.step_size))
194  plt.plot(self.x_comparison, energy_test_result_comparison, label = str(self.step_size_comparison))
195 
196  if(not zoom):
197  plt.ylim(0, np.max([np.max(energy_test_result), np.max(energy_test_result_comparison)])*(1.10))
198 
199  plt.ylabel("$E$")
200  plt.xlabel("$t$")
201  plt.legend()
202  plt.show()
203 
204  def saveAsTikz(self, filename):
205  tikz_save(self.results_path + 'mytikz.tex', figureheight='8cm', figurewidth='8cm')
206 
207 
208 
209 
210 def run():
211  if(len(sys.argv) > 2):
212  checkEnergy = CheckEnergy(sys.argv[1], sys.argv[2])
213  elif(len(sys.argv) == 2):
214  checkEnergy = CheckEnergy(sys.argv[1])
215  else:
216  checkEnergy = CheckEnergy()
217 
218 if __name__ == "__main__":
219  run()
def checkOnlyNeighborEnergyTest(self)
Definition: checkEnergy.py:140
def onlyNeighborSpringsEnergyTest(self, block_velocity, block_position)
Definition: checkEnergy.py:164
def onlyPullingSpringsEnergyTest(self, block_velocity, block_position)
Definition: checkEnergy.py:149
def plotEnergyTestComparison(self, energy_test_result, energy_test_result_comparison, zoom=True)
Definition: checkEnergy.py:192
def checkOnlyPullingEnergyTest(self)
Definition: checkEnergy.py:132
def loadFiles(self, run_name, numeric_method)
Definition: checkEnergy.py:74
def __init__(self, run_name="", comparison_name="", numeric_method="_midpoint_")
Definition: checkEnergy.py:26
def loadComparisonFiles(self, comparison_name, numeric_method)
Definition: checkEnergy.py:86
def checkPossibleEnergytests(self)
Definition: checkEnergy.py:128
def setUpPlotComparison(self)
Definition: checkEnergy.py:113
def plotEnergyTest(self, energy_test_result, zoom=True)
Definition: checkEnergy.py:181
def comparisonResult(self, comparison_name, numeric_method)
Definition: checkEnergy.py:55
def saveAsTikz(self, filename)
Definition: checkEnergy.py:204