Loading...
Searching...
No Matches
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-------------------------------------------------------------------------------
11License
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
35namespace Foam
36{
39 (
41 GCMMA,
43 );
44}
45
46
47// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
48
49void 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
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" << " ";
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 }
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 {
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;
112 << setw(width) << iter_ + 2 << " "
113 << setw(width) << cost_ << " ";
114 forAll(rhoValues, i)
115 {
117 << setw(width) << objValues[0][i] << " ";
118 }
119 costFile_<< endl;
121 }
122}
123
124
125// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
126
127Foam::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
147 mma_.setVariableRho();
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);
161 << "GCMMA converged ... " << Switch(isConverged) << endl;
162 return isConverged;
163}
164
165
167{
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// ************************************************************************* //
Istream and Ostream manipulators taking arguments.
Macros for easy insertion into run-time selection tables.
#define addToRunTimeSelectionTable(baseType, thisType, argNames)
Add to construction table with typeName as the key.
Class implementing a workaround for using the Globally converging wrapper of the MMA update method....
Definition GCMMA.H:65
virtual bool converged()
Check whether linear search has converged.
Definition GCMMA.C:146
virtual void updateStep()
Actually computes a new direction entirely, targeting the satisfaction of the GCMMA condition.
Definition GCMMA.C:159
OFstream GCMMAFile_
File with rho values in each inner loop.
Definition GCMMA.H:78
void writeToFiles(bool isConverged)
Write rho and objective/constraint values & approx to files.
Definition GCMMA.C:42
virtual void updateCorrection(scalarField &correction)
Replace the correction with the one computed in updateStep.
Definition GCMMA.C:172
MMA & mma_
Cast of the update method to MMA.
Definition GCMMA.H:73
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
static unsigned int defaultPrecision() noexcept
Return the default precision.
Definition IOstream.H:437
Update design variables using the Method of Moving Assymptotes. Can handle inequality constraints.
Definition MMA.H:65
const scalarField & getRho() const
Get rho value if needed.
Definition MMA.C:1128
const PtrList< scalarField > & getValuesAndApproximations() const
Get objective/constraint values and their approximations.
Definition MMA.C:1093
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers....
Definition PtrList.H:67
A simple wrapper around bool so that it can be read as a word: true/false, on/off,...
Definition Switch.H:81
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition Time.H:75
void size(const label n)
Older name for setAddressableSize.
Definition UList.H:118
static bool master(const label communicator=worldComm)
True if process corresponds to the master rank in the communicator.
Definition UPstream.H:1714
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition dictionary.H:133
Abstract base class for line search methods.
Definition lineSearch.H:54
label iter_
Optimisation cycle.
Definition lineSearch.H:112
label innerIter_
Inner line search iteration.
Definition lineSearch.H:117
Abstract base class for optimisation methods.
#define defineTypeNameAndDebug(Type, DebugSwitch)
Define the typeName and debug information.
Definition className.H:142
word timeName
Definition getTimeIndex.H:3
#define DebugInfo
Report an information message using Foam::Info.
Namespace for OpenFOAM.
Type & refCast(U &obj)
A dynamic_cast (for references) to Type reference.
Definition typeInfo.H:172
Field< scalar > scalarField
Specialisation of Field<T> for scalar.
Omanip< int > setw(const int i)
Definition IOmanip.H:199
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition Ostream.H:519
tmp< fvMatrix< Type > > correction(const fvMatrix< Type > &)
Return the correction form of the given matrix by subtracting the matrix multiplied by the current fi...
dictionary dict
#define forAll(list, i)
Loop across all elements in list.
Definition stdFoam.H:299