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 import numpy as np
5 
6 from analysisSharedFunctions import loadYaml, loadFile
7 from plotSetUp import setSvgLatexMatplotlibSettings, \
8  colors_qualitative_short as colors_short, \
9  linestyles
10 
11 
12 class CheckEnergy:
13  def __init__(
14  self,
15  run_name="",
16  comparison_name="",
17  numeric_method="_midpoint_"
18  ):
19  self.loadFiles(run_name, numeric_method)
20  self.setUpPlot()
22 
24 
25  if(comparison_name == ""):
26  self.singleResult()
27  else:
28  self.comparisonResult(comparison_name, numeric_method)
29 
31  print("Can only run test 1 if no fiction, neighbor springs, \
32  damper is activated and the pad behaves as it's fastened to the roof surface. ")
33  print("Can only run test 2 if no friction, stationary springs \
34  damper is activated and the pad behaves as it's fastened to the roof surface. ")
35  quit()
36 
37  def singleResult(self):
38  if(self.only_pulling_springs):
39  print("Only pullings springs in input.")
41  elif(self.only_neighbor_springs):
42  print("Only neighbor springs in input.")
44  else:
45  self.testTerminalMessage()
46 
47  self.plotEnergyTest(energy, zoom=False)
48  self.plotEnergyTest(energy)
49 
50  def comparisonResult(
51  self,
52  comparison_name,
53  numeric_method
54  ):
55  self.loadComparisonFiles(comparison_name, numeric_method)
56  self.setUpPlotComparison()
57  if(self.only_pulling_springs):
60  elif(self.only_neighbor_springs):
63  else:
64  self.testTerminalMessage()
65 
66  self.plotEnergyTestComparison(energy, energy_comparison, zoom=False)
67  self.plotEnergyTestComparison(energy, energy_comparison)
68 
69  def loadFiles(self, run_name, numeric_method):
70  self.results_path = os.path.dirname(os.path.realpath(__file__)) + "/../../../results/"
71 
72  self.run_parameters = loadYaml(run_name)
73 
74  self.block_position = loadFile(run_name, self.run_parameters, "block_position")
75  self.block_velocity = loadFile(run_name, self.run_parameters, "block_velocity")
76 
77  def loadComparisonFiles(self, comparison_name, numeric_method):
78  print("Remember, this code assumes that the only difference between the two runs is the stepsize.")
79 
80  self.block_position_comparison = loadFile(comparison_name, self.run_parameters, "block_position")
81  self.block_velocity_comparison = loadFile(comparison_name, self.run_parameters, "block_velocity")
82 
83  def setUpPlot(self):
84 
85  start = 0
86  stop = self.run_parameters["Parameters"]["max_time"]
87 
88  max_time = self.run_parameters["Parameters"]["max_time"]
89 
90  if(len(self.block_position.shape) == 1):
91  self.one_block_boolean = True
92  self.x = np.linspace(start=start, stop=stop, num=len(self.block_position))
93 
94  self.step_size = max_time/len(self.block_position)
95  else:
96  self.x = np.linspace(start=start, stop=stop, num=len(self.block_position[0]))
97  self.step_size = max_time/len(self.block_position[0])
98 
100  start = 0
101  stop = self.run_parameters["Parameters"]["max_time"]
102 
103  max_time = self.run_parameters["Parameters"]["max_time"]
104 
105  if(len(self.block_position_comparison.shape) == 1):
107  self.x_comparison = np.linspace(start=start, stop=stop, num=len(self.block_position_comparison))
108  self.step_size_comparison = max_time/len(self.block_position_comparison)
109  else:
110  self.x_comparison = np.linspace(start=start, stop=stop, num=len(self.block_position_comparison[0]))
111  self.step_size_comparison = max_time/len(self.block_position_comparison[0])
112  print(max_time, len(self.block_position_comparison[0]), self.step_size_comparison)
113 
114 
118 
120  no_friction = self.run_parameters["Debug"]["debug_no_friction"]
121  no_neighbor_springs = self.run_parameters["Debug"]["debug_no_neighbor_springs"]
122  no_pad = self.run_parameters["Debug"]["debug_no_pad"]
123 
124  self.only_pulling_springs = all(value == True for value in \
125  [no_friction, no_neighbor_springs, no_pad])
126 
128  no_friction = self.run_parameters["Debug"]["debug_no_friction"]
129  no_stationary_springs = self.run_parameters["Debug"]["debug_no_stationary_springs"]
130  no_damper = self.run_parameters["Debug"]["debug_no_damper"]
131  no_pad = self.run_parameters["Debug"]["debug_no_pad"]
132  if(no_stationary_springs and no_damper and not no_pad):
133  no_pad = True
134 
135  self.only_neighbor_springs = all(value == True for value in \
136  [no_friction, no_stationary_springs, no_pad])
137 
138  def onlyPullingSpringsEnergyTest(self, block_velocity, block_position):
139 
140  N = self.run_parameters["Parameters"]["N"]
141  m_scale_mass = self.run_parameters["Parameters"]["m_scale_mass"]
142  m_mass_x = self.run_parameters["Parameters"]["m_mass_x"]
143  m_scale_P = self.run_parameters["Parameters"]["m_scale_P"]
144  m_k_P0 = self.run_parameters["Parameters"]["m_k_P0"]
145 
146  m_u = m_scale_mass*m_mass_x/N
147  k_p = m_scale_P*m_k_P0/N
148 
149  energy_only_pulling_springs_test = 1/2 * m_u * \
150  np.sum(np.power(block_velocity, 2), axis=0) + \
151  1/2 * k_p * np.sum(np.power(block_position, 2), axis=0)
152  return(energy_only_pulling_springs_test)
153 
154  def onlyNeighborSpringsEnergyTest(self, block_velocity, block_position):
155  N = self.run_parameters["Parameters"]["N"]
156  m_scale_mass = self.run_parameters["Parameters"]["m_scale_mass"]
157  m_mass_x = self.run_parameters["Parameters"]["m_mass_x"]
158  m_scale_C = self.run_parameters["Parameters"]["m_scale_C"]
159  m_k_P0 = self.run_parameters["Parameters"]["m_k_P0"]
160 
161  m_u = m_scale_mass*m_mass_x/N
162  k_c = m_scale_C*m_k_P0*N
163 
164  last_energy_ledd = block_position[1:,] - block_position[:-1,]
165 
166  energy_only_neighbor_springs_test = 1/2 * m_u * np.sum(np.power(block_velocity, 2), axis=0) + \
167  1/2 * k_c * np.sum(np.power(last_energy_ledd, 2), axis=0)
168 
169  return(energy_only_neighbor_springs_test)
170 
171  def calculateError(
172  self,
173  energy_array
174  ):
175  return energy_array[-1] - energy_array[0]
176 
177  def compareError(
178  self,
179  energy_test_result,
180  energy_test_result_comparison
181  ):
182  error = self.calculateError(energy_test_result)
183  error_comparison = self.calculateError(energy_test_result_comparison)
184 
185  ratio = error/error_comparison
186 
187  print(error, error_comparison, ratio)
188 
189  def plotEnergyTest(self, energy_test_result, zoom=True):
190  plt.plot(self.x, energy_test_result, label="Energy",
191  color=colors_short[0],
192  linestyle=linestyles[0])
193 
194  if(not zoom):
195  plt.ylim(0, np.max(energy_test_result)*(1.10))
196 
197  plt.ylabel("\$E\$")
198  plt.xlabel("\$t\$")
199  plt.legend()
200  plt.show()
201 
203  self,
204  energy_test_result,
205  energy_test_result_comparison,
206  zoom=True
207  ):
208  self.compareError(energy_test_result, energy_test_result_comparison)
209  plt.plot(self.x, energy_test_result,
210  label=str(self.step_size),
211  color=colors_short[2],
212  linestyle=linestyles[2])
213  plt.plot(self.x_comparison, energy_test_result_comparison,
214  label=str(self.step_size_comparison),
215  color=colors_short[1],
216  linestyle=linestyles[1])
217 
218  if(not zoom):
219  plt.ylim(0, np.max([np.max(energy_test_result), np.max(energy_test_result_comparison)])*(1.10))
220 
221  plt.ylabel("\$E\$")
222  plt.xlabel("\$t\$")
223  plt.legend(title="\$dt\$")
224  plt.show()
225 
226 
227 def run():
228  if(len(sys.argv) > 2):
229  checkEnergy = CheckEnergy(sys.argv[1], sys.argv[2])
230  elif(len(sys.argv) == 2):
231  checkEnergy = CheckEnergy(sys.argv[1])
232  else:
233  checkEnergy = CheckEnergy()
234 
235 
236 if __name__ == "__main__":
237  run()
def checkOnlyNeighborEnergyTest(self)
Definition: checkEnergy.py:140
def onlyNeighborSpringsEnergyTest(self, block_velocity, block_position)
Definition: checkEnergy.py:164
def testTerminalMessage(self)
Definition: checkEnergy.py:30
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 compareError(self, energy_test_result, energy_test_result_comparison)
Definition: checkEnergy.py:181
def checkOnlyPullingEnergyTest(self)
Definition: checkEnergy.py:132
def loadFiles(self, run_name, numeric_method)
Definition: checkEnergy.py:74
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 loadFile(run_name, shared_names, selected='pad_position.csv')
def calculateError(self, energy_array)
Definition: checkEnergy.py:174
def setSvgLatexMatplotlibSettings(pyplot)
Definition: plotSetUp.py:11