from CBD.simulator import Simulator
from SinGen import *

from CBD.preprocessing.butcher import ButcherTableau as BT
from CBD.preprocessing.rungekutta import RKPreprocessor

sinGen = SinGen("SinGen")
sinGen.addFixedRateClock("clock", 1e-4)
tableau = BT.RKF45()
RKP = RKPreprocessor(tableau, atol=2e-5, hmin=1e-4, hmax=0.01, safety=.84)
newModel = RKP.preprocess(sinGen)

sim_time = 21.0
sim = Simulator(newModel)
sim.run(sim_time)

import matplotlib.pyplot as plt
import numpy as np

data = newModel.getSignalHistory('OUT1')
t, v = [t for t, _ in data], [v for _, v in data]

fig = plt.figure()
ax = fig.subplots()
ax.set_xticks(np.arange(0, 22.5, 2.5))
ax.set_yticks(np.arange(-1, 1.1, 0.25))
ax.plot(t, v, '-')
ax.plot(t, v, 'o', color='red', mfc='none')
plt.grid()
plt.show()
# plt.savefig("sin-rkf.png")
