GCMMA.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) 2020-2023 PCOpt/NTUA
9  Copyright (C) 2020-2023 FOSS GP
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "GCMMA.H"
30 #include "IOmanip.H"
32 
33 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 
35 namespace Foam
36 {
37  defineTypeNameAndDebug(GCMMA, 1);
39  (
40  lineSearch,
41  GCMMA,
43  );
44 }
45 
46 
47 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
48 
49 void Foam::GCMMA::writeToFiles(const bool isConverged)
50 {
52  const scalarField& rhoValues = mma_.getRho();
53  const label m(rhoValues.size() - 1);
54  if (Pstream::master())
55  {
56  unsigned int width = IOstream::defaultPrecision() + 5;
57  if (writeHeader_)
58  {
60  << setw(width) << "#OuterIter" << " "
61  << setw(width) << "InnerIter" << " "
62  << setw(width) << "rhoObj" << " ";
63 
64  costFile_
65  << setw(width) << "#nCycle" << " "
66  << setw(width) << "cumulativeCost" << " "
67  << setw(width) << "Objective" << " ";
68  for (label i = 0; i < m; ++i)
69  {
71  << setw(width) << "rhoConst" << " ";
72  costFile_
73  << setw(width) << "Constraint" << " ";
74  }
76  << setw(width) << "J" << " "
77  << setw(width) << "JTilda" << " ";
78  for (label i = 0; i < m; ++i)
79  {
81  << setw(width) << "C" << " "
82  << setw(width) << "CTilda" << " ";
83  }
84  GCMMAFile_<< endl;
85  costFile_<< endl;
86 
87  writeHeader_ = false;
88  }
90  << setw(width) << iter_ + 2 << " "
91  << setw(width) << innerIter_ << " ";
92 
93  forAll(rhoValues, i)
94  {
96  << setw(width) << rhoValues[i] << " ";
97  }
98  forAll(rhoValues, i)
99  {
100  GCMMAFile_
101  << setw(width) << objValues[0][i] << " "
102  << setw(width) << objValues[1][i] << " ";
103  }
104  GCMMAFile_ << endl;
105 
106  if (isConverged)
107  {
108  // The cost of this cycle is equal to the number of innerIters
109  // plus one for the adjoint solution
110  cost_ += innerIter_ + 1;
111  costFile_
112  << setw(width) << iter_ + 2 << " "
113  << setw(width) << cost_ << " ";
114  forAll(rhoValues, i)
115  {
116  costFile_
117  << setw(width) << objValues[0][i] << " ";
118  }
119  costFile_<< endl;
120  }
121  }
122 }
123 
124 
125 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
126 
127 Foam::GCMMA::GCMMA
128 (
129  const dictionary& dict,
130  const Time& time,
131  updateMethod& UpdateMethod
132 )
133 :
134  lineSearch(dict, time, UpdateMethod),
135  mma_(refCast<MMA>(UpdateMethod)),
136  GCMMAFile_
137  (time.globalPath()/"optimisation"/"objective"/time.timeName()/"GCMMA"),
138  costFile_
139  (
140  time.globalPath()/"optimisation"/"objective"/time.timeName()
141  /"GCMMACost"
142  ),
143  cost_(2),
144  writeHeader_(true)
145 {
146  // Force rho constants to be updated in each optimisation cycle
148 }
149 
150 
151 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * //
152 
154 {
155  // Calls MMA::updateValuessAndApproximations() and needs to be called
156  // before writeToFiles
157  bool isConverged = mma_.converged();
158  writeToFiles(isConverged);
160  DebugInfo
161  << "GCMMA converged ... " << Switch(isConverged) << endl;
162  return isConverged;
163 }
164 
165 
167 {
168  DebugInfo
169  << "GCMMA:: recomputing direction "<< endl;
170 
171  // Update rho values
172  mma_.updateRho();
173 
174  // Re-solve the subproblem
175  mma_.solveSubproblem();
176 }
177 
178 
180 {
181  correction = mma_.returnCorrection();
182 }
183 
184 
185 // ************************************************************************* //
tmp< fvMatrix< Type > > correction(const fvMatrix< Type > &)
Return the correction form of the given matrix by subtracting the matrix multiplied by the current fi...
MMA & mma_
Cast of the update method to MMA.
Definition: GCMMA.H:73
dictionary dict
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:116
void setVariableRho(bool varRho=true)
Set variable rho.
Definition: MMA.C:1118
OFstream GCMMAFile_
File with rho values in each inner loop.
Definition: GCMMA.H:78
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
Type & refCast(U &obj)
A dynamic_cast (for references). Generates a FatalError on failed casts and uses the virtual type() m...
Definition: typeInfo.H:159
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
A simple wrapper around bool so that it can be read as a word: true/false, on/off, yes/no, any/none. Also accepts 0/1 as a string and shortcuts t/f, y/n.
Definition: Switch.H:77
void writeToFiles(bool isConverged)
Write rho and objective/constraint values & approx to files.
Definition: GCMMA.C:42
static unsigned int defaultPrecision() noexcept
Return the default precision.
Definition: IOstream.H:423
Macros for easy insertion into run-time selection tables.
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:421
word timeName
Definition: getTimeIndex.H:3
virtual void updateStep()
Actually computes a new direction entirely, targeting the satisfaction of the GCMMA condition...
Definition: GCMMA.C:159
virtual void updateCorrection(scalarField &correction)
Replace the correction with the one computed in updateStep.
Definition: GCMMA.C:172
const scalarField & getRho() const
Get rho value if needed.
Definition: MMA.C:1112
label iter_
Optimisation cycle.
Definition: lineSearch.H:112
#define DebugInfo
Report an information message using Foam::Info.
Istream and Ostream manipulators taking arguments.
defineTypeNameAndDebug(combustionModel, 0)
label innerIter_
Inner line search iteration.
Definition: lineSearch.H:117
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers...
Definition: List.H:55
static bool master(const label communicator=worldComm)
True if process corresponds to the master rank in the communicator.
Definition: UPstream.H:1082
const PtrList< scalarField > & getValuesAndApproximations() const
Get objective/constraint values and their approximations.
Definition: MMA.C:1077
Omanip< int > setw(const int i)
Definition: IOmanip.H:199
virtual bool converged()
Check whether linear search has converged.
Definition: GCMMA.C:146
OFstream costFile_
File with CPU cost in each inner loop.
Definition: GCMMA.H:83
label cost_
Total cost, measured in Equivalent Flow Solutions, up to this cycle.
Definition: GCMMA.H:88
bool writeHeader_
Write the header of the output files.
Definition: GCMMA.H:93
Namespace for OpenFOAM.
addToRunTimeSelectionTable(functionObject, pointHistory, dictionary)