Cantera  2.1.2
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  /*!
105  * A group is 'valid' if all of its nonzero atom numbers have
106  * the same sign, either positive or negative. This method
107  * checks for this, and if the group is not valid it sets
108  * m_sign to -999, and sets all atom numbers to zero.
109  */
110  void validate();
111 
112  /**
113  * True if all non-zero atom numbers have the same sign.
114  */
115  bool valid() const {
116  return (m_sign != -999);
117  }
118  bool operator!() const {
119  return (m_sign == -999);
120  }
121  int sign() const {
122  return m_sign;
123  }
124  size_t size() const {
125  return m_comp.size();
126  }
127 
128  /// Number of atoms in the group (>= 0)
129  int nAtoms() const {
130  int sum = 0;
131  for (size_t m = 0; m < m_comp.size(); m++) {
132  sum += std::abs(m_comp[m]);
133  }
134  return sum;
135  }
136  /// Number of atoms of element m (positive or negative)
137  int nAtoms(size_t m) const {
138  if (m_comp.empty()) {
139  return 0;
140  }
141  return m_comp[m];
142  }
143 
144  std::ostream& fmt(std::ostream& s, const std::vector<std::string>& esymbols) const;
145 
146  friend std::ostream& operator<<(std::ostream& s,
147  const Group& g);
148 
149 private:
150  std::vector<int> m_comp;
151  int m_sign;
152 };
153 
154 }
155 
156 #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:115
void validate()
Definition: Group.cpp:16
Class Group is an internal class used by class ReactionPath.
Definition: Group.h:21
int nAtoms() const
Number of atoms in the group (>= 0)
Definition: Group.h:129
void operator-=(const Group &other)
Decrement the atom numbers by those in group 'other'.
Definition: Group.h:53
int nAtoms(size_t m) const
Number of atoms of element m (positive or negative)
Definition: Group.h:137