Skip to content

Commit

Permalink
Merge pull request #217 from mgreminger/no-auto-hz
Browse files Browse the repository at this point in the history
Default Unit Updates
  • Loading branch information
mgreminger authored Dec 23, 2023
2 parents f3d8125 + 5749a8e commit 47710d9
Show file tree
Hide file tree
Showing 19 changed files with 902 additions and 159 deletions.
332 changes: 245 additions & 87 deletions public/dimensional_analysis.py

Large diffs are not rendered by default.

42 changes: 32 additions & 10 deletions src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
incrementActiveCell, decrementActiveCell, deleteCell, activeMathField,
autosaveNeeded, mathJaxLoaded
} from "./stores";
import { isDefaultConfig } from "./sheet/Sheet";
import { getDefaultBaseUnits, isDefaultConfig } from "./sheet/Sheet";
import type { Statement } from "./parser/types";
import type { SystemDefinition } from "./cells/SystemCell";
import { isVisible, versionToDateString, debounce } from "./utility";
Expand Down Expand Up @@ -56,7 +56,10 @@
SideNavMenu,
SideNavItems,
SideNavLink,
HeaderActionLink
HeaderActionLink,
Tabs,
Tab,
TabContent
} from "carbon-components-svelte";
import CloudUpload from "carbon-icons-svelte/lib/CloudUpload.svelte";
Expand All @@ -81,10 +84,11 @@
import type MathCellElement from "./MathCell.svelte";
import GenerateCodeDialog from "./GenerateCodeDialog.svelte";
import CustomMatrixModal from "./CustomMatrixModal.svelte";
import BaseUnitsConfigDialog from "./BaseUnitsConfigDialog.svelte";
const apiUrl = window.location.origin;
const currentVersion = 20231113;
const currentVersion = 20231222;
const tutorialHash = "fFjTsnFoSQMLwcvteVoNtL";
const termsVersion = 20230608;
Expand Down Expand Up @@ -266,6 +270,7 @@
};
let mathCellConfigDialog: MathCellConfigDialog | null = null;
let baseUnitsConfigDialog: BaseUnitsConfigDialog | null = null;
function startWebWorker() {
if (pyodideLoadingTimeoutRef) {
Expand Down Expand Up @@ -820,7 +825,7 @@
statements.push(...endStatements);
return {statements: statements, systemDefinitions: systemDefinitions};
return {statements: statements, systemDefinitions: systemDefinitions, customBaseUnits: $config.customBaseUnits};
}
function checkParsingErrors() {
Expand Down Expand Up @@ -1067,6 +1072,9 @@ Please include a link to this sheet in the email to assist in debugging the prob
// old documents in database will not have the insertedSheets property or a config property
$insertedSheets = sheet.insertedSheets ?? [];
$config = sheet.config ?? getDefaultConfig();
if (!$config.customBaseUnits) {
$config.customBaseUnits = getDefaultBaseUnits();
}
if (!$history.map(item => item.hash !== "file" ? getSheetHash(new URL(item.url)) : "").includes(getSheetHash(window.location))) {
$history = requestHistory;
Expand Down Expand Up @@ -1757,7 +1765,7 @@ Please include a link to this sheet in the email to assist in debugging the prob
modalInfo = {
modalOpen: true,
state: "sheetSettings",
heading: "Sheet Number Format Settings",
heading: "Sheet Settings",
mathCell: null,
mathCellElement: null
};
Expand Down Expand Up @@ -2510,7 +2518,7 @@ Please include a link to this sheet in the email to assist in debugging the prob
primaryButtonText="Confirm"
secondaryButtonText="Restore Defaults"
on:click:button--primary={() => modalInfo.modalOpen = false}
on:click:button--secondary={() => mathCellConfigDialog?.resetDefaults()}
on:click:button--secondary={() => {mathCellConfigDialog?.resetDefaults(); baseUnitsConfigDialog?.resetDefaults();}}
bind:open={modalInfo.modalOpen}
>
{#if modalInfo.mathCell}
Expand All @@ -2521,10 +2529,24 @@ Please include a link to this sheet in the email to assist in debugging the prob
cellLevelConfig={true}
/>
{:else}
<MathCellConfigDialog
bind:this={mathCellConfigDialog}
bind:mathCellConfig={$config.mathCellConfig}
/>
<Tabs>
<Tab label="Number Format" />
<Tab label="Default Units" />
<svelte:fragment slot="content">
<TabContent>
<MathCellConfigDialog
bind:this={mathCellConfigDialog}
bind:mathCellConfig={$config.mathCellConfig}
/>
</TabContent>
<TabContent>
<BaseUnitsConfigDialog
bind:this={baseUnitsConfigDialog}
bind:baseUnits={$config.customBaseUnits}
/>
</TabContent>
</svelte:fragment>
</Tabs>
{/if}
</Modal>
{:else if modalInfo.state === "customMatrix"}
Expand Down
61 changes: 61 additions & 0 deletions src/BaseUnitsConfigDialog.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<script lang="ts">
import { ComboBox, ButtonSet, Button } from "carbon-components-svelte";
import CheckmarkOutline from "carbon-icons-svelte/lib/CheckmarkOutline.svelte";
import { mathCellChanged } from "./stores";
import { type CustomBaseUnits, baseUnitSystems, baseUnitChoices,
getDefaultBaseUnits, isDefaultBaseUnits } from "./sheet/Sheet";
export let baseUnits: CustomBaseUnits;
export function resetDefaults() {
baseUnits = getDefaultBaseUnits();
}
function update() {
$mathCellChanged = true;
}
</script>

<style>
div.container {
display: flex;
flex-direction: column;
gap: 20px;
}
div.combo-input {
max-width: 250px;
}
</style>


<div class="container">
<div>
<div class="bx--label">Unit System Shortcuts</div>
<ButtonSet>
{#each baseUnitSystems.keys() as name}
<Button
kind="tertiary"
on:click={() => baseUnits = getDefaultBaseUnits(name)}
icon={isDefaultBaseUnits(baseUnits, name) ? CheckmarkOutline : null}
>
{name}
</Button>
{/each}
</ButtonSet>
</div>

{#each baseUnitChoices as dimension (dimension.name)}
<div class="combo-input">
<ComboBox
titleText={dimension.label}
placeholder="Select default unit method"
bind:selectedId={baseUnits[dimension.name]}
items={dimension.choices.map(value => ({id: value, text: value}))}
on:select={update}
/>
</div>
{/each}
</div>


26 changes: 24 additions & 2 deletions src/GenerateCodeDialog.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,28 @@
} else {
const { functionName, parameterNames, generatedCode } = rewriteVariables(statement.functionName, statement.parameterNames, result.generatedCode);
let userReturnUnits = "";
if (isMatrixResult(result)) {
let matrixUnits = new Set<string>();
for (const row of result.results) {
for (const value of row) {
matrixUnits.add(value.units);
}
}
if (matrixUnits.size === 1) {
if (statement.units) {
userReturnUnits = statement.units;
} else if (result.results[0][0].customUnitsDefined) {
userReturnUnits = result.results[0][0].customUnits;
}
}
} else if (statement.units) {
userReturnUnits = statement.units;
} else if (result.customUnitsDefined) {
userReturnUnits = result.customUnits;
}
return `${generatedCode.includes('numpy') ? "import numpy\n\n": ""}def ${functionName}(${parameterNames.join(", ")}):
"""
Function '${functionName}' automatically generated by EngineeringPaper.xyz
Expand All @@ -181,12 +203,12 @@ ${parameterNames.map(parameterMap).join("\n")}
Returns
-------
${isMatrixResult(result) ? "numpy.array" : "float"}
Return value ${isMatrixResult(result) ? getMatrixUnitsResult(result, statement.units) : getUnitsDescription(statement.units ? statement.units : result.units)}
Return value ${isMatrixResult(result) ? getMatrixUnitsResult(result, userReturnUnits) : getUnitsDescription(userReturnUnits ? userReturnUnits : result.units)}
"""
${parameterNames.map(parameterConversionMap).filter(value => value !== "").map((row, i) => i === 0 ? "\n"+row : row).map(row => row+"\n").join("")}
result = ${generatedCode}
${getReturnConversion(statement.units ?? "")}
${getReturnConversion(userReturnUnits)}
`
}
}
Expand Down
27 changes: 17 additions & 10 deletions src/MathCell.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import { isFiniteImagResult, type Result, type FiniteImagResult,
type PlotResult, type MatrixResult, isMatrixResult } from "./resultTypes";
import type { CodeFunctionQueryStatement, QueryStatement } from "./parser/types";
import { convertUnits, unitsValid } from "./utility";
import { convertUnits } from "./utility";
import type { MathCellConfig } from "./sheet/Sheet";
import type MathCell from "./cells/MathCell";
import PlotCell from "./cells/PlotCell";
Expand Down Expand Up @@ -133,17 +133,24 @@
};
}
if ( statement.units ) {
// unit conversion required to user supplied units
resultUnitsLatex = statement.unitsLatex;
resultUnits = statement.units;
if ( statement.units || result.customUnitsDefined) {
// unit conversion required to user supplied units or sheet level user default units
// user supplied units in the statement takes precedence over sheet level user default units
if (statement.units) {
resultUnitsLatex = statement.unitsLatex;
resultUnits = statement.units;
} else {
resultUnitsLatex = result.customUnitsLatex;
resultUnits = result.customUnits;
}
if (result.numeric && result.real && result.finite && !numberConfig.symbolicOutput) {
const {newValue: localNewValue, unitsMismatch: localUnitsMismatch} = convertUnits(result.value, result.units, statement.units);
const {newValue: localNewValue, unitsMismatch: localUnitsMismatch} = convertUnits(result.value, result.units, resultUnits);
if (!localUnitsMismatch) {
resultLatex = customFormat(localNewValue, numberConfig.formatOptions);
if (!inMatrix) {
if (!inMatrix && statement.units) {
resultLatex = "=" + resultLatex;
}
} else {
Expand All @@ -152,13 +159,13 @@
} else if (isFiniteImagResult(result) && !numberConfig.symbolicOutput) {
// handle unit conversion for imaginary number
const {newValue: newRealValue, unitsMismatch: realUnitsMismatch} =
convertUnits(result.realPart, result.units, statement.units);
convertUnits(result.realPart, result.units, resultUnits);
const {newValue: newImagValue, unitsMismatch: imagUnitsMismatch} =
convertUnits(result.imagPart, result.units, statement.units);
convertUnits(result.imagPart, result.units, resultUnits);
if (!realUnitsMismatch && !imagUnitsMismatch) {
resultLatex = formatImag(newRealValue, newImagValue, numberConfig);
if (!inMatrix) {
if (!inMatrix && statement.units) {
resultLatex = "=" + resultLatex;
}
} else {
Expand Down
24 changes: 19 additions & 5 deletions src/PlotCell.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,14 @@
if ((plotCell.mathFields[0].statement?.type === "query" && plotCell.mathFields[0].statement.isRange) ||
plotCell.mathFields[0].statement?.type === "scatterQuery") {
// use input units from first plot statement
userInputUnits = plotCell.mathFields[0].statement.inputUnits;
userInputUnitsLatex = plotCell.mathFields[0].statement.inputUnitsLatex;
if (plotCell.mathFields[0].statement.inputUnits) {
userInputUnits = plotCell.mathFields[0].statement.inputUnits;
userInputUnitsLatex = plotCell.mathFields[0].statement.inputUnitsLatex;
} else if ($results[index] && $results[index][0] && $results[index][0].plot &&
($results[index][0] as PlotResult).data[0].inputCustomUnitsDefined) {
userInputUnits = ($results[index][0] as PlotResult).data[0].inputCustomUnits;
userInputUnitsLatex = ($results[index][0] as PlotResult).data[0].inputCustomUnitsLatex;
}
}
for (const [j, statement] of plotCell.mathFields.map((field) => field.statement).entries()) {
if ($results[index] && $results[index][j] &&
Expand Down Expand Up @@ -122,9 +128,17 @@
}
// convert outputs if units provided
if (statement.units) {
const userOutputUnits = statement.units;
const userOutputUnitsLatex = statement.unitsLatex;
if (statement.units || data.outputCustomUnitsDefined) {
let userOutputUnits: string;
let userOutputUnitsLatex: string;
if (statement.units) {
userOutputUnits = statement.units;
userOutputUnitsLatex = statement.unitsLatex;
} else {
userOutputUnits = data.outputCustomUnits;
userOutputUnitsLatex = data.outputCustomUnitsLatex;
}
const startingOutputUnits = data.outputUnits;
Expand Down
29 changes: 29 additions & 0 deletions src/Updates.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,35 @@ import { modifierKey } from "./stores";
}
</style>

<em>December 22, 2023</em>
<h4>New Default Units Feature</h4>
<p>
The default base units used for results can now be set in the sheet settings dialog.
To access the base unit settings, click on the sheet settings
icon <SettingsAdjust size={16}/> in the top menu bar and then click on the
<em>Default Units</em> tab. The default units settings will be saved with the sheet.
</p>
<br>
<p>
As in the past, the default unit system for EngineeringPaper.xyz sheets is the
SI unit system. The default unit symbols now all correspond to the official
SI unit symbols (for example, the time unit now defaults to <em>s</em> rather
than <em>sec</em>). However, all of the old unit symbols still work for user
specified units.
</p>
<br>
<p>
Finally, the inverse time unit, <em>1/s</em>, is no longer automatically converted to
<em>Hz</em> since this behavior was confusing when the frequency unit should have been
interpreted as <em>rad/s</em>. See this
<a href="https://blog.engineeringpaper.xyz/calculating-the-natural-frequencies-of-a-cantilever-beam"
target="_blank">
blog post</a> for details on how to properly
handle frequency units in EngineeringPaper.xyz.
</p>

<br>

<em>November 13, 2023</em>
<h4>New Scatter Plotting Feature</h4>
<p>
Expand Down
9 changes: 9 additions & 0 deletions src/resultTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ export type Result = {
symbolicValue?: string; // some old database entries may not have this value
units: string;
unitsLatex: string;
customUnitsDefined?: boolean; // some old database entries my not have this value
customUnits?: string; // only defined if customUnitsDefined is true
customUnitsLatex?: string; // only defined if customUnitsDefined is true
numeric: boolean;
real: boolean;
finite: boolean;
Expand Down Expand Up @@ -44,10 +47,16 @@ export type PlotData = {
inputReversed: boolean;
inputUnits: string;
inputUnitsLatex: string;
inputCustomUnitsDefined?: boolean; // some old database entries my not have this value
inputCustomUnits?: string; // only defined if inputCustomUnitsDefined is true
inputCustomUnitsLatex?: string; // only defined if inputCustomUnitsDefined is true
inputName: string;
inputNameLatex?: string; // old versions of saved results may not have this property
outputUnits: string;
outputUnitsLatex: string;
outputCustomUnitsDefined?: boolean; // some old database entries my not have this value
outputCustomUnits?: string; // only defined if outputCustomUnitsDefined is true
outputCustomUnitsLatex?: string; // only defined if outputCustomUnitsDefined is true
outputName: string;
outputNameLatex?: string; // old versions of saved results may not have this property
isScatter?: boolean; // old versions of saved results won't have this property
Expand Down
Loading

0 comments on commit 47710d9

Please sign in to comment.