Skip to content

Commit 8f3e009

Browse files
committed
Added a new fix/improvement in the cell minimization algorithm
1 parent d0580fc commit 8f3e009

1 file changed

Lines changed: 23 additions & 3 deletions

File tree

Modules/Optimizer.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ def __init__(self, starting_unit_cell):
4646
self.uc_0_inv = np.linalg.inv(self.uc_0)
4747
self.x_start = None
4848
self.reset_strain = False
49+
50+
# A control parameter for the line minimization (avoid failures)
51+
self.last_factor = 0
4952

5053
self.uc_old = None
5154

@@ -105,7 +108,8 @@ def get_line_step(self, grad):
105108
sys.stdout.flush()
106109

107110
print("[CELL] GRADIENT DOT DIRECTION = {}".format(factor))
108-
111+
failure_lmin = False
112+
109113
sys.stdout.flush()
110114
# Regularization (avoid run away)
111115
if factor > 2:
@@ -119,20 +123,36 @@ def get_line_step(self, grad):
119123
print(" incrementing by a 15 %")
120124

121125
elif factor < self.min_alpha_factor:
126+
# Check if the last time was good
127+
if self.last_factor != 0:
128+
if factor < self.last_factor:
129+
print("[CELL] Warning: reduced step and got worse; restarting line minimization with a good step.")
130+
failure_lmin = True
131+
self.last_factor = factor
122132
factor = self.min_alpha_factor
123133
good_step = False
124134

125135
if np.isnan(factor):
136+
self.last_factor = 0
126137
factor = self.min_alpha_factor
127138
good_step = False
128-
129-
self.alpha *= factor
139+
140+
# Check if the line minimization got contradditing result
141+
# In this case stop the line minimization and proceed with a standard gradient descend.
142+
if failure_lmin:
143+
good_step = True
144+
else:
145+
self.alpha *= factor
130146

131147

132148
#if self.alpha < self.min_alpha_step * self.alpha0:
133149
# self.alpha = self.min_alpha_step * self.alpha0
134150
#self.alpha *= factor
135151

152+
# Reset the check on the line minimization if this is a good step
153+
if good_step:
154+
self.last_factor = 0
155+
136156
return good_step
137157

138158
def reset(self, unit_cell):

0 commit comments

Comments
 (0)