4
$\begingroup$

I love the units system in Mathematica, it's an extremely useful addition to the language. Unfortunately there are still some sharp corners that I run into on a daily basis. For example, if I want to do a stoichiometry calculation, I would like to be able to do the following:

Vol = Quantity[300,"Microliters"]
Concentration = Quantity[2,"Millimolar"]
CdMW = ChemicalData["CadmiumOxide","MolecularWeight"]
mgCd = UnitConvert[Concentration*Vol*CdMW,"Milligrams"]

But this does not work, because "MolecularWeight" reports units of AtomicMassUnit, which Mathematica indicates is incompatible with "Grams"/"Moles", even though in practice (at least for stoichiometry calculations) they are effectively the same.

I have a workaround using the following code:

CdMW = Quantity[QuantityMagnitude[ChemicalData["CadmiumOxide","MolecularWeight]],"Grams"/"Moles"]

But I find this to be quite inelegant, and somewhat less than readable.

Is this the best way to do this, or is there a better approach?

$\endgroup$
1
  • $\begingroup$ I've run into this problem too and each time I just write a helper function as a workaround. Very cumbersome. $\endgroup$
    – Diffycue
    Oct 11, 2022 at 19:50

1 Answer 1

5
$\begingroup$

The "MolarMass" property has the same magnitude but the right unit dimensions for this application:

CdMW = ChemicalData["CadmiumOxide", "MolarMass"]
(* Quantity[128.413, ("Grams")/("Moles")] *)
mgCd = UnitConvert[Concentration*Vol*CdMW, "Milligrams"]
(* Quantity[0.077048, "Milligrams"] *)

As an aside, you can use WolframAlpha in your notebook for some inputs like this. Here the input string "convert 300 microliters 2mM cadmium oxide to milligrams" works: enter image description here

$\endgroup$
4
  • $\begingroup$ I was aware of the WolframAlpha technique, but it's a bit brittle, especially if I want to share the notebook with someone. It also gives some strange results occasionally. Try the following query in WolframAlpha Molar Mass Selenium $\endgroup$ Oct 11, 2022 at 19:45
  • $\begingroup$ You are absolutely right, but we are trying to improve. For the selenium example I agree the "Element" entity should win over the "Chemical" entity with formula Se8. If you see something which seems wrong there you can click the "feedback" button on the bottom and it will go on someone's todo list $\endgroup$
    – Jason B.
    Oct 11, 2022 at 19:56
  • $\begingroup$ @JasonB. Thanks for your work on this, it's a very useful feature. $\endgroup$
    – Diffycue
    Oct 11, 2022 at 20:09
  • $\begingroup$ @JasonB. WolframAlpha is already quite incredible, and I definitely use it for exploratory analysis/finding the correct compound name for something. This may just be my issue, but I don't know of a way to set a variable to the output of a W|A query other than using %ans notation. The feedback button is a useful feature that I wasn't aware of, and will make use of in the future. $\endgroup$ Oct 11, 2022 at 21:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.