def findzero(self, P1, P2, ind): """Uses secant method to find zero of test function.""" X1, V1 = P1 X2, V2 = P2 Z1 = copy(X1) Z2 = copy(X2) self.C._preTestFunc(X1, V1) T1 = self.func(X1, V1)[ind] # print 'X1 = ', repr(X1) # print 'T1 = ', repr(T1) self.C._preTestFunc(X2, V2) T2 = self.func(X2, V2)[ind] # print 'X2 = ', repr(X2) # print 'T2 = ', repr(T2) Tmax = 10*max(abs(T1),abs(T2)) p = 1 for i in range(self.C.MaxTestIters): if (Tmax < Inf) and (abs(T1-T2) > spacing(1)): r = pow(abs(T1/(T1-T2)),p) if r >= 1: r = 0.5 else: r = 0.5 X = X1 + r*(X2-X1) V = V1 + r*(V2-V1) self.C.Corrector(X,V) self.C._preTestFunc(X, V) T = self.func(X, V)[ind] # print 'X = ', repr(X) # print 'T = ', repr(T) if abs(T) < self.C.TestTol and min(linalg.norm(X-X1),linalg.norm(X-X2)) < self.C.VarTol: break elif abs(T) > Tmax: print('Test function going crazy: ', self, '\n') break else: if sign(T) == sign(T2): X2 = X V2 = V T2 = T p = 1.02 else: X1 = X V1 = V T1 = T p = 0.98 if self.C.verbosity >= 2 and i == self.C.MaxTestIters-1: print('Maximum test function iterations reached.\n') return X, V
def findzero(self, P1, P2, ind): """Uses secant method to find zero of test function.""" X1, V1 = P1 X2, V2 = P2 Z1 = copy(X1) Z2 = copy(X2) self.C._preTestFunc(X1, V1) T1 = self.func(X1, V1)[ind] self.C._preTestFunc(X2, V2) T2 = self.func(X2, V2)[ind] for i in range(self.C.MaxTestIters): try: r = abs(T1/(T1-T2)) if r >= 1: r = 0.5 except: r = 0.5 X = X1 + r*(X2-X1) V = V1 + r*(V2-V1) self.C.Corrector(X,V) self.C._preTestFunc(X, V) T = self.func(X, V)[ind] if abs(T) < self.C.TestTol and min(linalg.norm(X-X1),linalg.norm(X-X2)) < self.C.VarTol: break else: if sign(T) == sign(T2): X2 = X V2 = V T2 = T else: X1 = X V1 = V T1 = T if self.C.verbosity >= 2 and i == self.C.MaxTestIters-1: print 'Maximum test function iterations reached.\n' return X, V