# Quickstart


# Installation

autostorage requires Python 3.13 or later. With [Pixi](https://pixi.sh), add it from the [`avcopan/automech`](https://prefix.dev/channels/avcopan/automech) channel on prefix.dev, which also has autogeom:

``` bash
pixi workspace channel add https://repo.prefix.dev/avcopan/automech
pixi add autostorage
```

Or install it from PyPI:

``` bash
pip install autostorage  # or: uv add autostorage
```


# Usage

``` python
import numpy as np
from sqlmodel import select

from autostorage import (
    CalculationGeometryLink,
    CalculationRow,
    Database,
    GeometryRow,
    ModelRow,
    PropertyValueRow,
    Role,
    StationaryPointRow,
    energy_property_kind,
    query,
    rdkit_inchi,
)

# Open (or create) a SQLite database; ":memory:" also works for scratch use.
with Database("workflow.db") as db, db.session() as session:
    # Create a model specifying the program, method, and basis.
    model = ModelRow(program="orca", method="b3lyp", basis="def2-svp")

    # Create a calculation using this model.
    calc = CalculationRow(model=model, calc_type="energy")

    # Create a geometry (validated by autogeom: known elements, consistent spin).
    geo = GeometryRow(
        symbols=["H", "O", "H"],
        coordinates=np.array([[0, 0, 0.96], [0, 0, 0], [0.93, 0, -0.24]]),
        charge=0,
        spin=0,
    )

    # Link the geometry to the calculation as an input.
    link = CalculationGeometryLink(calculation=calc, geometry=geo, role=Role.INPUT)

    # Add all objects to the session and commit.
    session.add_all([model, calc, geo, link])
    session.commit()

    # Attach an energy to the geometry/calculation pair. Values are validated
    # against their property kind ("energy", "gradient", "hessian") on flush.
    energy = PropertyValueRow(
        geometry=geo,
        calculation=calc,
        property_kind_name=energy_property_kind.name,
        value=-76.02,
    )
    session.add(energy)
    session.commit()

    # Query the energy back by geometry and property kind.
    found = session.exec(
        select(PropertyValueRow).where(
            PropertyValueRow.geometry_id == geo.id,
            PropertyValueRow.property_kind_name == "energy",
        )
    ).one()
    print(float(found.value))

    # Stationary points are automatically given identities (InChI, SMILES,
    # Hill formula), which can be used to look them up.
    session.add(StationaryPointRow(geometry=geo, calculation=calc))
    session.commit()
    stmt = query.stationary_point_by_identity(rdkit_inchi, "InChI=1S/H2O/h1H2")
    print(session.exec(stmt).all())
```

The [Database.session()](../reference/Database.md#autostorage.Database.session) method returns an [AutostorageSession](../reference/AutostorageSession.md#autostorage.AutostorageSession) (a `sqlmodel.Session`) that supports the context manager protocol. Validation and automatic identities are only applied by these sessions. Commit explicitly to persist changes; closing a session rolls back anything uncommitted.

See the {doc}`API reference <apidocs/index>` for full details on every model and method.
