# Using terco as a library terco is a library first; the `terco` command-line program (see {doc}`cli`) is built on top of the same Fortran API described here. See {doc}`api` for the generated reference of every public entry point. ## From Fortran ```fortran use trc_api, only: trc_basis_t, trc_pairlist_t, trc_1e, trc_multipoles use trc_fock, only: trc_eri_t type(trc_basis_t) :: bas type(trc_pairlist_t) :: pl type(trc_eri_t) :: eri call bas%build(nsh, sh_l, sh_np, sh_e, sh_c, sh_r, nat, at_z, at_r, maxnp) call bas%to_device() call pl%build(bas, 1.0e-10_dp) call pl%to_device() call eri%build(bas, 1.0e-10_dp) ! Schwarz bounds, bins, pair tables call trc_1e(bas, pl, smat, tmat, vmat) ! S, T, V in one pass call eri%fock(bas, dmat, gmat) ! G = J - K/2 call eri%fock_many(bas, n, dmats, gmats) ! N densities, ONE integral pass ``` The basis, the pair list and the ERI object are built **once per geometry** and reused by every Fock build; rebuilding them per SCF iteration throws away the container's entire reason for existing. `fock_many` takes `(ndens, nao, nao)` -- the density index **first**: with it last, the N atomic updates for one `(mu,nu)` sit `nao^2` apart and the batch stops paying past N = 4. ## From another compiler (the C ABI) A gfortran or ifx host cannot read nvfortran's `.mod` files, so it goes through the C ABI. `libterco.so` is the default build -- nvfortran links it, so it already carries the NVHPC and OpenACC runtimes as its own `DT_NEEDED`, and the host's link line needs nothing but `-lterco`. From C, `#include "terco.h"` (generated from `trc_capi.F90`, under `include/`) and link against `libterco.so`: ```c #include "terco.h" trc_handle_t h; double z[3] = {8.0, 1.0, 1.0}; /* O, H, H */ double xyz[9] = { ... }; /* Bohr, atom-major x,y,z */ double energy; trc_create(&h); trc_set_molecule(h, 3, z, xyz, /*charge=*/0, /*multiplicity=*/1); trc_set_basis_json(h, "6-31g.json"); trc_set_method(h, "", /*grid_level=*/3); /* empty name: Hartree-Fock */ trc_set_convergence(h, 1.0e-10, 1.0e-7, /*max_iter=*/100, /*diis=*/10); trc_run_scf(h); trc_energy(h, &energy); trc_destroy(h); ``` This is the same sequence `test/link_c.c` runs end to end (water/6-31G RHF) as part of terco's own test suite, against `terco.h` and `libterco.so` built by the project's own C compiler -- not nvfortran, not gfortran -- so a mismatch between the generated header and the real ABI shows up there before any external caller sees it. From a Fortran caller built with a *different* compiler than the one that built `libterco.so`, compile `include/trc_c_interfaces.f90` with your own compiler and `use trc_c_interfaces`; that file is the authoritative declaration of the ABI and should not be re-typed by hand: ```fortran use trc_c_interfaces rc = trc_basis_create_libcint(atm, natm, bas, nbas, env, nenv, 1, hbas) rc = trc_eri_create(hbas, 1.0e-10_dp, heri) rc = trc_fock(heri, hbas, d, g, 1.0d0, 1.0d0, 1, 0.0d0) ! last: omega, 0 = Coulomb ``` ## TDDFT excitation energies and excited-state gradients through the C ABI Closed-shell singlet TDA/CIS and RPA (TDHF, Casida) excitation energies, and the gradient of one root, are in the C ABI since version 1.30, on the converged restricted reference of the handle (Cartesian or spherical basis, the latter since ABI 1.35, over a `--df` fit since ABI 1.36, energies only; LDA, GGA and global-hybrid functionals). The Fortran entry is `trc_tddft_run_solve` and `trc_tddft_run_gradient` (module `trc_tddft_run`); over a `--df` reference it is `trc_tddft_run_solve_fitted`, which takes the `df_jk_t` the SCF converged with. | Call | What it does | |---|---| | `trc_set_tddft(h, nroots, tda, singlet, target_root, grid_response)` | the roots wanted, TDA (non-zero) or RPA, `singlet` (non-zero; a triplet is `TRC_ERR_UNSUPPORTED`), the root, from 1, that the gradient and `trc_tddft_energy` follow, and the grid response of a Kohn-Sham gradient (non-zero: the exact derivative; zero: Pulay only, as pyscf) | | `trc_set_tddft_convergence(h, conv_tol, max_iteration, cpks_tol, cpks_max_iteration, roots_per_sweep)` | the solver and Z-vector settings; each 0 resets that setting to its default (1e-8, 50, 1e-9, 100, 2) | | `trc_set_tddft_missed_root_iterations(h, n)` | the iterations after the probe in which candidate Ritz pairs beyond the buffer are still iterated (0, the default since ABI 1.33, 24 before: none is iterated, so fewer matvecs, and the candidates are reported open (`trc_tddft_window_open`); 24 recovers a root the guesses miss, and is what to set before differentiating a followed root with `trc_run_gradient` or `trc_opt_set_tddft` (the CLI does for `--grad`/`--opt`); negative is `TRC_ERR_BADARG`; ABI 1.31) | | `trc_set_tddft_density_screen(h, on)` | whether the excited-state matvec screens its integrals by the trial densities (non-zero, the default) or not (zero: the same roots to the integral threshold, a slower run, logged as a warning; the gradient's Z-vector never screens this way; ABI 1.32) | | `trc_check_tddft(h, gradient)` | whether the run would be refused for the settings alone (a triplet, an open shell, meta-GGA, range-separated, VV10 or double-hybrid functional, an f basis, PCM, or with `gradient` a `--df` reference), before any SCF | | `trc_run_tddft(h)` | the roots, after `trc_run_scf` | | `trc_tddft_nroots(h, &n)`, `trc_tddft_energies(h, n, omega, f, ynorm, dipole)`, `trc_tddft_character(h, n, hole, particle, weight)` (`n` the roots the arrays hold; fewer than solved is `TRC_ERR_BADARG`), `trc_tddft_target_root(h, &root, &nroots)` | the roots: energies in Eh, oscillator strengths, \|\|Y\|\|^2, transition dipoles, and each root's dominant orbital pair (from the HOMO and the LUMO) | | `trc_tddft_window_open(h, &n)` | the candidate Ritz pairs the missed-root check of the last run left open (0: none; they do not by themselves mean a root is missing; `TRC_ERR_STATE` without a stored result; ABI 1.33) | | `trc_tddft_energy(h, &e)` | E_SCF + omega of the target root | ```c double omega[4], f[4], ynorm[4], dipole[12], e_excited, grad[9]; int n; trc_set_method(h, "pbe", 3); trc_run_scf(h); trc_set_tddft(h, 4, /*tda=*/1, /*singlet=*/1, /*target_root=*/2, /*grid_response=*/1); trc_run_tddft(h); trc_tddft_nroots(h, &n); trc_tddft_energies(h, n, omega, f, ynorm, dipole); trc_tddft_energy(h, &e_excited); trc_run_gradient(h); /* now the gradient of e_excited */ trc_gradient(h, grad); ``` After `trc_run_tddft`, `trc_run_gradient` differentiates the target root's energy and `trc_run_hessian` refuses; `trc_energy` stays the ground state's. A later `trc_run_scf` drops the roots and the gradient is the SCF's again. A later `trc_run_rimp2` or `trc_run_rccsd` also drops the roots. `libterco_opt` follows the context's target root through `trc_opt_set_tddft(opts, enable, gap_warn)` (`gap_warn` finite, zero or more; zero switches the gap warning off). `trc_opt_set_tddft_strict(opts, strict)` turns a change of the followed root's dominant orbital pair into a failed evaluation, which is DL-FIND's `error stop` path (the run writes what it can first); without it `trc_opt_result_state_changes` and `trc_opt_result_gap_warnings` count the warnings after the run. A P-RFO or Newton-Raphson search with terco's own Hessian source is refused with `TRC_ERR_UNSUPPORTED`. ## RI-CCSD and RI-CCSD(T) through the C ABI Closed-shell RI-CCSD, and (T) on request, are in the C ABI since version 1.28 (`TRC_ABI_MINOR`); the Fortran facade `trc_api` does not carry them. They run on the converged restricted Hartree-Fock reference in the handle, with the auxiliary basis of `trc_set_aux_json` (the one RI-MP2 reads). Six entry points, documented in `include/terco.h` (and in {doc}`api`): | Call | What it does | |---|---| | `trc_set_rccsd(h, triples, nfrozen, aux_block)` | `triples` 1 asks for (T); `nfrozen` frozen core orbitals, -1 counts the core by element; `aux_block` auxiliary functions per three-index block (0: all at once; the energy does not depend on it) | | `trc_set_rccsd_convergence(h, conv_energy, conv_amplitude, max_iterations, diis_size)` | the stopping rule; each 0 keeps the default (1e-8, 1e-6, 100, 8); both thresholds must hold | | `trc_set_rccsd_algorithm(h, ladder, triples_routine, triples_budget_mib)` | the ladder form, the (T) routine and the memory of its working tile; none changes the energy | | `trc_run_rccsd(h)` | the CCSD and, if asked, the (T) | | `trc_rccsd_energy(h, ...)` | `e_mp2`, `e_ccsd`, `e_t`, the T1 diagnostic, the iteration count, `converged` and `has_triples`, all energies being correlation energies | | `trc_rccsd_energy_parts(h, e_singles, e_doubles)` | `e_ccsd` as its two parts: what `t2` alone gives, and the `t1 t1` part (ABI 1.29) | | `trc_rccsd_dimensions(h, ...)` | the frozen, active occupied, virtual and auxiliary counts of the last run | ```c double e_mp2, e_ccsd, e_t, t1_diag, e_total; int iterations, converged, has_triples; trc_set_aux_json(h, "cc-pvdz-rifit.json"); trc_set_convergence(h, 1.0e-10, 1.0e-8, 100, 10); /* the SCF */ trc_run_scf(h); trc_set_rccsd(h, /*triples=*/1, /*nfrozen=*/-1, /*aux_block=*/0); trc_set_rccsd_convergence(h, 1.0e-10, 1.0e-8, 0, 0); /* 0 keeps iterations, DIIS */ int rc = trc_run_rccsd(h); /* TRC_OK, or see below */ trc_rccsd_energy(h, &e_mp2, &e_ccsd, &e_t, &t1_diag, &iterations, &converged, &has_triples); trc_energy(h, &e_total); /* after a converged run: the CCSD(T) total */ ``` After a converged run `trc_energy` returns the CCSD (or, with (T), the CCSD(T)) total energy, not the SCF energy; RI-MP2 and RI-CCSD share it, and the later run wins (a successful `trc_run_rccsd` drops a stored RI-MP2 result and the reverse). A refused run leaves the other method's result alone and drops its own method's earlier one. On a handle with `trc_set_df`, `trc_run_rccsd` releases the SCF's density fit before the solve, converged or not: the solver fits its own integrals, and the amplitudes need the device memory (16 GB at (H2O)6 aug-cc-pVQZ). Until `trc_run_scf` runs again, which fits anew, `trc_run_tddft` and a `trc_run_gradient` that needs the fit return `TRC_ERR_STATE`, and `trc_run_rimp2` fits its own integrals where it would have reused the SCF's, for the same energy. What a call returns: - `TRC_ERR_STATE`: no SCF has run, it did not converge, or no auxiliary basis was set. - `TRC_ERR_UNSUPPORTED`, with the reason in `trc_message`: an unrestricted or open-shell reference, a Kohn-Sham one, an effective core potential, dropped linearly dependent functions, a non-aufbau reference (mom or gdm), a named point group, a basis above this build's density-fitting ceilings, or, over several ranks, an explicit reference or tiled (T) routine. `TRC_ERR_BADARG` when the frozen core leaves no occupied orbital. - `TRC_ERR_NOCONV` when the iteration does not converge; the last energies are in `trc_rccsd_energy` with `converged` 0, (T) is not run and `trc_energy` is the SCF energy. - When the CCSD converged and (T) then failed (for example for device memory, or a budget below one triple) the return is `TRC_ERR_UNSUPPORTED` with the (T) message; `trc_rccsd_energy` still gives the converged CCSD with `has_triples` 0 and `trc_energy` is the CCSD total. ### The RI-CCSD gradient Since ABI 1.37, `trc_run_gradient` after a converged `trc_run_rccsd` without (T) is the analytic gradient of E_RHF + E_RI-CCSD, frozen core included: ```c trc_set_rccsd(h, /*triples=*/0, -1, 0); trc_set_rccsd_convergence(h, 1.0e-10, 1.0e-9, 0, 0); rc = trc_check_rccsd_gradient(h); /* before the SCF: will it be refused? */ trc_run_scf(h); trc_run_rccsd(h); trc_run_gradient(h); trc_gradient(h, grad); ``` It takes an exact-ERI RHF reference on one rank. `TRC_ERR_UNSUPPORTED`, with the reason in `trc_message`: RI-CCSD(T), an open shell, a Kohn-Sham reference, an effective core potential, PCM, a `trc_set_df` reference, an SCF taken from a file (`trc_load_scf`), a basis above the derivative kernels' ceilings, ghost atoms, a frozen orbital degenerate with a correlated one, more than one rank, or device memory that does not fit. `TRC_ERR_NOCONV` when the Lambda or Z-vector equations do not converge. `trc_check_rccsd_gradient` answers what the settings alone decide. The convergence settings of `trc_set_rccsd_convergence` (amplitude threshold, iteration limit, DIIS size) also drive the Lambda equations, so the gradient is as converged as they are. The integrals are rebuilt for the gradient and the solver restarts from the converged amplitudes, which the handle keeps in host memory (o v + o^2 v^2 doubles) when the gradient can follow; no device memory is held between the two calls. `aux_block` of `trc_set_rccsd` does not bound the gradient's own three-index blocks. `libterco_opt` has no RI-CCSD optimisation. After a converged run `trc_run_hessian` and `trc_run_polarizability` refuse. Over MPI ranks (`trc_set_comm`, one rank per device, every rank calling `trc_run_rccsd` together) the ladder of every iteration and the (T) are shared and every rank returns the same status and energies; see {doc}`theory`. ## Point-group symmetry (C ABI) Symmetry is off until a caller asks (ABI 1.23). Set the request before `trc_run_scf`; read the results after it. ```c trc_set_symmetry(h, "on", 0.0); /* tol 0: the default, 1e-5 Bohr */ trc_run_scf(h); char group[16]; trc_symmetry_group(h, group, sizeof group); /* "C2v" */ int nirrep; trc_symmetry_nirrep(h, &nirrep); int *mo_irrep = malloc(nao * nspin * sizeof(int)); trc_mo_irreps(h, mo_irrep); /* 1-based irrep per orbital */ ``` | Function | Version | Meaning | |---|---|---| | `trc_set_symmetry(h, group, tol)` | 1.23 | the request: `"off"` (the default), `"on"`/`"auto"` (the largest Abelian group; unsupported paths run in C1), or a group name (strict). `tol` in Bohr, 0 for the default | | `trc_set_irrep_nelec(h, spec)` | 1.23 | electrons per irrep, `"A'=6,A\"=2"` (restricted) or `"A'=4/3,A\"=1/1"` (alpha/beta); `""` restores aufbau. Makes the request strict | | `trc_set_symmetry_report_frame(h, frame)` | 1.23 | `1` input frame, `0` standard frame, `-1` the default (below) | | `trc_set_symmetry_skeleton(h, on)` | 1.25 | `1` force the skeleton Fock and gradient builds, `0` force the full builds, `-1` let the size rule choose (the default). Any other value is `TRC_ERR_BADARG`. The environment variable `TRC_SYM_SKELETON=0/1` overrides all three | | `trc_symmetry_group(h, buf, n)` | 1.23 | the group the last run used (`"C1"` when none) | | `trc_symmetry_detected(h, buf, n)` | 1.23 | the largest Abelian group found in the geometry | | `trc_symmetry_residual(h, &res)` | 1.23 | the largest atom displacement symmetrisation made, Bohr | | `trc_symmetry_frame(h, rot, shift)` | 1.23 | `x_std = rot (x - shift)`: `rot` is 9 doubles row-major, `shift` the centre of nuclear charge (identity and zero for C1) | | `trc_symmetry_report_frame(h, &frame)` | 1.23 | the frame the getters return now (`1` input, `0` standard) | | `trc_symmetry_nirrep(h, &n)` | 1.23 | number of irreps of the group used | | `trc_symmetry_irrep_name(h, i, buf, n)` | 1.23 | name of irrep `i` (1-based) | | `trc_symmetry_irrep_dim(h, dim)` | 1.23 | symmetry-adapted functions per irrep (`nirrep` ints, summing to `trc_nao`) | | `trc_mo_irreps(h, irreps)` | 1.23 | irrep of each orbital, 1-based, laid out like `trc_mo_energies` (`nao` by `nspin`, column-major); 0 for an orbital the linear-dependence cut dropped | | `trc_geometry(h, xyz)` | 1.23 | the geometry the context holds now (Bohr, atom-major): after a symmetric run, the symmetrised one in the reported frame | The getters return `TRC_ERR_STATE` unless the last run was symmetric (the group-name getters unless a symmetry request ran). A request longer than 32 characters, or an irrep specification longer than 256, is refused. **Frame.** With `"on"`/`"auto"` the geometry, gradient, orbitals, density, multipoles and the borrowed basis and ERI handles are reported in your **input frame** (the input symmetrised in it), rotated back exactly from the frame the SCF ran in. With a named group they are in the **standard frame** of the group (centre of nuclear charge at the origin, axes along the group's): `trc_symmetry_frame` maps your input to it. `trc_set_symmetry_report_frame` overrides either default. Energies, orbital energies, irrep labels, occupations and atomic charges do not depend on the frame. **Unsupported paths.** An `"on"`/`"auto"` request runs these in C1 and logs one line naming the reason; a named group (or any request with `trc_set_irrep_nelec`) refuses them at the run with `TRC_ERR_UNSUPPORTED`: density fitting, PCM, fractional occupations, SOSCF, MOM, GDM, a given or projected guess, more than one rank, RI-MP2, RI-CCSD, CPHF/CPKS, the Hessian, Fukui indices, restarts and the geometry optimiser. ## The SCF's record and diagnostics (C ABI) After `trc_run_scf` the context keeps what the run already tracked: the iteration table it printed, the events it decided and a few counters (ABI 1.37). Nothing is recomputed. Each returns `TRC_ERR_STATE` on a handle with no SCF. ```c int nrows; trc_scf_history_size(h, &nrows); double *e = malloc(nrows * sizeof(double)), *de = ..., *rms = ..., *comm = ...; int *mode = malloc(nrows * sizeof(int)), *flags = ...; trc_scf_history(h, nrows, e, de, rms, comm, mode, flags); int events[TRC_SCF_NEVENTS], diag[TRC_NDIAGNOSTICS]; trc_scf_events(h, events); trc_diagnostics(h, diag); ``` | Function | Version | Meaning | |---|---|---| | `trc_scf_history_size(h, &nrows)` | 1.37 | the rows `trc_scf_history` copies out: one per iteration the SCF printed, the second-order phase's included; 0 for a result loaded with `trc_load_scf` | | `trc_scf_history(h, nrows, energy, delta_e, rms_density, commutator, mode, flags)` | 1.37 | the table, `nrows` entries of each array (fewer than `trc_scf_history_size` is `TRC_ERR_BADARG`): the total energy; its change from the previous iteration (the first row's is the first energy itself); the density change sqrt(sum ΔD²)/nao, summed over both spins for UHF (-1 on a second-order step); the norm sqrt(sum e²) of the orthogonalised commutator e = Xᵀ(FDS - SDF)X, summed over both spins for UHF (the DIIS error `trc_set_convergence`'s `conv_diis` gates, not the AO-basis norm); the accelerator (0 plain or damped, 1 CDIIS, 2 ADIIS, 3 the diagonalisation GDM hands over from, 4 a GDM step, 5 a second-order step); and the bits 1 (incremental Fock build), 2 (level shift applied), 4 (MOM followed the density) | | `trc_scf_events(h, events)` | 1.37 | `TRC_SCF_NEVENTS` (10) integers: incremental Fock builds, GDM steps, rejected GDM trials, GDM's off-aufbau virtuals at the end, GDM declined at the handover for MOM, the level-shift stall guard dropped the shift, the iteration the second-order phase took over after (0 never), the most orbitals MOM held above aufbau, functions dropped as linearly dependent, the symmetric run declined to C1. After `trc_load_scf` all are 0 but the linear-dependence count | | `trc_diagnostics(h, values)` | 1.37 | `TRC_NDIAGNOSTICS` (8) integers, 0 where the run had no such thing: Kohn-Sham grid points, VV10 grid points, the skeleton symmetric build was used, atoms inequivalent under the group, RI-MP2/RI-CCSD auxiliary functions, density-fitting J/K auxiliary functions, Fock builds of the main loop, an effective core potential is attached | | `trc_nelec_grid(h, &nelec)` | 1.37 | the electron count the Kohn-Sham grid integrated the converged density to (within the quadrature error of the molecule's count; 0 for Hartree-Fock); a loaded result reports the count stored in the file | | `trc_set_gradient_grid_response(h, flag)` | 1.37 | whether the Kohn-Sham ground-state gradient (`trc_run_gradient`, and the Hessian from it) differentiates the grid: non-zero, the default, is the exact derivative of terco's energy; zero gives the Pulay terms only, pyscf's default (about 1e-5 Eh/Bohr away on water PBE). No effect for Hartree-Fock; the RI-MP2 and TDDFT gradients keep their own settings. Drops the stored gradient |