Exemplo n.º 1
0
def SaveEigenvalueSolverShiftInvert(solver, shiftInvertSolver):
    """
	Saves the output of FindEigenvaluesNearShift, including error estimates 
	to a hdf file.
	"""

    logger = GetFunctionLogger()

    conf = solver.BaseProblem.Config
    L = conf.AngularRepresentation.index_iterator.L
    assert len(L) == 1
    shift = conf.Arpack.shift

    # generate filename
    filename = NameGen.GetBoundstateFilename(conf, L[0])

    # Get eigenvalue error estimates
    errorEstimatesPIRAM = solver.Solver.GetErrorEstimates()
    convergenceEstimatesEig = solver.Solver.GetConvergenceEstimates()
    errorEstimatesGMRES = shiftInvertSolver.Solver.GetErrorEstimateList()

    # Get eigenvalues
    prop = solver.BaseProblem
    E = 1.0 / array(solver.GetEigenvalues()) + shift

    # remove file if it exists
    try:
        if os.path.exists(filename):
            if pyprop.ProcId == 0:
                os.remove(filename)
    except:
        logger.error("Could not remove %s (%s)" % (filename, sys.exc_info()[1]))

        # Store eigenvalues and eigenvectors
    logger.info("Now storing eigenvectors...")
    for i in range(len(E)):
        solver.SetEigenvector(prop.psi, i)
        prop.SaveWavefunctionHDF(filename, NameGen.GetEigenvectorDatasetPath(i))

    if pyprop.ProcId == 0:
        RemoveExistingDataset(filename, "/Eig/Eigenvalues")
        RemoveExistingDataset(filename, "/Eig/ErrorEstimateListGMRES")
        RemoveExistingDataset(filename, "/Eig/ErrorEstimateListPIRAM")
        RemoveExistingDataset(filename, "/Eig/ConvergenceEstimateEig")
        h5file = tables.openFile(filename, "r+")
        try:
            # myGroup = h5file.createGroup("/", "Eig")
            myGroup = h5file.getNode("/Eig")
            h5file.createArray(myGroup, "Eigenvalues", E)
            h5file.createArray(myGroup, "ErrorEstimateListGMRES", errorEstimatesGMRES)
            h5file.createArray(myGroup, "ErrorEstimateListPIRAM", errorEstimatesPIRAM)
            h5file.createArray(myGroup, "ConvergenceEstimateEig", convergenceEstimatesEig)

            # Store config
            myGroup._v_attrs.configObject = prop.Config.cfgObj

            # PIRAM stats
            myGroup._v_attrs.opCount = solver.Solver.GetOperatorCount()
            myGroup._v_attrs.restartCount = solver.Solver.GetRestartCount()
            myGroup._v_attrs.orthCount = solver.Solver.GetOrthogonalizationCount()
        except:
            logger.warning("Warning: could not store eigenvalues and error estimates!")
        finally:
            h5file.close()

    pypar.barrier()
Exemplo n.º 2
0
def LoadBoundstateIndex(psi, filename, eigenvectorIndex):
	"""
	Loads an eigenstate from a file <filename> into a wavefunction <psi>. 
	This function works in parallel.

	Because eigenstates can be found independently for each "L", 
	the number and order of angular indices are different in the
	stored eigenstates, from that of the wavefunction to load the 
	eigenstate into.

	The eigenstate config file is used to create an array of CoupledIndex elements
	to which the local CoupledIndex elements from the wavefunction are compared

	"""
	angularRank = 0
	logger = GetFunctionLogger()

	logger.info("Loading bound state #%i from %s" % (eigenvectorIndex, filename))
	
	with tables.openFile(filename, "r", MAX_THREADS=1) as f:
		#Get eigenvector dataset
		eigVec = f.getNode(GetEigenvectorDatasetPath(eigenvectorIndex))

		#Get corresponding eigenvalue
		E = f.root.Eig.Eigenvalues[eigenvectorIndex]

		#find angular indices of the saved eigenstate
		conf = pyprop.LoadConfigFromFile(filename, "/Eig")
		eigCoupledIndices = array([i for i in conf.AngularRepresentation.index_iterator])

		#find local angular indices of the wavefunction to load into
		repr = psi.GetRepresentation()
		angRepr = repr.GetRepresentation(angularRank)
		localIndices = array(repr.GetLocalGrid(angularRank), dtype=int)

		#Load the local angular indices into the wavefunction
		psi.Clear()
		for curLocalIdx, curGlobalIdx in enumerate(localIndices):
			#Get the CoupledIndex to this angular index
			curCoupledIndex =  angRepr.Range.GetCoupledIndex(int(curGlobalIdx))

			#Find out the index in the eigenvector corresponding
			eigIdx, = nonzero(ravel(eigCoupledIndices == curCoupledIndex))
			if len(eigIdx) == 0:
				continue
			elif len(eigIdx) > 1:
				raise Exception("What? %s" % eigIdx)
			else:
				eigIdx = eigIdx[0]
				

			#Slice the wavefunction at the correct angular index
			psiSlice = [s_[:]]*psi.GetRank()
			psiSlice[angularRank] = curLocalIdx

			#Slice the eigenstate at the correct angular index
			eigSlice = [s_[:]]*psi.GetRank()
			eigSlice[angularRank] = eigIdx

			psi.GetData()[curLocalIdx,:,:] = eigVec[eigIdx,:,:]

	return E