Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Stokhos_Sparse3TensorImp.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Stokhos Package
4 //
5 // Copyright 2009 NTESS and the Stokhos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #include "Stokhos_ConfigDefs.h"
11 #include "Teuchos_Assert.hpp"
12 
13 template <typename ordinal_type, typename value_type>
16  fill_completed(false)
17 {
18 }
19 
20 template <typename ordinal_type, typename value_type>
21 void
24 {
25 #ifdef STOKHOS_DEBUG
26  TEUCHOS_TEST_FOR_EXCEPTION(fill_completed == true, std::logic_error,
27  "You can't call add_term() after calling fillComplete()!");
28 #endif
29 
30  kji_data[k][j][i] = c;
31  ikj_data[i][k][j] = c;
32 }
33 
34 template <typename ordinal_type, typename value_type>
35 void
38 {
39 #ifdef STOKHOS_DEBUG
40  TEUCHOS_TEST_FOR_EXCEPTION(fill_completed == true, std::logic_error,
41  "You can't call sum_term() after calling fillComplete()!");
42 #endif
43 
44  kji_data[k][j][i] += c;
45  ikj_data[i][k][j] += c;
46 }
47 
48 template <typename ordinal_type, typename value_type>
49 void
52 {
53  if (fill_completed)
54  return;
55 
56  ikj_array.resize(ikj_data.size());
57  ordinal_type n = 0;
58  for (typename ikj_map::const_iterator i_it = ikj_data.begin();
59  i_it != ikj_data.end(); ++i_it) {
60  ikj_array.indices[n] = i_it->first;
61  ikj_array.values[n].resize(i_it->second.size());
62  ordinal_type l = 0;
63  for (typename kj_map::const_iterator k_it = i_it->second.begin();
64  k_it != i_it->second.end(); ++k_it) {
65  ikj_array.values[n].indices[l] = k_it->first;
66  ikj_array.values[n].values[l].resize(k_it->second.size());
67  ordinal_type m = 0;
68  for (typename j_map::const_iterator j_it = k_it->second.begin();
69  j_it != k_it->second.end(); ++j_it) {
70  ikj_array.values[n].values[l].indices[m] = j_it->first;
71  ikj_array.values[n].values[l].values[m] = j_it->second;
72  m++;
73  }
74  l++;
75  }
76  n++;
77  }
78 
79  kji_array.resize(kji_data.size());
80  n = 0;
81  for (typename kji_map::const_iterator k_it = kji_data.begin();
82  k_it != kji_data.end(); ++k_it) {
83  kji_array.indices[n] = k_it->first;
84  kji_array.values[n].resize(k_it->second.size());
85  ordinal_type l = 0;
86  for (typename ji_map::const_iterator j_it = k_it->second.begin();
87  j_it != k_it->second.end(); ++j_it) {
88  kji_array.values[n].indices[l] = j_it->first;
89  kji_array.values[n].values[l].resize(j_it->second.size());
90  ordinal_type m = 0;
91  for (typename i_map::const_iterator i_it = j_it->second.begin();
92  i_it != j_it->second.end(); ++i_it) {
93  kji_array.values[n].values[l].indices[m] = i_it->first;
94  kji_array.values[n].values[l].values[m] = i_it->second;
95  m++;
96  }
97  l++;
98  }
99  n++;
100  }
101 
102  ikj_data.clear();
103  kji_data.clear();
104 
105  fill_completed = true;
106 }
107 
108 template <typename ordinal_type, typename value_type>
109 bool
112 {
113  return fill_completed;
114 }
115 
116 template <typename ordinal_type, typename value_type>
117 void
119 print(std::ostream& os) const
120 {
121 #ifdef STOKHOS_DEBUG
122  TEUCHOS_TEST_FOR_EXCEPTION(fill_completed == false, std::logic_error,
123  "You must call fillComplete() before calling print()!");
124 #endif
125 
126  for (k_iterator k=k_begin(); k!=k_end(); ++k)
127  for (kj_iterator j=j_begin(k); j!=j_end(k); ++j)
128  for (kji_iterator i=i_begin(j); i!=i_end(j); ++i)
129  os << "k = " << index(k)
130  << ", j = " << index(j)
131  << ", i = " << index(i)
132  << ", Cijk = " << value(i) << std::endl;
133 }
134 
135 template <typename ordinal_type, typename value_type>
139 {
140 #ifdef STOKHOS_DEBUG
141  TEUCHOS_TEST_FOR_EXCEPTION(fill_completed == false, std::logic_error,
142  "You must call fillComplete() before calling getValue()!");
143 #endif
144 
145  k_iterator k_it = find_k(k);
146  if (k_it == k_end())
147  return value_type(0);
148 
149  kj_iterator j_it = find_j(k_it, j);
150  if (j_it == j_end(k_it))
151  return value_type(0);
152 
153  kji_iterator i_it = find_i(j_it, i);
154  if (i_it == i_end(j_it))
155  return value_type(0);
156 
157  return i_it.value();
158 }
159 
160 template <typename ordinal_type, typename value_type>
163 num_entries() const
164 {
165 #ifdef STOKHOS_DEBUG
166  TEUCHOS_TEST_FOR_EXCEPTION(fill_completed == false, std::logic_error,
167  "You must call fillComplete() before calling num_entries()!");
168 #endif
169 
170  ordinal_type num = 0;
171  for (k_iterator k = k_begin(); k != k_end(); ++k)
172  for (kj_iterator j = j_begin(k); j != j_end(k); ++j)
173  for (kji_iterator i = i_begin(j); i != i_end(j); ++i)
174  ++num;
175 
176  return num;
177 }
178 
179 template <typename ordinal_type, typename value_type>
182 num_k() const
183 {
184 #ifdef STOKHOS_DEBUG
185  TEUCHOS_TEST_FOR_EXCEPTION(fill_completed == false, std::logic_error,
186  "You must call fillComplete() before calling num_k()!");
187 #endif
188  return kji_array.size();
189 }
190 
191 template <typename ordinal_type, typename value_type>
194 num_j(const k_iterator& k) const
195 {
196 #ifdef STOKHOS_DEBUG
197  TEUCHOS_TEST_FOR_EXCEPTION(fill_completed == false, std::logic_error,
198  "You must call fillComplete() before calling num_j()!");
199 #endif
200 
201  return k.value().size();
202 }
203 
204 template <typename ordinal_type, typename value_type>
207 num_i(const kj_iterator& j) const
208 {
209 #ifdef STOKHOS_DEBUG
210  TEUCHOS_TEST_FOR_EXCEPTION(fill_completed == false, std::logic_error,
211  "You must call fillComplete() before calling num_i()!");
212 #endif
213  return j.value().size();
214 }
215 
216 template <typename ordinal_type, typename value_type>
220 {
221 #ifdef STOKHOS_DEBUG
222  TEUCHOS_TEST_FOR_EXCEPTION(fill_completed == false, std::logic_error,
223  "You must call fillComplete() before calling find_k()!");
224 #endif
225  return kji_array.find(k);
226 }
227 
228 template <typename ordinal_type, typename value_type>
231 find_j(const k_iterator& k, ordinal_type j) const
232 {
233 #ifdef STOKHOS_DEBUG
234  TEUCHOS_TEST_FOR_EXCEPTION(fill_completed == false, std::logic_error,
235  "You must call fillComplete() before calling find_j()!");
236 #endif
237  return k.value().find(j);
238 }
239 
240 template <typename ordinal_type, typename value_type>
243 find_i(const kj_iterator& j, ordinal_type i) const
244 {
245 #ifdef STOKHOS_DEBUG
246  TEUCHOS_TEST_FOR_EXCEPTION(fill_completed == false, std::logic_error,
247  "You must call fillComplete() before calling find_i()!");
248 #endif
249  return j.value().find(i);
250 }
251 
252 template <typename ordinal_type, typename value_type>
255 k_begin() const
256 {
257 #ifdef STOKHOS_DEBUG
258  TEUCHOS_TEST_FOR_EXCEPTION(fill_completed == false, std::logic_error,
259  "You must call fillComplete() before calling k_begin()!");
260 #endif
261  return kji_array.begin();
262 }
263 
264 template <typename ordinal_type, typename value_type>
267 k_end() const
268 {
269 #ifdef STOKHOS_DEBUG
270  TEUCHOS_TEST_FOR_EXCEPTION(fill_completed == false, std::logic_error,
271  "You must call fillComplete() before calling k_end()!");
272 #endif
273  return kji_array.end();
274 }
275 
276 template <typename ordinal_type, typename value_type>
279 k_rbegin() const
280 {
281 #ifdef STOKHOS_DEBUG
282  TEUCHOS_TEST_FOR_EXCEPTION(fill_completed == false, std::logic_error,
283  "You must call fillComplete() before calling k_rbegin()!");
284 #endif
285  return kji_array.rbegin();
286 }
287 
288 template <typename ordinal_type, typename value_type>
291 k_rend() const
292 {
293 #ifdef STOKHOS_DEBUG
294  TEUCHOS_TEST_FOR_EXCEPTION(fill_completed == false, std::logic_error,
295  "You must call fillComplete() before calling k_rend()!");
296 #endif
297  return kji_array.rend();
298 }
299 
300 template <typename ordinal_type, typename value_type>
303 j_begin(const k_iterator& k) const
304 {
305 #ifdef STOKHOS_DEBUG
306  TEUCHOS_TEST_FOR_EXCEPTION(fill_completed == false, std::logic_error,
307  "You must call fillComplete() before calling j_begin()!");
308 #endif
309  return k.value().begin();
310 }
311 
312 template <typename ordinal_type, typename value_type>
315 j_end(const k_iterator& k) const
316 {
317 #ifdef STOKHOS_DEBUG
318  TEUCHOS_TEST_FOR_EXCEPTION(fill_completed == false, std::logic_error,
319  "You must call fillComplete() before calling j_end()!");
320 #endif
321  return k.value().end();
322 }
323 
324 template <typename ordinal_type, typename value_type>
327 j_begin(const k_reverse_iterator& k) const
328 {
329 #ifdef STOKHOS_DEBUG
330  TEUCHOS_TEST_FOR_EXCEPTION(fill_completed == false, std::logic_error,
331  "You must call fillComplete() before calling j_begin()!");
332 #endif
333  return k.value().begin();
334 }
335 
336 template <typename ordinal_type, typename value_type>
339 j_end(const k_reverse_iterator& k) const
340 {
341 #ifdef STOKHOS_DEBUG
342  TEUCHOS_TEST_FOR_EXCEPTION(fill_completed == false, std::logic_error,
343  "You must call fillComplete() before calling j_end()!");
344 #endif
345  return k.value().end();
346 }
347 
348 template <typename ordinal_type, typename value_type>
351 i_begin(const kj_iterator& j) const
352 {
353 #ifdef STOKHOS_DEBUG
354  TEUCHOS_TEST_FOR_EXCEPTION(fill_completed == false, std::logic_error,
355  "You must call fillComplete() before calling i_begin()!");
356 #endif
357  return j.value().begin();
358 }
359 
360 template <typename ordinal_type, typename value_type>
363 i_end(const kj_iterator& j) const
364 {
365 #ifdef STOKHOS_DEBUG
366  TEUCHOS_TEST_FOR_EXCEPTION(fill_completed == false, std::logic_error,
367  "You must call fillComplete() before calling i_end()!");
368 #endif
369  return j.value().end();
370 }
371 
372 template <typename ordinal_type, typename value_type>
375 num_i() const
376 {
377 #ifdef STOKHOS_DEBUG
378  TEUCHOS_TEST_FOR_EXCEPTION(fill_completed == false, std::logic_error,
379  "You must call fillComplete() before calling num_i()!");
380 #endif
381  return ikj_array.size();
382 }
383 
384 template <typename ordinal_type, typename value_type>
387 num_k(const i_iterator& i) const
388 {
389 #ifdef STOKHOS_DEBUG
390  TEUCHOS_TEST_FOR_EXCEPTION(fill_completed == false, std::logic_error,
391  "You must call fillComplete() before calling num_k()!");
392 #endif
393  return i.value().size();
394 }
395 
396 template <typename ordinal_type, typename value_type>
399 num_j(const ik_iterator& k) const
400 {
401 #ifdef STOKHOS_DEBUG
402  TEUCHOS_TEST_FOR_EXCEPTION(fill_completed == false, std::logic_error,
403  "You must call fillComplete() before calling num_j()!");
404 #endif
405  return k.value().size();
406 }
407 
408 template <typename ordinal_type, typename value_type>
412 {
413 #ifdef STOKHOS_DEBUG
414  TEUCHOS_TEST_FOR_EXCEPTION(fill_completed == false, std::logic_error,
415  "You must call fillComplete() before calling find_i()!");
416 #endif
417  return ikj_array.find(i);
418 }
419 
420 template <typename ordinal_type, typename value_type>
423 find_k(const i_iterator& i, ordinal_type k) const
424 {
425 #ifdef STOKHOS_DEBUG
426  TEUCHOS_TEST_FOR_EXCEPTION(fill_completed == false, std::logic_error,
427  "You must call fillComplete() before calling find_k()!");
428 #endif
429  return i.value().find(k);
430 }
431 
432 template <typename ordinal_type, typename value_type>
435 find_j(const ik_iterator& k, ordinal_type j) const
436 {
437 #ifdef STOKHOS_DEBUG
438  TEUCHOS_TEST_FOR_EXCEPTION(fill_completed == false, std::logic_error,
439  "You must call fillComplete() before calling find_j()!");
440 #endif
441  return k.value().find(j);
442 }
443 
444 template <typename ordinal_type, typename value_type>
447 i_begin() const
448 {
449 #ifdef STOKHOS_DEBUG
450  TEUCHOS_TEST_FOR_EXCEPTION(fill_completed == false, std::logic_error,
451  "You must call fillComplete() before calling i_begin()!");
452 #endif
453  return ikj_array.begin();
454 }
455 
456 template <typename ordinal_type, typename value_type>
459 i_end() const
460 {
461 #ifdef STOKHOS_DEBUG
462  TEUCHOS_TEST_FOR_EXCEPTION(fill_completed == false, std::logic_error,
463  "You must call fillComplete() before calling i_end()!");
464 #endif
465  return ikj_array.end();
466 }
467 
468 template <typename ordinal_type, typename value_type>
471 i_rbegin() const
472 {
473 #ifdef STOKHOS_DEBUG
474  TEUCHOS_TEST_FOR_EXCEPTION(fill_completed == false, std::logic_error,
475  "You must call fillComplete() before calling i_rbegin()!");
476 #endif
477  return ikj_array.rbegin();
478 }
479 
480 template <typename ordinal_type, typename value_type>
483 i_rend() const
484 {
485 #ifdef STOKHOS_DEBUG
486  TEUCHOS_TEST_FOR_EXCEPTION(fill_completed == false, std::logic_error,
487  "You must call fillComplete() before calling i_rend()!");
488 #endif
489  return ikj_array.rend();
490 }
491 
492 template <typename ordinal_type, typename value_type>
495 k_begin(const i_iterator& i) const
496 {
497 #ifdef STOKHOS_DEBUG
498  TEUCHOS_TEST_FOR_EXCEPTION(fill_completed == false, std::logic_error,
499  "You must call fillComplete() before calling k_begin()!");
500 #endif
501  return i.value().begin();
502 }
503 
504 template <typename ordinal_type, typename value_type>
507 k_end(const i_iterator& i) const
508 {
509 #ifdef STOKHOS_DEBUG
510  TEUCHOS_TEST_FOR_EXCEPTION(fill_completed == false, std::logic_error,
511  "You must call fillComplete() before calling k_end()!");
512 #endif
513  return i.value().end();
514 }
515 
516 template <typename ordinal_type, typename value_type>
519 k_begin(const i_reverse_iterator& i) const
520 {
521 #ifdef STOKHOS_DEBUG
522  TEUCHOS_TEST_FOR_EXCEPTION(fill_completed == false, std::logic_error,
523  "You must call fillComplete() before calling k_begin()!");
524 #endif
525  return i.value().begin();
526 }
527 
528 template <typename ordinal_type, typename value_type>
531 k_end(const i_reverse_iterator& i) const
532 {
533 #ifdef STOKHOS_DEBUG
534  TEUCHOS_TEST_FOR_EXCEPTION(fill_completed == false, std::logic_error,
535  "You must call fillComplete() before calling k_end()!");
536 #endif
537  return i.value().end();
538 }
539 
540 template <typename ordinal_type, typename value_type>
543 j_begin(const ik_iterator& k) const
544 {
545 #ifdef STOKHOS_DEBUG
546  TEUCHOS_TEST_FOR_EXCEPTION(fill_completed == false, std::logic_error,
547  "You must call fillComplete() before calling j_begin()!");
548 #endif
549  return k.value().begin();
550 }
551 
552 template <typename ordinal_type, typename value_type>
555 j_end(const ik_iterator& k) const
556 {
557 #ifdef STOKHOS_DEBUG
558  TEUCHOS_TEST_FOR_EXCEPTION(fill_completed == false, std::logic_error,
559  "You must call fillComplete() before calling j_end()!");
560 #endif
561  return k.value().end();
562 }
k_iterator k_begin() const
Iterator pointing to first k entry.
ordinal_type num_j(const k_iterator &k) const
Number of j entries in C(i,j,k) for given k.
ordinal_type num_k() const
Number of k entries in C(i,j,k)
kj_iterator j_begin(const k_iterator &k) const
Iterator pointing to first j entry for given k.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
void fillComplete()
Signal all terms have been added.
i_reverse_iterator i_rbegin() const
Reverse iterator pointing to last k entry.
kj_iterator j_end(const k_iterator &k) const
Iterator pointing to last j entry for given k.
Bi-directional iterator for traversing a sparse array.
Bi-directional reverse iterator for traversing a sparse array.
void add_term(ordinal_type i, ordinal_type j, ordinal_type k, const value_type &c)
Add new term for given (i,j,k)
value_reference value() const
Return value associated with iterator.
i_reverse_iterator i_rend() const
Reverse iterator pointing to first k entry.
k_iterator k_end() const
Iterator pointing to last k entry.
k_iterator find_k(ordinal_type k) const
Return k iterator for given index k.
i_iterator i_begin() const
Iterator pointing to first k entry.
bool fillCompleted() const
Return whether fillComplete() has been called.
kji_iterator find_i(const kj_iterator &j, ordinal_type i) const
Return i iterator given j iterator and index i.
ordinal_type num_i() const
Number of i entries in C(i,j,k)
void print(std::ostream &os) const
Print tensor.
void sum_term(ordinal_type i, ordinal_type j, ordinal_type k, const value_type &c)
Add new term for given (i,j,k) and sum in if already there.
k_reverse_iterator k_rbegin() const
Reverse iterator pointing to last k entry.
value_type getValue(ordinal_type i, ordinal_type j, ordinal_type k) const
Get Cijk value for a given i, j, k indices.
value_reference value() const
Return value associated with iterator.
k_reverse_iterator k_rend() const
Reverse iterator pointing to first k entry.
int n
i_iterator i_end() const
Iterator pointing to last k entry.
kj_iterator find_j(const k_iterator &k, ordinal_type j) const
Return j iterator given k iterator and index j.
ordinal_type num_entries() const
Return number of non-zero entries.