Example programs
Programs and the references they are checked against
33 programs, 7,254 lines, 228 definitions. Beside each result is the reference to compare it with: a published energy, an independent implementation, or a convergence rate set by theory. A program with tests checks its result itself, and a few are checked by the compiler's own tests; for the rest, marked no test, the comparison is the reader's.
Every program here is loaded into the workbench and the
terminal, so you can run, compile, hash and diff any of them without
installing anything. In the repo they live in
lang/viv/examples/.
Quantum chemistry
Hartree–Fock from the Gaussian integrals up, with the published energies to compare against.
| Program | What it computes | Result | Lines | |
|---|---|---|---|---|
geomopt.viv atlas | Geometry optimization by numerical gradients: relax a diatomic's bond length to its equilibrium by finding where the RHF/STO-3G energy is stationary. | H₂ Rₑ = 1.346 a₀ HF/STO-3G equilibrium checked by a test in lang/viv/tests | 344 | |
geomopt_water.viv atlas | Polyatomic geometry optimization: relax water (H2O) to its equilibrium by numerical-gradient Newton on the RHF/STO-3G energy surface. | r 1.870 a₀, ∠ 100.0° HF/STO-3G water checked by a test in lang/viv/tests | 376 | |
h2_sto3g.viv atlas | Restricted Hartree-Fock for H2 at fixed geometry in the STO-3G basis. | −1.11671 Eₕ Szabo & Ostlund | 382 | 1 test |
hchain_sto3g.viv atlas | Restricted Hartree-Fock for a linear chain of N hydrogen atoms in STO-3G. | H₈ −4.06498, H₂₀ −4.79091 C++ interpreter, exact no test | 274 | |
scf_mcmurchie.viv atlas | Restricted Hartree-Fock with a general McMurchie-Davidson integral engine, supporting s AND p Cartesian Gaussians (extends to higher angular momentum). | −1.11671 Eₕ general integral engine no test | 378 | |
scf_sto3g.viv atlas | Molecule-driven restricted Hartree-Fock in STO-3G. | 12 molecules + MP2 published HF/STO-3G checked by a test in lang/viv/tests | 838 | |
scf_water.viv atlas | Restricted Hartree-Fock for WATER (H2O) in STO-3G, with a general McMurchie-Davidson integral engine over s AND p Cartesian Gaussians. | −74.94208 Eₕ Crawford −74.9420787, ~1 µEₕ no test | 401 | |
scf_water_631gd.viv atlas | Restricted Hartree-Fock for WATER (H2O) in 6-31G(d) [6-31G*], with a general McMurchie-Davidson integral engine over s, p, AND d Cartesian Gaussians. | −76.0192 Eₕ 6-31G*, 19 basis functions no test | 482 |
Plasma physics
Kinetic and fluid plasma: Landau damping, drift-wave turbulence, tokamak equilibrium, transport coefficients.
| Program | What it computes | Result | Lines | |
|---|---|---|---|---|
debye_scf.viv atlas | Self-consistent field solution of the nonlinear Poisson-Boltzmann equation for Debye screening of a charged wall in a plasma: the classical mean-field cousin of the Hartree-Fock SCF (scf_sto3g.viv). | self-consistent screening Debye length recovered no test | 160 | |
grad_shafranov.viv atlas | Grad-Shafranov equilibrium of a tokamak: the self-consistent MHD force balance J x B = grad p, written for the poloidal flux psi(R,Z). | relaxed equilibrium tokamak flux surfaces no test | 182 | |
hasegawa_wakatani.viv atlas | Hasegawa-Wakatani (1983): resistive drift-wave turbulence. | enstrophy + energy balance drift-wave turbulence no test | 236 | |
oneil_trapping.viv atlas | O'Neil (1965) trapping: the nonlinear sequel to Landau damping. | O'Neil trapping regime nonlinear saturation no test | 132 | |
spitzer_harm.viv atlas | Spitzer-Harm (1953) conductivity: the electrical conductivity of a fully ionized plasma from the linearized Fokker-Planck collision operator. | within 2% of NRL Spitzer–Härm 1953 no test | 96 | |
two_stream_pic.viv atlas | The two-stream instability by a 1D electrostatic particle-in-cell (PIC) code: the kinetic model in its rawest form -- push real particles, let them make their own field, and watch a beam-plasma instability grow out of noise. | growth rate vs theory particle-in-cell no test | 178 | |
vlasov_landau.viv atlas | Landau damping by a 1D1V Vlasov-Poisson solver, written in Viv. | γ matches the dispersion root Landau damping | 73 | 2 tests |
Nuclear & astrophysics
Neutron-star structure and nuclear level densities by exact counting.
| Program | What it computes | Result | Lines | |
|---|---|---|---|---|
level_density.viv atlas | Nuclear level density by exact many-fermion state counting. | p(40) = 37338 exact count vs Hardy–Ramanujan no test | 49 | |
tov.viv atlas | Neutron-star structure from the Tolman-Oppenheimer-Volkoff equations, with the relativistic free neutron-gas equation of state. | 0.7087 M☉ maximum Oppenheimer–Volkoff 1939 no test | 109 |
Numerical methods
Finite elements, molecular dynamics, a spectral Poisson solve, and the records refactor of the FEM solver.
| Program | What it computes | Result | Lines | |
|---|---|---|---|---|
annotated.viv atlas | The worked example for the native-annotation syntax: the Boys function and the primitive Gaussian overlap from the SCF programs, annotated at all three levels, with two labeled regions and a variable glossary. | — | 59 | 1 test |
fem_poisson.viv atlas | Finite-element solution of the 2D Poisson equation -Laplacian(u) = f on the unit square with u = 0 on the boundary, using linear (P1) triangular elements. | error ratio → 4 O(h²), the P1 rate checked by a test in lang/viv/tests | 166 | |
fem_poisson_records.viv atlas | The 2D Poisson FEM (see fem_poisson.viv), refactored with records to show the named-field bundling that motivated them: the mesh is a single Mesh value passed to functions (instead of three loose global arrays), and each element's geometry is a Tri value passed to helpers (instead of nine loose scalars). | identical to the above records are zero-cost no test | 178 | |
fft_poisson.viv atlas | Poisson's equation, solved spectrally: -u'' = f on a periodic domain. | error 1.7e-16 exact solution — spectral, to rounding | 118 | 4 tests 3 fig |
fused_views.viv atlas | Elementwise expressions and reductions over slices, as the compiled engines fuse them (lang/viv/src/fuse.rs): every result is compared to the element loop that writes the same arithmetic in the same order, to the bit. | — | 130 | 1 test |
md_polymer.viv atlas | Molecular dynamics of a Lennard-Jones bead-spring polymer. | energy conserved ~0.5% R_g 11.5 → 6.0 checked by a test in lang/viv/tests | 180 | |
xyz_atoms.viv atlas | Read a molecule written as XYZ text into atoms: symbol, atomic number, position (the parser is lib/xyz.viv). | — | 16 |
Automatic differentiation
Forward mode by dual numbers, reverse mode by tape, and deriv() — AD as a compiler transform.
| Program | What it computes | Result | Lines | |
|---|---|---|---|---|
autodiff_deriv.viv atlas | Automatic forward-mode AD: deriv(fn(x) -> expr, x0) returns the EXACT derivative d/dx expr at x0 -- the compiler dualizes the expression (chain rule over + - * /, exp, sqrt, pow), so no dual arithmetic is written by hand. | f′(2) = 9.926737 AD as an IR transform no test | 25 | |
autodiff_forward.viv atlas | Forward-mode automatic differentiation via dual numbers. | f′(2) = 9.926737 dual numbers, exact no test | 74 | |
autodiff_reverse.viv atlas | Reverse-mode automatic differentiation via a tape ("micrograd in Viv"). | ∇g matches forward one backward pass no test | 134 |
Symbolic computation & discovery
A computer-algebra system on flat arrays, symbolic regression, Meijer-G metamodeling, symmetry discovery.
| Program | What it computes | Result | Lines | |
|---|---|---|---|---|
meijer_g.viv atlas | Symbolic metamodeling by continuous optimization: the Meijer G-function trick of Alaa & van der Schaar (NeurIPS 2019), in Viv. | log ✓ rational ✓ arctan ✗ identification by snapping | 248 | 4 tests |
mini_cas.viv atlas | A mini computer-algebra system in Viv: symbolic computation written with no pointers, no strings and no Int values, only arrays of numbers. | 11 asserts pass symbolic diff, simplify to fixpoint | 492 | 5 tests |
nn_symmetry.viv atlas | NN-guided symmetry discovery: the AI-Feynman outer loop, in Viv. | exact after the substitution the oracle finds x−y | 447 | 1 test |
symbolic_regression.viv atlas | Symbolic regression in Viv: recover a formula from data by brute force. | 4 / 4 recovered exactly AI-Feynman inner loop no test | 240 |
Language features
The minimal demos: arithmetic and functions; control flow and recursion.
| Program | What it computes | Result | Lines | |
|---|---|---|---|---|
area.viv atlas | A few numeric functions, in real Viv syntax. | — | 22 | |
control.viv atlas | Control flow: recursion, while, for, break/continue, if/else. | — | 35 |
The basis-set ladder for water
At one fixed water geometry, the variational principle requires each larger basis to lower the Hartree–Fock energy toward the basis-set limit, and it does:
| Basis | Functions | E(SCF) / Eₕ |
|---|---|---|
| STO-3G (s, p) | 7 | −74.9421 |
| 6-31G (split valence) | 13 | −75.9356 |
| 6-31G* (+ d on O) | 19 | −76.0192 |
| the HF basis-set limit | — | ≈ −76.067 |
The STO-3G energy the program prints, −74.94208, is within about a microhartree of the value in
T. D. Crawford's published programming projects (−74.9420787); no test checks it. On top of the
converged reference, scf_sto3g.viv computes the MP2 correlation energy through four
O(N⁵) quarter-transforms: it prints −0.0491496 Eₕ for water, against the reference −0.049149636. Helium and
neon give exactly zero, as they should, because a minimal basis leaves them no virtual orbitals.
A convergence-rate check
fem_poisson.viv solves −∇²u = f on the unit square with P1 triangles against a
manufactured solution. P1 elements are second-order accurate, so the error has to
quarter each time the mesh halves. Measured ratios as the mesh refines: 3.45, 3.74,
3.87, approaching 4. The check needs only the rate, not a table of reference values.
The program runs one mesh and prints its rms error. The test
lang/viv/tests/fem_poisson.rs recompiles it at N = 8, 16, 32… and checks the
ratios, so cargo test checks the rate, not the program's own output.
fem_poisson_records.viv is the same solver rewritten with records — a
Mesh value and a per-element Tri instead of nine loose scalar
arguments. It produces identical numbers. Passing a record does not copy its array fields, so passing a
mesh around copies no array data.