def test_Pt_stress_cellopt():
    import numpy as np
    from numpy.testing import assert_allclose
    from ase.calculators.lammpsrun import LAMMPS
    from ase.build import bulk
    from ase.test.eam_pot import Pt_u3
    from ase.constraints import ExpCellFilter
    from ase.optimize import BFGS

    # (For now) reuse eam file stuff from other lammps test:
    pot_fn = 'Pt_u3.eam'
    f = open(pot_fn, 'w')
    f.write(Pt_u3)
    f.close()
    params = {}
    params['pair_style'] = 'eam'
    params['pair_coeff'] = ['1 1 {}'.format(pot_fn)]
    with LAMMPS(specorder=['Pt'], files=[pot_fn], **params) as calc:
        rng = np.random.RandomState(17)

        atoms = bulk('Pt') * (2, 2, 2)
        atoms.rattle(stdev=0.1)
        atoms.cell += 2 * rng.rand(3, 3)
        atoms.calc = calc

        assert_allclose(atoms.get_stress(),
                        calc.calculate_numerical_stress(atoms),
                        atol=1e-4,
                        rtol=1e-4)

        opt = BFGS(ExpCellFilter(atoms), trajectory='opt.traj')
        for i, _ in enumerate(opt.irun(fmax=0.05)):
            pass

        cell1_ref = np.array([[0.16298762, 3.89912471, 3.92825365],
                              [4.21007577, 0.63362427, 5.04668170],
                              [4.42895706, 3.29171414, 0.44623618]])

        assert_allclose(np.asarray(atoms.cell),
                        cell1_ref,
                        atol=1e-4,
                        rtol=1e-4)
        assert_allclose(atoms.get_stress(),
                        calc.calculate_numerical_stress(atoms),
                        atol=1e-4,
                        rtol=1e-4)

        assert i < 80, 'Expected 59 iterations, got many more: {}'.format(i)
Beispiel #2
0
def test_Ar_minimize_multistep():
    from ase.calculators.lammpsrun import LAMMPS
    from ase.cluster.icosahedron import Icosahedron
    from ase.data import atomic_numbers, atomic_masses
    from numpy.testing import assert_allclose

    ar_nc = Icosahedron('Ar', noshells=2)
    ar_nc.cell = [[300, 0, 0], [0, 300, 0], [0, 0, 300]]
    ar_nc.pbc = True

    params = {}
    params['pair_style'] = 'lj/cut 8.0'
    params['pair_coeff'] = ['1 1 0.0108102 3.345']
    params['masses'] = ['1 {}'.format(atomic_masses[atomic_numbers['Ar']])]

    with LAMMPS(specorder=['Ar'], **params) as calc:
        ar_nc.calc = calc
        F1_numer = calc.calculate_numerical_forces(ar_nc)

        assert_allclose(ar_nc.get_potential_energy(),
                        -0.468147667942117,
                        atol=1e-4,
                        rtol=1e-4)
        assert_allclose(ar_nc.get_forces(), F1_numer, atol=1e-4, rtol=1e-4)

        params['minimize'] = '1.0e-15 1.0e-6 2000 4000'  # add minimize
        calc.parameters = params

        # set_atoms=True to read final coordinates after minimization
        calc.run(set_atoms=True)

        # get final coordinates after minimization
        ar_nc.set_positions(calc.atoms.positions)

        assert_allclose(ar_nc.get_potential_energy(),
                        -0.4791815887032201,
                        atol=1e-4,
                        rtol=1e-4)
        assert_allclose(ar_nc.get_forces(),
                        calc.calculate_numerical_forces(ar_nc),
                        atol=1e-4,
                        rtol=1e-4)
Beispiel #3
0
def relaxBend(bend, left_idxs, right_idxs, edge, bond, mdrelax):

    constraints = []
    constraints.append(FixAtoms(indices=left_idxs))
    constraints.append(FixAtoms(indices=right_idxs))
    #twist       =   twistConst_Rod(bend, 1, edge, bond ,F = 20)
    #twist.set_angle(np.pi/3 + 2./180*np.pi)
    #constraints.append(twist)

    add_pot = LJ_potential_smooth(bend, bond)
    constraints.append(add_pot)

    calc = LAMMPS(parameters=get_lammps_params())
    bend.set_calculator(calc)
    # END CALCULATOR

    # RELAX
    bend.set_constraint(constraints)
    dyn = BFGS(bend, trajectory=mdrelax)
    dyn.run(fmax=0.05)
Beispiel #4
0
def all_lmp():

    all_lmp_dict = {}
    species = ["H", "He"]
    specie_symbol_list = " ".join(species)
    masses = [
        f"{i} {_Z_to_mass[_element_to_Z[species[i]]]}"
        for i in range(len(species))
    ]
    parameters = {
        "command": os.environ.get("lmp"),  # set up executable for ASE
        "newton": "off",
        "pair_style": "mgp",
        "mass": masses,
    }

    # set up input params
    for bodies in body_list:
        for multihyps in multi_list:
            # create ASE calc
            label = f"{bodies}{multihyps}"
            files = [f"{label}.mgp"]
            by = "yes" if bodies == "2" else "no"
            ty = "yes" if bodies == "3" else "no"
            parameters["pair_coeff"] = [
                f"* * {label}.mgp {specie_symbol_list} {by} {ty}"
            ]

            lmp_calc = LAMMPS(
                label=label,
                keep_tmp_files=True,
                tmp_dir="./tmp/",
                parameters=parameters,
                files=files,
                specorder=species,
            )
            all_lmp_dict[f"{bodies}{multihyps}"] = lmp_calc

    yield all_lmp_dict
    del all_lmp_dict
def test_Ar_minimize():
    from ase.calculators.lammpsrun import LAMMPS
    from ase.cluster.icosahedron import Icosahedron
    from ase.data import atomic_numbers, atomic_masses
    from numpy.testing import assert_allclose
    from ase.optimize import LBFGS

    ar_nc = Icosahedron('Ar', noshells=2)
    ar_nc.cell = [[300, 0, 0], [0, 300, 0], [0, 0, 300]]
    ar_nc.pbc = True

    params = {}
    params['pair_style'] = 'lj/cut 8.0'
    params['pair_coeff'] = ['1 1 0.0108102 3.345']
    params['masses'] = ['1 {}'.format(atomic_masses[atomic_numbers['Ar']])]

    with LAMMPS(specorder=['Ar'], **params) as calc:
        ar_nc.calc = calc

        assert_allclose(ar_nc.get_potential_energy(),
                        -0.468147667942117,
                        atol=1e-4,
                        rtol=1e-4)
        assert_allclose(ar_nc.get_forces(),
                        calc.calculate_numerical_forces(ar_nc),
                        atol=1e-4,
                        rtol=1e-4)

        dyn = LBFGS(ar_nc, force_consistent=False)
        dyn.run(fmax=1E-6)

        assert_allclose(ar_nc.get_potential_energy(),
                        -0.4791815886953914,
                        atol=1e-4,
                        rtol=1e-4)
        assert_allclose(ar_nc.get_forces(),
                        calc.calculate_numerical_forces(ar_nc),
                        atol=1e-4,
                        rtol=1e-4)
Beispiel #6
0
def test_no_data_file_wrap():
    """
    If 'create_atoms' hasn't been given the appropriate 'remap yes' option,
    atoms falling outside of a periodic cell are not actually created.  The
    lammpsrun calculator will then look at the thermo output and determine a
    discrepancy the number of atoms reported compared to the length of the
    ASE Atoms object and raise a RuntimeError.  This problem can only
    possibly arise when the 'no_data_file' option for the calculator is set
    to True.  Furthermore, note that if atoms fall outside of the box along
    non-periodic dimensions, create_atoms is going to refuse to create them
    no matter what, so you simply can't use the 'no_data_file' option if you
    want to allow for that scenario.
    """
    from ase.atoms import Atoms
    from ase.calculators.lammpsrun import LAMMPS

    # Make a periodic box and put one atom outside of it
    pos = [[0.0, 0.0, 0.0], [-2.0, 0.0, 0.0]]
    atoms = Atoms(symbols=["Ar"] * 2,
                  positions=pos,
                  cell=[10.0, 10.0, 10.0],
                  pbc=True)

    # Set parameters for calculator
    params = {}
    params["pair_style"] = "lj/cut 8.0"
    params["pair_coeff"] = ["1 1 0.0108102 3.345"]

    # Don't write a data file string. This will force
    # ase.calculators.lammps.inputwriter.write_lammps_in to write a bunch of
    # 'create_atoms' commands into the LAMMPS input file
    params["no_data_file"] = True

    with LAMMPS(specorder=["Ar"], **params) as calc:
        atoms.calc = calc
        atoms.get_potential_energy()
Beispiel #7
0
def shearDyn(width, ratio, edge, save=False, force_dir_update=True):

    atoms, L, W, length_int, b_idxs = create_stucture(ratio,
                                                      width,
                                                      edge,
                                                      key='rib+base')

    view(atoms)
    # FIXES
    constraints, add_LJ, rend_b, rend_t = get_constraints(
        atoms, edge, bond, b_idxs)
    # END FIXES

    # CALCULATOR LAMMPS
    calc = LAMMPS(parameters=get_lammps_params())
    atoms.set_calculator(calc)
    # END CALCULATOR

    # TRAJECTORY
    add = ''
    if force_dir_update: add = '_ff'
    mdfile, mdlogfile, mdrelax, simulfile   =   get_fileName('LJ', edge + add, width, \
                                                 length_int, int(T), taito)

    if save: traj = PickleTrajectory(mdfile, 'w', atoms)
    else: traj = None

    #data    =   np.zeros((M/interval, 5))

    # RELAX
    atoms.set_constraint(add_LJ)
    dyn = BFGS(atoms, trajectory=mdrelax)
    dyn.run(fmax=0.05)

    # FIX AFTER RELAXATION
    atoms.set_constraint(constraints)

    # DYNAMICS
    dyn = Langevin(atoms, dt * units.fs, T * units.kB, fric)
    n = 0
    header = '#t [fs], shift y [Angstrom], Rad, epot_tot [eV], ekin_tot [eV], etot_tot [eV] \n'
    write_line_own(mdlogfile, header, 'w')

    if T != 0:
        # put initial MaxwellBoltzmann velocity distribution
        mbd(atoms, T * units.kB)

    deltaY = 0.
    eta = 1.1  # ratio between the lengths of the streched and compressed side of the ribbon.
    r = L / W  # length to width ratio.
    deltaYMax = W / 2. * (eta + 1) / (eta - 1) * (1 - np.cos(2 * r *
                                                             (eta - 1) /
                                                             (eta + 1)))

    # Estimate for the required shift
    dy = v * dt
    M = deltaYMax / dy
    interval = int(M / 1000)

    kink_formed = False
    dir_vec = np.array([0., 1., 0.])
    i, m = 0, 0
    print 'width = %i, length = %i' % (width, length_int)
    while m <= int(M / 30):

        if tau < i * dt:
            deltaY += dy * dir_vec[1]
            for ind in rend_b:
                atoms[ind].position[:] += dy * dir_vec

        dyn.run(1)

        if i % interval == 0:

            epot, ekin = saveAndPrint(atoms, traj, False)[:2]

            R = get_R(L, deltaY)
            if force_dir_update: dir_vec = get_dir(atoms, rend_b, rend_t)
            data = [i * dt, deltaY, R, epot, ekin, epot + ekin]

            if save:
                stringi = ''
                for k, d in enumerate(data):
                    if k == 0:
                        stringi += '%.2f ' % d
                    elif k == 1 or k == 2:
                        stringi += '%.4f ' % d
                    else:
                        stringi += '%.12f ' % d
                write_line_own(mdlogfile, stringi + '\n', 'a')

            n += 1

            if thres_Z < np.max(atoms.positions[:, 2]) and m == 0:
                idxs = np.where(thres_Z < atoms.positions[:, 2])
                write_line_own(mdlogfile,
                               '# Kink! at idxs %s' % str(idxs) + '\n', 'a')
                print ' kink formed! '
                kink_formed = True

        if save and T != 0 and i * dt == tau:
            write_line_own(mdlogfile, '# Thermalization complete. ' + '\n',
                           'a')

        if i % int(M / 100) == 0: print str(i / int(M / 100)) + ' ~ % done'
        if kink_formed: m += 1
        i += 1


    make_simul_param_file(simulfile, W, L, width, length_int, v, dy, T, \
                          dt, fric, thres_Z, interval, deltaY, M, edge)
# https://physics.stackexchange.com/questions/250018

pair_style = 'buck/coul/long 12.0'
pair_coeff = ['1 1 3796.9 0.2603 124.90']
pair_coeff += ['2 2 1227.2 0.3214 124.90']
pair_coeff += ['1 2 4117.9 0.3048 0.0']
masses = [
    '1 {}'.format(atomic_masses[atomic_numbers['Na']]),
    '2 {}'.format(atomic_masses[atomic_numbers['Cl']])
]

calc = LAMMPS(
    specorder=['Na', 'Cl'],
    pair_style=pair_style,
    pair_coeff=pair_coeff,
    masses=masses,
    atom_style='charge',
    kspace_style='pppm 1.0e-5',
    keep_tmp_files=True,
)

for a in nacl:
    if a.symbol == 'Na':
        a.charge = +1.
    else:
        a.charge = -1.

nacl.set_calculator(calc)

E = nacl.get_potential_energy()
Beispiel #9
0
from ase.calculators.lammpsrun import LAMMPS
from ase.cluster.icosahedron import Icosahedron
from ase.data import atomic_numbers, atomic_masses
from numpy.testing import assert_allclose
from ase.optimize import LBFGS

ar_nc = Icosahedron('Ar', noshells=2)
ar_nc.cell = [[300, 0, 0], [0, 300, 0], [0, 0, 300]]
ar_nc.pbc = True

params = {}
params['pair_style'] = 'lj/cut 8.0'
params['pair_coeff'] = ['1 1 0.0108102 3.345']
params['masses'] = ['1 {}'.format(atomic_masses[atomic_numbers['Ar']])]

calc = LAMMPS(specorder=['Ar'], **params)

ar_nc.set_calculator(calc)

assert_allclose(ar_nc.get_potential_energy(),
                -0.468147667942117,
                atol=1e-4,
                rtol=1e-4)
assert_allclose(ar_nc.get_forces(),
                calc.calculate_numerical_forces(ar_nc),
                atol=1e-4,
                rtol=1e-4)

dyn = LBFGS(ar_nc, force_consistent=False)
dyn.run(fmax=1E-6)
Beispiel #10
0
from ase.io import read, write
import numpy as np
from ase.calculators.lammpsrun import LAMMPS
from ase import units
from ase.stressbox import stressbox
import math

parameters = {
    'units': 'metal',
    'atom_style': 'atomic',
    'boundary': 'p p p',
    'pair_style': 'sw',
    'pair_coeff': ['* * Si.sw Si']
}

lmp_calc = LAMMPS(parameters=parameters)

refatom = read('si_alpha_1000atoms_sw_relax.lmpdata',
               format='lammps-data',
               style="atomic")

patom = read('si_alpha_1000atoms_random.lmpdata',
             format='lammps-data',
             style="atomic")

patom.set_calculator(lmp_calc)

pstress = patom.get_cell() * 0.0

fixstrain = np.ones((3, 3))
Beispiel #11
0
def get_YoungAndTau3():
    wis = range(
        4, 120)  #[4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,25,28,30]
    #wYt     =   np.zeros((len(wis), 4))
    path_f = '/space/tohekorh/ShearSlide/pictures/Ytau3/%s/' % edge

    wYt = np.zeros((len(wis) - len(bad_wis), 4))
    j = 0

    for wi in wis:
        if wi not in bad_wis:
            print 'width %i' % wi
            atoms, fid = get_atoms(wi)
            constraints = []
            constraints.append(FixAtoms(indices=[0]))
            for k in range(len(atoms)):
                constraints.append(FixedPlane(k, [0, 0, 1]))

            atoms.set_constraint(constraints)

            # CALCULATOR
            calc = LAMMPS(parameters=parameters)
            atoms.set_calculator(calc)

            dyn = BFGS(
                atoms,
                logfile='/space/tohekorh/log_useless.log',
                trajectory='/space/tohekorh/ShearSlide/opm_dx=%.3f_%s.traj' %
                (dx, edge))
            dyn.run(fmax=fmax, steps=maxSteps)

            dedx = derivative(energy,
                              l0,
                              dx=dx,
                              n=1,
                              order=21,
                              args=(atoms, dyn))

            tau = dedx / 2.
            cparams.set_tau(tau)
            print 'edge stress %s eV/Angst = %.3f' % (edge, tau)

            qopm_l = (-2 * tau / (21 * cparams.get_w()) + 1) * l0

            energy(qopm_l, atoms, dyn)
            init_pos = atoms.positions.copy()

            lxs_stre = np.linspace(1. * qopm_l, (1. + perVal) * qopm_l, N)
            lxs_comp = np.linspace(1. * qopm_l, (1 - perVal) * qopm_l, N)

            lxs = np.zeros(2 * N - 1)
            energies = np.zeros(2 * N - 1)

            cparams.set_w(wi)

            n = 0
            for lx in lxs_stre:
                print lx / qopm_l
                energies[n] = energy(lx, atoms, dyn)
                lxs[n] = lx
                n += 1

            atoms.positions = init_pos
            for lx in lxs_comp[1:]:
                print lx / qopm_l
                energies[n] = energy(lx, atoms, dyn)
                lxs[n] = lx
                n += 1

            eps = (lxs - l0) / l0

            atoms.positions = init_pos
            e0 = energy(l0, atoms, dyn)
            energies -= e0

            Y_opm = curve_fit(energy_teor3, eps, energies, p0=[21])[0][:1]

            l_opm = -2 * tau / (Y_opm * cparams.get_w()) * l0 + l0
            eps0 = l_opm / l0 - 1
            print 'Youngs mod eV/Angst2 = %.3f' % (Y_opm)

            plt.scatter(eps, energies)
            plt.plot(eps, 2 * tau * l0 * eps, label='tau = %.3f' % tau)
            #plt.plot(eps, dedx*eps*l0, label = 'tau = %.3f' %tau)

            plot_eps = np.linspace(np.min(eps), np.max(eps), 200)
            plt.plot(plot_eps,
                     energy_teor3(plot_eps, Y_opm),
                     '-',
                     color='black',
                     label='fit, Y = %.3f eV/angst2' % (Y_opm))

            #        plt.plot([l_opm,l_opm], [-.1*np.max(energies), .1*np.max(energies)], '--',
            #                color = 'black', label = 'eps_opm = %.4f' %(l_opm/l0 - l0))
            plt.plot([0, 0], [-.1 * np.max(energies), .1 * np.max(energies)],
                     '--',
                     color='black')
            plt.plot([eps0, eps0],
                     [-.1 * np.max(energies), .1 * np.max(energies)],
                     '--',
                     color='black',
                     label='eps0 = %.5f' % (eps0))
            plt.legend(frameon=False)
            #plt.show()
            plt.savefig(path_f + 'w=%i_dx=%.3f.png' % (wi, dx))
            plt.clf()

            wYt[j] = [cparams.get_w(), Y_opm, tau, eps0]

            plt.scatter(wYt[:j + 1, 0],
                        wYt[:j + 1, 1],
                        label='Y',
                        color='black')
            plt.legend(loc=2, frameon=False)
            plt.twinx()
            plt.plot(wYt[:j + 1, 0], wYt[:j + 1, 2], '-o', label='tau')
            plt.xlabel('width')
            plt.title('Young s modulus and edge stress')
            plt.legend(frameon=False)
            plt.savefig(path_f + 'Ytau.png')
            plt.clf()

            np.savetxt(path_f + 'Ytau.txt',
                       wYt,
                       header='width, Y, tau, eps_opm')

            #plt.show()
            j += 1
Beispiel #12
0
import os
from ase.calculators.lammpsrun import LAMMPS

os.environ[
    "ASE_LAMMPSRUN_COMMAND"] = "/Users/Cas/gits/lammps-ace/src/lmp_serial"

model_dir = os.path.dirname(os.path.realpath(__file__))

parameters = {
    'pair_style': 'pace',
    'pair_coeff': ['* * Si_B8_N4_18_07_lap_dia_1.1_rep_2B+ACE.ace Si']
}

files = [os.path.join(model_dir, "Si_B8_N4_18_07_lap_dia_1.1_rep_2B+ACE.ace")]

calculator = LAMMPS(parameters=parameters, files=files)

name = "ACE"

no_checkpoint = True
Beispiel #13
0
def corr_KC():

    atoms = graphene_nanoribbon(1,
                                1,
                                type='armchair',
                                C_C=bond,
                                saturated=False)
    atoms.rotate([1, 0, 0], np.pi / 2, rotate_cell=True)

    if edge == 'ac':
        atoms.rotate([0, 0, 1], -np.pi / 2, rotate_cell=True)
        del atoms[[0, 3]]
        trans_idx = 1
    elif edge == 'zz':
        del atoms[[1, 0]]
        trans_idx = 0

    atoms.set_cell([20, 20, 10])
    atoms.center()

    params = {}
    params['positions'] = atoms.positions
    params['chemical_symbols'] = atoms.get_chemical_symbols()
    params['ia_dist'] = 10
    params['edge'] = edge
    params['bond'] = bond
    params['ncores'] = 2
    add_KC = KC_potential_p(params, True)

    constraints = []
    for i in range(len(atoms)):
        fix_l = FixedLine(i, [0., 0., 1.])
        constraints.append(fix_l)

    constraints.append(add_KC)

    lamp_parameters = get_lammps_params(H=False)
    calc = LAMMPS(parameters=lamp_parameters)  #, files=['lammps.data'])
    atoms.set_calculator(calc)
    atoms.set_constraint(constraints)

    #dyn     =   BFGS(atoms, trajectory = 'test.traj')
    #dyn.run(fmax=0.05)

    #plot_posits(atoms, edge, bond)
    trans_vec = trans_atomsKC(atoms.positions[trans_idx], edge, bond)
    atoms.translate(trans_vec)

    #plot_posits(atoms, edge, bond)
    init_pos = atoms.positions.copy()
    r_around = init_pos[trans_idx]

    #thetas      =   np.linspace(0, np.pi/3, 7) #, endpoint = False)
    #thetas_deg  =   np.array([1,3,5,7,9,11,12,13,15,17,43,45,47,48,49,51,57,55,57,59])
    thetas_deg = np.array([
        .5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5, 11.5, 12.5,
        13.5, 14.5, 15.5, 16.5, 17.5, 18.5, 19.5
    ])

    traj        =   PickleTrajectory(path + '%s_corr_twist_thetas_(%.1f-%.1f).traj' \
                                     %(edge, np.min(thetas_deg),
                                       np.max(thetas_deg)), 'w', atoms)

    n = 100

    for i, theta_deg in enumerate(thetas_deg):
        fname = path + 'corr_%s_theta=%.2f.data' % (edge, theta_deg)
        print 'Calculating theta = %.2f' % (theta_deg)
        theta = theta_deg / 360 * np.pi * 2
        print 'time ' + str(datetime.now().time())
        atoms.positions = init_pos
        atoms.rotate([0, 0, 1], theta, center=r_around)
        rot_init_pos = atoms.positions.copy()

        lat_vec_theta1 = lat_vec1.copy()
        lat_vec_theta2 = lat_vec2.copy()

        trans_vec2 = lat_vec_theta2.copy() / n

        data = np.zeros((n, n))

        for k in range(n):
            atoms.positions = rot_init_pos
            atoms.translate(lat_vec_theta1 * float(k) / n)
            #plot_posits(atoms, edge, bond, vecs =  [lat_vec_theta1, lat_vec_theta2])
            print '%.1f percent done' % (100 * float(k) / n)
            for l in range(n):
                atoms.translate(trans_vec2)
                emin = get_optimal_h(atoms, len(atoms), dyn=False)[0]
                data[
                    k,
                    l] = emin  #atoms.get_potential_energy()/len(atoms) #emin #
                saveAndPrint(atoms, traj, False)


        header  =   '%s runs along x-dir, angle measured from x-axis, natoms = %i. x (Angs), e (eV/atom), hmin \n\
the lattice vectors are l1 = [%.5f, %.5f, %.5f] and l2 = [%.5f, %.5f, %.5f], they are divided in %i parts. data[i,j,:] \n\
-> atoms pos += l1/n*i + l2/n*j, initial position is such that atom1 is in middle if hexagon.' \
    %(edge, len(atoms), lat_vec_theta1[0], lat_vec_theta1[1], lat_vec_theta1[2], \
      lat_vec_theta2[0], lat_vec_theta2[1], lat_vec_theta2[2], n)
        np.savetxt(fname, data, header=header)
Beispiel #14
0
def shearDyn(width, ratio, edge, save=False):

    atoms, L, W, length_int, b_idxs = create_stucture(ratio,
                                                      width,
                                                      edge,
                                                      key='top')

    view(atoms)
    # FIXES
    constraints, add_LJ, twist, rend_b, rend_t = get_constraints(atoms,
                                                                 edge,
                                                                 bond,
                                                                 b_idxs,
                                                                 key='twist')
    # END FIXES

    # CALCULATOR LAMMPS
    calc = LAMMPS(parameters=get_lammps_params())
    atoms.set_calculator(calc)
    # END CALCULATOR

    # TRAJECTORY
    mdfile, mdlogfile, mdrelax, simulfile   =   get_fileName('LJ', edge + '_twist', width, \
                                                 length_int, int(T), taito)

    if save: traj = PickleTrajectory(mdfile, 'w', atoms)
    else: traj = None

    #data    =   np.zeros((M/interval, 5))

    # RELAX
    atoms.set_constraint(add_LJ)
    dyn = BFGS(atoms, trajectory=mdrelax)
    dyn.run(fmax=0.05)

    # FIX AFTER RELAXATION
    atoms.set_constraint(constraints)

    # DYNAMICS
    dyn = Langevin(atoms, dt * units.fs, T * units.kB, fric)
    header = '#t [fs], shift y [Angstrom], Rad, epot_tot [eV], \
                ekin_tot [eV], etot_tot [eV], F [eV/angst] \n'

    write_line_own(mdlogfile, header, 'w')

    if T != 0:
        # put initial MaxwellBoltzmann velocity distribution
        mbd(atoms, T * units.kB)

    y0 = atoms.positions[rend_b, 1][0]

    kink_formed = False
    dir_vec = np.array([0., 1., 0.])
    i, m = 0, 0
    interval = 20
    print 'width = %i, length = %i' % (width, length_int)

    M_therm = int(tau / dt)

    dyn.run(M_therm)
    F = 0.

    while not kink_formed:

        pos_0 = atoms.positions[rend_b][0]
        F_vec = F * np.array([dir_vec[1], -dir_vec[0], 0.])
        twist.set_F(F_vec)
        dyn.run(interval)

        i += interval
        epot, ekin = saveAndPrint(atoms, traj, False)[:2]
        pos_1 = atoms.positions[rend_b][0]
        deltaY = pos_1[1] - y0

        v = np.sign(pos_1[1] - pos_0[1]) * np.linalg.norm(
            (pos_1 - pos_0) / (interval * dt))
        R = get_R(L, deltaY)
        dir_vec = get_dir(atoms, rend_b, rend_t)
        data = [
            i * dt, deltaY, R, epot, ekin, epot + ekin, F_vec[0], F_vec[1],
            F_vec[2]
        ]

        #if      vmax/5 < v:   F   -=  .01
        if v < vmax / 5 and not kink_formed: F += .01

        print deltaY, v, F

        if save:
            stringi = ''
            for k, d in enumerate(data):
                if k == 0:
                    stringi += '%.2f ' % d
                elif k == 1 or k == 2:
                    stringi += '%.4f ' % d
                else:
                    stringi += '%.12f ' % d
            write_line_own(mdlogfile, stringi + '\n', 'a')

        if thres_Z < np.max(atoms.positions[:, 2]) and m == 0:
            idxs = np.where(thres_Z < atoms.positions[:, 2])
            write_line_own(mdlogfile, '# Kink! at idxs %s' % str(idxs) + '\n',
                           'a')
            print ' kink formed! '
            kink_formed = True
            F = 0

        if save and T != 0 and i * dt == tau:
            write_line_own(mdlogfile, '# Thermalization complete. ' + '\n',
                           'a')

        if i % 100 == 0: print i
        if kink_formed: m += 1


    make_simul_param_file(simulfile, W, L, width, length_int, v, deltaY/i, T, \
                          dt, fric, thres_Z, interval, deltaY, i, edge)
Beispiel #15
0
lammpsrun calculator will then look at the thermo output and determine a
discrepancy the number of atoms reported compared to the length of the
ASE Atoms object and raise a RuntimeError.  This problem can only
possibly arise when the 'no_data_file' option for the calculator is set
to True.  Furthermore, note that if atoms fall outside of the box along
non-periodic dimensions, create_atoms is going to refuse to create them
no matter what, so you simply can't use the 'no_data_file' option if you
want to allow for that scenario.
"""
from ase.atoms import Atoms
from ase.calculators.lammpsrun import LAMMPS

# Make a periodic box and put one atom outside of it
pos = [[0.0, 0.0, 0.0], [-2.0, 0.0, 0.0]]
atoms = Atoms(symbols=["Ar"] * 2, positions=pos, cell=[10.0, 10.0, 10.0],
              pbc=True)

# Set parameters for calculator
params = {}
params["pair_style"] = "lj/cut 8.0"
params["pair_coeff"] = ["1 1 0.0108102 3.345"]

# Don't write a data file string. This will force
# ase.calculators.lammps.inputwriter.write_lammps_in to write a bunch of
# 'create_atoms' commands into the LAMMPS input file
params["no_data_file"] = True

with LAMMPS(specorder=["Ar"], **params) as calc:
    atoms.calc = calc
    energy = atoms.get_potential_energy()
Beispiel #16
0
def corr_KC(width, edge):

    params0 = get_simulParams(edge)[-1]
    bond = params0['bond']
    '''
    atoms   =   graphene_nanoribbon(1, 1, type= 'armchair', C_C=bond, saturated = False)
    atoms.rotate([1,0,0], np.pi/2, rotate_cell = True)
    atoms.rotate([0,0,1], -np.pi/2, rotate_cell = True)
    atoms.set_cell([20, 20, 10])
    atoms.center()
    del atoms[[2,3]]
    '''
    atoms = create_stucture(2, width, edge, key='top', a=bond)[0]

    atoms.set_cell([70, 70, 20])
    atoms.center()
    atoms.positions[:, 2] = 3.4

    h_t = []
    for i in range(len(atoms)):
        if atoms[i].number == 1:
            h_t.append(i)

    del atoms[h_t]

    #view(atoms)
    params = {}
    params['positions'] = atoms.positions
    params['chemical_symbols'] = atoms.get_chemical_symbols()
    params['ia_dist'] = 10
    params['edge'] = edge
    params['bond'] = bond
    params['ncores'] = 2
    params['no_edge_neigh'] = True
    add_KC = KC_potential_p(params)

    constraints = []
    for i in range(len(atoms)):
        fix_l = FixedLine(i, [0., 0., 1.])
        constraints.append(fix_l)

    constraints.append(add_KC)

    lamp_parameters = get_lammps_params(H=False)
    calc = LAMMPS(parameters=lamp_parameters)  #, files=['lammps.data'])
    atoms.set_calculator(calc)
    atoms.set_constraint(constraints)

    #dyn     =   BFGS(atoms, trajectory = 'test.traj')
    #dyn.run(fmax=0.05)

    #plot_posits(atoms, edge, bond)

    trans_vec = trans_atomsKC(atoms.positions[0], edge, bond)
    atoms.translate(trans_vec)

    #plot_posits(atoms, edge, bond)
    #exit()
    init_pos = atoms.positions.copy()
    r_around = init_pos[1]

    thetas = np.linspace(0, np.pi / 3, 61)
    n = 15

    lat_vec1 = np.array([3. / 2 * bond, np.sqrt(3) / 2 * bond, 0.])
    lat_vec2 = np.array([3. / 2 * bond, -np.sqrt(3) / 2 * bond, 0.])

    for i, theta in enumerate(thetas):
        fname = path + 'corr_%s_theta=%.2f.data' % (edge, theta /
                                                    (2 * np.pi) * 360)
        #if not os.path.isfile(fname):
        print 'Calculating theta = %.2f' % (theta / (2 * np.pi) * 360)
        atoms.positions = init_pos
        atoms.rotate([0, 0, 1], theta, center=r_around)

        R = np.array([[np.cos(theta), -np.sin(theta), 0.],
                      [np.sin(theta), np.cos(theta), 0.], [0., 0., 1.]])

        lat_vec_theta1 = np.dot(R, lat_vec1.copy())
        lat_vec_theta2 = np.dot(R, lat_vec2.copy())

        #trans_vec1      =   lat_vec_theta1.copy()/n
        trans_vec2 = lat_vec_theta2.copy() / n

        data = np.zeros((n, n))
        #plot_posits(atoms, edge, bond, vecs =  [lat_vec_theta1, lat_vec_theta2])

        for k in range(n):
            atoms.positions = init_pos
            atoms.translate(lat_vec_theta1 * float(k) / n)
            #plot_posits(atoms, edge, bond, vecs =  [lat_vec_theta1, lat_vec_theta2])
            #print trans_vec1*float(k)/n, k, n, float(k)/n

            for l in range(n):
                atoms.translate(trans_vec2)
                emin, hmin = get_optimal_h(atoms, len(atoms), dyn=False)
                #data[k,l,:]  =   [emin, hmin]
                data[k, l] = emin  #atoms.get_potential_energy()/len(atoms)

        header  =   '%s runs along x-dir, angle measured from x-axis, natoms = %i. x (Angs), e (eV/atom), hmin \n\
the lattice vectors are l1 = [%.5f, %.5f, %.5f] and l2 = [%.5f, %.5f, %.5f], they are divided in %i parts. data[i,j,:] \n\
-> atoms pos += l1/n*i + l2/n*j, initial position is such that atom1 is in middle if hexagon.' \
    %(edge, len(atoms), lat_vec_theta1[0], lat_vec_theta1[1], lat_vec_theta1[2], \
      lat_vec_theta2[0], lat_vec_theta2[1], lat_vec_theta2[2], n)
        np.savetxt(fname, data, header=header)
Beispiel #17
0
def shearDyn(width, edge, save = False):
    
    ratio   =   8
    atoms   =   create_stucture(ratio, width, edge, key = 'top')
    
    
    # FIXES
    constraints     =   []
    top, bot        =   get_topInds(atoms)
    rend            =   get_rightInds(atoms, top)
    fix_bot         =   FixAtoms(indices = bot)
    
    view(atoms)
    constraints.append(fix_bot)
    for i in rend:
        constraints.append(FixedLine(i, (1,0,0)))
    
    # KC
    params          =   get_params(atoms)
    params['top_inds']  \
                    =   top
    add_kc          =   KC_potential_p(params)
    constraints.append(add_kc)
    # END FIXES
    
    
    
    # CALCULATOR LAMMPS 
    parameters = {'pair_style':'rebo',
                  'pair_coeff':['* * CH.airebo C H'],
                  'mass'      :['1 12.0', '2 1.0'],
                  'units'     :'metal', 
                  'boundary'  :'p p f'}
    
    calc    =   LAMMPS(parameters=parameters) 
    atoms.set_calculator(calc)
    # END CALCULATOR
    
    
    # TRAJECTORY
    mdfile, mdlogfile, mdrelax  =   get_fileName(edge, width, ratio, v, taito = False)
    
    if save:    traj    =   PickleTrajectory(mdfile, 'w', atoms)
    else:       traj    =   None
    
    #data    =   np.zeros((M/interval, 5))
    
    # RELAX
    atoms.set_constraint(add_kc)
    dyn     =   BFGS(atoms, trajectory = mdrelax)
    dyn.run(fmax=0.05)
    
    # FIX AFTER RELAXATION
    atoms.set_constraint(constraints)
    
    # DYNAMICS
    dyn     =   Langevin(atoms, dt*units.fs, T*units.kB, fric)
    n       =   0
    header  =   '#t [fs], d [Angstrom], epot_tot [eV], ekin_tot [eV], etot_tot [eV] \n'
    log_f   =   open(mdlogfile, 'w')
    log_f.write(header)            
    log_f.close()

    if T != 0:
        # put initial MaxwellBoltzmann velocity distribution
        mbd(atoms, T*units.kB)
    
    
    for i in range(0, M):
        
        if tau < i*dt:
            hw   =   i*dy
            for ind in rend:
                atoms[ind].position[1] += dy 
        
        dyn.run(1)
        
        if i%interval == 0:

            epot, ekin  =   saveAndPrint(atoms, traj, False)[:2]
            
            if T != 0:
                if tau < i*dt:  hw   =   i*dy - tau*v
                else: hw =   0
            else:   hw = i*dy
                
            data        =   [i*dt, hw, epot, ekin, epot + ekin]
            
            if save:
                log_f   =   open(mdlogfile, 'a')
                stringi =   ''
                for k,d in enumerate(data):
                    if k == 0:           
                        stringi += '%.2f ' %d
                    elif k == 1:
                        stringi += '%.6f ' %d
                    else:
                        stringi += '%.12f ' %d
                
                log_f.write(stringi +  '\n')
                log_f.close()
                  

            n += 1
        
        
        if save and T != 0 and i*dt == tau:
            log_f   =   open(mdlogfile, 'a')
            log_f.write('# Thermalization complete. ' +  '\n')
            log_f.close()
            
            
        if 1e2 <= M:    
            if i%(int(M/100)) == 0: print 'ready = %.1f' %(i/(int(M/100))) + '%' 
from ase.test.eam_pot import Pt_u3
from ase.build import fcc111
import os

pot_fn = 'Pt_u3.eam'
f = open(pot_fn, 'w')
f.write(Pt_u3)
f.close()

slab = fcc111('Pt', size=(10, 10, 5), vacuum=30.0)

params = {}
params['pair_style'] = 'eam'
params['pair_coeff'] = ['1 1 {}'.format(pot_fn)]

calc = LAMMPS(specorder=['Pt'], parameters=params, files=[pot_fn])
slab.set_calculator(calc)
E = slab.get_potential_energy()
F = slab.get_forces()

assert abs(E - -2758.63) < 1E-2
assert abs(norm(F) - 11.3167) < 1E-4
assert abs(norm(slab.positions) - 955.259) < 1E-3

params['group'] = [
    'lower_atoms id ' +
    ' '.join([str(i + 1) for i, tag in enumerate(slab.get_tags()) if tag >= 4])
]
params['fix'] = ['freeze_lower_atoms lower_atoms setforce 0.0 0.0 0.0']
params['run'] = 100
params['timestep'] = 0.0005
Beispiel #19
0
def get_YoungAndTau2():

    wis = range(5, 120)

    path_f = '/space/tohekorh/ShearSlide/pictures/Ytau2/%s/' % edge
    wYt = np.zeros((len(wis) - len(bad_wis), 5))
    j = 0
    fa, fb = 0.09, 0

    for wi in wis:
        if wi not in bad_wis:
            print wi, edge
            atoms, fid = get_atoms(wi)
            constraints = []
            constraints.append(FixAtoms(indices=[0]))
            for k in range(len(atoms)):
                constraints.append(FixedPlane(k, [0, 0, 1]))
            #constraints.append(FixedPlane(fid[0], [0,0,1]))
            #constraints.append(FixedPlane(fid[1], [0,0,1]))

            atoms.set_constraint(constraints)

            # CALCULATOR
            calc = LAMMPS(parameters=parameters)
            atoms.set_calculator(calc)

            dyn = BFGS(
                atoms,
                trajectory='/space/tohekorh/ShearSlide/opm_dx=%.3f_%s.traj' %
                (dx, edge),
                maxstep=.01,
                logfile='/space/tohekorh/log_useless.log')
            dyn.run(fmax=fmax, steps=10 * maxSteps)
            cparams.set_w(wi)

            #derivative(energy, l0, dx=dx, n=1, order=5)

            #opm_l   =   fmin(energy, l0, xtol = 1e-6, ftol = 1e-6, args = (atoms, dyn))[0]
            cont = True
            cont = False
            try:
                #opm_l   =   fmin_cg(energy, l0, args=(atoms, dyn), gtol=1e-03,
                #                    epsilon=1.4901161193847656e-03,
                #                    full_output=0, disp=1)
                #qopm_l  =   (-2*(-.2)/(21*cparams.get_w()) + 1)*l0
                qopm_l = oneOverR(cparams.get_w(), fa, fb) * l0 + l0
                #qopm_l  =   l0

                res = minimize(energy, [qopm_l],
                               args=(atoms, dyn),
                               method='L-BFGS-B',
                               jac=None,
                               bounds=[(l0 * .995, l0 * 1.005)],
                               tol=None,
                               callback=None,
                               options={
                                   'disp': None,
                                   'iprint': -1,
                                   'gtol': 1e-012 * wi,
                                   'eps': 1e-04,
                                   'maxiter': 15,
                                   'ftol': 2.220446049250313e-09,
                                   'maxcor': 10,
                                   'maxfun': 100
                               })  #['x'][0] 'factr': 10000
                '''                
                options={'disp': None, 'iprint': -1, 
                                              'gtol': 1e-03*wi, 'eps': 1e-04, 
                                              'maxiter': 15,
                                              'ftol': 2.220446049250313e-09, 
                                              'maxcor': 10, 
                                              'maxfun': 100})
                '''
                opm_l = res['x'][0]
                eopm = res['fun']
                print 'number of iterations = %i' % res['nit']
                init_pos = atoms.positions.copy()

                cont = True
            except Exception as e:
                print e

            if cont:
                eps0 = (opm_l - l0) / l0
                print wi, opm_l, l0, opm_l - l0, eps0
                #qopm_l  =   (-2*(-.2)/(21*cparams.get_w()) + 1)*l0

                lxs_stre = np.linspace(1. * opm_l, (1. + perVal) * opm_l, N)
                lxs_comp = np.linspace(1. * opm_l, (1 - perVal) * opm_l, N)

                lxs = np.zeros(2 * N - 1)

                energies = np.zeros(2 * N - 1)

                cparams.set_eps0(eps0)

                n = 0
                for lx in lxs_stre:
                    print lx / opm_l
                    energies[n] = energy(lx, atoms, dyn)
                    lxs[n] = lx
                    n += 1

                atoms.positions = init_pos
                for lx in lxs_comp[1:]:
                    print lx / opm_l
                    energies[n] = energy(lx, atoms, dyn)
                    lxs[n] = lx
                    n += 1

                #min_e   =   energy(opm_l, atoms, dyn)
                energies = energies - np.min(energies)  #- min_e
                eps = (lxs - l0) / l0

                Y_opm = curve_fit(energy_teor2, eps, energies, p0=[21])[0][:1]
                tau = -Y_opm * cparams.get_w() * eps0 / 2
                tau2 = (eopm - energy(l0, atoms, dyn)) / (eps0 * l0)
                plt.scatter(eps, energies)
                plt.plot([eps0, eps0],
                         [-.1 * np.max(energies), .1 * np.max(energies)],
                         '--',
                         color='black',
                         label='eps_opm = %.4f' % eps0)

                plot_eps = np.linspace(np.min(eps), np.max(eps), 200)
                plt.plot(
                    plot_eps,
                    energy_teor(plot_eps, Y_opm, eps0),
                    '-',
                    color='black',
                    label='fit, Y = %.3f eV/angst2, \n tau = %.3f eV/Angst' %
                    (Y_opm, tau))
                plt.legend(frameon=False)
                plt.xlabel('eps')
                plt.ylabel('energy eV')
                plt.title('Energy and sheet elasticity fit to it')

                wYt[j] = [cparams.get_w(), Y_opm, tau, eps0, tau2]
                plt.savefig(path_f + 'w=%i.png' % wi)
                plt.clf()

                #plt.show()
                if j > 2:
                    fa, fb = curve_fit(oneOverR,
                                       wYt[:j, 0],
                                       wYt[:j, 3],
                                       p0=[1, 0])[0][:2]
                    #if j%5 == 0:
                    #    plt.scatter(wYt[:j,0], wYt[:j,3])
                    #    plt.plot(wYt[:j,0], oneOverR(wYt[:j,0], fa, fb))
                    #    plt.show()
                    #print fa, fb

                plt.scatter(wYt[:j + 1, 0],
                            wYt[:j + 1, 1],
                            label='Y',
                            color='black')
                plt.legend(loc=2, frameon=False)
                plt.twinx()
                plt.plot(wYt[:j + 1, 0], wYt[:j + 1, 2], '-o', label='tau')
                plt.plot(wYt[:j + 1, 0], wYt[:j + 1, 4], '-o', label='tau2')
                plt.xlabel('width')
                plt.title('Young s modulus and edge stress')
                plt.legend(frameon=False)
                plt.savefig(path_f + 'Ytau.png')
                plt.clf()

                #np.savetxt(path_f  + 'Ytau.txt', wYt,
                #           header = 'width, Y, tau, eps_opm')

                j += 1
Beispiel #20
0
def get_YoungAndTau():

    wis = range(5, 120)
    wYt = np.zeros((len(wis) - len(bad_wis), 4))
    j = 0

    for wi in wis:
        if wi not in bad_wis:
            print wi, edge
            atoms, fid = get_atoms(wi)
            constraints = []
            constraints.append(FixAtoms(indices=[0]))
            for k in range(len(atoms)):
                constraints.append(FixedPlane(k, [0, 0, 1]))
            #constraints.append(FixedPlane(fid[0], [0,0,1]))
            #constraints.append(FixedPlane(fid[1], [0,0,1]))

            atoms.set_constraint(constraints)

            # CALCULATOR
            calc = LAMMPS(parameters=parameters)
            atoms.set_calculator(calc)

            dyn = BFGS(
                atoms,
                trajectory='/space/tohekorh/ShearSlide/opm_dx=%.3f_%s.traj' %
                (dx, edge),
                maxstep=.01,
                logfile='/space/tohekorh/log_useless.log')
            dyn.run(fmax=fmax, steps=10 * maxSteps)

            qopm_l = (-2 * (-.2) / (21 * cparams.get_w()) + 1) * l0

            energy(qopm_l, atoms, dyn)
            init_pos = atoms.positions.copy()

            lxs_stre = np.linspace(1. * qopm_l, (1. + perVal) * qopm_l, N)
            lxs_comp = np.linspace(1. * qopm_l, (1 - perVal) * qopm_l, N)

            lxs = np.zeros(2 * N - 1)
            energies = np.zeros(2 * N - 1)

            cparams.set_w(wi)

            n = 0
            for lx in lxs_stre:
                print lx / qopm_l
                energies[n] = energy(lx, atoms, dyn)
                lxs[n] = lx
                n += 1

            atoms.positions = init_pos
            for lx in lxs_comp[1:]:
                print lx / qopm_l
                energies[n] = energy(lx, atoms, dyn)
                lxs[n] = lx
                n += 1

            energies = energies - np.min(energies)
            eps = (lxs - l0) / l0

            Y_opm, eps0 = curve_fit(energy_teor,
                                    eps,
                                    energies,
                                    p0=[21, qopm_l / l0 - 1])[0][:2]
            tau = -Y_opm * cparams.get_w() * eps0 / 2.

            plt.scatter(eps, energies)
            plt.plot([eps0, eps0],
                     [-.1 * np.max(energies), .1 * np.max(energies)],
                     '--',
                     color='black',
                     label='eps_opm = %.4f' % eps0)

            plot_eps = np.linspace(np.min(eps), np.max(eps), 200)
            plt.plot(plot_eps,
                     energy_teor(plot_eps, Y_opm, eps0),
                     '-',
                     color='black',
                     label='fit, Y = %.3f eV/angst2, \n tau = %.3f eV/Angst' %
                     (Y_opm, tau))
            plt.legend(frameon=False)
            plt.xlabel('eps')
            plt.ylabel('energy eV')
            plt.title('Energy and sheet elasticity fit to it')

            wYt[j] = [cparams.get_w(), Y_opm, tau, eps0]
            plt.savefig(
                '/space/tohekorh/ShearSlide/pictures/Ytau/%s/w=%i.png' %
                (edge, wi))
            plt.clf()

            #plt.show()

            plt.scatter(wYt[:j + 1, 0],
                        wYt[:j + 1, 1],
                        label='Y',
                        color='black')
            plt.legend(loc=2, frameon=False)
            plt.twinx()
            plt.plot(wYt[:j + 1, 0], wYt[:j + 1, 2], '-o', label='tau')
            plt.xlabel('width')
            plt.title('Young s modulus and edge stress')
            plt.legend(frameon=False)
            plt.savefig(
                '/space/tohekorh/ShearSlide/pictures/Ytau/%s/Ytau.png' % edge)
            plt.clf()

            np.savetxt('/space/tohekorh/ShearSlide/pictures/Ytau/%s/Ytau.txt' %
                       edge,
                       wYt,
                       header='width, Y, tau, eps_opm')

            j += 1
Beispiel #21
0
 def calc(self, **kwargs):
     from ase.calculators.lammpsrun import LAMMPS
     return LAMMPS(command=self.executable, **kwargs)
Beispiel #22
0
def runAndStudy(params_set, pot_key, save=False):

    bond = params_set['bond']
    T = params_set['T']
    taito = params_set['taito']
    dt, fric = params_set['dt'], params_set['fric']
    tau = params_set['tau']
    width = params_set['width']
    ratio = params_set['ratio']
    edge = params_set['edge']
    ncores = params_set['ncores']
    Ld_i = params_set['Ldilde_i']

    bend, straight, [matchL_idx, matchR_idx, vec], [L_bend, L_straight], [left_idxs, right_idxs]\
            =   create_bend_stucture(width, ratio, Ld_i, edge, bond)

    mdfile, mdlogfile, mdrelax, simulfile, folder, relaxed \
            =   get_fileName(pot_key, edge + '_corrStick', width, \
                             L_bend, L_straight, int(T), taito, key = 'corrStick')

    if relaxed:
        bend = PickleTrajectory(mdrelax, 'r')[-1]
    else:
        relaxBend(bend, left_idxs, right_idxs, edge, bond, mdrelax)
        bend.set_constraint([])

    shift_v = -straight.positions[matchR_idx] + (bend.positions[matchL_idx] +
                                                 vec)
    straight.translate(shift_v)

    atoms = bend + straight
    cell = [1.5 * (L_bend + L_straight), L_bend + L_straight, 20]
    atoms.set_cell(cell)
    atoms.positions[:, 2] = 3.4

    trans_vec = trans_atomsKC(straight.positions[matchR_idx], edge, bond)
    atoms.translate(trans_vec)

    #plot_posits(atoms, edge, bond)

    if edge == 'ac':
        nx = int((cell[0] / 5 - np.min(atoms.positions[:, 0])) / (3 * bond))
        ny = int((cell[1] / 5 - np.min(atoms.positions[:, 1])) /
                 (np.sqrt(3) * bond))
        atoms.translate([nx * 3. * bond, ny * np.sqrt(3) * bond, 0])
        width_f = np.sqrt(3) / 2. * bond * (width - 1)
    elif edge == 'zz':
        nx = int((cell[0] / 5 - np.min(atoms.positions[:, 0])) /
                 (np.sqrt(3) * bond))
        ny = int((cell[1] / 5 - np.min(atoms.positions[:, 1])) / (3 * bond))
        atoms.translate([nx * np.sqrt(3) * bond, ny * 3 * bond, 0])
        width_f = (3. / 2. * width - 1) * bond

    cminx, cmaxx = strip_Hend(atoms, 'right')
    left_b = get_idxOfEnds(atoms, cminx, cmaxx)[0]

    # CONSTRAINTS
    constraints = []
    constraints.append(FixAtoms(indices=left_b))

    params = {}
    params['positions'] = atoms.positions
    params['chemical_symbols'] = atoms.get_chemical_symbols()
    params['ia_dist'] = 10
    params['edge'] = edge
    params['bond'] = bond
    params['ncores'] = ncores
    add_pot = KC_potential_p(params)
    constraints.append(add_pot)
    atoms.set_constraint(constraints)
    ##

    # CALCULATOR
    calc = LAMMPS(parameters=get_lammps_params())
    atoms.set_calculator(calc)
    ##

    # DYNAMICS
    dyn = Langevin(atoms, dt * units.fs, T * units.kB, fric)
    header = '#t [fs], shift y [Angstrom], Rad, theta [rad], hmax [A], epot_tot [eV], ekin_tot [eV], etot_tot [eV], F [eV/angst] \n'
    write_line_own(mdlogfile, header, 'w')
    traj = PickleTrajectory(mdfile, 'w', atoms)

    if T != 0:
        # put initial MaxwellBoltzmann velocity distribution
        mbd(atoms, T * units.kB)
    ####

    # SIMULATION PARAMS
    nframes = 1000
    M = int(20 * tau / dt)
    interval = int(M / nframes)
    thres_cancel = 2 * bond
    stick = 'True'
    xmax_idx = np.where(atoms.positions[:,
                                        0] == np.max(atoms.positions[:,
                                                                     0]))[0][0]
    r_init = atoms.positions[xmax_idx].copy()

    R = L_bend / np.pi * 3.
    print '# data_line: width, length bend, length tail, tail/bend, theta'
    print width_f, L_bend, L_straight, L_straight / L_bend, width_f / (2 * R)
    # SIMULATION LOOP
    for i in range(nframes):

        print float(i) / nframes * 100.
        dyn.run(interval)

        epot, ekin = saveAndPrint(atoms, traj, False)[:2]
        data = [i * interval * dt, epot, ekin, epot + ekin]

        if save:
            stringi = ''
            for k, d in enumerate(data):
                if k == 0:
                    stringi += '%.2f ' % d
                elif k == 1 or k == 2:
                    stringi += '%.4f ' % d
                else:
                    stringi += '%.12f ' % d
            write_line_own(mdlogfile, stringi + '\n', 'a')

        #print np.linalg.norm(atoms.positions[xmax_idx] - r_init)
        if thres_cancel < np.linalg.norm(atoms.positions[xmax_idx] - r_init):
            stick = 'false'
            break

    make_stick_simul_param_file(simulfile, width, L_bend, L_straight, T, \
                                dt, fric, interval, M, edge, stick)

    return stick == 'True'
Beispiel #23
0
def corr_KC(width, edge):
    #width   =   5
    #edge    =   'ac'
    params0 = get_simulParams(edge)[-1]
    bond = params0['bond']

    atoms = create_stucture(1, width, edge, key='top', a=bond)[0]

    atoms.set_cell([40, 40, 20])
    atoms.center()
    atoms.positions[:, 2] = 3.4

    h_t = []
    for i in range(len(atoms)):
        if atoms[i].number == 1:
            h_t.append(i)

    del atoms[h_t]

    params = {}
    params['positions'] = atoms.positions
    params['chemical_symbols'] = atoms.get_chemical_symbols()
    params['ia_dist'] = 10
    params['edge'] = edge
    params['bond'] = bond
    params['ncores'] = 2
    add_KC = KC_potential_p(params)

    constraints = []
    for i in range(len(atoms)):
        fix_l = FixedLine(i, [0., 0., 1.])
        constraints.append(fix_l)

    constraints.append(add_KC)

    lamp_parameters = get_lammps_params(H=False)
    calc = LAMMPS(parameters=lamp_parameters)  #, files=['lammps.data'])
    atoms.set_calculator(calc)
    atoms.set_constraint(constraints)

    #dyn     =   BFGS(atoms, trajectory = 'test.traj')
    #dyn.run(fmax=0.05)

    trans_vec = trans_atomsKC(atoms.positions[0], edge, bond)
    atoms.translate(trans_vec)

    init_pos = atoms.positions.copy()
    middle = [
        np.average(init_pos[:, 0]),
        np.average(init_pos[:, 1]),
        np.average(init_pos[:, 2])
    ]

    thetas = np.linspace(np.pi / 2, np.pi, 91)
    L = 4 * bond
    ds = .05
    n = int(L / ds)

    for i, theta in enumerate(thetas):
        fname = path + 'corr_w=%02d_%s_theta=%.2f.data' % (width, edge, theta /
                                                           (2 * np.pi) * 360)
        if not os.path.isfile(fname):
            print 'Calculating w=%i, theta = %.2f' % (width, theta /
                                                      (2 * np.pi) * 360)

            atoms.positions = init_pos
            atoms.rotate([0, 0, 1], theta, center=middle)
            trans_vec = np.array([-np.sin(theta), np.cos(theta), 0])
            data = np.zeros((n, 3))

            for j in range(n):
                atoms.translate(ds * trans_vec)
                emin, hmin = get_optimal_h(atoms, len(atoms), dyn=False)
                data[j, :] = [j * ds, emin, hmin]
                #plot_posits(atoms, edge, bond)

            header = '%s runs along x-dir, angle measured from x-axis, natoms = %i. x (Angs), e (eV/atom), hmin' % (
                edge, len(atoms))
            np.savetxt(fname, data, header=header)
Beispiel #24
0
def shearDyn(params_set, pot_key, save=False):

    bond = params_set['bond']
    T = params_set['T']
    taito = params_set['taito']
    dt, fric = params_set['dt'], params_set['fric']
    tau = params_set['tau']
    vmax = params_set['vmax']
    vMAX = params_set['vMAX']
    thres_Z = params_set['thresZ']
    width = params_set['width']
    ratio = params_set['ratio']
    edge = params_set['edge']



    atoms, L, W, length_int, b_idxs     =   \
            create_stucture(ratio, width, edge, key = 'top', a = bond)

    mdfile, mdlogfile, mdrelax, simulfile, folder, relaxed \
                        =   get_fileName(pot_key, edge + '_twistRod', width, \
                                        length_int, vmax * 1000, int(T), taito)

    #view(atoms)
    # FIXES
    constraints, add_pot, twist, rend_b, rend_t =   \
            get_constraints(atoms, edge, bond, b_idxs, \
                            key = 'twist_p', pot = pot_key)
    # END FIXES

    if relaxed:
        atoms = PickleTrajectory(mdrelax, 'r')[-1]
    else:
        trans = trans_atomsKC(atoms.positions[rend_b], edge, bond)
        atoms.translate(trans)

    #plot_posits(atoms, edge, bond)

    # CALCULATOR LAMMPS
    calc = LAMMPS(parameters=get_lammps_params())
    atoms.set_calculator(calc)
    # END CALCULATOR

    # TRAJECTORY
    if save: traj = PickleTrajectory(mdfile, 'w', atoms)
    else: traj = None

    #data    =   np.zeros((M/interval, 5))

    # RELAX
    atoms.set_constraint(add_pot)
    dyn = BFGS(atoms, trajectory=mdrelax)
    dyn.run(fmax=0.05)

    dist = np.linalg.norm(atoms.positions[rend_b] - atoms.positions[rend_t])
    twist.set_dist(dist)
    # FIX AFTER RELAXATION
    atoms.set_constraint(constraints)

    # DYNAMICS
    dyn = Langevin(atoms, dt * units.fs, T * units.kB, fric)
    header = '#t [fs], shift y [Angstrom], Rad, theta [rad], hmax [A], epot_tot [eV], ekin_tot [eV], etot_tot [eV], F [eV/angst] \n'
    write_line_own(mdlogfile, header, 'w')

    if T != 0:
        # put initial MaxwellBoltzmann velocity distribution
        mbd(atoms, T * units.kB)

    y0 = atoms.positions[rend_b, 1]

    kink_formed = False
    kink_vanished = False
    i = 0
    print 'width = %i, length = %i, v=%.6f' % (width, length_int, vmax)

    M_therm = int(tau / dt)
    dyn.run(M_therm)

    M = int(2 * L / (np.pi * dt * vmax))
    M_min = int(2 * L / (np.pi * dt * vMAX))
    dtheta = np.pi / 2 / M
    dtheta_max = np.pi / 2 / M_min

    interval = int(M / 1000)
    theta, time, m = 0., 0., 0
    i_kink = 0

    while 0. <= theta:

        if not kink_formed:
            if theta < np.pi / 4:
                theta += dtheta_max
            else:
                theta += dtheta
            twist.set_angle(theta)
        else:
            if i_kink / 10 < m:
                theta -= dtheta
                twist.set_angle(theta)

        dyn.run(1)

        if i % interval == 0:
            epot, ekin = saveAndPrint(atoms, traj, False)[:2]
            deltaY = atoms.positions[rend_b, 1] - y0

            hmax = np.max(
                atoms.positions[:, 2])  #substract the height of the plane?
            R = get_R(L, deltaY)
            data = [time, deltaY, R, theta, hmax, epot, ekin, epot + ekin]

            if save:
                stringi = ''
                for k, d in enumerate(data):
                    if k == 0:
                        stringi += '%.2f ' % d
                    elif k == 1 or k == 2:
                        stringi += '%.4f ' % d
                    else:
                        stringi += '%.12f ' % d
                write_line_own(mdlogfile, stringi + '\n', 'a')

            if thres_Z < hmax and not kink_formed:
                idxs = np.where(thres_Z < atoms.positions[:, 2])
                write_line_own(mdlogfile,
                               '# Kink! at idxs %s' % str(idxs) + '\n', 'a')
                print ' kink formed! ' + str(i / interval)
                kink_formed = True
                i_kink = i

            if hmax < 3.6 and kink_formed and not kink_vanished:
                write_line_own(mdlogfile, '# Kink vanished! \n', 'a')
                print ' kink vanished '
                kink_vanished = True

            print i / interval, theta / (2 * np.pi) * 360, R

        if kink_formed: m += 1

        i += 1
        time += dt


    make_simul_param_file(simulfile, W, L, width, length_int, dtheta/dt, dtheta, T, \
                          dt, fric, thres_Z, interval, deltaY, theta, i, edge)

    return folder
Beispiel #25
0
from numpy.testing import assert_allclose
from ase.calculators.lammpsrun import LAMMPS
from ase.build import bulk
from ase.test.eam_pot import Pt_u3
from ase.constraints import ExpCellFilter
from ase.optimize import BFGS

# (For now) reuse eam file stuff from other lammps test:
pot_fn = 'Pt_u3.eam'
f = open(pot_fn, 'w')
f.write(Pt_u3)
f.close()
params = {}
params['pair_style'] = 'eam'
params['pair_coeff'] = ['1 1 {}'.format(pot_fn)]
calc = LAMMPS(specorder=['Pt'], files=[pot_fn], **params)

rng = np.random.RandomState(17)

atoms = bulk('Pt') * (2, 2, 2)
atoms.rattle(stdev=0.1)
atoms.cell += 2 * rng.rand(3, 3)
atoms.calc = calc

assert_allclose(atoms.get_stress(),
                calc.calculate_numerical_stress(atoms),
                atol=1e-4,
                rtol=1e-4)

opt = BFGS(ExpCellFilter(atoms), trajectory='opt.traj')
for i, _ in enumerate(opt.irun(fmax=0.05)):
Beispiel #26
0
from ase.calculators.lammpsrun import LAMMPS
from ase.cluster.icosahedron import Icosahedron
from ase.data import atomic_numbers, atomic_masses
from numpy.linalg import norm

ar_nc = Icosahedron('Ar', noshells=2)
ar_nc.cell = [[300, 0, 0], [0, 300, 0], [0, 0, 300]]
ar_nc.pbc = True

params = {}
params['pair_style'] = 'lj/cut 8.0'
params['pair_coeff'] = ['1 1 0.0108102 3.345']
params['mass'] = ['1 {}'.format(atomic_masses[atomic_numbers['Ar']])]

calc = LAMMPS(specorder=['Ar'], parameters=params)

ar_nc.set_calculator(calc)

E = ar_nc.get_potential_energy()
F = ar_nc.get_forces()

assert abs(E - -0.47) < 1E-2
assert abs(norm(F) - 0.0574) < 1E-4
assert abs(norm(ar_nc.positions) - 23.588) < 1E-3

params['minimize'] = '1.0e-15 1.0e-6 2000 4000'  # add minimize
calc.params = params

# set_atoms=True to read final coordinates after minimization
calc.run(set_atoms=True)
Beispiel #27
0
#!/usr/bin/env python3

import os, wget
from ase import Atom, Atoms
from ase.build import bulk
from ase.calculators.lammpsrun import LAMMPS  #基于文件的ASE计算器
from ase.calculators.lammpslib import LAMMPSlib  #基于LAMMPS原生的Python接口

# 势函数下载
url = "https://openkim.org/files/MO_418978237058_005/NiAlH_jea.eam.alloy"
pot_fname = wget.filename_from_url(url)
if not os.path.exists(pot_fname):
    pot_fname = wget.download(url)

# 模型构建
Ni = bulk('Ni', cubic=True)
H = Atom('H', position=Ni.cell.diagonal() / 2)
NiH = Ni + H
NiH.pbc = True

# 开始计算
lammps = LAMMPS(files=[pot_fname],
                parameters={
                    'pair_style': 'eam/alloy',
                    'pair_coeff': ['* * {} H Ni'.format(pot_fname)]
                })
lammps.set(command="/usr/bin/lmp")

NiH.calc = lammps
print("Energy ", NiH.get_potential_energy())
Beispiel #28
0
from ase import Atoms, Atom
from ase.calculators.lammpsrun import LAMMPS

a = [6.5, 6.5, 7.7]
d = 2.3608
NaCl = Atoms([Atom('Na', [0, 0, 0]), Atom('Cl', [0, 0, d])], cell=a, pbc=True)

calc = LAMMPS(tmp_dir='./', keep_tmp_files=True)
NaCl.set_calculator(calc)

print NaCl.get_stress()
Beispiel #29
0
def LAMMPSRunCalculator(model_name, model_type, supported_species, options,
                        debug, **kwargs):
    """
    Used for Portable Models or LAMMPS Simulator Models if specifically requested
    """
    def get_params(model_name, supported_units, supported_species, atom_style):
        """
        Extract parameters for LAMMPS calculator from model definition lines.
        Returns a dictionary with entries for "pair_style" and "pair_coeff".
        Expects there to be only one "pair_style" line. There can be multiple
        "pair_coeff" lines (result is returned as a list).
        """
        parameters = {}

        # In case the SM supplied its own atom_style in its model-init -- only needed
        # because lammpsrun writes data files and needs to know the proper format
        if atom_style:
            parameters["atom_style"] = atom_style

        # Set units to prevent them from defaulting to metal
        parameters["units"] = supported_units

        parameters["model_init"] = [
            "kim_init {} {}{}".format(model_name, supported_units, os.linesep)
        ]

        parameters["kim_interactions"] = "kim_interactions {}{}".format(
            (" ").join(supported_species), os.linesep)

        # For every species in "supported_species", add an entry to the
        # "masses" key in dictionary "parameters".
        parameters["masses"] = []
        for i, species in enumerate(supported_species):
            if species not in atomic_numbers:
                raise KIMCalculatorError(
                    "Could not determine mass of unknown species "
                    "{} listed as supported by model".format(species))
            massstr = str(
                convert(
                    atomic_masses[atomic_numbers[species]],
                    "mass",
                    "ASE",
                    supported_units,
                ))
            parameters["masses"].append(str(i + 1) + " " + massstr)

        return parameters

    options_not_allowed = [
        "parameters", "files", "specorder", "keep_tmp_files"
    ]

    _check_conflict_options(options,
                            options_not_allowed,
                            simulator="lammpsrun")

    # If no atom_style kwarg is passed, lammpsrun will default to atom_style atomic,
    # which is what we want for KIM Portable Models
    atom_style = kwargs.get("atom_style", None)

    # Simulator Models will supply their own units from their metadata. For Portable
    # Models, we use "metal" units.
    supported_units = kwargs.get("supported_units", "metal")

    # Set up kim_init and kim_interactions lines
    parameters = get_params(model_name, supported_units, supported_species,
                            atom_style)

    return LAMMPS(**parameters,
                  specorder=supported_species,
                  keep_tmp_files=debug,
                  **options)
Beispiel #30
0
from ase.calculators.lammpsrun import LAMMPS
from ase.cluster.icosahedron import Icosahedron
from ase.data import atomic_numbers, atomic_masses
from numpy.testing import assert_allclose

ar_nc = Icosahedron('Ar', noshells=2)
ar_nc.cell = [[300, 0, 0], [0, 300, 0], [0, 0, 300]]
ar_nc.pbc = True

params = {}
params['pair_style'] = 'lj/cut 8.0'
params['pair_coeff'] = ['1 1 0.0108102 3.345']
params['masses'] = ['1 {}'.format(atomic_masses[atomic_numbers['Ar']])]

with LAMMPS(specorder=['Ar'], **params) as calc:
    ar_nc.set_calculator(calc)
    F1_numer = calc.calculate_numerical_forces(ar_nc)

    assert_allclose(ar_nc.get_potential_energy(),
                    -0.468147667942117,
                    atol=1e-4,
                    rtol=1e-4)
    assert_allclose(ar_nc.get_forces(),
                    calc.calculate_numerical_forces(ar_nc),
                    atol=1e-4,
                    rtol=1e-4)

    params['minimize'] = '1.0e-15 1.0e-6 2000 4000'  # add minimize
    calc.parameters = params

    # set_atoms=True to read final coordinates after minimization