OpenPFC / A first program
Give the grid a physical meaning
Sixteen samples do not define a physical length until we say where they are.
Download runnable lesson (.zip)
Includes the model, inputs, required headers and a short run guide.
A Domain describes the global grid. Its dimensions count samples; its origin and spacing assign those samples physical coordinates. It does not allocate a solution field or choose a differential operator.
State the geometry in one expression
const auto domain = pfc::domain::create(
pfc::GridSize{{16, 8, 1}}, pfc::PhysicalOrigin{{-1, -2, 0}},
pfc::GridSpacing{{.125, .5, 1}}, pfc::Bool3{true, true, false});
const pfc::Int3 index{3, 5, 0};
const auto x = pfc::domain::to_coords(domain, index);
const auto restored = pfc::domain::to_indices(domain, x);
if (restored != index) throw std::runtime_error("Coordinate round trip failed");The named argument types help the compiler distinguish sample counts, origin and spacing. The first coordinate follows
\[ \begin{gathered} x_i=-1+.125i, \\ i=0,\ldots,15. \end{gathered} \tag{1}\]
Thus \(i=3\) means \(x=-.625\). Likewise, \(j=5\) means \(y=.5\). to_indices maps that sample’s coordinate back to its original index. This exact round trip concerns grid samples; arbitrary points require attention to the API’s rounding and bounds conventions.
Count a periodic endpoint once
The last x sample is .875, while the periodic box’s upper boundary is 1. The point at 1 is identified with the point at -1, so storing both would duplicate a sample. A nonperiodic axis instead spans its first and last samples. Here the nonperiodic z axis has one sample, representing a plane.
The program writes all three axes to results/domain.csv, including their last sample and upper boundary. Compare those columns before moving on to endpoint conventions.
Run your first experiment
openpfc run model.cpp input.json --launcher=noneThe geometry is deliberately visible in model.cpp; this lesson’s input is empty. Change the origin or spacing, then predict the printed coordinate before running again. Try changing the periodicity of x and compare its reported upper boundary. This program does not initialize MPI.
The complete source and input are available. Continue with distributed ownership or a first derivative.