|
|
@@ -43,7 +43,7 @@ class SinBlock(CBD):
|
|
|
|
|
|
class HarmonicA(CBD):
|
|
|
def __init__(self, block_name):
|
|
|
- CBD.__init__(self, block_name, output_ports=['x'])
|
|
|
+ CBD.__init__(self, block_name, output_ports=['x', 'v'])
|
|
|
|
|
|
# Create the blocks
|
|
|
self.addBlock(IntegratorBlock(block_name='int2'))
|
|
|
@@ -59,6 +59,7 @@ class HarmonicA(CBD):
|
|
|
self.addConnection('int2', 'neg')
|
|
|
self.addConnection('neg', 'int1')
|
|
|
self.addConnection('int1', 'int2')
|
|
|
+ self.addConnection('int1', 'v')
|
|
|
|
|
|
|
|
|
class ErrorA(CBD):
|
|
|
@@ -133,47 +134,65 @@ class ErrorB(CBD):
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
from pyCBD.converters.latexify import CBD2Latex
|
|
|
+ import numpy as np
|
|
|
|
|
|
errors = []
|
|
|
signals = []
|
|
|
- for dt in [0.1]:
|
|
|
+ dts = reversed([0.5, 0.1, 0.01])
|
|
|
+ TIME = 8.5
|
|
|
+ shapes = ['.', 'x', '+']
|
|
|
+ print(" starting plotter")
|
|
|
+ plt.figure(figsize=(5, 5))
|
|
|
+ plt.xlabel('x')
|
|
|
+ plt.ylabel('dx/dt')
|
|
|
+ plt.xlim((-4, 4))
|
|
|
+ plt.ylim((-4, 4))
|
|
|
+ for i, dt in enumerate(dts):
|
|
|
print("DT:", dt)
|
|
|
signals.append(str(dt))
|
|
|
|
|
|
outputs = []
|
|
|
- for cbd in [ErrorA("ErrorA"), ErrorB("ErrorB")]:
|
|
|
- cbd2latex = CBD2Latex(cbd, show_steps=True, render_latex=False)
|
|
|
+ for cbd in [HarmonicA("DTHO")]:
|
|
|
+ cbd2latex = CBD2Latex(cbd, show_steps=True, render_latex=True, delta_t="", time_variable='j')
|
|
|
cbd2latex.simplify()
|
|
|
+ raise 4
|
|
|
|
|
|
# Run the simulation
|
|
|
sim = Simulator(cbd)
|
|
|
sim.setDeltaT(dt)
|
|
|
- sim.run(100.0)
|
|
|
- errors.append(cbd.getSignalHistory("e"))
|
|
|
- outputs.append(cbd.getSignalHistory("out"))
|
|
|
- real = cbd.getSignalHistory('real')
|
|
|
-
|
|
|
- print(" starting plotter")
|
|
|
- plt.figure()
|
|
|
- plt.title(f"Integrator vs Derivator (delta = {dt})")
|
|
|
- plt.xlabel('time')
|
|
|
+ sim.run(TIME)
|
|
|
+ # errors.append(cbd.getSignalHistory("e"))
|
|
|
+ outputs.append(cbd.getSignalHistory("x"))
|
|
|
+ outputs.append(cbd.getSignalHistory("v"))
|
|
|
+ # real = cbd.getSignalHistory('real')
|
|
|
+
|
|
|
+
|
|
|
+ # plt.title(f"Integrator vs Derivator (delta = {dt})")
|
|
|
+
|
|
|
A, B = outputs
|
|
|
- plt.plot([t for t, _ in real], [x for _, x in real], label="actual")
|
|
|
- plt.plot([t for t, _ in A], [x for _, x in A], label="integrators")
|
|
|
- plt.plot([t for t, _ in B], [x for _, x in B], label="derivators")
|
|
|
- plt.legend()
|
|
|
- plt.show()
|
|
|
+ plt.plot([x for _, x in A], [x for _, x in B], shapes[i], label=f"{dt}")
|
|
|
+ # plt.plot([t for t, _ in B], [x for _, x in B], label="derivators")
|
|
|
|
|
|
- plt.figure()
|
|
|
- plt.title("Errors")
|
|
|
- plt.xlabel('time')
|
|
|
- plt.ylabel('N')
|
|
|
- for i in range(2):
|
|
|
- for j in range(2):
|
|
|
- time = [x for x, _ in errors[(i*2)+j]]
|
|
|
- value = [x for _, x in errors[(i*2)+j]]
|
|
|
- plt.plot(time, value, label=("delta = " + signals[i] + "; " + "AB"[j]))
|
|
|
- plt.yscale("log")
|
|
|
- plt.legend()
|
|
|
+ # ts = np.arange(0, 10, 0.01)
|
|
|
+ # plt.plot(ts, np.sin(ts), label="sine")
|
|
|
+
|
|
|
+ times = np.arange(0, TIME, 0.01)
|
|
|
+ plt.plot(np.sin(times), np.cos(times), label="analytical", c='r', lw=1)
|
|
|
+
|
|
|
+ plt.tight_layout()
|
|
|
+ plt.legend(loc='upper right')
|
|
|
plt.show()
|
|
|
|
|
|
+ # plt.figure()
|
|
|
+ # plt.title("Errors")
|
|
|
+ # plt.xlabel('time')
|
|
|
+ # plt.ylabel('N')
|
|
|
+ # for i in range(len(dts)):
|
|
|
+ # for j in range(len(dts)):
|
|
|
+ # time = [x for x, _ in errors[(i*2)+j]]
|
|
|
+ # value = [x for _, x in errors[(i*2)+j]]
|
|
|
+ # plt.plot(time, value, label=("delta = " + signals[i] + "; " + "AB"[j]))
|
|
|
+ # plt.yscale("log")
|
|
|
+ # plt.legend()
|
|
|
+ # plt.show()
|
|
|
+
|