diff --git a/src/datatables/data/american_wire_gauge.ts b/src/datatables/data/american_wire_gauge.ts deleted file mode 100644 index 218ab9d..0000000 --- a/src/datatables/data/american_wire_gauge.ts +++ /dev/null @@ -1,62 +0,0 @@ -export default { - title: 'American Wire Gauge', - columns: [ - { - diameter: { - title: 'Diameter', - type: 'number', - unit: 'mm' - } - } - ], - data: { - '0000': { diameter: 11.684 }, - '000': { diameter: 10.405 }, - '00': { diameter: 9.266 }, - '0': { diameter: 8.251 }, - '(4/0)': { diameter: 11.684 }, - '(3/0)': { diameter: 10.405 }, - '(2/0)': { diameter: 9.266 }, - '(1/0)': { diameter: 8.251 }, - '1': { diameter: 7.348 }, - '2': { diameter: 6.544 }, - '3': { diameter: 5.827 }, - '4': { diameter: 5.189 }, - '5': { diameter: 4.621 }, - '6': { diameter: 4.115 }, - '7': { diameter: 3.665 }, - '8': { diameter: 3.264 }, - '9': { diameter: 2.906 }, - '10': { diameter: 2.588 }, - '11': { diameter: 2.305 }, - '12': { diameter: 2.053 }, - '13': { diameter: 1.828 }, - '14': { diameter: 1.628 }, - '15': { diameter: 1.45 }, - '16': { diameter: 1.291 }, - '17': { diameter: 1.15 }, - '18': { diameter: 1.024 }, - '19': { diameter: 0.912 }, - '20': { diameter: 0.812 }, - '21': { diameter: 0.723 }, - '22': { diameter: 0.644 }, - '23': { diameter: 0.573 }, - '24': { diameter: 0.511 }, - '25': { diameter: 0.455 }, - '26': { diameter: 0.405 }, - '27': { diameter: 0.361 }, - '28': { diameter: 0.321 }, - '29': { diameter: 0.286 }, - '30': { diameter: 0.255 }, - '31': { diameter: 0.227 }, - '32': { diameter: 0.202 }, - '33': { diameter: 0.18 }, - '34': { diameter: 0.16 }, - '35': { diameter: 0.143 }, - '36': { diameter: 0.127 }, - '37': { diameter: 0.113 }, - '38': { diameter: 0.101 }, - '39': { diameter: 0.0897 }, - '40': { diameter: 0.0799 } - } -}; diff --git a/src/datatables/data/index.ts b/src/datatables/data/index.ts index d9efaca..c5c5420 100644 --- a/src/datatables/data/index.ts +++ b/src/datatables/data/index.ts @@ -1,3 +1,10 @@ -export const allDataTables: { [key: string]: { [key: string]: any } } = { - 'american-wire-gauge': {} +import type { DataTable } from '../types'; +import wiregauge from './wire_gauge'; +import material_electrical_properties from './material_electrical_properties'; + +const allDataTables: { [key: string]: DataTable } = { + 'wire-gauge': wiregauge, + 'material-electrical-properties': material_electrical_properties }; + +export default allDataTables; diff --git a/src/datatables/data/material_electrical_properties.ts b/src/datatables/data/material_electrical_properties.ts index 8b78de9..912d016 100644 --- a/src/datatables/data/material_electrical_properties.ts +++ b/src/datatables/data/material_electrical_properties.ts @@ -10,11 +10,11 @@ export default { } ], data: { - copper: { - resistivity: 1.68e-8 + Copper: { + resistivity_20c: 1.68e-8 }, - aluminum: { - resistivity: 2.82e-8 + Aluminum: { + resistivity_20c: 2.82e-8 } } }; diff --git a/src/datatables/data/wire_gauge.ts b/src/datatables/data/wire_gauge.ts new file mode 100644 index 0000000..94b2a19 --- /dev/null +++ b/src/datatables/data/wire_gauge.ts @@ -0,0 +1,75 @@ +import type { DataTable } from '../types'; + +const data: DataTable = { + title: 'American Wire Gauge', + columns: [ + { + diameter: { + title: 'Diameter', + type: 'number', + unit: 'mm' + }, + area: { + title: 'Area', + type: 'number', + unit: 'mm^2' + } + } + ], + data: { + '0000 AWG': { diameter: 11.684 }, + '000 AWG': { diameter: 10.405 }, + '00 AWG': { diameter: 9.266 }, + '0 AWG': { diameter: 8.251 }, + '(4/0) AWG': { diameter: 11.684 }, + '(3/0) AWG': { diameter: 10.405 }, + '(2/0) AWG': { diameter: 9.266 }, + '(1/0) AWG': { diameter: 8.251 }, + '1 AWG': { diameter: 7.348 }, + '2 AWG': { diameter: 6.544 }, + '3 AWG': { diameter: 5.827 }, + '4 AWG': { diameter: 5.189 }, + '5 AWG': { diameter: 4.621 }, + '6 AWG': { diameter: 4.115 }, + '7 AWG': { diameter: 3.665 }, + '8 AWG': { diameter: 3.264 }, + '9 AWG': { diameter: 2.906 }, + '10 AWG': { diameter: 2.588 }, + '11 AWG': { diameter: 2.305 }, + '12 AWG': { diameter: 2.053 }, + '13 AWG': { diameter: 1.828 }, + '14 AWG': { diameter: 1.628 }, + '15 AWG': { diameter: 1.45 }, + '16 AWG': { diameter: 1.291 }, + '17 AWG': { diameter: 1.15 }, + '18 AWG': { diameter: 1.024 }, + '19 AWG': { diameter: 0.912 }, + '20 AWG': { diameter: 0.812 }, + '21 AWG': { diameter: 0.723 }, + '22 AWG': { diameter: 0.644 }, + '23 AWG': { diameter: 0.573 }, + '24 AWG': { diameter: 0.511 }, + '25 AWG': { diameter: 0.455 }, + '26 AWG': { diameter: 0.405 }, + '27 AWG': { diameter: 0.361 }, + '28 AWG': { diameter: 0.321 }, + '29 AWG': { diameter: 0.286 }, + '30 AWG': { diameter: 0.255 }, + '31 AWG': { diameter: 0.227 }, + '32 AWG': { diameter: 0.202 }, + '33 AWG': { diameter: 0.18 }, + '34 AWG': { diameter: 0.16 }, + '35 AWG': { diameter: 0.143 }, + '36 AWG': { diameter: 0.127 }, + '37 AWG': { diameter: 0.113 }, + '38 AWG': { diameter: 0.101 }, + '39 AWG': { diameter: 0.0897 }, + '40 AWG': { diameter: 0.0799 } + } +}; + +for (const key in data.data) { + data.data[key].area = Math.PI * data.data[key].diameter ** 2; +} + +export default data; diff --git a/src/datatables/index.ts b/src/datatables/index.ts index 5c3ff98..37174b2 100644 --- a/src/datatables/index.ts +++ b/src/datatables/index.ts @@ -1,17 +1,18 @@ import type { DataTable } from './types.ts'; -import { allDataTables } from './data/index'; export async function getDataTable(name: string): Promise { - const x = await import(`./${name}`); - return x.default; + const allDataTables = (await import('./data/index')).default; + return allDataTables[name]; } -export async function listDataTables(): Promise<{ name: string }[]> { - const x: { name: string }[] = []; - for (const key in allDataTables) { - x.push({ name: key }); - } - return x; +/* Used in case later we want any kind of computed extra data */ +export function dataTableLookup(table: DataTable, key: string): any { + return table.data[key]; +} + +export async function listDataTables(): Promise<{ [name: string]: DataTable }> { + const allDataTables = (await import('./data/index')).default; + return allDataTables; } export { DataTable }; diff --git a/src/datatables/types.ts b/src/datatables/types.ts index 37f4f48..c2199d4 100644 --- a/src/datatables/types.ts +++ b/src/datatables/types.ts @@ -4,16 +4,14 @@ Used for calculator presets */ export interface DataTable { - [key: string]: { - title: string; - /* A JSON schema properties */ - columns: { + title: string; + /* A JSON schema properties */ + columns: { + [key: string]: any; + }; + data: { + [key: string]: { [key: string]: any; }; - data: { - [key: string]: { - [key: string]: any; - }; - }; }; } diff --git a/src/pages/tools/number/generic-calc/data/index.ts b/src/pages/tools/number/generic-calc/data/index.ts index 727349c..9249a30 100644 --- a/src/pages/tools/number/generic-calc/data/index.ts +++ b/src/pages/tools/number/generic-calc/data/index.ts @@ -1,3 +1,4 @@ import ohmslaw from './ohms_law'; +import voltagedropinwire from './wire_voltage_drop'; -export default [ohmslaw]; +export default [ohmslaw, voltagedropinwire]; diff --git a/src/pages/tools/number/generic-calc/data/wire_voltage_drop.ts b/src/pages/tools/number/generic-calc/data/wire_voltage_drop.ts index e69de29..f0338a2 100644 --- a/src/pages/tools/number/generic-calc/data/wire_voltage_drop.ts +++ b/src/pages/tools/number/generic-calc/data/wire_voltage_drop.ts @@ -0,0 +1,59 @@ +import type { GenericCalcType } from './types'; +const voltagedropinwire: GenericCalcType = { + title: 'Round trip voltage drop in cable', + name: 'cable-voltage-drop', + formula: 'x = (((p * L) / (A/10**6) ) *2) * I**2', + selections: [ + { + title: 'Material', + source: 'material-electrical-properties', + default: 'Copper', + bind: { + p: 'resistivity_20c' + } + }, + + { + title: 'Wire Gauge', + source: 'wire-gauge', + default: '24 AWG', + bind: { + A: 'area' + } + } + ], + variables: [ + { + name: 'L', + title: 'Length', + unit: 'm', + default: 1 + }, + { + name: 'A', + title: 'Wire Area', + unit: 'mm', + default: 1 + }, + + { + name: 'I', + title: 'Current', + unit: 'A', + default: 1 + }, + { + name: 'p', + title: 'Resistivity', + unit: 'Ω/m3', + default: 1 + }, + { + name: 'x', + title: 'Voltage Drop', + unit: 'V' + } + ] +}; + +export default voltagedropinwire; diff --git a/src/pages/tools/number/generic-calc/index.tsx b/src/pages/tools/number/generic-calc/index.tsx index 2b8b69e..473c85d 100644 --- a/src/pages/tools/number/generic-calc/index.tsx +++ b/src/pages/tools/number/generic-calc/index.tsx @@ -18,7 +18,7 @@ import { UpdateField } from '@components/options/ToolOptions'; import { InitialValuesType } from './types'; import type { GenericCalcType } from './data/types'; import type { DataTable } from 'datatables'; -import { getDataTable } from 'datatables'; +import { getDataTable, dataTableLookup } from 'datatables'; import nerdamer from 'nerdamer'; import 'nerdamer/Algebra'; @@ -77,8 +77,6 @@ export default async function makeTool( currentValues: InitialValuesType, updateFieldFunc: UpdateField ) => { - const newValsBoundToPreset = { ...valsBoundToPreset }; - const newPresets = { ...currentValues.presets }; newPresets[selection] = preset; updateFieldFunc('presets', newPresets); @@ -86,7 +84,7 @@ export default async function makeTool( // Clear old selection for (const key in valsBoundToPreset) { if (valsBoundToPreset[key] === selection) { - delete newValsBoundToPreset[key]; + delete valsBoundToPreset[key]; } } @@ -94,13 +92,19 @@ export default async function makeTool( (sel) => sel.title === selection ); - if (preset != '') { + if (preset && preset != '') { if (selectionData) { for (const key in selectionData.bind) { - newValsBoundToPreset[key] = selection; + valsBoundToPreset[key] = selection; + + if (currentValues.outputVariable === key) { + handleSelectedTargetChange('', updateFieldFunc); + } + updateVarField( key, - dataTables[selectionData.source].data[preset][ + + dataTableLookup(dataTables[selectionData.source], preset)[ selectionData.bind[key] ], currentValues, @@ -108,14 +112,11 @@ export default async function makeTool( ); } } else { - setValsBoundToPreset(newValsBoundToPreset); throw new Error( `Preset "${preset}" is not valid for selection "${selection}"` ); } } - - setValsBoundToPreset(newValsBoundToPreset); }; calcData.variables.forEach((variable) => { @@ -138,13 +139,16 @@ export default async function makeTool( if (selection.default == '') return; for (const key in selection.bind) { initialValues.vars[key] = { - value: - dataTables[selection.source].data[selection.default][ - selection.bind[key] - ], - unit: dataTables[selection.source].cols[selection.bind[key]].unit + value: dataTableLookup( + dataTables[selection.source], + selection.default + )[selection.bind[key]], + + unit: + dataTables[selection.source].columns[selection.bind[key]]?.unit || + '' }; - valsBoundToPreset[key] = selection.default; + valsBoundToPreset[key] = selection.title; } }); @@ -182,9 +186,12 @@ export default async function makeTool( disablePortal id="combo-box-demo" value={values.presets[preset.title]} - options={Object.keys( - dataTables[preset.source].data - ).sort()} + options={[ + '', + ...Object.keys( + dataTables[preset.source].data + ).sort() + ]} sx={{ width: 300 }} onChange={(event, newValue) => { handleSelectedPresetChange( @@ -207,7 +214,7 @@ export default async function makeTool( ) }, { - title: 'Input Variables', + title: 'Variables', component: ( @@ -221,7 +228,7 @@ export default async function makeTool( {calcData.variables.map((variable) => ( - {variable.name} + {variable.title} handleSelectedTargetChange( variable.name, diff --git a/src/pages/tools/number/generic-calc/meta.ts b/src/pages/tools/number/generic-calc/meta.ts index f095164..3254deb 100644 --- a/src/pages/tools/number/generic-calc/meta.ts +++ b/src/pages/tools/number/generic-calc/meta.ts @@ -18,7 +18,7 @@ allGenericCalcs.forEach((x) => { tools.push( defineTool('number', { name: x.title, - path: 'generic-calc/x.name', + path: 'generic-calc/' + x.name, icon: '', description: '', shortDescription: '',