Skip to content

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 ProgramInput, ProgramSpec, 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 = ProgramInput(
    program="geometric",
    calctype="optimization",  # type: ignore
    structure=h2,
    subprograms=[
        ProgramSpec.model_validate(
            {
                "program": "terachem",
                "calctype": "gradient",
                "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(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.results.provenance)
    print(prog_output.traceback)
    raise

else:
    # Check results
    print("Energies:", prog_output.results.energies)
    print("Structures:", prog_output.results.structures)
    print("Trajectory:", prog_output.results.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.results.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(
    program="terachem",
    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(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.results.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.results: ", prog_output.results)
    print("output.results.energies:", prog_output.results.energies)
    print("output.results.structures:", prog_output.results.structures)
    print("output.results.final_structure:", prog_output.results.final_structure)
    print(prog_output.input_data)  # Input data used to generate the calculation
    print(prog_output.results.provenance)  # Provenance of generated calculation