Cantera  2.0
Group.h
Go to the documentation of this file.
1 /**
2  * @file Group.h
3  */
4 
5 // Copyright 2001 California Institute of Technology
6 
7 #ifndef CT_RXNPATH_GROUP
8 #define CT_RXNPATH_GROUP
9 
10 #include "cantera/base/ct_defs.h"
11 
12 #include <iostream>
13 
14 namespace Cantera
15 {
16 
17 /**
18  * Class Group is an internal class used by class ReactionPath. It
19  * represents some subset of the atoms of a molecule.
20  */
21 class Group
22 {
23 public:
24  Group() : m_sign(-999) { }
25  Group(size_t n) : m_sign(0) {
26  m_comp.resize(n,0);
27  }
28  Group(const std::vector<int>& elnumbers) :
29  m_comp(elnumbers), m_sign(0) {
30  validate();
31  }
32  Group(const std::vector<size_t>& elnumbers) :
33  m_comp(elnumbers.size()), m_sign(0) {
34  for (size_t i = 0; i < elnumbers.size(); i++) {
35  m_comp[i] = int(elnumbers[i]);
36  }
37  validate();
38  }
39  Group(const Group& g) :
40  m_comp(g.m_comp), m_sign(g.m_sign) { }
41  Group& operator=(const Group& g) {
42  if (&g != this) {
43  m_comp = g.m_comp;
44  m_sign = g.m_sign;
45  }
46  return *this;
47  }
48  virtual ~Group() {}
49 
50  /**
51  * Decrement the atom numbers by those in group 'other'.
52  */
53  void operator-=(const Group& other) {
54  verifyInputs(*this, other);
55  for (size_t m = 0; m < m_comp.size(); m++) {
56  m_comp[m] -= other.m_comp[m];
57  }
58  validate();
59  }
60  void operator+=(const Group& other) {
61  verifyInputs(*this, other);
62  for (size_t m = 0; m < m_comp.size(); m++) {
63  m_comp[m] += other.m_comp[m];
64  }
65  validate();
66  }
67  void operator*=(int a) {
68  for (size_t m = 0; m < m_comp.size(); m++) {
69  m_comp[m] *= a;
70  }
71  validate();
72  }
73  bool operator==(const Group& other) const {
74  verifyInputs(*this, other);
75  for (size_t m = 0; m < m_comp.size(); m++) {
76  if (m_comp[m] != other.m_comp[m]) {
77  return false;
78  }
79  }
80  return true;
81  }
82  friend Group operator-(const Group& g1, const Group& g2) {
83  verifyInputs(g1, g2);
84  Group diff(g1);
85  diff -= g2;
86  return diff;
87  }
88  friend Group operator+(const Group& g1, const Group& g2) {
89  verifyInputs(g1, g2);
90  Group sum(g1);
91  sum += g2;
92  return sum;
93  }
94  friend void verifyInputs(const Group& g1, const Group& g2) {
95  // if (Debug::on) {
96  // if (g1.size() != g2.size()) {
97  // cerr << "Group: size mismatch!" << std::endl;
98  // cerr << " group 1 = " << g1 << std::endl;
99  // cerr << " group 2 = " << g2 << std::endl;
100  // }
101  // }
102  }
103 
104  void validate();
105 
106  /**
107  * True if all non-zero atom numbers have the same sign.
108  */
109  bool valid() const {
110  return (m_sign != -999);
111  }
112  bool operator!() const {
113  return (m_sign == -999);
114  }
115  int sign() const {
116  return m_sign;
117  }
118  size_t size() const {
119  return m_comp.size();
120  }
121 
122  /// Number of atoms in the group (>= 0)
123  int nAtoms() const {
124  int sum = 0;
125  for (size_t m = 0; m < m_comp.size(); m++) {
126  sum += std::abs(m_comp[m]);
127  }
128  return sum;
129  }
130  /// Number of atoms of element m (positive or negative)
131  int nAtoms(size_t m) const {
132  if (m_comp.empty()) {
133  return 0;
134  }
135  return m_comp[m];
136  }
137 
138  std::ostream& fmt(std::ostream& s, const std::vector<std::string>& esymbols) const;
139 
140  friend std::ostream& operator<<(std::ostream& s,
141  const Group& g);
142 
143 private:
144  std::vector<int> m_comp;
145  int m_sign;
146 };
147 
148 }
149 
150 #endif