def solveTemp(lr): try: answer = solveRoot(lambda T: getTemp(T, lr), self.h5file['logtemp'][0], self.h5file['logtemp'][-1], (), tol) except ValueError as err: print "Root for log10(T) not bracketed on entire table! " + str(err) # see if lower or upper temperature bound best logtemp = self.h5file['logtemp'] answer1 = multidimInterp((ye, logtemp[0], lr), [self.h5file['ye'][:], self.h5file['logtemp'], self.h5file['logrho']], self.h5file[quantity][...], linInterp, 2) - target answer2 = multidimInterp((ye, logtemp[-1], lr), [self.h5file['ye'][:], self.h5file['logtemp'], self.h5file['logrho']], self.h5file[quantity][...], linInterp, 2) - target if (abs(answer1) < abs(answer2)): answer = self.h5file['logtemp'][0] print "Recovering with lowest table value, answer: %s" % answer else: answer = self.h5file['logtemp'][-1] print "Recovering with highest value, answer: %s" % answer return numpy.power(10.0, answer)
def entropyOfT(logtemp, logrho, target): lrindex = getLogRhoIndex(logrho) return multidimInterp((logtemp, logrho), [theEos.h5file['logtemp'][:], theEos.h5file['logrho'][:]], theEos.h5file['entropy'][11, :, :], linInterp, 2) - target
def findIndVarOfMinAbsQuantity(self, indVar, point, quantity, function=(lambda x, q: q), target=0.0): """ Given an independent variable indVar, The values of the other independent variables as point, and a dependent variable quantity, Find for what value of indVar's table grid-points is quantity closest to zero. Uses simple sequential search. Designed for use in conjunction with solveForQuantity. If function is specified, it searches for function(indVar, quantity closest to zero) If target specified finds closest value to target """ assert indVar in self.indVars, "Input indVar %s is not a valid indVar!" % indVar indVarsTable = self.getIndVarsTable(omitTheseIndVars=(indVar,)) closestIndVar = None closestQuantity = 1.0e300 for i, var in enumerate(self.h5file[indVar][:]): index = self.tableIndexer(indVar, i) thisQuantity = function(var, multidimInterp(point, indVarsTable, self.h5file[quantity][index], linInterp, 2) ) - target if abs(thisQuantity) < closestQuantity: closestIndVar = var closestQuantity = abs(thisQuantity) return closestIndVar
def entropyOfT(logtemp, logrho, target): return multidimInterp((ye, logtemp, logrho), [theEos.h5file['ye'][:], theEos.h5file['logtemp'][:], theEos.h5file['logrho'][:]], theEos.h5file['entropy'][:, :, :], linInterp, 2) - target
def getTemp(t, lr): answer = multidimInterp((ye, t, lr), [self.h5file['ye'][:], self.h5file['logtemp'], self.h5file['logrho']], self.h5file[quantity][...], linInterp, 2) - target return answer
def findYeOfMinAbsMunu(self, point): """ Given a point in T, rho, calculate for what value of the Ye table grid-points is munu the closest to zero. """ closestYeToMunusZero = None closestMunuToZero = 1.0e300 for i, ye in enumerate(self.h5file['ye'][:]): munu = multidimInterp(point, [self.h5file['logtemp'], self.h5file['logrho']], self.h5file['munu'][i, ...], linInterp, 2) if abs(munu) < closestMunuToZero: closestYeToMunusZero = ye closestMunuToZero = abs(munu) return closestYeToMunusZero
def setBetaEqState(self, pointDict, useThisYeIfSolveFails=None): """ Takes dictionary for physical state values EXCEPT Ye, then sets Ye via solving for neutrino-less beta equilibrium. Modifies self.physicalState """ assert isinstance(pointDict, dict) assert 'ye' not in pointDict, "You can't SPECIFY a Ye if you're " \ "setting neutrinoless beta equlibrium!" self.validatePointDict(pointDict) assert len(pointDict) < 3, "State overdetermined for more than 2 indVars!" #defines 1D root solver to use in routine solveRoot = scipyOptimize.brentq # solveRootBisect #ASSUME 2 INDEPENENT VARIABLES ARE rho & temp logtemp = pointDict['logtemp'] logrho = pointDict['logrho'] tol = 1.e-6 getYe = lambda x : multidimInterp((x, logtemp, logrho), [self.h5file['ye'][:], self.h5file['logtemp'], self.h5file['logrho']], self.h5file['munu'][...], linInterp, 2) #check for bracketing error in root solve for ye try: currentYe = solveRoot(getYe, self.h5file['ye'][0], self.h5file['ye'][-1], (), tol) except ValueError as err: print "Error in scipy root solver solving for ye: ", str(err) if useThisYeIfSolveFails is None: currentYe = self.findYeOfMinAbsMunu((logtemp, logrho)) print "Recovering with findYeOfMinAbsMunu, answer: %s" % currentYe else: currentYe = useThisYeIfSolveFails print "Setting Ye to useThisYeIfSolveFails, answer: %s" % currentYe newDict = pointDict.copy() newDict['ye'] = currentYe self.setState(newDict) return currentYe
def quantityOfSolveVar(x): #Here we construct the point to interpolate at, but we # must do it carefully since we don't know apriori what # solveVar is point = [] #todo factor this for out of quantityOfSolveVar for indVar in self.indVars: if indVar not in pointDict: #print "NOT", indVar value = x else: value = pointDict[indVar] if indVar == 'logtemp': value = pointAsFunctionOfSolveVar(x) #print indVar, value point.append(value) point = tuple(point) if setBetaEqInSolve: # tempPointDict = {self.indVars[i]: point[i] # for i in range(len(self.indVars)) if not self.indVars[i] == 'ye'} for i in range(len(self.indVars)): print self.indVars[i] tempPointDict = [] print "Should not have gotten to this point; debug me!" sys.exit() yeForSolve = linInterp(tempPointDict['logrho'], cachedBetaEqYeVsRhos[0], cachedBetaEqYeVsRhos[1]) tempPointDict.update({'ye': yeForSolve}) point = self.pointFromDict(tempPointDict) del tempPointDict answer = function(x, multidimInterp(point, indVarsTable, self.h5file[quantity][...], linInterp, 2) ) - target return answer
def setConstQuantityAndBetaEqState(self, pointDict, quantity, target): """ Does inefficient 2D root solve to set state at neutrino-less beta equilibrium and quantity = target. Sets the physicalState and then returns ye, temp Modifies self.physicalState """ print "setConstQuantityAndBetaEqState: ", pointDict assert 'ye' not in pointDict, "You can't SPECIFY a Ye if you're " \ "setting neutrinoless beta equlibrium!" self.validatePointDict(pointDict) assert len(pointDict) < 2, "State overdetermined for more than 1 indVars!" #todo: check quantity is valid 3D table #defines 1D root solver to use in routine solveRoot = scipyOptimize.brentq # solveRootBisect solveVarName = 'logtemp' currentSolveVar = 0.0 currentYe = 0.25 #previous variables used to measure convergence of solve # so set them to something significantly different than starting values previousSolveVar = 100.0 previousYe = 100.0 yeError = relativeError(currentYe, previousYe) solveVarError = relativeError(currentSolveVar, previousSolveVar) otherVarName = pointDict.keys()[0] otherVar = pointDict.values()[0] maxIters = 5 tol = 1e-3 iteration = 0 while iteration < maxIters and yeError + solveVarError > tol/2.0: previousSolveVar = currentSolveVar previousYe = currentYe getSolveVar = lambda x: multidimInterp((currentYe, x, otherVar), [self.h5file['ye'][:], self.h5file[solveVarName], self.h5file[otherVarName]], self.h5file[quantity][...], linInterp, 2) - target try: currentSolveVar = solveRoot(getSolveVar, self.h5file[solveVarName][0], self.h5file[solveVarName][-1], (),tol) except ValueError as err: print "Root for log10(T) not bracketed on entire table: " \ + str(err) # see if lower or upper temperature bound best logtemp = self.h5file['logtemp'] answer1 = multidimInterp((currentYe, logtemp[0], otherVar), [self.h5file['ye'][:], self.h5file['logtemp'], self.h5file['logrho']], self.h5file[quantity][...], linInterp, 2) - target answer2 = multidimInterp((currentYe, logtemp[-1], otherVar), [self.h5file['ye'][:], self.h5file['logtemp'], self.h5file['logrho']], self.h5file[quantity][...], linInterp, 2) - target if (abs(answer1) < abs(answer2)): currentSolveVar = self.h5file['logtemp'][0] print "Recovering with lowest table value, answer: %s" % currentSolveVar else: currentSolveVar = self.h5file['logtemp'][-1] print "Recovering with highest value, answer: %s" % currentSolveVar getYe = lambda x : multidimInterp((x, currentSolveVar, otherVar), [self.h5file['ye'][:], self.h5file[solveVarName], self.h5file[otherVarName]], self.h5file['munu'][...], linInterp, 2) #check for bracketing error in root solve for ye try: currentYe = solveRoot(getYe, self.h5file['ye'][0], self.h5file['ye'][-1], (), tol) except ValueError as err: print "Error in scipy root solver solving for ye: ", str(err) currentYe = self.findYeOfMinAbsMunu((currentSolveVar, otherVar)) print "Recovering with findYeOfMinAbsMunu, answer: %s" % currentYe #print "currentYe: ", currentYe, "\tcurrentT: ", currentSolveVar yeError = relativeError(currentYe, previousYe) solveVarError = relativeError(currentSolveVar, previousSolveVar) iteration += 1 #print "errs: ", yeError, solveVarError newDict = pointDict.copy() newDict['ye'] = currentYe temp = numpy.power(10.0,currentSolveVar) # TODO TEMP HARD CODE newDict['temp'] = temp self.setState(newDict) return currentYe, temp # TODO TEMP HARD CODE