@rangefoundry/catalog 0.0.0 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,399 @@
1
+ /** Pressures are printed rounded, to 10 bar on some sheets: allow 5 bar or 1 %, whichever is larger. */
2
+ function near(actual, expected) {
3
+ return Math.abs(actual - expected) <= Math.max(5, 0.01 * expected);
4
+ }
5
+ const diameterSymbols = ['P1', 'P2', 'R1', 'H1', 'H2'];
6
+ const lengthOrder = ['L1', 'L2', 'L3', 'L6'];
7
+ function findSpecValue(values, side, symbol, variant) {
8
+ return values.find((v) => v.side === side && v.symbol === symbol && v.variant === variant)?.value;
9
+ }
10
+ function roundToTenth(x) {
11
+ return Math.round(x * 10) / 10;
12
+ }
13
+ function suffix(variant) {
14
+ return variant === undefined ? '' : `@${variant}`;
15
+ }
16
+ /** PK is 1.15 times Pmax, PE 1.25 or 1.30 times Pmax. */
17
+ function checkPressureRatios(values, variant) {
18
+ const findings = [];
19
+ const at = suffix(variant);
20
+ const pmax = findSpecValue(values, undefined, 'Pmax', variant);
21
+ const pk = findSpecValue(values, undefined, 'PK', variant);
22
+ const pe = findSpecValue(values, undefined, 'PE', variant);
23
+ if (pmax !== undefined && pk !== undefined && !near(pk, 1.15 * pmax)) {
24
+ findings.push({
25
+ finding: `pk-ratio${at}`,
26
+ message: `PK = ${pk} bar is not 1.15 × Pmax = ${roundToTenth(1.15 * pmax)} bar`,
27
+ });
28
+ }
29
+ if (pmax !== undefined && pe !== undefined && !near(pe, 1.25 * pmax) && !near(pe, 1.3 * pmax)) {
30
+ findings.push({
31
+ finding: `pe-ratio${at}`,
32
+ message: `PE = ${pe} bar is neither 1.25 nor 1.30 × Pmax = ${pmax} bar`,
33
+ });
34
+ }
35
+ return findings;
36
+ }
37
+ /** No cartridge diameter exceeds the chamber's. */
38
+ function checkDiameterFit(values, variant) {
39
+ const findings = [];
40
+ for (const symbol of diameterSymbols) {
41
+ const cartridge = findSpecValue(values, 'cartridge', symbol, variant);
42
+ const chamber = findSpecValue(values, 'chamber', symbol, variant);
43
+ if (cartridge !== undefined && chamber !== undefined && cartridge > chamber) {
44
+ findings.push({
45
+ finding: `diameter-fit:${symbol}${suffix(variant)}`,
46
+ message: `cartridge ${symbol} = ${cartridge} mm exceeds chamber ${symbol} = ${chamber} mm`,
47
+ });
48
+ }
49
+ }
50
+ return findings;
51
+ }
52
+ /** Cartridge lengths rise from L1 to L6 (L6 may equal L3). */
53
+ function checkLengthOrder(values, variant) {
54
+ const findings = [];
55
+ const lengths = lengthOrder
56
+ .map((symbol) => ({ symbol, value: findSpecValue(values, 'cartridge', symbol, variant) }))
57
+ .filter((l) => l.value !== undefined);
58
+ for (const [i, b] of lengths.entries()) {
59
+ const a = lengths[i - 1];
60
+ if (a === undefined)
61
+ continue;
62
+ const ordered = b.symbol === 'L6' ? a.value <= b.value : a.value < b.value;
63
+ if (!ordered) {
64
+ findings.push({
65
+ finding: `length-order:${a.symbol}-${b.symbol}${suffix(variant)}`,
66
+ message: `cartridge ${a.symbol} = ${a.value} mm is not below ${b.symbol} = ${b.value} mm`,
67
+ });
68
+ }
69
+ }
70
+ return findings;
71
+ }
72
+ /** The case length L3 does not exceed the chamber's. */
73
+ function checkCaseFitsChamber(values, variant) {
74
+ const caseLength = findSpecValue(values, 'cartridge', 'L3', variant);
75
+ const chamberLength = findSpecValue(values, 'chamber', 'L3', variant);
76
+ if (caseLength !== undefined && chamberLength !== undefined && caseLength > chamberLength) {
77
+ return [
78
+ {
79
+ finding: `case-fits-chamber${suffix(variant)}`,
80
+ message: `cartridge L3 = ${caseLength} mm exceeds chamber L3 = ${chamberLength} mm`,
81
+ },
82
+ ];
83
+ }
84
+ return [];
85
+ }
86
+ /** Checks the values of one sheet; every finding must be fixed or acknowledged in `exceptions`. */
87
+ export function checkSpec(spec) {
88
+ const variants = [...new Set(spec.values.map((v) => v.variant))];
89
+ return variants.flatMap((variant) => [
90
+ ...checkPressureRatios(spec.values, variant),
91
+ ...checkDiameterFit(spec.values, variant),
92
+ ...checkLengthOrder(spec.values, variant),
93
+ ...checkCaseFitsChamber(spec.values, variant),
94
+ ]);
95
+ }
96
+ /** Twist per turn in mm, 1:4 to about 1:70 in: pistol and muzzleloader bullets list twists such as 1:48. */
97
+ export const twistRange = Object.freeze({
98
+ min: 100,
99
+ max: 1800,
100
+ });
101
+ /** A finite twist inside the range; NaN is not a misprint but a value that was not read. */
102
+ export const twistPlausible = (mm) => Number.isFinite(mm) && mm >= twistRange.min && mm <= twistRange.max;
103
+ /** Diameter and mass within small-arms ranges. */
104
+ function checkBulletSize(bullet) {
105
+ const findings = [];
106
+ const diameter = bullet.diameter?.value;
107
+ const mass = bullet.mass.value;
108
+ if (diameter !== undefined && (diameter < 4 || diameter > 20)) {
109
+ findings.push({
110
+ finding: 'diameter-range',
111
+ message: `diameter ${diameter} mm is outside 4 to 20 mm`,
112
+ });
113
+ }
114
+ // Up to 70 g: .50 BMG match bullets reach about 1000 gr (65 g).
115
+ if (mass < 1 || mass > 70) {
116
+ findings.push({ finding: 'mass-range', message: `mass ${mass} g is outside 1 to 70 g` });
117
+ }
118
+ return findings;
119
+ }
120
+ /** Each coefficient within the range of its drag model. */
121
+ function checkCoefficientRanges(ballisticCoefficients) {
122
+ return ballisticCoefficients.flatMap(({ dragModel, value }) => {
123
+ const [low, high] = dragModel === 'G1' ? [0.05, 1.2] : [0.03, 0.65];
124
+ if (!(value < low || value > high))
125
+ return [];
126
+ return [
127
+ {
128
+ finding: `ballistic-coefficient-range:${dragModel}`,
129
+ message: `${dragModel} BC ${value} is outside ${low} to ${high}`,
130
+ },
131
+ ];
132
+ });
133
+ }
134
+ /** The G7 is 0.43 to 0.55 times the G1 for every pair in the records; outside 0.35 to 0.65 one of the two is wrong. */
135
+ function checkG7G1Ratio(ballisticCoefficients) {
136
+ const g1Coefficients = ballisticCoefficients.filter((b) => b.dragModel === 'G1' && !b.minVelocity);
137
+ const g7Coefficients = ballisticCoefficients.filter((b) => b.dragModel === 'G7' && !b.minVelocity);
138
+ const [g1Coefficient, ...moreG1] = g1Coefficients;
139
+ const [g7Coefficient, ...moreG7] = g7Coefficients;
140
+ if (!g1Coefficient || !g7Coefficient || moreG1.length > 0 || moreG7.length > 0)
141
+ return [];
142
+ const [g1, g7] = [g1Coefficient.value, g7Coefficient.value];
143
+ const ratio = g7 / g1;
144
+ if (!(ratio < 0.35 || ratio > 0.65))
145
+ return [];
146
+ return [
147
+ {
148
+ finding: 'g7-g1-ratio',
149
+ message: `G7 ${g7} is ${ratio.toFixed(2)} times G1 ${g1}, outside 0.35 to 0.65`,
150
+ },
151
+ ];
152
+ }
153
+ /** Each coefficient within the range of its drag model, and the G7 to G1 ratio of one pair. */
154
+ function checkBallisticCoefficients(bullet) {
155
+ const ballisticCoefficients = bullet.ballisticCoefficients ?? [];
156
+ return [
157
+ ...checkCoefficientRanges(ballisticCoefficients),
158
+ ...checkG7G1Ratio(ballisticCoefficients),
159
+ ];
160
+ }
161
+ /** Length between 1 and 7 diameters. */
162
+ function checkBulletLength(bullet) {
163
+ if (!bullet.length || !bullet.diameter)
164
+ return [];
165
+ const ratio = bullet.length.value / bullet.diameter.value;
166
+ if (!(ratio < 1 || ratio > 7))
167
+ return [];
168
+ return [
169
+ {
170
+ finding: 'length-ratio',
171
+ message: `length ${bullet.length.value} mm is ${roundToTenth(ratio)} diameters`,
172
+ },
173
+ ];
174
+ }
175
+ /** Recommended and slowest twist within {@link twistRange}. */
176
+ function checkBulletTwist(bullet) {
177
+ const findings = [];
178
+ for (const twist of [bullet.recommendedTwist?.value, bullet.slowestTwist?.value]) {
179
+ if (twist !== undefined && !twistPlausible(twist)) {
180
+ findings.push({
181
+ finding: 'twist-range',
182
+ message: `twist ${twist} mm per turn is outside ${twistRange.min} to ${twistRange.max} mm`,
183
+ });
184
+ }
185
+ }
186
+ return findings;
187
+ }
188
+ /**
189
+ * Checks one bullet record against physical ranges: small-arms diameters and masses, BCs, length
190
+ * against diameter, twist. A finding means a misread or mistyped value; it must be fixed.
191
+ */
192
+ export function checkBullet(bullet) {
193
+ return [
194
+ ...checkBulletSize(bullet),
195
+ ...checkBallisticCoefficients(bullet),
196
+ ...checkBulletLength(bullet),
197
+ ...checkBulletTwist(bullet),
198
+ ];
199
+ }
200
+ /**
201
+ * Checks one capacity: positive, and at most the water filling the cylinder that encloses the case
202
+ * (the larger of cartridge P1 and R1, times L3, of `spec`, the cartridge's sheet) at
203
+ * 1 g/cm3, the highest density of water.
204
+ */
205
+ export function checkCapacity(capacity, spec) {
206
+ const w = capacity.waterCapacity.value;
207
+ const values = spec?.values ?? [];
208
+ const diameters = ['P1', 'R1']
209
+ .map((symbol) => findSpecValue(values, 'cartridge', symbol))
210
+ .filter((d) => d !== undefined);
211
+ const length = findSpecValue(values, 'cartridge', 'L3');
212
+ // mm3 to cm3 at 1 g/cm3: grams.
213
+ const max = diameters.length > 0 && length !== undefined
214
+ ? ((Math.PI / 4) * Math.max(...diameters) ** 2 * length) / 1000
215
+ : Infinity;
216
+ if (w > 0 && w <= max)
217
+ return [];
218
+ return [
219
+ {
220
+ finding: 'capacity-range',
221
+ message: `water capacity ${w} g is outside 0 to ${roundToTenth(max)} g (cylinder enclosing the case)`,
222
+ },
223
+ ];
224
+ }
225
+ /** Bounds of a case product: flash hole diameter in mm and empty case mass in g, wide enough for every cartridge of the catalogue. */
226
+ const caseRanges = {
227
+ flashHole: { min: 0.3, max: 5, unit: 'mm' },
228
+ mass: { min: 0.1, max: 400, unit: 'g' },
229
+ };
230
+ /**
231
+ * Checks one case: the flash hole and the empty mass inside small-arms ranges, and the stated water
232
+ * capacity as {@link checkCapacity} checks a measured one against the cartridge's sheet.
233
+ */
234
+ export function checkCase(record, spec) {
235
+ const out = [];
236
+ for (const key of ['flashHole', 'mass']) {
237
+ const v = record[key]?.value;
238
+ const { min, max, unit } = caseRanges[key];
239
+ if (v !== undefined && !(v >= min && v <= max))
240
+ out.push({
241
+ finding: `${key}-range`,
242
+ message: `${key === 'mass' ? 'mass' : 'flash hole'} ${v} ${unit} is outside ${min} to ${max} ${unit}`,
243
+ });
244
+ }
245
+ if (record.waterCapacity)
246
+ out.push(...checkCapacity({ waterCapacity: record.waterCapacity }, spec));
247
+ return out;
248
+ }
249
+ /** Universal gas constant, J/(kmol K) (CODATA 2018, exact). */
250
+ const universalGasConstant = 8314.462618;
251
+ /** Below the solid density: safety data sheets print 1.5 and 1.6 g/cm3, rounded to 0.1. */
252
+ const bulkDensityMax = 1.65;
253
+ /** Temperature bracket of the thermochemistry solver, K. */
254
+ const flameTemperatureRange = Object.freeze({ min: 300, max: 6000 });
255
+ /** Molar mass of H, the lightest product species, kg/kmol. */
256
+ const molarMassMin = 1.008;
257
+ /** Bulk density below the solid density. */
258
+ function checkBulkDensity(powder) {
259
+ const bulkDensity = powder.bulkDensity?.value;
260
+ if (bulkDensity === undefined || (bulkDensity > 0 && bulkDensity < bulkDensityMax))
261
+ return [];
262
+ return [
263
+ {
264
+ finding: 'bulk-density-range',
265
+ message: `bulk density ${bulkDensity} g/cm3 is outside 0 to ${bulkDensityMax} g/cm3`,
266
+ },
267
+ ];
268
+ }
269
+ /** Loading density below the solid density, flame temperature inside the solver's bracket, molar mass above the lightest species, impetus against the ideal gas, gamma and covolume positive. */
270
+ function checkThermochemistry(thermochemistry) {
271
+ const findings = [];
272
+ const load = thermochemistry.loadingDensity.value;
273
+ if (!(load > 0 && load < bulkDensityMax)) {
274
+ findings.push({
275
+ finding: 'loading-density-range',
276
+ message: `loading density ${load} g/cm3 is outside 0 to ${bulkDensityMax} g/cm3`,
277
+ });
278
+ }
279
+ const { min, max } = thermochemistry.flameTemperature;
280
+ if (!(min >= flameTemperatureRange.min && max <= flameTemperatureRange.max)) {
281
+ findings.push({
282
+ finding: 'flame-temperature-range',
283
+ message: `flame temperature ${min} to ${max} K is outside ${flameTemperatureRange.min} to ${flameTemperatureRange.max} K`,
284
+ });
285
+ }
286
+ if (!(thermochemistry.molarMass.min > molarMassMin)) {
287
+ findings.push({
288
+ finding: 'molar-mass-range',
289
+ message: `molar mass ${thermochemistry.molarMass.min} kg/kmol is not above ${molarMassMin} kg/kmol (H)`,
290
+ });
291
+ }
292
+ const ideal = (universalGasConstant * thermochemistry.flameTemperature.value) /
293
+ thermochemistry.molarMass.value;
294
+ if (!(Math.abs(thermochemistry.impetus.value - ideal) <= 0.005 * ideal)) {
295
+ findings.push({
296
+ finding: 'impetus-ideal-gas',
297
+ message: `impetus ${thermochemistry.impetus.value} J/kg is more than 0.5 % off R0 T / M = ${Math.round(ideal)} J/kg`,
298
+ });
299
+ }
300
+ if (!(thermochemistry.gamma.min > 1)) {
301
+ findings.push({
302
+ finding: 'gamma-range',
303
+ message: `gamma ${thermochemistry.gamma.min} is not above 1`,
304
+ });
305
+ }
306
+ if (!(thermochemistry.covolume.min > 0)) {
307
+ findings.push({
308
+ finding: 'covolume-range',
309
+ message: `covolume ${thermochemistry.covolume.min} m3/kg is not positive`,
310
+ });
311
+ }
312
+ return findings;
313
+ }
314
+ /**
315
+ * Checks one powder: bulk and loading density below the solid density, thermochemistry inside the
316
+ * solver's temperature bracket, above the lightest species and against the ideal gas.
317
+ */
318
+ export function checkPowder(powder) {
319
+ return [
320
+ ...checkBulkDensity(powder),
321
+ ...(powder.thermochemistry ? checkThermochemistry(powder.thermochemistry) : []),
322
+ ];
323
+ }
324
+ /** World air temperature extremes, degC (WMO 2025). */
325
+ export const airTemperatureRange = Object.freeze({
326
+ min: -89.2,
327
+ max: 56.7,
328
+ });
329
+ /** Station pressure, bar: the sea-level extremes of WMO 2025 (870 and 1089.1 hPa) carried to 9000 m and to -500 m in an isothermal column at the coldest air temperature give 0.1635 and 1.1951 bar. */
330
+ const stationPressureRange = Object.freeze({ min: 0.16, max: 1.2 });
331
+ /** Propellant temperature not below the coldest air recorded, every velocity positive. */
332
+ function checkShotStrings(strings) {
333
+ const findings = [];
334
+ strings.forEach((string, i) => {
335
+ // Powder takes the temperature of its surroundings: not below the coldest air recorded.
336
+ const propellantTemperature = string.propellantTemperature?.value;
337
+ if (propellantTemperature !== undefined &&
338
+ !(propellantTemperature >= airTemperatureRange.min)) {
339
+ findings.push({
340
+ finding: 'propellant-temperature-range',
341
+ message: `string ${i + 1}: propellant temperature ${propellantTemperature} degC is below ${airTemperatureRange.min} degC`,
342
+ });
343
+ }
344
+ string.shots.forEach((shot, k) => {
345
+ if (!(shot.velocity.value > 0)) {
346
+ findings.push({
347
+ finding: 'velocity-range',
348
+ message: `string ${i + 1} shot ${k + 1}: velocity ${shot.velocity.value} m/s is not positive`,
349
+ });
350
+ }
351
+ });
352
+ });
353
+ return findings;
354
+ }
355
+ /** Relative humidity within 0 to 100 %. */
356
+ function checkHumidity(conditions) {
357
+ const relativeHumidity = conditions.relativeHumidity?.value;
358
+ if (relativeHumidity === undefined || (relativeHumidity >= 0 && relativeHumidity <= 100))
359
+ return [];
360
+ return [
361
+ {
362
+ finding: 'humidity-range',
363
+ message: `relative humidity ${relativeHumidity} % is outside 0 to 100 %`,
364
+ },
365
+ ];
366
+ }
367
+ /** Humidity, air temperature and station pressure within their physical and recorded ranges. */
368
+ function checkConditions(conditions) {
369
+ const findings = checkHumidity(conditions);
370
+ const air = conditions.airTemperature.value;
371
+ if (!(air >= airTemperatureRange.min && air <= airTemperatureRange.max)) {
372
+ findings.push({
373
+ finding: 'air-temperature-range',
374
+ message: `air temperature ${air} degC is outside ${airTemperatureRange.min} to ${airTemperatureRange.max} degC`,
375
+ });
376
+ }
377
+ const stationPressure = conditions.stationPressure?.value;
378
+ if (stationPressure !== undefined &&
379
+ !(stationPressure >= stationPressureRange.min && stationPressure <= stationPressureRange.max)) {
380
+ findings.push({
381
+ finding: 'pressure-range',
382
+ message: `station pressure ${stationPressure} bar is outside ${stationPressureRange.min} to ${stationPressureRange.max} bar`,
383
+ });
384
+ }
385
+ return findings;
386
+ }
387
+ /** Checks one chronograph session against physical limits and the world temperature extremes. */
388
+ export function checkChronoSession(session) {
389
+ const findings = [...checkShotStrings(session.strings), ...checkConditions(session.conditions)];
390
+ const distance = session.muzzleDistance?.value;
391
+ if (distance !== undefined && !(distance >= 0)) {
392
+ findings.push({
393
+ finding: 'muzzle-distance-range',
394
+ message: `muzzle distance ${distance} mm is negative`,
395
+ });
396
+ }
397
+ return findings;
398
+ }
399
+ //# sourceMappingURL=plausibility.js.map
@@ -0,0 +1,44 @@
1
+ import { type Bullet, type Cartridge, type Powder, type Primer, type PublishedLoads } from '@rangefoundry/schema';
2
+ import type { Finding } from './plausibility.ts';
3
+ /** A file of published loads and the cartridge folder it is read from. */
4
+ export type LocatedPublishedLoads = {
5
+ folder: string;
6
+ file: PublishedLoads;
7
+ };
8
+ /** The records published loads are checked against. */
9
+ export type PublishedLoadTargets = {
10
+ cartridges: Set<string>;
11
+ manufacturers: Set<string>;
12
+ bullets: readonly Pick<Bullet, 'id'>[];
13
+ powders: readonly Pick<Powder, 'id'>[];
14
+ primers: readonly Primer[];
15
+ };
16
+ /**
17
+ * The rules of published load files, each finding one error:
18
+ * - the folder is the file's cartridge, which exists;
19
+ * - the maker is a manufacturer record, and the file name is the maker's id (`<maker>-customer`
20
+ * for a table of loads customers sent);
21
+ * - the loads have unique ids and are sorted by them;
22
+ * - a bullet, powder or primer record a load names exists, and a printed primer size agrees with
23
+ * the records;
24
+ * - the value of a printed figure follows its printed text.
25
+ */
26
+ export declare function checkPublishedLoads(files: readonly LocatedPublishedLoads[], targets: PublishedLoadTargets, errors: string[]): void;
27
+ /** A plausibility finding of one load of a published load file. */
28
+ export type PublishedLoadFinding = Finding & {
29
+ load: string;
30
+ };
31
+ /**
32
+ * The plausibility findings of a published load file, each a warning: a printed value that looks
33
+ * wrong is still the maker's value and stays as printed. Findings:
34
+ * - `start-above-max`: the start charge is above the max charge of the same load;
35
+ * - `velocity-order`: the velocity of the max charge is below that of the start charge (same point);
36
+ * - `pressure-order`: the pressure of the max charge is below that of the start charge (same unit
37
+ * and method, both with a value in bar);
38
+ * - `pressure-above-cartridge-max`: a pressure with a value in bar whose method is the cartridge
39
+ * record's `pressureMethod` is above the record's largest `Pmax`. Other methods, and case-contact
40
+ * pressure always, are not compared: no value is converted between pressure bases;
41
+ * - `coal-above-cartridge-max`: COAL above the record's largest `L6` (maximum cartridge length).
42
+ * Without a cartridge record only the order findings apply.
43
+ */
44
+ export declare function checkPublishedLoadPlausibility(file: PublishedLoads, cartridge: Pick<Cartridge, 'id' | 'pressureMethod' | 'values'> | undefined): PublishedLoadFinding[];