Metadata-Version: 2.4
Name: dyson_orca_tools
Version: 0.1.1
Summary: Dyson orca tools package
Author-email: Andres Ortega Guerrero <andres.ortega-guerrero@empa.ch>, Gonçalo Catarina <goncalo.catarina@empa.ch>
Requires-Python: >=3.10
Description-Content-Type: text/markdown
Classifier: Development Status :: 3 - Alpha
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: POSIX :: Linux
Classifier: Operating System :: MacOS :: MacOS X
Classifier: Programming Language :: Python :: 3
License-File: LICENSE
Requires-Dist: typer
Requires-Dist: numpy
Requires-Dist: pyscf ; extra == "cube"
Requires-Dist: bumpver==2023.1129 ; extra == "dev"
Requires-Dist: pre-commit==3.6.0 ; extra == "dev"
Requires-Dist: pytest ; extra == "dev"
Requires-Dist: matplotlib ; extra == "plot"
Project-URL: Homepage, https://github.com/AndresOrtegaGuerrero/dyson-orca-tools
Project-URL: Issues, https://github.com/AndresOrtegaGuerrero/dyson-orca-tools/issues
Provides-Extra: cube
Provides-Extra: dev
Provides-Extra: plot

# dyson-orca-tools
Python-based tool for computing Dyson orbitals from CASCI and CASSCF wavefunctions generated by the ORCA quantum chemistry package. It parses ORCA’s JSON-formatted wavefunction outputs to extract and process the relevant one-electron transition amplitudes.


## Installation

```shell
pip install dyson-orca-tools            # core: Dyson orbitals, spectral functions, e-h maps
pip install "dyson-orca-tools[plot]"    # + matplotlib figures
pip install "dyson-orca-tools[cube]"    # + PySCF cube files
```

Development setup and releasing: see [CONTRIBUTING.md](CONTRIBUTING.md).

## Usage

### What you need from ORCA

One CASCI/CASSCF calculation for the reference (N electrons) and one for each charged species
(N−1 and/or N+1), each with `PrintWF det` and a small `TPrintWF` in `%casscf`, plus the
`orca_2json` file of every calculation (see [ORCA instructions](#-orca-instructions)):

```
%casscf nel 6  norb 6
        mult   1,3          # several multiplicities in one run are fine (one block each)
        nroots 2,2
        printwf det
        ci TPrintWF 1e-6 end
end
```

A calculation with several multiplicities gives one block per multiplicity in the output;
ORCA prints them from the highest multiplicity down, whatever the order in the input, and
numbers the roots from 0 inside each block. `prepare` reads every block, so one file is enough
per species.

### Workflow

```shell
# 1. parameters file: CI vectors, root energies and active space of every state
dyson_orca_tools prepare -i ref/out.out \
    -f cation/out.out:cation/mol.json \
    -f anion/out.out:anion/mol.json \
    -o results/params.json

# 2. Dyson orbitals + spectral function, written under results/
dyson_orca_tools spectrum -i ref/mol.json -p results/params.json -o results \
    --eta 0.05 --plot --cube

# 3. re-plot later without recomputing
dyson_orca_tools plot -o results --title "CH2 CASCI(6,6)" --vertical
```

`spectrum` implements the spectral function of Kumar et al., *JACS* 2025, 147, 24993 (eq. 10):

    ρ(ω) = η Σ_j |⟨Ψ±,j| a / a† |Ψ0⟩|² / ((ω − E_j)² + η²)

one peak per charged root j, with its Dyson orbital ϱ±,j and strength ⟨ϱ|ϱ⟩.

### `prepare` — choosing the states

| option | meaning |
|---|---|
| `-i FILE` | ORCA output of the reference calculation |
| `-f OUT[:JSON]` | ORCA output of one charged calculation (repeatable); its `orca_2json` file is `<name>.json` next to it unless given after the colon |
| `--initial-state MULT:ROOT` | reference state (repeatable). Default: the lowest-energy root over all blocks |
| `--all-initial-roots` | every root of every block of the reference as a reference state |
| `-o FILE` | parameters file to write (JSON paths are stored relative to it) |

Every `MULT=` block of a charged output becomes one run with all its roots. Some combinations:

```shell
# ground state only, charged species with one multiplicity each
dyson_orca_tools prepare -i ref/out.out -f cation/out.out:cation/mol.json -f anion/out.out:anion/mol.json -o results/params.json

# singlet/triplet reference (mult 1,3): S0 and T0 as references, ions with doublets and quartets (mult 2,4)
dyson_orca_tools prepare -i ref/out.out -f cation/out.out:cation/mol.json -f anion/out.out:anion/mol.json \
    --initial-state 1:0 --initial-state 3:0 -o results/params.json

# an excited reference as well: S0, S1 and T0
dyson_orca_tools prepare -i ref/out.out -f ... --initial-state 1:0 --initial-state 1:1 --initial-state 3:0 -o results/params.json

# only the cation side
dyson_orca_tools prepare -i ref/out.out -f cation/out.out:cation/mol.json -o results/params.json
```

`prepare` prints the chosen references and, per run, Σc² of every root so you can see how much
the determinant printout truncated. A reference may be of any charge and multiplicity; the
charged runs must differ from it by one electron and change the multiplicity parity.

### `spectrum` — options

| option | default | meaning |
|---|---|---|
| `-i FILE` | | `orca_2json` file of the reference calculation (its orbitals carry the Dyson orbitals) |
| `-p FILE` | | parameters file from `prepare` |
| `-o DIR` | `.` | output folder (layout below) |
| `--eta` | `0.05` | Lorentzian broadening (eV) |
| `--shift` | `0` | rigid shift of all peak energies (eV) |
| `--shift-removal`, `--shift-addition` | `0` | extra shift (eV) of one branch only, e.g. to anchor the first removal/addition peak to the measured PIR/NIR |
| `--align-fermi` | off | put ω = 0 at the midgap of the ground-state reference, so ω > 0 is electron addition (see below) |
| `--common-reference` | off | measure every spectrum from the lowest reference energy instead of from its own reference state |
| `--min-strength` | `1e-8` | drop peaks below this strength (spin-forbidden ones) before labelling |
| `--omega-min/--omega-max/--npts` | peaks ± 1 eV, 2000 | grid of ρ(ω) |
| `--cube` | off | one cube per peak (needs `[cube]`); grid options below |
| `--plot`, `--vertical`, `--legend-outside`, `--label-threshold S`, `--label-energy` | off | figure (needs `[plot]`): energy on the y axis, legend outside, label only peaks with strength ≥ S, append ω (relative to the chosen zero) to the labels |

### `spectrum` — output layout

One spectrum is written **per reference state and per ion multiplicity**. Peaks of different ion
multiplicities are never summed: their relative weights would need the spin-coupling factors,
which are not applied, whereas each spectrum on its own is exact up to one overall scale.

```
results/                       one reference, one ion multiplicity: files directly here
results/m1r0/                  reference "m1r0" (mult 1, root 0), only doublet ions reachable -> files directly here
results/m3r0/m2/               reference "m3r0" (mult 3, root 0), doublet ions
results/m3r0/m4/               reference "m3r0", quartet ions
results/dyson_peaks.csv        one table over everything (`initial` and `mult` columns), only when there are several folders
```

The folder of a reference is its label, `m<mult>r<root>`, or whatever `"label"` you set in
the parameters file. From a singlet reference every quartet root has zero strength
(spin-forbidden) and is dropped by `--min-strength`, so only `m2` remains and the files are
written directly in the reference folder; from a triplet reference both appear.

Each leaf folder contains:

| file | content |
|---|---|
| `dyson_peaks.csv` | label, side (−/+), multiplicity, ω (eV), strength, branch (CASCI/CASSCF) |
| `dyson_composition.csv` | per peak: strength, Σc² of the root, leading determinant, and d_p² for every active MO (HOMO−k / LUMO+k of the reference); the d_p² sum to the strength |
| `spectral_function.dat` | ω, ρ(ω) on the grid |
| `dyson_orbitals_ao.txt` | one column of AO coefficients per peak (ORCA AO order) |
| `dyson_<side><j>_m<mult>.cube` | with `--cube` |
| `spectral_function.png/.pdf` | with `--plot` |

Peaks are labelled ϱ−,j / ϱ+,j by increasing energy of the N±1 state within each side (j = 0 is
the ground state of the ion), numbered separately in every folder.

Peak energies are the poles of the one-particle Green's function: removal at ω = E_N − E_{N−1}
(= −IP) and addition at ω = E_{N+1} − E_N (= −EA), both measured from the reference state the
electron is removed from or added to, i.e. the vacuum level (as in Kumar et al., JACS 2025, 147,
24993, Fig. 2b/6b). Both branches are therefore usually negative. To get an STS-like axis with
ω > 0 for electron addition, `--align-fermi` subtracts the midgap of the ground-state reference,
μ = (max removal ω + min addition ω)/2, from every peak; `--shift-removal`/`--shift-addition`
move one branch only, which is the usual way to anchor the computed first PIR/NIR to the measured
ones since CASCI gaps between charge sectors are not reliable. The shifts add up
(ω → ω − μ + shift + shift_side) and are recorded in the header of `spectral_function.dat`.
With `--common-reference` all spectra share the lowest reference energy as zero, and the console
prints E_ref − E_0 for each reference either way.

### `plot`

Re-draws `spectral_function.png/.pdf` from a leaf folder: `dyson_orca_tools plot -o results/m3r0/m4
[--title ...] [--vertical] [--legend-outside] [--label-threshold S] [--label-energy]`.

### `dyson` — a single pair

`dyson_orca_tools dyson -i initial.json -f final.json -p params.json [-o DIR] [--cube]` computes one
Dyson orbital between one reference and one charged state (the old single-state parameters format
is accepted).

### Cube grid

`dyson`, `spectrum --cube` and `ehmap --cubes` share three grid options (lengths in Bohr):

| option | default | meaning |
|---|---|---|
| `--cube-points` | `80` | points per axis: `N` or `NX,NY,NZ` |
| `--cube-spacing` | – | approximate grid spacing (PySCF rounds it to fit the box); overrides `--cube-points` |
| `--cube-margin` | `14.0` | padding around the molecule |

e.g. `--cube-points 60,60,120 --cube-margin 6` or `--cube-spacing 0.2`.

### Orbitals: CASCI vs CASSCF

The tool detects from the MO overlap whether reference and charged states share their orbitals.
With one orbital set (CASCI, `!MORead NoIter` with `ActOrbs/IntOrbs/ExtOrbs unchanged`) the
Dyson orbital is a pure active-space object. With separately optimized CASSCF orbitals the
non-orthogonal branch is used, including the relaxation of the inactive orbitals via the
core-block determinant (Schur complement); `spectrum` prints that determinant per run.

### Parameters file

`prepare` writes it; you only need its structure to edit labels or hand-pick roots. `initial` is
a list of reference states (a single dict is accepted too), `final` a list of runs, one per
ORCA JSON and multiplicity, each with its roots. Energies in Hartree. The multiplicity of every
state is taken from here, not from the JSON (whose `Multiplicity` is only the input value), and
every determinant must have 2M_S = mult − 1, as ORCA prints the M_S = S component.

```json
{
  "parameters": {
    "initial": [
      {"label": "S0", "root": 0, "nelc": 4, "norb": 4, "mult": 1, "energy": -230.5123,
       "spin_ci": {"[2200]": 0.957520133, "[2020]": -0.224387606, "[0202]": -0.063982267}},
      {"label": "T0", "root": 0, "nelc": 4, "norb": 4, "mult": 3, "energy": -230.4800,
       "spin_ci": {"[2uu0]": 0.98, "[u2u0]": -0.12}}
    ],
    "final": [
      {"file": "../anion/mol.json", "nelc": 5, "norb": 4, "mult": 2,
       "roots": [
         {"energy": -230.4901, "spin_ci": {"[22u0]": 0.993890846, "[20u2]": -0.052026571}},
         {"energy": -230.4012, "spin_ci": {"[2u20]": 0.98}}
       ]},
      {"file": "../cation/mol.json", "nelc": 3, "norb": 4, "mult": 2,
       "roots": [{"energy": -230.2410, "spin_ci": {"[2u00]": 0.97}}]}
    ]
  }
}
```

## 🧪 ORCA Instructions
To extract the required data from your CASSCF or CASCI calculations in ORCA, you must use the utility program `orca_2json`.

This tool converts ORCA wavefunction files into structured `.json` format for downstream processing.

### 🔧 Configuration File
You can create a basename-dependent configuration file, named:

```shell
BaseName.json.conf
```
#### 📌 Notes
Replace BaseName with the actual name of your ORCA Basename described in your input (e.g., mol.gbw → mol.json.conf)

This file tells `orca_2json` which parts of the wavefunction to extract. You must include the molecular orbital coefficients and the overlap matrix.

Here is a recommended configuration:

```json
{
  "MOCoefficients": true,
  "Basisset": true,
  "MullikenCharge": false,
  "LoewdinCharge": false,
  "1elIntegrals": ["S"],
  "JSONFormats": ["json"]
}
```

After that you can obtain json file from the calculations

```bash
orca_2json mol.gbw
```


## Contact

If you have any questions or suggestions, feel free to reach out:

- **Authors**: Andres Ortega-Guerrero, Gonçalo Catarina
- **Email**: [andres.ortega-guerrero@empa.ch](andres.ortega-guerrero@empa.ch) , [goncalo.catarina@empa.ch](goncalo.catarina@empa.ch)

