Пример #1
0
def add_one(aggregate_id, seeds, monomer, hm_orientations, method):
    """
    :type aggregate_id str
    :type seeds list of Molecules
    :type monomer Molecule.Molecule
    :type hm_orientations int how many orientations
    :type method dict containing charge, multiplicity, software
    """
    if check_stop_signal():
        print("Function: add_one")
        return StopIteration
    print('  There are', len(seeds), 'seed molecules')
    cwd = os.getcwd()

    dict_of_optimized_molecules = {}
    for seed_count, each_seed in enumerate(seeds):
        if check_stop_signal():
            print("Function: add_one")
            return
        print('   Seed: {}'.format(seed_count))
        seed_id = "{:03d}".format(seed_count)
        seeds_home = 'seed_' + seed_id
        file_manager.make_directories(seeds_home)
        os.chdir(seeds_home)
        each_seed.mol_to_xyz('seed.xyz')
        monomer.mol_to_xyz('monomer.xyz')
        mol_id = '{0}_{1}'.format(seed_id, aggregate_id)

        all_orientations = tabu.generate_orientations(mol_id,
                                                      seeds[seed_count],
                                                      monomer, hm_orientations)
        for name, molecule in sorted(all_orientations.items(),
                                     key=operator.itemgetter(0)):
            o_status = optimise(molecule, method)
            if o_status is True:
                print("      E(%10s): %12.7f" % (name, molecule.energy))
                dict_of_optimized_molecules[name] = molecule
            else:
                print('    Optimisation failed:', name, 'will be discarded')
        os.chdir(cwd)
    if len(dict_of_optimized_molecules) < 2:
        return list(dict_of_optimized_molecules.values())
    print("  Clustering")
    selected_seeds = clustering.choose_geometries(dict_of_optimized_molecules)
    file_manager.make_directories('selected')
    for each_file in selected_seeds.values():
        xyz_file = 'seed_' + each_file.name[
            4:7] + '/result_' + each_file.name + '.xyz'
        shutil.copy(xyz_file, 'selected/')
    return list(selected_seeds.values())
Пример #2
0
def react(reactant_a,
          reactant_b,
          gamma_min=100,
          gamma_max=1000,
          hm_orientations=8,
          method=None):
    cwd = os.getcwd()
    software = method['software']
    print_header(gamma_max, gamma_min, hm_orientations, software)
    # prepare job directories
    product_dir = cwd + '/products'
    file_manager.make_directories(product_dir)
    file_manager.make_directories('trial_geometries')
    os.chdir('trial_geometries')

    all_orientations = tabu.generate_orientations('geom', reactant_a,
                                                  reactant_b, hm_orientations)

    os.chdir(cwd)

    gamma_list = np.linspace(gamma_min, gamma_max, num=10, dtype=float)
    orientations_to_optimize = all_orientations.copy()

    for gamma in gamma_list:
        print('  Current gamma :', gamma)
        gamma_id = "%04d" % (int(gamma))
        gamma_home = cwd + '/gamma_' + gamma_id
        file_manager.make_directories(gamma_home)
        os.chdir(gamma_home)

        optimized_molecules = optimize_all(gamma_id, gamma,
                                           orientations_to_optimize,
                                           product_dir, method)

        print("      ", len(optimized_molecules),
              "geometries from this gamma cycle")
        orientations_to_optimize = clustering.choose_geometries(
            optimized_molecules)
        if len(orientations_to_optimize) == 0:
            print("No orientations to optimized for the next gamma cycle.")
            break
        print("      ", len(orientations_to_optimize),
              "geometries in the next gamma cycle")
        print("number of products found from gamma:", gamma, " = ",
              len(table_of_product_inchi_strings))
        for key, value in orientations_to_optimize.items():
            print("the key for next gamma cycle:", key)
    print("\n\n\n\n")
    os.chdir(cwd)
    return
Пример #3
0
def add_one1(aggregate_id1, aggregate_id2, seeds1, seeds2, hm_orientations,
             method):
    """

    """
    seeds2 = seeds2[0]
    if check_stop_signal():
        aggregator_logger.info("Function: add_one")
        return StopIteration

    aggregator_logger.info('  There are {} seed molecules'.format(len(seeds1)))
    cwd = os.getcwd()

    list_of_optimized_molecules = []
    for seed_count, each_seed in enumerate(seeds1):
        if check_stop_signal():
            print("Function: add_one")
            return
        print('   Seed: {}'.format(seed_count))
        seed_id = "{:03d}".format(seed_count)
        seeds_home = 'seed_' + seed_id
        file_manager.make_directories(seeds_home)
        os.chdir(seeds_home)
        each_seed.mol_to_xyz('seed.xyz')
        seeds2.mol_to_xyz('monomer.xyz')
        mol_id = '{0}_{1}_{2}'.format(seed_id, aggregate_id1, aggregate_id2)

        all_orientations = tabu.generate_orientations(mol_id,
                                                      seeds1[seed_count],
                                                      seeds2, hm_orientations)
        not_converged = all_orientations[:]
        converged = []
        for i in range(10):
            aggregator_logger.info(
                "Round %d of block optimizations with %d molecules" %
                (i + 1, len(not_converged)))
            if len(not_converged) == 0:
                break
            status_list = [
                optimise(each_mol, method, max_cycles=350, convergence='loose')
                for each_mol in not_converged
            ]
            converged = [
                n for n, s in zip(not_converged, status_list) if s is True
            ]
            list_of_optimized_molecules.extend(converged)
            not_converged = [
                n for n, s in zip(not_converged, status_list)
                if s == 'CycleExceeded'
            ]
            not_converged = clustering.remove_similar(not_converged)

        os.chdir(cwd)
    print(list_of_optimized_molecules)
    if len(list_of_optimized_molecules) < 2:
        return list_of_optimized_molecules
    print("  Clustering")
    selected_seeds = clustering.choose_geometries(list_of_optimized_molecules)
    file_manager.make_directories('selected')
    for each_file in selected_seeds:
        status = optimise(each_file,
                          method,
                          max_cycles=350,
                          convergence='normal')
        if status is True:
            xyz_file = 'seed_' + each_file.name[
                4:
                7] + '/job_' + each_file.name + '/result_' + each_file.name + '.xyz'
            shutil.copy(xyz_file, 'selected/')
        else:
            selected_seeds.remove(each_file)
    return selected_seeds
Пример #4
0
def main():
    args = argument_parse()
    if args.verbosity == 0:
        logger.setLevel(logging.DEBUG)
        formatter = logging.Formatter(
            '%(name)-12s %(filename)s %(funcName)s '
            '%(lineno)d %(levelname)-8s: %(message)s')
    elif args.verbosity == 1:
        formatter = logging.Formatter('%(message)s')
        logger.setLevel(logging.INFO)
    elif args.verbosity == 2:
        formatter = logging.Formatter('%(message)s')
        logger.setLevel(logging.WARNING)
    elif args.verbosity == 3:
        formatter = logging.Formatter('%(message)s')
        logger.setLevel(logging.ERROR)
    elif args.verbosity == 4:
        formatter = logging.Formatter('%(message)s')
        logger.setLevel(logging.CRITICAL)
    else:
        formatter = logging.Formatter('%(message)s')
        logger.setLevel(logging.CRITICAL)

    handler.setFormatter(formatter)
    logger.addHandler(handler)

    logger.info('Starting PyAR at %s in %s' %
                (datetime.datetime.now(), os.getcwd()))
    logger.debug('Logging level is %d' % args.verbosity)
    logger.debug('Parsed arguments %s' % args)

    method_args = {
        'charge': args.charge,
        'multiplicity': args.multiplicity,
        'scftype': args.scftype,
        'software': args.software
    }

    logger.info('Charge:        %s' % method_args['charge'])
    logger.info('Multiplicity:  %s' % method_args['multiplicity'])
    logger.info('SCF Type:      %s' % method_args['scftype'])

    logger.info('QM Software:  %s' % method_args['software'])

    how_many_orientations = args.hm_orientations
    logger.info('%s orientations will be used' % how_many_orientations)

    input_molecules = setup_molecules(args.input_files)

    if args.site is None:
        site = None
        proximity_factor = 2.3
    else:
        site = args.site
        proximity_factor = 2.3

    if args.aggregate:
        size_of_aggregate = args.aggregate_size
        if size_of_aggregate is None:
            logger.error('For an Aggregation run '
                         'specify the aggregate size '
                         '(number of monomers to be added) '
                         'using the argument\n -as <integer>')
            sys.exit('Missing arguments: -as #')

        if how_many_orientations is None:
            logger.error("For aggregation, specify how many orientations"
                         "are to be used, by the argument\n"
                         "-number_of_orientations <number of orientations>")
            sys.exit('Missing arguments: -N #')

        if len(input_molecules) == 1:
            print('Provide at least two files')
            sys.exit('Missing arguments: Provide at least two files')
        else:
            monomer = input_molecules[-1]
            seeds = input_molecules[:-1]

        t1 = time.clock()
        aggregator.aggregate(seeds,
                             monomer,
                             aggregate_size=size_of_aggregate,
                             hm_orientations=how_many_orientations,
                             method=method_args)

        logger.info('Total Time: {}'.format(time.clock() - t1))

    if args.react:
        minimum_gamma = args.gmin
        maximum_gamma = args.gmax
        if len(input_molecules) == 1:
            logger.error('Reactor requires at least two molecules')
            sys.exit('Missing arguments: provide at least two molecules')
        if minimum_gamma is None or maximum_gamma is None:
            logger.error('For a Reactor run specify the '
                         'values of gamma_min and gamma_max using \n'
                         '-gmin <integer> -gmax <integer>')
            sys.exit('missing arguments: -gmin <integer> -gmax <integer>')
        if how_many_orientations is None:
            logger.error("For reaction, specify how many orientations"
                         "are to be used, by the argument\n"
                         "-number_of_orientations <number of orientations>")
            sys.exit('Missing argumetns: -N #')

        if site is not None:
            site = [site[0], input_molecules[0].number_of_atoms + site[1]]
        t1 = time.clock()
        number_of_orientations = int(how_many_orientations)
        reactor.react(input_molecules[0],
                      input_molecules[1],
                      gamma_min=minimum_gamma,
                      gamma_max=maximum_gamma,
                      hm_orientations=number_of_orientations,
                      method=method_args,
                      site=site,
                      proximity_factor=proximity_factor)
        logger.info('Total run time: {}'.format(time.clock() - t1))
        return

    if args.optimize:
        if args.gamma:
            gamma = args.gamma
        else:
            gamma = 0.0

        list_of_optimized_molecules = optimiser.bulk_optimize(
            input_molecules, method_args, gamma)
        if len(list_of_optimized_molecules) == 0:
            print('no optimized molecules')
        energy_dict = {n.name: n.energy for n in input_molecules}
        write_csv_file('energy.csv', energy_dict)
        from data_analysis import clustering
        clustering_results = clustering.choose_geometries(
            list_of_optimized_molecules)
        logger.info(clustering_results)

    if args.makebond:
        a = args.makebond[0]
        b = input_molecules[0].number_of_atoms + args.makebond[1]
        if how_many_orientations is None:
            logger.error("For aggregation, specify how many orientations"
                         "are    to be used, by the argument\n"
                         "-N <number of orientations>")
            sys.exit('Missing arguments: -N #')

        molecules = tabu.generate_guess_for_bonding(
            'abc', input_molecules[0], input_molecules[1], a, b,
            int(number_of_orientations))
        for each_molecule in molecules:
            coordinates = each_molecule.coordinates
            start_dist = np.linalg.norm(coordinates[a] - coordinates[b])
            final_distance = each_molecule.covalent_radius[a] + \
                             each_molecule.covalent_radius[b]
            step = int(abs(final_distance - start_dist) * 10)
            if args.software == 'orca':
                c_k = '\n!ScanTS\n% geom scan B ' + str(a) + ' ' + str(b) +\
                      '= ' + str(start_dist) + ', ' + str(final_distance) + \
                      ', ' + str(step) + ' end end\n'

                optimiser.optimise(each_molecule,
                                   method_args,
                                   0.0,
                                   custom_keyword=c_k)
            else:
                print('Optimization with %s is not implemented yet' %
                      args.software)