Cantera  2.5.1
Group.h
Go to the documentation of this file.
1 /**
2  * @file Group.h
3  */
4 
5 // This file is part of Cantera. See License.txt in the top-level directory or
6 // at https://cantera.org/license.txt for license and copyright information.
7 
8 #ifndef CT_RXNPATH_GROUP
9 #define CT_RXNPATH_GROUP
10 
11 #include "cantera/base/ct_defs.h"
12 
13 namespace Cantera
14 {
15 
16 /**
17  * Class Group is an internal class used by class ReactionPath. It
18  * represents some subset of the atoms of a molecule.
19  */
20 class Group
21 {
22 public:
23  Group() : m_sign(-999) { }
24  Group(size_t n) : m_sign(0) {
25  m_comp.resize(n,0);
26  }
27  Group(const vector_int& elnumbers) :
28  m_comp(elnumbers), m_sign(0) {
29  validate();
30  }
31  Group(const std::vector<size_t>& elnumbers) :
32  m_comp(elnumbers.size()), m_sign(0) {
33  for (size_t i = 0; i < elnumbers.size(); i++) {
34  m_comp[i] = int(elnumbers[i]);
35  }
36  validate();
37  }
38  Group(const Group& g) :
39  m_comp(g.m_comp), m_sign(g.m_sign) { }
40  Group& operator=(const Group& g) {
41  if (&g != this) {
42  m_comp = g.m_comp;
43  m_sign = g.m_sign;
44  }
45  return *this;
46  }
47  virtual ~Group() {}
48 
49  /**
50  * Decrement the atom numbers by those in group 'other'.
51  */
52  void operator-=(const Group& 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  for (size_t m = 0; m < m_comp.size(); m++) {
60  m_comp[m] += other.m_comp[m];
61  }
62  validate();
63  }
64  void operator*=(int a) {
65  for (size_t m = 0; m < m_comp.size(); m++) {
66  m_comp[m] *= a;
67  }
68  validate();
69  }
70  bool operator==(const Group& other) const {
71  for (size_t m = 0; m < m_comp.size(); m++) {
72  if (m_comp[m] != other.m_comp[m]) {
73  return false;
74  }
75  }
76  return true;
77  }
78  friend Group operator-(const Group& g1, const Group& g2) {
79  Group diff(g1);
80  diff -= g2;
81  return diff;
82  }
83  friend Group operator+(const Group& g1, const Group& g2) {
84  Group sum(g1);
85  sum += g2;
86  return sum;
87  }
88  /*!
89  * A group is 'valid' if all of its nonzero atom numbers have
90  * the same sign, either positive or negative. This method
91  * checks for this, and if the group is not valid it sets
92  * m_sign to -999, and sets all atom numbers to zero.
93  */
94  void validate();
95 
96  /**
97  * True if all non-zero atom numbers have the same sign.
98  */
99  bool valid() const {
100  return (m_sign != -999);
101  }
102  bool operator!() const {
103  return (m_sign == -999);
104  }
105  int sign() const {
106  return m_sign;
107  }
108  size_t size() const {
109  return m_comp.size();
110  }
111 
112  /// Number of atoms in the group (>= 0)
113  int nAtoms() const {
114  int sum = 0;
115  for (size_t m = 0; m < m_comp.size(); m++) {
116  sum += std::abs(m_comp[m]);
117  }
118  return sum;
119  }
120  /// Number of atoms of element m (positive or negative)
121  int nAtoms(size_t m) const {
122  if (m_comp.empty()) {
123  return 0;
124  }
125  return m_comp[m];
126  }
127 
128  std::ostream& fmt(std::ostream& s, const std::vector<std::string>& esymbols) const;
129 
130  friend std::ostream& operator<<(std::ostream& s,
131  const Group& g);
132 
133 private:
134  vector_int m_comp;
135  int m_sign;
136 };
137 
138 }
139 
140 #endif
Class Group is an internal class used by class ReactionPath.
Definition: Group.h:21
bool valid() const
True if all non-zero atom numbers have the same sign.
Definition: Group.h:99
void validate()
Definition: Group.cpp:15
int nAtoms() const
Number of atoms in the group (>= 0)
Definition: Group.h:113
void operator-=(const Group &other)
Decrement the atom numbers by those in group 'other'.
Definition: Group.h:52
int nAtoms(size_t m) const
Number of atoms of element m (positive or negative)
Definition: Group.h:121
This file contains definitions of terms that are used in internal routines and are unlikely to need m...
std::vector< int > vector_int
Vector of ints.
Definition: ct_defs.h:182
Namespace for the Cantera kernel.
Definition: AnyMap.cpp:264
Versions 6.2.0 and 6.2.1 of fmtlib do not include this define before they include windows....
Definition: fmt.h:35