Skip to content

Commit

Permalink
qwen
Browse files Browse the repository at this point in the history
  • Loading branch information
tailuge committed Nov 17, 2024
1 parent 3d0e982 commit 342e66e
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 9 deletions.
2 changes: 1 addition & 1 deletion dist/diagram.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/mathaven.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 8 additions & 7 deletions src/mathaven.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { R } from "./model/physics/claude/constants";
import { NumericCalculation } from "./model/physics/claude/geminipro"
import { Mathaven } from "./model/physics/claude/qwen";
import { State } from "./model/physics/claude/state";

declare global {
Expand All @@ -8,7 +8,8 @@ declare global {
}
}

const calc = new NumericCalculation(2.0, Math.PI / 4, 1.5 * 2 / R, 2 * 2 / R)
//const calc = new NumericCalculation(2.0, Math.PI / 4, 1.5 * 2 / R, 2 * 2 / R)
const calc = new Mathaven(2.0, Math.PI / 4, 1.5 * 2 / R, 2 * 2 / R)

try {
calc.solve()
Expand Down Expand Up @@ -52,7 +53,7 @@ function color(index: number): string {
const lightness = 50
return `hsl(${hue}, ${saturation}%, ${lightness}%)`
}
function extractValues<T>(history, selector: (s: State) => T): T[] {
function extractValues<T>(history, selector: (s: Mathaven) => T): T[] {
return history.map(selector)
}

Expand All @@ -70,14 +71,14 @@ function createTrace(x: number[], y: number[], name: string, color: string) {
}
}

const vals = (selector: (s: State) => number): number[] => extractValues(calc.history, selector)
const vals = (selector: (s: Mathaven) => number): number[] => extractValues(calc.history, selector)

const impulse = vals(h => h.P).map((_,i)=>i)
const data = [
createTrace(impulse, vals(h=>h.s), 's', color(0)),
createTrace(impulse, vals(h=>h.phi), 'phi', color(1)),
createTrace(impulse, vals(h=>h.sPrime), 'sPrime', color(2)),
createTrace(impulse, vals(h=>h.phiPrime), 'phiPrime', color(3)),
createTrace(impulse, vals(h=>h.φ), 'φ', color(1)),
createTrace(impulse, vals(h=>h.), "s'", color(2)),
createTrace(impulse, vals(h=>h.φʹ), "φʹ", color(3)),
];

window.Plotly.newPlot("mathaven-div", data, layout, config)
Expand Down
2 changes: 2 additions & 0 deletions src/model/physics/claude/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ export const μw = 0.14

// Fixed angle of cushion contact point above ball center
export const sinTheta = 2 / 5
export const sinθ = sinTheta

// Fixed angle of cushion contact point above ball center
export const cosTheta = Math.sqrt(21) / 5
export const cosθ = cosTheta

// Number of iterations
export const N = 1000
Expand Down
90 changes: 90 additions & 0 deletions src/model/physics/claude/qwen.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { M, R, ee, μs, μw, sinθ, cosθ, N } from "./constants"

export class Mathaven {
P: number = 0;
WzI: number = 0;

// centroid velocity
vx: number;
vy: number;

// angular velocity
ωx: number;
ωy: number;
ωz: number;

// slip speed and angles at I and C
s: number;
φ: number;
: number;
φʹ: number;

readonly history: Array<Partial<Mathaven>> = [];

constructor(v0: number, α: number, ω0S: number, ω0T: number) {
this.vx = v0 * Math.cos(α);
this.vy = v0 * Math.sin(α);
this.ωx = -ω0T * Math.sin(α);
this.ωy = ω0T * Math.cos(α);
this.ωz = ω0S;
this.updateSlipSpeedsAndAngles();
}

private updateSlipSpeedsAndAngles(): void {
this.s = Math.sqrt(
Math.pow(this.vx + R * (this.ωy * sinθ - this.ωz * cosθ), 2) +
Math.pow(-this.vy * sinθ - R * this.ωx, 2)
);
this.φ = Math.atan2(-this.vy * sinθ - R * this.ωx, this.vx + R * (this.ωy * sinθ - this.ωz * cosθ));
this. = Math.abs(this.vx - R * this.ωy);
this.φʹ = this.vx - R * this.ωy > 0 ? this.φ : Math.PI + this.φ;
}

public compressionPhase(): void {
const ΔP = ((1 + ee) * M * this.vy) / N;
while (this.vy > 0) {
this.updateSingleStep(ΔP);
}
}

public restitutionPhase(targetWorkRebound: number): void {
const ΔP = ((1 + ee) * M * this.vy) / N;
while (this.WzI < targetWorkRebound) {
this.updateSingleStep(ΔP);
}
}

private iter;
private updateSingleStep(ΔP: number): void {
this.updateVelocity(ΔP);
this.updateAngularVelocity(ΔP);
this.updateWorkDone(ΔP);
this.history.push({ ...this });
if (this.iter++ > 2000) {
throw "Solution not found"
}
}

private updateVelocity(ΔP: number): void {
this.vx -= (1 / M) * (μw * Math.cos(this.φ) + μs * Math.cos(this.φʹ) * (sinθ + μw * Math.sin(this.φ) * cosθ)) * ΔP;
this.vy -= (1 / M) * (cosθ - μw * sinθ * Math.sin(this.φ) + μs * Math.sin(this.φʹ) * (sinθ + μw * Math.sin(this.φ) * cosθ)) * ΔP;
}

private updateAngularVelocity(ΔP: number): void {
this.ωx += -(5 / (2 * M * R)) * (μw * Math.sin(this.φ) + μs * Math.sin(this.φʹ) * (sinθ + μw * Math.sin(this.φ) * cosθ)) * ΔP;
this.ωy += -(5 / (2 * M * R)) * (μw * Math.cos(this.φ) * sinθ - μs * Math.cos(this.φʹ) * (sinθ + μw * Math.sin(this.φ) * cosθ)) * ΔP;
this.ωz += (5 / (2 * M * R)) * (μw * Math.cos(this.φ) * cosθ) * ΔP;
}

private updateWorkDone(ΔP: number): void {
const ΔWzI = ΔP / 2 * (this.vy + this.vy);
this.WzI += ΔWzI;
}

public solve(): void {
this.compressionPhase();
const targetWorkRebound = (1 - ee * ee) * this.WzI;
this.restitutionPhase(targetWorkRebound);
}
}

0 comments on commit 342e66e

Please sign in to comment.