2025-04-02 00:13:17 -06:00
|
|
|
import {
|
|
|
|
|
Box,
|
2025-04-02 12:41:18 -06:00
|
|
|
Autocomplete,
|
|
|
|
|
TextField,
|
2025-04-02 00:13:17 -06:00
|
|
|
Radio,
|
|
|
|
|
Table,
|
|
|
|
|
TableBody,
|
|
|
|
|
TableCell,
|
|
|
|
|
TableHead,
|
|
|
|
|
TableRow
|
|
|
|
|
} from '@mui/material';
|
|
|
|
|
import React, { useState } from 'react';
|
|
|
|
|
import ToolContent from '@components/ToolContent';
|
|
|
|
|
import { ToolComponentProps } from '@tools/defineTool';
|
|
|
|
|
import ToolTextResult from '@components/result/ToolTextResult';
|
2025-04-02 19:42:13 -06:00
|
|
|
import NumericInputWithUnit from '@components/input/NumericInputWithUnit';
|
2025-04-02 12:41:18 -06:00
|
|
|
import { UpdateField } from '@components/options/ToolOptions';
|
2025-04-02 00:13:17 -06:00
|
|
|
import { InitialValuesType } from './types';
|
2025-04-04 00:53:21 -06:00
|
|
|
import type { AlternativeVarInfo, GenericCalcType } from './data/types';
|
2025-04-02 12:41:18 -06:00
|
|
|
import type { DataTable } from 'datatables';
|
2025-04-02 13:38:12 -06:00
|
|
|
import { getDataTable, dataTableLookup } from 'datatables';
|
2025-04-02 00:13:17 -06:00
|
|
|
|
2025-04-04 00:53:21 -06:00
|
|
|
import nerdamer from 'nerdamer-prime';
|
|
|
|
|
import 'nerdamer-prime/Algebra';
|
|
|
|
|
import 'nerdamer-prime/Solve';
|
|
|
|
|
import 'nerdamer-prime/Calculus';
|
|
|
|
|
import Qty from 'js-quantities';
|
2025-04-04 01:11:50 -06:00
|
|
|
import { error } from 'console';
|
2025-04-04 00:53:21 -06:00
|
|
|
|
|
|
|
|
function numericSolveEquationFor(
|
|
|
|
|
equation: string,
|
|
|
|
|
varName: string,
|
|
|
|
|
variables: { [key: string]: number }
|
|
|
|
|
) {
|
|
|
|
|
let expr = nerdamer(equation);
|
|
|
|
|
for (const key in variables) {
|
|
|
|
|
expr = expr.sub(key, variables[key].toString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let result: nerdamer.Expression | nerdamer.Expression[] =
|
|
|
|
|
expr.solveFor(varName);
|
|
|
|
|
|
|
|
|
|
// Sometimes the result is an array, check for it while keeping linter happy
|
|
|
|
|
if ((result as unknown as nerdamer.Expression).toDecimal === undefined) {
|
|
|
|
|
result = (result as unknown as nerdamer.Expression[])[0];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return parseFloat(
|
|
|
|
|
(result as unknown as nerdamer.Expression).evaluate().toDecimal()
|
|
|
|
|
);
|
|
|
|
|
}
|
2025-04-02 00:13:17 -06:00
|
|
|
|
2025-04-02 12:41:18 -06:00
|
|
|
export default async function makeTool(
|
|
|
|
|
calcData: GenericCalcType
|
|
|
|
|
): Promise<React.JSXElementConstructor<ToolComponentProps>> {
|
2025-04-02 00:13:17 -06:00
|
|
|
const initialValues: InitialValuesType = {
|
|
|
|
|
outputVariable: '',
|
2025-04-02 12:41:18 -06:00
|
|
|
vars: {},
|
|
|
|
|
presets: {}
|
2025-04-02 00:13:17 -06:00
|
|
|
};
|
|
|
|
|
|
2025-04-02 12:41:18 -06:00
|
|
|
const dataTables: { [key: string]: DataTable } = {};
|
|
|
|
|
|
|
|
|
|
for (const selection of calcData.selections || []) {
|
|
|
|
|
dataTables[selection.source] = await getDataTable(selection.source);
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-02 00:13:17 -06:00
|
|
|
return function GenericCalc({ title }: ToolComponentProps) {
|
|
|
|
|
const [result, setResult] = useState<string>('');
|
2025-04-04 00:53:21 -06:00
|
|
|
|
|
|
|
|
const [alternatesByVariable, setAlternatesByVariable] = useState<{
|
|
|
|
|
[key: string]: {
|
|
|
|
|
value: {
|
|
|
|
|
value: number;
|
|
|
|
|
unit: string;
|
|
|
|
|
};
|
|
|
|
|
}[];
|
|
|
|
|
}>({});
|
2025-04-02 00:13:17 -06:00
|
|
|
|
2025-04-02 12:41:18 -06:00
|
|
|
// For UX purposes we need to track what vars are
|
|
|
|
|
const [valsBoundToPreset, setValsBoundToPreset] = useState<{
|
|
|
|
|
[key: string]: string;
|
|
|
|
|
}>({});
|
|
|
|
|
|
2025-04-02 19:22:29 -06:00
|
|
|
const [extraOutputs, setExtraOutputs] = useState<{
|
2025-04-03 05:32:47 -06:00
|
|
|
[key: string]: number;
|
2025-04-02 19:22:29 -06:00
|
|
|
}>({});
|
|
|
|
|
|
2025-04-02 00:13:17 -06:00
|
|
|
const updateVarField = (
|
|
|
|
|
name: string,
|
|
|
|
|
value: number,
|
2025-04-02 19:42:13 -06:00
|
|
|
unit: string,
|
2025-04-02 00:13:17 -06:00
|
|
|
values: InitialValuesType,
|
|
|
|
|
updateFieldFunc: UpdateField<InitialValuesType>
|
|
|
|
|
) => {
|
|
|
|
|
// Make copy
|
|
|
|
|
const newVars = { ...values.vars };
|
|
|
|
|
newVars[name] = {
|
|
|
|
|
value,
|
2025-04-02 19:42:13 -06:00
|
|
|
unit: unit
|
2025-04-02 00:13:17 -06:00
|
|
|
};
|
|
|
|
|
updateFieldFunc('vars', newVars);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleSelectedTargetChange = (
|
|
|
|
|
varName: string,
|
|
|
|
|
updateFieldFunc: UpdateField<InitialValuesType>
|
|
|
|
|
) => {
|
|
|
|
|
updateFieldFunc('outputVariable', varName);
|
|
|
|
|
};
|
|
|
|
|
|
2025-04-02 12:41:18 -06:00
|
|
|
const handleSelectedPresetChange = (
|
|
|
|
|
selection: string,
|
|
|
|
|
preset: string,
|
|
|
|
|
currentValues: InitialValuesType,
|
|
|
|
|
updateFieldFunc: UpdateField<InitialValuesType>
|
|
|
|
|
) => {
|
|
|
|
|
const newPresets = { ...currentValues.presets };
|
|
|
|
|
newPresets[selection] = preset;
|
|
|
|
|
updateFieldFunc('presets', newPresets);
|
|
|
|
|
|
|
|
|
|
// Clear old selection
|
|
|
|
|
for (const key in valsBoundToPreset) {
|
|
|
|
|
if (valsBoundToPreset[key] === selection) {
|
2025-04-02 13:38:12 -06:00
|
|
|
delete valsBoundToPreset[key];
|
2025-04-02 12:41:18 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const selectionData = calcData.selections?.find(
|
|
|
|
|
(sel) => sel.title === selection
|
|
|
|
|
);
|
|
|
|
|
|
2025-04-02 13:38:12 -06:00
|
|
|
if (preset && preset != '<custom>') {
|
2025-04-02 12:41:18 -06:00
|
|
|
if (selectionData) {
|
|
|
|
|
for (const key in selectionData.bind) {
|
2025-04-02 13:38:12 -06:00
|
|
|
valsBoundToPreset[key] = selection;
|
|
|
|
|
|
|
|
|
|
if (currentValues.outputVariable === key) {
|
|
|
|
|
handleSelectedTargetChange('', updateFieldFunc);
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-02 12:41:18 -06:00
|
|
|
updateVarField(
|
|
|
|
|
key,
|
2025-04-02 13:38:12 -06:00
|
|
|
|
|
|
|
|
dataTableLookup(dataTables[selectionData.source], preset)[
|
2025-04-02 12:41:18 -06:00
|
|
|
selectionData.bind[key]
|
|
|
|
|
],
|
2025-04-02 19:42:13 -06:00
|
|
|
|
|
|
|
|
dataTables[selectionData.source].columns[selectionData.bind[key]]
|
|
|
|
|
?.unit || '',
|
2025-04-02 12:41:18 -06:00
|
|
|
currentValues,
|
|
|
|
|
updateFieldFunc
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
throw new Error(
|
|
|
|
|
`Preset "${preset}" is not valid for selection "${selection}"`
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
calcData.variables.forEach((variable) => {
|
2025-04-04 00:53:21 -06:00
|
|
|
if (variable.solvable === undefined) {
|
|
|
|
|
variable.solvable = true;
|
|
|
|
|
}
|
2025-04-02 12:41:18 -06:00
|
|
|
if (variable.default === undefined) {
|
|
|
|
|
initialValues.vars[variable.name] = {
|
|
|
|
|
value: NaN,
|
|
|
|
|
unit: variable.unit
|
|
|
|
|
};
|
|
|
|
|
initialValues.outputVariable = variable.name;
|
|
|
|
|
} else {
|
|
|
|
|
initialValues.vars[variable.name] = {
|
|
|
|
|
value: variable.default || 0,
|
|
|
|
|
unit: variable.unit
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
calcData.selections?.forEach((selection) => {
|
|
|
|
|
initialValues.presets[selection.title] = selection.default;
|
|
|
|
|
if (selection.default == '<custom>') return;
|
|
|
|
|
for (const key in selection.bind) {
|
|
|
|
|
initialValues.vars[key] = {
|
2025-04-02 13:38:12 -06:00
|
|
|
value: dataTableLookup(
|
|
|
|
|
dataTables[selection.source],
|
|
|
|
|
selection.default
|
|
|
|
|
)[selection.bind[key]],
|
|
|
|
|
|
|
|
|
|
unit:
|
|
|
|
|
dataTables[selection.source].columns[selection.bind[key]]?.unit ||
|
|
|
|
|
''
|
2025-04-02 12:41:18 -06:00
|
|
|
};
|
2025-04-02 13:38:12 -06:00
|
|
|
valsBoundToPreset[key] = selection.title;
|
2025-04-02 12:41:18 -06:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2025-04-04 00:53:21 -06:00
|
|
|
function getAlternate(
|
|
|
|
|
alternateInfo: AlternativeVarInfo,
|
|
|
|
|
mainInfo: GenericCalcType['variables'][number],
|
|
|
|
|
mainValue: {
|
|
|
|
|
value: number;
|
|
|
|
|
unit: string;
|
|
|
|
|
}
|
|
|
|
|
) {
|
|
|
|
|
if (isNaN(mainValue.value)) return NaN;
|
|
|
|
|
const canonicalValue = Qty(mainValue.value, mainValue.unit).to(
|
|
|
|
|
mainInfo.unit
|
|
|
|
|
).scalar;
|
|
|
|
|
|
|
|
|
|
return numericSolveEquationFor(alternateInfo.formula, 'x', {
|
|
|
|
|
v: canonicalValue
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getMainFromAlternate(
|
|
|
|
|
alternateInfo: AlternativeVarInfo,
|
|
|
|
|
mainInfo: GenericCalcType['variables'][number],
|
|
|
|
|
alternateValue: {
|
|
|
|
|
value: number;
|
|
|
|
|
unit: string;
|
|
|
|
|
}
|
|
|
|
|
) {
|
|
|
|
|
if (isNaN(alternateValue.value)) return NaN;
|
|
|
|
|
const canonicalValue = Qty(alternateValue.value, alternateValue.unit).to(
|
|
|
|
|
alternateInfo.unit
|
|
|
|
|
).scalar;
|
|
|
|
|
|
|
|
|
|
return numericSolveEquationFor(alternateInfo.formula, 'v', {
|
|
|
|
|
x: canonicalValue
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
calcData.variables.forEach((variable) => {
|
|
|
|
|
if (variable.alternates) {
|
|
|
|
|
variable.alternates.forEach((alt) => {
|
|
|
|
|
const altValue = getAlternate(
|
|
|
|
|
alt,
|
|
|
|
|
variable,
|
|
|
|
|
initialValues.vars[variable.name]
|
|
|
|
|
);
|
|
|
|
|
if (alternatesByVariable[variable.name] === undefined) {
|
|
|
|
|
alternatesByVariable[variable.name] = [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
alternatesByVariable[variable.name].push({
|
|
|
|
|
value: { value: altValue, unit: variable.unit }
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2025-04-02 00:13:17 -06:00
|
|
|
return (
|
|
|
|
|
<ToolContent
|
|
|
|
|
title={title}
|
|
|
|
|
inputComponent={null}
|
|
|
|
|
initialValues={initialValues}
|
|
|
|
|
toolInfo={{
|
2025-04-04 00:53:21 -06:00
|
|
|
title: calcData.title,
|
2025-04-02 00:13:17 -06:00
|
|
|
description:
|
2025-04-04 00:53:21 -06:00
|
|
|
(calcData.description || '') +
|
|
|
|
|
' Generated from formula: ' +
|
|
|
|
|
calcData.formula
|
2025-04-02 00:13:17 -06:00
|
|
|
}}
|
|
|
|
|
getGroups={({ values, updateField }) => [
|
2025-04-02 12:41:18 -06:00
|
|
|
{
|
|
|
|
|
title: 'Presets',
|
|
|
|
|
component: (
|
|
|
|
|
<Box>
|
|
|
|
|
<Table>
|
|
|
|
|
<TableHead>
|
|
|
|
|
<TableRow>
|
|
|
|
|
<TableCell>Option</TableCell>
|
|
|
|
|
<TableCell>Value</TableCell>
|
|
|
|
|
</TableRow>
|
|
|
|
|
</TableHead>
|
|
|
|
|
<TableBody>
|
|
|
|
|
{calcData.selections?.map((preset) => (
|
|
|
|
|
<TableRow key={preset.title}>
|
|
|
|
|
<TableCell>{preset.title}</TableCell>
|
|
|
|
|
<TableCell>
|
|
|
|
|
<Autocomplete
|
|
|
|
|
disablePortal
|
|
|
|
|
id="combo-box-demo"
|
|
|
|
|
value={values.presets[preset.title]}
|
2025-04-02 13:38:12 -06:00
|
|
|
options={[
|
|
|
|
|
'<custom>',
|
|
|
|
|
...Object.keys(
|
|
|
|
|
dataTables[preset.source].data
|
|
|
|
|
).sort()
|
|
|
|
|
]}
|
2025-04-02 12:41:18 -06:00
|
|
|
sx={{ width: 300 }}
|
|
|
|
|
onChange={(event, newValue) => {
|
|
|
|
|
handleSelectedPresetChange(
|
|
|
|
|
preset.title,
|
|
|
|
|
newValue || '',
|
|
|
|
|
values,
|
|
|
|
|
updateField
|
|
|
|
|
);
|
|
|
|
|
}}
|
|
|
|
|
renderInput={(params) => (
|
|
|
|
|
<TextField {...params} label="Preset" />
|
|
|
|
|
)}
|
|
|
|
|
></Autocomplete>
|
|
|
|
|
</TableCell>
|
|
|
|
|
</TableRow>
|
|
|
|
|
))}
|
|
|
|
|
</TableBody>
|
|
|
|
|
</Table>
|
|
|
|
|
</Box>
|
|
|
|
|
)
|
|
|
|
|
},
|
2025-04-02 00:13:17 -06:00
|
|
|
{
|
2025-04-02 13:38:12 -06:00
|
|
|
title: 'Variables',
|
2025-04-02 00:13:17 -06:00
|
|
|
component: (
|
|
|
|
|
<Table>
|
|
|
|
|
<TableHead>
|
|
|
|
|
<TableRow>
|
|
|
|
|
<TableCell>Value</TableCell>
|
|
|
|
|
<TableCell>Solve For</TableCell>
|
|
|
|
|
</TableRow>
|
|
|
|
|
</TableHead>
|
|
|
|
|
<TableBody>
|
2025-04-02 12:41:18 -06:00
|
|
|
{calcData.variables.map((variable) => (
|
2025-04-02 00:13:17 -06:00
|
|
|
<TableRow key={variable.name}>
|
|
|
|
|
<TableCell>
|
2025-04-04 00:53:21 -06:00
|
|
|
<Table>
|
|
|
|
|
<TableRow>
|
|
|
|
|
<TableCell>{variable.title}</TableCell>
|
|
|
|
|
<TableCell>
|
|
|
|
|
<NumericInputWithUnit
|
|
|
|
|
description={
|
|
|
|
|
valsBoundToPreset[variable.name] || ''
|
|
|
|
|
}
|
|
|
|
|
defaultPrefix={variable.defaultPrefix}
|
|
|
|
|
value={values.vars[variable.name]}
|
|
|
|
|
disabled={
|
|
|
|
|
values.outputVariable === variable.name ||
|
|
|
|
|
valsBoundToPreset[variable.name] !== undefined
|
|
|
|
|
}
|
|
|
|
|
disableChangingUnit={
|
|
|
|
|
valsBoundToPreset[variable.name] !== undefined
|
|
|
|
|
}
|
|
|
|
|
onOwnChange={(val) =>
|
|
|
|
|
updateVarField(
|
|
|
|
|
variable.name,
|
|
|
|
|
val.value,
|
|
|
|
|
val.unit,
|
|
|
|
|
values,
|
|
|
|
|
updateField
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
type="number"
|
|
|
|
|
/>
|
|
|
|
|
</TableCell>
|
|
|
|
|
</TableRow>
|
|
|
|
|
|
|
|
|
|
{variable.alternates?.map((alt) => (
|
|
|
|
|
<TableRow key={alt.title}>
|
|
|
|
|
<TableCell>{alt.title}</TableCell>
|
|
|
|
|
<TableCell>
|
|
|
|
|
<NumericInputWithUnit
|
|
|
|
|
key={alt.title}
|
|
|
|
|
description={
|
|
|
|
|
valsBoundToPreset[alt.title] || ''
|
|
|
|
|
}
|
|
|
|
|
defaultPrefix={alt.defaultPrefix || ''}
|
|
|
|
|
value={{
|
|
|
|
|
value:
|
|
|
|
|
getAlternate(
|
|
|
|
|
alt,
|
|
|
|
|
variable,
|
|
|
|
|
values.vars[variable.name]
|
|
|
|
|
) || NaN,
|
|
|
|
|
unit: alt.unit || ''
|
|
|
|
|
}}
|
|
|
|
|
disabled={
|
|
|
|
|
values.outputVariable === variable.name ||
|
|
|
|
|
valsBoundToPreset[variable.name] !==
|
|
|
|
|
undefined
|
|
|
|
|
}
|
|
|
|
|
disableChangingUnit={
|
|
|
|
|
valsBoundToPreset[variable.name] !==
|
|
|
|
|
undefined
|
|
|
|
|
}
|
|
|
|
|
onOwnChange={(val) =>
|
|
|
|
|
updateVarField(
|
|
|
|
|
variable.name,
|
|
|
|
|
getMainFromAlternate(alt, variable, val),
|
|
|
|
|
variable.unit,
|
|
|
|
|
values,
|
|
|
|
|
updateField
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
></NumericInputWithUnit>
|
|
|
|
|
</TableCell>
|
|
|
|
|
</TableRow>
|
|
|
|
|
))}
|
|
|
|
|
</Table>
|
2025-04-02 00:13:17 -06:00
|
|
|
</TableCell>
|
|
|
|
|
|
|
|
|
|
<TableCell>
|
|
|
|
|
<Radio
|
|
|
|
|
value={variable.name}
|
|
|
|
|
checked={values.outputVariable === variable.name}
|
2025-04-02 13:38:12 -06:00
|
|
|
disabled={
|
2025-04-04 00:53:21 -06:00
|
|
|
valsBoundToPreset[variable.name] !== undefined ||
|
|
|
|
|
variable.solvable === false
|
2025-04-02 13:38:12 -06:00
|
|
|
}
|
2025-04-02 00:13:17 -06:00
|
|
|
onClick={() =>
|
|
|
|
|
handleSelectedTargetChange(
|
|
|
|
|
variable.name,
|
|
|
|
|
updateField
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
/>
|
|
|
|
|
</TableCell>
|
|
|
|
|
</TableRow>
|
|
|
|
|
))}
|
2025-04-02 19:22:29 -06:00
|
|
|
|
|
|
|
|
{calcData.extraOutputs?.map((extraOutput) => (
|
|
|
|
|
<TableRow key={extraOutput.title}>
|
2025-04-03 05:32:47 -06:00
|
|
|
<TableCell>
|
2025-04-04 00:58:04 -06:00
|
|
|
{extraOutput.title}
|
2025-04-03 05:32:47 -06:00
|
|
|
<NumericInputWithUnit
|
|
|
|
|
disabled={true}
|
|
|
|
|
defaultPrefix={extraOutput.defaultPrefix}
|
|
|
|
|
value={{
|
|
|
|
|
value: extraOutputs[extraOutput.title],
|
|
|
|
|
unit: extraOutput.unit
|
|
|
|
|
}}
|
|
|
|
|
></NumericInputWithUnit>
|
|
|
|
|
</TableCell>
|
2025-04-02 19:22:29 -06:00
|
|
|
<TableCell></TableCell>
|
|
|
|
|
</TableRow>
|
|
|
|
|
))}
|
2025-04-02 00:13:17 -06:00
|
|
|
</TableBody>
|
|
|
|
|
</Table>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
]}
|
|
|
|
|
compute={(values) => {
|
|
|
|
|
if (values.outputVariable === '') {
|
|
|
|
|
setResult('Please select a solve for variable');
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-04-04 01:11:50 -06:00
|
|
|
let expr: nerdamer.Expression | null = null;
|
|
|
|
|
|
|
|
|
|
for (const i of calcData.variables) {
|
|
|
|
|
if (i.name === values.outputVariable) {
|
|
|
|
|
if (i.formula !== undefined) {
|
|
|
|
|
expr = nerdamer(i.formula);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (expr == null) {
|
|
|
|
|
expr = nerdamer(calcData.formula);
|
|
|
|
|
}
|
|
|
|
|
if (expr == null) {
|
|
|
|
|
throw new Error('No formula found');
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-04-02 00:13:17 -06:00
|
|
|
|
|
|
|
|
Object.keys(values.vars).forEach((key) => {
|
|
|
|
|
if (key === values.outputVariable) return;
|
2025-04-04 01:11:50 -06:00
|
|
|
if (expr === null) {
|
|
|
|
|
throw new Error('Math fail');
|
|
|
|
|
}
|
2025-04-02 00:13:17 -06:00
|
|
|
expr = expr.sub(key, values.vars[key].value.toString());
|
|
|
|
|
});
|
|
|
|
|
|
2025-04-04 00:53:21 -06:00
|
|
|
let result: nerdamer.Expression | nerdamer.Expression[] =
|
|
|
|
|
expr.solveFor(values.outputVariable);
|
2025-04-02 00:13:17 -06:00
|
|
|
|
|
|
|
|
// Sometimes the result is an array
|
2025-04-04 00:53:21 -06:00
|
|
|
if (
|
|
|
|
|
(result as unknown as nerdamer.Expression).toDecimal === undefined
|
|
|
|
|
) {
|
|
|
|
|
if ((result as unknown as nerdamer.Expression[])?.length < 1) {
|
|
|
|
|
values.vars[values.outputVariable].value = NaN;
|
|
|
|
|
if (calcData.extraOutputs !== undefined) {
|
|
|
|
|
for (let i = 0; i < calcData.extraOutputs.length; i++) {
|
|
|
|
|
const extraOutput = calcData.extraOutputs[i];
|
|
|
|
|
extraOutputs[extraOutput.title] = NaN;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
throw new Error('No solution found for this input');
|
|
|
|
|
}
|
2025-04-02 00:13:17 -06:00
|
|
|
result = (result as unknown as nerdamer.Expression[])[0];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setResult(result.toString());
|
|
|
|
|
|
|
|
|
|
if (result) {
|
|
|
|
|
if (values.vars[values.outputVariable] != undefined) {
|
|
|
|
|
values.vars[values.outputVariable].value = parseFloat(
|
2025-04-04 00:53:21 -06:00
|
|
|
(result as unknown as nerdamer.Expression)
|
|
|
|
|
.evaluate()
|
|
|
|
|
.toDecimal()
|
2025-04-02 00:13:17 -06:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2025-04-04 00:53:21 -06:00
|
|
|
values.vars[values.outputVariable].value = NaN;
|
2025-04-02 00:13:17 -06:00
|
|
|
}
|
2025-04-02 19:22:29 -06:00
|
|
|
|
|
|
|
|
if (calcData.extraOutputs !== undefined) {
|
|
|
|
|
for (let i = 0; i < calcData.extraOutputs.length; i++) {
|
|
|
|
|
const extraOutput = calcData.extraOutputs[i];
|
|
|
|
|
|
|
|
|
|
let expr = nerdamer(extraOutput.formula);
|
|
|
|
|
|
|
|
|
|
Object.keys(values.vars).forEach((key) => {
|
|
|
|
|
if (key === values.outputVariable) return;
|
|
|
|
|
expr = expr.sub(key, values.vars[key].value.toString());
|
|
|
|
|
});
|
|
|
|
|
|
2025-04-04 00:53:21 -06:00
|
|
|
// todo could this have multiple solutions too?
|
2025-04-02 19:22:29 -06:00
|
|
|
const result: nerdamer.Expression = expr.evaluate();
|
|
|
|
|
|
|
|
|
|
if (result) {
|
2025-04-03 05:32:47 -06:00
|
|
|
extraOutputs[extraOutput.title] = parseFloat(
|
|
|
|
|
result.toDecimal()
|
|
|
|
|
);
|
2025-04-02 19:22:29 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-04-02 00:13:17 -06:00
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
}
|