Loading...
Searching...
No Matches
ITHACAregularization.C
Go to the documentation of this file.
1/*---------------------------------------------------------------------------*\
2 ██╗████████╗██╗ ██╗ █████╗ ██████╗ █████╗ ███████╗██╗ ██╗
3 ██║╚══██╔══╝██║ ██║██╔══██╗██╔════╝██╔══██╗ ██╔════╝██║ ██║
4 ██║ ██║ ███████║███████║██║ ███████║█████╗█████╗ ██║ ██║
5 ██║ ██║ ██╔══██║██╔══██║██║ ██╔══██║╚════╝██╔══╝ ╚██╗ ██╔╝
6 ██║ ██║ ██║ ██║██║ ██║╚██████╗██║ ██║ ██║ ╚████╔╝
7 ╚═╝ ╚═╝ ╚═╝ ╚═╝╚═╝ ╚═╝ ╚═════╝╚═╝ ╚═╝ ╚═╝ ╚═══╝
8 * In real Time Highly Advanced Computational Applications for Finite Volumes
9 * Copyright (C) 2017 by the ITHACA-FV authors
10-------------------------------------------------------------------------------
11License
12 This file is part of ITHACA-FV
13 ITHACA-FV is free software: you can redistribute it and/or modify
14 it under the terms of the GNU Lesser 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 ITHACA-FV is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU Lesser General Public License for more details.
21 You should have received a copy of the GNU Lesser General Public License
22 along with ITHACA-FV. If not, see <http://www.gnu.org/licenses/>.
23\*---------------------------------------------------------------------------*/
24
28
30
31
32// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
34{
35
36Eigen::VectorXd TSVD(Eigen::MatrixXd A,
37 Eigen::MatrixXd b, int filter)
38{
39 M_Assert(b.cols() == 1, "The b input in TSVD must have only one column");
40 Eigen::JacobiSVD<Eigen::MatrixXd> svd(A,
41 Eigen::ComputeThinU | Eigen::ComputeThinV);
42 Eigen::MatrixXd U = svd.matrixU();
43 Eigen::MatrixXd V = svd.matrixV();
44 Eigen::VectorXd x = Eigen::VectorXd::Zero(b.size());
45
46 for (label i = 0; i < filter; i++)
47 {
48 x += (U.col(i).dot(b.col(0)) / svd.singularValues()(i)) * (V.col(i));
49 }
50
51 return x;
52}
53
54Eigen::VectorXd TSVD(Eigen::MatrixXd A,
55 Eigen::MatrixXd b, double noiseVariance, word parameterMethod)
56{
57 int filter;
58
59 if (parameterMethod == "DP")
60 {
61 Info << "\nRegularization parameter selected by Discrepancy principle" << endl;
62 Eigen::JacobiSVD<Eigen::MatrixXd> svd(A,
63 Eigen::ComputeThinU | Eigen::ComputeThinV);
64 Eigen::MatrixXd U = svd.matrixU();
65 Eigen::VectorXd bVect = b.col(0);
66 double min;
67
68 for (int col = 0; col < U.cols() - 1; col++)
69 {
70 double f = 0;
71
72 for (int i = col + 1; i < U.cols(); i++)
73 {
74 Eigen::VectorXd tempU = U.col(i);
75 f += tempU.col(i).dot(bVect) * tempU.col(i).dot(bVect);
76 }
77
78 f += 2 * noiseVariance * col;
79
80 if (col == 0)
81 {
82 min = f;
83 filter = col + 1;
84 }
85 else if (min > f)
86 {
87 min = f;
88 filter = col + 1;
89 }
90
91 Info << "debug : f = " << f << endl;
92 Info << "debug : min = " << min << endl;
93 Info << "debug : k = " << filter << endl;
94 }
95 }
96 else if (parameterMethod == "UPRE")
97 {
98 Info << "\nRegularization parameter selected by Discrepancy principle" << endl;
99 }
100 else
101 {
102 Info << "Regularization parameter selection methods available are:" << endl
103 << "DP, UPRE" << endl;
104 exit(1);
105 }
106
107 return ITHACAregularization::TSVD(A, b, filter);
108}
109
110Eigen::VectorXd Tikhonov(Eigen::MatrixXd A,
111 Eigen::MatrixXd b, double regularizationParameter)
112{
113 M_Assert(b.cols() == 1, "The b input in TSVD must have only one column");
114 Eigen::MatrixXd Anew = A.transpose() * A + regularizationParameter *
115 Eigen::MatrixXd::Identity(A.rows(), A.cols());
116 Eigen::MatrixXd bNew = A.transpose() * b;
117 Eigen::VectorXd x = A.inverse() * b;
118 std::cout << "x = \n" << x.transpose() << std::endl;
119 x = Anew.inverse() * bNew;
120 std::cout << "xNew = \n" << x.transpose() << std::endl;
121 return x;
122}
123}
#define M_Assert(Expr, Msg)
Header file of the ITHACAregularization class, it contains the implementation of several methods for ...
volScalarField & A
Namespace for regularization of ill-conditione linear system.
Eigen::VectorXd Tikhonov(Eigen::MatrixXd A, Eigen::MatrixXd b, double regularizationParameter)
Truncated Singular Value regularization.
Eigen::VectorXd TSVD(Eigen::MatrixXd A, Eigen::MatrixXd b, int filter)
Truncated Singular Value regularization.
singVal col(i)
Info<< endl;Info<< "*********************************************************"<< endl;Info<< "Performing test for the parameterized BC inverse solver"<< endl;Info<< endl;word outputFolder="./ITHACAoutput/parameterizedBCtest_RBFparameter/";volScalarField gTrueField=example_paramBC.list2Field(example_paramBC.gTrue);ITHACAstream::exportSolution(gTrueField, "1", outputFolder, "gTrue");Eigen::VectorXd rbfWidth=EigenFunctions::ExpSpaced(0.001, 100, rbfWidthTest_size);ITHACAstream::exportMatrix(rbfWidth, "rbfShapeParameters", "eigen", outputFolder);Eigen::VectorXd residualNorms;residualNorms.resize(rbfWidthTest_size);Eigen::VectorXd heatFluxL2norm(rbfWidthTest_size);Eigen::VectorXd heatFluxLinfNorm=heatFluxL2norm;Eigen::VectorXd condNumber=heatFluxL2norm;Eigen::MatrixXd singVal;for(int i=0;i< rbfWidthTest_size;i++){ Info<< "*********************************************************"<< endl;Info<< "RBF parameter "<< rbfWidth(i)<< endl;Info<< "Test "<< i+1<< endl;Info<< endl;example_paramBC.set_gParametrized("rbf", rbfWidth(i));example_paramBC.parameterizedBCoffline(1);example_paramBC.parameterizedBC("fullPivLU");volScalarField gParametrizedField=example_paramBC.list2Field(example_paramBC.g);ITHACAstream::exportSolution(gParametrizedField, std::to_string(i+1), outputFolder, "gParametrized");volScalarField &T(example_paramBC._T());ITHACAstream::exportSolution(T, std::to_string(i+1), outputFolder, "T");residualNorms(i)=Foam::sqrt(example_paramBC.residual.squaredNorm());Eigen::MatrixXd A=example_paramBC.Theta.transpose() *example_paramBC.Theta;Eigen::JacobiSVD< Eigen::MatrixXd > svd(A, Eigen::ComputeThinU|Eigen::ComputeThinV)
volVectorField & U
label i
Definition pEqn.H:46