Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ The user-visible changes of canoP, by version (see `CONTRIBUTING.md` for the ver

- canoP studio in English or in French: a FR/EN switch in the top bar, saved in the settings. New
installations start in English; studios set up before keep French.
- Tutorial of canoP studio in the documentation: the build, a run of the blast example, and a
convergence campaign with a new initial condition in Python and its analysis.

## 0.1.0 - 2026-11-27

Expand Down
4 changes: 4 additions & 0 deletions docs/_static/canop.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@
.sidebar-logo { max-width: 85%; }
table.docutils td, table.docutils th { vertical-align: top; }
table.canop-params td:first-child { white-space: nowrap; }
/* tutorial: the screenshot of a panel beside its explanation, below it on narrow screens */
img.tuto-side { float: left; margin: 0.3em 1.5em 1em 0; }
@media (max-width: 60em) { img.tuto-side { float: none; display: block; margin: 0 auto 1em; } }
article h2, article h3 { clear: both; }
7 changes: 7 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ getting-started/installation
getting-started/running
```

```{toctree}
:caption: Tutorial
:maxdepth: 2

tutorial/index
```

```{toctree}
:caption: User guide
:maxdepth: 2
Expand Down
93 changes: 93 additions & 0 deletions docs/tutorial/build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<!--
SPDX-FileCopyrightText: 2026 canoP authors

SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-->

# 1. Build canoP

The left panel of canoP studio holds its settings, in two parts: **Code and build** (what is built,
with which environment, and where) and **Execution** (how the runs are launched). This page goes
through their choices, then builds canoP once.

## The Code and build panel

```{image} images/build-code-panel.webp
:alt: The Code and build panel of canoP studio
:width: 300px
:class: tuto-side
```

**canoP sources** is the clone the studio runs from. **Build directory** is empty by default:
`<sources>/_build`. Each variant (Kokkos backend and architectures) has its own directory: the OpenMP
build in `_build`, a Cuda build for an A100 in `_build_cuda_ampere80`, so that switching the backend
keeps both builds.

**Dimension** chooses the executable, `canoP_2d` or `canoP_3d`; a build compiles the one chosen. The
model (`onephase`, `twophase5eq`, `twophase7eq`) is not chosen here but in the input file
(`simulation.model`): the physics is written in Python and compiled at run time, so a new model,
initial condition or flux needs no new build of canoP.

**Kokkos backend**: OpenMP for the CPUs, Cuda for NVIDIA GPUs. **CPU architecture** (and, for Cuda,
the architecture of the GPU of the runs, e.g. `AMPERE80` for an A100) is passed to Kokkos; *none*
leaves the choice to the compiler.

**Environment (modules…)** are the shell lines run before the build and before each run: they load
a C++17 compiler, CMake, MPI, HDF5 and a Python 3.11 or later. **CMake options** are added to the
configuration. Both, and the Slurm options of the runs, belong to the backend: switching from
OpenMP to Cuda shows those of Cuda, and back. The studio sets the backend and architectures of Kokkos
itself and removes them from the CMake options.

**Build on**: *this node* runs the build on the node of the studio (the login node); *a compute
node* submits a Slurm job of one task with **Build jobs** cores, with the **Slurm options of the
build**.
A Cuda build needs no GPU. On a shared login node, prefer the compute node.

**Configure** runs CMake only; **Build** configures when needed and compiles; **Empty the build
directory** deletes the build of this variant (CMake configuration included). **Executable** shows
the executable once it exists.

## The first build

Click **Build**. With *a compute node*, the studio submits the job and the top bar shows
`build: pending`, then `build: running`; a message tells when it ends. The first build of a clone
takes longer than the next ones: canoP builds its own Kokkos (shared library), pykokkos-base and
pykokkos, and, when the Python of the environment lacks numpy, h5py or pybind11 3.1.0, makes its
Python environment `<sources>/.local/python<version>` with pip, which needs access to the internet
from the node of the build. On a cluster, the first build took about ten minutes on 8 cores of a
compute node; the next builds only compile what changed.

The **Log** tab, **Build**, follows the log while it is written; the menu also gives the logs of
the previous builds, and **Search** finds a word or a regular expression in the log:

```{image} images/build-log.webp
:alt: The log of the build in the Log tab
:width: 100%
```

When a build fails, the error is in this log: an environment line to fix (a module that does not
exist), a missing dependency found by CMake, or an error of the compiler.

## The Execution panel

```{image} images/build-execution-panel.webp
:alt: The Execution panel of canoP studio, with the launcher sbatch
:width: 300px
:class: tuto-side
```

**Launcher**: *direct* runs canoP on the node of the studio, for short runs; *mpirun* runs it
there with MPI (`mpirun -np`); *srun* starts it through Slurm and waits for its allocation; *sbatch*
submits a Slurm job with the **Slurm options of the runs** of the backend, which runs canoP with
`srun`. The panel then lists the partitions of the cluster (`sinfo`), with their time limit and
nodes.

**MPI processes** × **OpenMP threads** are the cores of a run; 0 threads leaves `OMP_NUM_THREADS`
unset, so that OpenMP takes every core it sees: with *direct* on a login node, set a few threads.
**Extra arguments** are added to the command line of canoP.

**Safeguards of the campaigns run by an agent**: an AI agent that drives the studio through its MCP
tools (Claude Code, for example) cannot submit more runs, nodes, cores, GPUs or core-hours than
these limits without asking you; only you can change them.

Next: {doc}`run`.
198 changes: 198 additions & 0 deletions docs/tutorial/campaign.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
<!--
SPDX-FileCopyrightText: 2026 canoP authors

SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-->

# 3. A convergence campaign

This page measures the order of convergence of the scheme of canoP. A new initial condition, written
in Python, gives a smooth problem with a known solution: a sine wave of density advected at uniform
velocity in a periodic domain. A campaign runs it on uniform meshes of 32² to 512² cells, and its
`analysis.py` plots the error against the number of cells.

## The input file

The base of the campaign is the input file of the editor. In the **Run** tab, start from the blast
example and change it as below (or paste it in the text of the ini file):

```{literalinclude} files/convergence.ini
:language: ini
```

Compared with the blast example:

- `physics.initial_condition = sine`, the new initial condition, with its parameters in `[sine]`;
- `simulation.time_order = 2` (SSP Runge-Kutta 2): with the forward Euler scheme, the error in time,
of order 1 at a fixed CFL number, would hide the order 2 in space;
- `amr.epsilon_refine = -1` and `amr.epsilon_coarsen = -1`: the refinement indicator lies in
[0, 1], so every block is refined up to `amr.max_refine` and none is coarsened, which gives a
uniform mesh of 2<sup>max_refine</sup> cells per direction;
- `simulation.tmax = 1` with the velocity (1, 1): the wave crosses the periodic domain once in each
direction and is back at its initial position;
- `hyperbolic_flux = hllc` and fewer outputs (`io.save_count = 5`).

## The campaign

In the **Campaign** tab, **New campaign** takes the input file of the editor as its base. Give it a
name and one line per parameter to vary, `section.key = v1, v2, ...`; the campaign has one run per
combination of the values (here 5):

```{image} images/campaign-new.webp
:alt: The dialog of a new campaign, with the sweep of amr.max_refine
:width: 640px
```

A campaign is a directory `_campaigns/<name>/`: a signac project with one directory per run
(`workspace/<id>/`), its own copy of the physics of canoP (`physics/`, in a git repository: each launch
commits it and records the commit in the runs) and its analysis (`analysis.py`). Its runs are
*prepared*: nothing runs before they are launched.

```{image} images/campaign-prepared.webp
:alt: The five prepared runs of the campaign
:width: 100%
```

## A new initial condition

An initial condition is a file of `physics/<model>/initial_conditions/`; its name is the value of
`physics.initial_condition`. The campaign uses its own copy of the physics: add the file to
`_campaigns/convergence/physics/onephase/initial_conditions/sine.py`, for example in the editor of VS
Code:

```{literalinclude} files/sine.py
:language: python
```

The file has two parts:

- `params(dim)` runs in plain Python when the run starts: it reads the parameters of the input file
(`get_real`, `get_int`, `get_string` with a section, a key and a default) and returns them by name.
The docstring describes them: canoP studio shows it under the mouse, as on the previous page.
- `initial_state` is a pykokkos function (`@pk.function`), translated into C++ and compiled for the
CPU or the GPU. It receives the centre of a cell (`z = 0` in 2D), the parameters (`ip.m0`, ...),
the domain (`domain[0:3]`: lower corner, `domain[3:6]`: lengths) and the dimension, and returns
the primitive state `Prim(m, P, T, vx, vy, vz)` of the cell.

Select a run of the campaign, **View** loads it in the editor, and **Initial condition** checks the
new file at once: it compiles the kernels of the initial condition and shows the result.

```{image} images/campaign-initial-condition.webp
:alt: The sine wave on the uniform mesh of the coarsest run
:width: 100%
```

## What pykokkos accepts

A pykokkos function is not Python: it is translated into C++, with a subset of Python. These rules
were checked by compiling variants of `sine.py`:

| In `initial_state` | Result |
|---|---|
| a new variable without annotation, `s2 = sx * sy` | refused by the C++ compiler, at the line: `'s2' was not declared in this scope`; write `s2: float = sx * sy` |
| `max(a, b)` of Python | refused: `undefined symbol max used`; use `pk.fmax` (and `pk.fmin` for a minimum) |
| `np.sin(x)` of numpy | refused: `undefined symbol np used`; use the functions of pykokkos: `pk.sin`, `pk.exp`, `pk.sqrt`, `pk.atan2`, ... |
| a constant of the module, `SHIFT = 0.5` | refused: `undefined symbol SHIFT used`; make it a parameter in `params`, or write the value |
| `a if c else b` | refused: `Not supported for translation`; write an `if` statement |
| a list, `[0.5, 0.25][0]` | refused, without the line: `AttributeError: 'List' object has no attribute 'id'` |
| `math.pi`, `abs(x)`, `x ** 2` | accepted |
| `1 / 2`, `ip.n / 2` with an integer parameter | accepted, a real division as in Python (0.5) |

Other points to keep in mind:

- **Every initial condition of the model is compiled together.** An error in `sine.py` stops every
run of the model that uses this physics, whatever its initial condition: here, the runs of this
campaign only, since it has its own copy of the physics.
- **Errors point to the line of the file of the physics**, for the translation by pykokkos and for
the C++ compiler. On a GPU, nvcc gives no column: check a new file on the CPU first.
- **A change compiles again** the kernels that contain the file, about 10 s each, at the next run or
initial condition.
- **The parameters are numbers.** `params` may compute anything in Python, but returns integers or
reals, with names that do not depend on the input file.

## The runs

The runs of a campaign are Slurm jobs, one per run. The **Launcher** of the **Execution** panel must
be *sbatch* (see {doc}`build`), and its settings apply to every run: here 1 MPI process of 16 OpenMP
threads, with the `#SBATCH` lines of the panel (a partition, a time limit of one hour, one node).
**Launch the prepared runs (5)** asks for a confirmation:

```{image} images/campaign-launch.webp
:alt: The confirmation of the launch of the five runs
:width: 520px
```

At the launch, the studio:

1. commits the physics of the campaign if it changed (here the new `sine.py`) in the git repository
of the campaign;
2. copies that commit into each run (`canoP_physics/`) and writes the commits of canoP and of the
physics at the top of its input file (lines `# [canoP studio]`): each run keeps the code and the
physics it ran with;
3. submits the runs with signac-flow (`python project.py submit -o run` in the directory of the
campaign), then commits the launch.

The table follows the runs: *queued*, *running*, then *finished*, with their outputs and the
time reached. Depending on the limits of the partition, the runs run at the same time or one after
the other. A click on a run gives its details: its Slurm job, the versions of canoP and of the physics
it ran with, its parameters, and its metrics, read from its log by the campaign (`project.py`) once
it is finished.

```{image} images/campaign-finished.webp
:alt: The five finished runs of the campaign, with their durations and metrics
:width: 100%
```

## The analysis

The **Analysis** tab draws a metric of the finished runs against a parameter of the campaign. Choose
the campaign, `amr.max_refine` as the parameter (x) and `elapsed_s`, the duration of each run, as the
metric (y). The other metrics (`n_outputs`, `t_final`, `mcups`) are read from the logs by the
campaign itself (`project.py`) when its runs finish.

Here the durations are 7, 7, 45, 68 and 473 s. The smallest runs spend their time mostly in the start
of canoP and in the outputs. From 256² to 512², the duration is multiplied by 7, close to 8, the work
of one more level in 2D: 4 times more cells, and twice more time steps, since the time step follows
the size of the cells.

The panel on the right belongs to the campaign: it is drawn by its file `analysis.py`, made from a
template when the campaign is created. Replace that file with the analysis of the convergence,
`_campaigns/convergence/analysis.py`:

```{literalinclude} files/analysis.py
:language: python
```

The studio calls three functions of this file, and loads it again as soon as it is saved:

- `analyse(points, x, y)` receives the points of the graph, `[(x, y, run), ...]` for the finished
runs, where `run` is the start of the id of the run (8 characters, as in the table), and returns a
result made of numbers, strings, lists and dictionaries, which the page keeps as `an_result`. Here
it does not use x and y: for each run it finds its directory (`_run_dir`), reads its last output
(`workspace/<id>/sine_iter*.xmf`, with the XDMF reader of VTK), compares the density with the exact
solution, the initial wave moved by (vx, vy) t, and computes the L1 error and the observed order
between two successive levels, log(e<sub>coarse</sub> / e<sub>fine</sub>) / log(n<sub>fine</sub> /
n<sub>coarse</sub>).
- `draw(ax, points, result, x, y)` may add to the matplotlib graph of the tab (nothing here).
- `panel(app)` builds the panel with the widgets of trame and Vuetify (`app.v3`, `app.html`), bound
to `an_result`: the log-log plot of the error, drawn as an image by `analyse`, and the table of the
errors and orders.

The file runs in the studio, with its Python (numpy, matplotlib and VTK are there): keep it fast, as
here with the errors kept from one call to the next as long as the outputs do not change.

```{image} images/campaign-analysis.webp
:alt: The Analysis tab: duration of the runs against amr.max_refine, and the convergence of the L1 error
:width: 100%
```

The L1 error goes from 1.1 × 10<sup>-2</sup> on 32² cells to 9.0 × 10<sup>-5</sup> on 512² cells, and
the observed order rises from 1.69 (32² to 64²) to 1.83 (256² to 512²). It is below 2 because the
minmod limiter flattens the slopes at the extrema of the wave, where the reconstruction is only of
order 1; these regions shrink as the mesh is refined, and the order approaches 2.

The campaign of this page, as it ran (its git repository: physics, input files, analysis, metrics),
is in [canoPdev/tutorial](https://github.com/canoPdev/tutorial), with its results on the
[wiki](https://github.com/canoPdev/tutorial/wiki). Its README tells how to run it again.

Next: back to {doc}`index`.
Loading
Loading