예제 #1
0
파일: mpdeb.py 프로젝트: haoxy97/I-ReaxFF
def gradb(direcs,
          v='bo5',
          bd='H-H',
          nn=True,
          bo_layer=[9, 2],
          massages=1,
          debd=False,
          deba=True,
          deang=True,
          dft='siesta',
          batch=50):
    ''' variables like: bo1_C-H, boc5_C rosi_C-H boc1
    '''
    v_ = v
    v = v + '_' + bd
    print('-  grading ... ...')
    ffield = 'ffield.json' if nn else 'ffield'

    rn = MPNN(libfile=ffield,
              direcs=direcs,
              dft=dft,
              nn=nn,
              bo_layer=bo_layer,
              massages=massages,
              batch_size=batch,
              pkl=True)
    rn.initialize()
    rn.session(learning_rate=3.0e-4, method='AdamOptimizer')

    if nn:
        bdlit = [
            'bop', 'bop_si', 'bop_pi', 'bop_pp', 'F', 'bosi', 'bopi', 'bopp',
            'powb', 'expb', 'sieng', 'EBD'
        ]
    else:
        bdlit = [
            'bop', 'bop_si', 'bop_pi', 'bop_pp', 'f_1', 'f_2', 'f_3', 'f_4',
            'f_5', 'bosi', 'bopi', 'bopp', 'powb', 'expb', 'sieng', 'EBD'
        ]

    if debd:
        bonds = rn.bonds
        for b in bonds:
            v = v_ + '_' + b

            grad = rn.get_gradient(rn.Loss, rn.p[v])
            text_ = '-  the gradient of Loss/%s is ' % v
            logger.info(text_ + str(grad))

            if grad is None:
                continue
            if not np.isnan(grad):
                continue

            if rn.nbd[b] > 0:
                grad = rn.get_gradient(rn.__dict__['EBD'][b], rn.p[v])
                logger.info('-  the gradient of %s/%s is: %s' %
                            ('EBD' + '_' + b, v, str(grad)))
                if not grad is None:
                    if np.isnan(grad):
                        for l in bdlit:
                            grad = rn.get_gradient(rn.__dict__[l][bd], rn.p[v])
                            logger.info('-  the gradient of %s/%s is: %s' %
                                        (l + '_' + b, v, str(grad)))
    v = v_ + '_' + bd
    if deba:
        sl = ['EL', 'EOV', 'EUN']
        alist = {
            'EL': ['Delta_lp', 'Delta_e', 'explp'],
            'EOV': ['Delta_lpcorr', 'Delta_lp', 'nlp', 'so', 'otrm1', 'otrm2'],
            'EUN': [
                'expeu1', 'expeu3', 'Delta_lpcorr', 'Delta_lp', 'Delta_e',
                'explp', 'nlp'
            ]
        }
        for sp in rn.spec:
            for l in sl:
                if sp in rn.__dict__[l]:
                    grad = rn.get_gradient(rn.__dict__[l][sp], rn.p[v])
                    logger.info('-  the gradient of %s/%s is: %s' %
                                (l + '_' + sp, v, str(grad)))

                    if not grad is None:
                        if np.isnan(grad):
                            for al in alist[l]:
                                grad = rn.get_gradient(rn.__dict__[al][sp],
                                                       rn.p[v])
                                logger.info('-  the gradient of %s/%s is: %s' %
                                            (al + '_' + sp, v, str(grad)))

    if deang:
        al = ['EANG', 'EPEN', 'ETC']
        for ang in rn.angs:
            # v  = 'val1'+'_'+ang
            if rn.nang[ang] > 0:
                for l in al:
                    grad = rn.get_gradient(rn.__dict__[l][ang], rn.p[v])
                    logger.info('-  the gradient of %s/%s is: %s' %
                                (l + '_' + ang, v, str(grad)))

    tl = ['ETOR', 'Efcon']
    for tor in rn.tors:
        # v  = 'tor2' # +'_'+tor
        if rn.ntor[tor] > 0:
            for l in tl:
                grad = rn.get_gradient(rn.__dict__[l][tor], rn.p[v])
                logger.info('-  the gradient of %s/%s is: %s' %
                            (l + '_' + tor, v, str(grad)))
    rn.sess.close()
예제 #2
0
파일: train.py 프로젝트: haoxy97/I-ReaxFF
def train_():
    # this trajectory files can visulized by ase, i.e. use commond
    # can see the configurations "ase gui C2H2-0.traj"
    direcs = {
        'C2H2-0': '../data/C2H2-0.traj',
        'C2H30O130': '../data/C2H30O130.traj',
        'C2H40': '../data/C2H40.traj',
        'nm1-5': '../data/C1N1O2H3-0.traj',
        'nmb': '../data/nmb.traj',
        'h2osw': '../data/h2osw.traj',
        'nm2l': '../data/nm2l.traj',
        'C4N4O8H12-3-0': '../data/nml3-0.traj',
        'C4N4O8H12-5-0': '../data/nml5-0.traj',
        'C4N4O8H12-5-1': '../data/nml5-1.traj',
        'C4N4O8H12-6-0': '../data/nml6-0.traj',
        'C4N4O8H12-6-1': '../data/nml6-1.traj',
        'C4N4O8H12-7-1': '../data/nml7-1.traj',
        'C4N4O8H12-8-0': '../data/nml8-0.traj',
        'C4N4O8H12-9-0': '../data/nml9-0.traj',
        'C4N4O8H12-11': '../data/nml11.traj',
        'C4N4O8H12-10-0': '../data/nml10-0.traj',
        'C4N4O8H12-10-1': '../data/nml10-1.traj',
        'C4N4O8H12-12-1': '../data/nml12-1.traj',
        'C4N4O8H12-13-0': '../data/nml13-0.traj',
        'C4N4O8H12-140': '../data/nml14-0.traj',
        'C4N4O8H12-141': '../data/nml14-1.traj',
        'nm1-CN': '../data/nm1-CN.traj',
        'c2h6-0': '../data/c2h6-1-0.traj',
        'c2h6-1': '../data/c2h6-1-1.traj',
        'c2h6-2': '../data/c2h6-1-2.traj',
        'c2h6-3': '../data/c2h6-1-3.traj',
        'c2h6-4': '../data/c2h6-1-4.traj',
        'c2h4-0': '../data/c2h4-1-0.traj',
        'c2h4-1': '../data/c2h4-1-1.traj',
        'c2h4-2': '../data/c2h4-1-2.traj',
        'c2h4-3': '../data/c2h4-1-3.traj',
        'c2h4-CC': '../data/c2h4-CC.traj',
        'c2h2-CC': '../data/c2h2-CC.traj',
        'c2h6-CC': '../data/c2h6-CC.traj',
        'c2h2-1-0': '../data/c2h2-1-0.traj',
        'c2h2-1-1': '../data/c2h2-1-1.traj',
        'c2h2-1-2': '../data/c2h2-1-2.traj',
        'c2h2-1-3': '../data/c2h2-1-3.traj',
    }

    mn = MPNN(libfile='ffield.json',
              direcs=direcs,
              dft='siesta',
              weight={
                  'c2h6-CC': 50.0,
                  'others': 2.0
              },
              messages=1,
              bo_layer=[4, 1],
              bf_layer=[12, 3],
              be_layer=[9, 2],
              EnergyFunction=3,
              MessageFunction=1,
              pkl=False,
              batch_size=50,
              losFunc='n2',
              convergence=0.98)

    mn.run(learning_rate=1.0e-4, step=100, print_step=10, writelib=100)
    mn.close()
예제 #3
0
def e(traj='siesta.traj', batch_size=50, nn=True):
    ffield = 'ffield.json' if nn else 'ffield'
    images = Trajectory(traj)
    e = []
    eb, el, eo, eu = [], [], [], []
    ea, ep, etc = [], [], []
    et, ef = [], []
    ev, eh, ec = [], [], []

    from irff.irff import IRFF
    ir = IRFF(atoms=images[0], libfile=ffield, nn=nn)

    for i, atoms in enumerate(images):
        ir.get_potential_energy(atoms)
        e.append(ir.E.numpy())
        eb.append(ir.Ebond.numpy())
        el.append(ir.Elone.numpy())
        eo.append(ir.Eover.numpy())
        eu.append(ir.Eunder.numpy())
        ea.append(ir.Eang.numpy())
        ep.append(ir.Epen.numpy())
        et.append(ir.Etor.numpy())
        ef.append(ir.Efcon.numpy())
        etc.append(ir.Etcon.numpy())
        ev.append(ir.Evdw.numpy())
        eh.append(ir.Ehb.numpy())
        ec.append(ir.Ecoul.numpy())

    from irff.mpnn import MPNN
    rn = MPNN(libfile=ffield,
              direcs={'tmp': traj},
              dft='siesta',
              opt=[],
              optword='nocoul',
              batch_size=batch_size,
              atomic=True,
              clip_op=False,
              InitCheck=False,
              nn=nn,
              bo_layer=[9, 2],
              pkl=False)
    molecules = rn.initialize()
    rn.session(learning_rate=1.0e-10, method='AdamOptimizer')

    mol = 'tmp'
    e_ = rn.get_value(rn.E[mol])
    eb_ = rn.get_value(rn.ebond[mol])
    el_ = rn.get_value(rn.elone[mol])
    eu_ = rn.get_value(rn.eunder[mol])
    eo_ = rn.get_value(rn.eover[mol])
    ea_ = rn.get_value(rn.eang[mol])
    ep_ = rn.get_value(rn.epen[mol])
    etc_ = rn.get_value(rn.tconj[mol])
    et_ = rn.get_value(rn.etor[mol])
    ef_ = rn.get_value(rn.efcon[mol])
    ev_ = rn.get_value(rn.evdw[mol])
    ec_ = rn.get_value(rn.ecoul[mol])
    eh_ = rn.get_value(rn.ehb[mol])

    e_reax = {
        'Energy': e,
        'Ebond': eb,
        'Eunger': eu,
        'Eover': eo,
        'Eang': ea,
        'Epen': ep,
        'Elone': el,
        'Etcon': etc,
        'Etor': et,
        'Efcon': ef,
        'Evdw': ev,
        'Ecoul': ec,
        'Ehbond': eh
    }
    e_irff = {
        'Energy': e_,
        'Ebond': eb_,
        'Eunger': eu_,
        'Eover': eo_,
        'Eang': ea_,
        'Epen': ep_,
        'Elone': el_,
        'Etcon': etc_,
        'Etor': et_,
        'Efcon': ef_,
        'Evdw': ev_,
        'Ecoul': ec_,
        'Ehbond': eh_
    }

    for key in e_reax:
        plt.figure()
        plt.ylabel('%s (eV)' % key)
        plt.xlabel('Step')

        plt.plot(e_reax[key],
                 alpha=0.01,
                 linestyle='-.',
                 marker='o',
                 markerfacecolor='none',
                 markeredgewidth=1,
                 markeredgecolor='b',
                 markersize=4,
                 color='blue',
                 label='ReaxFF')
        plt.plot(e_irff[key],
                 alpha=0.01,
                 linestyle=':',
                 marker='^',
                 markerfacecolor='none',
                 markeredgewidth=1,
                 markeredgecolor='red',
                 markersize=4,
                 color='red',
                 label='IRFF')
        plt.legend(loc='best',
                   edgecolor='yellowgreen')  # lower left upper right
        plt.savefig('%s.eps' % key)
        plt.close()
예제 #4
0
def db(gen='C3H7O1-0.traj', batch_size=50, nn=True, frame=7):
    ffield = 'ffield.json' if nn else 'ffield'
    # atoms  = read(gen,index=1)
    images = Trajectory(gen)
    atoms = images[frame]

    # his    = TrajectoryWriter('tmp.traj',mode='w')
    # calc   = SinglePointCalculator(atoms,energy=0.0,free_energy=0.0)
    # atoms.set_calculator(calc)
    # his.write(atoms=atoms)
    # his.close()

    from irff.irff import IRFF
    e, ei = [], []
    ir = IRFF(atoms=atoms, libfile=ffield, nn=nn)

    ir.get_potential_energy(atoms)

    ei.append(ir.Ebond)
    ebond = ir.ebond.numpy()

    from irff.mpnn import MPNN
    rn = MPNN(libfile=ffield,
              direcs={'tmp': gen},
              dft='siesta',
              opt=[],
              optword='nocoul',
              batch_size=batch_size,
              atomic=True,
              clip_op=False,
              InitCheck=False,
              nn=nn,
              bo_layer=[9, 2],
              pkl=False)
    molecules = rn.initialize()
    rn.session(learning_rate=1.0e-10, method='AdamOptimizer')

    mol = 'tmp'
    er = rn.get_value(rn.ebond[mol])
    blab = rn.lk.bdlab

    bosi = ir.bosi.numpy()
    bopsi = ir.bop_si.numpy()
    boppi = ir.bop_pi.numpy()
    boppp = ir.bop_pp.numpy()

    bopow3 = ir.bopow3.numpy()
    eterm3 = ir.eterm3.numpy()

    bop = ir.bop.numpy()
    bo = ir.bo0.numpy()

    eterm1 = ir.eterm1.numpy()
    bopow1 = ir.bopow1.numpy()
    bodiv1 = ir.bodiv1.numpy()

    r = ir.r.numpy()
    # F      = ir.F.numpy()
    # Fi     = ir.Fi.numpy()
    # Fj     = ir.Fj.numpy()
    D_ = rn.get_value(rn.Deltap)
    D = ir.Deltap.numpy()

    for bd in rn.bonds:
        for nb in range(rn.nbd[bd]):
            ebd = rn.get_value(rn.EBD[bd])
            mol_, i, j = blab[bd][nb]
            bosi_ = rn.get_value(rn.bosi[bd])
            bopsi_ = rn.get_value(rn.bop_si[bd])
            boppi_ = rn.get_value(rn.bop_pi[bd])
            boppp_ = rn.get_value(rn.bop_pp[bd])

            bopow3_ = rn.get_value(rn.bopow3[bd])
            eterm3_ = rn.get_value(rn.eterm3[bd])

            bop_ = rn.get_value(rn.bop[bd])
            bo_ = rn.get_value(rn.bo0[bd])

            eterm1_ = rn.get_value(rn.eterm1[bd])
            bopow1_ = rn.get_value(rn.bopow1[bd])
            bodiv1_ = rn.get_value(rn.bodiv1[bd])

            rbd = rn.get_value(rn.rbd[bd])

            # if abs(bo_[nb][0]-bo[i][j])>0.00001:
            print(
                '-  %s %2d %2d:' % (bd, i, j),
                'rbd %10.7f %10.7f' % (rbd[nb][frame], r[i][j]),
                'bop %10.7f %10.7f' % (bop_[nb][frame], bop[i][j]),
                'Di %10.7f %10.7f' % (D_[i][frame], D[i]),
                'Dj %10.7f %10.7f' % (D_[j][frame], D[j]),
                'bo %10.7f %10.7f' % (bo_[nb][frame], bo[i][j]),
                # 'F %10.7f %10.7f' %(F_[nb][0],F[i][j]),
                # 'Fi %10.7f %10.7f' %(Fi_[nb][0],Fi[i][j]),
                # 'Fj %10.7f %10.7f' %(Fj_[nb][0],Fj[i][j]),
                'ebond %10.7f %10.7f' % (ebd[nb][frame], ebond[i][j]))

    rcbo = rn.get_value(rn.rc_bo)
    eb = rn.get_value(rn.ebond[mol])
    # print(rcbo)
    print('\n-  bond energy:', ir.Ebond.numpy(), eb[frame], end='\n')
예제 #5
0
파일: irffdeb.py 프로젝트: haoxy97/I-ReaxFF
def da(traj='opt.traj', batch_size=50, ef=3, nn=True, frame=44):
    ffield = 'ffield.json' if nn else 'ffield'
    images = Trajectory(traj)
    atoms = images[frame]
    his = TrajectoryWriter('tmp.traj', mode='w')
    his.write(atoms=atoms)
    # his.write(atoms=images[frame+1])
    his.close()

    ir = IRFF(atoms=atoms, libfile=ffield, nn=nn, autograd=False)
    ir.calculate(atoms)

    rn = MPNN(libfile=ffield,
              direcs={'tmp': 'tmp.traj'},
              dft='siesta',
              opt=[],
              optword='nocoul',
              batch_size=batch_size,
              atomic=True,
              clip_op=False,
              InitCheck=False,
              nn=nn,
              bo_layer=[9, 2],
              EnergyFunction=ef,
              pkl=False)
    molecules = rn.initialize()
    rn.session(learning_rate=1.0e-10, method='AdamOptimizer')

    mol = 'tmp'
    anglab = rn.lk.anglab
    angs = []
    for ang in ir.angs:
        angs.append(list(ang))

    ea_ = rn.get_value(rn.EANG)
    ea = ir.eang.numpy()
    f7_ = rn.get_value(rn.f_7)
    f7 = ir.f_7.numpy()
    f8_ = rn.get_value(rn.f_8)
    f8 = ir.f_8.numpy()
    expang_ = rn.get_value(rn.expang)
    expang = ir.expang.numpy()

    theta_ = rn.get_value(rn.theta)
    theta = ir.theta.numpy()
    theta0_ = rn.get_value(rn.theta0)
    theta0 = ir.thet0.numpy()

    sbo3_ = rn.get_value(rn.SBO3)
    sbo3 = ir.SBO3.numpy()

    fa = open('ang.txt', 'w')
    for ang in rn.angs:
        for a_ in range(rn.nang[ang]):
            a = anglab[ang][a_][1:]

            if a not in angs:
                a.reverse()
            i, j, k = a
            if a in angs:
                ai = angs.index(a)
                print(
                    ' * %2d%s-%2d%s-%2d%s:' %
                    (i, ir.atom_name[i], j, ir.atom_name[j], k,
                     ir.atom_name[k]),
                    #'eang: %10.8f  %10.8f' %(ea_[ang][a_][0],ea[ai]),
                    'f7: %10.8f  %10.8f' % (f7_[ang][a_][0], f7[ai]),
                    'f8: %10.8f  %10.8f' % (f8_[ang][a_][0], f8[ai]),
                    'expang: %10.8f  %10.8f' %
                    (expang_[ang][a_][0], expang[ai]),
                    'theta: %10.8f  %10.8f' % (theta_[ang][a_][0], theta[ai]),
                    # 'sbo3: %10.8f  %10.8f' %(sbo3_[ang][a_][0],sbo3[ai]),
                    'theta0: %10.8f  %10.8f' %
                    (theta0_[ang][a_][0], theta0[ai]),
                    file=fa)
            else:
                print(
                    ' * %2d%s-%2d%s-%2d%s:' %
                    (i, ir.atom_name[i], j, ir.atom_name[j], k,
                     ir.atom_name[k]),
                    #'eang: %10.8f' %(ea_[ang][a_][0]),
                    'f7: %10.8f' % (f7_[ang][a_][0]),
                    'f8: %10.8f' % (f8_[ang][a_][0]),
                    'expang: %10.8f' % (expang_[ang][a_][0]),
                    'expang: %10.8f' % (expang_[ang][a_][0]),
                    'theta: %10.8f' % (theta_[ang][a_][0]),
                    # 'sbo3: %10.8f' %(sbo3_[ang][a_][0]),
                    'theta0: %10.8f' % (theta0_[ang][a_][0]))
    fa.close()
    epen = rn.get_value(rn.epen)
    eang = rn.get_value(rn.eang)
    print(' * penalty energy:', ir.Epen.numpy(), epen[mol][0])
    print(' * angel energy:', ir.Eang.numpy(), eang[mol][0])
예제 #6
0
파일: irffdeb.py 프로젝트: haoxy97/I-ReaxFF
def db(traj='opt.traj', batch_size=50, ef=3, nn=True, frame=40):
    ffield = 'ffield.json' if nn else 'ffield'
    images = Trajectory(traj)
    atoms = images[frame]

    e, ei = [], []
    ir = IRFF(atoms=atoms, libfile=ffield, nn=nn, autograd=False)

    ir.calculate(atoms)

    ei.append(ir.Ebond)
    ebond = ir.ebond  # .numpy()

    rn = MPNN(libfile=ffield,
              direcs={'tmp': traj},
              dft='siesta',
              opt=[],
              optword='nocoul',
              batch_size=batch_size,
              atomic=True,
              clip_op=False,
              InitCheck=False,
              nn=nn,
              bo_layer=[9, 2],
              EnergyFunction=ef,
              pkl=False)
    molecules = rn.initialize()
    rn.session(learning_rate=1.0e-10, method='AdamOptimizer')

    mol = 'tmp'
    er = rn.get_value(rn.ebond[mol])
    blab = rn.lk.bdlab

    bosi = ir.bosi.numpy()
    bopsi = ir.bop_si.numpy()
    boppi = ir.bop_pi.numpy()
    boppp = ir.bop_pp.numpy()

    bopow3 = ir.bopow3.numpy()
    eterm3 = ir.eterm3.numpy()

    bop = ir.bop.numpy()
    bo = ir.bo0.numpy()

    eterm1 = ir.eterm1.numpy()
    bopow1 = ir.bopow1.numpy()
    bodiv1 = ir.bodiv1.numpy()

    r = ir.r.numpy()
    D_ = rn.get_value(rn.Deltap)
    D = ir.Deltap.numpy()

    ebd_ = np.zeros([ir.natom, ir.natom])

    for bd in rn.bonds:
        for nb in range(rn.nbd[bd]):
            ebd = rn.get_value(rn.EBD[bd])
            mol_, i, j = blab[bd][nb]
            bosi_ = rn.get_value(rn.bosi[bd])
            bopsi_ = rn.get_value(rn.bop_si[bd])
            boppi_ = rn.get_value(rn.bop_pi[bd])
            boppp_ = rn.get_value(rn.bop_pp[bd])

            bopow3_ = rn.get_value(rn.bopow3[bd])
            eterm3_ = rn.get_value(rn.eterm3[bd])

            bop_ = rn.get_value(rn.bop[bd])
            bo_ = rn.get_value(rn.bo0[bd])

            eterm1_ = rn.get_value(rn.eterm1[bd])
            bopow1_ = rn.get_value(rn.bopow1[bd])
            bodiv1_ = rn.get_value(rn.bodiv1[bd])

            rbd = rn.get_value(rn.rbd[bd])

            ebd_[i][j] = ebd[nb][frame]
            ebd_[j][i] = ebd[nb][frame]
            # if abs(bo_[nb][0]-bo[i][j])>0.00001:
            print('-  %s %2d %2d:' % (bd, i, j),
                  'rbd %10.7f %10.7f' % (rbd[nb][frame], r[i][j]),
                  'bop %10.7f %10.7f' % (bop_[nb][frame], bop[i][j]),
                  'Di %10.7f %10.7f' % (D_[i][frame], D[i]),
                  'Dj %10.7f %10.7f' % (D_[j][frame], D[j]),
                  'bo %10.7f %10.7f' % (bo_[nb][frame], bo[i][j]),
                  'ebond %10.7f %10.7f' % (ebd[nb][frame], ebond[i][j]))

    rcbo = rn.get_value(rn.rc_bo)
    eb = rn.get_value(rn.ebond[mol])
    print('\n-  bond energy:', ir.Ebond.numpy(), eb[frame], end='\n')

    for i in range(ir.natom):
        for j in range(ir.natom):
            if abs(ebd_[i][j] - ebond[i][j]) > 0.00001:
                print('-  %s %2d %2d:' % (bd, i, j), 'rbd %10.7f' % (r[i][j]),
                      'bop %10.7f' % (bop[i][j]),
                      'Di %10.7f %10.7f' % (D_[i][frame], D[i]),
                      'Dj %10.7f %10.7f' % (D_[j][frame], D[j]),
                      'bo %10.7f' % (bo[i][j]),
                      'ebond %10.7f %10.7f' % (ebd_[i][j], ebond[i][j]))

    # print(rcbo)
    print('\n-  bond energy:', ir.Ebond.numpy(), eb[frame], end='\n')