-
Notifications
You must be signed in to change notification settings - Fork 0
/
mult-var.py
156 lines (127 loc) · 3.88 KB
/
mult-var.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
import json
import random as rd
from fh2o_module import li_dawes_guo as ldg
from optimization import cbpd
from optimization import result
from optimization import scipy as scp
from geometries import *
ANG_RANGE = 10
BOND_RANGE = 0.3
MAX_ITERATIONS = 50
TOLERANCE = 1e-5
DIVERGE_VAL = 115.302739153
CASES_PER_GEOMETRY = 100
ldg.init()
def gen_random_conv_case(geometry):
point = geometry["stationary"][:] # Copy array
relevant_vars = geometry["relevant_vars"]
specie = geometry["specie"]
for var_idx, is_relevant in enumerate(relevant_vars):
if not is_relevant:
continue
var_range = BOND_RANGE
if var_idx > 2: # Means its an angle variation
var_range = ANG_RANGE
p = rd.uniform(-var_range, var_range)
point[var_idx] += p
return {
"point": point,
"variation": -1,
"variable": "random",
"specie": specie,
"relevant_vars": relevant_vars,
}
def try_converge_cbpd(case):
p = case["point"]
v = case["variation"]
var = case["variable"]
specie = case["specie"]
print("[CBPD] - Trying converge init point: {} - ".format(p), end='')
f = cbpd.FunctionCBPD(ldg.pes, p)
result = f.converge_numerically(
tolerance = TOLERANCE,
max_iterations = MAX_ITERATIONS,
)
if result.final_value == DIVERGE_VAL:
result.converge = False
if result.converge:
print("Success")
else:
print("Fail")
result.specie = specie
result.variable = var
result.variation = v
return result
def try_converge_newton(case):
p = case["point"]
v = case["variation"]
var = case["variable"]
specie = case["specie"]
relevant_vars = case["relevant_vars"]
print("[Newton] - Trying converge init point: {} - ".format(p), end='')
f = scp.FunctionScipy(ldg.pes, p, relevant_vars)
result = f.converge_newton(
tolerance = TOLERANCE,
max_iterations = MAX_ITERATIONS,
)
if result.final_value == DIVERGE_VAL:
result.converge = False
if result.converge:
print("Success")
else:
print("Fail")
result.specie = specie
result.variable = var
result.variation = v
return result
# Gen all cases
all_cases = []
for geo in geometries:
specie = geo['specie']
energy = geo['energy']
cases = []
for i in range(CASES_PER_GEOMETRY):
c = gen_random_conv_case(geo)
cases.append(c)
all_cases.append({
"cases": cases,
"energy": energy,
"specie": specie
})
# CBPD
results_cbpd = result.Results()
for data in all_cases:
partial_results = result.Results()
print("\n--------------\n")
print("\tCBPD \t" + data["specie"])
print("\n--------------\n")
for c in data["cases"]:
r = try_converge_cbpd(c)
partial_results.add_single_result(r)
partial_results.normalize_final_points(data["energy"])
results_cbpd.add_multiple_results(partial_results.results)
results_cbpd.csv('results/mult_var_cbpd.csv')
# Newton
results_newton = result.Results()
for data in all_cases:
partial_results = result.Results()
print("\n--------------\n")
print("\tNewton \t" + data["specie"])
print("\n--------------\n")
for c in data["cases"]:
r = try_converge_newton(c)
partial_results.add_single_result(r)
partial_results.normalize_final_points(data["energy"])
results_newton.add_multiple_results(partial_results.results)
results_newton.csv('results/mult_var_newton.csv')
# Parse results
parsed_cbpd = results_cbpd.get_results_metrics()
parsed_newton = results_newton.get_results_metrics()
print("\n--------------\n")
print("\tCBPD")
print("\n--------------\n")
print(json.dumps(parsed_cbpd, indent = 2))
print("\n--------------\n")
print("\tNewton")
print("\n--------------\n")
print(json.dumps(parsed_newton, indent = 2))