import torch import mysql.connector import numpy as np import tqdm from tqdm.contrib.concurrent import process_map from ase.data import atomic_numbers from materials_toolkit.data import HDF5Dataset, StructureData, batching, Batching import os @batching( entry_id=Batching(dtype=torch.long), structure_id=Batching(dtype=torch.long), calculation_id=Batching(dtype=torch.long), spacegroup=Batching(dtype=torch.long), ) class OQMDData(StructureData): pass class OQMD(HDF5Dataset): data_class = OQMDData def get_all_structures(host, user, password, database="qmdb"): with mysql.connector.connect( host=host, user=user, password=password, database=database ) as db: structures = [] with db.cursor() as c: # calculate atoms count for all structures c.execute( 'select TABLE_NAME from information_schema.tables where table_name="structures_natoms";' ) if len(c.fetchall()) == 0: c.execute( "create table structures_natoms select structure_id,count(*) as natoms from atoms group by structure_id;" ) # filter valid structures (natoms match between structures table and structures_natoms) c.execute( 'select TABLE_NAME from information_schema.tables where table_name="valid_structure";' ) if len(c.fetchall()) == 0: c.execute( """ create table valid_structure select structures.id as structure_id,structures.natoms as natoms from structures inner join structures_natoms on structures.id=structures_natoms.structure_id and structures.natoms=structures_natoms.natoms; """ ) # get minimal energy of an entry, select lower energy from converged calculations and reject lda setup c.execute( 'select TABLE_NAME from information_schema.tables where table_name="entries_minimal_energy";' ) if len(c.fetchall()) == 0: # TODO filtrer standard et static c.execute( f""" create table entries_minimal_energy select calculations.entry_id as entry_id, min(calculations.energy_pa) as energy_pa from calculations inner join valid_structure on calculations.converged is not null and calculations.converged=true and calculations.label NOT LIKE '%lda' and (calculations.label LIKE 'static%' or calculations.label LIKE 'standard%') and valid_structure.structure_id=calculations.output_id group by calculations.entry_id; """ ) c.execute( 'select TABLE_NAME from information_schema.tables where table_name="entries_with_structure";' ) if len(c.fetchall()) == 0: c.execute( """ create table entries_with_structure select calculations.entry_id as entry_id, calculations.output_id as structure_id, calculations.id as calculation_id, calculations.label as calculation_label, calculations.energy_pa as energy_pa from entries_minimal_energy left join calculations on calculations.entry_id=entries_minimal_energy.entry_id and calculations.energy_pa=entries_minimal_energy.energy_pa inner join valid_structure on valid_structure.structure_id=calculations.output_id group by calculations.entry_id; """ ) c.execute("select * from entries_with_structure;") entries_calc = c.fetchall() for entry_id, struct_id, calc_id, calc_label, energy_pa in tqdm.tqdm( entries_calc ): c.execute( f""" select struct.natoms, spacegroups.hm, struct.x1, struct.x2, struct.x3, struct.y1, struct.y2, struct.y3, struct.z1, struct.z2, struct.z3, struct.volume from ( select natoms, x1, x2, x3, y1, y2, y3, z1, z2, z3, volume, coalesce(spacegroup_id, 1) as spacegroup_id from structures where structures.id={struct_id} ) as struct inner join spacegroups on struct.spacegroup_id=spacegroups.number; """ ) results = c.fetchall() assert len(results) == 1, f"error structure {struct_id}" ( natoms, space_group, x1, x2, x3, y1, y2, y3, z1, z2, z3, volume, ) = results[0] c.execute( f""" select element_id,x,y,z from atoms where structure_id={struct_id} """ ) atoms = c.fetchall() if len(atoms) != natoms: print(entry_id, struct_id, calc_label, energy_pa) for element_id, x, y, z in atoms: print(f"{element_id:>4s} {x:>.3f} {y:>.3f} {z:>.3f}") assert len(atoms) == natoms, f"error {len(atoms)} {natoms}" z_lst = [] coords = [] for e_id, x, y, z in atoms: z_lst.append(atomic_numbers[e_id]) coords.append([x, y, z]) coords = np.array(coords) cell = np.array([[x1, x2, x3], [y1, y2, y3], [z1, z2, z3]]) structures.append( OQMDData( z=torch.tensor(z_lst, dtype=torch.long).view(-1), pos=torch.from_numpy(coords).float().view(-1, 3), cell=torch.from_numpy(cell).float().view(1, 3, 3), entry_id=torch.tensor([entry_id]).long().view(1), structure_id=torch.tensor([struct_id]).long().view(1), calculation_id=torch.tensor([calc_id]).long().view(1), spacegroup=torch.tensor([space_group]).long().view(1), energy_pa=torch.tensor([energy_pa]).float().view(1), ) ) return structures def main(): import argparse parser = argparse.ArgumentParser() parser.add_argument("--host", default="localhost", help="mysql host serveur") parser.add_argument("--database", default="qmdb", help="name of the database") parser.add_argument("--user", help="user of the mysql server") parser.add_argument("--password", help="password of the user") args = parser.parse_args() structures = get_all_structures( host=args.host, user=args.user, password=args.password, database="qmdb" ) oqmd_dir = os.path.join(".", "open-quantum-materials-database") processed_dir = os.path.join(oqmd_dir, "processed") HDF5Dataset.create_dataset(processed_dir, structures) dataset = OQMD(oqmd_dir) dataset.compute_convex_hulls() if __name__ == "__main__": main() # check entry 351317