Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add aliases for Angstrom to Units.pm #762

Merged
merged 6 commits into from
Feb 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions htdocs/helpFiles/Entering-Units.html
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ <h2 style="text-align: center">Units Available in WeBWorK</h2>
</tr>
<tr>
<td style="padding: 0.5rem; border: 1px solid black">Angstroms</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">A</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">angstrom</td>
</tr>
<tr>
<td style="padding: 0.5rem; border: 1px solid black">Light years</td>
Expand Down Expand Up @@ -171,7 +171,7 @@ <h2 style="text-align: center">Units Available in WeBWorK</h2>
</tr>
<tr>
<td style="padding: 0.5rem; border: 1px solid black">Amperes</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">amp</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">A</td>
</tr>
<tr>
<td style="padding: 0.5rem; border: 1px solid black">Moles</td>
Expand Down
4 changes: 2 additions & 2 deletions htdocs/helpFiles/Units.html
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ <h2 style="text-align: center">Units Available in WeBWorK</h2>
</tr>
<tr>
<td style="padding: 0.5rem; border: 1px solid black">Angstroms</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">A</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">angstrom</td>
</tr>
<tr>
<td style="padding: 0.5rem; border: 1px solid black">Light years</td>
Expand Down Expand Up @@ -171,7 +171,7 @@ <h2 style="text-align: center">Units Available in WeBWorK</h2>
</tr>
<tr>
<td style="padding: 0.5rem; border: 1px solid black">Amperes</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">amp</td>
<td style="padding: 0.5rem; border: 1px solid black; text-align: center">A</td>
</tr>
<tr>
<td style="padding: 0.5rem; border: 1px solid black">Moles</td>
Expand Down
23 changes: 17 additions & 6 deletions lib/Units.pm
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ sub evaluate_units {

# Methods for evaluating units in answers
package Units;
use utf8;

#require Exporter;
#@ISA = qw(Exporter);
Expand Down Expand Up @@ -223,7 +224,9 @@ our %known_units = (
# micron -- micrometer
# um -- micrometer
# nm -- nanometer
# A -- Angstrom
# angstrom -- angstrom
# Angstrom -- angstrom
# Å -- angstrom
# pm -- picometer
# fm -- femtometer
#
Expand Down Expand Up @@ -251,7 +254,15 @@ our %known_units = (
'factor' => 1E-9,
'm' => 1
},
'A' => {
'angstrom' => {
'factor' => 1E-10,
'm' => 1
},
'Angstrom' => {
'factor' => 1E-10,
'm' => 1
},
'Å' => { # unicode "\x{00C5}"
'factor' => 1E-10,
'm' => 1
},
Expand Down Expand Up @@ -712,8 +723,8 @@ our %known_units = (
# kohm -- kilo-ohm
# Mohm -- mega-ohm
# S -- siemens
# A -- ampere
# mA -- milli-ampere
# mamp -- milli-ampere
#
'C' => {
'factor' => 1,
Expand Down Expand Up @@ -812,11 +823,11 @@ our %known_units = (
'amp' => 2,
's' => 3,
},
'mA' => { # milliampere
'factor' => 0.001,
'A' => { # ampere
'factor' => 1,
'amp' => 1,
},
'mamp' => {
'mA' => { # milliampere
'factor' => 0.001,
'amp' => 1,
},
Expand Down
2 changes: 1 addition & 1 deletion macros/answers/AnswerFormatHelp.pl
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,7 @@ sub AnswerFormatHelp {
OpenWindow.document.write("<title>Abbreviations for Units</title>")
OpenWindow.document.write("<body bgcolor='#ffffff'>")
OpenWindow.document.write("<center><h2>Abbreviations for Units</h2></center>")
OpenWindow.document.write("<ul><li>Some WeBWorK problems ask for answers with units. Below is a list of basic units and how they need to be abbreviated in WeBWorK answers. In some problems, youmay need to combine units (e.g, velocity might be in <code>ft/s</code> for feet per second).</li></ul><br /><br /><center><table border='1' cellpadding='4' cellspacing='0'><tr><th>Unit</th><th>Abbreviation</th></tr><tr><td colspan='2' align='center'><b>Time</b></td></tr><tr><td> Seconds </td><td align='center'> s</td></tr><tr><td> Minutes </td><td align='center'> min</td></tr><tr><td> Hours </td><td align='center'> hr</td></tr><tr><td> Days </td><td align='center'> day</td></tr><tr><td> Years </td><td align='center'> year</td></tr><tr><td> Milliseconds </td><td align='center'> ms</td></tr><tr><td colspan='2' align='center'><b>Distance</b></td></tr><tr><td> Feet </td><td align='center'> ft</td></tr><tr><td> Inches </td><td align='center'> in</td></tr><tr><td> Miles </td><td align='center'> mi</td></tr><tr><td> Meters </td><td align='center'> m</td></tr><tr><td> Centimeters </td><td align='center'> cm</td></tr><tr><td> Millimeters </td><td align='center'> mm</td></tr><tr><td> Kilometers </td><td align='center'> km</td></tr><tr><td> Angstroms </td><td align='center'> A</td></tr><tr><td> Light years </td><td align='center'> light-year</td></tr><tr><td colspan='2' align='center'><b>Mass</b></td></tr><tr><td> Grams </td><td align='center'> g</td></tr><tr><td> Kilograms </td><td align='center'> kg</td></tr><tr><td> Slugs </td><td align='center'> slug</td></tr><tr><td colspan='2' align='center'><b>Volume</b></td></tr><tr><td> Liters </td><td align='center'> L</td></tr><tr><td> Cubic Centimeters </td><td align='center'> cc</td></tr><tr><td> Milliliters </td><td align='center'> ml</td></tr><tr><td colspan='2' align='center'><b>Force</b></td></tr><tr><td> Newtons </td><td align='center'> N</td></tr><tr><td> Dynes </td><td align='center'> dyne</td></tr><tr><td> Pounds </td><td align='center'> lb</td></tr><tr><td> Tons </td><td align='center'> ton</td></tr><tr><td colspan='2' align='center'><b>Work/Energy</b></td></tr><tr><td> Joules </td><td align='center'> J</td></tr><tr><td> kilo Joule </td><td align='center'> kJ</td></tr><tr><td> ergs </td><td align='center'> erg</td></tr><tr><td> foot pounds </td><td align='center'> lbf</td></tr><tr><td> calories </td><td align='center'> cal</td></tr><tr><td> kilo calories </td><td align='center'> kcal</td></tr><tr><td> electron volts </td><td align='center'> eV</td></tr><tr><td> kilo Watt hours </td><td align='center'> kWh</td></tr><tr><td colspan='2' align='center'><b>Misc</b></td></tr><tr><td> Amperes </td><td align='center'> amp</td></tr><tr><td> Moles </td><td align='center'> mol</td></tr><tr><td> Degrees Centrigrade </td><td align='center'> degC</td></tr><tr><td> Degrees Fahrenheit </td><td align='center'> degF</td></tr><tr><td> Degrees Kelvin </td><td align='center'> degK</td></tr><tr><td> Angle degrees </td><td align='center'> deg</td></tr><tr><td> Angle radians </td><td align='center'> rad</td></tr></table></center><br /><br />")
OpenWindow.document.write("<ul><li>Some WeBWorK problems ask for answers with units. Below is a list of basic units and how they need to be abbreviated in WeBWorK answers. In some problems, youmay need to combine units (e.g, velocity might be in <code>ft/s</code> for feet per second).</li></ul><br /><br /><center><table border='1' cellpadding='4' cellspacing='0'><tr><th>Unit</th><th>Abbreviation</th></tr><tr><td colspan='2' align='center'><b>Time</b></td></tr><tr><td> Seconds </td><td align='center'> s</td></tr><tr><td> Minutes </td><td align='center'> min</td></tr><tr><td> Hours </td><td align='center'> hr</td></tr><tr><td> Days </td><td align='center'> day</td></tr><tr><td> Years </td><td align='center'> year</td></tr><tr><td> Milliseconds </td><td align='center'> ms</td></tr><tr><td colspan='2' align='center'><b>Distance</b></td></tr><tr><td> Feet </td><td align='center'> ft</td></tr><tr><td> Inches </td><td align='center'> in</td></tr><tr><td> Miles </td><td align='center'> mi</td></tr><tr><td> Meters </td><td align='center'> m</td></tr><tr><td> Centimeters </td><td align='center'> cm</td></tr><tr><td> Millimeters </td><td align='center'> mm</td></tr><tr><td> Kilometers </td><td align='center'> km</td></tr><tr><td> Angstroms </td><td align='center'> angstrom</td></tr><tr><td> Light years </td><td align='center'> light-year</td></tr><tr><td colspan='2' align='center'><b>Mass</b></td></tr><tr><td> Grams </td><td align='center'> g</td></tr><tr><td> Kilograms </td><td align='center'> kg</td></tr><tr><td> Slugs </td><td align='center'> slug</td></tr><tr><td colspan='2' align='center'><b>Volume</b></td></tr><tr><td> Liters </td><td align='center'> L</td></tr><tr><td> Cubic Centimeters </td><td align='center'> cc</td></tr><tr><td> Milliliters </td><td align='center'> ml</td></tr><tr><td colspan='2' align='center'><b>Force</b></td></tr><tr><td> Newtons </td><td align='center'> N</td></tr><tr><td> Dynes </td><td align='center'> dyne</td></tr><tr><td> Pounds </td><td align='center'> lb</td></tr><tr><td> Tons </td><td align='center'> ton</td></tr><tr><td colspan='2' align='center'><b>Work/Energy</b></td></tr><tr><td> Joules </td><td align='center'> J</td></tr><tr><td> kilo Joule </td><td align='center'> kJ</td></tr><tr><td> ergs </td><td align='center'> erg</td></tr><tr><td> foot pounds </td><td align='center'> lbf</td></tr><tr><td> calories </td><td align='center'> cal</td></tr><tr><td> kilo calories </td><td align='center'> kcal</td></tr><tr><td> electron volts </td><td align='center'> eV</td></tr><tr><td> kilo Watt hours </td><td align='center'> kWh</td></tr><tr><td colspan='2' align='center'><b>Misc</b></td></tr><tr><td> Amperes </td><td align='center'> A</td></tr><tr><td> Moles </td><td align='center'> mol</td></tr><tr><td> Degrees Centrigrade </td><td align='center'> degC</td></tr><tr><td> Degrees Fahrenheit </td><td align='center'> degF</td></tr><tr><td> Degrees Kelvin </td><td align='center'> degK</td></tr><tr><td> Angle degrees </td><td align='center'> deg</td></tr><tr><td> Angle radians </td><td align='center'> rad</td></tr></table></center><br /><br />")
OpenWindow.document.write("</body>")
OpenWindow.document.write("</html>")
OpenWindow.document.close()
Expand Down
22 changes: 11 additions & 11 deletions t/units/basic_parser.t
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ subtest 'Check some known units' => sub {
ok my @unit_names = (split /\|/, $joule->getUnitNames), 'Can getUnitNames';

is \@unit_names, bag {
all_items(match qr/^[-%\w]+$/);
all_items(match qr/^(?:[-%\w]+|\p{Lu})$/);
item 'J';
item 'N';
item 'm';
Expand Down Expand Up @@ -174,26 +174,26 @@ subtest 'Check possible answer format branches' => sub {
subtest 'Check possible answers compared to the number 0.005 T' => sub {
my $correct = NumberWithUnits('0.005 T');

is check_score($correct, '0.005 T'), 1, '0.005 T is correct';
is check_score($correct, '5*10^-13 T*m/A'), 1, '5*10^-13 T*m/A is correct';
is check_score($correct, '0 T*m/A'), 0, '0 T*m/A is incorrect';
is check_score($correct, '0.005 T'), 1, '0.005 T is correct';
is check_score($correct, '5*10^-13 T*m/angstrom'), 1, '5*10^-13 T*m/angstrom is correct';
is check_score($correct, '0 T*m/angstrom'), 0, '0 T*m/angstrom is incorrect';
};

subtest 'Check possible answers compared to the formula 0.005 T' => sub {
my $correct = FormulaWithUnits('0.005 T');

is check_score($correct, '0.005 T'), 1, '0.005 T is correct';
is check_score($correct, '5*10^-13 T*m/A'), 1, '5*10^-13 T*m/A is correct';
is check_score($correct, '0 T*m/A'), 0, '0 T*m/A is incorrect';
is check_score($correct, '0.005 T'), 1, '0.005 T is correct';
is check_score($correct, '5*10^-13 T*m/angstrom'), 1, '5*10^-13 T*m/angstrom is correct';
is check_score($correct, '0 T*m/angstrom'), 0, '0 T*m/angstrom is incorrect';
};

subtest 'Check possible answers compared to the formula 0.009 x^2 T' => sub {
my $correct = FormulaWithUnits('0.009 x^2 T');

is check_score($correct, '0.009 x^2 T'), 1, '0.009 x^2 T is correct';
is check_score($correct, '9*10^-13 x^2 T*m/A'), 1, '9*10^-13 x^2 T*m/A is correct';
is check_score($correct, '0 T*m/A'), 0, '0 T*m/A is incorrect';
is check_score($correct, '0 x^2 T*m/A'), 0, '0 x^2 T*m/A is incorrect';
is check_score($correct, '0.009 x^2 T'), 1, '0.009 x^2 T is correct';
is check_score($correct, '9*10^-13 x^2 T*m/angstrom'), 1, '9*10^-13 x^2 T*m/angstrom is correct';
is check_score($correct, '0 T*m/angstrom'), 0, '0 T*m/angstrom is incorrect';
is check_score($correct, '0 x^2 T*m/angstrom'), 0, '0 x^2 T*m/angstrom is incorrect';
};

subtest 'Check possible answers compared to the the number 1 amu' => sub {
Expand Down
17 changes: 11 additions & 6 deletions t/units/conversions.t
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ subtest 'Check fundamental units' => sub {
is \%Units::known_units, hash {
field m => { factor => 1, m => 1 };

all_keys match qr/^(?:[a-z02]+(?:-\w+)?|%)$/i;
all_keys match qr/^(?:[a-z02]+(?:-\w+)?|%|\p{Lu})$/i;
all_vals hash {
field factor => !number(0);

Expand All @@ -49,9 +49,11 @@ subtest 'Check base units' => sub {
is { evaluate_units('V') }, in_base_units(amp => -1, s => -3, kg => 1, m => 2, factor => 1), 'Volt';
is { evaluate_units('J*s') }, in_base_units(kg => 1, m => 2, s => -1, factor => 1), 'Joule-seconds';

is { evaluate_units('V/m') }, in_base_units(kg => 1, m => 1, s => -3, amp => -1, factor => 1),
is { evaluate_units('V/m') },
in_base_units(kg => 1, m => 1, s => -3, amp => -1, factor => 1),
'Volts per metre';
is { evaluate_units('N/C') }, in_base_units(kg => 1, m => 1, s => -3, amp => -1, factor => 1),
is { evaluate_units('N/C') },
in_base_units(kg => 1, m => 1, s => -3, amp => -1, factor => 1),
'Newtons per Coulomb';
};

Expand Down Expand Up @@ -98,13 +100,15 @@ subtest 'Check a collection of units' => sub {
is multiply_by(1e9, evaluate_units('Pa')), { evaluate_units('GPa') }, 'gigapascal conversion';
is multiply_by(1000, evaluate_units('kPa')), { evaluate_units('MPa') }, 'kilopascal conversion';

is multiply_by(2 * 1000 * $Units::PI, evaluate_units('rad/s')), { evaluate_units('kHz') },
is multiply_by(2 * 1000 * $Units::PI, evaluate_units('rad/s')),
{ evaluate_units('kHz') },
'kilohertz conversion';

is multiply_by(0.01, %Units::fundamental_units), { evaluate_units('%') }, 'percent conversion';

my $todo = todo 'use within() to fudge factor in 9th decimal place';
is multiply_by((180 / $Units::PI)**2, evaluate_units('deg^2')), { evaluate_units('sr') },
is multiply_by((180 / $Units::PI)**2, evaluate_units('deg^2')),
{ evaluate_units('sr') },
'solid angle conversion';
};

Expand All @@ -115,7 +119,8 @@ subtest 'Check astronomical units' => sub {
is { evaluate_units('c*yr') }, { evaluate_units('light-year') }, 'light year';

my $todo = todo 'use within() to fudge factor in 9th decimal place';
is multiply_by(cos($second_arc) / sin($second_arc), evaluate_units('AU')), { evaluate_units('parsec') },
is multiply_by(cos($second_arc) / sin($second_arc), evaluate_units('AU')),
{ evaluate_units('parsec') },
'parsec conversion';
};

Expand Down
10 changes: 8 additions & 2 deletions t/units/electromagnetic.t
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ my $watt = NumberWithUnits(1, 'W');
my $milliwatt = NumberWithUnits(1E3, 'mW');
my $megawatt = NumberWithUnits(1E-6, 'MW');

my $amp = NumberWithUnits(1, 'amp');
my $milliamp = NumberWithUnits(1, 'mamp');
my $amp = NumberWithUnits(1, 'amp');
my $ampere = NumberWithUnits(1, 'A');
my $milliamp = NumberWithUnits(1000, 'mA');

my $tesla = NumberWithUnits(1, 'T');
my $millitesla = NumberWithUnits(1000, 'mT');
Expand All @@ -41,6 +42,11 @@ subtest 'LaTeX output' => sub {
is $microcoulomb->TeX, '1\times 10^{6}\ {\rm \mu C}', 'LaTeX output for micrometers';
};

subtest 'Current' => sub {
is check_score($amp, $ampere), 1, '1 amp is 1 ampere';
is check_score($milliamp, $ampere), 1, '1000 milliamps is 1 ampere';
};

subtest 'Charge' => sub {
is check_score($millicoulomb, $coulomb), 1, '1000 millicoulombs is 1 coulomb';
is check_score($nanocoulomb, $coulomb), 1, '10^9 nanocoulombs is 1 coulomb';
Expand Down
2 changes: 2 additions & 0 deletions t/units/length.t
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ loadMacros('parserNumberWithUnits.pl');
my $micron = NumberWithUnits(1, 'um');
my $picometer = NumberWithUnits(1E6, 'pm');
my $femtometer = NumberWithUnits(1E9, 'fm');
my $angstrom = NumberWithUnits(1E4, 'angstrom');

subtest 'LaTeX output' => sub {
is $picometer->TeX, '1\times 10^{6}\ {\rm pm}', 'LaTeX output for 1E6 picometers';
Expand All @@ -26,6 +27,7 @@ subtest 'LaTeX output' => sub {
subtest 'Equivalent to micrometer' => sub {
is check_score($picometer, $micron), 1, '1 micrometer in picometers';
is check_score($femtometer, $micron), 1, '1 micrometer in femtometers';
is check_score($angstrom, $micron), 1, '1 micrometer in picometers';
};

done_testing();