Loading...
Searching...
No Matches
inverseLaplacianProblemTotalHeatMeasure_CG.C
Go to the documentation of this file.
1/*---------------------------------------------------------------------------*\
2 ██╗████████╗██╗ ██╗ █████╗ ██████╗ █████╗ ███████╗██╗ ██╗
3 ██║╚══██╔══╝██║ ██║██╔══██╗██╔════╝██╔══██╗ ██╔════╝██║ ██║
4 ██║ ██║ ███████║███████║██║ ███████║█████╗█████╗ ██║ ██║
5 ██║ ██║ ██╔══██║██╔══██║██║ ██╔══██║╚════╝██╔══╝ ╚██╗ ██╔╝
6 ██║ ██║ ██║ ██║██║ ██║╚██████╗██║ ██║ ██║ ╚████╔╝
7 ╚═╝ ╚═╝ ╚═╝ ╚═╝╚═╝ ╚═╝ ╚═════╝╚═╝ ╚═╝ ╚═╝ ╚═══╝
8
9 * In real Time Highly Advanced Computational Applications for Finite Volumes
10 * Copyright (C) 2017 by the ITHACA-FV authors
11-------------------------------------------------------------------------------
12
13 License
14 This file is part of ITHACA-FV
15
16 ITHACA-FV is free software: you can redistribute it and/or modify
17 it under the terms of the GNU Lesser General Public License as published by
18 the Free Software Foundation, either version 3 of the License, or
19 (at your option) any later version.
20
21 ITHACA-FV is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU Lesser General Public License for more details.
25
26 You should have received a copy of the GNU Lesser General Public License
27 along with ITHACA-FV. If not, see <http://www.gnu.org/licenses/>.
28
29\*---------------------------------------------------------------------------*/
30
33
34
36
37// * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * * //
38
39// Constructors
41
48
49// * * * * * * * * * * * * * * Full Order Methods * * * * * * * * * * * * * * //
50
52{
53 fvMesh& mesh = _mesh();
54 set_g();
56 cgIter = 0;
57 J = 0;
58 P = g;
59 gradJ = g; //Gradient of the cost function [W/m2]
60 gamma = 0.0;
61 gamma_den = 0.0;
62 label sampleI = 1;
63 gList.resize(0);
64 Tfield.resize(0);
65 lambdaField.resize(0);
66 deltaTfield.resize(0);
67 M_Assert(std::abs(gIntegral_meas) > 1e-16, "First set up gIntegral_meas");
68 M_Assert(std::abs(gIntegralWeight) > 1e-16, "First set up gIntegralWeight");
69 M_Assert(!interpolation, "Interpolation not implemented yet");
70
71 while (cgIter < cgIterMax)
72 {
73 Info << "Iteration " << cgIter + 1 << endl;
74 restart();
77
78 if (saveSolInLists && cgIter == 0)
79 {
80 gList.append(g.clone());
81 }
82
83 volScalarField& T = _T();
84 ITHACAstream::exportSolution(T, std::to_string(sampleI),
85 "./ITHACAoutput/CGtest/", T.name());
87
89 {
90 Jlist.conservativeResize(cgIter + 1, 1);
91 Jlist(cgIter) = J;
92 ITHACAstream::exportMatrix(Jlist, "costFunctionFull", "eigen", "./");
93 return (1);
94 }
95
96 Jlist.conservativeResize(cgIter + 1, 1);
97 Jlist(cgIter) = J;
99 volScalarField& lambda = _lambda();
100 ITHACAstream::exportSolution(lambda, std::to_string(sampleI),
101 "./ITHACAoutput/CGtest/", lambda.name());
102 computeGradJ();
105 volScalarField& deltaT = _deltaT();
106 ITHACAstream::exportSolution(deltaT, std::to_string(sampleI),
107 "./ITHACAoutput/CGtest/", deltaT.name());
111
112 if (saveSolInLists)
113 {
114 volScalarField& T = _T();
115 volScalarField& lambda = _lambda();
116 volScalarField& deltaT = _deltaT();
117 gList.append(g.clone());
118 Tfield.append(T.clone());
119 lambdaField.append(lambda.clone());
120 deltaTfield.append(deltaT.clone());
121 sampleI++;
122 }
123
124 cgIter++;
125 }
126
127 return (0);
128}
129
131{
132 fvMesh& mesh = _mesh();
133 volScalarField& lambda = _lambda();
134 gradJ_L2norm = 0;
135 forAll (lambda.boundaryField()[hotSide_ind], faceI)
136 {
137 gradJ [faceI] = - lambda.boundaryField()[hotSide_ind][faceI] +
139 gradJ_L2norm += gradJ[faceI] * gradJ[faceI] *
140 mesh.magSf().boundaryField()[hotSide_ind][faceI];
141 }
142 gradJ_L2norm = Foam::sqrt(gradJ_L2norm);
143 Info << "gradJ L2norm = " << gradJ_L2norm << endl;
144}
145
147{
148 fvMesh& mesh = _mesh();
149 double Pintegral = ITHACAutilities::integralOnPatch(mesh, P, "hotSide");
150 beta = Tdiff.dot(Tsens) - gIntegralWeight * Pintegral *
152 double betaDiv = Tsens.dot(Tsens) - gIntegralWeight * Pintegral * Pintegral;
153 beta = beta / betaDiv;
154 Info << "beta = " << beta << endl;
155}
156
158{
159 double Jold = J;
160 J = 0.5 * Tdiff.dot(Tdiff) + 0.5 * gIntegralWeight * (gIntegral -
162 //reduce(J, sumOp<double>());
163 Info << "J = " << J << endl;
164
165 if (J <= Jtol)
166 {
167 Info << "Convergence reached in " << cgIter << " iterations" << endl;
168 return (1);
169 }
170 else if (Foam::mag((Jold - J) / J) <= JtolRel)
171 {
172 Info << "Relative tolerance criteria meet in " << cgIter << " iterations" <<
173 endl;
174 Info << "|Jold - J| / |J| = " << Foam::mag((Jold - J) / J) << endl;
175 return (1);
176 }
177 else
178 {
179 return (0);
180 }
181}
182
183
184
forAll(example_CG.gList, solutionI)
Definition CGtest.H:21
Foam::fvMesh & mesh
Definition createMesh.H:47
#define M_Assert(Expr, Msg)
int conjugateGradientConvergenceCheck()
Convergence cher for the conjugate gradient method.
void computeGradJ()
Computes the gradient of cost function J and its L2 norm.
Implementation of a Alifanov's regularization to solve inverse Laplacian problems.
int cgIterMax
Maximum CG iterations.
Eigen::VectorXd Tsens
Vector of solutions of the sensitivity problem at the thermocouples points.
Eigen::MatrixXd Jlist
Vector to store the const function J.
void set_valueFraction()
Set valueFraction list values for Robin condition.
PtrList< volScalarField > lambdaField
List of adjoint solutions.
double gamma
Conjugate coefficient.
bool saveSolInLists
Flag to save solutions in lists.
double Jtol
Absolute stopping criterion for the CG.
int cgIter
Conjugate Gradient (CG) interations counter.
double gamma_den
Denoinator of the conjugate coefficient.
void sensibilitySolAtThermocouplesLocations()
Fill the Foam::vector containing the values of the sensitivity solution at the thermocouples location...
void updateHeatFlux()
Updates the heat flux in the conjugate gradient iterations.
List< scalar > gradJ
Gradient of the cost function.
bool interpolation
Flag for interpolation of the temperature measurements.
List< scalar > P
Search direction.
void solveSensitivity()
Solve sensibility problem.
double gradJ_L2norm
L2 norm of the gradient of J.
PtrList< volScalarField > Tfield
List of temperature solutions.
double JtolRel
Relative stopping criterion for the CG.
autoPtr< volScalarField > _deltaT
Sensibility temperature field.
void solveAdjoint()
Solve adjoint problem.
PtrList< volScalarField > deltaTfield
List of sensitivity solutions.
autoPtr< volScalarField > _lambda
Adjoint field.
void searchDirection()
Computes the search direction P.
void differenceBetweenDirectAndMeasure()
Computes the difference between direct problem solution and measure.
Eigen::VectorXd Tdiff
Difference between computed and measured temperatures at the thermocouples.
label hotSide_ind
Index of the hotSide patch.
List< scalar > g
Heat flux at hotSide [W/m2].
double J
Cost funtion [K^2].
List< List< scalar > > gList
List of boundary heat fluxes.
void set_g()
Set the right g size and fills it with zeros.
autoPtr< fvMesh > _mesh
Mesh.
void solveDirect()
Solve direct problem.
autoPtr< volScalarField > _T
Temperature field.
volScalarField & T
Definition createT.H:46
Header file of the inverseLaplacianProblemTotalHeatMeasure_CG class.
void exportSolution(GeometricField< Type, PatchField, GeoMesh > &s, fileName subfolder, fileName folder, word fieldName)
Export a field to file in a certain folder and subfolder.
void exportMatrix(Eigen::Matrix< T, -1, dim > &matrix, word Name, word type, word folder)
Export the reduced matrices in numpy (type=python), matlab (type=matlab) and txt (type=eigen) format ...
double integralOnPatch(fvMesh &mesh, volScalarField &field, word patch)
Evaluate the integral on a patch.