#!/usr/bin/python3
# This file was automatically generated from drawio2cbd with the command:
#   /home/red/git/DrawioConvert/__main__.py Test.drawio -e Test -Ssfargv -F CBD -E delta=0.1

from Test import *
import numpy as np
from pyCBD.simulator import Simulator

DELTA_T = 0.1

cbd = Test("Test")

from pyCBD.preprocessing.butcher import ButcherTableau as BT
from pyCBD.preprocessing.rungekutta import RKPreprocessor
cbd.addFixedRateClock("clock", DELTA_T)
tableau = BT.RKF45()
RKP = RKPreprocessor(tableau, atol=2e-5, hmin=0.01, safety=.84)
transformed = RKP.preprocess(cbd)
sim = Simulator(transformed)
sim.run(1.4)

# Run the Simulation
# sim = Simulator(cbd)
# sim.setDeltaT(DELTA_T)
# sim.run(1.4)

s = transformed.getSignalHistory("y")
L = len(s)

import matplotlib.pyplot as plt

plt.plot([t for t, _ in s], np.tan([t for t, _ in s]), color="green")
plt.plot([t for t, _ in s], [x for _, x in s], color="blue")
plt.plot([t for t, _ in s], [x for _, x in s], 'o', fillstyle='none', color="red")
plt.show()

T = [t for t, _ in s]
DT = [T[i + 1] - T[i] for i in range(len(T) - 1)]
print(min(DT), max(DT))

print("+------------+------------+------------+------------+")
print("|    TIME    |    VALUE   |    TAN T   |    ERROR   |")
print("+------------+------------+------------+------------+")
for i in range(L):
	t, v = s[i]
	a = np.tan(t)
	e = a - v
	print(f"| {t:10.7f} | {v:10.7f} | {a:10.7f} | {e:10.7f} |")
print("+------------+------------+------------+------------+")
