FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
pointstar_factory.hpp
1// FEAT3: Finite Element Analysis Toolbox, Version 3
2// Copyright (C) 2010 by Stefan Turek & the FEAT group
3// FEAT3 is released under the GNU General Public License version 3,
4// see the file 'copyright.txt' in the top level directory for details.
5
6#pragma once
7
8#include <kernel/util/math.hpp>
9#include <kernel/lafem/dense_vector.hpp>
10#include <kernel/lafem/sparse_matrix_csr.hpp>
11#include <kernel/lafem/sparse_matrix_banded.hpp>
12#include <kernel/lafem/pointstar_structure.hpp>
13
14#include <stack>
15
16namespace FEAT
17{
18 namespace LAFEM
19 {
25 template<typename DataType_, typename IndexType_>
27 {
28 protected:
33
35 _m(m), _d(d)
36 {
37 XASSERTM(m >= Index(3), "You need at least 3 inner nodes per dimension");
38 XASSERTM(d >= Index(1), "You need at least 1 dimension");
39 }
40
41 virtual ~PointstarFactoryBase() {}
42
43 public:
51
58 virtual DataType_ lambda_min() const = 0;
59
66 virtual DataType_ lambda_max() const = 0;
67
74 virtual DataType_ spectral_cond() const
75 {
76 return lambda_max() / lambda_min();
77 }
78
89 {
90 // compute vector length
91 Index neq(1);
92 for(Index i(0); i < _d; ++i)
93 neq *= _m;
94
95 // create vector
96 DenseVector<DataType_, IndexType_> vector(neq, DataType_(1));
97
98 // compute x scaling factor
99 const DataType_ xsf = DataType_(Math::pi<DataType_>()) / DataType_(_m+1);
100
101 {
102 Memory::TypedView<DataType_> v = vector.elements_view_rw();
103 for(Index i(0); i < neq; ++i)
104 {
105 for(Index quo(1); quo < neq; quo *= _m)
106 {
107 v[i] *= Math::sin(xsf * DataType_(((i / quo) % _m) + Index(1)));
108 }
109 }
110 }
111
112 // return vector
113 return vector;
114 }
115
126 {
127 // compute vector length
128 Index neq(1);
129 for(Index i(0); i < _d; ++i)
130 neq *= _m;
131
132 // create vector
133 DenseVector<DataType_, IndexType_> vector(neq, DataType_(1));
134
135 // compute x scaling factor
136 const DataType_ xsf = DataType_(1) / DataType_(_m+1);
137
138 {
139 Memory::TypedView<DataType_> v = vector.elements_view_rw();
140 for(Index i(0); i < neq; ++i)
141 {
142 for(Index quo(1); quo < neq; quo *= _m)
143 {
144 // compute coordinate x in (0,1)
145 DataType_ x(xsf * DataType_(((i / quo) % _m) + Index(1)));
146 v[i] *= DataType_(4) * x * (DataType_(1) - x);
147 }
148 }
149 }
150
151 // return vector
152 return vector;
153 }
154 }; // PointstarFactoryBase
155
169 template<typename DataType_, typename IndexType_>
171 public PointstarFactoryBase<DataType_, IndexType_>
172 {
173 public:
184 PointstarFactoryBase<DataType_, IndexType_>(m, d)
185 {
186 }
187
188 virtual ~PointstarFactoryFD() {}
189
197 {
198 // declare some variables
199 Index i,j,k,l,n,neq,nnze,bpl,rpb,rps,b;
200 IndexType_ *epr,*dor,*ior,*col_idx;
201 DataType_ *data;
202 const Index m(this->_m), d(this->_d);
203
204 // calculate number of equations and non-zero entries
205 neq = m;
206 nnze = 3*m - 2;
207 for(i = 1; i < d; ++i)
208 {
209 nnze *= m;
210 nnze += 2*(m-1) * neq;
211 neq *= m;
212 }
213
214 // create five vectors
215 DenseVector<IndexType_, IndexType_> vec_row_ptr(neq+IndexType_(1));
216 DenseVector<IndexType_, IndexType_> vec_col_idx(nnze);
217 DenseVector<IndexType_, IndexType_> vec_dor(neq, IndexType_(0));
218 DenseVector<IndexType_, IndexType_> vec_epr(neq, IndexType_(1));
220
221 // get data
222 Memory::TypedView<DataType_> data_view = vec_data.elements_view_w();
223 data = data_view.get_w();
224
225 // get epr
226 Memory::TypedView<IndexType_> epr_view = vec_epr.elements_view_rw();
227 epr = epr_view.get_w();
228
229 // get dor
230 Memory::TypedView<IndexType_> dor_view = vec_dor.elements_view_rw();
231 dor = dor_view.get_w();
232
233 // get ior
234 Memory::TypedView<IndexType_> row_ptr_view = vec_row_ptr.elements_view_w();
235 ior = row_ptr_view.get_w();
236
237 // get column indices
238 Memory::TypedView<IndexType_> col_idx_view = vec_col_idx.elements_view_w();
239 col_idx = col_idx_view.get_w();
240
241 /* In this step we will recursively calculate the number of non-zero
242 * elements in a row (= epr), and the number of non-zero elements left of
243 * the main diagonal element in a row (= dor).
244 * The indices of the first element of a row (= ior) and the indices of
245 * the main diagonal elements (= dor) will later be recalculated from
246 * epr and dor.
247 */
248
249 /* blocks per level */
250 bpl = 1;
251
252 /* rows per block */
253 rpb = neq;
254
255 /* go through all levels */
256 for(l = 0; l < d; ++l)
257 {
258 /* rows per sub-block */
259 rps = rpb / m;
260
261 /* go through all blocks */
262 for(b = 0; b < bpl; ++b)
263 {
264 /* index of b-th block */
265 k = rpb * b;
266
267 /* add the right diagonal to sub-block 1 */
268 for(j = 0; j < rps; ++j)
269 ++epr[k+j];
270
271 /* add left and right diagonals to sub-blocks 2..m-1 */
272 for(i = 1; i < m - 1; ++i)
273 {
274 /* index of i-th sub-block */
275 n = k + rps*i;
276 for(j = 0; j < rps; ++j)
277 {
278 epr[n+j] += 2;
279 ++dor[n+j];
280 }
281 }
282
283 /* add left diagonal to sub-block m */
284 n = k + rps*(m - 1);
285 for(j = 0; j < rps; ++j)
286 {
287 ++epr[n+j];
288 ++dor[n+j];
289 }
290 }
291
292 /* increase blocks per level */
293 bpl *= m;
294
295 /* decrease rows per block */
296 rpb = rps;
297 }
298
299 /* initialize row and diagonal pointer arrays */
300 ior[0] = 0;
301 for(i = 0; i < neq; ++i)
302 {
303 ior[i+1] = ior[i] + epr[i];
304 dor[i] += ior[i];
305 }
306
307 /* initialize data array */
308 const DataType_ v1 = DataType_(2*d);
309 const DataType_ v2 = DataType_(-1);
310 for(i = 0; i < neq; ++i)
311 {
312 /* left of main diagonal */
313 for(j = ior[i]; j < dor[i]; ++j)
314 data[j] = v2;
315
316 /* main diagonal */
317 data[dor[i]] = v1;
318
319 /* right of main diagonal */
320 for(j = dor[i]+1; j < ior[i+1]; ++j)
321 data[j] = v2;
322 }
323
324 /* We will now overwrite epr with the number of non-zero elements
325 * right of the main diagonal element of a row.
326 * At the same time, we can already set the indices of the main
327 * diagonal elements.
328 */
329 for(i = 0; i < neq; ++i)
330 {
331 epr[i] = ior[i+1] - dor[i] - 1;
332 col_idx[dor[i]] = IndexType_(i);
333 }
334
335 /* Now we need to calculate the indices for the off-main-diagonal
336 * non-zero elements.
337 * Once again, we will do this recursively ^_^
338 *
339 * We will go through every level of the matrix, and in each level,
340 * we will set the indices of the diagonal-matrix blocks which to
341 * not belong to the main diagonal.
342 */
343
344 /* blocks per level */
345 bpl = 1;
346
347 /* rows per block */
348 rpb = neq;
349
350 /* go through all levels */
351 for(l = 0; l < d; ++l)
352 {
353 /* rows per sub-block */
354 rps = rpb / m;
355
356 /* go through all blocks */
357 for(b = 0; b < bpl; ++b)
358 {
359 /* index of b-th block */
360 k = rpb * b;
361
362 /* go through the first m-1 sub-blocks */
363 for(i = 0; i < rpb - rps; ++i)
364 {
365 /* and this is where the magic happens */
366 col_idx[dor[k+i]+epr[k+i]] = IndexType_(k + i + rps);
367 col_idx[dor[neq-k-i-1]-epr[k+i]] = IndexType_(neq - k - i - rps - 1);
368 }
369
370 for(i = 0; i < rpb - rps; ++i)
371 --epr[k+i];
372 }
373
374 /* increase blocks per level */
375 bpl *= m;
376
377 /* decrease rows per block */
378 rpb = rps;
379 }
380
381 col_idx_view.release();
382 row_ptr_view.release();
383 dor_view.release();
384 epr_view.release();
385 data_view.release();
386
387 // return the matrix
388 return SparseMatrixCSR<DataType_, IndexType_>(neq, neq, vec_row_ptr, vec_col_idx, vec_data);
389 }
390
401 virtual DataType_ lambda_min() const override
402 {
403 // 4 * d * sin(pi/(2*m+2))^2 = 2 * d * (1 - cos(pi/(m+1)))
404 return DataType_(2) * DataType_(this->_d) *
405 (DataType_(1) - Math::cos(Math::pi<DataType_>() / DataType_(this->_m+1)));
406 }
407
418 virtual DataType_ lambda_max() const override
419 {
420 // 2 * d * (1 + cos(pi/(m+1)))
421 return DataType_(2) * DataType_(this->_d) *
422 (DataType_(1) + Math::cos(Math::pi<DataType_>() / DataType_(this->_m+1)));
423 }
424 }; // class PointstarFactoryFD
425
438 template<typename DataType_, typename IndexType_>
440 public PointstarFactoryBase<DataType_, IndexType_>
441 {
442 public:
450 PointstarFactoryBase<DataType_, IndexType_>(m, Index(2))
451 {
452 }
453
454 virtual ~PointstarFactoryFE() {}
455
465 {
466 // declare some variables
467 Index i,j,k,l,n,neq,nnze,bpl,rpb,rps,b;
468 IndexType_ *epr,*dor,*ior,*col_idx;
469 DataType_ *data;
470 const Index m(this->_m), d(this->_d);
471
472 /* calculate number of equations and non-zero entries */
473 neq = m;
474 nnze = j = 3*m - 2;
475 for(i = 1; i < d; i++)
476 {
477 nnze *= j;
478 neq *= m;
479 }
480
481 // create five vectors
482 DenseVector<IndexType_, IndexType_> vec_row_ptr(neq+IndexType_(1));
483 DenseVector<IndexType_, IndexType_> vec_col_idx(nnze, IndexType_(neq));
484 DenseVector<IndexType_, IndexType_> vec_dor(neq, IndexType_(0));
485 DenseVector<IndexType_, IndexType_> vec_epr(neq, IndexType_(1));
487
488 // get data
489 Memory::TypedView<DataType_> data_view = vec_data.elements_view_w();
490 data = data_view.get_w();
491
492 // get epr
493 Memory::TypedView<IndexType_> epr_view = vec_epr.elements_view_rw();
494 epr = epr_view.get_w();
495
496 // get dor
497 Memory::TypedView<IndexType_> dor_view = vec_dor.elements_view_rw();
498 dor = dor_view.get_w();
499
500 // get ior
501 Memory::TypedView<IndexType_> row_ptr_view = vec_row_ptr.elements_view_w();
502 ior = row_ptr_view.get_w();
503
504 // get column indices
505 Memory::TypedView<IndexType_> col_idx_view = vec_col_idx.elements_view_rw();
506 col_idx = col_idx_view.get_w();
507
508 /* In this step we will recursively calculate the number of non-zero
509 * elements in a row (= epr), and the number of non-zero elements left of
510 * the main diagonal element in a row (= dor).
511 * The indices of the first element of a row (= ior) and the indices of
512 * the main diagonal elements (= dor) will later be recalculated from
513 * epr and dor.
514 */
515
516 /* blocks per level */
517 bpl = neq / m;
518
519 /* rows per block */
520 rpb = m;
521
522 /* go through all levels */
523 for(l = 0; l < d; ++l)
524 {
525 /* rows per sub-block */
526 rps = rpb / m;
527
528 /* go through all blocks */
529 for(b = 0; b < bpl; ++b)
530 {
531 /* index of b-th block */
532 k = rpb * b;
533
534 /* add the right diagonal to sub-block 1 */
535 for(j = 0; j < rps; ++j)
536 epr[k+j] *= 2;
537
538 /* add left and right diagonals to sub-blocks 2..m-1 */
539 for(i = 1; i < m - 1; ++i)
540 {
541 /* index of i-th sub-block */
542 n = k + rps*i;
543 for(j = 0; j < rps; ++j)
544 {
545 dor[n+j] += epr[n+j];
546 epr[n+j] *= 3;
547 }
548 }
549
550 /* add left diagonal to sub-block m */
551 n = k + rps*(m - 1);
552 for(j = 0; j < rps; ++j)
553 {
554 dor[n+j] += epr[n+j];
555 epr[n+j] *= 2;
556 }
557 }
558
559 /* decrease blocks per level */
560 bpl /= m;
561
562 /* increase rows per block */
563 rpb *= m;
564 }
565
566 /* initialize row and diagonal pointer arrays */
567 /* calculate row and diagonal indices */
568 ior[0] = 0;
569 for(i = 0; i < neq; ++i)
570 {
571 ior[i+1] = ior[i] + epr[i];
572 dor[i] += ior[i];
573 }
574
575 /* initialize data array */
576 j = Index(3);
577 for(i = 1; i < d; ++i)
578 j *= Index (3);
579 const DataType_ v1 = DataType_(j - Index(1));
580 const DataType_ v2 = DataType_(-1);
581
582 for(i = 0; i < neq; ++i)
583 {
584 /* left of main diagonal */
585 for(j = ior[i]; j < dor[i]; ++j)
586 data[j] = v2;
587
588 /* main diagonal */
589 data[dor[i]] = v1;
590
591 /* right of main diagonal */
592 for(j = dor[i]+1; j < ior[i+1]; ++j)
593 data[j] = v2;
594 }
595
596 /* First, we initialize all indices to lie on the main diagonal.
597 * At the same time, we will reset epr to 1.
598 */
599 for(i = 0; i < neq; ++i)
600 {
601 epr[i] = 1;
602 for(j = ior[i]; j < ior[i+1]; ++j)
603 col_idx[j] = IndexType_(neq+i);
604 }
605
606 /* blocks per level */
607 bpl = neq / m;
608
609 /* rows per block */
610 rpb = m;
611
612 /* go through all levels */
613 for(l = d; l > 0; --l)
614 {
615 /* rows per sub-block */
616 rps = rpb / m;
617
618 /* go through all blocks */
619 for(b = 0; b < bpl; ++b)
620 {
621 /* index of b-th block */
622 k = rpb * b;
623
624 /* go through the first sub-block */
625 for(i = 0; i < rps; ++i)
626 {
627 for(n = 0; 2*n*epr[k+i] < ior[k+i+1]-ior[k+i]; ++n)
628 {
629 for(j = 0; j < epr[k+i]; ++j)
630 {
631 col_idx[ior[k+i]+2*n*epr[k+i]+j+epr[k+i]] += IndexType_(rps);
632 col_idx[ior[neq-k-i]-2*n*epr[k+i]-j-epr[k+i]-1] -= IndexType_(rps);
633 }
634 }
635 epr[k+i] *= 2;
636 epr[k+rpb-i-1] *= 2;
637 }
638
639 /* go through the next m-2 sub-blocks */
640 for(i = rps; i < rpb - rps; ++i)
641 {
642 for(n = 0; 3*n*epr[k+i] < ior[k+i+1]-ior[k+i]; ++n)
643 {
644 for(j = 0; j < epr[k+i]; ++j)
645 {
646 col_idx[ior[k+i]+3*n*epr[k+i]+j+2*epr[k+i]] += IndexType_(rps);
647 col_idx[ior[neq-k-i]-3*n*epr[k+i]-j-2*epr[k+i]-1] -= IndexType_(rps);
648 }
649 }
650 epr[k+i] *= 3;
651 }
652 }
653
654 /* decrease blocks per level */
655 bpl /= m;
656
657 /* increase rows per block */
658 rpb *= m;
659 }
660
661 // finally, update col_idx
662 for(i = 0; i < nnze; ++i)
663 col_idx[i] -= IndexType_(neq);
664
665 col_idx_view.release();
666 row_ptr_view.release();
667 dor_view.release();
668 epr_view.release();
669 data_view.release();
670
671 // return the matrix
672 return SparseMatrixCSR<DataType_, IndexType_>(neq, neq, vec_row_ptr, vec_col_idx, vec_data);
673 }
674
685 {
686 // create matrix with default values
688
689 // get matrix arrays
690 const IndexType_ m = IndexType_(this->_m);
691 const Memory::TypedView<IndexType_> row_ptr = matrix.row_ptr_view_r();
692 const Memory::TypedView<IndexType_> col_idx = matrix.col_idx_view_r();
693 Memory::TypedView<DataType_> val = matrix.val_view_rw();
694
695 // process first vertex line
696 {
697 // process first vertex in first line
698 {
699 const IndexType_ row = 0;
700 for(IndexType_ k(row_ptr[row]); k < row_ptr[row + 1]; ++k)
701 {
702 const IndexType_ col = col_idx[k];
703 if(col == row)
704 val[k] = DataType_(4);
705 else if(col == row+1)
706 val[k] = DataType_(-1);
707 else if(col == row+m)
708 val[k] = DataType_(-1);
709 else if(col == row+m+1)
710 val[k] = DataType_(-2);
711 else
712 throw 0;
713 }
714 }
715
716 // loop over all inner vertices in first line
717 for(IndexType_ jj(1); jj + 1 < m; ++jj)
718 {
719 // loop over all non-zeros
720 const IndexType_ row = jj;
721 for(IndexType_ k(row_ptr[row]); k < row_ptr[row + 1]; ++k)
722 {
723 const IndexType_ col = col_idx[k];
724 if(col == row)
725 val[k] = DataType_(8);
726 else if(col+1 == row)
727 val[k] = DataType_(-1);
728 else if(col == row+1)
729 val[k] = DataType_(-1);
730 else
731 val[k] = DataType_(-2);
732 }
733 }
734
735 // process last vertex in first line
736 {
737 const IndexType_ row = m - 1;
738 for(IndexType_ k(row_ptr[row]); k < row_ptr[row + 1]; ++k)
739 {
740 const IndexType_ col = col_idx[k];
741 if(col == row)
742 val[k] = DataType_(4);
743 else if(col+1 == row)
744 val[k] = DataType_(-1);
745 else if(col == row+m)
746 val[k] = DataType_(-1);
747 else if(col+1 == row+m)
748 val[k] = DataType_(-2);
749 else
750 throw 0;
751 }
752 }
753 }
754
755 // loop over all inner vertex lines
756 for(IndexType_ ii(1); ii + 1 < m; ++ii)
757 {
758 // process first vertex in line ii
759 {
760 const IndexType_ row = ii*m;
761 for(IndexType_ k(row_ptr[row]); k < row_ptr[row + 1]; ++k)
762 {
763 const IndexType_ col = col_idx[k];
764 if(col == row)
765 val[k] = DataType_(8);
766 else if(col+m == row)
767 val[k] = DataType_(-1);
768 else if(col == row+m)
769 val[k] = DataType_(-1);
770 else
771 val[k] = DataType_(-2);
772 }
773 }
774
775 // loop over all inner vertices in line ii
776 for(IndexType_ jj(1); jj + 1 < m; ++jj)
777 {
778 // loop over all non-zeros
779 const IndexType_ row = ii*m + jj;
780 for(IndexType_ k(row_ptr[row]); k < row_ptr[row+1]; ++k)
781 val[k] = DataType_(col_idx[k] == row ? 16 : -2);
782 }
783
784 // process last vertex in line ii
785 {
786 const IndexType_ row = ii*m + m - 1;
787 for(IndexType_ k(row_ptr[row]); k < row_ptr[row + 1]; ++k)
788 {
789 const IndexType_ col = col_idx[k];
790 if(col == row)
791 val[k] = DataType_(8);
792 else if(col+m == row)
793 val[k] = DataType_(-1);
794 else if(col == row+m)
795 val[k] = DataType_(-1);
796 else
797 val[k] = DataType_(-2);
798 }
799 }
800 }
801
802
803 // process last vertex line
804 {
805 // process first vertex in last line
806 {
807 const IndexType_ row = (m-1)*m;
808 for(IndexType_ k(row_ptr[row]); k < row_ptr[row + 1]; ++k)
809 {
810 const IndexType_ col = col_idx[k];
811 if(col == row)
812 val[k] = DataType_(4);
813 else if(col == row+1)
814 val[k] = DataType_(-1);
815 else if(col+m == row)
816 val[k] = DataType_(-1);
817 else if(col+m == row+1)
818 val[k] = DataType_(-2);
819 else
820 throw 0;
821 }
822 }
823
824 // loop over all inner vertices in last line
825 for(IndexType_ jj(1); jj + 1 < m; ++jj)
826 {
827 // loop over all non-zeros
828 const IndexType_ row = (m-1)*m + jj;
829 for(IndexType_ k(row_ptr[row]); k < row_ptr[row + 1]; ++k)
830 {
831 const IndexType_ col = col_idx[k];
832 if(col == row)
833 val[k] = DataType_(8);
834 else if(col+1 == row)
835 val[k] = DataType_(-1);
836 else if(col == row+1)
837 val[k] = DataType_(-1);
838 else
839 val[k] = DataType_(-2);
840 }
841 }
842
843 // process last vertex in last line
844 {
845 const IndexType_ row = (m-1)*m + m - 1;
846 for(IndexType_ k(row_ptr[row]); k < row_ptr[row + 1]; ++k)
847 {
848 const IndexType_ col = col_idx[k];
849 if(col == row)
850 val[k] = DataType_(4);
851 else if(col+1 == row)
852 val[k] = DataType_(-1);
853 else if(col+m == row)
854 val[k] = DataType_(-1);
855 else if(col+m+1 == row)
856 val[k] = DataType_(-2);
857 else
858 throw 0;
859 }
860 }
861 }
862
863 // return modified matrix
864 return matrix;
865 }
866
867
871 virtual DataType_ lambda_min() const override
872 {
873 const DataType_ c(Math::cos(Math::pi<DataType_>() / DataType_(this->_m+1)));
874
875 // lambda_min = 3^d - 1 - \sum_{k=1}^{d-1} 2^{d-k} {d \choose k} \cos\Big(\frac{\pi}{m+1}\Big)^k
876
877 Index v(Index(1)); // v := 2^d
878 Index a(Index(1)); // a := 3^d-1
879 for(Index i(0); i < this->_d; ++i)
880 {
881 v *= Index(2);
882 a *= Index(3);
883 }
884
885 DataType_ y = DataType_(v);
886 for(Index k(1); k < this->_d; ++k)
887 {
888 (y *= c) += DataType_((v *= this->_d - k + Index(1)) /= (Index(2) * k));
889 }
890
891 return DataType_(a - Index(1)) - c*y;
892 }
893
897 virtual DataType_ lambda_max() const override
898 {
899 return DataType_(8) + DataType_(4)*Math::sqr(Math::cos(Math::pi<DataType_>() / DataType_(this->_m+1)));
900 }
901 }; // class PointstarFactory
902
903
909 template<typename DataType_, typename IndexType_>
911 {
912 protected:
914 const std::vector<IndexType_> & _num_of_subintervalls;
916 const std::vector<DataType_> & _dimensions;
918 const Index _d;
919
920 PointstarFactoryBase2(const std::vector<IndexType_> & num_of_subintervalls,
921 const std::vector<DataType_> & dimensions) :
922 _num_of_subintervalls(num_of_subintervalls),
923 _dimensions(dimensions),
924 _d(Index(_dimensions.size()))
925 {
926 XASSERTM(_d >= Index(1), "You need at least 1 dimension");
927 XASSERTM(_d + 1 == _num_of_subintervalls.size(), "Vector-sizes do not match (n_1 = n_2 + 1)!");
928
929 for (Index i(1); i <= _d; ++i)
930 {
931 XASSERTM(_num_of_subintervalls[i] >= Index(4), "You need at least 4 subintervalls per dimension");
932 }
933 }
934
935 virtual ~PointstarFactoryBase2() {}
936
937 public:
945
952 virtual DataType_ lambda_min() const = 0;
953
960 virtual DataType_ lambda_max() const = 0;
961
968 virtual DataType_ spectral_cond() const
969 {
970 return lambda_max() / lambda_min();
971 }
972
983
994 }; // PointstarFactoryBase2
995
1006 template<typename DataType_, typename IndexType_>
1008 public PointstarFactoryBase2<DataType_, IndexType_>
1009 {
1010 public:
1019 PointstarFactoryFD2(const std::vector<IndexType_> & num_of_subintervalls,
1020 const std::vector<DataType_> & dimensions) :
1021 PointstarFactoryBase2<DataType_, IndexType_>(num_of_subintervalls, dimensions)
1022 {
1023 }
1024
1025 virtual ~PointstarFactoryFD2() {}
1026
1034 {
1035 const IndexType_ d(IndexType_(this->_d));
1036 const IndexType_ * const pnos(this->_num_of_subintervalls.data());
1037 const DataType_ * const pdim(this->_dimensions.data());
1038
1042 SparseMatrixBanded<DataType_, IndexType_> matrix(PointstarStructureFD::value<DataType_, IndexType_>(this->_num_of_subintervalls));
1043 const IndexType_ neq(IndexType_(matrix.num_rows()));
1044 Memory::TypedView<DataType_> pval(matrix.val_view_w());
1045
1049 // calculate diagonal entries
1050 DataType_ diagonal_entry(0);
1051 for (IndexType_ i(0); i < d; ++i)
1052 {
1053 diagonal_entry += DataType_(2.0) * Math::sqr(pdim[i] / DataType_(pnos[i + 1]));
1054 }
1055
1056 // Fill diagonal with entries
1057 for (IndexType_ i(0); i < neq; ++i)
1058 {
1059 pval[neq * d + i] = diagonal_entry;
1060 }
1061
1062 // Fill subdiagonals with entries
1063 for (IndexType_ i(0), k(pnos[0] - 1); i < d; ++i, k *= pnos[i] - 1)
1064 {
1065 const DataType_ subdiagonal_entry(-Math::sqr(pdim[i] / DataType_(pnos[i + 1])));
1066
1067 for (IndexType_ j(0); j < neq / k / (pnos[i + 1] - 1); ++j)
1068 {
1069 const IndexType_ k1(neq * d + k * (pnos[i + 1] - 1) * j);
1070 const IndexType_ k2(neq * (i + 1));
1071
1072 if (j != 0)
1073 {
1074 for (IndexType_ l(pnos[i] - 1); l > 0; --l)
1075 {
1076 pval[k1 + k2 - l] = DataType_(0);
1077 pval[k1 - k2 - l + k] = DataType_(0);
1078 }
1079 }
1080
1081 for (IndexType_ l(0); l < k * (pnos[i + 1] - 2); ++l)
1082 {
1083 pval[k1 + l + k2] = subdiagonal_entry;
1084 pval[k1 + l - k2 + k] = subdiagonal_entry;
1085 }
1086 }
1087 }
1088
1089 pval.release();
1090
1091 // return the matrix
1092 return matrix;
1093 }
1094
1105 virtual DataType_ lambda_min() const override
1106 {
1107 DataType_ x(DataType_(0.0));
1108 const IndexType_ * const pnos(this->_num_of_subintervalls.data());
1109 const DataType_ * const pdim(this->_dimensions.data());
1110
1111 for (Index i(0); i < this->_d; ++i)
1112 {
1113 const DataType_ h(pdim[i] / DataType_(pnos[i + 1]));
1114 x += Math::sqr(h) * (DataType_(1.0) - Math::cos(Math::pi<DataType_>() * h / pdim[i]));
1115 }
1116
1117 return DataType_(2) * x;
1118 }
1119
1130 virtual DataType_ lambda_max() const override
1131 {
1132 DataType_ x(DataType_(0.0));
1133 const IndexType_ * const pnos(this->_num_of_subintervalls.data());
1134 const DataType_ * const pdim(this->_dimensions.data());
1135
1136 for (Index i(0); i < this->_d; ++i)
1137 {
1138 const DataType_ h(pdim[i] / DataType_(pnos[i + 1]));
1139 x += Math::sqr(h) * (DataType_(1.0) + Math::cos(Math::pi<DataType_>() * h / pdim[i]));
1140 }
1141
1142 return DataType_(2) * x;
1143 }
1144
1146 {
1147 const IndexType_ * const pnos(this->_num_of_subintervalls.data());
1148 const Index d(this->_d);
1149
1150 // compute vector length
1151 Index size(1);
1152 for (Index i(1); i <= d; ++i)
1153 {
1154 size *= pnos[i] - 1;
1155 }
1156
1157 // create vector
1158 DenseVector<DataType_, IndexType_> vector(size, DataType_(1));
1159 Memory::TypedView<DataType_> v = vector.elements_view_rw();
1160
1161 for(Index i(0); i < size; ++i)
1162 {
1163 for(Index j(0), quo(1); j < d; ++j, quo *= pnos[j] - 1)
1164 {
1165 // compute x scaling factor
1166 const DataType_ xsf(Math::pi<DataType_>() / DataType_(pnos[j + 1]));
1167 v[i] *= Math::sin(xsf * DataType_(((i / quo) % (pnos[j + 1] - 1)) + Index(1)));
1168 }
1169 }
1170 v.release();
1171
1172 // return vector
1173 return vector;
1174 }
1175
1186 {
1187 const Index d(this->_d);
1188 const IndexType_ * const pnos(this->_num_of_subintervalls.data());
1189 const DataType_ * const pdim(this->_dimensions.data());
1190
1191 // compute vector length
1192 Index size(1);
1193 for (Index i(1); i <= d; ++i)
1194 {
1195 size *= pnos[i] - 1;
1196 }
1197
1198 // create vector
1199 DenseVector<DataType_, IndexType_> vector(size, DataType_(1));
1200 Memory::TypedView<DataType_> v = vector.elements_view_rw();
1201
1202 for(Index i(0); i < size; ++i)
1203 {
1204 for(Index j(0), quo(1); j < d; ++j, quo *= pnos[j] - 1)
1205 {
1206 // compute x scaling factor
1207 const DataType_ xsf(pdim[j] / DataType_(pnos[j + 1]));
1208
1209 // compute coordinate x in (0,pdim[j])
1210 DataType_ x(xsf * DataType_(((i / quo) % (pnos[j + 1] - 1)) + Index(1)));
1211
1212 v[i] *= DataType_(4) * x * (DataType_(1) - x);
1213 }
1214 }
1215 v.release();
1216
1217 // return vector
1218 return vector;
1219 }
1220 }; // class PointstarFactoryFD2
1221
1222 } // namespace LAFEM
1223} // namespace FEAT
#define XASSERTM(expr, msg)
Assertion macro definition with custom message.
Definition: assertion.hpp:263
Dense data vector class template.
Pointstar factory base class.
virtual DenseVector< DataType_, IndexType_ > eigenvector_min() const =0
Computes the eigenvector of the smallest eigenvalue.
virtual DataType_ lambda_min() const =0
Computes the smallest eigenvalue of the pointstar matrix.
virtual DenseVector< DataType_, IndexType_ > vector_q2_bubble() const =0
Computes a Q2-bubble vector.
const std::vector< DataType_ > & _dimensions
vector with lengths of the n-dimensional hypercube
virtual DataType_ lambda_max() const =0
Computes the largest eigenvalue of the pointstar matrix.
virtual DataType_ spectral_cond() const
Computes the spectral condition number of the pointstar matrix.
const Index _d
number of dimensions
const std::vector< IndexType_ > & _num_of_subintervalls
vector with number of subintervalls per dimension (plus a leading 2).
virtual SparseMatrixBanded< DataType_, IndexType_ > matrix_banded() const =0
Computes the pointstar matrix in banded format.
Pointstar factory base class.
virtual DataType_ lambda_max() const =0
Computes the largest eigenvalue of the pointstar matrix.
Index _m
number of inner nodes per dimension
virtual SparseMatrixCSR< DataType_, IndexType_ > matrix_csr() const =0
Computes the pointstar matrix in CSR format.
DenseVector< DataType_, IndexType_ > vector_q2_bubble() const
Computes a Q2-bubble vector.
virtual DataType_ spectral_cond() const
Computes the spectral condition number of the pointstar matrix.
virtual DenseVector< DataType_, IndexType_ > eigenvector_min() const
Computes the eigenvector of the smallest eigenvalue.
virtual DataType_ lambda_min() const =0
Computes the smallest eigenvalue of the pointstar matrix.
Finite-Differences pointstar matrix factory.
DenseVector< DataType_, IndexType_ > vector_q2_bubble() const override
Computes a Q2-bubble vector.
virtual DataType_ lambda_min() const override
Computes the smallest eigenvalue of the FD-style matrix.
virtual SparseMatrixBanded< DataType_, IndexType_ > matrix_banded() const override
Generates a FD-style pointstar banded matrix.
virtual DataType_ lambda_max() const override
Computes the largest eigenvalue of the FD-style matrix.
virtual DenseVector< DataType_, IndexType_ > eigenvector_min() const override
Computes the eigenvector of the smallest eigenvalue.
PointstarFactoryFD2(const std::vector< IndexType_ > &num_of_subintervalls, const std::vector< DataType_ > &dimensions)
Creates the pointstar factory.
Finite-Differences pointstar matrix factory.
virtual DataType_ lambda_max() const override
Computes the largest eigenvalue of the FD-style matrix.
PointstarFactoryFD(Index m, Index d=Index(2))
Creates the pointstar factory.
virtual DataType_ lambda_min() const override
Computes the smallest eigenvalue of the FD-style matrix.
virtual SparseMatrixCSR< DataType_, IndexType_ > matrix_csr() const override
Generates a FD-style pointstar CSR matrix.
Finite-Element pointstar matrix factory.
virtual DataType_ lambda_min() const override
Computes the smallest eigenvalue of the FE-style matrix.
PointstarFactoryFE(Index m)
Creates the pointstar factory.
virtual SparseMatrixCSR< DataType_, IndexType_ > matrix_csr_neumann() const
Generates a FE-style pointstar CSR matrix with Neumann boundaries.
virtual SparseMatrixCSR< DataType_, IndexType_ > matrix_csr() const override
Generates a FE-style pointstar CSR matrix.
virtual DataType_ lambda_max() const override
Computes the largest eigenvalue of the FE-style matrix.
Index num_rows() const
Retrieve matrix row count.
CSR based sparse matrix.
Typed memory view class.
T_ * get_w()
Returns a typed pointer to the array that grants write (and read) access.
void release() const
Releases this view.
T_ sin(T_ x)
Returns the sine of a value.
Definition: math.hpp:344
T_ sqr(T_ x)
Returns the square of a value.
Definition: math.hpp:95
T_ cos(T_ x)
Returns the cosine of a value.
Definition: math.hpp:386
FEAT namespace.
Definition: adjactor.hpp:12
std::uint64_t Index
Index data type.