Reference documentation for deal.II version 8.4.2
dynamic_sparsity_pattern.cc
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2008 - 2015 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE at
12 // the top level of the deal.II distribution.
13 //
14 // ---------------------------------------------------------------------
15 
16 #include <deal.II/lac/dynamic_sparsity_pattern.h>
17 #include <deal.II/base/memory_consumption.h>
18 
19 #include <algorithm>
20 #include <cmath>
21 #include <numeric>
22 #include <functional>
23 
24 DEAL_II_NAMESPACE_OPEN
25 
26 
27 
28 template <typename ForwardIterator>
29 void
31  ForwardIterator end,
32  const bool indices_are_sorted)
33 {
34  const int n_elements = end - begin;
35  if (n_elements <= 0)
36  return;
37 
38  const size_type stop_size = entries.size() + n_elements;
39 
40  if (indices_are_sorted == true && n_elements > 3)
41  {
42  // in debug mode, check whether the
43  // indices really are sorted.
44 #ifdef DEBUG
45  {
46  ForwardIterator test = begin, test1 = begin;
47  ++test1;
48  for ( ; test1 != end; ++test, ++test1)
49  Assert (*test1 > *test, ExcInternalError());
50  }
51 #endif
52 
53  if (entries.size() == 0 || entries.back() < *begin)
54  {
55  entries.insert(entries.end(), begin, end);
56  return;
57  }
58 
59  // find a possible insertion point for
60  // the first entry. check whether the
61  // first entry is a duplicate before
62  // actually doing something.
63  ForwardIterator my_it = begin;
64  size_type col = *my_it;
65  std::vector<size_type>::iterator it =
66  Utilities::lower_bound(entries.begin(), entries.end(), col);
67  while (*it == col)
68  {
69  ++my_it;
70  if (my_it == end)
71  break;
72  col = *my_it;
73  // check the very next entry in the
74  // current array
75  ++it;
76  if (it == entries.end())
77  break;
78  if (*it > col)
79  break;
80  if (*it == col)
81  continue;
82  // ok, it wasn't the very next one, do a
83  // binary search to find the insert point
84  it = Utilities::lower_bound(it, entries.end(), col);
85  if (it == entries.end())
86  break;
87  }
88  // all input entries were duplicates.
89  if (my_it == end)
90  return;
91 
92  // resize vector by just inserting the
93  // list
94  const size_type pos1 = it - entries.begin();
95  Assert (pos1 <= entries.size(), ExcInternalError());
96  entries.insert (it, my_it, end);
97  it = entries.begin() + pos1;
98  Assert (entries.size() >= (size_type)(it-entries.begin()), ExcInternalError());
99 
100  // now merge the two lists.
101  std::vector<size_type>::iterator it2 = it + (end-my_it);
102 
103  // as long as there are indices both in
104  // the end of the entries list and in the
105  // input list
106  while (my_it != end && it2 != entries.end())
107  {
108  if (*my_it < *it2)
109  *it++ = *my_it++;
110  else if (*my_it == *it2)
111  {
112  *it++ = *it2++;
113  ++my_it;
114  }
115  else
116  *it++ = *it2++;
117  }
118  // in case there are indices left in the
119  // input list
120  while (my_it != end)
121  *it++ = *my_it++;
122 
123  // in case there are indices left in the
124  // end of entries
125  while (it2 != entries.end())
126  *it++ = *it2++;
127 
128  // resize and return
129  const size_type new_size = it - entries.begin();
130  Assert (new_size <= stop_size, ExcInternalError());
131  entries.resize (new_size);
132  return;
133  }
134 
135  // unsorted case or case with too few
136  // elements
137  ForwardIterator my_it = begin;
138 
139  // If necessary, increase the size of the
140  // array.
141  if (stop_size > entries.capacity())
142  entries.reserve (stop_size);
143 
144  size_type col = *my_it;
145  std::vector<size_type>::iterator it, it2;
146  // insert the first element as for one
147  // entry only first check the last
148  // element (or if line is still empty)
149  if ( (entries.size()==0) || ( entries.back() < col) )
150  {
151  entries.push_back(col);
152  it = entries.end()-1;
153  }
154  else
155  {
156  // do a binary search to find the place
157  // where to insert:
158  it2 = Utilities::lower_bound(entries.begin(), entries.end(), col);
159 
160  // If this entry is a duplicate, continue
161  // immediately Insert at the right place
162  // in the vector. Vector grows
163  // automatically to fit elements. Always
164  // doubles its size.
165  if (*it2 != col)
166  it = entries.insert(it2, col);
167  else
168  it = it2;
169  }
170 
171  ++my_it;
172  // Now try to be smart and insert with
173  // bias in the direction we are
174  // walking. This has the advantage that
175  // for sorted lists, we always search in
176  // the right direction, what should
177  // decrease the work needed in here.
178  for ( ; my_it != end; ++my_it)
179  {
180  col = *my_it;
181  // need a special insertion command when
182  // we're at the end of the list
183  if (col > entries.back())
184  {
185  entries.push_back(col);
186  it = entries.end()-1;
187  }
188  // search to the right (preferred search
189  // direction)
190  else if (col > *it)
191  {
192  it2 = Utilities::lower_bound(it++, entries.end(), col);
193  if (*it2 != col)
194  it = entries.insert(it2, col);
195  }
196  // search to the left
197  else if (col < *it)
198  {
199  it2 = Utilities::lower_bound(entries.begin(), it, col);
200  if (*it2 != col)
201  it = entries.insert(it2, col);
202  }
203  // if we're neither larger nor smaller,
204  // then this was a duplicate and we can
205  // just continue.
206  }
207 }
208 
209 
212 {
213  return entries.capacity()*sizeof(size_type)+sizeof(Line);
214 }
215 
216 
218  :
219  rows(0),
220  cols(0),
221  rowset(0)
222 {}
223 
224 
225 
228  :
229  Subscriptor(),
230  rows(0),
231  cols(0),
232  rowset(0)
233 {
234  (void)s;
235  Assert (s.rows == 0, ExcInvalidConstructorCall());
236  Assert (s.cols == 0, ExcInvalidConstructorCall());
237 }
238 
239 
240 
242  const size_type n,
243  const IndexSet &rowset_
244  )
245  :
246  rows(0),
247  cols(0),
248  rowset(0)
249 {
250  reinit (m,n, rowset_);
251 }
252 
253 
255  :
256  rows(0),
257  cols(0),
258  rowset(0)
259 {
260  reinit (rowset_.size(), rowset_.size(), rowset_);
261 }
262 
263 
265  :
266  rows(0),
267  cols(0),
268  rowset(0)
269 {
270  reinit (n,n);
271 }
272 
273 
274 
277 {
278  (void)s;
279  Assert (s.rows == 0, ExcInvalidConstructorCall());
280  Assert (s.cols == 0, ExcInvalidConstructorCall());
281 
282  Assert (rows == 0, ExcInvalidConstructorCall());
283  Assert (cols == 0, ExcInvalidConstructorCall());
284 
285  return *this;
286 }
287 
288 
289 
290 void
292  const size_type n,
293  const IndexSet &rowset_)
294 {
295  rows = m;
296  cols = n;
297  rowset=rowset_;
298 
299  Assert(rowset.size()==0 || rowset.size() == m, ExcInvalidConstructorCall());
300 
301  std::vector<Line> new_lines (rowset.size()==0 ? rows : rowset.n_elements());
302  lines.swap (new_lines);
303 }
304 
305 
306 
307 void
309 {}
310 
311 
312 
313 bool
315 {
316  return ((rows==0) && (cols==0));
317 }
318 
319 
320 
323 {
324  size_type m = 0;
325  for (size_type i=0; i<lines.size(); ++i)
326  {
327  m = std::max (m, static_cast<size_type>(lines[i].entries.size()));
328  }
329 
330  return m;
331 }
332 
333 
334 
335 bool
337  const size_type j) const
338 {
339  Assert (i<rows, ExcIndexRange(i, 0, rows));
340  Assert (j<cols, ExcIndexRange(j, 0, cols));
341  Assert( rowset.size()==0 || rowset.is_element(i), ExcInternalError());
342 
343  const size_type rowindex =
344  rowset.size()==0 ? i : rowset.index_within_set(i);
345 
346  return std::binary_search (lines[rowindex].entries.begin(),
347  lines[rowindex].entries.end(),
348  j);
349 }
350 
351 
352 
353 void
355 {
356  Assert (rows==cols, ExcNotQuadratic());
357 
358  // loop over all elements presently
359  // in the sparsity pattern and add
360  // the transpose element. note:
361  //
362  // 1. that the sparsity pattern
363  // changes which we work on, but
364  // not the present row
365  //
366  // 2. that the @p{add} function can
367  // be called on elements that
368  // already exist without any harm
369  for (size_type row=0; row<lines.size(); ++row)
370  {
371  const size_type rowindex =
372  rowset.size()==0 ? row : rowset.nth_index_in_set(row);
373 
374  for (std::vector<size_type>::const_iterator
375  j=lines[row].entries.begin();
376  j != lines[row].entries.end();
377  ++j)
378  // add the transpose entry if
379  // this is not the diagonal
380  if (rowindex != *j)
381  add (*j, rowindex);
382  }
383 }
384 
385 
386 
387 void
388 DynamicSparsityPattern::print (std::ostream &out) const
389 {
390  for (size_type row=0; row<lines.size(); ++row)
391  {
392  out << '[' << (rowset.size()==0 ? row : rowset.nth_index_in_set(row));
393 
394  for (std::vector<size_type >::const_iterator
395  j=lines[row].entries.begin();
396  j != lines[row].entries.end(); ++j)
397  out << ',' << *j;
398 
399  out << ']' << std::endl;
400  }
401 
402  AssertThrow (out, ExcIO());
403 }
404 
405 
406 
407 void
408 DynamicSparsityPattern::print_gnuplot (std::ostream &out) const
409 {
410  for (size_type row=0; row<lines.size(); ++row)
411  {
412  const size_type rowindex =
413  rowset.size()==0 ? row : rowset.nth_index_in_set(row);
414 
415  for (std::vector<size_type >::const_iterator
416  j=lines[row].entries.begin();
417  j != lines[row].entries.end(); ++j)
418  // while matrix entries are usually
419  // written (i,j), with i vertical and
420  // j horizontal, gnuplot output is
421  // x-y, that is we have to exchange
422  // the order of output
423  out << *j << " "
424  << -static_cast<signed int>(rowindex)
425  << std::endl;
426  }
427 
428 
429  AssertThrow (out, ExcIO());
430 }
431 
432 
433 
436 {
437  size_type b=0;
438  for (size_type row=0; row<lines.size(); ++row)
439  {
440  const size_type rowindex =
441  rowset.size()==0 ? row : rowset.nth_index_in_set(row);
442 
443  for (std::vector<size_type>::const_iterator
444  j=lines[row].entries.begin();
445  j != lines[row].entries.end(); ++j)
446  if (static_cast<size_type>(std::abs(static_cast<int>(rowindex-*j))) > b)
447  b = std::abs(static_cast<signed int>(rowindex-*j));
448  }
449 
450  return b;
451 }
452 
453 
454 
457 {
458  size_type n=0;
459  for (size_type i=0; i<lines.size(); ++i)
460  {
461  n += lines[i].entries.size();
462  }
463 
464  return n;
465 }
466 
467 
470 {
471  //TODO: IndexSet...
472  size_type mem = sizeof(DynamicSparsityPattern);
473  for (size_type i=0; i<lines.size(); ++i)
475 
476  return mem;
477 }
478 
479 
480 // explicit instantiations
482  size_type *,
483  const bool);
485  const size_type *,
486  const bool);
487 #ifndef DEAL_II_VECTOR_ITERATOR_IS_POINTER
488 template void DynamicSparsityPattern::Line::
489 add_entries(std::vector<size_type>::iterator,
490  std::vector<size_type>::iterator,
491  const bool);
492 #endif
493 
494 DEAL_II_NAMESPACE_CLOSE
Iterator lower_bound(Iterator first, Iterator last, const T &val)
Definition: utilities.h:604
size_type nth_index_in_set(const unsigned int local_index) const
Definition: index_set.h:1432
void add(const size_type i, const size_type j)
#define AssertThrow(cond, exc)
Definition: exceptions.h:358
types::global_dof_index size_type
size_type size() const
Definition: index_set.h:1247
#define Assert(cond, exc)
Definition: exceptions.h:294
void add_entries(ForwardIterator begin, ForwardIterator end, const bool indices_are_sorted)
void reinit(const size_type m, const size_type n, const IndexSet &rowset=IndexSet())
size_type index_within_set(const size_type global_index) const
Definition: index_set.h:1475
void print_gnuplot(std::ostream &out) const
std_cxx11::enable_if< std_cxx11::is_fundamental< T >::value, std::size_t >::type memory_consumption(const T &t)
DynamicSparsityPattern & operator=(const DynamicSparsityPattern &)
void print(std::ostream &out) const
bool is_element(const size_type index) const
Definition: index_set.h:1317
bool exists(const size_type i, const size_type j) const
size_type n_elements() const
Definition: index_set.h:1380