diff --git a/src/OpenFOAM/matrices/QRMatrix/QRMatrix.C b/src/OpenFOAM/matrices/QRMatrix/QRMatrix.C index fa9f6833af..0b63f2017b 100644 --- a/src/OpenFOAM/matrices/QRMatrix/QRMatrix.C +++ b/src/OpenFOAM/matrices/QRMatrix/QRMatrix.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2016 OpenFOAM Foundation - Copyright (C) 2019-2020 OpenCFD Ltd. + Copyright (C) 2019-2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -492,9 +492,10 @@ template MatrixType Foam::pinv ( const MatrixType& A, - const scalar tol + const scalar tolerance ) { + scalar tol = tolerance; typedef typename MatrixType::cmptType cmptType; if (A.empty()) @@ -506,7 +507,12 @@ MatrixType Foam::pinv if (A.size() == 1) { - return MatrixType({1, 1}, cmptType(1.0)/(A(0,0) + cmptType(VSMALL))); + if (A(0,0) == cmptType(0)) + { + return MatrixType({1, 1}, cmptType(0)); + } + + return MatrixType({1, 1}, cmptType(1)/(A(0,0) + cmptType(VSMALL))); } QRMatrix QRM @@ -521,27 +527,47 @@ MatrixType Foam::pinv // R1 (KP:p. 648) // Find the first diagonal element index with (almost) zero value in R label firstZeroElemi = 0; + label i = 0; + while (i < 2) { const List diag(R.diag()); - auto lessT = [&](const cmptType& x) { return mag(x) < tol; }; + auto lessThan = [=](const cmptType& x) { return tol > mag(x); }; firstZeroElemi = std::distance ( diag.cbegin(), - std::find_if(diag.cbegin(), diag.cend(), lessT) + std::find_if(diag.cbegin(), diag.cend(), lessThan) ); - } - if (firstZeroElemi == 0) - { - WarningInFunction - << "The largest (magnitude) diagonal element is (almost) zero." - << nl << "Returning a zero matrix." - << endl; + if (firstZeroElemi == 0) + { + if (i == 0) + { + WarningInFunction + << "The largest diagonal element magnitude is nearly zero. " + << "Tightening the tolerance." + << endl; - return MatrixType(A.sizes(), Zero); + tol = 1e-13; + ++i; + } + else + { + WarningInFunction + << "The largest diagonal element magnitude is nearly zero. " + << "Returning a zero matrix." + << endl; + ++i; + + return MatrixType({A.n(), A.m()}, Zero); + } + } + else + { + i += 2; + } } // Exclude the first (almost) zero diagonal row and the rows below @@ -599,4 +625,5 @@ MatrixType Foam::pinv } } + // ************************************************************************* // diff --git a/src/OpenFOAM/matrices/QRMatrix/QRMatrix.H b/src/OpenFOAM/matrices/QRMatrix/QRMatrix.H index 415fbba853..ee9bd62edf 100644 --- a/src/OpenFOAM/matrices/QRMatrix/QRMatrix.H +++ b/src/OpenFOAM/matrices/QRMatrix/QRMatrix.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2016 OpenFOAM Foundation - Copyright (C) 2019 OpenCFD Ltd. + Copyright (C) 2019-2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. diff --git a/src/functionObjects/field/DMD/DMD.C b/src/functionObjects/field/DMD/DMD.C new file mode 100644 index 0000000000..e225d31cb4 --- /dev/null +++ b/src/functionObjects/field/DMD/DMD.C @@ -0,0 +1,234 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | www.openfoam.com + \\/ M anipulation | +------------------------------------------------------------------------------- + Copyright (C) 2020-2021 OpenCFD Ltd. +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +\*---------------------------------------------------------------------------*/ + +#include "DMD.H" +#include "DMDModel.H" +#include "addToRunTimeSelectionTable.H" + +// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // + +namespace Foam +{ +namespace functionObjects +{ + defineTypeNameAndDebug(DMD, 0); + addToRunTimeSelectionTable(functionObject, DMD, dictionary); +} +} + + +// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // + +void Foam::functionObjects::DMD::snapshot() +{ + bool processed = false; + processed = processed || getSnapshot(); + processed = processed || getSnapshot(); + processed = processed || getSnapshot(); + processed = processed || getSnapshot(); + processed = processed || getSnapshot(); + + if (!processed) + { + FatalErrorInFunction + << " functionObjects::" << type() << " " << name() << ":" + << " cannot find required input field during snapshot loading: " + << fieldName_ << nl + << " Do you execute required functionObjects" + << " before executing DMD, e.g. mapFields?" + << exit(FatalError); + } +} + + +void Foam::functionObjects::DMD::initialise() +{ + const label nComps = DMDModelPtr_->nComponents(fieldName_); + + if (patch_.empty()) + { + nSnap_ = nComps*mesh_.nCells(); + } + else + { + const label patchi = mesh_.boundaryMesh().findPatchID(patch_); + + if (patchi < 0) + { + FatalErrorInFunction + << "Cannot find patch " << patch_ + << exit(FatalError); + } + + nSnap_ = nComps*(mesh_.C().boundaryField()[patchi]).size(); + } + + const label nSnapTotal = returnReduce(nSnap_, sumOp