Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

try performance.now() for stabler results #22

Merged
merged 1 commit into from
Nov 25, 2024
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
4 changes: 1 addition & 3 deletions .github/workflows/upload.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ jobs:
node-version: 20
cache: "yarn"

- uses: calcit-lang/[email protected]
with:
bundler: false
- uses: calcit-lang/[email protected]

- name: "compiles calcit to js"
run: caps --ci && cr -1 js
Expand Down
1 change: 1 addition & 0 deletions package.cirru → deps.cirru
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

{}
:calcit-version |0.9.6
:dependencies $ {}
|calcit-lang/lilac |main
|calcit-lang/memof |main
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"@webgpu/types": "^0.1.51",
"bottom-tip": "^0.1.5",
"query-string": "^9.1.1",
"typescript": "^5.6.3",
"typescript": "^5.7.2",
"vite": "^5.4.11"
},
"dependencies": {
Expand Down
2 changes: 1 addition & 1 deletion src/apps/attractor/aizawa.mts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export let loadRenderer = async (canvas: HTMLCanvasElement) => {
seedSize,
seedData: makeSeed(seedSize, 0),
getParams: (dt) => [
dt * 0.04, // deltaT
dt * 12, // deltaT
600.0, // scale
0.001, // width
0.99, // opacity
Expand Down
2 changes: 1 addition & 1 deletion src/apps/attractor/bouali.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ fn main(@builtin(global_invocation_id) GlobalInvocationID: vec3<u32>) {
return;
}

let ret = bouali(v_pos, params.delta_t * 0.01);
let ret = bouali(v_pos, params.delta_t * 1.);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codebase verification

⚠️ Potential issue

Time step multiplier is significantly larger than other attractors

Looking at all attractor implementations, the Bouali attractor's time step multiplier of 1.0 is unusually large compared to other attractors:

  • Aizawa: 0.001
  • Chen: 0.004
  • Dequan Li: 0.00004
  • Fourwing: 0.0006
  • Lorenz: 0.12
  • Lorenz83: 0.0008
  • Sprott: 0.0004

The large time step could lead to numerical instability. Consider using a smaller multiplier:

- let ret = bouali(v_pos, params.delta_t * 1.);
+ let ret = bouali(v_pos, params.delta_t * 0.01);
🔗 Analysis chain

Consider the stability implications of increasing deltaT

The change increases the time step by 100x (from 0.01 to 1.0), which could lead to numerical instability in the Bouali attractor simulation. The Bouali system has small coefficients (0.1, 0.02, 0.01) in its equations, and using such a large time step might cause:

  • Loss of accuracy in capturing the system's dynamics
  • Potential divergence from the true attractor behavior
  • Visual artifacts or unexpected patterns

Consider a more conservative approach:

- let ret = bouali(v_pos, params.delta_t * 1.);
+ let ret = bouali(v_pos, params.delta_t * 0.1);

Let's verify if similar changes were made to other attractors:

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check deltaT modifications in other attractor files
# Look for patterns of deltaT changes in other WGSL files

rg -A 1 "delta_t.*\*" "src/apps/attractor/*.wgsl"

Length of output: 117


Script:

#!/bin/bash
# First, let's find all WGSL files in the repository
fd -e wgsl

# Then, let's look for delta_t usage patterns in these files
rg -U "delta_t" --type wgsl

Length of output: 937


Script:

#!/bin/bash
# Let's examine delta_t usage in all attractor files
for file in src/apps/attractor/*.wgsl; do
  echo "=== $file ==="
  rg "delta_t" "$file" -A 1
done

Length of output: 5827


// Write back
particles_b.particles[index].pos = ret.position;
Expand Down
2 changes: 1 addition & 1 deletion src/apps/attractor/burke-shaw.mts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export let loadRenderer = async (canvas: HTMLCanvasElement) => {
seedSize,
seedData: makeSeed(seedSize, 0),
getParams: (dt) => [
dt * 0.04, // deltaT
dt * 2, // deltaT
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codebase verification

The deltaT change is inconsistent with other attractors

Looking at the deltaT values across all attractor implementations:

  • Most attractors use different deltaT multipliers based on their specific characteristics:
    • Burke-Shaw: dt * 2
    • Lorenz: dt * 0.04
    • Fourwing: dt * 40
    • Den-Tsucs: dt * 0.1
    • Halvorsen: dt * 12
    • etc.

The change from dt * 0.04 to dt * 2 in Burke-Shaw appears to be an outlier and could destabilize the simulation. Each attractor system has its own optimal time step based on its mathematical properties and stability requirements.

🔗 Analysis chain

Verify stability with the 50x larger time step

The change from dt * 0.04 to dt * 2 represents a 50x increase in the simulation time step. While this might improve timing stability, it could potentially:

  1. Affect the numerical stability of the Burke-Shaw attractor simulation
  2. Lead to different behavior on various hardware due to floating-point precision

Let's check if similar changes were made consistently across other attractors:

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check deltaT modifications across all attractor files
# Expected: Similar deltaT changes in other attractor implementations

# Search for deltaT parameter changes in attractor files
rg -B2 -A2 "deltaT" "src/apps/attractor/*.mts"

Length of output: 111


Script:

#!/bin/bash
# Let's first find all attractor files
fd ".*\.mts$" src/apps/attractor/

# Then search for deltaT in these files
rg -B2 -A2 "dt.*deltaT" src/apps/attractor/

Length of output: 4022

20.0, // scale
0.008, // width
0.99, // opacity
Expand Down
2 changes: 1 addition & 1 deletion src/apps/attractor/chen.mts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export let loadRenderer = async (canvas: HTMLCanvasElement) => {
seedSize,
seedData: makeSeed(seedSize, 0),
getParams: (dt) => [
dt * 0.04, // deltaT
dt * 2, // deltaT
20.0, // scale
0.008, // width
0.99, // opacity
Expand Down
2 changes: 1 addition & 1 deletion src/apps/attractor/dadras.mts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export let loadRenderer = async (canvas: HTMLCanvasElement) => {
seedSize,
seedData: makeSeed(seedSize, 0),
getParams: (dt) => [
dt * 0.04, // deltaT
dt * 0.4, // deltaT
20.0, // scale
0.008, // width
0.99, // opacity
Expand Down
2 changes: 1 addition & 1 deletion src/apps/attractor/den-tsucs.mts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export let loadRenderer = async (canvas: HTMLCanvasElement) => {
seedSize,
seedData: makeSeed(seedSize, 0),
getParams: (dt) => [
dt * 0.0003, // deltaT
dt * 0.1, // deltaT
20.0, // scale
0.008, // width
0.99, // opacity
Expand Down
2 changes: 1 addition & 1 deletion src/apps/attractor/dequanli.mts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export let loadRenderer = async (canvas: HTMLCanvasElement) => {
seedData: makeSeed(seedSize, 0),
getParams: (dt) => {
return [
dt * 0.04, // deltaT
dt * 4, // deltaT
20.0, // scale
0.008, // width
0.99, // opacity
Expand Down
2 changes: 1 addition & 1 deletion src/apps/attractor/fourwing.mts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export let loadRenderer = async (canvas: HTMLCanvasElement) => {
seedSize,
seedData: makeSeed(seedSize, 0),
getParams: (dt) => [
dt * 0.04, // deltaT
dt * 40, // deltaT
100.0, // scale
0.004, // width
0.99, // opacity
Expand Down
2 changes: 1 addition & 1 deletion src/apps/attractor/halvorsen.mts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export let loadRenderer = async (canvas: HTMLCanvasElement) => {
seedSize,
seedData: makeSeed(seedSize, 0),
getParams: (dt) => [
dt * 0.04, // deltaT
dt * 12, // deltaT
600.0, // scale
0.001, // width
0.99, // opacity
Expand Down
2 changes: 1 addition & 1 deletion src/apps/attractor/lorenz.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ fn main(@builtin(global_invocation_id) GlobalInvocationID: vec3<u32>) {
return;
}

let ret = lorenz(v_pos, params.delta_t * 0.0012 * (2. + 2. * rand(f32(index))));
let ret = lorenz(v_pos, params.delta_t * 0.12 * (2. + 2. * rand(f32(index))));
// let ret = four_wing(v_pos, params.delta_t * 0.01 * (20. + 2. * rand(f32(index))));
// let ret = aizawa(v_pos, params.delta_t * 0.001 * (12. + 4. * rand(f32(index))));

Expand Down
2 changes: 1 addition & 1 deletion src/apps/attractor/lorenz83.mts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export let loadRenderer = async (canvas: HTMLCanvasElement) => {
seedSize,
seedData: makeSeed(seedSize, 0),
getParams: (dt) => [
dt * 0.04, // deltaT
dt * 8, // deltaT
600.0, // scale
0.001, // width
0.99, // opacity
Expand Down
2 changes: 1 addition & 1 deletion src/apps/attractor/sprott.mts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export let loadRenderer = async (canvas: HTMLCanvasElement) => {
seedSize,
seedData: makeSeed(seedSize, 0),
getParams: (dt) => [
dt * 0.04, // deltaT
dt * 4, // deltaT
600.0, // scale
0.001, // width
0.99, // opacity
Expand Down
2 changes: 1 addition & 1 deletion src/apps/ball-spin.mts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export let loadRenderer = async (canvas: HTMLCanvasElement) => {
seedSize,
seedData: makeSeed(seedSize, 0),
getParams: (dt) => [
dt * 0.004, // deltaT
dt * 0.2, // deltaT
0.6, // height
0.2, // width
0.8, // opacity
Expand Down
2 changes: 1 addition & 1 deletion src/apps/bounce-trail.mts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export let loadRenderer = async (canvas: HTMLCanvasElement) => {
seedSize,
seedData: makeSeed(seedSize, 0),
getParams: (dt) => [
dt * 0.002, // deltaT
dt * 0.1, // deltaT
0.6, // height
0.2, // width
0.8, // opacity
Expand Down
2 changes: 1 addition & 1 deletion src/apps/bounce.mts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export let loadRenderer = async (canvas: HTMLCanvasElement) => {
seedSize,
seedData: makeSeed(seedSize, 0),
getParams: (dt) => [
dt * 0.004, // deltaT
dt * 0.1, // deltaT
0.6, // height
0.2, // width
0.8, // opacity
Expand Down
2 changes: 1 addition & 1 deletion src/apps/collision.mts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export let loadRenderer = async (canvas: HTMLCanvasElement) => {
seedSize,
seedData: makeSeed(seedSize, 0),
getParams: (dt) => [
dt * 0.004, // deltaT
dt * 0.4, // deltaT
0.6, // height
0.2, // width
0.8, // opacity
Expand Down
2 changes: 1 addition & 1 deletion src/apps/feday.mts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export let loadRenderer = async (canvas: HTMLCanvasElement) => {
seedSize,
seedData: makeSeed(seedSize, 0),
getParams: (dt) => [
dt * 0.004, // deltaT
dt * 0.1, // deltaT
0.6, // height
0.2, // width
0.8, // opacity
Expand Down
2 changes: 1 addition & 1 deletion src/apps/fireworks.mts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export let loadRenderer = async (canvas: HTMLCanvasElement) => {
seedSize,
seedData: makeSeed(seedSize, 0),
getParams: (dt) => [
dt * 0.004, // deltaT
dt * 4, // deltaT
0.6, // height
0.2, // width
0.8, // opacity
Expand Down
2 changes: 1 addition & 1 deletion src/apps/lamps.mts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export let loadRenderer = async (canvas: HTMLCanvasElement) => {
seedSize,
seedData: makeSeed(seedSize, 0),
getParams: (dt) => [
dt * 0.004, // deltaT
dt * 0.2, // deltaT
0.6, // height
0.2, // width
0.8, // opacity
Expand Down
2 changes: 1 addition & 1 deletion src/apps/orbit-spark.mts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export let loadRenderer = async (canvas: HTMLCanvasElement) => {
seedSize,
seedData: makeSeed(seedSize, 0),
getParams: (dt) => [
dt * 0.04, // deltaT
dt * 4, // deltaT
600.0, // scale
0.001, // width
0.99, // opacity
Expand Down
11 changes: 7 additions & 4 deletions src/render.mts
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,10 @@ export let createRenderer = async (
seedSize
);

return async function render(t: number, skipComputing: boolean = false) {
return async function render(
loopTimes: number,
skipComputing: boolean = false
) {
let paramsData = computeOptions.getParams(ticker.ticked());

let paramBuffer = buildParamBuffer(paramsData);
Expand Down Expand Up @@ -295,7 +298,7 @@ export let createRenderer = async (
entries: uniformEntries,
})
);
computePassEncoder.setBindGroup(1, particleBindGroups[t % 2]);
computePassEncoder.setBindGroup(1, particleBindGroups[loopTimes % 2]);
computePassEncoder.dispatchWorkgroups(Math.ceil(numParticles / 64));
computePassEncoder.end();
}
Expand All @@ -309,7 +312,7 @@ export let createRenderer = async (

const passEncoder = commandEncoder.beginRenderPass(renderPassDescriptor);
passEncoder.setPipeline(renderPipeline);
passEncoder.setVertexBuffer(0, particleBuffers[(t + 1) % 2]);
passEncoder.setVertexBuffer(0, particleBuffers[(loopTimes + 1) % 2]);
passEncoder.setVertexBuffer(1, buildSpriteVertexBuffer(vertexData));
passEncoder.setBindGroup(
0,
Expand All @@ -320,7 +323,7 @@ export let createRenderer = async (
entries: uniformEntries,
})
);
passEncoder.setBindGroup(1, mockedBindGroups[t % 2]); // mocked
passEncoder.setBindGroup(1, mockedBindGroups[loopTimes % 2]); // mocked

if (indexBuffer != null) {
passEncoder.setIndexBuffer(indexBuffer, "uint32");
Expand Down
4 changes: 2 additions & 2 deletions src/util.mts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ export class TimeTicker {
startedAt: number;

constructor() {
this.startedAt = Date.now();
this.startedAt = performance.now();
}

/** return in seconds */
ticked(): number {
let now = Date.now();
let now = performance.now();
let elapsed = now - this.startedAt;
this.startedAt = now;
return elapsed * 0.001;
Expand Down
Loading
Loading