def newtoninterpolate(alpha_data, phi_data, old_alpha, old_phi): # Interpolates using a polynomial of increasing order # The coefficients of the interpolated polynomial are found using the Newton method if very_old_alpha is None: return quadinterpolate(alpha_lo, alpha_hi, old_alpha, old_phi) if old_alpha in alpha_data: return quadinterpolate(alpha_lo, alpha_hi, old_alpha, old_phi) alpha_data.append(old_alpha) phi_data.append(old_phi) coefs = newton_poly(alpha_data, phi_data) alpha, ii = newtonroot(coefs, alpha_data, old_alpha) if alpha < alpha_lo or alpha > alpha_hi: # Root is outside the domain if getMPIRankWorld() == 0: zoomlogger.debug( "NOTE: Newton interpolation converged on a root outside of [alpha_lo,alpha_hi]. Falling back on cubic interpolation" ) return cubicinterpolate(alpha_lo, alpha_hi, old_alpha, old_phi, very_old_alpha, very_old_phi) if np.abs(alpha - old_alpha) < tol_df or np.abs( alpha) < tol_sm * np.abs(old_alpha): alpha = 0.5 * old_alpha if abs(alpha) <= 0: if getMPIRankWorld() == 0: zoomlogger.debug( "NOTE: Newton interpolation returned alpha <= 0. Falling back on cubic interpolation" ) return cubicinterpolate(alpha_lo, alpha_hi, old_alpha, old_phi, very_old_alpha, very_old_phi) return alpha, len(alpha_data)
def getMeshHandler(self): """ Returns a handle to a mesh meshing the design. In the current implementation a mesh file name in Triangle format is returned. """ args = self.getCommandString().split() args[-1]=args[-1]%self.getScriptFileName() if getMPIRankWorld() == 0: import subprocess open(self.getScriptFileName(),"w").write(self.getScriptString()) ret = subprocess.call(args) / 256 else: ret=0 ret=getMPIWorldMax(ret) if ret > 0: raise RuntimeError("Could not build mesh: %s"%" ".join(args)) else: # <hack> so that users can set the mesh filename they want. name=self.getScriptFileName() if (".poly" in name) or (".ele" in name) or (".node" in name): s=name.split(".")[:-1] name=s[0] if len(s) > 1: name+=".%s"*(len(s)-1)%tuple(s[1:]) files=glob.glob("%s.1.*"%name) for f in files: sufx=f.split(".")[-1] mshName=self.getMeshFileName()+"."+sufx os.rename(f,mshName) # </hack> return self.getMeshFileName()
def getScriptHandler(self): """ Returns a handler to the script file to generate the geometry. In the current implementation a script file name is returned. """ if getMPIRankWorld() == 0: open(self.getScriptFileName(),"w").write(self.getScriptString()) return self.getScriptFileName()
def newtonroot(coefs, alpha_data, startingGuess): # Solves for the root of a polynomial using the newton method dfcoefs = list(range(1, len(coefs))) for i in range(0, len(coefs) - 1): dfcoefs[i] *= coefs[i + 1] tol = 1e-6 error = 100 maxiterations = 100 point = startingGuess counter = 0 for i in range(0, maxiterations): counter += 1 product = 1 numer = coefs[0] denom = dfcoefs[0] for i in range(1, len(coefs)): product *= (point - alpha_data[i - 1]) numer += product * coefs[i] if i < len(coefs) - 1: denom += product * dfcoefs[i] if denom == 0: if getMPIRankWorld() == 0: zoomlogger.debug( "NOTE: The Newton solver failed (denom==0). Falling back on cubic interpolation" ) return cubicinterpolate(alpha_lo, alpha_hi, old_alpha, old_phi, very_old_alpha, very_old_phi) newpoint = float(point - numer / denom) error = abs(newpoint - point) if error < tol: if getMPIRankWorld() == 0: zoomlogger.debug( "Newton interpolation converged in %d iterations. Got alpha = %f" % (counter, newpoint)) return newpoint, counter else: point = newpoint # zoomlogger.debug("NOTE: Newton interpolation failed to converge (exceeded max iterations). Falling back on cubic interpolation" % error) return cubicinterpolate(alpha_lo, alpha_hi, old_alpha, old_phi, very_old_alpha, very_old_phi)