---
title: "Scientific validation and computational limits"
description: "The snapshot, structural and numerical checks behind every design, and the limits on design size."
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Scientific validation and computational limits}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")
library(FielDHub)
```

## What the checks establish

FielDHub uses three complementary checks: complete fixed-seed snapshots,
independent structural invariants, and independently calculated numerical
references. Snapshots detect changes; they do not establish scientific validity.
The invariant tests use explicit expected counts from the input fixtures, not
counts inferred from the generated design's summaries. Mutation tests check
that the independent helpers reject missing, repeated, or relabeled units.

The fixed-seed catalogue covers every design engine, including generated and
supplied labels, multiple locations, unequal replication, checks, and fillers.
`tests/testthat/test_scientific_invariants.R` checks the following properties on
every test platform.

| Design family | Independently checked property |
|---|---|
| Completely randomized | Requested treatment multiplicities, including unequal replication |
| Complete blocks | Every test entry once per replicate and location; requested extra copies of checks |
| Latin squares | Every treatment once in each row and column of every square |
| Cyclic Latin rectangles | Every treatment once per row, no within-column repeats, equal replication and full additive row/column/treatment model rank |
| Full factorial | Every requested factor combination in every replicate and location |
| Split and split-split plots | Complete factor combinations and a single whole-plot treatment within each physical whole plot |
| Strip plots | Complete horizontal-by-vertical combinations per replicate and location |
| Incomplete blocks and lattices | Complete treatment replicates, fixed block sizes, distinct nested units, and block efficiency |
| Row-column | Complete treatment replicates and complete replicate grids; separate SVD-based joint-efficiency tests |
| Diagonal and optimized arrangements | Experimental-entry multiplicities, total check allocation, and filler counts |
| Single diagonal check optimization | Fixed check slots, individual check replication, untouched entries and fillers, independent coordinate distances, first exchange checked against exhaustive enumeration, and seeded replay |
| Augmented complete blocks | Unreplicated test entries and every check in every block |
| Partially replicated | Requested one- and two-copy entry groups, excluding fillers |
| Sparse and multi-location allocations | Across-location copy budgets in balanced fixtures, site-level restrictions, and agreement between count matrices and entry lists |
| Family splitting | Every original entry retained; family counts across locations differ by at most one |
| Pair swapping | Entry frequencies and geometry retained; reported distances agree with `stats::dist()` |

All field-design catalogue cases also have distinct final
`LOCATION`/`ROW`/`COLUMN` coordinates. `PLOT` is not a universal unique key:
split-unit designs can share a whole-plot number, fillers can have `PLOT = 0`,
and identifiers can restart at each location. Use the experimental hierarchy
and final physical coordinates appropriate to the design.

For example, a complete-block and physical-coordinate check can be written
without calling a package validator:

```{r complete-block-check}
design <- RCBD(t = 6, reps = 3, seed = 42)
book <- design$fieldBook
stopifnot(all(table(book$TREATMENT, book$REP) == 1L))
layout <- field_layout(design)
stopifnot(!anyDuplicated(layout[c("LOCATION", "ROW", "COLUMN")]))
stopifnot(identical(reproduce_design(design), design))
```

The complete-block, Latin-square, and full-factorial properties follow the
definitions in the NIST/SEMATECH handbook:
[randomized blocks](https://www.itl.nist.gov/div898/handbook/pri/section3/pri332.htm),
[Latin squares](https://www.itl.nist.gov/div898/handbook/pri/section3/pri3321.htm), and
[full factorials](https://www.itl.nist.gov/div898/handbook/pri/section3/pri333.htm).

The Latin rectangle definition follows
[Doyle's treatment](https://math.dartmouth.edu/~doyle/docs/latin/latin.pdf).
`test_latin.R` checks consecutive cyclic shifts with independent
row/column/symbol permutations for treatment counts 3 through 8, every supported
row count and several seeds. A separate model-matrix rank calculation verifies
connectivity and `(rows - 2) * (t - 1)` residual degrees of freedom. Two-row
rectangles are saturated. Columns need not be balanced incomplete blocks, and
the generator does not sample uniformly over all Latin rectangles.

## Numerical reference checks

For equally replicated incomplete-block designs, the independent test oracle
forms the treatment-by-block incidence matrix $N$ and computes

$$C = \operatorname{diag}(r) - N\operatorname{diag}(1/k)N^\mathsf{T}.$$

Here $r$ contains treatment replications and $k$ contains block sizes. If the
design is connected, the harmonic mean of its nonzero information eigenvalues,
divided by the common replication, gives the relative A-efficiency. The oracle
is checked against complete blocks (efficiency 1), the six pairs of four
treatments (efficiency 2/3), and a disconnected design (efficiency 0).
It then checks the reported efficiencies of incomplete-block, alpha, square,
and rectangular lattice catalogue cases. The legacy `blocksModel` table for
these functions describes the first location. The independent row-column
oracle instead uses an SVD projection to handle rank-deficient and crossed
row/column effects; see `test_row_column.R`.

The production block-efficiency calculation uses the singular values of
normalized, mean-centered treatment/block incidence, distinct from the oracle's
information-matrix eigendecomposition. Connectivity is checked directly in the
incidence graph. A disconnected comparison has zero overall efficiency, even
if comparisons within one connected component are estimable. A connected model
whose numerical information cannot be resolved raises a structured numerical
error instead of reporting a misleading positive efficiency.

Incomplete-block, lattice, onestage and twostage row-column designs, and sparse and p-rep
allocation across locations, use FielDHub's own compiled block search;
its efficiencies are checked against the same eigenvalue definitions
(`test_incomplete_blocks.R` for a single level of blocks and for resolvable
designs, `test_row_column.R` for twostage rows).
These checks validate achieved designs, not a claim of global optimality or
uniform sampling over all possible randomizations.

Spatial simulation tests compare the separable AR1 construction against an
independent dense covariance/Cholesky calculation, including negative
correlations and nonsquare fields (`test_simulation.R`). Response tests
check the stated treatment-specific model and unequal replication
(`test_simulation.R`). Design-aware
simulation tests check, for every design, that responses equal the mean plus
the stored effects, that each effect is constant within its treatment or
experimental unit, and that records replay exactly (`test_simulation.R`).
These are computational checks, not evidence that simulated responses fit a
particular biological experiment.

## Search bounds and diagnostics

| Operation | Bound and behavior at the limit |
|---|---|
| RCBD, Latin-square, incomplete-block, lattice, and row-column construction | Validates counts before allocation and rejects a projected total exceeding R's integer-index bound; available memory can impose a lower practical limit |
| Generated factorial, split-plot, split-split-plot, and strip-plot expansion | Checks the product of factor counts, replications, and locations against R's integer-index bound before expanding levels or allocating the field book |
| Spatial field construction | Validates scalar or per-location dimensions and rejects projected diagonal, optimized, augmented-RCBD, and p-rep field sizes beyond R's integer-index bound |
| Block-model connectivity | At most one graph expansion per treatment; stops when all treatments are reached or no new treatment is reachable |
| Latin-square placement | At most 100,000 placement iterations per square; a `fieldhub_search_error` records the square and budget on exhaustion |
| Cyclic Latin rectangles | Direct construction with work proportional to locations × rows × treatments; no iterative search, and projected field size checked before allocation |
| Repeated-check placement in RCBD | At most 100 attempts per block; warns and falls back to unrestricted placement if stratification cannot be completed |
| Single diagonal check optimization | At most 100 accepted exchanges or 100,000 candidate evaluations per location; an interrupted scan is never reported as a local optimum |
| Pair-swap optimization | A finite geometry-dependent threshold range, at most `stop_iter` sweeps per threshold, and bounded sampled candidates per swap; diagnostics distinguish exhaustion, completion, and an empty range |
| Row-column search | Positive finite whole-number `iterations`; onestage uses it to set the number of joint searches requested (the default 200 requests 10; `max(1, round(10 * iterations / 200))`), fewer when the work cap stops the run (`infoDesign$joint_searches`); twostage uses it as the effort of the row search, scaling the row search's evaluation budget by `iterations / 1000` (default 1,000, the full search) |
| Resolvable block search (incomplete-block, lattice and twostage row-column designs) | 20 searches (fewer above 1,000 blocks; one from a lattice construction) under an evaluation budget and a fixed work cap, so a run stops at a point set by the seed and inputs; a 60-second wall-clock limit remains as a safeguard and warns if it stops a run; shapes above 2,000 blocks or whose predicted work exceeds the cap get an unoptimized connected design with a warning, except a lattice with an algebraic construction (returned from the construction, A-optimal, no warning) and two replicates of blocks of two (built in closed form, no warning); twostage rows above the same kind of limit are left as the column design gave them, with a warning |
| Allocation block search | 20 searches under an evaluation budget, with full swap passes up to 632 plots and a lattice start for square-lattice shapes, with interrupts checked; a 60-second wall-clock limit warns and records the stop; a final even-up step leaves each entry with at most one copy more at any location than at another |
| Allocation balancing | At most one upward pass through the allocation rows; warns if no eligible entry can complete the balancing |
| Dimension cache | At most 64 retained queries and 4 MiB of retained keys and results; temporary search memory is outside this bound |

Finite iteration counts are not wall-clock or memory guarantees. Work still
grows with field size, and the work of a onestage row-column search is capped, so
on large designs the cap sets the number of joint searches. Forced allocation balancing can add entry copies to fill
smaller locations; inspect the returned allocation rather than assuming every
input copy budget is unchanged under that option. Pair-swap diagnostics
describe the retained result separately from an unsuccessful final attempt.

## Reproducibility and release evidence

Golden outputs are recorded on macOS. Structural and numerical tests run on
all supported test platforms, since optimizer choices can depend on R and
dependency versions and floating-point behavior. Replay comparisons use the
same package versions and recorded RNG settings. Review and explain any
deliberate fixed-seed change before accepting updated snapshots.

The release benchmark scripts under `tools/` measure optimizers, spatial
simulation, dimension searches, and layout exports. They record package/R
versions, platform, fixed inputs, elapsed time, and available allocation
measurements. Largest observed R allocation is not peak resident memory.
Compare results on the same host with comparable load, investigate changes,
and follow `RELEASING.md`; a successful local check does not establish that the
remote CI matrix or a deployment has passed.

## Acknowledgment

Earlier versions of FielDHub used the `blocksdesign` package (Edmondson 2021)
to optimize block and row-column designs. FielDHub's block engine is an
independent implementation, written without reading the source of
`blocksdesign`; its designs were validated against the results of
`blocksdesign` as a black box, on 90,200 onestage row-column designs and on the
incomplete-block, lattice and twostage row-column grids. FielDHub is grateful
to Rodney N. Edmondson for the package and for the optimized designs it gave
FielDHub for years.

Edmondson, R. N. (2021). *blocksdesign: Nested and Crossed Block Designs for
Factorial, Fractional Factorial and Unstructured Treatment Sets*. R package.
<https://CRAN.R-project.org/package=blocksdesign>
