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 qcio import DualProgramInput, Structure

from qcop 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_inp = 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:
    output = compute("geometric", prog_inp, propagate_wfn=True, rm_scratch_dir=False)
except exceptions.QCOPBaseError as e:
    # Calculation failed
    output = e.program_output
    print(output.stdout)  # or output.pstdout for short
    # Input data used to generate the calculation
    print(output.input_data)
    # Provenance of generated calculation
    print(output.provenance)
    print(output.traceback)
    raise

else:
    # Check results
    print("Energies:", output.results.energies)
    print("Structures:", output.results.structures)
    print("Trajectory:", output.results.trajectory)
    # Stdout from the program
    print(output.stdout)  # or output.pstdout for short
    # Input data used to generate the calculation
    print(output.input_data)
    # Provenance of generated calculation
    print(output.provenance)

TeraChem

"""Example of how to run an optimization calculation with TeraChem using qcop."""

from qcio import ProgramInput, Structure

from qcop 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 instance of ProgramOutput
    prog_output = compute("terachem", prog_input, collect_files=True)
except exceptions.QCOPBaseError as e:
    prog_output = e.program_output
    print(prog_output.stdout)  # or output.pstdout for short
    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.stdout)  # or output.pstdout for short
    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.provenance)  # Provenance of generated calculation