Using terco as a library
terco is a library first; the terco command-line program (see Command-line program)
is built on top of the same Fortran API described here. See API reference for
the generated reference of every public entry point.
From 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:
#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:
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 |
|---|---|
|
the roots wanted, TDA (non-zero) or RPA, |
|
the solver and Z-vector settings; each 0 resets that setting to its default (1e-8, 50, 1e-9, 100, 2) |
|
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 ( |
|
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) |
|
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 |
|
the roots, after |
|
the roots: energies in Eh, oscillator strengths, ||Y||^2, transition dipoles, and each root’s dominant orbital pair (from the HOMO and the LUMO) |
|
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; |
|
E_SCF + omega of the target root |
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 API reference):
Call |
What it does |
|---|---|
|
|
|
the stopping rule; each 0 keeps the default (1e-8, 1e-6, 100, 8); both thresholds must hold |
|
the ladder form, the (T) routine and the memory of its working tile; none changes the energy |
|
the CCSD and, if asked, the (T) |
|
|
|
|
|
the frozen, active occupied, virtual and auxiliary counts of the last run |
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 intrc_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_BADARGwhen the frozen core leaves no occupied orbital.TRC_ERR_NOCONVwhen the iteration does not converge; the last energies are intrc_rccsd_energywithconverged0, (T) is not run andtrc_energyis 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_UNSUPPORTEDwith the (T) message;trc_rccsd_energystill gives the converged CCSD withhas_triples0 andtrc_energyis 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:
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 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.
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 |
|---|---|---|
|
1.23 |
the request: |
|
1.23 |
electrons per irrep, |
|
1.23 |
|
|
1.25 |
|
|
1.23 |
the group the last run used ( |
|
1.23 |
the largest Abelian group found in the geometry |
|
1.23 |
the largest atom displacement symmetrisation made, Bohr |
|
1.23 |
|
|
1.23 |
the frame the getters return now ( |
|
1.23 |
number of irreps of the group used |
|
1.23 |
name of irrep |
|
1.23 |
symmetry-adapted functions per irrep ( |
|
1.23 |
irrep of each orbital, 1-based, laid out like |
|
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.
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 |
|---|---|---|
|
1.37 |
the rows |
|
1.37 |
the table, |
|
1.37 |
|
|
1.37 |
|
|
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 |
|
1.37 |
whether the Kohn-Sham ground-state gradient ( |