Exemple #1
0
def kernel(localizer, mo_coeff=None, callback=None, verbose=logger.NOTE):
    if mo_coeff is None:
        mo_coeff = numpy.asarray(localizer.mo_coeff, order='C')
    else:
        mo_coeff = numpy.asarray(mo_coeff, order='C')
        localizer.mo_coeff = mo_coeff

    if localizer.verbose >= logger.WARN:
        localizer.check_sanity()
    localizer.dump_flags()

    cput0 = (time.clock(), time.time())
    if isinstance(verbose, logger.Logger):
        log = verbose
    else:
        log = logger.Logger(localizer.stdout, verbose)
    if localizer.conv_tol_grad is None:
        conv_tol_grad = numpy.sqrt(localizer.conv_tol * .1)
        log.info('Set conv_tol_grad to %g', conv_tol_grad)
    else:
        conv_tol_grad = localizer.conv_tol_grad

    u0 = localizer.get_init_guess(localizer.init_guess)
    rotaiter = ciah.rotate_orb_cc(localizer, u0, conv_tol_grad, verbose=log)
    u, g_orb, stat = next(rotaiter)
    cput1 = log.timer('initializing CIAH', *cput0)

    tot_kf = stat.tot_kf
    tot_hop = stat.tot_hop
    conv = False
    e_last = 0
    for imacro in range(localizer.max_cycle):
        norm_gorb = numpy.linalg.norm(g_orb)
        u0 = lib.dot(u0, u)
        e = localizer.cost_function(u0)
        e_last, de = e, e - e_last

        log.info('macro= %d  f(x)= %.14g  delta_f= %g  |g|= %g  %d KF %d Hx',
                 imacro + 1, e, de, norm_gorb, stat.tot_kf + 1, stat.tot_hop)
        cput1 = log.timer('cycle= %d' % (imacro + 1), *cput1)

        if (norm_gorb < conv_tol_grad and abs(de) < localizer.conv_tol):
            conv = True

        if callable(callback):
            callback(locals())

        if conv:
            break

        u, g_orb, stat = rotaiter.send(u0)
        tot_kf += stat.tot_kf
        tot_hop += stat.tot_hop

    rotaiter.close()
    log.info('macro X = %d  f(x)= %.14g  |g|= %g  %d intor %d KF %d Hx',
             imacro + 1, e, norm_gorb, (imacro + 1) * 2, tot_kf + imacro + 1,
             tot_hop)
    localizer.mo_coeff = lib.dot(mo_coeff, u0)
    return localizer.mo_coeff
Exemple #2
0
def kernel(localizer, mo_coeff=None, callback=None, verbose=logger.NOTE):
    if mo_coeff is None:
        mo_coeff = numpy.asarray(localizer.mo_coeff, order="C")
    else:
        mo_coeff = numpy.asarray(mo_coeff, order="C")
        localizer.mo_coeff = mo_coeff

    if localizer.verbose >= logger.WARN:
        localizer.check_sanity()
    localizer.dump_flags()

    cput0 = (time.clock(), time.time())
    if isinstance(verbose, logger.Logger):
        log = verbose
    else:
        log = logger.Logger(localizer.stdout, verbose)
    if localizer.conv_tol_grad is None:
        conv_tol_grad = numpy.sqrt(localizer.conv_tol * 0.1)
        log.info("Set conv_tol_grad to %g", conv_tol_grad)
    else:
        conv_tol_grad = localizer.conv_tol_grad

    u0 = localizer.get_init_guess(localizer.init_guess)
    rotaiter = ciah.rotate_orb_cc(localizer, u0, conv_tol_grad, verbose=log)
    u, g_orb, stat = next(rotaiter)
    cput1 = log.timer("initializing CIAH", *cput0)

    tot_kf = stat.tot_kf
    tot_hop = stat.tot_hop
    conv = False
    e_last = 0
    for imacro in range(localizer.max_cycle):
        norm_gorb = numpy.linalg.norm(g_orb)
        u0 = lib.dot(u0, u)
        e = localizer.cost_function(u0)
        e_last, de = e, e - e_last

        log.info(
            "macro= %d  f(x)= %.14g  delta_f= %g  |g|= %g  %d Hx %d KF",
            imacro + 1,
            e,
            de,
            norm_gorb,
            stat.tot_hop,
            stat.tot_kf + 1,
        )
        cput1 = log.timer("cycle= %d" % (imacro + 1), *cput1)

        if norm_gorb < conv_tol_grad and abs(de) < localizer.conv_tol:
            conv = True

        if callable(callback):
            callback(locals())

        if conv:
            break

        u, g_orb, stat = rotaiter.send(u0)
        tot_kf += stat.tot_kf
        tot_hop += stat.tot_hop

    rotaiter.close()
    log.info(
        "macro X = %d  f(x)= %.14g  |g|= %g  %d intor %d Hx %d KF",
        imacro + 1,
        e,
        norm_gorb,
        (imacro + 1) * 2,
        tot_hop,
        tot_kf + imacro + 1,
    )
    localizer.mo_coeff = lib.dot(mo_coeff, u0)
    return localizer.mo_coeff