Optimization
geomeTRIC
"""Example of using geometric subprogram to optimize H2 bond length.
Constraints docs: https://geometric.readthedocs.io/en/latest/constraints.html
"""
from qcdata import DualProgramInput, Structure
from qccompute import compute, exceptions
# Create Structure
h2 = Structure(
symbols=["H", "H"],
geometry=[[0, 0.0, 0.0], [0, 0, 1.4]], # type: ignore
)
# Define the program input
prog_input = DualProgramInput(
calctype="optimization", # type: ignore
structure=h2,
subprogram="terachem",
subprogram_args={ # type: ignore
"model": {"method": "HF", "basis": "6-31g"},
"keywords": {"purify": "no"},
},
keywords={
"check": 3,
# This is obviously a stupid constraint, but it's just an example to show how
# to use them
"constraints": {
"freeze": [
{"type": "distance", "indices": [0, 1], "value": 1.4},
],
},
},
)
# Run calculation
try:
prog_output = compute(
"geometric", prog_input, propagate_wfn=True, rm_scratch_dir=False
)
except exceptions.QCComputeBaseError as e:
# Calculation failed
prog_output = e.prog_output
print(prog_output.logs)
# Input data used to generate the calculation
print(prog_output.input_data)
# Provenance of generated calculation
print(prog_output.provenance)
print(prog_output.traceback)
raise
else:
# Check results
print("Energies:", prog_output.data.energies)
print("Structures:", prog_output.data.structures)
print("Trajectory:", prog_output.data.trajectory)
# Stdout from the program
print(prog_output.logs)
# Input data used to generate the calculation
print(prog_output.input_data)
# Provenance of generated calculation
print(prog_output.provenance)
TeraChem
"""Example of how to run an optimization calculation with TeraChem using qccompute."""
from qcdata import ProgramInput, Structure
from qccompute import compute, exceptions
# Create the structure
# Can also open a structure from a file
# structure = Structure.open("path/to/h2o.xyz")
structure = Structure(
symbols=["O", "H", "H"],
geometry=[ # type: ignore
[0.0, 0.0, 0.0],
[0.52421003, 1.68733646, 0.48074633],
[1.14668581, -0.45032174, -1.35474466],
],
)
# Define the program input
prog_input = ProgramInput(
structure=structure,
# Can be "energy", "gradient", "hessian", "optimization", "transition_state"
calctype="optimization", # type: ignore
model={"method": "hf", "basis": "sto-3g"}, # type: ignore
keywords={"purify": "no", "new_minimizer": "yes"}, # new_minimizer yes is required
)
# Run the calculation
try:
# prog_output is a ProgramOutput instance
prog_output = compute("terachem", prog_input, collect_files=True)
except exceptions.QCComputeBaseError as e:
prog_output = e.prog_output
print(prog_output.logs)
print(f"Success: {prog_output.success}") # False
print(prog_output.input_data) # Input data used to generate the calculation
print(prog_output.provenance) # Provenance of generated calculation
print(prog_output.traceback) # or output.ptraceback for short
raise
else:
# Check results
print(prog_output.logs)
print(f"Success: {prog_output.success}") # True
print("output.data: ", prog_output.data)
print("output.data.energies:", prog_output.data.energies)
print("output.data.structures:", prog_output.data.structures)
print("output.data.final_structure:", prog_output.data.final_structure)
print(prog_output.input_data) # Input data used to generate the calculation
print(prog_output.provenance) # Provenance of generated calculation