Cantera  2.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 namespace Cantera
13 {
14 
15 /**
16  * Class Group is an internal class used by class ReactionPath. It
17  * represents some subset of the atoms of a molecule.
18  */
19 class Group
20 {
21 public:
22  Group() : m_sign(-999) { }
23  Group(size_t n) : m_sign(0) {
24  m_comp.resize(n,0);
25  }
26  Group(const std::vector<int>& elnumbers) :
27  m_comp(elnumbers), m_sign(0) {
28  validate();
29  }
30  Group(const std::vector<size_t>& elnumbers) :
31  m_comp(elnumbers.size()), m_sign(0) {
32  for (size_t i = 0; i < elnumbers.size(); i++) {
33  m_comp[i] = int(elnumbers[i]);
34  }
35  validate();
36  }
37  Group(const Group& g) :
38  m_comp(g.m_comp), m_sign(g.m_sign) { }
39  Group& operator=(const Group& g) {
40  if (&g != this) {
41  m_comp = g.m_comp;
42  m_sign = g.m_sign;
43  }
44  return *this;
45  }
46  virtual ~Group() {}
47 
48  /**
49  * Decrement the atom numbers by those in group 'other'.
50  */
51  void operator-=(const Group& other) {
52  verifyInputs(*this, other);
53  for (size_t m = 0; m < m_comp.size(); m++) {
54  m_comp[m] -= other.m_comp[m];
55  }
56  validate();
57  }
58  void operator+=(const Group& other) {
59  verifyInputs(*this, other);
60  for (size_t m = 0; m < m_comp.size(); m++) {
61  m_comp[m] += other.m_comp[m];
62  }
63  validate();
64  }
65  void operator*=(int a) {
66  for (size_t m = 0; m < m_comp.size(); m++) {
67  m_comp[m] *= a;
68  }
69  validate();
70  }
71  bool operator==(const Group& other) const {
72  verifyInputs(*this, other);
73  for (size_t m = 0; m < m_comp.size(); m++) {
74  if (m_comp[m] != other.m_comp[m]) {
75  return false;
76  }
77  }
78  return true;
79  }
80  friend Group operator-(const Group& g1, const Group& g2) {
81  verifyInputs(g1, g2);
82  Group diff(g1);
83  diff -= g2;
84  return diff;
85  }
86  friend Group operator+(const Group& g1, const Group& g2) {
87  verifyInputs(g1, g2);
88  Group sum(g1);
89  sum += g2;
90  return sum;
91  }
92  friend void verifyInputs(const Group& g1, const Group& g2) {
93  // if (Debug::on) {
94  // if (g1.size() != g2.size()) {
95  // cerr << "Group: size mismatch!" << std::endl;
96  // cerr << " group 1 = " << g1 << std::endl;
97  // cerr << " group 2 = " << g2 << std::endl;
98  // }
99  // }
100  }
101 
102  /*!
103  * A group is 'valid' if all of its nonzero atom numbers have
104  * the same sign, either positive or negative. This method
105  * checks for this, and if the group is not valid it sets
106  * m_sign to -999, and sets all atom numbers to zero.
107  */
108  void validate();
109 
110  /**
111  * True if all non-zero atom numbers have the same sign.
112  */
113  bool valid() const {
114  return (m_sign != -999);
115  }
116  bool operator!() const {
117  return (m_sign == -999);
118  }
119  int sign() const {
120  return m_sign;
121  }
122  size_t size() const {
123  return m_comp.size();
124  }
125 
126  /// Number of atoms in the group (>= 0)
127  int nAtoms() const {
128  int sum = 0;
129  for (size_t m = 0; m < m_comp.size(); m++) {
130  sum += std::abs(m_comp[m]);
131  }
132  return sum;
133  }
134  /// Number of atoms of element m (positive or negative)
135  int nAtoms(size_t m) const {
136  if (m_comp.empty()) {
137  return 0;
138  }
139  return m_comp[m];
140  }
141 
142  std::ostream& fmt(std::ostream& s, const std::vector<std::string>& esymbols) const;
143 
144  friend std::ostream& operator<<(std::ostream& s,
145  const Group& g);
146 
147 private:
148  std::vector<int> m_comp;
149  int m_sign;
150 };
151 
152 }
153 
154 #endif
This file contains definitions of terms that are used in internal routines and are unlikely to need m...
bool valid() const
True if all non-zero atom numbers have the same sign.
Definition: Group.h:113
void validate()
Definition: Group.cpp:17
Class Group is an internal class used by class ReactionPath.
Definition: Group.h:19
int nAtoms() const
Number of atoms in the group (>= 0)
Definition: Group.h:127
void operator-=(const Group &other)
Decrement the atom numbers by those in group 'other'.
Definition: Group.h:51
int nAtoms(size_t m) const
Number of atoms of element m (positive or negative)
Definition: Group.h:135