| 1 | /**
|
|---|
| 2 | * @file solver_regular.cpp
|
|---|
| 3 | * @author Julian Iseringhausen <isering@ins.uni-bonn.de>
|
|---|
| 4 | * @date Mon Apr 18 13:11:32 2011
|
|---|
| 5 | *
|
|---|
| 6 | * @brief VMG::SolverRegular
|
|---|
| 7 | *
|
|---|
| 8 | */
|
|---|
| 9 |
|
|---|
| 10 | #ifdef HAVE_CONFIG_H
|
|---|
| 11 | #include <config.h>
|
|---|
| 12 | #endif
|
|---|
| 13 |
|
|---|
| 14 | #include <iostream>
|
|---|
| 15 |
|
|---|
| 16 | #include "base/discretization.hpp"
|
|---|
| 17 | #include "base/stencil.hpp"
|
|---|
| 18 | #include "grid/multigrid.hpp"
|
|---|
| 19 | #include "solver/solver_regular.hpp"
|
|---|
| 20 | #include "mg.hpp"
|
|---|
| 21 |
|
|---|
| 22 | using namespace VMG;
|
|---|
| 23 |
|
|---|
| 24 | // TODO: Implement global communication here
|
|---|
| 25 | // TODO: Implement this more efficiently
|
|---|
| 26 | // TODO: This has to be reviewed for parallelization
|
|---|
| 27 |
|
|---|
| 28 | void SolverRegular::AssembleMatrix(const Grid& rhs)
|
|---|
| 29 | {
|
|---|
| 30 | Grid::iterator grid_iter;
|
|---|
| 31 | Stencil::iterator stencil_iter;
|
|---|
| 32 | int mat_index, mat_index2;
|
|---|
| 33 | vmg_float prefactor_inv = 1.0 / MG::GetDiscretization()->OperatorPrefactor(rhs);
|
|---|
| 34 | const Stencil& A = MG::GetDiscretization()->GetStencil();
|
|---|
| 35 |
|
|---|
| 36 | #ifdef DEBUG_MATRIX_CHECKS
|
|---|
| 37 | rhs.IsConsistent();
|
|---|
| 38 | #endif
|
|---|
| 39 |
|
|---|
| 40 | this->Realloc(rhs.Global().SizeGlobal().Product());
|
|---|
| 41 |
|
|---|
| 42 | for (grid_iter = rhs.Iterators().Local().Begin(); grid_iter != rhs.Iterators().Local().End(); ++grid_iter) {
|
|---|
| 43 |
|
|---|
| 44 | mat_index = rhs.GlobalLinearIndex(*grid_iter + rhs.Global().BeginLocal());
|
|---|
| 45 |
|
|---|
| 46 | assert(mat_index >= 0 && mat_index<this->Size());
|
|---|
| 47 |
|
|---|
| 48 | this->Sol(mat_index) = 0.0;
|
|---|
| 49 | this->Rhs(mat_index) = prefactor_inv * rhs.GetVal(*grid_iter);
|
|---|
| 50 |
|
|---|
| 51 | for (int l=0; l<this->Size(); l++)
|
|---|
| 52 | this->Mat(mat_index, l) = 0.0;
|
|---|
| 53 |
|
|---|
| 54 | this->Mat(mat_index, mat_index) = A.GetDiag();
|
|---|
| 55 |
|
|---|
| 56 | for (stencil_iter = A.begin(); stencil_iter != A.end(); ++stencil_iter) {
|
|---|
| 57 |
|
|---|
| 58 | mat_index2 = rhs.GlobalLinearIndex(*grid_iter + rhs.Global().BeginLocal() + stencil_iter->Disp());
|
|---|
| 59 |
|
|---|
| 60 | assert(mat_index2 >= 0 && mat_index2<this->Size());
|
|---|
| 61 |
|
|---|
| 62 | this->Mat(mat_index, mat_index2) += stencil_iter->Val();
|
|---|
| 63 |
|
|---|
| 64 | }
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | for (int i=0; i<3; ++i) {
|
|---|
| 68 |
|
|---|
| 69 | for (grid_iter = rhs.Iterators().Boundary1()[i].Begin(); grid_iter != rhs.Iterators().Boundary1()[i].End(); ++grid_iter) {
|
|---|
| 70 |
|
|---|
| 71 | mat_index = rhs.GlobalLinearIndex(*grid_iter + rhs.Global().BeginLocal());
|
|---|
| 72 |
|
|---|
| 73 | assert(mat_index >= 0 && mat_index<this->Size());
|
|---|
| 74 |
|
|---|
| 75 | this->Sol(mat_index) = this->Rhs(mat_index) = rhs.GetVal(*grid_iter);
|
|---|
| 76 |
|
|---|
| 77 | for (int l=0; l<this->Size(); l++)
|
|---|
| 78 | this->Mat(mat_index, l) = 0.0;
|
|---|
| 79 |
|
|---|
| 80 | this->Mat(mat_index, mat_index) = 1.0;
|
|---|
| 81 |
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | for (grid_iter = rhs.Iterators().Boundary2()[i].Begin(); grid_iter != rhs.Iterators().Boundary2()[i].End(); ++grid_iter) {
|
|---|
| 85 |
|
|---|
| 86 | mat_index = rhs.GlobalLinearIndex(*grid_iter + rhs.Global().BeginLocal());
|
|---|
| 87 |
|
|---|
| 88 | assert(mat_index >= 0 && mat_index<this->Size());
|
|---|
| 89 |
|
|---|
| 90 | this->Sol(mat_index) = this->Rhs(mat_index) = rhs.GetVal(*grid_iter);
|
|---|
| 91 |
|
|---|
| 92 | for (int l=0; l<this->Size(); l++)
|
|---|
| 93 | this->Mat(mat_index, l) = 0.0;
|
|---|
| 94 |
|
|---|
| 95 | this->Mat(mat_index, mat_index) = 1.0;
|
|---|
| 96 |
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | void SolverRegular::ExportSol(Grid& sol, Grid& rhs)
|
|---|
| 104 | {
|
|---|
| 105 | int index;
|
|---|
| 106 | Index offset;
|
|---|
| 107 |
|
|---|
| 108 | for (int i=0; i<3; ++i)
|
|---|
| 109 | offset[i] = (sol.Local().HaloEnd1()[i] > 0 ? 1 : 0);
|
|---|
| 110 |
|
|---|
| 111 | for (Grid::iterator iter = sol.Iterators().CompleteGrid().Begin(); iter != sol.Iterators().CompleteGrid().End(); ++iter) {
|
|---|
| 112 | index = sol.GlobalLinearIndex(sol.Global().BeginLocal() + *iter - offset);
|
|---|
| 113 | sol(*iter) = this->Sol(index);
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | #ifdef DEBUG_MATRIX_CHECKS
|
|---|
| 117 | sol.IsConsistent();
|
|---|
| 118 | #endif
|
|---|
| 119 | }
|
|---|