Sunday, October 11, 2020

Ideal gas law with dimensional quantities and a physical constant

Maxima is a system for symbolic and numerical computation. Let's use the ezunits package of Maxima to handle dimensional quantities in a problem involving the ideal gas law, pV = nRT.

Inspired by this problem on Stackoverflow: https://stackoverflow.com/questions/64279773/ezunits-not-so-ez-ideal-gas Thanks to user Dux for posing the problem.

In addition to ezunits, we will make use of the physical_constants package, which includes the molar gas constant as %R. Physical constants don't have an ordinary value, so they are carried around as symbols in calculations, until constvalue pulls out the value of the physical constant.

This notebook has been composd in Jupyter using the maxima-jupyter kernel. For more about maxima-jupyter, see: https://github.com/robert-dodier/maxima-jupyter

Problem statement

Fuel cell vehicle, molecular hydrogen fuel. 2 tanks, T = 20°C, p_rel = 700 bar, m_tank = 6 kg. What is the volume of one tank? Solve for the tank volume assuming ideal gas law applies.

Solution
In [1]:
load (ezunits);
Out[1]:
\[\tag{${\it \%o}_{1}$}\mbox{ /home/robert/maxima/maxima-code/share/ezunits/ezunits.mac }\]
In [2]:
load (physical_constants);
Out[2]:
\[\tag{${\it \%o}_{2}$}\mbox{ /home/robert/maxima/maxima-code/share/ezunits/physical\_constants.mac }\]

Inspect value of molar gas constant %R.

In [3]:
%R, constvalue;
Out[3]:
\[\tag{${\it \%o}_{3}$}\frac{1039309}{125000}\;\frac{\mathrm{J}}{\mathrm{mol}\,\mathrm{K}}\]

Declare unit conversion for bars. 1 bar = 100,000 pascals; this is a little less than one standard atmosphere, which is 101,325 pascals.

In [4]:
declare_unit_conversion (1`bar = 100000`Pa);
Out[4]:
\[\tag{${\it \%o}_{4}$}\mathbf{done}\]

Convert from degrees Celsius to kelvins.

In [5]:
T: 20`degC `` K;
Out[5]:
\[\tag{${\it \%o}_{5}$}\frac{5863}{20}\;\mathrm{K}\]
In [6]:
p_rel: 700`bar;
Out[6]:
\[\tag{${\it \%o}_{6}$}700\;\mathrm{bar}\]
In [7]:
m_tank: 6`kg;
Out[7]:
\[\tag{${\it \%o}_{7}$}6\;\mathrm{kg}\]
In [8]:
p: p_rel+1`bar;
Out[8]:
\[\tag{${\it \%o}_{8}$}701\;\mathrm{bar}\]

Convert from bars to newtons per square meter.

In [9]:
p: p``N/m^2;
Out[9]:
\[\tag{${\it \%o}_{9}$}70100000\;\frac{\mathrm{N}}{\mathrm{m}^2}\]

Molar mass of molecular hydrogen.

In [10]:
M_H_2:2*1.01`g/mol;
Out[10]:
\[\tag{${\it \%o}_{10}$}2.02\;\frac{\mathrm{g}}{\mathrm{mol}}\]
In [11]:
R: %R/M_H_2, constvalue;
Out[11]:
\[\tag{${\it \%o}_{11}$}4.116075247524752\;\frac{\mathrm{J}}{\mathrm{g}\,\mathrm{K}}\]

Solve ideal gas law for volume. dimensionally helps other functions handle dimensional quantities. Maxima prefers exact numbers to inexact, so a floating point value is replaced by a rational approximation.

In [12]:
dimensionally (solve (p*V = m_tank*R*T, V));
rat: replaced -7239.764752871287 by -1223527483/169001 = -7239.764752871285
Out[12]:
\[\tag{${\it \%o}_{12}$}\left[ V=\frac{1223527483}{11846970100000}\;\frac{\mathrm{m}^2\,\mathrm{kg}\,\mathrm{J}}{\mathrm{g}\,\mathrm{N}} \right] \]
In [13]:
V: rhs(%[1]);
Out[13]:
\[\tag{${\it \%o}_{13}$}\frac{1223527483}{11846970100000}\;\frac{\mathrm{m}^2\,\mathrm{kg}\,\mathrm{J}}{\mathrm{g}\,\mathrm{N}}\]

V is the total volume of two tanks. Find the volume of one tank.

In [14]:
V_tank: V/2;
Out[14]:
\[\tag{${\it \%o}_{14}$}\frac{1223527483}{23693940200000}\;\frac{\mathrm{m}^2\,\mathrm{kg}\,\mathrm{J}}{\mathrm{g}\,\mathrm{N}}\]

ezunits does not automatically convert units to basic units. Let's express V in its basic units.

In [15]:
fundamental_units (V_tank);
Out[15]:
\[\tag{${\it \%o}_{15}$}m^3\]
In [16]:
V_tank `` fundamental_units(V_tank);
Out[16]:
\[\tag{${\it \%o}_{16}$}\frac{1223527483}{23693940200}\;\mathrm{m}^3\]
In [17]:
float (%);
Out[17]:
\[\tag{${\it \%o}_{17}$}0.05163883561249133\;\mathrm{m}^3\]

Let's express the volume in more convenient units.

In [18]:
V_tank: % `` liter;
Out[18]:
\[\tag{${\it \%o}_{18}$}51.63883561249133\;\mathrm{liter}\]

We don't really need so many digits in the result.

In [19]:
fpprintprec: 4;
Out[19]:
\[\tag{${\it \%o}_{19}$}4\]
In [20]:
V_tank;
Out[20]:
\[\tag{${\it \%o}_{20}$}51.64\;\mathrm{liter}\]

Great, we'll let that be enough for now. Thanks for reading.

PS. This document was created as a Jupyter notebook using the maxima-jupyter kernel. It was converted to HTML via jupyter nbconvert --to html --template basic ezunits_ideal_gas_law_problem.ipynb and then the MathJax preamble was pasted in by hand.

No comments:

Post a Comment