# Using ReactionBalance on chemical entities with missing formula

I am currently working on a project (turns out: a bigger project, than originally thought...) with all hypothetically "thinkable" products for a reaction with two given educts. These educts are Entity["Chemical", "AmmoniumPerchlorate"] and Entity["Chemical", "Aluminum"]. Yes, these are the main ingredients of a common solid rocket composite propellant, used in the Space Shuttle booster, for instance.

I gathered all compounds with Al, Cl, H, N, O as involved elements, together with Cl, H, N and O, I have 35 products in total. In a first approach, I restrict myself for a product combination of up to five products, yielding in 384,132 subsets. Further plausibility checks gave approx. 168,000 of such hypothetical reactions.

While I tried some preliminary tests (balancing with the built-in ReactionBalance as well as some basic linear algebra and Nullspace computations on the composition matrix) I saw, Mathematica is unable to declare specific reactions in the first place. Consider the (hypothetical, more precisely: impossible, due to "unbalanceability") reaction:

ChemicalReaction[<|Entity["Chemical", "AmmoniumPerchlorate"] -> 1, Entity["Chemical", "Aluminum"] -> 1|> -> <|Entity["Chemical", "AluminumChloride"] -> 1, Entity["Chemical", "AluminumNitrate"] -> 1, Entity["Chemical", "Diazine"] -> 1|>]


The output:

My first suspect was, Entity["Chemical", "Diazine"] has no Hill formula or Hill formula string, but it has. However: EntityProperty["Chemical", "Formula"] -> Missing["NotAvailable"] and EntityProperty["Chemical", "FormulaString"] -> Missing["NotAvailable"] (part of a PropertyAssociation request). So, I had to add this entity value manually, which sounds easier than it is:

Entity["Chemical", "Diazine"][EntityProperty["Chemical", "Formula"]] = "N2H2"


does not work:

And even if there's a workaround for adding own content to built-in entities, my concern is, that ReactionBalance would fail in any case, due to possible side effects.

How can I use ReactionBalance on chemicals which do not have "Formula" in their corresponding Entity?

(Side note to Jason B.: fantastic work in the chemistry department! Brilliant, and a big step for mathematical chemistry with Mathematica. Makes me feel small...)

• Once again I would just like to point out that StackExchange is a site for questions and answers, not for comments and discussions :) I have slightly edited your post to more clearly emphasize the question and make it properly "answerable". Commented May 2 at 13:24
• I thought the preliminary remarks were useful to clarify the motivation behind my question. Sometimes it is appropriate to anticipate the "why" in order not to provoke workarounds that do not fit the planned workflow. But thank you for your valuable insights. Commented May 2 at 13:37
• Oh, I wasn't referring to your introductory text, but to the final part where you wrote "Comments?" :) Commented May 2 at 13:39

I don't know why "Diazine" is missing its formula, I will update the database and after the next knowledgebase update it should work.

It will work just fine if you convert all the entities into molecules though:

Map[KeyMap[Molecule],
ChemicalReaction[<|Entity["Chemical", "AmmoniumPerchlorate"] -> 1,
Entity["Chemical", "Aluminum"] -> 1|> -> <|
Entity["Chemical", "AluminumChloride"] -> 1,
Entity["Chemical", "AluminumNitrate"] -> 1,
Entity["Chemical", "Diazine"] -> 1|>], {2}]


As a side note - is that right? From searching it looks like the proper spelling is diazene

• Yes, I would prefer diazene too. Commented May 2 at 18:23
• Thanks again Jason, but well, not quite... I had to unflag this as a solution. Entity["Chemical", "Aluminum"] is translated to AlH3 (the Molecule procedure obviously always tries to saturate valences with H, so also in your example). Aluminum hydride is of course not my desired reactant. I will see if I can make do with Chemical Formula for the time being. BTW, FindIsomers[ChemicalFormula["N2H2"], "Entity"] gives no result. Strange. Commented May 2 at 22:40

You can use ChemicalFormula instead of an entity.

ReactionBalance[ChemicalReaction[<|ChemicalFormula["H2"] -> 1,
ChemicalFormula["N2"] -> 1|> -> <|ChemicalFormula["NH3"] -> 1|>]]
(* ChemicalReaction[<|ChemicalFormula[{"H" -> 2}] -> 3,
ChemicalFormula[{"N" -> 2}] -> 1|> -> <|ChemicalFormula[{"N" -> 1, "H" -> 3}] -> 2|>] *)

ReactionBalance[
ChemicalReaction[<|Entity["Chemical", "AmmoniumPerchlorate"] -> 1,
Entity["Chemical", "Aluminum"] -> 1|> -> <|
Entity["Chemical", "AluminumChloride"] -> 1,
Entity["Chemical", "AluminumNitrate"] -> 1,
ChemicalFormula["NH2"] -> 1|>]]
(* ReactionBalance::nosln: The reaction cannot be balanced. *)

• Nice! That should be suitable for the reaction balance calculations. In the next steps, however, I would like to filter out the reactions with a positive sum of enthalpy values in order to allow only exothermic reactions. At this point, I would have to change to entity constructs again (or declare corresponding replacement rules). Commented May 2 at 13:51
• I guess in such case you could store the entities that you had to convert to ChemicalFormula in an association, then get them back from the ChemicalFormula after balancing the reaction ... Commented May 2 at 14:14