diff --git a/gui/src/components/units/Quantity.spec.js b/gui/src/components/units/Quantity.spec.js index cd278f551c83904cef2d91b0399bfe5062d9105d..ec8894d313f600d47f76b710de79642560b2ea34 100644 --- a/gui/src/components/units/Quantity.spec.js +++ b/gui/src/components/units/Quantity.spec.js @@ -76,6 +76,7 @@ test.each([ ['do not convert to base', 'eV', {energy: {definition: 'joule'}}, 1, 1.602176634e-19], ['combination', 'a_u_force * angstrom', {force: {definition: 'newton'}, length: {definition: 'meter'}}, 1, 8.23872349823899e-18], ['use base units if derived unit not defined in system', 'newton * meter', {mass: {definition: 'kilogram'}, time: {definition: 'second'}, length: {definition: 'meter'}}, 1, 1], + ['system contains base unit with prefix', 'm^3', {length: {definition: 'cm'}}, 0.001, 1000], ['unit definition with prefix', 'kg^2', {mass: {definition: 'mg'}}, 1, 1e12], ['expression as definition', 'N', {force: {definition: '(kg m) / s^2'}}, 1, 1], ['delta inherited for single base unit', 'delta_celsius', {temperature: {definition: 'K'}}, 1, 1], diff --git a/gui/src/components/units/Unit.js b/gui/src/components/units/Unit.js index 58ab8e6810c21d5483b771c3ec3fc8eea2c9e087..2e9bb6f9449de0031b91a2ddf6f48a6bd933743a 100644 --- a/gui/src/components/units/Unit.js +++ b/gui/src/components/units/Unit.js @@ -339,8 +339,26 @@ export class Unit { const baseDim = BASE_DIMENSIONS[i] if (Math.abs(newDimensions[i] || 0) > 1e-12) { if (has(system, baseDim)) { + let unitDef + const baseUnitDefinition = system[baseDim].definition + // First try to look up the unit in the list of known units: + // most likely it is already there. If not found, then we need + // to try and parse the unit definition: this happens e.g. when + // units with prefixes are used as base units in the unit + // system. + unitDef = UNITS[system[baseDim]?.definition] + if (!unitDef) { + try { + const baseUnit = new Unit(baseUnitDefinition) + const baseUnitList = baseUnit.mathjsUnit.units + if (baseUnitList.length === 1) unitDef = baseUnitList[0].unit + } catch (e) {} + } + if (!unitDef) { + throw Error(`Could not find base unit for "${system[baseDim].definition}"`) + } proposedUnitList.push({ - unit: UNITS[system[baseDim].definition], + unit: unitDef, prefix: PREFIXES.NONE[''], power: unit.power ? newDimensions[i] * unit.power : 0, delta: unit.delta diff --git a/gui/src/components/units/Unit.spec.js b/gui/src/components/units/Unit.spec.js index b79b0868e7598e3f8c3a32ea4dd84188e1d3bbc1..27bf841cac08d0dde2947d13f897a10c040dc768 100644 --- a/gui/src/components/units/Unit.spec.js +++ b/gui/src/components/units/Unit.spec.js @@ -115,6 +115,7 @@ test.each([ ['do not convert to base', 'eV', {energy: {definition: 'joule'}}, 'J'], ['combination', 'a_u_force * angstrom', {force: {definition: 'newton'}, length: {definition: 'meter'}}, 'N m'], ['use base units if derived unit not defined in system', 'newton * meter', {mass: {definition: 'kilogram'}, time: {definition: 'second'}, length: {definition: 'meter'}}, '(kg m m) / s^2'], + ['system contains base unit with prefix', 'm^3', {length: {definition: 'cm'}}, 'cm^3'], ['unit definition with prefix', 'kg^2', {mass: {definition: 'mg'}}, 'mg^2'], ['expression as definition', 'N', {force: {definition: '(kg m) / s^2'}}, '(kg m) / s^2'], ['delta inherited for single base unit', 'delta_celsius', {temperature: {definition: 'K'}}, 'ΔK'],