Cantera  4.0.0a1
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
11#include "cantera/base/global.h"
12
13namespace Cantera
14{
15
16Array2D::Array2D(const size_t m, const size_t n, const double v)
17 : m_nrows(m)
18 , m_ncols(n)
19{
20 m_data.assign(n*m, v);
21}
22
23Array2D::Array2D(const size_t m, const size_t n, span<const double> values)
24 : m_nrows(m)
25 , m_ncols(n)
26{
27 if (values.size() != n * m) {
28 throw CanteraError("Array2D::Array2D",
29 "Input array has incorrect length. Expected {}, got {}.", n * m, values.size());
30 }
31 m_data.assign(values.begin(), values.end());
32}
33
35 : m_data(y.m_data)
36 , m_nrows(y.m_nrows)
37 , m_ncols(y.m_ncols)
38{
39}
40
41Array2D& Array2D::operator=(const Array2D& y)
42{
43 if (&y == this) {
44 return *this;
45 }
46 m_nrows = y.m_nrows;
47 m_ncols = y.m_ncols;
48 m_data = y.m_data;
49 return *this;
50}
51
52void Array2D::resize(size_t n, size_t m, double v)
53{
54 m_nrows = n;
55 m_ncols = m;
56 m_data.resize(n*m, v);
57}
58
59void Array2D::appendColumn(span<const double> c)
60{
61 checkArraySize("Array2D::appendColumn", c.size(), m_nrows);
62 m_ncols++;
63 m_data.resize(m_nrows * m_ncols);
64 for (size_t i = 0; i < m_nrows; i++) {
65 value(i, m_ncols - 1) = c[i];
66 }
67}
68
69void Array2D::setRow(size_t n, span<const double> rw)
70{
71 checkArraySize("Array2D::setRow", rw.size(), m_ncols);
72 for (size_t j = 0; j < m_ncols; j++) {
73 m_data[m_nrows*j + n] = rw[j];
74 }
75}
76
77void Array2D::getRow(size_t n, span<double> rw) const
78{
79 checkArraySize("Array2D::getRow", rw.size(), m_ncols);
80 for (size_t j = 0; j < m_ncols; j++) {
81 rw[j] = m_data[m_nrows*j + n];
82 }
83}
84
85void Array2D::setColumn(size_t m, span<const double> col)
86{
87 checkArraySize("Array2D::setColumn", col.size(), m_nrows);
88 for (size_t i = 0; i < m_nrows; i++) {
89 m_data[m_nrows*m + i] = col[i];
90 }
91}
92
93void Array2D::getColumn(size_t m, span<double> col) const
94{
95 checkArraySize("Array2D::getColumn", col.size(), m_nrows);
96 for (size_t i = 0; i < m_nrows; i++) {
97 col[i] = m_data[m_nrows*m + i];
98 }
99}
100
101std::ostream& operator<<(std::ostream& s, const Array2D& m)
102{
103 size_t nr = m.nRows();
104 size_t nc = m.nColumns();
105 for (size_t i = 0; i < nr; i++) {
106 s << m(i,0);
107 for (size_t j = 1; j < nc; j++) {
108 s << ", " << m(i,j);
109 }
110 s << std::endl;
111 }
112 return s;
113}
114
115void operator*=(Array2D& m, double a)
116{
117 scale(m.data().begin(), m.data().end(), m.data().begin(), a);
118}
119
120}
Header file for class Cantera::Array2D.
A class for 2D arrays stored in column-major (Fortran-compatible) form.
Definition Array.h:32
vector< double > m_data
Data stored in a single array.
Definition Array.h:200
Array2D()=default
Default constructor.
size_t m_nrows
Number of rows.
Definition Array.h:203
void getColumn(size_t m, span< double > col) const
Get the values in column m.
Definition Array.cpp:93
void setRow(size_t n, span< const double > rw)
Set the nth row to array rw.
Definition Array.cpp:69
void appendColumn(span< const double > c)
Append a column to the existing matrix using a std vector.
Definition Array.cpp:59
void getRow(size_t n, span< double > rw) const
Get the nth row and return it in a vector.
Definition Array.cpp:77
size_t nRows() const
Number of rows.
Definition Array.h:167
size_t m_ncols
Number of columns.
Definition Array.h:206
size_t nColumns() const
Number of columns.
Definition Array.h:172
void setColumn(size_t m, span< const double > col)
Set the values in column m to those in array col.
Definition Array.cpp:85
span< double > col(size_t j)
Return a writable span over column j.
Definition Array.h:189
vector< double > & data()
Return a reference to the data vector.
Definition Array.h:177
double & value(size_t i, size_t j)
Returns a changeable reference to position in the matrix.
Definition Array.h:151
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:52
Base class for exceptions thrown by Cantera classes.
Definitions for the classes that are thrown when Cantera experiences an error condition (also contain...
This file contains definitions for utility functions and text for modules, inputfiles and logging,...
void scale(InputIter begin, InputIter end, OutputIter out, S scale_factor)
Multiply elements of an array by a scale factor.
Definition utilities.h:118
Namespace for the Cantera kernel.
Definition AnyMap.cpp:595
void operator*=(Array2D &m, double a)
Overload the times equals operator for multiplication of a matrix and a scalar.
Definition Array.cpp:115
std::ostream & operator<<(std::ostream &s, const Array2D &m)
Output the current contents of the Array2D object.
Definition Array.cpp:101
void checkArraySize(const char *procedure, size_t available, size_t required)
Wrapper for throwing ArraySizeError.
Various templated functions that carry out common vector and polynomial operations (see Templated Arr...