Skip to content

Save, load and replay a project ​

Use these recipes when a grid takes more than one command: a Project records every command in a journal, so you can undo it, save the whole state to a directory, load it back (replayed and checked, or fast without replay), and edit the journal as a script.

The script runs in about 2 s.

Undo and redo a command ​

python
p = Project()
p.load_geometry("sphere.stl")
p.build_topology(
    "cubed_sphere", nk=9, ds=0.02, growth=1.15, ni=33, nj=64, theta_cap=np.deg2rad(35.0)
)
p.march()
marched = [b.nodes.copy() for b in p.mesh.blocks]
p.redistribute("sub0", "j_lo_k_lo", "tanh2", beta=2.0)

p.undo()  # back to the marched grid, bit for bit
print(
    "undo restores:",
    all(np.array_equal(b.nodes, n) for b, n in zip(p.mesh.blocks, marched, strict=True)),
)
p.redo()  # and forward again
print([type(c).__name__ for c in p.history])
text
undo restores: True
['LoadGeometry', 'BuildTopology', 'March', 'Redistribute']
  • Every Project method that changes state is one command: undo() restores the state before it bit for bit, redo() re-applies it. A new command after an undo clears the redo stack.
  • Grid snapshots for undo live in an on-disk cache, not in memory.
  • Output commands (write_geo, write_adjacency, write_xall, ...) are recorded in the journal but never undone: the files stay written.

Save a project and load it back ​

python
p.write_geo("out/sphere")
p.save("sphere.shore")  # project.toml, journal.py, inputs/, adjacency.json, grids/
print(sorted(f.name for f in Path("sphere.shore").iterdir()))

t0 = time.perf_counter()
q = Project.load("sphere.shore")  # replays the journal, checks every grid (to 1e-10)
t1 = time.perf_counter()
r = Project.load("sphere.shore", replay=False)  # reads the saved grids instead
t2 = time.perf_counter()
print(f"replayed load {t1 - t0:.2f} s, load without replay {t2 - t1:.2f} s")
same = all(
    np.array_equal(a.nodes, b.nodes) for a, b in zip(q.mesh.blocks, r.mesh.blocks, strict=True)
)
print("same blocks:", same, "same journal:", q.journal() == r.journal())
text
['adjacency.json', 'grids', 'inputs', 'journal.py', 'project.toml']
replayed load 0.17 s, load without replay 0.02 s
same blocks: True same journal: True

The project directory holds:

EntryContent
project.tomlformat, SHORE version, command count, each component's block labels, input hashes
journal.pythe history as a replayable script, output commands dropped
inputs/copies of the geometry files (SHA-256 checked on load)
grids/<component>/each component's blocks (.npz) and face graph (adjacency.json)
adjacency.jsonthe face graph of the current component
  • Project.load(dir) checks the input hashes, replays the journal and requires every rebuilt grid to match the saved one to 1e-10 of the block's size (ADR 0061: the last bits depend on the machine); a changed grid raises MeshInputError with its deviation.
  • The directory is portable: inputs are copied in and the journal refers to them relative to the project.

Load a project fast, without replay ​

Project.load(dir, replay=False) reads each component's blocks from its saved grids and face graph and only records the journal: no march, no collar build. Use it to open a large project to write outputs or make small edits. The blocks, face BCs, journal and any Xall run written from it equal those of a replayed load.

python
try:
    r.undo()  # the loaded history was recorded, not run: it cannot be undone
except ParameterError as err:
    print("refused:", err)
try:
    r.march()  # nor can a loaded mesh be marched again
except ParameterError as err:
    print("refused:", err)
r.snap()  # new commands work, and can be undone
r.undo()
text
refused: the history before this point was loaded without replay (Project.load(replay=False)): it cannot be undone.
refused: march: the current mesh (blocks) cannot be marched; ...
  • The loaded history cannot be undone, and a loaded mesh cannot be marched again. Commands you run after the load can be undone as usual.
  • Seams read back from grids are copies, not shared views; commands that edit nodes (snap, redistribute, surface_grid) write them through, so seams stay closed (ADR 0040).
  • A project saved before the face graphs existed cannot be loaded without replay: load it once with replay and save it again.

Edit a journal and replay it ​

python
p.save_journal("sphere_journal.py")
print(Path("sphere_journal.py").read_text())

# a finer wall-normal schedule: edit the script, replay it
text = Path("sphere_journal.py").read_text()
Path("sphere_fine.py").write_text(text.replace("nk=9, ds=0.02", "nk=17, ds=0.005"))
fine = run_journal("sphere_fine.py")  # or: shore run sphere_fine.py
print("fine:", fine.mesh.blocks[0].nodes.shape)
python
# SHORE journal: replay with `shore run <this file>`.
from shore.project import Project

p = Project()
p.load_geometry('sphere.stl')
p.build_topology('cubed_sphere', nk=9, ds=0.02, ni=33, nj=64, theta_cap=0.6108652381980153)
p.march()
p.redistribute('sub0', 'j_lo_k_lo', 'tanh2', beta=2.0)
p.write_geo('out/sphere')
  • A journal is a plain Python script of Project calls, with defaults left out. Change a parameter, insert a command, point load_geometry at an edited file, and replay it with run_journal or shore run.
  • Paths in a journal are used as written, relative to the directory you run from. The journal inside a saved project refers to inputs/...: run it from the project directory, or pass run_journal(path, input_dir=project_dir).
  • A journal is executed as Python: run only journals you trust.
bash
shore run sphere_fine.py --save-project sphere_fine.shore

Build a project from the CLI ​

shore mesh, shore collar and shore primitive * take --project DIR --component NAME: they load the project (or create it), build that component and save it. --no-replay opens the existing project without replay.

bash
shore mesh sphere.stl --ni 33 --nj 64 --nk 9 --ds 0.02 --theta-cap-deg 35 \
    --project case.shore --component sphere
shore primitive box --min -4 -4 -4 --max 4 4 4 --ni 17 --nj 17 --nk 17 \
    --project case.shore --component background --no-replay
cd case.shore && shore run journal.py     # replays both components

Write the merged Xall files of all components with shore xall case.shore --out run (Multi-component Xall run).

See also ​

Released under the MIT License.