MatrixTools.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2019 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "MatrixTools.H"
29 
30 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * * //
31 
32 template<class Form1, class Form2, class Type>
34 (
35  const Matrix<Form1, Type>& A,
36  const Matrix<Form2, Type>& B,
37  const bool verbose,
38  const label maxDiffs,
39  const scalar relTol,
40  const scalar absTol
41 )
42 {
43  const label len = A.size();
44 
45  if (len != B.size())
46  {
47  if (verbose)
48  {
49  Info<< "Matrices have different sizes: "
50  << len << " vs " << B.size() << nl;
51  }
52  return false;
53  }
54 
55  auto iter1 = A.cbegin();
56  auto iter2 = B.cbegin();
57 
58  label nDiffs = 0;
59 
60  for (label i = 0; i < len; ++i)
61  {
62  if ((absTol + relTol*mag(*iter2)) < Foam::mag(*iter1 - *iter2))
63  {
64  ++nDiffs;
65 
66  if (verbose)
67  {
68  Info<< "Matrix element " << i
69  << " differs beyond tolerance: "
70  << *iter1 << " vs " << *iter2 << nl;
71  }
72  if (maxDiffs && maxDiffs < nDiffs)
73  {
74  Info<< "More than " << maxDiffs << " elements differ."
75  << " Skipping further comparisons." << nl;
76  return false;
77  }
78  }
79 
80  ++iter1;
81  ++iter2;
82  }
83 
84  if (verbose)
85  {
86  if (nDiffs)
87  {
88  Info<< "Some elements differed" << nl;
89  }
90  else
91  {
92  Info<< "All elements equal within the tolerances" << nl;
93  }
94  }
95 
96  return !nDiffs;
97 }
98 
99 
100 template<class Container>
102 (
103  Ostream& os,
104  const Container& mat
105 )
106 {
107  os << mat.m() << ' ' << mat.n();
108 
109  if (mat.m() == 1)
110  {
111  // row-vector
112  os << " (";
113  for (label j = 0; j < mat.n(); ++j)
114  {
115  if (j) os << ' ';
116  os << mat(0,j);
117  }
118  os << ')' << nl;
119  }
120  else if (mat.n() == 1)
121  {
122  // col-vector
123 
124  os << " (";
125  for (label i = 0; i < mat.m(); ++i)
126  {
127  if (i) os << ' ';
128  os << mat(i,0);
129  }
130  os << ')' << nl;
131  }
132  else
133  {
134  // Regular
135 
136  os << nl << '(' << nl;
137 
138  for (label i = 0; i < mat.m(); ++i)
139  {
140  os << '(';
141  for (label j = 0; j < mat.n(); ++j)
142  {
143  if (j) os << ' ';
144  os << mat(i,j);
145  }
146  os << ')' << nl;
147  }
148  os << ')' << nl;
149  }
150 
151  return os;
152 }
153 
154 
155 // ************************************************************************* //
MatrixTools.H
B
static const Foam::dimensionedScalar B("", Foam::dimless, 18.678)
A
static const Foam::dimensionedScalar A("", Foam::dimPressure, 611.21)
Foam::MatrixTools::equal
bool equal(const Matrix< Form1, Type > &A, const Matrix< Form2, Type > &B, const bool verbose=false, const label maxDiffs=10, const scalar relTol=1e-5, const scalar absTol=1e-8)
Compare matrix elements for absolute or relative equality.
Definition: MatrixTools.C:34
Foam::Matrix
A templated (m x n) matrix of objects of <T>. The layout is (mRows x nCols) - row-major order:
Definition: DiagonalMatrix.H:53
Foam::Info
messageStream Info
Information stream (stdout output on master, null elsewhere)
os
OBJstream os(runTime.globalPath()/outputName)
Foam::MatrixTools::printMatrix
Ostream & printMatrix(Ostream &os, const Container &mat)
Simple ASCII output of Matrix, MatrixBlock.
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::mag
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56