-
Notifications
You must be signed in to change notification settings - Fork 3
/
qde_plots.py
163 lines (118 loc) · 7.58 KB
/
qde_plots.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import addcopyfighandler
import findiff
from plots_general import my_plot
from test_core import *
addcopyfighandler.dummy_use = 1
def plot_potential_harmonic(**kwargs):
equilibrium, force = (Hydrogen.equilibrium, Hydrogen.force_const)
grid = np.linspace(-0.2, 0.2, 100) + equilibrium
pot = force * (grid - equilibrium) ** 2
axes = my_plot(grid, pot / Constants.eh_per_cm_1, **kwargs)
axes.set_xlabel(r'$\mathrm{H-H\ dist, a_0}$')
axes.set_ylabel(r'$\mathrm{Energy, cm^{-1}}$')
return axes
def plot_potential_morse(**kwargs):
grid = np.linspace(-0.7, 9, 1000) + r0
pot = Hydrogen.get_potential_morse(grid)
axes = my_plot(grid, pot / Constants.eh_per_cm_1, **kwargs)
axes.set_xlabel(r'$\mathrm{r, a_0}$')
axes.set_ylabel(r'$\mathrm{Energy, cm^{-1}}$')
return axes
def plot_force_morse(**kwargs):
grid = np.linspace(-0.7, 9, 1000) + re
force = Hydrogen.get_force_morse(grid)
axes = my_plot(grid, force / Constants.eh_per_cm_1, **kwargs)
axes.set_xlabel(r'$\mathrm{r, a_0}$')
axes.set_ylabel(r'$\mathrm{Force, cm^{-1} / a_0}$')
axes.set_ylim(bottom=-20000, top=20000)
return axes
def plot_solution_tr(t, r, **kwargs):
axes = my_plot(t, r, **kwargs)
axes.set_xlabel('t, a.u.')
axes.set_ylabel('r, Bohr')
return axes
def plot_solution_rp(r, p, **kwargs):
axes = my_plot(r, p, **kwargs)
axes.set_xlabel('r, Bohr')
axes.set_ylabel('p, a.u.')
return axes
def plot_solution_rp_tr(t, r, **kwargs):
dt = t[1] - t[0]
d_dt = findiff.FinDiff(0, dt)
p = Hydrogen.mu * d_dt(r)
return plot_solution_rp(r, p, **kwargs)
def plot_solution_rp_file(file_path='solution.txt', **kwargs):
solution = np.loadtxt(file_path)
return plot_solution_rp(solution[0, :], solution[1, :], **kwargs)
def plot_error(solution_n, true_solution_n, Ns, **kwargs):
"""Plots error at given values of Ns. solution_n and answer_n are function of n."""
plot_data = np.empty((2, len(Ns)))
for i in range(len(Ns)):
N = Ns[i]
solution = solution_n(N)
true_solution = true_solution_n(N)
# error = abs((solution[-1] - true_solution[-1]) / true_solution[-1]) * 100
error = np.sqrt(sum((solution - true_solution) ** 2) / len(solution))
# error = max(abs(solution - true_solution))
plot_data[:, i] = (N, error)
axes = my_plot(plot_data[0, :], plot_data[1, :], log=True, **kwargs)
axes.set_xlabel('M')
axes.set_ylabel('RMSE, Bohr')
return axes
def plot_all_errors_vs_n_eq_1():
"""Plots errors as a function of N for multiple approaches, 1 equation per step."""
Ns = np.geomspace(10, 1000, 5, dtype=int)
analytical_solution_n = lambda n: get_analytical_solution(problem_id=0, N=n, time_max=400, initial_position=1.3)[1]
solution_n = lambda n: np.loadtxt(f'../results/qp/eq_1/N_{n}/solution.txt')[0, :]
axes = plot_error(solution_n, analytical_solution_n, Ns, label='QP')
solution_n = lambda n: np.loadtxt(f'../results/qbsolv/eq_1/attempts_1/kd_15/N_{n}/solution.txt')[0, :]
axes = plot_error(solution_n, analytical_solution_n, Ns, axes=axes, label='QBSolv')
solution_n = lambda n: np.loadtxt(f'../results/dwave/eq_1/attempts_1/N_{n}/solution.txt')[0, :]
axes = plot_error(solution_n, analytical_solution_n, Ns, axes=axes, label='DWave 1')
# solution_n = lambda n: np.loadtxt(f'../results/dwave/eq_1/attempts_5/initial_1.3/N_{n}/solution.txt')[0, :]
# axes = plot_error(solution_n, analytical_solution_n, Ns, axes=axes, label='Dwave 5')
solution_n = lambda n: np.loadtxt(f'../results/dwave/eq_1/attempts_10/N_{n}/solution.txt')[0, :]
axes = plot_error(solution_n, analytical_solution_n, Ns, axes=axes, label='Dwave 10')
def plot_all_errors_vs_n_eq_2():
"""Plots errors as a function of N for multiple approaches, 2 equations per step."""
Ns = np.geomspace(10, 1000, 5, dtype=int)
analytical_solution_n = lambda n: get_analytical_solution(problem_id=0, N=n, time_max=400, initial_position=1.3)[1]
solution_n = lambda n: np.loadtxt(f'../results/qp/eq_2/N_{n}/solution.txt')[0, :]
axes = plot_error(solution_n, analytical_solution_n, Ns, label='QP')
solution_n = lambda n: np.loadtxt(f'../results/qbsolv/eq_2/attempts_1/N_{n}/solution.txt')[0, :]
axes = plot_error(solution_n, analytical_solution_n, Ns, axes=axes, label='QBSolv 1')
# solution_n = lambda n: np.loadtxt(f'../results/qbsolv/eq_2/attempts_1/greedy/N_{n}/solution.txt')[0, :]
# axes = plot_error(solution_n, analytical_solution_n, Ns, axes=axes, label='QBSolv 1 greedy')
solution_n = lambda n: np.loadtxt(f'../results/qbsolv/eq_2/attempts_1/scaled/N_{n}/solution.txt')[0, :]
axes = plot_error(solution_n, analytical_solution_n, Ns, axes=axes, label='QBSolv 1 scaled')
solution_n = lambda n: np.loadtxt(f'../results/qbsolv/eq_2/attempts_10/N_{n}/solution.txt')[0, :]
axes = plot_error(solution_n, analytical_solution_n, Ns, axes=axes, label='QBSolv 10')
solution_n = lambda n: np.loadtxt(f'../results/qbsolv/eq_2/attempts_10/scaled/N_{n}/solution.txt')[0, :]
axes = plot_error(solution_n, analytical_solution_n, Ns, axes=axes, label='QBSolv 10 scaled')
# solution_n = lambda n: np.loadtxt(f'../results/dwave/eq_2/attempts_1/N_{n}/solution.txt')[0, :]
# axes = plot_error(solution_n, analytical_solution_n, Ns, axes=axes, label='DWave')
# solution_n = lambda n: np.loadtxt(f'../results/dwave/eq_2/attempts_5/N_{n}/solution.txt')[0, :]
# axes = plot_error(solution_n, analytical_solution_n, Ns, axes=axes, label='DWave 5')
solution_n = lambda n: np.loadtxt(f'../results/dwave/eq_2/attempts_10/at_20/N_{n}/solution.txt')[0, :]
axes = plot_error(solution_n, analytical_solution_n, Ns, axes=axes, label='DWave 10')
# solution_n = lambda n: np.loadtxt(f'../results/dwave/eq_2/attempts_1/at_200/chain/N_{n}/solution.txt')[0, :]
# axes = plot_error(solution_n, analytical_solution_n, Ns, axes=axes, label='DWave+ 1')
#
# solution_n = lambda n: np.loadtxt(f'../results/dwave/eq_2/attempts_5/at_200/N_{n}/solution.txt')[0, :]
# axes = plot_error(solution_n, analytical_solution_n, Ns, axes=axes, label='DWave+ 5')
solution_n = lambda n: np.loadtxt(f'../results/dwave/eq_2/attempts_1/greedy/N_{n}/solution.txt')[0, :]
axes = plot_error(solution_n, analytical_solution_n, Ns, axes=axes, label='DWave 1 + greedy', color='m')
def plot_trajectories():
"""Plots a few representative trajectories."""
grid, analytical_solution = get_analytical_solution(problem_id=0, N=1000, time_max=400, initial_position=1.3)
axes = plot_solution_rp_tr(grid, analytical_solution, axes=None, marker='None', color='b', label='Analytical')
axes = plot_solution_rp_file('../results/dwave/eq_1/attempts_1/N_1000/solution.txt', axes=axes, marker='None', color='g', label='DWave 1')
axes = plot_solution_rp_file('../results/dwave/eq_1/attempts_10/N_1000/solution.txt', axes=axes, marker='None', color='k', label='DWave 10')
axes = plot_solution_rp_file('../results/dwave/eq_2/attempts_1/greedy/N_1000/solution.txt', axes=axes, marker='None', color='m', label='DWave 1 + greedy')
def main():
solution = get_solution(problem_id=0, N=50, time_max=400, initial_position=1.3, points_per_step=1, equations_per_step=2, max_attempts=1, max_error=1e-10, method='qp')[1]
axes = plot_solution_rp(solution[0, :], solution[1, :], axes=None, marker='None')
solution = get_solution(problem_id=0, N=50, time_max=400, initial_position=1.3, points_per_step=1, equations_per_step=2, max_attempts=1, max_error=1e-10, method='qbsolv')[1]
axes = plot_solution_rp(solution[0, :], solution[1, :], axes=axes, marker='None')
if __name__ == '__main__':
main()