Hello,
After having spent many years authoring problems for some electrical engineering courses I have taught, I decided it was time to try and make the process easier and better looking (so immediately, you probably know that I so far have accomplished neither of those goals!).
There are a number of functions and constants that I use in these courses (I will focus on the quantum mechanics course I am writing problems for currently) which I don't wish to redefine for each and every problem. Therefore I figured this would be the perfect time to try making my own specialized context, which I titled 'Quantum'. I will show the 'contextQuantum.pl' file I created and show a test problem below.
I put the context file in the macros directory for my course and you can see that I load it and call it in the problem without that generating any errors. Though I did note that even if the file or context is spelled wrong, webwork does not throw any errors, so that isn't saying much by itself.
Now if I try to use a constant, say h_bar, directly in the problem, for example:
$test = h_bar;
Printing $test in the problem text just displays "h_bar" and not the value. But if I use Compute(h_bar); then the number comes through. But the built in constants, such as "pi" work just fine as one would expect.
Similar issue for functions, they only evaluate when called within functionParser(). I put comments next to the different scenarios I tried and what errors I got when processing the test.pg file
Any guidance would be greatly appreciated. Though there is ample documentation on writing your own contexts, there doesn't seem to be much out there on having this reside in it's own contextName.pl file, or not much that I could find. Github is a nice resource to see how others treat this, but I found a wide variation in styles, which makes it hard to figure out what is or isn't necessary to get this to work. And apologies in advance for rookie Perl mistakes; this is my first time diving this deep into it!
Here is the test.pg file I am using:
## DBsubject('Quantum Mechanics')
## DBchapter('Exam')
## DBsection('Wavefunctions')
## Date('03/18/2014')
## Author('Joel Therrien')
## Institution('UML')
#############################################
# Initialization
DOCUMENT();
loadMacros(
"PGstandard.pl",
"MathObjects.pl",
"AnswerFormatHelp.pl",
"PGgraphmacros.pl",
"unionTables.pl",
"parserFunction.pl",
"contextQuantum.pl"
);
TEXT(beginproblem());
$refreshCachedImages = 1;
##############################################
# Setup
Context("Quantum");
$tst = context::Quantum::Tunneling(1,2); # This one works just fine
# $tst = Compute(Tunneling(1,2)); # This one throws the following error: Undefined subroutine &main::Tunneling called at line 35 of (eval 2857)
parserFunction( "fTest(t)" => "context::Quantum::Tunneling(1,t)"); #This one throws the following error: 'context' is not defined in this context;
parserFunction( "fTest(t)" => "Tunneling(1,t)"); # This one throws the following error: Can't locate object method "weaken" via package "context::Quantum::Tunneling"
$tst2 = fTest(2);
#############################################
# Main text
Context()->texStrings;
BEGIN_TEXT
test
Test 1 = $tst
test 2 = $tst2
END_TEXT
Context()->normalStrings;
###############################################
# Answer evaluation
$showPartialCorrectAnswers = 1;
############################
# Solution
ENDDOCUMENT();
Here is the contextQuantum.pl file:
##########################################################################
#
# Hopefully this will become a library of quantum functions and constants
#
loadMacros("MathObjects.pl");
my $h = 6.6E-34; #Planck's constant
my $h_bar = 1.05E-34; #Planck's reduced constant
my $m_e = 9.11E-31; #electron rest mass
sub _contextQuantum_init {
my $context = $main::context{Quantum} = Parser::Context->getCopy("Numeric");
$context->flags->set(
zeroLevel => 1E-50,
zeroLevelTol => 1E-50
);
$context->constants->add('h' => {value => $h, TeX => '\boldsymbol{h}'},
'hbar' => {value => $h_bar, Tex => '\boldsymbol{\hbar}'},
'me' => {value => $m_e, TeX => '\boldsymbol{m_{e}}'}
);
$context->functions->add(
Tunneling => {class => 'context::Quantum::Tunneling'}
);
}
package context::Quantum;
our @ISA = qw(Parser::Function::numeric2);
sub Tunneling {
my ($n,$r) = @_; my $T = 1;
$T=2.3*$n;
return $T
}
package main;
1;
~~~~