# contextReaction.pl - Implements a MathObject class for checmical reactions.

### From WeBWorK

# NAME

contextReaction.pl - Implements a MathObject class for checmical reactions.

# DESCRIPTION

This file implements a Context in which checmical reactions can be specified and compared. Reactions can be composed of sums of integer multiples of elements (possibly with subscripts), separated by a right arrow (indicated by "-->"). Helpful error messages are given when a reaction is not of the correct form. Sums of compounds can be given in any order, but the elements within a compound must be in the order given by the correct answer; e.g., if the correct answer specifies CO_2, then O_2C would be marked incorrect.

To use the context include

loadMacros("contextReaction.pl"); Context("Reaction");

at the top of your PG file, then create `Formula()`

objects for your
reactions. For example:

$R = Formula("4P + 5O_2 --> 2P_2O_5");

Reactions know how to create their own TeX versions (via $R->TeX), and know how to check student answers (via $R->cmp), just like any other MathObject.

The Reaction Context also allows you to create parts of reactions. E.g., you could create

$products = Formula("4CO_2 + 6H_2O");

which you could use in a problem as follows:

loadMacros("contextReaction.pl"); Context("Reaction"); $reactants = Formula("2C_2H_6 + 7O_2"); $products = Formula("4CO_2 + 6H_2O"); Context()->texStrings; BEGIN_TEXT \($reactants \longrightarrow\) \{ans_rule(30)\}. END_TEXT Context()->normalStrings; ANS($products->cmp);

Note that sums and products are not simplified in any way, so that Formula("O + O") and Formula("2O") and Formula("O_2") are all different and unequal in this context.

All the elements of the periodic table are available within the Reaction Context. If you need additional terms, like "Heat" for example, you can add them as variables:

Context()->variables->add(Heat => $context::Reaction::CONSTANT);

Then you can make formulas that include Heat as a term. These "constants" are not allowed to have coefficients or subscripts, and can not be combined with compounds except by addition. If you want a term that can be combined in those ways, use $context::Reaction::ELEMENT instead.