WeBWorK Problems

contextReaction and complex compounds

contextReaction and complex compounds

by Eric Stroyan -
Number of replies: 3
contextReaction.pl works very nicely...but I have discovered a bit of a problem when entering compounds with complex ions.
Compounds such as Zn(ClO_{3})_{2} are not liked by the macro. Any suggestions? I have included a short sample problem below the error message generated by fixing the choice to a reaction scheme that triggers the error.
It may also be necessary for contextReaction to have a comma put in line 132 between Pb and Bi. I was having lead containing formulae rejected until I changed my copy.
Thanks for all the help.

Error messages

The left-hand side of '_' must be an element, not a compound; see position 17 of formula at line 395 of [TMPL]/macros/contextReaction.pl Died within context::Reaction::BOP::underscore::_check called at line 35 of [PG]/lib/Parser/BOP.pm from within Parser::BOP::new called at line 494 of [PG]/lib/Parser.pm from within Parser::Precedence called at line 381 of [PG]/lib/Parser.pm from within Parser::Close called at line 145 of [PG]/lib/Parser.pm from within Parser::parse called at line 50 of [PG]/lib/Parser.pm from within Parser::new called at line 19 of [PG]/lib/Value/Formula.pm from within Value::Formula::new called at line 64 of [PG]/macros/Parser.pl from within main::Formula called at line 105 of [TMPL]/chem1l2/predicting_products/pred_prod.pg


The file responsible
DOCUMENT();
#
# Predict products of a chemical reaction.
# Student presented with the (balanced) reactants and asked for products.
# Answer must present balanced products.
# If a reaction does not proceed as written the product is NR.
#
loadMacros(
"PG.pl",
"PGbasicmacros.pl",
"PGchoicemacros.pl",
"PGanswermacros.pl",
"PGauxiliaryFunctions.pl",
"MathObjects.pl",
"Parser.pl",
"PGstandard.pl",
"contextReaction.pl");
#
# Normally this hash would be stored under macros and called above
#
%reactions=(
1=>{equation=>"N_{2} + H_{2} \longrightarrow NH_{3}",
class=>"combination",
components=>[
{type=>"reactant", compound=>"N_{2}", coef=>1 },
{type=>"reactant", compound=>"H_{2}", coef=>3 },
{type=>"product", compound=>"NH_{3}", coef=>2 },]},
2=>{equation=>"Cu + Zn(ClO_{3})_{2} \longrightarrow NR",
class=>"single-replacement",
components=>[
{type=>"reactant", compound=>"Cu", coef=>"" },
{type=>"reactant", compound=>"Pb(ClO_{3})_{2}", coef=>"" },
{type=>"product", compound=>"NR", coef=>"" },]},
3=>{equation=>"CuNO_{3} + CuCl \longrightarrow NR",
class=>"double-replacement",
components=>[
{type=>"reactant", compound=>"CuNO_{3}", coef=>"" },
{type=>"reactant", compound=>"CuCl", coef=>"" },
{type=>"product", compound=>"NR", coef=>"" },]},
);
#
Context("Reaction");
Context()->variables->add(NR => $context::Reaction::CONSTANT);
#
# Pick a reaction from the selected database
#
$number_of_reactions = keys %reactions;
@items=(1..$number_of_reactions);
$S = list_random(@items);
#
# Get skeleton equation, type of reaction, then get
# info about coefficients and calculate formula masses
#
$equation = $reactions{$S}{equation};
$type = $reactions{$S}{class};
$i=0;
$j=0;
for $who (@{$reactions{$S}{components}})
{
$kind=$who->{type};
if($kind eq "reactant")
{$n_r =$i;
$react[$i] = $who->{compound};
$react_coef[$i]=$who->{coef};
$i=$i+1;
}
if($kind eq "product")
{$n_p=$j;
$prod[$j] = $who->{compound};
$prod_coef[$j]=$who->{coef};
$j=$j+1;
}
}
#
# Build reactants and products sides of equation
#
$reactants = '';
for ($i=0;$i<=$n_r;$i++)
{ if($i < $n_r)
{$and = ' + ';}
if($i ==$n_r)
{$and = '';}
$r_c = '';
if($react_coef[$i] != 1)
{$r_c = $react_coef[$i]}
$reactants = $reactants.$r_c.$react[$i].$and;
}
$products = '';
for ($i=0;$i<=$n_p;$i++)
{ if($i < $n_p)
{$and = ' + ';}
if($i ==$n_p)
{$and = '';}
$p_c = '';
if($prod_coef[$i] != 1)
{$p_c = $prod_coef[$i]}
$products = $products.$p_c.$prod[$i].$and;
}
#
$react = Formula($reactants);
$prod = Formula($products);
#
Context()->texStrings;
BEGIN_TEXT
For the following problem your answer must be entered in LaTeX format.
as discussed in lecture.
$BR
Predict the products for the $type reaction shown.$BR
If a combination does not react the products should be input as NR.$BR
\($react \longrightarrow\) \{ans_rule(30)\}.
END_TEXT
Context()->normalStrings;
#
ANS($prod->cmp);
ENDDOCUMENT();
In reply to Eric Stroyan

Re: contextReaction and complex compounds

by Davide Cervone -
Eric:

I have updated the contextReaction.pl file to allow subscripts on compounds as well as elements. (But not on molecules; should that be allowed, too? I.e., do you want (O_2)_3?)

I'm not currently set up to update the SVN repository, so I'm attaching the updated file here. I corrected the problem with Pb as well. Thanks for the catch.

Mike, perhaps you can update the SVN version? Thanks.

Davide