コード例 #1
0
def test_main():

    version = kimpy.sem_ver.get_sem_ver()
    ls_less_than, error = kimpy.sem_ver.is_less_than('2.0.1', '2.1.0')
    check_error(error, 'kimpy.sem_ver.is_less_than')
    out = kimpy.sem_ver.parse_sem_ver(version)
    version_related = out[:-1]
    version_rebuilt_1 = '{}.{}.{}{}+{}'.format(*version_related)
    version_rebuilt_2 = '{}.{}.{}-{}+{}'.format(*version_related)

    assert ls_less_than == 1
    assert version in (version_rebuilt_1, version_rebuilt_2)
コード例 #2
0
def dirs_for_collection(collection, col):
    for it in [
            kimpy.collection_item_type.modelDriver,
            kimpy.collection_item_type.portableModel,
            kimpy.collection_item_type.simulatorModel,
    ]:
        extent, error = col.cache_list_of_directory_names(collection, it)
        check_error(error, 'cache_list_of_directory_names')
        print(str(collection), ':', str(it), ':')

        for i in range(extent):
            name, error = col.get_directory_name(i)
            check_error(error, 'get_directory_name')
            print('    ', name)
コード例 #3
0
ファイル: test_model.py プロジェクト: yafshar/kimpy
def get_neigh(data, cutoffs, neighbor_list_index, particle_number):
    error = 0

    # we only support one neighbor list
    rcut = data['cutoff']
    if len(cutoffs) != 1 or cutoffs[0] > rcut:
        error = 1
    if neighbor_list_index != 0:
        error = 1

    # invalid id
    number_of_particles = data['num_particles']
    if particle_number >= number_of_particles or particle_number < 0:
        error = 1
    check_error(error, 'get_neigh')

    neighbors = data['neighbors'][particle_number]
    return (neighbors, error)
コード例 #4
0
ファイル: test_graphite.py プロジェクト: yafshar/kimpy
def test_main():

    # create contributing atoms
    alat = 2.46
    d = 3.35
    cell, contrib_coords, contrib_species = create_graphite_unit_cell(alat, d)

    # create padding atoms
    cutoffs = np.array([d + 0.01, d + 0.02], dtype=np.double)
    influence_dist = cutoffs[1]
    pbc = np.array([1, 1, 1], dtype=np.intc)
    out = nl.create_paddings(influence_dist, cell, pbc,
                             contrib_coords, contrib_species)
    pad_coords, pad_species, pad_image, error = out
    check_error(error, 'nl.create_padding')

    assert pad_coords.shape == (96, 3)
    # print('pad_coords is of shape:', pad_coords.shape)

    coords = np.concatenate((contrib_coords, pad_coords))
    coords = np.asarray(coords, dtype=np.double)
    species = np.concatenate((contrib_species, pad_species))
    species = np.asarray(species, dtype=np.intc)
    fname = 'atoms.xyz'
    write_XYZ(fname, cell, species, coords)

    # flag to indicate wheter create neighbor list for an atom
    n_pad = pad_coords.shape[0]
    n_contrib = contrib_coords.shape[0]
    need_neigh = np.concatenate((np.ones(n_contrib), np.zeros(n_pad)))
    need_neigh = np.asarray(need_neigh, dtype=np.intc)

    # create neighbor list
    neigh = nl.initialize()
    error = nl.build(neigh, coords, influence_dist, cutoffs, need_neigh)
    check_error(error, 'nl.build')

    # build again (it will automatically empty previous neigh list)
    error = nl.build(neigh, coords, influence_dist, cutoffs, need_neigh)
    check_error(error, 'nl.build')

    # test get neigh function
    neigh_list_index = 0
    particle = 1
    num_neigh, neighbors, error = nl.get_neigh(
        neigh, cutoffs, neigh_list_index, particle)
    check_error(error, 'nl.get_neigh')
    assert num_neigh == 14
    # print('Atom 1 has {} neighbors:'.format(num_neigh), end=' ')
    # for i in neighbors:
    #  print(i, end=' ')

    neigh_list_index = 1
    particle = 4
    num_neigh, neighbors, error = nl.get_neigh(
        neigh, cutoffs, neigh_list_index, particle)
    check_error(error, 'nl.get_neigh')
    assert num_neigh == 0

    # expect error message from this
    # neigh_list_index = 1
    # particle = n_contrib + n_pad
    # num_neigh, neighbors, error = nl.get_neigh(neigh, cutoffs, neigh_list_index, particle)
    # assert error == 1

    # delete neighbor list
    nl.clean(neigh)

    # remove the created file
    try:
        os.remove(fname)
        os.remove('kim.log')
    except:
        pass
コード例 #5
0
ファイル: test_model.py プロジェクト: yafshar/kimpy
def test_main():

    modelname = 'ex_model_Ar_P_Morse'
    # modelname = 'Three_Body_Stillinger_Weber_Si__MO_405512056662_004'
    # modelname = 'LennardJones612_Universal__MO_826355984548_002'
    print()
    print('=' * 80)
    print('Matching results for KIM model:', modelname)
    print()

    # create model
    requestedUnitsAccepted, kim_model, error = kimpy.model.create(
        kimpy.numbering.zeroBased,
        kimpy.length_unit.A,
        kimpy.energy_unit.eV,
        kimpy.charge_unit.e,
        kimpy.temperature_unit.K,
        kimpy.time_unit.ps,
        modelname,
    )
    check_error(error, 'kimpy.model.create')
    if not requestedUnitsAccepted:
        report_error('requested units not accepted in kimpy.model.create')

    # units
    l_unit, e_unit, c_unit, te_unit, ti_unit = kim_model.get_units()
    check_error(error, 'kim_model.get_units')
    print('Length unit is:', str(l_unit))
    print('Energy unit is:', str(e_unit))
    print('Charge unit is:', str(c_unit))
    print('Temperature unit is:', str(te_unit))
    print('Time unit is:', str(ti_unit))
    print()

    # create compute arguments
    compute_arguments, error = kim_model.compute_arguments_create()
    check_error(error, 'kim_model.compute_arguments_create')

    # check compute arguments
    num_compute_arguments = (
        kimpy.compute_argument_name.get_number_of_compute_argument_names())
    print('Number of compute_arguments:', num_compute_arguments)

    for i in range(num_compute_arguments):

        name, error = \
            kimpy.compute_argument_name.get_compute_argument_name(i)
        check_error(error, 'kim_model.get_compute_argument_name')

        dtype, error = \
            kimpy.compute_argument_name.get_compute_argument_data_type(name)
        check_error(error, 'kim_model.get_compute_argument_data_type')

        support_status, error = \
            compute_arguments.get_argument_support_status(name)
        check_error(error, 'compute_argument.get_argument_support_status')

        n_space_1 = 21 - len(str(name))
        n_space_2 = 7 - len(str(dtype))
        print('Compute Argument name "{}" '.format(name) + ' ' * n_space_1 +
              'is of type "{}" '.format(dtype) + ' ' * n_space_2 +
              'and has support status "{}".'.format(support_status))

        # can only handle energy and force as a required arg
        if support_status == kimpy.support_status.required:
            if name not in (kimpy.compute_argument_name.partialEnergy,
                            kimpy.compute_argument_name.partialForces):
                report_error('Unsupported required ComputeArgument')

        # must have energy and forces
        if name in (kimpy.compute_argument_name.partialEnergy,
                    kimpy.compute_argument_name.partialForces):
            if support_status not in (kimpy.support_status.required,
                                      kimpy.support_status.optional):
                report_error('Energy or forces not available')
    print()

    # check compute callbacks
    num_callbacks = \
        kimpy.compute_callback_name.get_number_of_compute_callback_names()
    print('Number of callbacks:', num_callbacks)

    for i in range(num_callbacks):

        name, error = kimpy.compute_callback_name.get_compute_callback_name(i)
        check_error(error, 'kim_model.get_compute_callback_name')

        support_status, error = \
            compute_arguments.get_callback_support_status(name)
        check_error(error, 'compute_argument.get_callback_support_status')

        n_space = 18 - len(str(name))
        print('Compute callback "{}"'.format(name) + ' ' * n_space +
              'has support status "{}".'.format(support_status))

        # cannot handle any "required" callbacks
        if support_status == kimpy.support_status.required:
            report_error('Unsupported required ComputeCallback')
    print()

    # parameter
    num_params = kim_model.get_number_of_parameters()
    print('Number of parameters:', num_params)
    print()

    for i in range(num_params):
        out = kim_model.get_parameter_metadata(i)
        dtype, extent, name, description, error = out
        check_error(error, 'kim_model.get_parameter_metadata')
        print('Parameter No.', i)
        print('    data type:', dtype)
        print('    extent:', extent)
        print('    name:', name)
        print('    description:', description)
    print()

    # register argument
    argon = create_fcc_argon()

    coords = np.asarray(argon.get_positions(), dtype=np.double)
    N = coords.shape[0]
    print('Number of particles:', N)
    forces = np.zeros((N, 3), dtype=np.double)
    energy = np.array([0.0], dtype=np.double)
    num_particles = np.array([N], dtype=np.intc)
    species_code = np.zeros(num_particles, dtype=np.intc)
    particle_contributing = np.zeros(num_particles, dtype=np.intc)

    error = compute_arguments.set_argument_pointer(
        kimpy.compute_argument_name.numberOfParticles, num_particles)
    check_error(error, 'kimpy.compute_argument.set_argument_pointer')

    error = compute_arguments.set_argument_pointer(
        kimpy.compute_argument_name.particleSpeciesCodes, species_code)
    check_error(error, 'kimpy.compute_argument.set_argument_pointer')

    error = compute_arguments.set_argument_pointer(
        kimpy.compute_argument_name.particleContributing,
        particle_contributing)
    check_error(error, 'kimpy.compute_argument.set_argument_pointer')

    error = compute_arguments.set_argument_pointer(
        kimpy.compute_argument_name.coordinates, coords)
    check_error(error, 'kimpy.compute_argument.set_argument_pointer')

    error = compute_arguments.set_argument_pointer(
        kimpy.compute_argument_name.partialEnergy, energy)
    check_error(error, 'kimpy.compute_argument.set_argument_pointer')

    error = compute_arguments.set_argument_pointer(
        kimpy.compute_argument_name.partialForces, forces)
    check_error(error, 'kimpy.compute_argument.set_argument_pointer')

    # neighbor list
    neigh = dict()

    # register get neigh callback
    error = compute_arguments.set_callback(
        kimpy.compute_callback_name.GetNeighborList, get_neigh, neigh)
    check_error(error, 'kimpy.compute_argument.set_callback')

    # influence distance and cutoff of model
    model_influence_dist = kim_model.get_influence_distance()
    out = kim_model.get_neighbor_list_cutoffs_and_hints()
    model_cutoffs, padding_not_require_neigh_hints = out
    print('Model influence distance:', model_influence_dist)
    print('Model cutoffs:', model_cutoffs)
    print('Model padding neighbors hints:', padding_not_require_neigh_hints)
    print()

    # species support and code
    species_support, code, error = kim_model.get_species_support_and_code(
        kimpy.species_name.Ar)
    check_error(error or not species_support,
                'kim_model.get_species_support_and_code')
    print('Species Ar is supported and its code is:', code)
    print()

    # setup particle species
    species_code[:] = code

    # setup particleContributing
    particle_contributing[:] = 1

    # compute energy and force for different structures
    alat = 5.26
    min_alat = 0.8 * 5.26
    max_alat = 1.2 * 5.26
    inc_alat = 0.025 * alat
    all_alat = np.arange(min_alat, max_alat, inc_alat)

    print('=' * 80)
    print('Result for KIM model:', modelname)
    print()
    print('       energy          force norm        lattice spacing')
    print()

    for a in all_alat:
        argon = create_fcc_argon(a)
        # NOTE should not change coords address
        np.copyto(coords, argon.get_positions())
        # NOTE safe to change content of neigh
        create_neigh(coords, model_influence_dist, neigh)
        error = kim_model.compute(compute_arguments)
        print('{:18.10e} {:18.10e} {:18.10e}'.format(energy[0],
                                                     np.linalg.norm(forces),
                                                     a))

    # destory compute arguments
    error = kim_model.compute_arguments_destroy(compute_arguments)
    check_error(error, 'kim_model.compute_arguments_destroy')

    out = kim_model.is_routine_present(
        kimpy.model_routine_name.WriteParameterizedModel)
    present, required, error = out
    check_error(error, 'kim_model.is_routine_present')
    if present:
        kim_model.write_parameterized_model('.', 'Morse_Ar')
        try:
            os.remove('Morse_Ar.params')
            os.remove('CMakeLists.txt')
            os.remove('kim.log')
        except:
            pass

    # destory model
    kimpy.model.destroy(kim_model)
コード例 #6
0
def example_main():
    col, error = kimpy.collections.create()
    check_error(error, 'collections.create')

    project, semver = col.get_project_name_and_sem_ver()
    print('Project:', project)
    print('semVer:', semver)

    for it in [
            kimpy.collection_item_type.modelDriver,
            kimpy.collection_item_type.portableModel,
            kimpy.collection_item_type.simulatorModel,
    ]:
        name, error = col.get_environment_variable_name(it)
        check_error(error, 'get_environment_variable_name')
        print(str(it), ' env name:', name)

    name, value = col.get_configuration_file_environment_variable()
    print('config file env name:', name)
    print('config file env value:', value)

    filename = col.get_configuration_file_name()
    print('config file name:', filename)

    for kc in [
            kimpy.collection.system,
            kimpy.collection.user,
            kimpy.collection.environmentVariable,
            kimpy.collection.currentWorkingDirectory,
    ]:
        dirs_for_collection(kc, col)

    for kc in [
            kimpy.collection.system,
            kimpy.collection.user,
            kimpy.collection.environmentVariable,
            kimpy.collection.currentWorkingDirectory,
    ]:
        names_for_collection(kc, col)

    for it in [
            kimpy.collection_item_type.modelDriver,
            kimpy.collection_item_type.portableModel,
            kimpy.collection_item_type.simulatorModel,
    ]:
        extent, error = col.cache_list_of_item_names_by_type(it)
        check_error(error, 'cache_list_of_item_names_by_type')
        print(str(it), ':')

        for i in range(extent):
            name, error = col.get_item_name_by_type(i)
            check_error(error, 'get_item_name_by_type')
            print('    ', name)

    filename, collection, error = \
        col.get_item_library_file_name_and_collection(
            kimpy.collection_item_type.simulatorModel,
            'Sim_LAMMPS_LJcut_AkersonElliott_Alchemy_PbAu',)

    check_error(error, 'get_item_library_file_name_and_collection')

    msg = 'Simulator Model Sim_LAMMPS_LJcut_AkersonElliott_Alchemy_PbAu '
    msg += 'has library name "{}" and is part of the '.format(filename)
    msg += '"{}" collection'.format(str(collection))
    print(msg)

    extent, error = col.cache_list_of_item_metadata_files(
        kimpy.collection_item_type.simulatorModel,
        "Sim_LAMMPS_LJcut_AkersonElliott_Alchemy_PbAu",
    )
    check_error(error, 'cache_list_of_item_metadata_files')

    for i in range(extent):
        file_name, file_length, file_raw_data, \
            avail_as_str, file_str, error = col.get_item_metadata_file(i)

        check_error(error, 'get_item_metadata_file')
        msg = 'Metadata file {} ({}) '.format(i, file_name)
        msg += 'is of length {}'.format(file_length)
        print(msg)
        print(file_str)

    kimpy.collections.destroy(col)
コード例 #7
0
def example_main():
    modelname = 'Sim_LAMMPS_LJcut_AkersonElliott_Alchemy_PbAu'
    sm, error = kimpy.simulator_model.create(modelname)
    check_error(error, 'simulator_model.create')

    name, version = sm.get_simulator_name_and_version()
    print('Simulator name:', name)
    print('Simulator version:', version)

    extent = sm.get_number_of_supported_species()
    print('Simulator supports {} species.'.format(extent))
    for i in range(extent):
        species, error = sm.get_supported_species(i)
        check_error(error, 'get_supported_species')
        print('{}  {}'.format(i, species))

    error = sm.add_template_map('atom-type-sym-list', 'Pb Pb Au Pb')
    check_error(error, 'add_template_map')
    sm.close_template_map()
    number_fields = sm.get_number_of_simulator_fields()
    print('Simulator model has {} fields.'.format(number_fields))

    for i in range(number_fields):
        extent, field_name, error = sm.get_simulator_field_metadata(i)
        check_error(error, 'get_simulator_field_metadata')
        print('Field {} is {} and has lines: {}'.format(i, field_name, extent))
        for j in range(extent):
            field_line, error = sm.get_simulator_field_line(i, j)
            check_error(error, 'get_simulator_field_line')
            print('    ', field_line)

    dirname = sm.get_parameter_file_directory_name()
    print('Simulator model parameter directory name is:', dirname)

    specname = sm.get_specification_file_name()
    print('Simulator model specification file name is:', specname)
    fname = join(dirname, specname)
    with open(fname, 'r') as f:
        print(f.read())

    num_param_files = sm.get_number_of_parameter_files()
    print('Simulator model has {} parameter files.'.format(num_param_files))
    for i in range(num_param_files):
        paramname, error = sm.get_parameter_file_name(i)
        check_error(error, 'get_parameter_file_name')
        print('Parameter file {} has name: {}'.format(i, paramname))
        fname = join(dirname, paramname)
        with open(fname, 'r') as f:
            print(f.read())

    kimpy.simulator_model.destroy(sm)
コード例 #8
0
def test_main():

    modelname = 'LennardJones612_UniversalShifted__MO_959249795837_003'

    # create model
    requestedUnitsAccepted, kim_model, error = kimpy.model.create(
        kimpy.numbering.zeroBased,
        kimpy.length_unit.A,
        kimpy.energy_unit.eV,
        kimpy.charge_unit.e,
        kimpy.temperature_unit.K,
        kimpy.time_unit.ps,
        modelname,
    )
    check_error(error, 'kimpy.model.create')

    if not requestedUnitsAccepted:
        report_error('requested units not accepted in kimpy.model.create')

    # create compute arguments
    compute_arguments, error = kim_model.compute_arguments_create()
    check_error(error, 'kim_model.compute_arguments_create')

    # register argument
    argon = get_argon_dimer()

    coords = np.asarray(argon.get_positions(), dtype=np.double)
    N = coords.shape[0]
    forces = np.zeros((N, 3), dtype=np.double)
    energy = np.array([0.0], dtype=np.double)
    num_particles = np.array([N], dtype=np.intc)
    species_code = np.zeros(num_particles, dtype=np.intc)
    particle_contributing = np.zeros(num_particles, dtype=np.intc)

    error = compute_arguments.set_argument_pointer(
        kimpy.compute_argument_name.numberOfParticles, num_particles)
    check_error(error, 'kimpy.compute_argument.set_argument_pointer')

    error = compute_arguments.set_argument_pointer(
        kimpy.compute_argument_name.particleSpeciesCodes, species_code)
    check_error(error, 'kimpy.compute_argument.set_argument_pointer')

    error = compute_arguments.set_argument_pointer(
        kimpy.compute_argument_name.particleContributing,
        particle_contributing)
    check_error(error, 'kimpy.compute_argument.set_argument_pointer')

    error = compute_arguments.set_argument_pointer(
        kimpy.compute_argument_name.coordinates, coords)
    check_error(error, 'kimpy.compute_argument.set_argument_pointer')

    error = compute_arguments.set_argument_pointer(
        kimpy.compute_argument_name.partialEnergy, energy)
    check_error(error, 'kimpy.compute_argument.set_argument_pointer')

    error = compute_arguments.set_argument_pointer(
        kimpy.compute_argument_name.partialForces, forces)
    check_error(error, 'kimpy.compute_argument.set_argument_pointer')

    # species support and code
    species_support, code, error = \
        kim_model.get_species_support_and_code(kimpy.species_name.Ar)
    check_error(error or not species_support,
                'kim_model.get_species_support_and_code')

    # setup particle species
    species_code[:] = code

    # setup particleContributing
    particle_contributing[:] = 1

    # register callbacks
    error = compute_arguments.set_callback(
        kimpy.compute_callback_name.GetNeighborList, get_neigh, neigh_data)
    check_error(error, 'kimpy.compute_argument.set_callback')

    error = compute_arguments.set_callback(
        kimpy.compute_callback_name.ProcessDEDrTerm, process_dEdr, dEdr_data)
    check_error(error, 'kimpy.compute_argument.set_callback')

    error = compute_arguments.set_callback(
        kimpy.compute_callback_name.ProcessD2EDr2Term, process_d2Edr2,
        d2Edr2_data)
    check_error(error, 'kimpy.compute_argument.set_callback')

    # 1st call of compute
    error = kim_model.compute(compute_arguments)
    check_error(error, 'kim_model.compute')

    # 2nd call of compute
    neigh_data['key'] = 2
    dEdr_data['key'] = 2
    d2Edr2_data['key'] = 2
    error = kim_model.compute(compute_arguments)
    check_error(error, 'kim_model.compute')

    # destory compute arguments and model
    error = kim_model.compute_arguments_destroy(compute_arguments)
    check_error(error, 'kim_model.compute_arguments_destroy')
    kimpy.model.destroy(kim_model)

    # check callback can modify local data
    assert neigh_data['key'] == 3
    assert neigh_data['new_key'] == 1
    assert dEdr_data['key'] == 3
    assert dEdr_data['new_key'] == 1
    assert d2Edr2_data['key'] == 3
    assert d2Edr2_data['new_key'] == 1