Interactive 2D wave optics simulator. Turn any image into a refractive scene, add sources, watch waves refract, diffract and interfere. Probes, intensity maps, video export.
C#
A re-run's memory budget counts the outgoing store as reclaimed
Alexander Graefcc3b840ee0f4
diff --git a/CLAUDE.md b/CLAUDE.mdindex e857d8b..9955abe 100644--- a/CLAUDE.md+++ b/CLAUDE.md@@ -903,9 +903,13 @@ FrameStore (temporary file in %LOCALAPPDATA%\WaveDudeNext\cache, DeleteOnClose,frames in the run's `FrameShape` (halves, or palette index bytes; the canvas shrunk by the divisor, see"Reduced preview" in the WPF shell list), planes in `FrameLayer` order, only thosepresent (`RunSpec.Layers`: field always; intensity when the intensity preview or export is on or a- probe needs it). The budget is `FrameMemory.Budget(percent)`: what `GlobalMemoryStatusEx` says is free now+ probe needs it). The budget is `FrameMemory.Budget(percent, reclaimedBytes)`: what `GlobalMemoryStatusEx` says is free nowminus a 2 GB margin (the application's own buffers and headroom for other programs, a sum that does not- scale with the machine's RAM), so a run never pushes the machine into its page file; Simulation > Frames+ scale with the machine's RAM), so a run never pushes the machine into its page file; `reclaimedBytes` is the+ outgoing run's `Store.RamBytes`, counted as available since `DiscardRun` frees it moments later (2026-10-16,+ found on the 16 GB laptop: measured next to the old 5.4 GB store, a half-resolution re-run that fit whole in+ RAM got a 0.6 GB budget and spilled 90 % to the HDD; the workstation's re-runs hit the disk the same way; the+ arithmetic is a pure internal overload that `library_check` checks exactly); Simulation > Frameskept in memory (2026-10-15, the user's design after the three-step share read as crude) offers "Auto" (thesame word in every language) and 10 % to 100 % of the physical RAM as an upper bound on top of that, thesetting `FrameMemoryPercent` with 0 = Auto, the default (a `user.config` from before keeps its 50 untildiff --git a/WaveDudeNext.WPF/MainWindow.Simulation.cs b/WaveDudeNext.WPF/MainWindow.Simulation.csindex 63fbc6a..f4ad10b 100644--- a/WaveDudeNext.WPF/MainWindow.Simulation.cs+++ b/WaveDudeNext.WPF/MainWindow.Simulation.cs@@ -91,8 +91,9 @@ public partial class MainWindowPreviewQuality quality = fullQuality ? PreviewQuality.Full : PreviewQualitySetting;RunSpec spec = RunSpec.Create(Project, LoadSceneImage(), Settings.Default.UseGpu, trackIntensity, quality);// the frames stay in memory up to the budget (the user's share of the RAM, capped by what is free now); only- // the rest needs room on the cache drive (2026-10-14)- long budget = FrameMemory.Budget(Settings.Default.FrameMemoryPercent);+ // the rest needs room on the cache drive (2026-10-14). The outgoing run's frames count as free: DiscardRun+ // below gives them back before the new store's blocks are touched (2026-10-16)+ long budget = FrameMemory.Budget(Settings.Default.FrameMemoryPercent, Run?.Store.RamBytes ?? 0);int ramFrames = Math.Min(spec.StoredFrameCount, FrameStore.FramesInBudget(spec.Shape, spec.Layers, budget));if (ramFrames < spec.StoredFrameCount){diff --git a/WaveDudeNext/Runs/FrameMemory.cs b/WaveDudeNext/Runs/FrameMemory.csindex 4a509e5..6a4bfb4 100644--- a/WaveDudeNext/Runs/FrameMemory.cs+++ b/WaveDudeNext/Runs/FrameMemory.cs@@ -63,15 +63,23 @@ public static class FrameMemory/// <paramref name="percentOfPhysical"/> of the physical memory when that is not <see cref="Automatic"/>; never negative./// </summary>/// <param name="percentOfPhysical">The user's upper bound, 1 to 100 (clamped), or <see cref="Automatic"/> for none.</param>- public static long Budget(int percentOfPhysical)+ /// <param name="reclaimedBytes">Memory the caller gives back before the new frames are allocated (the outgoing+ /// run's frames), counted as available: Windows still reports it as in use when asked now. Without it a re-run+ /// measured next to its predecessor's store under-budgets by that store's size and spills a run that fits+ /// (found on a 16 GB laptop, 2026-10-16).</param>+ public static long Budget(int percentOfPhysical, long reclaimedBytes = 0)+ => Budget(AvailableBytes(), PhysicalBytes(), percentOfPhysical, reclaimedBytes);++ /// <summary>The arithmetic of <see cref="Budget(int, long)"/> on given measurements, exact for the harness.</summary>+ internal static long Budget(long availableBytes, long physicalBytes, int percentOfPhysical, long reclaimedBytes){- long byAvailable = AvailableBytes() - Margin;+ long byAvailable = availableBytes + reclaimedBytes - Margin;if (percentOfPhysical <= Automatic){return Math.Max(0, byAvailable);}double share = Math.Clamp(percentOfPhysical, 1, 100) / 100.0;- long byShare = (long)(PhysicalBytes() * share);+ long byShare = (long)(physicalBytes * share);return Math.Max(0, Math.Min(byShare, byAvailable));}diff --git a/WaveDudeNext/Runs/FrameStore.cs b/WaveDudeNext/Runs/FrameStore.csindex 5c25452..392cb8c 100644--- a/WaveDudeNext/Runs/FrameStore.cs+++ b/WaveDudeNext/Runs/FrameStore.cs@@ -40,7 +40,7 @@ namespace WaveDudeNext.Runs;/// the memory manager's own allowance and then write at disk speed whatever the file's attributes say/// (a 10 GB full-HD run took 14 s against the drive with 40 GB of memory free), and a mapping paid a page/// fault per fresh page and flushed everything on unmap. The budget is ours, decided from what is free when-/// the run starts (<see cref="FrameMemory.Budget"/>), so the machine is never pushed into its page file.+/// the run starts (<see cref="FrameMemory.Budget(int, long)"/>), so the machine is never pushed into its page file./// Threads: the run's worker is the only writer (<see cref="Write(int, ReadOnlySpan{Half}, ReadOnlySpan{Half})"/>,/// frames in order); any thread may read frames below <see cref="Count"/>, which is published after the/// frame's bytes. Reads and writes run concurrently; <see cref="Dispose"/> waits for them, frees the memory@@ -177,7 +177,7 @@ public sealed unsafe class FrameStore : IFrameSource, IDisposable/// <param name="layers">The planes every frame holds; at least one.</param>/// <param name="capacity">Number of frames the run produces (<see cref="RunSpec.FrameCount"/>).</param>/// <param name="visualizeEvery">Steps per frame, kept for <see cref="StepOf"/>.</param>- /// <param name="ramBudget">Bytes of memory the frames may take; null takes <see cref="FrameMemory.Budget"/> at the default share.</param>+ /// <param name="ramBudget">Bytes of memory the frames may take; null takes <see cref="FrameMemory.Budget(int, long)"/> at the default share.</param>/// <exception cref="IOException">The file cannot be created or sized (for instance no disk space).</exception>public static FrameStore Create(string path, FrameShape shape, FrameLayers layers, int capacity, int visualizeEvery, long? ramBudget = null) =>new(path, shape, layers, capacity, visualizeEvery, ramBudget ?? FrameMemory.Budget(FrameMemory.DefaultPercent));diff --git a/_tools/library_check/Program.cs b/_tools/library_check/Program.csindex fec64f2..407f8a7 100644--- a/_tools/library_check/Program.cs+++ b/_tools/library_check/Program.cs@@ -724,6 +724,15 @@ internal static class ProgramCheck(FrameMemory.PhysicalBytes() > 1L << 30 && FrameMemory.AvailableBytes() <= FrameMemory.PhysicalBytes() && FrameMemory.Budget(50) <= FrameMemory.PhysicalBytes() / 2&& Math.Abs(automatic - Math.Max(0, FrameMemory.AvailableBytes() - FrameMemory.Margin)) < 512L << 20 && FrameMemory.Budget(50) <= automatic + (512L << 20) && FrameMemory.Budget(1) <= FrameMemory.PhysicalBytes() / 100,"memory: " + FrameMemory.Gigabytes(FrameMemory.PhysicalBytes()) + " physical, " + FrameMemory.Gigabytes(FrameMemory.AvailableBytes()) + " available, automatic " + FrameMemory.Gigabytes(automatic) + ", at most 50 % " + FrameMemory.Gigabytes(FrameMemory.Budget(50)));+ // the outgoing run's frames count as available (reclaimed; the laptop finding of 2026-10-16): exact on the+ // pure overload — 3 GB free + 5 GB reclaimed - 2 GB margin = 6 GB, capped by a 25 % share of 16 GB, never negative+ const long gb = 1L << 30;+ Check(FrameMemory.Budget(3 * gb, 16 * gb, FrameMemory.Automatic, 5 * gb) == 6 * gb+ && FrameMemory.Budget(3 * gb, 16 * gb, 25, 5 * gb) == 4 * gb+ && FrameMemory.Budget(3 * gb, 16 * gb, FrameMemory.Automatic, 0) == 1 * gb+ && FrameMemory.Budget(1 * gb, 16 * gb, FrameMemory.Automatic, 0) == 0+ && FrameMemory.Budget(1 * gb, 16 * gb, 100, 512L << 20) == 0,+ "budget: reclaimed memory of the outgoing run counts as available, the share still caps it");// a full-HD store of 1 GB: the writes must run at memory speed and the dispose must not flush the// dirty pages to disk (the file is deleted anyway; this used to take a minute for a 10 GB run)