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
12 changes: 12 additions & 0 deletions biome-plugins/no-math-round.grit
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Flags Math.round(). Prefer roundSymmetric from scenerystack/dot so positive and
// negative values round the same way (see phetsims/dot#35).
// Suppress a deliberate use with: // biome-ignore lint/plugin/no-math-round: <reason>
language js

`Math.round($value)` as $call where {
register_diagnostic(
span = $call,
message = "Use roundSymmetric(value) from scenerystack/dot instead of Math.round(), which rounds positive and negative numbers differently.",
severity = "warn"
)
}
18 changes: 18 additions & 0 deletions biome-plugins/no-native-timer.grit
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Flags bare setTimeout / setInterval (and window.* forms). Sim code should step
// from the model/view step(dt) chain, or use stepTimer from scenerystack/axon when a
// delayed callback is truly needed. Method calls like stepTimer.setTimeout are allowed.
// Suppress a deliberate use with: // biome-ignore lint/plugin/no-native-timer: <reason>
language js

or {
`setTimeout($...)`,
`setInterval($...)`,
`window.setTimeout($...)`,
`window.setInterval($...)`
} as $call where {
register_diagnostic(
span = $call,
message = "Do not call setTimeout/setInterval in sim code; step from model/view step(dt), or use stepTimer from scenerystack/axon for a delayed callback.",
severity = "warn"
)
}
13 changes: 13 additions & 0 deletions biome-plugins/no-window-inner-size.grit
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Flags window.innerWidth / window.innerHeight. Prefer Sim.dimensionProperty (or the
// screen's layoutBounds / visibleBounds) so layout tracks the sim display, not the
// browser chrome.
// Suppress a deliberate use with: // biome-ignore lint/plugin/no-window-inner-size: <reason>
language js

or { `window.innerWidth`, `window.innerHeight` } as $hit where {
register_diagnostic(
span = $hit,
message = "Use Sim.dimensionProperty (or layoutBounds/visibleBounds) instead of window.innerWidth/innerHeight.",
severity = "warn"
)
}
35 changes: 35 additions & 0 deletions biome-plugins/require-property-suffix.grit
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Flags axon Property instances whose binding name does not end in "Property".
// Covers same-line const/let/var and this.field for the common *Property constructors.
// Class fields (`public readonly foo = new NumberProperty(...)`, bare `foo = ...`) are
// not reliably matched by Biome GritQL — rename those in review or with a codemod.
// Type-only annotations are out of scope (PhET's rule is type-checker based).
// Suppress a deliberate use with: // biome-ignore lint/plugin/require-property-suffix: <reason>
// GritQL regexes here must use non-capturing groups (?:...); a plain (...) silently never matches.
language js

or {
`const $name = new $ctor($...)`,
`let $name = new $ctor($...)`,
`var $name = new $ctor($...)`,
`this.$name = new $ctor($...)`
} as $stmt where {
$ctor <: or {
`Property`,
`BooleanProperty`,
`NumberProperty`,
`StringProperty`,
`DerivedProperty`,
`DerivedStringProperty`,
`DynamicProperty`,
`TinyProperty`,
`EnumerationProperty`,
`StringUnionProperty`,
`ProfileColorProperty`
},
not $name <: r"(?s)(?:.*(?:Property|PROPERTY)|property|_property)$",
register_diagnostic(
span = $name,
message = "Axon Property bindings must end in \"Property\" (e.g. massProperty, not mass).",
severity = "warn"
)
}
10 changes: 9 additions & 1 deletion biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"assist": {
"actions": {
"source": {
"organizeImports": "on"
"organizeImports": "on",
"useSortedPackageJson": "on"
}
}
},
Expand Down Expand Up @@ -82,7 +83,9 @@
"style": {
"noCommonJs": "error",
"noDefaultExport": "off",
"noEnum": "error",
"noExportedImports": "warn",
"noNamespace": "error",
"noParameterAssign": "error",
"preset": "recommended",
"useBlockStatements": "error",
Expand All @@ -98,6 +101,7 @@
"strictCase": false
}
},
"useAsConstAssertion": "warn",
"useForOf": "warn",
"useImportType": "error",
"useNamingConvention": {
Expand Down Expand Up @@ -170,6 +174,10 @@
"plugins": [
"./biome-plugins/no-native-tofixed.grit",
"./biome-plugins/no-math-random.grit",
"./biome-plugins/no-math-round.grit",
"./biome-plugins/no-native-timer.grit",
"./biome-plugins/no-window-inner-size.grit",
"./biome-plugins/require-property-suffix.grit",
"./biome-plugins/no-hardcoded-text.grit",
{
"path": "./biome-plugins/no-inline-color.grit",
Expand Down
57 changes: 29 additions & 28 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
{
"name": "equilibrium-lab",
"private": true,
"version": "0.0.0",
"type": "module",
"private": true,
"description": "Chemical equilibrium for General Chemistry: watch a mixture react until Q reaches K with the forward and reverse rates equal, work ICE tables against a live simulation, apply Le Chatelier stresses and see whether Q or K moved, and find ΔH° and ΔS° from a van 't Hoff plot.",
"license": "AGPL-3.0-or-later",
"repository": {
"type": "git",
"url": "https://github.com/OpenLyceum/EquilibriumLab.git"
},
"keywords": [
"simulation",
"SceneryStack",
Expand All @@ -26,33 +20,45 @@
"education",
"pwa"
],
"homepage": "https://openlyceum.github.io/EquilibriumLab",
"repository": {
"type": "git",
"url": "https://github.com/OpenLyceum/EquilibriumLab.git"
},
"license": "AGPL-3.0-or-later",
"type": "module",
"scripts": {
"start": "vite",
"dev": "vite",
"build": "tsc && vite build",
"build:single": "tsc && vite build --mode single",
"preview": "vite preview",
"lint": "biome check --error-on-warnings .",
"format": "biome format --write .",
"fix": "biome check --write .",
"check": "tsc --noEmit && tsc -p tsconfig.scripts.json --noEmit && tsc -p tsconfig.test.json --noEmit",
"check:unused": "knip",
"check:architecture": "depcruise src",
"check:unused": "knip",
"clean": "rm -rf dist",
"dev": "vite",
"fix": "biome check --write .",
"format": "biome format --write .",
"icons": "node scripts/generate-icons.ts",
"knip": "knip",
"lint": "biome check --error-on-warnings .",
"prepare": "test \"$(git rev-parse --show-toplevel 2>/dev/null)\" = \"$PWD\" && git config core.hooksPath .githooks || true",
"preview": "vite preview",
"release": "npm run check && npm run lint && npm run check:architecture && npm run build && npm test && npm version patch && git push && git push --tags",
"watch": "tsc --watch",
"start": "vite",
"test": "vitest run",
"test:watch": "vitest",
"test:architecture": "node --test tests/*.node-test.ts",
"test:fuzz": "node scripts/test-fuzz.ts",
"test:fuzz:quick": "node scripts/test-fuzz.ts 10",
"test:fuzz:long": "node scripts/test-fuzz.ts 300",
"test:fuzz:quick": "node scripts/test-fuzz.ts 10",
"test:soak": "npm run test:soak:headless && npm run test:soak:browser",
"test:soak:headless": "vitest run --config vitest.soak.config.ts",
"test:soak:browser": "npx playwright test tests/soak/browser.spec.ts",
"icons": "node scripts/generate-icons.ts",
"clean": "rm -rf dist",
"prepare": "test \"$(git rev-parse --show-toplevel 2>/dev/null)\" = \"$PWD\" && git config core.hooksPath .githooks || true",
"knip": "knip",
"test:architecture": "node --test tests/*.node-test.ts"
"test:soak:headless": "vitest run --config vitest.soak.config.ts",
"test:watch": "vitest",
"watch": "tsc --watch"
},
"overrides": {
"brace-expansion": "~5.0.12",
"lodash": "~4.18.1",
"three": "~0.125.2"
},
"dependencies": {
"scenerystack": "^3.0.0"
Expand All @@ -75,11 +81,6 @@
"engines": {
"node": ">=26"
},
"overrides": {
"lodash": "~4.18.1",
"three": "~0.125.2",
"brace-expansion": "~5.0.12"
},
"devEngines": {
"runtime": {
"name": "node",
Expand Down
10 changes: 5 additions & 5 deletions src/common/model/ParticleSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { PARTICLE_SYSTEM_SETTINGS } from "../../EquilibriumLabConstants.js";
* height is proportional to the volume, so compressing the gas squeezes the particles together.
*/

import { Random } from "scenerystack/dot";
import { Random, roundSymmetric } from "scenerystack/dot";
import {
BOX_MAX_HEIGHT,
BOX_WIDTH,
Expand Down Expand Up @@ -123,7 +123,7 @@ export class ParticleSystem {
const ppm = this.reaction.definition.particlesPerMole;
this.reaction.species.forEach((s, i) => {
if (!s.bulk) {
const count = Math.round(this.mixture.getAmount(i) * ppm);
const count = roundSymmetric(this.mixture.getAmount(i) * ppm);
for (let k = 0; k < count && this.particles.length < MAX_PARTICLES; k++) {
this.particles.push(
this.createParticle(i, this.random.nextDouble() * BOX_WIDTH, this.random.nextDouble() * this.height, 1),
Expand All @@ -134,7 +134,7 @@ export class ParticleSystem {
const flows = this.mixture.getFlows();
this.forwardDone = Math.floor(flows.forward * ppm);
this.reverseDone = Math.floor(flows.reverse * ppm);
this.netDone = Math.round(this.mixture.getTotalExtent() * ppm);
this.netDone = roundSymmetric(this.mixture.getTotalExtent() * ppm);
}

/** Replays the mixture's reaction since the last step, then moves the particles. */
Expand Down Expand Up @@ -195,7 +195,7 @@ export class ParticleSystem {

// Keep the composition matched to the net extent (equilibrate() and dropped backlogs move it
// without gross flows).
const netTarget = Math.round(this.mixture.getTotalExtent() * ppm);
const netTarget = roundSymmetric(this.mixture.getTotalExtent() * ppm);
for (let i = 0; i < MAX_EVENTS_PER_STEP && this.netDone !== netTarget; i++) {
const forward = netTarget > this.netDone;
if (!this.react(forward)) {
Expand Down Expand Up @@ -251,7 +251,7 @@ export class ParticleSystem {
if (this.reaction.species[index]?.bulk) {
return;
}
const count = Math.round(Math.abs(moles) * this.reaction.definition.particlesPerMole);
const count = roundSymmetric(Math.abs(moles) * this.reaction.definition.particlesPerMole);
if (moles > 0) {
for (let k = 0; k < count && this.particles.length < MAX_PARTICLES; k++) {
// Added material enters at the top of the box.
Expand Down
6 changes: 5 additions & 1 deletion src/common/model/Reaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import type { Range } from "scenerystack/dot";
import { GAS_CONSTANT, GAS_CONSTANT_L_BAR, STANDARD_TEMPERATURE } from "../../EquilibriumLabConstants.js";

import type stringsEn from "../../i18n/strings_en.json";

export type Phase = "gas" | "aqueous";

/** The family a reaction belongs to, which groups it in the reaction menus. */
Expand All @@ -36,9 +38,11 @@ export type ReactionId =
| "genericCombination"
| "genericDimer";

export type SpeciesId = keyof (typeof stringsEn)["common"]["speciesNames"];

export type Species = {
/** Identifier used for colours, particle sprites and string lookups (e.g. "NO2"). */
readonly id: string;
readonly id: SpeciesId;
/** Formula as RichText markup, without phase or coefficient (e.g. "NO<sub>2</sub>"). */
readonly formula: string;
/** Signed stoichiometric coefficient: negative for reactants, positive for products. */
Expand Down
12 changes: 9 additions & 3 deletions src/common/view/ParticleBoxNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { PARTICLE_BOX_NODE_SETTINGS } from "../../EquilibriumLabConstants.js";
*/

import { Multilink, type TReadOnlyProperty } from "scenerystack/axon";
import { Bounds2, Random } from "scenerystack/dot";
import { Bounds2, Random, roundSymmetric } from "scenerystack/dot";
import { Shape } from "scenerystack/kite";
import { optionize } from "scenerystack/phet-core";
import {
Expand Down Expand Up @@ -297,7 +297,10 @@ class ParticleCanvasNode extends CanvasNode {
};
reaction.species.forEach((s, i) => {
if (s.bulk) {
const count = Math.min(MAX_BULK_DOTS, Math.round((concentrations[i] ?? 0) * 30 * (height / this.boxMaxHeight)));
const count = Math.min(
MAX_BULK_DOTS,
roundSymmetric((concentrations[i] ?? 0) * 30 * (height / this.boxMaxHeight)),
);
context.fillStyle = speciesStyleIn(reaction, i).color.value.withAlpha(0.55).toCSS();
for (let k = 0; k < count; k++) {
const fraction = this.scatter[k];
Expand All @@ -307,7 +310,10 @@ class ParticleCanvasNode extends CanvasNode {
}
}
});
const inert = Math.min(INERT_DOTS_CAP, Math.round(mixture.getInertAmount() * reaction.definition.particlesPerMole));
const inert = Math.min(
INERT_DOTS_CAP,
roundSymmetric(mixture.getInertAmount() * reaction.definition.particlesPerMole),
);
context.fillStyle = EquilibriumLabColors.inertGasColorProperty.value.toCSS();
for (let k = 0; k < inert; k++) {
const fraction = this.scatter[this.scatter.length - 1 - k];
Expand Down
3 changes: 2 additions & 1 deletion src/common/view/QKGaugeNode.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { roundSymmetric } from "scenerystack/dot";
import { QK_GAUGE_NODE_SETTINGS } from "../../EquilibriumLabConstants.js";
/**
* QKGaugeNode.ts
Expand Down Expand Up @@ -134,7 +135,7 @@ export class QKGaugeNode extends Node {
const lnQ = kp ? mixture.lnQp : mixture.lnQc;
const symbols = kp ? strings.kpSymbols : strings.kcSymbols;
const log10K = lnK / Math.LN10;
const centerDecade = Math.round(log10K);
const centerDecade = roundSymmetric(log10K);
drawTicks(centerDecade);
const toX = (log10: number) => ((log10 - centerDecade + HALF_SPAN_DECADES) / (2 * HALF_SPAN_DECADES)) * width;

Expand Down
13 changes: 3 additions & 10 deletions src/common/view/reactionChoices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,16 @@

import type { TReadOnlyProperty } from "scenerystack/axon";
import { StringManager } from "../../i18n/StringManager.js";
import type { ReactionId } from "../model/Reaction.js";
import type { ReactionId, SpeciesId } from "../model/Reaction.js";
import type { ComboChoice } from "./ControlFactory.js";

export function reactionChoices(ids: readonly ReactionId[]): ComboChoice<ReactionId>[] {
const names = StringManager.getInstance().getCommonStrings().reactionNames;
return ids.map((id) => ({ value: id, label: names[`${id}StringProperty`] }));
}

type SpeciesNames = ReturnType<StringManager["getCommonStrings"]>["speciesNames"];

/** The spoken name of a species, e.g. "nitrogen dioxide" for NO2. */
export function speciesName(id: string): TReadOnlyProperty<string> {
export function speciesName(id: SpeciesId): TReadOnlyProperty<string> {
const names = StringManager.getInstance().getCommonStrings().speciesNames;
const key = `${id}StringProperty` as keyof SpeciesNames;
const name = names[key];
if (!name) {
throw new Error(`No name for species ${id}`);
}
return name;
return names[`${id}StringProperty`];
}
Loading
Loading