def test_compute_dataset(fractal_compute_server): client = portal.FractalClient(fractal_compute_server.get_address("")) ds_name = "He_PES" ds = portal.collections.Dataset(ds_name, client, ds_type="ie") # Adds options option = portal.data.get_options("psi_default") opt_ret = client.add_options([option]) opt_key = option["name"] # Add two helium dimers to the DB at 4 and 8 bohr He1 = portal.Molecule([[2, 0, 0, -2], [2, 0, 0, 2]], dtype="numpy", units="bohr", frags=[1]) ds.add_ie_rxn("He1", He1, attributes={"r": 4}, reaction_results={"default": {"Benchmark": 0.0009608501557}}) # Save the DB and re-acquire via classmethod r = ds.save() ds = portal.collections.Dataset.from_server(client, ds_name) assert "Dataset(" in str(ds) # Test collection lists ret = client.list_collections() assert ret == {"dataset": [ds_name]} ret = client.list_collections("dataset") assert ret == [ds_name] He2 = portal.Molecule([[2, 0, 0, -4], [2, 0, 0, 4]], dtype="numpy", units="bohr", frags=[1]) ds.add_ie_rxn("He2", He2, attributes={"r": 4}, reaction_results={"default": {"Benchmark": -0.00001098794749}}) # Save the DB and overwrite the result, reacquire via client r = ds.save(overwrite=True) ds = client.get_collection("dataset", ds_name) # Compute SCF/sto-3g ret = ds.compute("SCF", "STO-3G") assert len(ret["submitted"]) == 3 fractal_compute_server.objects["queue_nanny"].await_results() # Query computed results assert ds.query("SCF", "STO-3G") assert pytest.approx(0.6024530476071095, 1.e-5) == ds.df.loc["He1", "SCF/STO-3G"] assert pytest.approx(-0.006895035942673289, 1.e-5) == ds.df.loc["He2", "SCF/STO-3G"] # Check results assert ds.query("Benchmark", "", reaction_results=True) assert pytest.approx(0.00024477933196125805, 1.e-5) == ds.statistics("MUE", "SCF/STO-3G")
def test_task_molecule_no_orientation(data, fractal_compute_server): """ Molecule orientation should not change on compute """ # Reset database each run reset_server_database(fractal_compute_server) client = ptl.FractalClient(fractal_compute_server) mol = ptl.Molecule(symbols=["H", "H"], geometry=[0, 0, 0, 0, 5, 0], connectivity=[(0, 1, 1)]) mol_id = client.add_molecules([mol])[0] program, method, basis = data ret = client.add_compute(program, method, basis, "energy", None, [mol_id]) # Manually handle the compute fractal_compute_server.await_results() # Check for the single result ret = client.query_results(id=ret.submitted) assert len(ret) == 1 assert ret[0].status == "COMPLETE" assert ret[0].molecule == mol_id # Make sure no other molecule was added ret = client.query_molecules(molecular_formula=["H2"]) assert len(ret) == 1 assert ret[0].id == mol_id
def test_procedure_optimization(fractal_compute_server): # Add a hydrogen molecule hydrogen = portal.Molecule([[1, 0, 0, -0.672], [1, 0, 0, 0.672]], dtype="numpy", units="bohr") client = portal.FractalClient(fractal_compute_server.get_address("")) mol_ret = client.add_molecules({"hydrogen": hydrogen.to_json()}) # Add compute compute = { "meta": { "procedure": "optimization", "program": "geometric", "options": "none", "qc_meta": { "driver": "gradient", "method": "HF", "basis": "sto-3g", "options": "none", "program": "psi4" }, }, "data": [mol_ret["hydrogen"]], } # Ask the server to compute a new computation r = requests.post(fractal_compute_server.get_address("task_scheduler"), json=compute) assert r.status_code == 200 # Get the first submitted job, the second index will be a hash_index submitted = r.json()["data"]["submitted"] compute_key = submitted[0][1] # Manually handle the compute nanny = fractal_compute_server.objects["queue_nanny"] nanny.await_results() assert len(nanny.list_current_tasks()) == 0 # # Query result and check against out manual pul results1 = client.get_procedures({"program": "geometric"}) results2 = client.get_procedures({"hash_index": compute_key}) for results in [results1, results2]: assert len(results) == 1 assert isinstance(str(results[0]), str) # Check that repr runs assert pytest.approx(-1.117530188962681, 1e-5) == results[0].final_energy()
import qcfractal.interface as portal # Builds a blank database object # Tell the database we are going to build interaction energies db = portal.collections.Database("Water", db_type="ie") # Portal has some molecules stored for easy access. water_dimer = portal.data.get_molecule("water_dimer_minima.psimol") water_dimer_stretch = portal.data.get_molecule("water_dimer_stretch.psimol") # We can also create a new molecule from canonical strings such as a Psi4 molecule helium_dimer = portal.Molecule("He 0 0 -5\n--\nHe 0 0 5", dtype="psi4") # Add several intermolecular interaction, dimers are automatically fragmented db.add_ie_rxn("Water Dimer", water_dimer) db.add_ie_rxn("Water Dimer Stretch", water_dimer_stretch) db.add_ie_rxn("Helium Dimer", helium_dimer) #print(helium_dimer) #import json #print(json.dumps(db.data["reactions"][2], indent=2)) # Build a interface to the server p = portal.FractalClient("localhost:7777") # Add the database to the server db.save(p)
def test_procedure_optimization(fractal_compute_server): # Add a hydrogen molecule hydrogen = portal.Molecule([[1, 0, 0, -0.672], [1, 0, 0, 0.672]], dtype="numpy", units="bohr") client = portal.FractalClient(fractal_compute_server.get_address("")) mol_ret = client.add_molecules({"hydrogen": hydrogen.to_json()}) # Add compute compute = { "meta": { "procedure": "optimization", "program": "geometric", "options": "none", "qc_meta": { "driver": "gradient", "method": "HF", "basis": "sto-3g", "options": "none", "program": "psi4" }, }, "data": [mol_ret["hydrogen"]], } # Ask the server to compute a new computation r = requests.post(fractal_compute_server.get_address("task_scheduler"), json=compute) assert r.status_code == 200 # Get the first submitted job, the second index will be a hash_index submitted = r.json()["data"]["submitted"] compute_key = submitted[0] # Manually handle the compute nanny = fractal_compute_server.objects["queue_nanny"] nanny.await_results() assert len(nanny.list_current_tasks()) == 0 # # Query result and check against out manual pul results1 = client.get_procedures({"program": "geometric"}) results2 = client.get_procedures({"queue_id": compute_key}) for results in [results1, results2]: assert len(results) == 1 assert isinstance(str(results[0]), str) # Check that repr runs assert pytest.approx(-1.117530188962681, 1e-5) == results[0].final_energy() # Check pulls traj = results[0].get_trajectory(projection={"properties": True}) energies = results[0].energies() assert len(traj) == len(energies) assert results[0].final_molecule()["symbols"] == ["H", "H"] # Check individual elements for ind in range(len(results[0]._trajectory)): raw_energy = traj[ind]["properties"]["return_energy"] assert pytest.approx(raw_energy, 1.e-5) == energies[ind] # Check that duplicates are caught r = requests.post(fractal_compute_server.get_address("task_scheduler"), json=compute) assert r.status_code == 200 assert len(r.json()["data"]["completed"]) == 1
def test_compute_queue_stack(fractal_compute_server): # Add a hydrogen and helium molecule hydrogen = portal.Molecule([[1, 0, 0, -0.5], [1, 0, 0, 0.5]], dtype="numpy", units="bohr") helium = portal.Molecule([[2, 0, 0, 0.0]], dtype="numpy", units="bohr") storage = fractal_compute_server.objects["storage_socket"] mol_ret = storage.add_molecules({ "hydrogen": hydrogen.to_json(), "helium": helium.to_json() }) hydrogen_mol_id = mol_ret["data"]["hydrogen"] helium_mol_id = mol_ret["data"]["helium"] option = portal.data.get_options("psi_default") opt_ret = storage.add_options([option]) opt_key = option["name"] # Add compute compute = { "meta": { "procedure": "single", "driver": "energy", "method": "HF", "basis": "sto-3g", "options": opt_key, "program": "psi4", }, "data": [hydrogen_mol_id, helium.to_json()], } # Ask the server to compute a new computation r = requests.post(fractal_compute_server.get_address("task_scheduler"), json=compute) assert r.status_code == 200 # Manually handle the compute nanny = fractal_compute_server.objects["queue_nanny"] nanny.await_results() assert len(nanny.list_current_tasks()) == 0 # Query result and check against out manual pul results_query = { "program": "psi4", "molecule_id": [hydrogen_mol_id, helium_mol_id], "method": compute["meta"]["method"], "basis": compute["meta"]["basis"] } results = storage.get_results(results_query)["data"] assert len(results) == 2 for r in results: if r["molecule_id"] == hydrogen_mol_id: assert pytest.approx(-1.0660263371078127, 1e-5) == r["properties"]["scf_total_energy"] elif r["molecule_id"] == helium_mol_id: assert pytest.approx(-2.807913354492941, 1e-5) == r["properties"]["scf_total_energy"] else: raise KeyError("Returned unexpected Molecule ID.")
def test_compute_database(fractal_compute_server): client = portal.FractalClient(fractal_compute_server.get_address("")) db_name = "He_PES" db = portal.collections.Database(db_name, client, db_type="ie") # Adds options option = portal.data.get_options("psi_default") opt_ret = client.add_options([option]) opt_key = option["name"] # Add two helium dimers to the DB at 4 and 8 bohr He1 = portal.Molecule([[2, 0, 0, -2], [2, 0, 0, 2]], dtype="numpy", units="bohr", frags=[1]) db.add_ie_rxn("He1", He1, attributes={"r": 4}, reaction_results={"default": { "Benchmark": 0.0009608501557 }}) # Save the DB and re-acquire r = db.save() db = portal.collections.Database.from_server(client, db_name) He2 = portal.Molecule([[2, 0, 0, -4], [2, 0, 0, 4]], dtype="numpy", units="bohr", frags=[1]) db.add_ie_rxn( "He2", He2, attributes={"r": 4}, reaction_results={"default": { "Benchmark": -0.00001098794749 }}) # Save the DB and overwrite the result r = db.save(overwrite=True) # Open a new database db = portal.collections.Database.from_server(client, db_name) # Compute SCF/sto-3g ret = db.compute("SCF", "STO-3G") fractal_compute_server.objects["queue_nanny"].await_results() # Query computed results assert db.query("SCF", "STO-3G") assert pytest.approx(0.6024530476071095, 1.e-5) == db.df.loc["He1", "SCF/STO-3G"] assert pytest.approx(-0.006895035942673289, 1.e-5) == db.df.loc["He2", "SCF/STO-3G"] # Check results assert db.query("Benchmark", "", reaction_results=True) assert pytest.approx(0.00024477933196125805, 1.e-5) == db.statistics("MUE", "SCF/STO-3G")