Exemple #1
0
 def test_mistaken_macro(self):
     swap_args = {"swap": ["VP"], "debug": True}
     bare_swap = AtomicSwapper([], maintain_num_species=False)
     bare_swap.periodic_table = get_periodic_table()
     bare_swap.swap_args = swap_args["swap"]
     # try to parse swaps
     bare_swap.parse_swaps()
     self.assertEqual(bare_swap.swap_pairs, [[["V"], ["P"]]])
Exemple #2
0
 def test_null_self_swap(self):
     # spoof AtomicSwapper __init__
     swap_args = {"swap": ["KK:PP"], "debug": True}
     bare_swap = AtomicSwapper([])
     bare_swap.periodic_table = get_periodic_table()
     bare_swap.swap_args = swap_args["swap"]
     # try to parse swaps
     bare_swap.parse_swaps()
     self.assertEqual(bare_swap.swap_pairs, [[["K"], ["K"]], [["P"], ["P"]]])
     # set up test data for real swap
     doc = dict()
     doc["atom_types"] = ["K", "K", "P", "P"]
     swapped_docs, num_swapped = bare_swap.atomic_swaps(doc)
     self.assertEqual(num_swapped, 0)
Exemple #3
0
 def test_maintain_num_species(self):
     # spoof AtomicSwapper __init__
     swap_args = {"swap": ["[Li,Na]K"], "debug": True}
     bare_swap = AtomicSwapper([])
     bare_swap.periodic_table = get_periodic_table()
     bare_swap.swap_args = swap_args["swap"]
     # try to parse swaps
     bare_swap.parse_swaps()
     # set up test data for real swap
     doc = dict()
     doc["atom_types"] = ["Li", "Na"]
     swapped_docs, num_swapped = bare_swap.atomic_swaps(doc)
     print(swapped_docs)
     self.assertEqual(num_swapped, 0)
Exemple #4
0
 def test_many_to_one_macro(self):
     swap_args = {"swap": ["[V]P"], "debug": True}
     bare_swap = AtomicSwapper([], maintain_num_species=False)
     bare_swap.periodic_table = get_periodic_table()
     bare_swap.swap_args = swap_args["swap"]
     # try to parse swaps
     bare_swap.parse_swaps()
     self.assertEqual(bare_swap.swap_pairs, [[["N", "P", "As", "Sb", "Bi"], ["P"]]])
     # set up test data for real swap
     doc = dict()
     doc["atom_types"] = ["P", "Sb", "As", "As"]
     swapped_docs, num_swapped = bare_swap.atomic_swaps(doc)
     self.assertEqual(num_swapped, 1)
     self.assertEqual(swapped_docs[0]["atom_types"], ["P", "P", "P", "P"])
Exemple #5
0
 def test_multiple_simple_swap(self):
     # spoof AtomicSwapper __init__
     swap_args = {"swap": ["AsP:LiNa"], "debug": True}
     bare_swap = AtomicSwapper([])
     bare_swap.periodic_table = get_periodic_table()
     bare_swap.swap_args = swap_args["swap"]
     # try to parse swaps
     bare_swap.parse_swaps()
     self.assertEqual(bare_swap.swap_pairs, [[["As"], ["P"]], [["Li"], ["Na"]]])
     # set up test data for real swap
     doc = dict()
     doc["atom_types"] = ["Li", "Li", "As", "As"]
     swapped_docs, num_swapped = bare_swap.atomic_swaps(doc)
     self.assertEqual(num_swapped, 1)
     self.assertEqual(swapped_docs[0]["atom_types"], ["Na", "Na", "P", "P"])
Exemple #6
0
 def test_one_to_many_swap(self):
     # spoof AtomicSwapper __init__
     swap_args = {"swap": ["As[P,Sb,Zn,Cu]"], "debug": True}
     bare_swap = AtomicSwapper([])
     bare_swap.periodic_table = get_periodic_table()
     bare_swap.swap_args = swap_args["swap"]
     # try to parse swaps
     bare_swap.parse_swaps()
     self.assertEqual(bare_swap.swap_pairs, [[["As"], ["P", "Sb", "Zn", "Cu"]]])
     # set up test data for real swap
     doc = dict()
     doc["atom_types"] = ["Li", "Li", "As", "As"]
     swapped_docs, num_swapped = bare_swap.atomic_swaps(doc)
     self.assertEqual(num_swapped, 4)
     P_found = False
     Sb_found = False
     Zn_found = False
     Cu_found = False
     for new_doc in swapped_docs:
         self.assertTrue("As" not in new_doc["atom_types"])
         if "P" in new_doc["atom_types"]:
             self.assertTrue(
                 x not in new_doc["atom_types"] for x in ["Sb", "Zn", "Cu"]
             )
             self.assertEqual(new_doc["atom_types"], ["Li", "Li", "P", "P"])
             P_found = True
         if "Sb" in new_doc["atom_types"]:
             self.assertTrue(
                 x not in new_doc["atom_types"] for x in ["P", "Zn", "Cu"]
             )
             self.assertEqual(new_doc["atom_types"], ["Li", "Li", "Sb", "Sb"])
             Sb_found = True
         if "Zn" in new_doc["atom_types"]:
             self.assertTrue(
                 x not in new_doc["atom_types"] for x in ["P", "Sb", "Cu"]
             )
             self.assertEqual(new_doc["atom_types"], ["Li", "Li", "Zn", "Zn"])
             Zn_found = True
         if "Cu" in new_doc["atom_types"]:
             self.assertTrue(
                 x not in new_doc["atom_types"] for x in ["P", "Sb", "Zn"]
             )
             self.assertEqual(new_doc["atom_types"], ["Li", "Li", "Cu", "Cu"])
             Cu_found = True
     self.assertTrue(P_found)
     self.assertTrue(Sb_found)
     self.assertTrue(Zn_found)
     self.assertTrue(Cu_found)
Exemple #7
0
 def test_multiple_many_to_one_swap(self):
     # spoof AtomicSwapper __init__
     swap_args = {"swap": ["[Li,Na]K:[Ru, Rh]La"], "debug": True}
     bare_swap = AtomicSwapper([], maintain_num_species=False)
     bare_swap.periodic_table = get_periodic_table()
     bare_swap.swap_args = swap_args["swap"]
     # try to parse swaps
     bare_swap.parse_swaps()
     self.assertEqual(
         bare_swap.swap_pairs, [[["Li", "Na"], ["K"]], [["Ru", "Rh"], ["La"]]]
     )
     # set up test data for real swap
     doc = dict()
     doc["atom_types"] = ["Li", "Na", "Ru", "Rh"]
     swapped_docs, num_swapped = bare_swap.atomic_swaps(doc)
     self.assertEqual(num_swapped, 1)
     self.assertEqual(swapped_docs[0]["atom_types"], ["K", "K", "La", "La"])
Exemple #8
0
 def test_many_to_many_swap_awkward(self):
     # spoof AtomicSwapper __init__
     swap_args = {"swap": ["[Li,Na]K:[V,I][I]"], "debug": True}
     bare_swap = AtomicSwapper([], maintain_num_species=False)
     bare_swap.periodic_table = get_periodic_table()
     bare_swap.swap_args = swap_args["swap"]
     # try to parse swaps
     bare_swap.parse_swaps()
     self.assertEqual(
         bare_swap.swap_pairs,
         [[["Li", "Na"], ["K"]], [["V", "I"], ["Li", "Na", "K", "Rb", "Cs", "Fr"]]],
     )
     # set up test data for real swap
     doc = dict()
     doc["atom_types"] = ["Li", "Na", "V", "I"]
     swapped_docs, num_swapped = bare_swap.atomic_swaps(doc)
     self.assertEqual(num_swapped, 6)
     self.assertEqual(swapped_docs[0]["atom_types"], ["K", "K", "Li", "Li"])
Exemple #9
0
    def __init__(self, *args, **kwargs):
        """ Initialise the query with command line arguments and return
        results.

        """
        # read args
        self.kwargs = kwargs
        self.args = vars(args[0])
        self.args['no_quickstart'] = self.kwargs.get('no_quickstart')
        self.argstr = kwargs.get('argstr')

        file_exts = ['cell', 'res', 'pdb', 'markdown', 'latex', 'param', 'xsf']
        self.export = any([self.args.get(ext) for ext in file_exts])

        self.subcommand = self.args.pop("subcmd")

        if self.subcommand != 'import':
            self.settings = load_custom_settings(
                config_fname=self.args.get('config'),
                debug=self.args.get('debug'),
                no_quickstart=self.args.get('no_quickstart'))
            result = make_connection_to_collection(
                self.args.get('db'),
                check_collection=(self.subcommand != "stats"),
                mongo_settings=self.settings)
            self.client, self.db, self.collections = result

        if self.subcommand == 'stats':
            self.stats()

        try:
            if self.subcommand == 'import':
                from matador.db import Spatula
                self.importer = Spatula(self.args)

            if self.subcommand == 'query':
                self.query = DBQuery(self.client, self.collections,
                                     **self.args)
                self.cursor = self.query.cursor

            if self.subcommand == 'swaps':
                from matador.swaps import AtomicSwapper
                self.query = DBQuery(self.client, self.collections,
                                     **self.args)
                if self.args.get('hull_cutoff') is not None:
                    self.hull = QueryConvexHull(query=self.query, **self.args)
                    self.swapper = AtomicSwapper(self.hull.hull_cursor,
                                                 **self.args)
                else:
                    self.swapper = AtomicSwapper(self.query.cursor,
                                                 **self.args)
                self.cursor = self.swapper.cursor

            if self.subcommand == 'refine':
                from matador.db import Refiner
                self.query = DBQuery(self.client, self.collections,
                                     **self.args)
                if self.args.get('hull_cutoff') is not None:
                    self.hull = QueryConvexHull(self.query, **self.args)
                    self.refiner = Refiner(self.hull.cursor, self.query.repo,
                                           **self.args)
                else:
                    self.refiner = Refiner(self.query.cursor, self.query.repo,
                                           **self.args)

                self.cursor = self.refiner.cursor

            if self.subcommand == 'hull' or self.subcommand == 'voltage':
                self.hull = QueryConvexHull(**self.args,
                                            voltage=self.subcommand ==
                                            'voltage',
                                            client=self.client,
                                            collections=self.collections)
                self.cursor = self.hull.hull_cursor

            if self.subcommand == 'changes':
                from matador.db import DatabaseChanges
                if len(self.collections) != 1:
                    raise SystemExit(
                        'Cannot view changes of more than one collection at once.'
                    )
                if self.args.get('undo'):
                    action = 'undo'
                else:
                    action = 'view'
                changeset = self.args.get('changeset')
                if changeset is None:
                    changeset = 0
                DatabaseChanges([key for key in self.collections][0],
                                changeset_ind=changeset,
                                action=action,
                                mongo_settings=self.settings,
                                override=kwargs.get('no_quickstart'))

            if self.subcommand == 'hulldiff':
                from matador.hull.hull_diff import diff_hulls
                if self.args.get('compare') is None:
                    raise SystemExit(
                        'Please specify which hulls to query with --compare.')
                diff_hulls(self.client, self.collections, **self.args)

            if self.export and self.cursor:
                from matador.export import query2files
                if self.args.get('write_n') is not None:
                    self.cursor = [
                        doc for doc in self.cursor if len(doc['stoichiometry'])
                        == self.args.get('write_n')
                    ]
                if not self.cursor:
                    print_failure('No structures left to export.')
                query2files(self.cursor,
                            **self.args,
                            argstr=self.argstr,
                            subcmd=self.subcommand,
                            hash_dupe=True)

            if self.args.get('view'):
                from matador.utils.viz_utils import viz
                if self.args.get('top') is None:
                    self.top = len(self.cursor)
                else:
                    self.top = self.args.get('top')
                if len(self.cursor[:self.top]) > 10:
                    from time import sleep
                    print_warning(
                        'WARNING: opening {} files with ase-gui...'.format(
                            len(self.cursor)))
                    print_warning(
                        'Please kill script within 3 seconds if undesired...')
                    sleep(3)
                if len(self.cursor[:self.top]) > 20:
                    print_failure(
                        'You will literally be opening that many windows, ' +
                        'I\'ll give you another 5 seconds to reconsider...')

                    sleep(5)
                    print_notify('It\'s your funeral...')
                    sleep(1)
                for doc in self.cursor[:self.top]:
                    viz(doc)

            if self.subcommand != 'import':
                self.client.close()

        except (RuntimeError, SystemExit, KeyboardInterrupt) as oops:
            if isinstance(oops, RuntimeError):
                print_failure(oops)
            elif isinstance(oops, SystemExit):
                print_warning(oops)
            try:
                self.client.close()
            except AttributeError:
                pass
            raise oops