integrators_experiment.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/usr/bin/python3
  2. # This file was automatically generated from drawio2cbd with the command:
  3. # C:/Users/randy/Documents/git/DrawioConvert/__main__.py -F CBD -d experiments/Integrators -e G experiments/Integrators/integrators.drawio
  4. from integrators_Integrators import *
  5. from integrators_G import *
  6. from integrators_Stiff import *
  7. from CBD.simulator import Simulator
  8. import matplotlib.pyplot as plt
  9. import numpy as np
  10. def func(t):
  11. return t / (t*t + 1)
  12. def I(x):
  13. return np.log(x*x + 1) / 2
  14. def stiff(t):
  15. return np.exp(-15 * t)
  16. T = 1
  17. times = np.arange(0, T, 0.001)
  18. ys = stiff(times)
  19. #plt.plot(times, ys, label="real")
  20. # ints = {
  21. # "forwards euler": [],
  22. # "backwards euler": [],
  23. # "trapezoid": [],
  24. # "simpson 1/3": [],
  25. # "analytic": 4.60522018
  26. # }
  27. from CBD.preprocessing.butcher import ButcherTableau as BT
  28. from CBD.preprocessing.rungekutta import RKPreprocessor
  29. cbd = Stiff("stiff")
  30. cbd.addFixedRateClock("clock", 0.1)
  31. tableau = BT.MidpointEuler()
  32. RKP = RKPreprocessor(tableau, atol=2e-5, hmin=0.01, safety=.84)
  33. transformed = RKP.preprocess(cbd)
  34. sim = Simulator(transformed)
  35. sim.run(T)
  36. hist = transformed.getSignalHistory("y")
  37. T = [t for t, _ in hist]
  38. plt.plot(T, [x for _, x in hist], c="b")
  39. plt.plot(T, [x for _, x in hist], 'o', fillstyle='none', lw=0.5, c="r")
  40. plt.plot(T, [-0.1 for _ in range(len(hist))], '|', lw=0.5, c="g")
  41. DT = [T[i + 1] - T[i] for i in range(len(T) - 1)]
  42. print(min(DT), max(DT))
  43. # for dt in [0.1, 0.01]:
  44. # print("DT =", dt)
  45. #
  46. # cbd = G("G")
  47. # # cbd = Stiff("stiff")
  48. #
  49. # # Run the Simulation
  50. # sim = Simulator(cbd)
  51. # sim.setDeltaT(dt)
  52. # sim.run(T)
  53. #
  54. # # hist = cbd.getSignalHistory("y")
  55. # # plt.plot([t for t, _ in hist], [x for _, x in hist], label="dt =" + str(dt))
  56. # #
  57. # ints["forwards euler"].append(cbd.getSignalHistory("fw")[-1].value)
  58. # ints["backwards euler"].append(cbd.getSignalHistory("bk")[-1].value)
  59. # ints["trapezoid"].append(cbd.getSignalHistory("trp")[-1].value)
  60. # ints["simpson 1/3"].append(cbd.getSignalHistory("smp")[-1].value)
  61. #
  62. # hist = cbd.getSignalHistory("fw")
  63. # plt.plot([t for t, _ in hist], [x for _, x in hist], '+', label="forwards, dt=" + str(dt))
  64. # # # hist = cbd.getSignalHistory("bk")
  65. # # # plt.plot([t for t, _ in hist], [x for _, x in hist], '.', label="backwards")
  66. # # # hist = cbd.getSignalHistory("trp")
  67. # # # plt.plot([t for t, _ in hist], [x for _, x in hist], 'x', label="trapezoid")
  68. # hist = cbd.getSignalHistory("smp")
  69. # plt.plot([t for t, _ in hist], [x for _, x in hist], ',', label="simpson, dt=" + str(dt))
  70. # # # plt.plot(times, ys, label="real")
  71. # # # plt.legend()
  72. # # # plt.show()
  73. # print("Results:")
  74. # for k, v in ints.items():
  75. # print(k + ":")
  76. # print("\t", v)
  77. #
  78. # plt.legend()
  79. plt.show()