Cantera  4.0.0a1
Loading...
Searching...
No Matches
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
12
13namespace 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 */
20class Group
21{
22public:
23 Group() : m_sign(-999) { }
24 Group(size_t n) : m_sign(0) {
25 m_comp.resize(n,0);
26 }
27 Group(span<const int> elnumbers)
28 : m_comp(elnumbers.begin()
29 , elnumbers.end())
30 , m_sign(0)
31 {
32 validate();
33 }
34 Group(span<const size_t> elnumbers) :
35 m_comp(elnumbers.size()), m_sign(0)
36 {
37 for (size_t i = 0; i < elnumbers.size(); i++) {
38 m_comp[i] = int(elnumbers[i]);
39 }
40 validate();
41 }
42 Group(const Group& g) :
43 m_comp(g.m_comp), m_sign(g.m_sign) { }
44 Group& operator=(const Group& g) {
45 if (&g != this) {
46 m_comp = g.m_comp;
47 m_sign = g.m_sign;
48 }
49 return *this;
50 }
51 virtual ~Group() {}
52
53 /**
54 * Decrement the atom numbers by those in group 'other'.
55 */
56 void operator-=(const Group& other) {
57 for (size_t m = 0; m < m_comp.size(); m++) {
58 m_comp[m] -= other.m_comp[m];
59 }
60 validate();
61 }
62 void operator+=(const Group& other) {
63 for (size_t m = 0; m < m_comp.size(); m++) {
64 m_comp[m] += other.m_comp[m];
65 }
66 validate();
67 }
68 void operator*=(int a) {
69 for (size_t m = 0; m < m_comp.size(); m++) {
70 m_comp[m] *= a;
71 }
72 validate();
73 }
74 bool operator==(const Group& other) const {
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 Group diff(g1);
84 diff -= g2;
85 return diff;
86 }
87 friend Group operator+(const Group& g1, const Group& g2) {
88 Group sum(g1);
89 sum += g2;
90 return sum;
91 }
92 /*!
93 * A group is 'valid' if all of its nonzero atom numbers have
94 * the same sign, either positive or negative. This method
95 * checks for this, and if the group is not valid it sets
96 * m_sign to -999, and sets all atom numbers to zero.
97 */
98 void validate();
99
100 /**
101 * True if all non-zero atom numbers have the same sign.
102 */
103 bool valid() const {
104 return (m_sign != -999);
105 }
106 bool operator!() const {
107 return (m_sign == -999);
108 }
109 int sign() const {
110 return m_sign;
111 }
112 size_t size() const {
113 return m_comp.size();
114 }
115
116 //! Number of atoms in the group (>= 0)
117 int nAtoms() const {
118 int sum = 0;
119 for (size_t m = 0; m < m_comp.size(); m++) {
120 sum += std::abs(m_comp[m]);
121 }
122 return sum;
123 }
124 //! Number of atoms of element m (positive or negative)
125 int nAtoms(size_t m) const {
126 if (m_comp.empty()) {
127 return 0;
128 }
129 return m_comp[m];
130 }
131
132 std::ostream& fmt(std::ostream& s, span<const string> esymbols) const;
133
134private:
135 vector<int> m_comp;
136 int m_sign;
137};
138
139}
140
141#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:103
void validate()
A group is 'valid' if all of its nonzero atom numbers have the same sign, either positive or negative...
Definition Group.cpp:15
int nAtoms() const
Number of atoms in the group (>= 0)
Definition Group.h:117
void operator-=(const Group &other)
Decrement the atom numbers by those in group 'other'.
Definition Group.h:56
int nAtoms(size_t m) const
Number of atoms of element m (positive or negative)
Definition Group.h:125
This file contains definitions of constants, types and terms that are used in internal routines and a...
Namespace for the Cantera kernel.
Definition AnyMap.cpp:595