How hiskp-lqcd/z2-phi4 works
1. What the repository is
- This is a C++ simulation code for the $Z_2$-$\phi^4$ lattice model.
- The core application generates field configurations with Monte Carlo updates, measures correlators/contractions, and writes binary outputs for later analysis.
2. Top-level runtime architecture
- The runtime path is: input file → simulation setup → lattice updates/measurements → binary output files.
- Postprocessing is separated into standalone tools and R-based analysis/plotting scripts.
3. Build and dependency boundary
- The project is built with CMake (CMakeLists.txt, main/CMakeLists.txt, include/CMakeLists.txt).
- It vendors Kokkos as a git submodule under external/kokkos, which is the main portability/performance layer.
- The README indicates optional build flags for timers, debug, FFTW, and CUFFT, so compute backends may vary by platform.
4. Core simulation executable(s)
- main/main.cpp is the primary entry point.
- Additional command-line tools in main/ indicate a small executable suite:
- contraction.cpp for producing contractions/observables,
- binning_contraction.cpp for binning-based postprocessing,
- conf_bin2asci.cpp for converting configuration binaries to ASCII,
- merging_replicas.cpp for combining replica outputs.
- These tools sit on top of the same simulation/data format conventions.
5. Physics and state representation
- The include/ and modules/ trees define the core domain objects: geometry, lattice, measurements, updates, DFT, random number utilities, and I/O parameters.
- The repository appears organized around a lattice state (Viewphi mentioned in the README) and parameter bundles (cluster::IO_params) that are passed across subsystems.
6. Update / Monte Carlo subsystem
- modules/metropolis.cpp and include/updates.hpp are the main update layer.
- modules/cluster.cpp suggests cluster-related logic is available, though the README notes cluster update work is incomplete or partially future work.
- Random-number support is isolated in modules/rand.cpp, modules/ranlxd.cpp, include/rand.hpp, and include/random.hpp.
7. Geometry and indexing layer
- Geometry is its own subsystem: include/geometry.hpp, modules/geometry/hopping.cpp, and modules/geometry/lex2c.cpp.
- This layer likely converts between lattice coordinates and linear storage/layout, and defines neighbor/hopping structure used by updates and measurements.
8. Measurement and transform layer
- modules/measurements.cpp and include/measurements.hpp handle observable computation and binary measurement output.
- include/DFT.hpp and modules/DFT.cpp indicate a Fourier-transform/analysis step, likely used for momentum-space observables or contractions.
- The README’s “contraction” output format is distinct from the configuration format, so measurement data is a separate output stream from raw configurations.
9. Binary I/O format boundary
- modules/write_viewer.cpp implements configuration writing, including headers and the serialized field view.
- The measurement output header is written by write_header_measuraments in modules/measurements.cpp.
- Both config and measurement outputs are binary files with headers, and the measurement files contain an integer configuration index followed by time-sliced double arrays.
10. Shared utility layer
- include/mutils.hpp, include/utils.hpp, modules/mutils.cpp, modules/utils.cpp, modules/endian.cpp, and include/IO_params.hpp provide supporting functionality: parameter handling, helper routines, and binary portability concerns.
- include/random.hpp and modules/ranlxd.cpp show that RNG implementation details are encapsulated rather than exposed to higher layers.
11. Analysis and plotting pipeline
- Ranalysis/ contains R scripts for reading headers, processing output, and comparing measurements.
- Rplot/ contains an R/Shiny plotting layer (ui.R, server.R, Shiny.Rmd, ggplot.R) plus helpers for reading blocks and error estimation.
- This is the second stage of the architecture: it consumes binary outputs from the simulator and produces figures, summaries, and likely paper-quality plots.
12. Documentation, examples, and validation
- doc/ contains the paper draft (phi4.tex, phi4.pdf) and example analysis notebooks, showing the simulation and analysis are tightly coupled to a publication workflow.
- example_script/ provides platform-specific build/run scripts for HPC systems.
- test/ contains reference data, input files, and R-based checks, indicating regression testing is done by comparing generated outputs to known-good binaries.
13. Likely end-to-end data flow
- Input parameters are read from an .in file.
- The simulator evolves lattice configurations and writes binary configuration files into the output directory.
- Measurement routines write separate binary contraction/observable files with headers and time-sliced data.
- R scripts and tools in main//Ranalysis//Rplot/ then decode, bin, compare, and visualize those outputs.
14. What to draw in an architecture diagram
- Treat the C++ simulation core as one subsystem, the binary file format as the main boundary, and the R analysis/plotting layer as a downstream consumer.
- Show Kokkos/external dependencies below the simulator as the portability/performance layer, and the example scripts/build system as the deployment wrapper around the executable suite.
Main entry points: main.cpp Main sim · write_viewer.cpp Config I/O · measurements.cpp Measurements · process_output.R R analysis
Components 21
C++ simulation core
Primary Monte Carlo runtime and shared domain logic.
- Main sim · Primary executable that reads input, initializes simulation, and drives Monte Carlo runs.
main/main.cpp - Updates · Metropolis and related update routines for evolving lattice configurations.
modules/metropolis.cpp - Cluster · Cluster-update subsystem present in the codebase, likely partial or experimental.
modules/cluster.cpp - Measurements · Observable calculation and measurement-file writing for simulation outputs.
modules/measurements.cpp - Config I/O · Writes configuration binaries and serialized lattice views.
modules/write_viewer.cpp - DFT · Fourier-transform and momentum-space analysis routines used by measurements and contractions.
modules/DFT.cpp - Geometry · Lattice geometry and coordinate-to-storage mapping used across updates and measurements.
modules/geometry/lex2c.cpp - Lattice · Core lattice/state representation shared by simulation subsystems.
include/lattice.hpp - Params · Simulation and I/O parameter bundle passed through runtime components.
include/IO_params.hpp - Randomness · Random-number interfaces and implementation details for stochastic updates.
modules/ranlxd.cpp
Postprocessing and plots
Standalone analysis, binning, and visualization tools.
- Contraction · Standalone executable for producing contractions and related observables.
main/contraction.cpp - Binning · Postprocessing tool for binning contraction output.
main/binning_contraction.cpp - Conf ASCII · Converts configuration binaries into ASCII form for inspection or downstream use.
main/conf_bin2asci.cpp - Replica merge · Combines outputs from multiple replicas into a merged dataset.
main/merging_replicas.cpp - R analysis · R scripts for reading headers, processing outputs, and comparing measurements.
Ranalysis/process_output.R - R plots · Plotting and interactive visualization layer for analyzed outputs.
Rplot/server.R
Build and portability
CMake build system and external performance layer.
Docs and examples
Usage examples, scripts, and reference material.
- Scripts · Platform-specific build/run scripts and example input files.
example_script/example.in - Paper docs · Paper draft and example analysis material tying the code to a publication workflow.
doc/phi4.tex - Regression data · Reference binaries, inputs, and comparison scripts used for validation.
test/test.sh
Connections 23
- CMake → Main sim: builds · CMake defines the primary executable.
- CMake → Contraction: builds · CMake also wires the analysis tools.
- CMake → Kokkos: depends on · Build integrates the vendored Kokkos submodule.
- Main sim → Params: reads · Main entrypoint consumes run parameters.
- Main sim → Lattice: initializes · Simulation allocates and evolves lattice state.
- Main sim → Updates: drives · Main runtime applies Monte Carlo updates.
- Updates → Geometry: uses · Updates depend on lattice geometry and neighbor indexing.
- Updates → Randomness: samples · Stochastic moves use the RNG subsystem.
- Cluster → Geometry: uses · Cluster logic also relies on geometry/indexing.
- Main sim → Measurements: measures · Simulation loop triggers observable measurement.
- Measurements → DFT: calls · Momentum-space or contraction analysis uses DFT routines.
- Measurements → Config I/O: writes · Measurements are serialized to binary output streams.
- Config I/O → Params: uses · Binary output metadata is driven by simulation parameters.
- Contraction → DFT: uses · Contraction tool reuses transform/analysis kernels.
- Binning → Contraction: consumes · Binning operates on contraction outputs.
- Conf ASCII → Config I/O: converts · ASCII conversion reads configuration binaries produced by the writer.
- Replica merge → Config I/O: merges · Replica merging operates on the same binary config format.
- R analysis → Measurements: reads · R analysis decodes measurement headers and binary outputs.
- R plots → R analysis: visualizes · Plotting layer consumes processed analysis results.
- Regression data → Config I/O: validates · Regression fixtures check configuration output compatibility.
- Regression data → R analysis: checks · R-based tests compare processed measurement results.
- Scripts → Main sim: runs · Example scripts build and launch the simulator on HPC systems.
- Paper docs → R analysis: documents · Documentation includes example analysis and paper workflow.