Пример #1
0
def main():
    """
    Reads an experiment .abinit.json from stdin or from the file marked in the first argument,
    and copies the unit cell in each cardinal direction according to the value
    in "meta.repeat_cell", modifiying "meta.atoms" and "direct.acell".
    Outputs in .abinit.json format to stdout.
    """
    # Get input from stdin or first argument's file
    with handle_command_line_IO.get_input_file(
            display_help_message) as input_file:
        experiment = Experiment.load_from_json_file(input_file)

        # Make this transparent; if no scaling is specified, don't scale
        if experiment.meta and 'dopings' in experiment.meta and 'atoms' in experiment.meta:

            # Get the atoms and dopings, remove the dopings
            experiment.meta['atoms'] = \
                dope_atoms(parse_atoms(experiment.meta['atoms']),
                           parse_atoms(experiment.meta.pop('dopings', None)),
                           tolerance=1e-4)
        else:
            handle_command_line_IO.errprint(
                "No doping of unit cell specified; taking no action")

        # Echo the resulting experiment back to stdout
        print json.dumps(experiment, cls=SimpleObjectJSONEncoder, indent=4)
def experiment_union(experiment_one,
                     experiment_two,
                     collision_resolution='raise'):
    """
    Returns a new Experiment representing the union of settings specified in the two given Experiments.
    Direct settings are merged using union of lists,
    while meta settings are merged by creating a dict with the settings specified in both.
    The behavior when two experiments specify the same setting
    can be changed with the 'collision_resolution' parameter:
        'raise' (Default) : raise a RuntimeError if the same setting is 
                            specified in both experiments
        'keep_original'   : keep the setting specified in the first colliding experiment,
                            and discard the rest
        'overwrite'        : keep the setting specified in the last colliding experiment,
                            and discard the rest
    """
    result_meta = dict(experiment_one.meta
                       or {})  #shallow copy - fine bc we won't edit deeply
    if experiment_two.meta:
        for key in experiment_two.meta:
            if key in result_meta:
                if collision_resolution == 'keep_original':
                    pass
                elif collision_resolution == 'overwrite':
                    result_meta[key] = experiment_two.meta[key]
                else:  # collision_resolution == 'raise':
                    raise RuntimeError('Meta setting ' + key +
                                       ' is specified in two experiments')
            else:
                result_meta[key] = experiment_two.meta[key]
    return Experiment(direct=(experiment_one.direct or []) +
                      (experiment_two.direct or []),
                      meta=result_meta)
Пример #3
0
def main():
    """
    Displays the atoms from the meta attributes of the input experiment within a rhombal unit cell
    """
    with handle_command_line_IO.get_input_file(
            display_help_function) as input_file:
        experiment = Experiment.load_from_json_file(input_file)
        atoms = parse_atoms(experiment.meta['atoms'])
        display_atoms_rhombus(atoms)
def main():
    """
    Reads an experiment .abinit.json from stdin or from the file marked in the first argument,
    and copies the unit cell in each cardinal direction according to the value
    in "meta.repeat_cell", modifiying "meta.atoms" and "direct.acell".
    Outputs in .abinit.json format to stdout.
    """
    # Get input from stdin or first argument's file
    input_file, other_file_names = \
        handle_command_line_IO.get_input_file_and_args(display_help_message)
    with input_file:
        experiment = Experiment.load_from_json_file(input_file)

        # Get all of the other specified input files
        for file_name in other_file_names:
            with open(file_name, "r") as next_file:
                next_experiment = Experiment.load_from_json_file(next_file)
                experiment = experiment_union(experiment, next_experiment)

        # Echo the resulting experiment back to stdout
        print json.dumps(experiment, cls=SimpleObjectJSONEncoder, indent=4)
def main():
    """
    Reads an experiment .abinit.json from stdin or from the file marked in the first argument,
    and generates a 2-D chiral tesselation of the cell according to the values in
    in "meta.make_chirality", modifiying "meta.atoms" and "direct.acell".
    Outputs in .abinit.json format to stdout.
    """
    # Get input from stdin or first argument's file
    with handle_command_line_IO.get_input_file(display_help_message) as input_file:
        experiment = Experiment.load_from_json_file(input_file)

        # Make this transparent; if no scaling is specified, don't scale
        if experiment.meta and 'make_chirality' in experiment.meta and 'atoms' in experiment.meta:
            # Get the number of times to repeat
            desired_chirality = experiment.meta.pop('make_chirality', None)
            if not isinstance(desired_chirality, list) or len(desired_chirality) != 2:
                raise RuntimeError("Desired chirality must be 2-D.")

            rhombus_basis = [[1, 0.5, 0], [0, 3**0.5/2, 0], [0, 0, 1]]

            original_number_of_atoms = len(experiment.meta['atoms'])

            # Repeat the atoms
            experiment.meta['atoms'] = \
                generate_xy_chiral_cell(parse_atoms(experiment.meta['atoms']), desired_chirality, rhombus_basis)

            # Scale the unit cell
            normalization_factor = (desired_chirality[0] ** 2 + desired_chirality[1] ** 2) ** 0.5
            for attribute in experiment.direct:
                if attribute.name == "acell":
                    attribute.value = \
                        [attribute.value[0] * normalization_factor, \
                         attribute.value[1] * normalization_factor, \
                         attribute.value[2]]

            # Validate  (because chirality is easy to screw up)
            # Make sure there are no collisions
            for collision in get_collisions(experiment.meta['atoms']):
                handle_command_line_IO.errprint(\
                    "collision between "+str(collision[0])+" and "+str(collision[1]))
            # Make sure there are the right number of atoms
            expected_number_of_atoms = original_number_of_atoms * sum([x*x for x in desired_chirality])
            if expected_number_of_atoms != len(experiment.meta['atoms']):
                handle_command_line_IO.errprint('WARNING\nExpected '+str(expected_number_of_atoms)+\
                                   ' atoms, found '+str(len(experiment.meta['atoms'])))


        else:
            handle_command_line_IO.errprint("No chirality of unit cell specified; taking no action")

        # Echo the resulting experiment back to stdout
        print json.dumps(experiment, cls=SimpleObjectJSONEncoder, indent=4)
Пример #6
0
def main():
    """
    Displays the atoms from the meta attributes of the input experiment within a rhombal unit cell
    """
    with handle_command_line_IO.get_input_file(display_help_function) as input_file:
        experiment = Experiment.load_from_json_file(input_file)
        atoms = parse_atoms(experiment.meta['atoms'])
        rhomb_atoms = get_rhombal_cell(atoms)
        acell_raw = experiment.get_direct_property('acell')
        cell_size = [float(coord) for coord in acell_raw.value]
        correct_size_rhomb_atoms = [Atom(atom.znucl, atom.coord*cell_size) for atom in rhomb_atoms]
        print len(atoms)
        print '' #no comment on the file
        for atom in correct_size_rhomb_atoms:
            print repr_in_xyz(atom)
def main():
    with handle_command_line_IO.get_input_file(display_help_message) as input_file: # get from stdin or first argument's file
        experiment = Experiment.load_from_json_file(input_file)

    if experiment.meta and 'atoms' in experiment.meta : #allow for pass-through if there's nothing to do
        atoms = sorted(parse_atoms(experiment.meta['atoms']), key=lambda atom: atom.znucl) # sorted so we can group by znucl
        experiment.meta['atoms'] = None
        atom_types = OrderedSet([atom.znucl for atom in atoms])
        znucl_to_typat_mapping = dict((znucl, index+1) for index, znucl in enumerate(atom_types))

        experiment.direct.append(SimpleAttribute(name="natom", value=len(atoms), comment=GENERATED_VALUE_COMMENT))
        experiment.direct.append(SimpleAttribute(name="ntypat", value=len(atom_types), comment=GENERATED_VALUE_COMMENT))
        experiment.direct.append(SimpleAttribute(name="znucl", value=list(atom_types), comment=GENERATED_VALUE_COMMENT))
        experiment.direct.append(SimpleAttribute(name="typat", value=[znucl_to_typat_mapping[atom.znucl] for atom in atoms], comment=GENERATED_VALUE_COMMENT))
        experiment.direct.append(SimpleAttribute(name="xred", value=[atom.coord.coordinate_array for atom in atoms], comment=GENERATED_VALUE_COMMENT))

    print json.dumps(experiment, cls=SimpleObjectJSONEncoder, indent=4) 
Пример #8
0
def main():
    """
    Reads an experiment .abinit.json from stdin or from the file marked in the first argument,
    and copies the unit cell in each cardinal direction according to the value
    in "meta.repeat_cell", modifiying "meta.atoms" and "direct.acell".
    Outputs in .abinit.json format to stdout.
    """
    # Get input from stdin or first argument's file
    with handle_command_line_IO.get_input_file(
            display_help_message) as input_file:
        experiment = Experiment.load_from_json_file(input_file)

        # Make this transparent; if no scaling is specified, don't scale
        if experiment.meta and 'repeat_cell' in experiment.meta and 'atoms' in experiment.meta:
            # Get the number of times to repeat
            repeat_factor = experiment.meta['repeat_cell']
            if isinstance(repeat_factor, list):
                if len(repeat_factor) != 3:
                    raise RuntimeError("Unit cell must be 3-D.")
            else:
                # if this throws an error, just report it to the user
                repeat_factor = [int(repeat_factor)] * 3

            # Repeat the atoms
            experiment.meta['atoms'] = \
                repeat_atoms_in_unit_cell(parse_atoms(experiment.meta['atoms']), repeat_factor)

            # Scale the unit cell
            for attribute in experiment.direct:
                if attribute.name == "acell":
                    attribute.value = [
                        float(attribute.value[i]) * repeat_factor[i]
                        for i in xrange(0, 3)
                    ]

            # Take out repetition of cell so it doesn't happen again somehow
            experiment.meta.pop('repeat_cell', None)
        else:
            handle_command_line_IO.errprint(
                "No repeat of unit cell specified; taking no action")

        # Echo the resulting experiment back to stdout
        print json.dumps(experiment, cls=SimpleObjectJSONEncoder, indent=4)
Пример #9
0
def main():
    """
    Displays the atoms from the meta attributes of the input experiment within a rhombal unit cell
    """
    with handle_command_line_IO.get_input_file(display_help_function) as input_file:
        print repr_experiment_in_cif(Experiment.load_from_json_file(input_file))