summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHernan Grecco <hernan.grecco@gmail.com>2014-10-30 16:56:24 -0300
committerHernan Grecco <hernan.grecco@gmail.com>2014-10-30 16:56:24 -0300
commit89636acb69b788d3f858b454f816ee558724030c (patch)
treef1173b7f941f5dde4602b679e0caeb443756de60
parent800d7fc4811d3cf874cab9009c4af9b21fde47a4 (diff)
parent43dce2faf6b4631bba618e2f2f206e07a75e778e (diff)
downloadpint-89636acb69b788d3f858b454f816ee558724030c.tar.gz
Merge branch 'doc_changes' of git://github.com/rsking84/pint into rsking84-doc_changes
-rw-r--r--docs/contexts.rst14
-rw-r--r--pint/default_en.txt31
2 files changed, 44 insertions, 1 deletions
diff --git a/docs/contexts.rst b/docs/contexts.rst
index 46cbf94..c98e3a5 100644
--- a/docs/contexts.rst
+++ b/docs/contexts.rst
@@ -108,6 +108,15 @@ calculate, for example, the wavelength in water of a laser which on air is 530 n
>>> f.to('nm', 'sp', n=1.33)
<Quantity(398.496240602, 'nanometer')>
+Contexts can also accept Pint Quantity objects as parameters. For example, the 'chemistry'
+context accepts the molecular weight of a substance (as a Quantity with dimensions of
+[mass]/[substance]) to allow conversion between moles and mass.
+
+.. doctest::
+
+ >>> substance = 95 * ureg('g')
+ >>> moles = substance.to('moles', 'chemistry', mw = 5 * ureg('g/mol'))
+ <Quantity(19.0, 'mole')>
Defining contexts in a file
@@ -129,7 +138,8 @@ The `@context` directive indicates the beginning of the transformations which ar
All parameters are named and default values are mandatory. Multiple parameters
are separated by commas (like in a python function definition). Finally, you provide the name
of the context (e.g. spectroscopy) and, optionally, a short version of the name (e.g. sp)
-separated by an equal sign.
+separated by an equal sign. See the definition of the 'chemistry' context in default_en.txt
+for an example of a multiple-parameter context.
Conversions rules are specified by providing source and destination dimensions separated
using a colon (`:`) from the equation. A special variable named `value` will be replaced
@@ -140,6 +150,8 @@ A single forward arrow (`->`) indicates that the equations is used to transform
from the first dimension to the second one. A double arrow (`<->`) is used to
indicate that the transformation operates both ways.
+Context definitions are stored and imported exactly like custom units definition file
+(and can be included in the same file as unit definitions). See "Defining units" for details.
Defining contexts programmatically
----------------------------------
diff --git a/pint/default_en.txt b/pint/default_en.txt
index 631cd7e..1559990 100644
--- a/pint/default_en.txt
+++ b/pint/default_en.txt
@@ -318,3 +318,34 @@ firkin = barrel / 4
[temperature] -> [energy]: boltzmann_constant * value
[energy] -> [temperature]: value / boltzmann_constant
@end
+
+@context(mw=0,volume=0,solvent_mass=0) chemistry = chem
+ # mw is the molecular weight of the species
+ # volume is the volume of the solution
+ # solvent_mass is the mass of solvent in the solution
+
+ # moles -> mass require the molecular weight
+ [substance] -> [mass]: value * mw
+ [mass] -> [substance]: value / mw
+
+ # moles/volume -> mass/volume and moles/mass -> mass / mass
+ # require the molecular weight
+ [substance] / [volume] -> [mass] / [volume]: value * mw
+ [mass] / [volume] -> [substance] / [volume]: value / mw
+ [substance] / [mass] -> [mass] / [mass]: value * mw
+ [mass] / [mass] -> [substance] / [mass]: value / mw
+
+ # moles/volume -> moles requires the solution volume
+ [substance] / [volume] -> [substance]: value * volume
+ [substance] -> [substance] / [volume]: value / volume
+
+ # moles/mass -> moles requires the solvent (usually water) mass
+ [substance] / [mass] -> [substance]: value * solvent_mass
+ [substance] -> [substance] / [mass]: value / solvent_mass
+
+ # moles/mass -> moles/volume require the solvent mass and the volume
+ [substance] / [mass] -> [substance]/[volume]: value * solvent_mass / volume
+ [substance] / [volume] -> [substance] / [mass]: value / solvent_mass *
+volume
+
+@end