util.py
Go to the documentation of this file.
1 from atomicdata import *
2 
3 elemlist = [ 'H', 'C', 'O', 'N', 'S', 'Cl', 'Ti', 'Sn', 'Ca', 'Na',
4  'Al', 'Fe', 'Si', 'K', 'Mn', 'P', 'Ba' ]
5 
6 printmore = False
7 
8 def tot_weight(compos):
9  weight = 0
10  for element, number in compos.items():
11  weight += number * atomic_weight[element]
12  return weight
13 
14 def tot_fractions(fractions):
15  tions = 0
16  for material, fraction in fractions.items():
17  tions += fraction
18  return tions
19 
20 def elem_frac(compos, elem):
21  if elem in compos:
22  return compos[elem] * atomic_weight[elem] / tot_weight(compos)
23  else:
24  return 0
25 
26 def elem_frac_total(elem, compositions, fractions):
27  frac = 0
28  for material, compos in compositions.items():
29  if material in fractions:
30  frac += elem_frac(compos, elem) * fractions[material]
31  return frac / tot_fractions(fractions)
32 
33 def zovera(elem):
34  # Not quite correct since atomic_weight in amu is only protons +
35  # neutrons up to the mass defect
36  return atomic_number[elem]/atomic_weight[elem]
37 
38 # takes a weight-fraction composition
39 def zoveramaterial(compos):
40  zoverasum = 0
41  for elem in elemlist:
42  if elem in compos:
43  zoverasum += zovera(elem)*compos[elem]
44  return zoverasum
45 
46 def make_composition(name, fractions, compositions):
47  the_composition = { }
48 
49  for elem in elemlist:
50  the_composition[elem] = elem_frac_total(elem, compositions, fractions)
51 
52  return the_composition
53 
54 
55 def print_composition(composition):
56  for elem in elemlist:
57  if elem in composition and composition[elem] > 0:
58  print(" <fraction n=\"{0:.7f}\" ref={1:<10s}/>"
59  .format(composition[elem],
60  "\"{0:s}\"".format(atomic_names[elem]))),
61  if elem == 'Cl':
62  print("<!-- Cl-35: {0:.4f}, Cl-37: {1:.4f} -->".
63  format(composition[elem]*cl35massfrac,
64  composition[elem]*cl37massfrac))
65  else:
66  print("")
67  if printmore:
68  for elem in elemlist:
69  if elem in composition and composition[elem] > 0:
70  print("const double {0:s}_{1:<2s} = {2:f};"
71  .format(name, elem, elem_frac_total(elem, compositions, fractions)))
def elem_frac(compos, elem)
Definition: util.py:20
def print_composition(composition)
Definition: util.py:55
def zovera(elem)
Definition: util.py:33
def tot_fractions(fractions)
Definition: util.py:14
def elem_frac_total(elem, compositions, fractions)
Definition: util.py:26
def tot_weight(compos)
Definition: util.py:8
bool print
def zoveramaterial(compos)
Definition: util.py:39
std::string format(const int32_t &value, const int &ndigits=8)
Definition: HexUtils.cpp:14
def make_composition(name, fractions, compositions)
Definition: util.py:46