| Cantera 3.0.0

Loading...
Searching...
No Matches
Array.cpp
Go to the documentation of this file.
1/**
2 * @file Array.cpp Implementation file for class Cantera::Array2D
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#include "cantera/base/Array.h"
9#include "cantera/base/utilities.h"
10#include "cantera/base/global.h"
11
12 namespace Cantera
13{
14
15 Array2D::Array2D(const size_t m, const size_t n, const double v)
16 : m_nrows(m)
17 , m_ncols(n)
18{
19 m_data.assign(n*m, v);
20}
21
22 Array2D::Array2D(const size_t m, const size_t n, const double* values)
23 : m_nrows(m)
24 , m_ncols(n)
25{
26 m_data.assign(values, values + n*m);
27}
28
29 Array2D::Array2D(const Array2D& y)
30 : m_data(y.m_data)
31 , m_nrows(y.m_nrows)
32 , m_ncols(y.m_ncols)
33{
34}
35
36Array2D& Array2D::operator=(const Array2D& y)
37{
38 if (&y == this) {
39 return *this;
40 }
41 m_nrows = y.m_nrows;
42 m_ncols = y.m_ncols;
43 m_data = y.m_data;
44 return *this;
45}
46
47 void Array2D::resize(size_t n, size_t m, double v)
48{
49 m_nrows = n;
50 m_ncols = m;
51 m_data.resize(n*m, v);
52}
53
54 void Array2D::appendColumn(const vector<double>& c)
55{
56 m_ncols++;
57 m_data.resize(m_nrows * m_ncols);
58 for (size_t m = 0; m < m_nrows; m++) {
59 value(m_ncols, m) = c[m];
60 }
61}
62
63 void Array2D::appendColumn(const double* const c)
64{
65 m_ncols++;
66 m_data.resize(m_nrows * m_ncols);
67 for (size_t m = 0; m < m_nrows; m++) {
68 value(m_ncols, m) = c[m];
69 }
70}
71
72 void Array2D::setRow(size_t n, const double* const rw)
73{
74 for (size_t j = 0; j < m_ncols; j++) {
75 m_data[m_nrows*j + n] = rw[j];
76 }
77}
78
79 void Array2D::getRow(size_t n, double* const rw)
80{
81 for (size_t j = 0; j < m_ncols; j++) {
82 rw[j] = m_data[m_nrows*j + n];
83 }
84}
85
86 void Array2D::setColumn(size_t m, double* const col)
87{
88 for (size_t i = 0; i < m_nrows; i++) {
89 m_data[m_nrows*m + i] = col[i];
90 }
91}
92
93 void Array2D::getColumn(size_t m, double* const col)
94{
95 for (size_t i = 0; i < m_nrows; i++) {
96 col[i] = m_data[m_nrows*m + i];
97 }
98}
99
100 Array2D::iterator Array2D::begin() {
101 warn_deprecated("Array2D::begin", "To be removed after Cantera 3.0.");
102 return m_data.begin();
103}
104
105 Array2D::iterator Array2D::end() {
106 warn_deprecated("Array2D::end", "To be removed after Cantera 3.0.");
107 return m_data.end();
108}
109
110 Array2D::const_iterator Array2D::begin() const {
111 warn_deprecated("Array2D::begin", "To be removed after Cantera 3.0.");
112 return m_data.begin();
113}
114
115 Array2D::const_iterator Array2D::end() const {
116 warn_deprecated("Array2D::end", "To be removed after Cantera 3.0.");
117 return m_data.end();
118}
119
120std::ostream& operator<<(std::ostream& s, const Array2D& m)
121{
122 size_t nr = m.nRows();
123 size_t nc = m.nColumns();
124 for (size_t i = 0; i < nr; i++) {
125 s << m(i,0);
126 for (size_t j = 1; j < nc; j++) {
127 s << ", " << m(i,j);
128 }
129 s << std::endl;
130 }
131 return s;
132}
133
134 void operator*=(Array2D& m, double a)
135{
136 scale(m.begin(), m.end(), m.begin(), a);
137}
138
139}
Array.h
Header file for class Cantera::Array2D.
Cantera::Array2D
A class for 2D arrays stored in column-major (Fortran-compatible) form.
Definition Array.h:32
Cantera::Array2D::m_data
vector< double > m_data
Data stored in a single array.
Definition Array.h:251
Cantera::Array2D::Array2D
Array2D()=default
Default constructor.
Cantera::Array2D::m_nrows
size_t m_nrows
Number of rows.
Definition Array.h:254
Cantera::Array2D::getColumn
void getColumn(size_t m, double *const col)
Get the values in column m.
Definition Array.cpp:93
Cantera::Array2D::setColumn
void setColumn(size_t m, double *const col)
Set the values in column m to those in array col.
Definition Array.cpp:86
Cantera::Array2D::begin
iterator begin()
Return an iterator pointing to the first element.
Definition Array.cpp:100
Cantera::Array2D::end
iterator end()
Return an iterator pointing past the last element.
Definition Array.cpp:105
Cantera::Array2D::nRows
size_t nRows() const
Number of rows.
Definition Array.h:192
Cantera::Array2D::iterator
vector< double >::iterator iterator
Type definition for the iterator class that is can be used by Array2D types.
Definition Array.h:40
Cantera::Array2D::m_ncols
size_t m_ncols
Number of columns.
Definition Array.h:257
Cantera::Array2D::nColumns
size_t nColumns() const
Number of columns.
Definition Array.h:197
Cantera::Array2D::appendColumn
void appendColumn(const vector< double > &c)
Append a column to the existing matrix using a std vector.
Definition Array.cpp:54
Cantera::Array2D::setRow
void setRow(size_t n, const double *const rw)
Set the nth row to array rw.
Definition Array.cpp:72
Cantera::Array2D::getRow
void getRow(size_t n, double *const rw)
Get the nth row and return it in a vector.
Definition Array.cpp:79
Cantera::Array2D::value
double & value(size_t i, size_t j)
Returns a changeable reference to position in the matrix.
Definition Array.h:176
Cantera::Array2D::resize
virtual void resize(size_t n, size_t m, double v=0.0)
Resize the array, and fill the new entries with 'v'.
Definition Array.cpp:47
Cantera::Array2D::const_iterator
vector< double >::const_iterator const_iterator
Type definition for the const_iterator class that is can be used by Array2D types.
Definition Array.h:48
global.h
This file contains definitions for utility functions and text for modules, inputfiles and logging,...
Cantera::scale
void scale(InputIter begin, InputIter end, OutputIter out, S scale_factor)
Multiply elements of an array by a scale factor.
Definition utilities.h:104
Cantera
Namespace for the Cantera kernel.
Definition AnyMap.cpp:564
Cantera::operator*=
void operator*=(Array2D &m, double a)
Overload the times equals operator for multiplication of a matrix and a scalar.
Definition Array.cpp:134
Cantera::warn_deprecated
void warn_deprecated(const string &source, const AnyBase &node, const string &message)
A deprecation warning for syntax in an input file.
Definition AnyMap.cpp:1926
Cantera::operator<<
std::ostream & operator<<(std::ostream &s, const Array2D &m)
Output the current contents of the Array2D object.
Definition Array.cpp:120
utilities.h
Various templated functions that carry out common vector and polynomial operations (see Templated Arr...