Example #1
0
def VCAGPM(engine,app):
    '''
    This method implements the grand potential based methods.
    '''
    records={}
    def gp(values,keys):
        if tuple(values) not in records:
            engine.cache.pop('ptmesh',None)
            engine.update(**{key:value for key,value in zip(keys,values)})
            engine.rundependences(app.name)
            records[tuple(values)]=engine.records[app.dependences[0]]
        return records[tuple(values)]
    if isinstance(app.BS,HP.BaseSpace):
        mode,nbs,nder,minormax='+',len(app.BS.tags),app.options.get('nder',0),app.options.get('minormax','min')
        result=np.zeros((app.BS.rank(0),nbs+nder+1))
        for i,paras in enumerate(app.BS(mode)):
            result[i,0:nbs]=np.array(list(paras.values()))
            result[i,nbs]=gp(list(paras.values()),list(paras.keys()))
        if nder>0:result[:,nbs+1:]=HM.derivatives(result[:,0],result[:,nbs],ders=list(range(1,nder+1))).T
        index=np.argmin(result[:,-1]) if minormax=='min' else np.argmax(result[:,-1]) if minormax=='max' else np.argmax(np.abs(result[:,-1]))
        engine.log<<'Summary:\n%s\n'%HP.Sheet(
                                cols=           app.BS.tags+['%sgp'%('' if nder==0 else '%s der of '%HP.ordinal(nder-1))],
                                contents=       np.append(result[index,0:nbs],result[index,-1]).reshape((1,-1))
                                )
        name='%s_%s'%(engine.tostr(mask=app.BS.tags),app.name)
        if app.savedata: np.savetxt('%s/%s.dat'%(engine.dout,name),result)
        if app.plot: app.figure('L',result,'%s/%s'%(engine.dout,name),interpolate=True,legend=['%sgp'%('%s der of '%HP.ordinal(k-1) if k>0 else '') for k in range(nder+1)])
        if app.returndata: return result
    else:
        result=HM.fstable(gp,list(app.BS.values()),args=(list(app.BS.keys()),),**app.options)
        engine.log<<'Summary:\n%s\n'%HP.Sheet(cols=list(app.BS.keys())+['niter','nfev','gp'],contents=np.append(result.x,[result.nit,result.nfev,result.fun]).reshape((1,-1)))
        if app.savedata: np.savetxt('%s/%s_%s.dat'%(engine.dout,engine.tostr(mask=list(app.BS.keys())),app.name),np.append(result.x,result.fun))
        if app.returndata: return {key:value for key,value in zip(list(app.BS.keys()),result.x)},result.fun
Example #2
0
def VCAGPM(engine,app):
    '''
    This method implements the grand potential based methods.
    '''
    records={}
    def gp(values,keys):
        if tuple(values) not in records:
            engine.cache.pop('pt_kmesh',None)
            engine.update(**{key:value for key,value in zip(keys,values)})
            engine.rundependences(app.name)
            records[tuple(values)]=engine.records[app.dependences[0]]
        return records[tuple(values)]
    if isinstance(app.BS,HP.BaseSpace):
        mode,nbs,nder,minormax='+',len(app.BS.tags),app.options.get('nder',0),app.options.get('minormax','min')
        result=np.zeros((app.BS.rank(0),nbs+nder+1))
        for i,paras in enumerate(app.BS(mode)):
            result[i,0:nbs]=np.array(paras.values())
            result[i,nbs]=gp(paras.values(),paras.keys())
        if nder>0:result[:,nbs+1:]=HM.derivatives(result[:,0],result[:,nbs],ders=range(1,nder+1)).T
        index=np.argmin(result[:,-1]) if minormax=='min' else np.argmax(result[:,-1]) if minormax=='max' else np.argmax(np.abs(result[:,-1]))
        engine.log<<'Summary:\n%s\n'%HP.Sheet(
                                cols=           app.BS.tags+['%sgp'%('' if nder==0 else '%s der of '%HP.ordinal(nder-1))],
                                contents=       np.append(result[index,0:nbs],result[index,-1]).reshape((1,-1))
                                )
        name='%s_%s'%(engine.tostr(mask=app.BS.tags),app.name)
        if app.savedata: np.savetxt('%s/%s.dat'%(engine.dout,name),result)
        if app.plot: app.figure('L',result,'%s/%s'%(engine.dout,name),interpolate=True,legend=['%sgp'%('%s der of '%HP.ordinal(k-1) if k>0 else '') for k in xrange(nder+1)])
        if app.returndata: return result
    else:
        result=HM.fstable(gp,app.BS.values(),args=(app.BS.keys(),),**app.options)
        engine.log<<'Summary:\n%s\n'%HP.Sheet(cols=app.BS.keys()+['niter','nfev','gp'],contents=np.append(result.x,[result.nit,result.nfev,result.fun]).reshape((1,-1)))
        if app.savedata: np.savetxt('%s/%s_%s.dat'%(engine.dout,engine.tostr(mask=app.BS.keys()),app.name),np.append(result.x,result.fun))
        if app.returndata: return {key:value for key,value in zip(app.BS.keys(),result.x)},result.fun
Example #3
0
def EDEL(engine, app):
    '''
    This method calculates the energy levels of the Hamiltonian.
    '''
    name = '%s_%s' % (engine.tostr(mask=app.path.tags), app.name)
    result = np.zeros((app.path.rank(0), app.ns * (app.nder + 1) + 1))
    result[:, 0] = app.path.mesh(0) if len(
        app.path.tags) == 1 and app.path.mesh(0).ndim == 1 else np.array(
            range(app.path.rank(0)))
    for i, paras in enumerate(app.path('+')):
        engine.update(**paras)
        result[i, 1:app.ns + 1] = engine.eigs(
            sector=app.sector,
            k=app.ns,
            evon=False,
            resetmatrix=True if i == 0 else False,
            resettimers=True if i == 0 else False)[1]
        engine.log << '%s\n\n' % engine.timers.tostr(HP.Timers.ALL)
        if app.plot: engine.timers.graph(parents=HP.Timers.ALL)
    else:
        if app.plot:
            engine.timers.cleancache()
            if app.savefig:
                plt.savefig('%s/%s_TIMERS.png' % (engine.log.dir, name))
            plt.close()
    if app.nder > 0:
        for i in range(app.ns):
            result.T[[j * app.ns + i + 1 for j in range(1, app.nder + 1)
                      ]] = HM.derivatives(result[:, 0],
                                          result[:, i + 1],
                                          ders=list(range(1, app.nder + 1)))
    if app.savedata: np.savetxt('%s/%s.dat' % (engine.dout, name), result)
    if app.plot:
        ns = app.ns
        options = {
            'legend':
            [('%s der of ' % HP.ordinal(k // ns - 1) if k // ns > 0 else '') +
             '$E_{%s}$' % (k % ns) for k in range(result.shape[1] - 1)],
            'legendloc':
            'lower right'
        } if ns <= 10 else {}
        app.figure('L', result, '%s/%s' % (engine.dout, name), **options)
    if app.returndata: return result
Example #4
0
def EDEL(engine,app):
    '''
    This method calculates the energy levels of the Hamiltonian.
    '''
    name='%s_%s'%(engine.tostr(mask=app.path.tags),app.name)
    result=np.zeros((app.path.rank(0),app.ns*(app.nder+1)+1))
    result[:,0]=app.path.mesh(0) if len(app.path.tags)==1 and app.path.mesh(0).ndim==1 else np.array(range(app.path.rank(0)))
    for i,paras in enumerate(app.path('+')):
        engine.update(**paras)
        result[i,1:app.ns+1]=engine.eigs(sector=app.sector,k=app.ns,evon=False,resetmatrix=True if i==0 else False,resettimers=True if i==0 else False)[1]
        engine.log<<'%s\n\n'%engine.timers.tostr(HP.Timers.ALL)
        if app.plot: engine.timers.graph(parents=HP.Timers.ALL)
    else:
        if app.plot:
            engine.timers.cleancache()
            if app.savefig: plt.savefig('%s/%s_TIMERS.png'%(engine.log.dir,name))
            plt.close()
    if app.nder>0:
        for i in range(app.ns): result.T[[j*app.ns+i+1 for j in range(1,app.nder+1)]]=HM.derivatives(result[:,0],result[:,i+1],ders=list(range(1,app.nder+1)))
    if app.savedata: np.savetxt('%s/%s.dat'%(engine.dout,name),result)
    if app.plot:
        ns=app.ns
        options={'legend':[('%s der of '%HP.ordinal(k//ns-1) if k//ns>0 else '')+'$E_{%s}$'%(k%ns) for k in range(result.shape[1]-1)],'legendloc':'lower right'} if ns<=10 else {}
        app.figure('L',result,'%s/%s'%(engine.dout,name),**options)
    if app.returndata: return result