From 1a195f90ccc6983415fce92fd9eb0088fd955a20 Mon Sep 17 00:00:00 2001 From: Abdallah Ammar Date: Wed, 28 Aug 2024 01:06:31 +0200 Subject: [PATCH] add SQLite database --- tests/create_database.py | 42 ++++++++++++++++++++ tests/molecule.py | 86 ++++++++++++++++++++++++++++++++++++++++ tests/test_hf.py | 58 +++++++++++++-------------- 3 files changed, 157 insertions(+), 29 deletions(-) create mode 100644 tests/create_database.py create mode 100644 tests/molecule.py diff --git a/tests/create_database.py b/tests/create_database.py new file mode 100644 index 0000000..3a91252 --- /dev/null +++ b/tests/create_database.py @@ -0,0 +1,42 @@ + +import sqlite3 + +from molecule import Molecule +from molecule import save_molecules_to_json, load_molecules_from_json +from molecule import create_database, add_molecule_to_db + + + +molecules = [ + Molecule( + name="H2O", + multiplicity=1, + geometry=[ + {"element": "O", "x": 0.000000, "y": 0.000000, "z": 0.117790}, + {"element": "H", "x": 0.000000, "y": 0.755453, "z": -0.471161}, + {"element": "H", "x": 0.000000, "y": -0.755453, "z": -0.471161} + ], + energies={ + "RHF": { + "cc-pvdz": -76.0267058009, + "cc-pvtz": -76.0570239304, + "cc-pvqz": -76.0646816616 + }, + } + ), +] + + +# Save molecules to JSON +save_molecules_to_json(molecules, 'molecules.json') + +# Load molecules from JSON +loaded_molecules = load_molecules_from_json('molecules.json') +print(loaded_molecules) + +# Create a database and add molecules +db_name = 'molecules.db' +create_database(db_name) +for molecule in molecules: + add_molecule_to_db(db_name, molecule) + diff --git a/tests/molecule.py b/tests/molecule.py new file mode 100644 index 0000000..9d7b648 --- /dev/null +++ b/tests/molecule.py @@ -0,0 +1,86 @@ + +import json +import sqlite3 + +class Molecule: + def __init__(self, name, multiplicity, geometry, energies): + self.name = name + self.multiplicity = multiplicity + self.geometry = geometry # List of tuples (atom, x, y, z) + self.energies = energies # Dictionary of dictionaries: {method: {basis: energy}} + + def get_energy(self, method, basis): + """Retrieve energy for a specific method and basis set.""" + return self.energies.get(method, {}).get(basis, None) + + def to_dict(self): + return { + "name": self.name, + "multiplicity": self.multiplicity, + "geometry": self.geometry, + "energies": self.energies, + } + + @staticmethod + def from_dict(data): + return Molecule( + name=data["name"], + multiplicity=data["multiplicity"], + geometry=data["geometry"], + energies=data["energies"] + ) + +def save_molecules_to_json(molecules, filename): + with open(filename, 'w') as f: + json_data = [molecule.to_dict() for molecule in molecules] + json.dump(json_data, f, indent=4) + +def load_molecules_from_json(filename): + with open(filename, 'r') as f: + json_data = json.load(f) + return [Molecule.from_dict(data) for data in json_data] + + +def create_database(db_name): + conn = sqlite3.connect(db_name) + cursor = conn.cursor() + cursor.execute('''CREATE TABLE IF NOT EXISTS molecules + (name TEXT, multiplicity INTEGER, geometry TEXT, energies TEXT)''') + conn.commit() + conn.close() + +def add_molecule_to_db(db_name, molecule): + conn = sqlite3.connect(db_name) + cursor = conn.cursor() + geometry_str = json.dumps(molecule.geometry) + energies_str = json.dumps(molecule.energies) + cursor.execute("INSERT INTO molecules VALUES (?, ?, ?, ?)", + (molecule.name, molecule.multiplicity, geometry_str, energies_str)) + conn.commit() + conn.close() + +def get_molecules_from_db(db_name): + conn = sqlite3.connect(db_name) + cursor = conn.cursor() + cursor.execute("SELECT name, multiplicity, geometry, energies FROM molecules") + rows = cursor.fetchall() + molecules = [] + for row in rows: + name, multiplicity, geometry_str, energies_str = row + geometry = json.loads(geometry_str) + energies = json.loads(energies_str) # energies is a dictionary of dictionaries + molecules.append(Molecule(name, multiplicity, geometry, energies)) + conn.close() + return molecules + +def write_geometry_to_xyz(molecule, filename): + with open(filename, 'w') as f: + # First line: number of atoms + f.write(f"{len(molecule.geometry)}\n") + # Second line: empty comment line + f.write("\n") + # Remaining lines: atom positions + for atom, x, y, z in molecule.geometry: + f.write(f"{atom} {x:.6f} {y:.6f} {z:.6f}\n") + + diff --git a/tests/test_hf.py b/tests/test_hf.py index f25183f..fd04121 100644 --- a/tests/test_hf.py +++ b/tests/test_hf.py @@ -5,6 +5,9 @@ import subprocess import platform from datetime import datetime +from molecule import get_molecules_from_db + + current_date = datetime.now() quack_root = os.getenv('QUACK_ROOT') @@ -65,15 +68,6 @@ list_opt = [hf_opt, mp_opt, cc_opt, tda_opt, gf_opt, gw_opt, gt_opt, acfdt_opt, # --- -mol_multip = { - "Ne": 1, - "H2O": 1, -} - -list_basis = ["cc-pvdz", "cc-pvtz", "cc-pvqz"] - -# --- - class class_RHF: def gen_input(): @@ -163,30 +157,36 @@ def main(): work_path.mkdir(parents=True, exist_ok=True) print(f"Directory '{work_path}' created.\n") - for methd in ["RHF", "UHF", "GHF", "ROHF"]: + for mol in molecules: - work_methd = Path('{}/{}'.format(work_path, methd)) - if not work_methd.exists(): - work_methd.mkdir(parents=True, exist_ok=True) - print(f"Directory '{work_methd}' created.\n") + mol_name = mol.name + mol_mult = mol.multiplicity - class_methd = class_map.get(methd) - # create input files - class_methd.gen_input() + for methd in list_methd: - for mol in mol_multip: + if methd not in mol.energies: + print(f"Method {methd} does not exist for {mol_name}.") + continue - multip = mol_multip[mol] + for bas, _ in mol.energies[methd].items(): - for bas in list_basis: + work_methd = Path('{}/{}'.format(work_path, methd)) + if not work_methd.exists(): + work_methd.mkdir(parents=True, exist_ok=True) + print(f"Directory '{work_methd}' created.\n") + + class_methd = class_map.get(methd) + + # create input files + class_methd.gen_input() + + file_out = "{}/{}/{}_{}_{}.out".format(work_path, methd, mol_name, mol_mult, bas) - file_out = "{}/{}/{}_{}_{}.out".format(work_path, methd, mol, multip, bas) - - print(" testing {} for {}@{} (2S+1 = {})".format(methd, mol, bas, multip)) + print(" testing {} for {}@{} (2S+1 = {})".format(methd, mol_name, bas, mol_mult)) print(" file_out: {}".format(file_out)) - class_methd.run_job(file_out, mol, bas, multip) + class_methd.run_job(file_out, mol_name, bas, mol_mult) print("\n") print("\n\n") @@ -195,10 +195,10 @@ def main(): print("\n\n\n") +db_name = 'molecules.db' +molecules = get_molecules_from_db(db_name) + +list_methd = ["RHF", "UHF", "GHF", "ROHF"] + main() - - - - -