KASKADE 7 development version
Loading...
Searching...
No Matches
fixdune.hh
Go to the documentation of this file.
1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2/* */
3/* This file is part of the library KASKADE 7 */
4/* https://www.zib.de/research/projects/kaskade7-finite-element-toolbox */
5/* */
6/* Copyright (C) 2002-2020 Zuse Institute Berlin */
7/* */
8/* KASKADE 7 is distributed under the terms of the ZIB Academic License. */
9/* see $KASKADE/academic.txt */
10/* */
11/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
12
13#if !defined(FIXDUNE_HH)
14#define FIXDUNE_HH
15
16#include <type_traits>
17
18// Here we'd rather include dune/common/config.h, but this messes around with
19// including "FC.h" without specifying the dune/... path. Fortunately,
20// the grid config header has the common module version defined as well.
21#include "dune/grid/config.h"
22
23#include "dune/common/fmatrix.hh"
24#include "dune/common/fvector.hh"
25#include "dune/common/version.hh"
26#include "dune/geometry/type.hh"
27#include "dune/geometry/referenceelements.hh"
28
29
30#include "dune/istl/matrix.hh"
31#include "dune/istl/bvector.hh"
32#include "dune/istl/bcrsmatrix.hh"
33
35#include "utilities/scalar.hh"
40
41
42namespace Dune
43{
44
49 template <class T, int n>
51 {
52 x += y;
53 return x;
54 }
55
60 template <class T, int n>
62 {
63 for (int i=0; i<n; ++i)
64 x[i] = -x[i];
65 return x;
66 }
67
72 template <class T, int n>
74 {
75 for (int i=0; i<n; ++i)
76 x[i] = std::max(x[i],y[i]);
77 return x;
78 }
79
84 template <class T, int n>
86 {
87 for (int i=0; i<n; ++i)
88 x[i] = std::min(x[i],y[i]);
89 return x;
90 }
91
92
97 template <class T, int n, int m>
99 {
101 for (int j=0; j<m; ++j)
102 for (int i=0; i<n; ++i)
103 y[j*n+i] = x[i][j];
104 return y;
105 }
106
111 template <int n, int m, class T>
113 {
115 for (int j=0; j<m; ++j)
116 for (int i=0; i<n; ++i)
117 y[i][j] = x[j*n+i];
118 return y;
119 }
120
121
126 template <class T, int n, int m>
128 {
130 for (int i=0; i<n; ++i)
131 for (int j=0; j<m; ++j)
132 A[i][j] = x[i]*y[j];
133 return A;
134 }
135
140 template <class T, int n>
142 {
143 T nm = x.two_norm();
144 if (nm>0)
145 x /= nm;
146 return x;
147 }
148
153 template <class T, int n, int m>
155 Dune::FieldVector<T,m> const& y)
156 {
158 for (int i=0; i<n; ++i)
159 z[i] = x[i];
160 for (int i=0; i<m; ++i)
161 z[i+n] = y[i];
162 return z;
163 }
164
165
166#ifndef DUNE_ALBERTA_ALGEBRA_HH
171 template <class T>
173 {
174 return x[0]*y[1] - x[1]*y[0];
175 }
176
181 template <class T>
183 {
185 A[0][0] = -x[1];
186 A[0][1] = x[0];
187 }
188
193 template <class T>
195 {
197 for (int i=0; i<3; ++i)
198 t[i] = x[(i+1)%3]*y[(i+2)%3]-x[(i+2)%3]*y[(i+1)%3];
199 return t;
200 }
201
206 template <class T>
208 {
210 for (int i=0; i<3; ++i)
211 {
212 A[i][(i+2)%3] = x[(i+1)%3];
213 A[i][(i+1)%3] = -x[(i+2)%3];
214 }
215 return A;
216 }
217
218#endif
219
220
221
226 template <class T, class S, int n, int m>
228 Dune::FieldVector<S,m> const& x)
229 {
231 //A.umv(x,b);
232 for (int i=0; i<n; ++i)
233 for (int j=0; j<m; ++j)
234 b[i] += A[i][j]*x[j];
235 return b;
236 }
237
242 template <class T, int n, int m>
244 {
246 for (int i=0; i<n; ++i)
247 C[i] = -A[i]; // vector negation of rows
248 return C;
249 }
250
251 template <class T, int n, int m>
253 {
254 if constexpr (n*m==1)
255 return A[0][0];
256 else
257 return A;
258 }
259
260 // Ambiguity with Dune::transpose() as of Dune 2.9.0
261 // Note that Dune::transposed() does not perform a recursive transpose.
262 //
263 // template <class T, typename = std::enable_if_t<std::is_arithmetic<T>::value>>
264 // T transpose(T x)
265 // {
266 // return x;
267 // }
268 //
269 // /**
270 // * \ingroup linalgbasic
271 // * \brief Matrix transposition \f$ A \mapsto A^T \f$
272 // */
273 // template <class T, int n, int m>
274 // Dune::FieldMatrix<T,m,n> transpose(Dune::FieldMatrix<T,n,m> const& A)
275 // {
276 // Dune::FieldMatrix<T,m,n> At;
277 // for (int i=0; i<n; ++i)
278 // for (int j=0; j<m; ++j)
279 // At[j][i] = transpose(A[i][j]);
280 // return At;
281 // }
282} // end of namespace Dune
283
284
285namespace Kaskade
286{
287 // We define horzcat for Dune::FieldVector/Matrix in the Kaskade namespace because there's
288 // also a horzcat() for DynamicMatrix in the Kaskade namespace. This would otherwise
289 // completely hide the definitions here (seen from within the Kaskade namespace).
290 template <class Scalar, int n>
292 {
293 return c1;
294 }
295
303 template <class Scalar, int n, class ... Rest>
304 auto horzcat(Dune::FieldVector<Scalar,n> const& a, Rest ... rest)
305 {
306 // Works, but I think it's way too slow unless the compiler
307 // does some magic...
308
309 auto b = horzcat(rest...);
310 using B = decltype(b);
312
313 // leads to compiler error as of GCC 8.2 - why?
314 // static_assert(rank<B>==1 || rank<B>==2);
315 if constexpr (rank<B> == 1)
316 {
318 for (int i=0; i<n; ++i)
319 {
320 result[i][0] = a[i];
321 result[i][1] = b[i];
322 }
323 return result;
324 }
325 else if constexpr (rank<B> == 2)
326 {
328 for (int i=0; i<n; ++i)
329 {
330 result[i][0] = a[i];
331 for (int j=0; j<B::cols; ++j)
332 result[i][1+j] = b[i][j];
333 }
334 return result;
335 }
336 else
337 {
338 // never get here
339 return 0;
340 }
341 }
342
343 template <class Scalar, int n, int m>
345 {
346 return a;
347 }
348
349 template <class Scalar, int n, int m, class ... Rest>
350 auto horzcat(Dune::FieldMatrix<Scalar,n,m> const& a, Rest ... rest)
351 {
352 // Works, but I think it's way too slow unless the compiler
353 // does some magic...
354
355 auto b = horzcat(rest...);
356 using B = decltype(b);
358
359 if constexpr (rank<B> == 1)
360 {
362 for (int i=0; i<n; ++i)
363 {
364 for (int j=0; j<m; ++j)
365 result[i][j] = a[i][j];
366 result[i][m] = b[i];
367 }
368 return result;
369 }
370 else if constexpr (rank<B> == 2)
371 {
373 for (int i=0; i<n; ++i)
374 {
375 for (int j=0; j<m; ++j)
376 result[i][j] = a[i][j];
377 for (int j=0; j<B::cols; ++j)
378 result[i][m+j] = b[i][j];
379 }
380 return result;
381 }
382 else
383 {
384 // never get here
385 return 0;
386 }
387 }
388
389
390 // TODO: generic version via parameter packs?
391 template <class Scalar, int n>
393 Dune::FieldVector<Scalar,n> const& col2,
394 Dune::FieldVector<Scalar,n> const& col3)
395 {
397 for (int i=0; i<n; ++i)
398 {
399 A[i][0] = col1[i]; A[i][1] = col2[i]; A[i][2] = col3[i];
400 }
401 return A;
402 }
403
407 template <class Scalar, int n, int m>
409 {
410 assert(0<=j && j<m);
412 for (int i=0; i<n; ++i)
413 x[i] = A[i][j];
414 return x;
415 }
416
421 template <class Scalar, int n>
423 {
425
426 for(int i=0; i<n; ++i)
427 for(int j=0; j<n; ++j)
428 result[i+j*n] = A[i][j];
429
430 return result;
431 }
432
437 template <int n, class Scalar>
439 {
441
442 for(int i=0; i<n; ++i)
443 for(int j=0; j<n; ++j)
444 result[i][j] = v[i+j*n];
445
446 return result;
447 }
448
449
450
451
456 template <class T, int n, int m>
458 {
459 T x = 0;
460 for (int i=0; i<n; ++i)
461 for (int j=0; j<m; ++j)
462 x += A[i][j] * B[i][j];
463 return x;
464 }
465
466} // end of namespace Kaskade
467
468namespace Dune
469{
470
477 template <class T, int n>
479 {
480 T x = 0;
481 for (int i=0; i<n; ++i)
482 x += A[i][i];
483 return x;
484 }
485
486}
487
492template <class T, int n>
494{
496 for (int i=0; i<n; ++i)
497 d[i] = A[i][i];
498 return d;
499}
500
505template <class T, int n>
507{
509 for (int i=0; i<n; ++i)
510 I[i][i] = 1;
511 return I;
512}
513
518template <class T>
520{
522 skew[0][1] = -v[2];
523 skew[0][2] = v[1];
524 skew[1][0] = v[2];
525 skew[1][2] = -v[0];
526 skew[2][0] = -v[1];
527 skew[2][1] = v[0];
528 return skew;
529}
530
535template <class T, int n>
537{
538 assert(0<=i && i<n);
540 ei[i] = 1;
541 return ei;
542}
543
547template <class Matrix, bool transposed>
548struct Transpose {};
549
550template <class Scalar, int n, int m, bool transposed>
551struct Transpose<Dune::FieldMatrix<Scalar,n,m>,transposed>
552{
554};
555
556
564template <class MatrixZ, class Matrix>
565void MatMult(MatrixZ& z, Matrix const& x, Matrix const& y)
566{
567 assert(((void*)&z != (void*)&x) && ((void*)&z != (void*)&y));
568 assert(x.M() == y.N());
569
570 z.setSize(x.N(),y.M());
571
572 for (int i=0; i<z.N(); i++ ) {
573 for (int j=0; j<z.M(); j++ ) {
574 typename MatrixZ::block_type temp = 0.0;
575 for (int k=0; k<x.M(); k++)
576 temp += x[i][k]*y[k][j];
577 z[i][j] = temp;
578 }
579 }
580}
581
582
583//---------------------------------------------------------------------
584
590namespace FixDuneDetail
591{
592
593 //TODO: in Dune 2.0 this is not needed anymore!
594 template <int Codim>
595 struct GetIndexOfSubEntity
596 {
597 template <class Cell, class IS>
598 static int value(Cell const& cell, int codim, int entity, IS const& is)
599 {
600 if (codim==Codim)
601 // return is.template subIndex<Codim>(cell,entity);
602 return is.subIndex(cell,entity,Codim);
603 else
604 return GetIndexOfSubEntity<Codim-1>::value(cell,codim,entity,is);
605 }
606 };
607
608 template <>
609 struct GetIndexOfSubEntity<0>
610 {
611 template <class Cell, class IS>
612 static int value(Cell const& cell, int codim, int entity, IS const& is)
613 {
614 assert (codim==0);
615 // return is.template subIndex<0>(cell,entity);
616 return is.subIndex(cell,entity,0);
617 }
618 };
619
620 template <int Codim>
621 struct GetNumberOfSubEntities
622 {
623 template <class Cell>
624 static int value( Cell const& cell, int codim )
625 {
626 if (codim==Codim)
627 return cell.template count<Codim>() ;
628 else
629 return GetNumberOfSubEntities<Codim-1>::value(cell, codim) ;
630 }
631 } ;
632
633 template <>
634 struct GetNumberOfSubEntities<0>
635 {
636 template <class Cell>
637 static int value( Cell const& cell, int codim )
638 {
639 assert (codim==0);
640 return cell.template count<0>();
641 }
642 };
643
644} // End of namespace FixDuneDetail
646
654template <class IndexSet, class Cell>
655size_t subIndex(IndexSet const& is, Cell const& cell, int codim, int subentity)
656{
657 return FixDuneDetail::GetIndexOfSubEntity<Cell::dimension>::value(cell,codim,subentity,is);
658}
659
666template <class Cell>
667int count(Cell const& cell, int codim)
668{
669 return FixDuneDetail::GetNumberOfSubEntities<Cell::dimension>::value(cell, codim) ;
670}
671
672
673// ---------------------------------------------------------------------
674
691template <class LocalCoordinate>
692typename LocalCoordinate::field_type checkInside(Dune::GeometryType const& gt,
693 LocalCoordinate const& x)
694{
695 if (gt.isSimplex())
696 // The reference simplex is the set defined by componentwise x_i>=0 and 1-sum x_i>=0. Thus we return
697 // the negative of the minimum of these conditions.
698 return -std::min(*std::min_element(x.begin(),x.end()),1-x.one_norm());
699 else if (gt.isCube())
700 // The reference cube is the set defined by componentwise xi>=0 and xi_<=1.
701 return -std::min(*std::min_element(x.begin(),x.end()),1-*std::max_element(x.begin(),x.end()));
702 else
703 // Other elements are not (yet) covered. Fallback to standard Dune implementation
704 return Dune::ReferenceElements<typename LocalCoordinate::field_type,
705 LocalCoordinate::dimension>::general(gt).checkInside(x)? 0: 1;
706}
707
718template <class Cell>
719std::pair<Dune::FieldVector<double,Cell::dimension>,Dune::FieldVector<double,Cell::dimension>>
720boundingBox(Cell const& cell)
721{
722 Dune::FieldVector<double,Cell::dimension> boxmin(std::numeric_limits<double>::max()),
723 boxmax(std::numeric_limits<double>::min());
724
725 auto geo = cell.geometry();
726 for(int i=0; i<geo.corners(); ++i)
727 {
728 auto corner = geo.corner(i);
729 for(int d=0; d<Cell::dimension; ++d)
730 {
731 boxmin[d] = std::min(boxmin[d],corner[d]);
732 boxmax[d] = std::max(boxmax[d],corner[d]);
733 }
734 }
735 return std::make_pair(boxmin,boxmax);
736}
737
738
740template <class Scalar, int n, int m, class Allocator1, class Allocator2>
742{
743 to.resize(from.dim()*m);
744 for(size_t i=0; i<from.N(); ++i)
745 for(int j=0; j<n; ++j)
746 to[(i*n+j)/m][(i*n+j)%m] = from[i][j];
747}
748
753template <class Scalar, int n, int m, class Allocator>
755 std::ostream& os=std::cout)
756{
757 for(size_t i0=0; i0<A.N(); ++i0)
758 for(size_t i1=0; i1<n; ++i1)
759 {
760 for(size_t j0=0; j0<A.M(); ++j0)
761 for(size_t j1=0; j1<m; ++j1)
762 if(A.exists(i0,j0)) os << A[i0][j0][i1][j1] << " ";
763 os << '\n';
764 }
765 os << '\n';
766}
767
772template <class Scalar, int n, int m, class Allocator>
773std::ostream& operator<<(std::ostream& os,
775{
776 bcrsPrint(A,os);
777 return os;
778}
779
780#endif
void transferBlockVector(Dune::BlockVector< Dune::FieldVector< Scalar, n >, Allocator1 > const &from, Dune::BlockVector< Dune::FieldVector< Scalar, m >, Allocator2 > &to)
copy between two block vectors of different element lengths
Definition fixdune.hh:741
int count(Cell const &cell, int codim)
Computes the number of subentities of codimension c of a given entity.
Definition fixdune.hh:667
size_t subIndex(IndexSet const &is, Cell const &cell, int codim, int subentity)
Definition fixdune.hh:655
void bcrsPrint(Dune::BCRSMatrix< Dune::FieldMatrix< Scalar, n, m >, Allocator > const &A, std::ostream &os=std::cout)
Pretty prints the sparse matrix A to the given output stream.
Definition fixdune.hh:754
std::ostream & operator<<(std::ostream &os, Dune::BCRSMatrix< Dune::FieldMatrix< Scalar, n, m >, Allocator > const &A)
Pretty prints the sparse matrix A to the given output stream.
Definition fixdune.hh:773
Dune::FieldVector< T, n > unitVector(int i)
Returns the unit vector with .
Definition fixdune.hh:536
Dune::FieldVector< S, n > operator*(Dune::FieldMatrix< T, n, m > const &A, Dune::FieldVector< S, m > const &x)
Matrix-vector multiplication .
Definition fixdune.hh:227
Dune::FieldMatrix< T, n, m > outerProduct(Dune::FieldVector< T, n > const &x, Dune::FieldVector< T, m > const &y)
outer vector product .
Definition fixdune.hh:127
Dune::FieldMatrix< T, n, m > asMatrix(Dune::FieldVector< T, n *m > const &x)
Converts a vector of size nm to a matrix of size n x m by filling columns successively.
Definition fixdune.hh:112
T trace(Dune::FieldMatrix< T, n, n > const &A)
Matrix trace .
Definition fixdune.hh:478
Dune::FieldVector< T, n > max(Dune::FieldVector< T, n > x, Dune::FieldVector< T, n > const &y)
Componentwise maximum.
Definition fixdune.hh:73
Dune::FieldMatrix< T, n, n > unitMatrix()
Returns the identity matrix of size n .
Definition fixdune.hh:506
Dune::FieldMatrix< T, 3, 3 > skewMatrix(Dune::FieldVector< T, 3 > v)
Returns the skew symmetric matrix related to the cross product with (3d) vector v.
Definition fixdune.hh:519
Dune::FieldVector< T, n > operator+(Dune::FieldVector< T, n > x, Dune::FieldVector< T, n > const &y)
Vector addition .
Definition fixdune.hh:50
Dune::FieldVector< T, n > normalize(Dune::FieldVector< T, n > x)
returns the normalized vector
Definition fixdune.hh:141
Dune::FieldMatrix< T, 1, 2 > vectorProductMatrix(Dune::FieldVector< T, 2 > const &x)
the matrix satisfying for all
Definition fixdune.hh:182
T vectorProduct(Dune::FieldVector< T, 2 > const &x, Dune::FieldVector< T, 2 > const &y)
vector product .
Definition fixdune.hh:172
Dune::FieldVector< T, n > diag(Dune::FieldMatrix< T, n, n > const &A)
Matrix diagonal as a vector.
Definition fixdune.hh:493
Dune::FieldVector< Scalar, n *n > vectorize(Dune::FieldMatrix< Scalar, n, n > const &A)
matrix entries as vector, concatenating columns
Definition fixdune.hh:422
constexpr int rank
Reports the rank of vector, matrix, and tensor types of static size.
Definition scalar.hh:153
Dune::FieldVector< T, n > operator-(Dune::FieldVector< T, n > x)
Vector negation .
Definition fixdune.hh:61
Dune::FieldMatrix< Scalar, n, n > unvectorize(Dune::FieldVector< Scalar, n *n > const &v)
forming quadratic matrix columns of vector segments
Definition fixdune.hh:438
void MatMult(MatrixZ &z, Matrix const &x, Matrix const &y)
Computes Z = X*Y. X and Y need to be compatible, i.e. X.M()==Y.N() has to hold. No aliasing may occur...
Definition fixdune.hh:565
Dune::FieldVector< T, n+m > vertcat(Dune::FieldVector< T, n > const &x, Dune::FieldVector< T, m > const &y)
Concatenation of vectors.
Definition fixdune.hh:154
T contraction(Dune::FieldMatrix< T, n, m > const &A, Dune::FieldMatrix< T, n, m > const &B)
Matrix contraction (Frobenius product) .
Definition fixdune.hh:457
Dune::FieldVector< T, n *m > asVector(Dune::FieldMatrix< T, n, m > const &x)
Converts a matrix of size n x m to a vector of size n*m by concatenating all the columns.
Definition fixdune.hh:98
Dune::FieldVector< T, n > min(Dune::FieldVector< T, n > x, Dune::FieldVector< T, n > const &y)
Componentwise minimum.
Definition fixdune.hh:85
std::pair< Dune::FieldVector< double, Cell::dimension >, Dune::FieldVector< double, Cell::dimension > > boundingBox(Cell const &cell)
Computes the bounding box of a cell.
Definition fixdune.hh:720
LocalCoordinate::field_type checkInside(Dune::GeometryType const &gt, LocalCoordinate const &x)
Checks whether a point is inside or outside the reference element, and how much.
Definition fixdune.hh:692
auto normalForm(Dune::FieldMatrix< T, n, m > const &A)
Definition fixdune.hh:252
Dune::FieldVector< Scalar, n > column(Dune::FieldMatrix< Scalar, n, m > const &A, int j)
Extracts the j-th column of the given matrix A.
Definition fixdune.hh:408
auto horzcat(Dune::FieldVector< Scalar, n > const &c1)
Definition fixdune.hh:291
static int const rows
Definition scalar.hh:77
Dune::FieldMatrix< Scalar, transposed? m:n, transposed? n:m > type
Definition fixdune.hh:553
Returns the matrix type or its transposed type, depending on the given transpose flag.
Definition fixdune.hh:548