Canoe
Comprehensive Atmosphere N' Ocean Engine
Kinetics.cc
Go to the documentation of this file.
1 #include "Kinetics.h"
2 
3 #include <deal.II/lac/precondition.h>
4 #include <deal.II/lac/solver_gmres.h>
5 
6 Kinetics::Kinetics(int nrows, int ncols, int nhalo)
7  : m_grid(nrows, ncols, nhalo),
8  m_psi(m_grid, "psi"),
9  m_eddy(m_grid, "eddy"),
10  m_q(m_grid, "q"),
11  m_advection(m_grid, m_psi),
12  m_diffusion(m_grid, m_eddy),
13  m_pattern(m_grid.rank(), m_grid.rank(), 9),
14  m_src(m_grid.rank()),
15  m_rhs(m_grid.rank()),
16  m_bnd(m_grid.rank()),
17  m_buffer(m_grid.pattern()) {
18  m_pattern.compress();
19  m_adj.reinit(m_pattern);
20  m_buffer = 0.;
21 
22  initialize();
23  m_advection.update();
24  m_diffusion.update();
25 
26  m_psi.finish();
27  m_eddy.finish();
28  m_q.finish();
29 }
30 
32  m_psi.setZero();
33  m_q.setZero();
34  m_eddy.setOnes();
35  m_eddy *= 0.1;
36  for (int i = 0; i < m_grid.rows(); i++)
37  for (int j = 0; j < m_grid.cols(); j++) {
38  m_psi.val(i, j) = -(j + 1) * m_grid.dd();
39  // m_psi.val(i, j) = 0.;
40  if (i > m_grid.rows() / 4. && i < 3. * m_grid.rows() / 4. &&
41  j > m_grid.cols() / 4. && j < 3. * m_grid.cols() / 4.)
42  m_q.val(i, j) = 1.;
43  }
44  m_q.set_boundary(3, Dirichlet, 1.);
45  for (int i = 0; i < m_grid.rank(); i++)
46  m_src(i) = m_q.val(i / m_grid.cols(), i % m_grid.cols());
47 }
48 
49 void Kinetics::assemble(double theta, double dt) {
50  m_buffer.add(1., m_diffusion());
51  m_buffer.add(-1., m_advection());
52  m_buffer.mmult(m_adj, m_q.neumann());
53  m_mass.reinit(m_pattern);
54  m_force.reinit(m_pattern);
55 
56  m_mass = dealii::IdentityMatrix(m_grid.rank());
57  m_mass.add(-theta * dt, m_adj);
58 
59  m_force = dealii::IdentityMatrix(m_grid.rank());
60  m_force.add((1. - theta) * dt, m_adj);
61 
62  m_buffer.vmult(m_bnd, m_q.dirichlet());
63  m_bnd *= dt;
64 }
65 
67  std::cout << "== begin ==" << std::endl;
68  std::cout << m_psi << std::endl;
69  std::cout << m_eddy << std::endl;
70  std::cout << m_q << std::endl;
71  std::cout << "== source ==" << std::endl;
72  std::cout << m_src << std::endl;
73  /*
74  std::cout << "== advection ==" << std::endl;
75  m_advection().print_formatted(std::cout, 2, false);
76  std::cout << "== diffusion ==" << std::endl;
77  m_diffusion().print_formatted(std::cout, 2, false);
78  std::cout << "== mass matrix ==" << std::endl;
79  m_mass.print_formatted(std::cout, 2, false);
80  std::cout << "== force matrix ==" << std::endl;
81  m_force.print_formatted(std::cout, 2, false);
82  */
83  std::cout << "== end ==" << std::endl;
84 }
85 
86 void Kinetics::run(int nt) {
87  dealii::SolverControl control(1000, 1e-12);
88  dealii::SolverGMRES<> solver(control);
89 
90  for (int t = 0; t < nt; t++) {
91  m_force.vmult(m_rhs, m_src);
92  m_rhs += m_bnd;
93  solver.solve(m_mass, m_src, m_rhs, dealii::PreconditionIdentity());
94 
95  if (t % 10 == 0) {
96  std::cout << m_src << std::endl;
97  // std::cout << "Iteration converges after " << control.last_step()
98  // << " steps." << std::endl;
99  }
100  }
101 }
@ Dirichlet
Definition: Boundary.h:4
dealii::SparsityPattern m_pattern
Definition: Kinetics.h:23
dealii::Vector< Scalar > m_src
Definition: Kinetics.h:27
dealii::SparseMatrix< Scalar > m_force
Definition: Kinetics.h:25
Variable< Scalar, 2 > m_eddy
Definition: Kinetics.h:17
Diffusion< Scalar, 2, 4 > m_diffusion
Definition: Kinetics.h:21
void assemble(double, double)
Definition: Kinetics.cc:49
dealii::Vector< Scalar > m_rhs
Definition: Kinetics.h:27
void initialize()
Definition: Kinetics.cc:31
dealii::SparseMatrix< Scalar > m_mass
Definition: Kinetics.h:25
dealii::Vector< Scalar > m_bnd
Definition: Kinetics.h:27
Variable< Scalar, 2 > m_psi
Definition: Kinetics.h:17
Kinetics(int, int, int=1)
Definition: Kinetics.cc:6
void checkout()
Definition: Kinetics.cc:66
Variable< Scalar, 2 > m_q
Definition: Kinetics.h:17
dealii::SparseMatrix< Scalar > m_buffer
Definition: Kinetics.h:41
Advection< Scalar, 2, 4 > m_advection
Definition: Kinetics.h:19
RectGrid< Scalar, Dimension > m_grid
Definition: Kinetics.h:15
dealii::SparseMatrix< Scalar > m_adj
Definition: Kinetics.h:25
void run(int)
Definition: Kinetics.cc:86