Line data Source code
1 : /*
2 : * Copyright (c) 2022: G-CSC, Goethe University Frankfurt
3 : * Author: Felix Salfelder, 2022
4 : *
5 : * This file is part of UG4.
6 : *
7 : * UG4 is free software: you can redistribute it and/or modify it under the
8 : * terms of the GNU Lesser General Public License version 3 (as published by the
9 : * Free Software Foundation) with the following additional attribution
10 : * requirements (according to LGPL/GPL v3 §7):
11 : *
12 : * (1) The following notice must be displayed in the Appropriate Legal Notices
13 : * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14 : *
15 : * (2) The following notice must be displayed at a prominent place in the
16 : * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17 : *
18 : * (3) The following bibliography is recommended for citation and must be
19 : * preserved in all covered files:
20 : * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21 : * parallel geometric multigrid solver on hierarchically distributed grids.
22 : * Computing and visualization in science 16, 4 (2013), 151-164"
23 : * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24 : * flexible software system for simulating pde based models on high performance
25 : * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26 : *
27 : * This program is distributed in the hope that it will be useful,
28 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 : * GNU Lesser General Public License for more details.
31 : */
32 :
33 : #ifndef UG_GRAPH_INTERFACE_BIDIR_H
34 : #define UG_GRAPH_INTERFACE_BIDIR_H
35 :
36 : #include "sparsematrix_boost.h"
37 :
38 : #ifndef NDEBUG
39 : #include "common/stopwatch.h"
40 : #endif
41 :
42 : namespace ug{
43 :
44 : // give access to edges both ways.
45 : // keep a transpose matrix to cache iterators.
46 : template<class T>
47 0 : class BidirectionalMatrix{
48 : public: // types
49 : typedef typename T::const_row_iterator const_row_iterator;
50 : public:
51 0 : explicit BidirectionalMatrix(T const* m=nullptr)
52 0 : : _matrix(m) {
53 0 : if(m){
54 : refresh();
55 : }else{
56 : }
57 0 : }
58 : explicit BidirectionalMatrix(BidirectionalMatrix const& o)
59 : : _matrix(o._matrix) {
60 : _matrix_transpose = o._matrix_transpose; // BUG: missing copy constructor.
61 : }
62 : BidirectionalMatrix& operator=(BidirectionalMatrix const& o) {
63 0 : _matrix = o._matrix;
64 0 : _matrix_transpose = o._matrix_transpose;
65 0 : return *this;
66 : }
67 :
68 : public: // interface
69 : void refresh(){
70 : #ifndef NDEBUG
71 : double t = -get_clock_s();
72 : #endif
73 : assert(_matrix);
74 0 : _matrix_transpose.set_as_transpose_of2(*_matrix);
75 : assert(_matrix->num_rows() == _matrix_transpose.num_cols());
76 : assert(_matrix->num_cols() == _matrix_transpose.num_rows());
77 : #ifndef NDEBUG
78 : t += get_clock_s();
79 : UG_LOG("Transpose in bidirectional refresh " << t << "\n");
80 : #endif
81 0 : }
82 :
83 : int num_rows() const {
84 0 : return std::max(_matrix->num_rows(), _matrix->num_cols());
85 : }
86 : int num_cols() const { untested();
87 : return num_rows();
88 : }
89 : int num_connections(int v) const {
90 : if(v<_matrix->num_rows()){
91 : return _matrix->num_connections(v);
92 : }else{
93 : assert(v<_matrix_transpose.num_rows());
94 : return _matrix_transpose.num_connections(v);
95 : }
96 : }
97 : int out_degree(int v) const {
98 : assert(_matrix);
99 0 : if(size_t(v)<_matrix->num_rows()){
100 : assert(size_t(v)<_matrix->num_rows());
101 0 : return boost::out_degree(v, *_matrix);
102 : }else{
103 : return 0;
104 : }
105 : }
106 : int in_degree(int v) const {
107 : // could use difference, requires zero-pruning in _matrix_transpose.
108 : if(size_t(v)<_matrix_transpose.num_rows()){
109 : return boost::out_degree(v, _matrix_transpose);
110 : }else{
111 : return 0;
112 : }
113 : }
114 : int degree(int v) const { untested();
115 : return 2*out_degree(v);
116 : }
117 :
118 : const_row_iterator begin_row(int row) const {
119 : assert(_matrix);
120 : if(size_t(row)<_matrix->num_rows()){
121 : }else{
122 : row = 0;
123 : }
124 : return _matrix->begin_row(row);
125 : }
126 : const_row_iterator end_row(int row) const {
127 : assert(_matrix);
128 : if(size_t(row)<_matrix->num_rows()){
129 : return _matrix->end_row(row);
130 : }else{
131 : return _matrix->begin_row(0);
132 : }
133 : }
134 :
135 : const_row_iterator begin_col(int col) const {
136 0 : if(size_t(col)<_matrix_transpose.num_rows()){
137 : }else{
138 : col = 0;
139 : }
140 0 : return _matrix_transpose.begin_row(col);
141 : }
142 : const_row_iterator end_col(int col) const {
143 0 : if(size_t(col)<_matrix_transpose.num_rows()){
144 : return _matrix_transpose.end_row(col);
145 : }else{
146 : return _matrix_transpose.begin_row(0);
147 : }
148 : }
149 :
150 : private:
151 : T const* _matrix;
152 : T _matrix_transpose;
153 : };
154 :
155 : } // ug
156 :
157 : #endif // guard
|