## WeBWorK Problems

### New Physics questions

by Boyd Duffee -
Number of replies: 11
I'd like to author new physics questions and I'm wondering which techniques I should use from the CAPA problems and which should be updated to current best practice. Specifically, I'm after how to add physical constants and new units.

The StdConst.pg file dates from 2003, only holds 9 constants and I can't find usage instructions or examples. Has it been abandoned? I'd like be able to extend it as I did with Astro::Constants on CPAN. Any suggestions?

I'd also like to know how to add more units. I got caught out trying to enter kiloJoules.

many thanks,
Boyd

I'm running (recently installed):
Version 2.4.5
Branch: rel-2-4-patches

### Re: New Physics questions

by Gavin LaRose -
Hi Boyd,

For best practices when writing problems, I'd suggest starting with the sample problems and tips pages at <http://webwork.maa.org/wiki/Category:Authors>. The crib sheet I give my students on units is <http://cvs.webwork.rochester.edu/viewcvs.cgi/NationalProblemLibrary/Michigan/units.html?cvsroot=National+Problem+Library>, and you can find a complete list of the units that are allowed in the WeBWorK system in the Units.pm file, which is in the CVS at <http://cvs.webwork.rochester.edu/viewcvs.cgi/pg/lib/Units.pm?cvsroot=UR+WeBWorK+System>, or as pg/lib/Units.pm in your installation.

I don't see any reason not to add more units. Mike or Davide would probably be the best people to say if it makes sense just to add them to Units.pm or somewhere else.

Gavin

### Re: New Physics questions

by Davide Cervone -
As Gavin points out, the file that defines the units is pg/lib/Units.pm, and it does contain kiloJoules as kJ, which might not be what you want. There is no easy way to add units from within a problem file, but you can add them by editing pg/lib/Units.pm (and restarting the web server afterward).

It would be useful to be able to add units, and it has come up before. I've added it to my list of things to do.

Davide

### Re: New Physics questions

by Alex Jordan -
I've tried the following (just one example) to add units from within a pg file:

package Units;
$known_units{'tsp'} = {'factor' => 0.0000049289, 'm' => 3 };$known_units{'tbsp'} = {'factor' => 0.0000147867,
'm' => 3
};
package main;

This allows me the problem author to create objects like NumberWithUnits(2, 'tsp'). There are no compilation errors, and the object prints fine in the body of the problem. However if I make this kind of thing an answer to a question, and a student enters "2 tsp", then their answer is split as (value, unit) = ("2 tsp", ) rather than ("2", "tsp") and the student gets error messages that 'tsp' is not defined in context.

I don't understand why the mechanism would be able to split the author's "2 tsp" well, but not the student's "2 tsp".

### Re: New Physics questions

by Davide Cervone -
Since you are using MathObjects to handle the answer checking, then I can tell you why.

First, you need to understand the mechanism by which the different components are loaded into WeBWorK. There are two ways this happens: perl modules and perl libraries. The former end in .pm and they live in the pg/lib directory (and its subdirectories), while the latter end in .pl and live in pg/macros. The difference is that the .pm files are loaded at the time a apache server child process starts up (so that they only have to be loaded once rather than each time a problem is processed by WeBWorK), while .pl files are loaded only when a problem asks for them (so are processed separately each time the problem is processed -- i.e., every time a student views the problem or submits an answer).

The Units package is a .pm file, and so are the MathObject NumberWithUnits and FormulaWithUnits packages (indeed most of MathObjects is made of .pm files). The NumberWithUnits package obtains a copy of the known_units at the time is starts up (and uses that to make a regular expression pattern used to identify the units), which is well before your problem runs, and so when you modify the known_units, the NumberWithUnits package doesn't know about that.

Because you create the NumberWithUnits directly with NumberWithUnits(2,'tsp'), there is no question about what the units are and what the number is (and the regular expression isn't used). But when trying to parse the student answer, the regular expression is used, and your tsp is not in that, so it isn't recognized.

There is currently no reasonable way for you to add units on a problem-by-problem basis. The NumberWithUnits object was always intended as a temporary hack to allow units until I made a more native MathObject unit implementation. Unfortunately, that hasn't happened yet (7 or 8 years later).

The difference between a module and a library has another consequence that you might not think of. Since modules are loaded when the child process starts up, they are used over and over by the process for all the WeBWorK problems that it processes. That is, the modules are shared across problems. So if you modify the module, you do so not just for your problem, bur for all the other problems that are processed by the same child process. So it is a BAD IDEA to make the changes to Units are you are doing here. This can lead to unexpected results in other problems (in your case, other problems that use Units will have tsp defined, which could alter their meaning, if they had variables t, s, and p, for example). Since only processes that run your problem will be affected, these issues would be particularly hard to isolate and would generate random-seeming results in other problems. So it is best to leave the perl modules untouched. [This is how things used to work, at least -- a lot has changed with WeBWorK in the last few years, and I haven't tested it, but I think you can still affect the modules globally this way.]

It might be possible to make a subclass of the Unit class to which you add your unit, and a subclass of the NumberWithUnits class, modified to use your new Unit subclass, and to hook that into the context properly to recognize your units, but I haven't looked to see how hard that would be. The real solution is for me to write the native MathObjects unit implementation.

Davide

### Re: New Physics questions

by Alex Jordan -
Oh wow! Thank you for educating me. I'll have our server rebooted now. I knew I could edit Units.pm directly, but I was trying to work in changes so that our problem files could transfer easily to other institutions if they make it to the OPL.

We'll stick with existing units for now. If we do add any to Units.pm, I'll put comments in those problems which use the new units in case the problems make it to the OPL.

Thanks again for your thorough explanation. I feel like you are teaching me to fish. The more I know about all of this (.pm versus .pl) the more able I am to work.

### Re: New Physics questions

by Michael Gage -
Is there some reason that we shouldn't add tsp and tbsp as units to Units.pm?

Would this cause trouble if t and s were being used as variables in a problem?

You could create your version of Units.pm and then submit it via github as a pull request to github.com/openwebwork/pg .   After some public discussion as to the pros and cons of adding these units we could, if it's ok with everyone, update the standard version of Units.pm.  I think it already has some interesting units such as fortnight, why not gallon, cup, quart, teaspoon, tablespoon.

-- Mike

### Re: New Physics questions

by Davide Cervone -
Would this cause trouble if t and s were being used as variables in a problem?

No, if it is in the Units module to start with, it should not cause trouble.

### Re: New Physics questions

by Alex Jordan -
Just to clarify - I only meant for 'tsp' and 'tbsp' to be used as examples. We were more interested in generally being able to add units at will. Along with oddball units like these ones, it would be helpful to add 'foot' and 'feet' alongside 'ft', etc. And it would be helpful to add 'days' alongside 'day', etc.

The Currency context does this well; you can add 'dollar' and 'dollars' for example. But the currency context doesn't have to be capable of inter-unit conversion, which is an important element for the units package to have.

### Re: New Physics questions

by John Jones -
All examples mentioned so far are units suitable for Units.pm.  I think any normal physics units would qualify.

John

### Re: New Physics questions --- singular units

by Dick Lane -
I have no objection to "oddball" units that go beyond SI and Imperial.  On the other hand, I think it is a mistake to confuse linguistic style with dimensional analysis.  I think scientific units should exist with unique symbolic form --- my experience has been that such a form is singular.

I am fine with prose such as
one half hour is 30 minutes
one inch is (approximately) 2.54 centimeters
two meters is/are (approximately) 78.74 inches
I believe the corresponding dimensional statements have the form
1/2 hr = 30 min
1 in = 2.54 cm (approx)
2 m = 78.74 in (approx)
rather than
0.5 hr = 30 mins
1 in = 2.54 cms
2 ms = 78.74 ins
(with or without the approximation quasi-quibble).  Would the following would be allowed?
2.54 cm = 1 ins

If the decision is to proceed with both singular and plural forms, I hope the auxiliary algorithms to handle unit conversion are similarly flexible.  (? If both "dollar" and "dollars" are allowed, is appropriate conversion into "cent" and "cents" being handled ?)