Files
muSRFlow/tests/singlehist-parity.test.js

200 lines
6.9 KiB
JavaScript

// Copyright (C) 2026 Zaher Salman
// SPDX-License-Identifier: GPL-3.0-or-later
const assert = require("assert");
const fs = require("fs");
const path = require("path");
const vm = require("vm");
const repoRoot = path.resolve(__dirname, "..");
function loadLibrary() {
const code = fs.readFileSync(path.join(repoRoot, "muSRFitlib.js"), "utf8");
const sandbox = {
console: console,
window: { MSREditor: {} },
alert: function(message) {
throw new Error("Unexpected alert: " + message);
}
};
sandbox.global = sandbox;
vm.runInNewContext(code, sandbox, { filename: "muSRFitlib.js" });
return sandbox;
}
function singleHistConfig(runNumbers) {
return {
all: {
fitAsyType: "SingleHist",
LRBF: "21 25,23 27",
runNumbers: runNumbers,
selYear: "2024",
instrName: "LEM",
theoFunBlock: "asymmetry Asy\nsimplExpo Lam\nTFieldCos Phi Frq",
Tis: "0",
Tfs: "8",
BINS: "100",
Tpis: "",
Tpfs: "",
VIEWBINS: "",
plotLifetimeCorrection: "1"
},
// This is the parameter list produced by the Constraints tab for one
// oscillating component and two histogram groups. PhiH2 is the second
// detector phase; all other physical model parameters stay common.
funOptions: [
{ value: "No1", disabled: false },
{ value: "NBg1", disabled: false },
{ value: "Asy", disabled: false },
{ value: "Lam", disabled: false },
{ value: "Phi", disabled: false },
{ value: "Frq", disabled: false },
{ value: "No2", disabled: false },
{ value: "NBg2", disabled: false },
{ value: "PhiH2", disabled: false }
],
funLines: [],
sharedFlags: {}
};
}
function testDetectorDependentPhaseUsesRunMaps(lib) {
const model = lib.buildModelBlocks(singleHistConfig("514"), 0);
// THEORY addresses phase through map1 instead of hard-wiring the first
// detector phase. The other physical parameters remain shared/direct.
assert.match(model.theoryBlock, /^asymmetry\s+3$/m);
assert.match(model.theoryBlock, /^simplExpo\s+4$/m);
assert.match(model.theoryBlock, /^TFieldCos\s+map1\s+6$/m);
// The first and second histogram RUNs select different phase parameters.
const maps = model.runBlock.match(/^map\s+\d+$/gm) || [];
assert.deepStrictEqual(Array.from(maps), ["map 5", "map 9"]);
assert.match(model.runBlock, /^forward\s+21 25$/m);
assert.match(model.runBlock, /^forward\s+23 27$/m);
// Run-local parameters carry the run suffix even for a single physical run.
assert.match(model.parameterBlock, /\bPhi_0514\b/);
assert.match(model.parameterBlock, /\bPhiH2_0514\b/);
assert.doesNotMatch(model.parameterBlock, /relPhi/);
}
function testMultipleComponentsGetIndependentDetectorPhaseMaps(lib) {
const config = {
all: {
fitAsyType: "SingleHist",
LRBF: "21,23",
runNumbers: "514",
selYear: "2024",
instrName: "LEM",
theoFunBlock: [
"asymmetry Asy1",
"simplExpo Lam1",
"TFieldCos Phi1 Frq1",
"+",
"asymmetry Asy2",
"simpleGss Sgm2",
"TFieldCos Phi2 Frq2"
].join("\n")
},
funOptions: [
{ value: "Asy1", disabled: false },
{ value: "Lam1", disabled: false },
{ value: "Phi1", disabled: false },
{ value: "Frq1", disabled: false },
{ value: "NBg1", disabled: false },
{ value: "No1", disabled: false },
{ value: "NBg2", disabled: false },
{ value: "No2", disabled: false },
{ value: "Phi1H2", disabled: false },
{ value: "Asy2", disabled: false },
{ value: "Sgm2", disabled: false },
{ value: "Phi2", disabled: false },
{ value: "Frq2", disabled: false },
{ value: "Phi2H2", disabled: false }
],
funLines: [],
sharedFlags: {}
};
const model = lib.buildModelBlocks(config, 0);
assert.match(model.theoryBlock, /^TFieldCos\s+map1\s+4$/m);
assert.match(model.theoryBlock, /^TFieldCos\s+map2\s+13$/m);
const maps = model.runBlock.match(/^map\s+\d+(?:\s+\d+)+$/gm) || [];
assert.deepStrictEqual(Array.from(maps), ["map 3 12", "map 9 14"]);
}
function testMultiplePhysicalRunsStayAnMsr2dataTemplate(lib) {
const msr = lib.buildMSR(singleHistConfig("514,515"));
const runLines = msr.match(/^RUN\s+.*$/gm) || [];
// Multi-run generation intentionally remains template based: one physical
// run (the first selected run) times the histogram groups. msr2data later
// expands this template to all selected physical runs.
assert.strictEqual(runLines.length, 2);
assert.ok(runLines.every(function(line) { return /0514\b/.test(line); }));
assert.ok(runLines.every(function(line) { return !/0515\b/.test(line); }));
assert.match(msr, /^# Run Numbers:\s+514,515$/m);
assert.match(msr, /^runs\s+1\s+2$/m);
// Detector-dependent phase mapping must already be correct in the template
// so msr2data can duplicate the relationship consistently for each run.
assert.match(msr, /^TFieldCos\s+map1\s+6$/m);
assert.match(msr, /^map\s+5$/m);
assert.match(msr, /^map\s+9$/m);
}
function testGlobalPlotListsEveryExpandedHistogramRun(lib) {
// Simulate the structure produced by msr2data for two physical runs and two
// SingleHist histogram groups. The PLOT block may still contain the template
// `runs 1 2` line, but musrview must be told to draw all four expanded RUN
// blocks in the global file.
const globalMsr = [
"FITPARAMETER",
"1 Dummy 1 0.1",
"",
"THEORY",
"simplExpo 1",
"",
"GLOBAL",
"fittype 0",
"",
"RUN run514.bin",
"forward 21 25",
"",
"RUN run514.bin",
"forward 23 27",
"",
"RUN run515.bin",
"forward 21 25",
"",
"RUN run515.bin",
"forward 23 27",
"",
"COMMANDS",
"MINIMIZE",
"",
"PLOT 0",
"runs 1 2",
"use_fit_ranges",
""
].join("\n");
const normalized = lib.normalizeMSRPlotRuns(
globalMsr,
singleHistConfig("514,515").all,
[514, 515]
);
assert.match(normalized, /^runs\s+1\s+2\s+3\s+4$/m);
}
const lib = loadLibrary();
testDetectorDependentPhaseUsesRunMaps(lib);
testMultipleComponentsGetIndependentDetectorPhaseMaps(lib);
testMultiplePhysicalRunsStayAnMsr2dataTemplate(lib);
testGlobalPlotListsEveryExpandedHistogramRun(lib);
console.log("single-histogram parity tests passed");