1 #ifndef SRC_TRANSPORT_VARIABLE_H_
2 #define SRC_TRANSPORT_VARIABLE_H_
3 #include <deal.II/lac/sparse_matrix.h>
15 template <
typename _Scalar,
int _Dim>
18 template <
typename _Scalar>
20 :
public Eigen::Array<_Scalar, Eigen::Dynamic, Eigen::Dynamic> {
21 template <
typename STREAM>
23 os << std::setw(9) << var.m_name <<
"|";
24 for (
int i = 0; i < var.rows(); i++)
25 os << std::setw(10) << i - var.m_grid.halo();
27 for (
int i = -1; i < var.rows(); i++) os <<
"----------";
29 for (
int j = 0;
j < var.cols();
j++) {
30 os << std::setw(9) <<
j - var.m_grid.halo();
32 for (
int i = 0; i < var.rows(); i++) os << std::setw(10) << var(i,
j);
35 for (
int i = -1; i < var.rows(); i++) os <<
"----------";
43 typedef Eigen::Array<Scalar, Eigen::Dynamic, Eigen::Dynamic>
Base;
44 enum { Dimension = 2 };
63 template <
typename OtherDerived>
64 explicit Variable(
const Eigen::DenseBase<OtherDerived>& other)
67 template <
typename OtherDerived>
69 Base::operator=(other);
76 m_value(*this, m_grid.halo(), m_grid.halo(), m_grid.rows(),
79 m_longname(other.m_longname),
80 m_units(other.m_units),
81 m_pattern(m_grid.rankh(), m_grid.rank(), 1),
82 m_dirichlet(m_grid.rankh()) {
83 for (
int i = 0; i < m_grid.rows(); i++)
84 for (
int j = 0;
j < m_grid.cols();
j++)
85 m_pattern.add(m_grid.globalh(i,
j), m_grid.global(i,
j));
87 for (
int i = 0; i < m_grid.rankh(); i++) m_dirichlet(i) = 0.;
91 m_value = other.m_value;
96 std::string longname =
"", std::string units =
"")
97 :
Base(grid.rowsh(), grid.colsh()),
99 m_value(*this, grid.halo(), grid.halo(), grid.rows(), grid.cols()),
101 m_longname(longname),
103 m_pattern(grid.rankh(), grid.rank(), 1),
104 m_dirichlet(grid.rankh()) {
105 for (
int i = 0; i < m_grid.rows(); i++)
106 for (
int j = 0;
j < m_grid.cols();
j++)
107 m_pattern.add(m_grid.globalh(i,
j), m_grid.global(i,
j));
109 for (
int i = 0; i < m_grid.rankh(); i++) m_dirichlet(i) = 0.;
113 if (id < 0 || id > 3) {
114 std::cerr <<
"Boundary Id error" << std::endl;
117 m_boundary[id].m_type = type;
118 m_boundary[id].m_value = value;
122 std::ofstream out(filename,
123 std::ios::out | std::ios::binary | std::ios::trunc);
124 int nrows = m_grid.rows(), ncols = m_grid.cols(), ncolsh = m_grid.colsh(),
125 nhalo = m_grid.halo();
126 out.write(
static_cast<char*
>(&nrows),
sizeof(
int));
127 out.write(
static_cast<char*
>(&ncols),
sizeof(
int));
128 for (
int i = nhalo; i < nhalo + nrows; i++)
129 out.write(
static_cast<char*
>(this->data() + nhalo + i * ncolsh),
135 std::ofstream out(filename, std::ios::out | std::ios::trunc);
136 for (
int j = 0;
j < m_grid.cols();
j++) {
137 for (
int i = 0; i < m_grid.rows(); i++)
138 out << std::setw(14) << std::setprecision(6) << m_value(i,
j);
145 const dealii::SparseMatrix<Scalar>&
neumann()
const {
return m_neumann; }
147 const dealii::Vector<Scalar>&
dirichlet()
const {
return m_dirichlet; }
149 inline Eigen::Block<Base>&
val() {
return m_value; }
151 inline Scalar&
val(
int i,
int j) {
return m_value.coeffRef(i,
j); }
153 inline const Eigen::Block<Base>&
val()
const {
return m_value; }
155 inline const Scalar&
val(
int i,
int j)
const {
return m_value.coeff(i,
j); }
157 inline std::string
get_name()
const {
return m_name; }
161 inline std::string
get_units()
const {
return m_units; }
164 template <
typename _Scalar>
166 switch (m_boundary[0].m_type) {
168 for (
int i = 0; i < m_grid.rows(); i++)
169 m_dirichlet(m_grid.globalh(i, -1)) = m_boundary[0].m_value;
176 std::cerr <<
"Boundary type not found" << std::endl;
179 switch (m_boundary[1].m_type) {
181 for (
int i = 0; i < m_grid.rows(); i++)
182 m_dirichlet(m_grid.globalh(i, m_grid.cols())) = m_boundary[1].m_value;
189 std::cerr <<
"Boundary type not found" << std::endl;
192 switch (m_boundary[2].m_type) {
194 for (
int j = 0;
j < m_grid.cols();
j++)
195 m_dirichlet(m_grid.globalh(-1,
j)) = m_boundary[2].m_value;
202 std::cerr <<
"Boundary type not found" << std::endl;
205 switch (m_boundary[3].m_type) {
207 for (
int j = 0;
j < m_grid.cols();
j++)
208 m_dirichlet(m_grid.globalh(m_grid.rows(),
j)) = m_boundary[3].m_value;
215 std::cerr <<
"Boundary type not found" << std::endl;
218 m_pattern.compress();
220 m_neumann.reinit(m_pattern);
221 for (
int i = 0; i < m_grid.rows(); i++)
222 for (
int j = 0;
j < m_grid.cols();
j++)
223 m_neumann.set(m_grid.globalh(i,
j), m_grid.global(i,
j), 1.);
Eigen::Block< Base > m_value
Variable(const Variable &other)
std::string get_name() const
std::string get_longname() const
void write_file(const char *filename) const
const Eigen::Block< Base > & val() const
Variable & operator=(const Variable &other)
dealii::Vector< Scalar > m_dirichlet
std::array< BoundaryInfo< Scalar >, 4 > m_boundary
Variable(const RectGrid< Scalar, Dimension > &grid, std::string name="", std::string longname="", std::string units="")
Scalar & val(int i, int j)
const dealii::SparseMatrix< Scalar > & neumann() const
dealii::SparseMatrix< Scalar > m_neumann
friend STREAM & operator<<(STREAM &os, const Variable &var)
dealii::SparsityPattern m_pattern
void set_boundary(int id, BoundaryType type, Scalar value)
Eigen::Array< Scalar, Eigen::Dynamic, Eigen::Dynamic > Base
const Scalar & val(int i, int j) const
const RectGrid< Scalar, Dimension > & m_grid
const dealii::Vector< Scalar > & dirichlet() const
Variable & operator=(const Eigen::ArrayBase< OtherDerived > &other)
Variable(const Eigen::DenseBase< OtherDerived > &other)
std::string get_units() const
void write_binary(const char *filename) const
Eigen::Block< Base > & val()