Cantera  2.0
Group.cpp
Go to the documentation of this file.
1 /**
2  * @file Group.cpp
3  *
4  * Implementation file for the Group class used in reaction path analysis.
5  */
6 
7 // Copyright 2001 California Institute of Technology
8 
9 // reaction path analysis support
10 
11 #include "cantera/kinetics/Group.h"
12 
13 #include <algorithm>
14 #include <cmath>
15 
16 namespace Cantera
17 {
18 
19 /**
20  * A group is 'valid' if all of its nonzero atom numbers have
21  * the same sign, either positive or negative. This method
22  * checks for this, and if the group is not valid it sets
23  * m_sign to -999, and sets all atom numbers to zero.
24  */
26 {
27 
28  size_t n = m_comp.size();
29 
30  // if already checked and not valid, return
31  if (m_sign == -999) {
32  return;
33  }
34 
35  m_sign = 0;
36  bool ok = true;
37  for (size_t m = 0; m < n; m++) {
38  if (m_comp[m] != 0) {
39  if (m_sign == 0) {
40  m_sign = m_comp[m]/abs(m_comp[m]);
41  } else if (m_sign * m_comp[m] < 0) {
42  ok = false;
43  break;
44  }
45  }
46  }
47  if (!ok) {
48  m_sign = -999;
49  m_comp.resize(n,0);
50  }
51 }
52 
53 std::ostream& Group::fmt(std::ostream& s,
54  const std::vector<std::string>& esymbols) const
55 {
56  s << "(";
57  int nm;
58  bool first = true;
59  size_t n = m_comp.size();
60  for (size_t m = 0; m < n; m++) {
61  nm = m_comp[m];
62  if (nm != 0) {
63  if (!first) {
64  s << "-";
65  }
66  s << esymbols[m];
67  if (nm != 1) {
68  s << nm;
69  }
70  first = false;
71  }
72  }
73  s << ")";
74  return s;
75 }
76 
77 std::ostream& operator<<(std::ostream& s, const Cantera::Group& g)
78 {
79  if (g.valid()) {
80  s << g.m_comp;
81  } else {
82  s << "<none>";
83  }
84  return s;
85 }
86 
87 }