Cholesky_Midpoint_RF is a MATLAB project for generating 2D Gaussian random fields on structured grids.
It provides multiple covariance kernels, solver backends, visualization/export utilities, benchmarking scripts, and a backward-compatible legacy entry point.
- Covariance models:
gaussian,exponential,spherical,matern - Solver backends:
cholesky,kl,circulant,approximate, andautoselection - Generation modes: single realization, batch generation, optional parallel and GPU workflows
- Conditional random fields (CRF): unconditional + conditioned realizations from grid and observation tables
- Visualization: contour, surface, histogram, variogram cloud
- Export: CSV, VTK, MAT
- Compatibility: legacy wrapper
RandomField2DCholMethod.m
+rf2d/: main packagecreateGenerator.m: public factory APIRandomFieldGenerator.m: high-level generator objectCovarianceModel.m: covariance kernels and pairwise/lagged covarianceConditionalRandomField.m: CRF workflow for unconditional + conditional fieldsrunConditionalRandomField.m: public CRF entry pointValidation.m: model/solver/grid/parameter validationLegacyAdapter.m: bridge for the legacy function interface+solvers/: solver implementations and auto-selection factory+viz/: plotting helpers+io/: export helpers
RandomField2DCholMethod.m: historical API wrapperrandex.m/conrandex.m: CRF compatibility wrappersdemo/: benchmark and Live Script conversion utilitytests/: MATLAB unit tests+docs/generateDocs.m: HTML documentation publishing+toolbox/buildToolbox.m:.mltbxpackaging helpertoolbox/rf2d_toolbox.prj: toolbox project file
- MATLAB R2020b or newer
- Statistics and Machine Learning Toolbox
- Parallel Computing Toolbox (optional, for
parfor,spmd, and GPU workflows)
Clone the repository and add it to your MATLAB path:
addpath(genpath(pwd))x = linspace(0, 100, 128);
y = linspace(0, 40, 64);
params = struct("variance", 2.0, "corrLength", [20 8], "smoothness", 1.2, "nugget", 1e-8);
g = rf2d.createGenerator(x, y, "matern", params, "Solver", "auto", "Seed", 1234);
f = g.realize();
figure; g.contourPlot(f, 20);rf2d.createGenerator(x, y, covarianceModel, covarianceParameters, Name=Value) supports:
Solver:"auto"|"cholesky"|"kl"|"circulant"|"approximate"Seed: random seedUseGPU: logical flag for solver samplingMean: scalar mean value of the fieldMaxDirectPoints: threshold used by automatic solver routingMaxRank: rank for Nyström approximationKLModes: number of KL modesJitter: numerical stabilization term
Common generator methods:
realize()orrealize("UseGPU", ..., "UseParallel", ...)generateBatch(nFields, useGPU, useParallel)reseed(seed)contourPlot,surfacePlot,histogramPlot,variogramCloudexportCSV,exportVTK,exportMAT
CRF API:
rf2d.runConditionalRandomField(config): file-based CRF pipelinerf2d.ConditionalRandomField.fromTables(gridTable, observationTable, config): in-memory CRF pipelinerandex(config)andconrandex(config): compatibility entry points
Minimal CRF config fields:
grid.dx,grid.dycovModel.type,covModel.rangeX,covModel.rangeY,covModel.nuggetprior.mean,prior.stdsim.nRealizations,sim.randomSeed,sim.jitterio.gridTablePath,io.observationPath,io.unconditionalPath,io.conditionalPathlogging.enabled,logging.level
CRF input tables:
- Grid table columns:
id,j,k - Observation table columns:
j,k,obsValue,obsVar
When Solver="auto":
- uses Cholesky for smaller problems (
n <= MaxDirectPoints) - uses circulant embedding on regular grids for larger problems
- falls back to approximate Nyström for large non-regular cases
If solver preparation fails for numerical reasons, the generator falls back to the approximate solver.
The historical function is preserved:
[RFC, RFPHI, c, phi] = RandomField2DCholMethod("Coord1.xlsx", 1, 0);It delegates to rf2d.LegacyAdapter and keeps output compatibility.
Run:
demo.demo_benchmark_rf2dRun the CRF demo with project-level sample tables (outside any data subfolder):
demo.demo_crf_rf2dTo convert the demo script into a Live Script:
demo.createLiveScriptRun:
docs.generateDocsGenerated HTML is written to docs/html/.
Run:
toolbox.buildToolboxThis creates an installable .mltbx under dist/.
Run all tests:
results = runtests("tests", "IncludeSubfolders", true);
table(results)Run tests with coverage output:
import matlab.unittest.TestRunner
import matlab.unittest.plugins.CodeCoveragePlugin
import matlab.unittest.plugins.codecoverage.CoverageReport
suite = testsuite("tests", "IncludeSubfolders", true);
runner = TestRunner.withTextOutput;
runner.addPlugin(CodeCoveragePlugin.forFolder(pwd, "Producing", CoverageReport("tests/coverage")));
results = runner.run(suite);