Loading...
Searching...
No Matches
SQPBase.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) 2007-2021 PCOpt/NTUA
9 Copyright (C) 2013-2021 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 "SQPBase.H"
30#include "IOmanip.H"
33
34// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
35
36namespace Foam
38 defineTypeNameAndDebug(SQPBase, 1);
39}
40
41
42// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
43
44Foam::SQPBase::SQPBase
45(
46 const fvMesh& mesh,
47 const dictionary& dict,
48 autoPtr<designVariables>& designVars,
49 const updateMethod& UpdateMethod,
50 const word& type
51)
52:
54 (
55 mesh,
56 dict,
57 designVars,
58 UpdateMethod.nConstraints(),
59 type
60 ),
61 LagrangianDerivatives_(designVars().getVars().size(), Zero),
62 constraintDerivativesOld_
63 (
64 UpdateMethod.nConstraints(),
65 scalarField(LagrangianDerivatives_.size(), Zero)
66 ),
67 lamdas_
68 (
69 UpdateMethod.found("lamdas") ?
70 scalarField("lamdas", UpdateMethod, UpdateMethod.nConstraints()) :
71 scalarField(UpdateMethod.nConstraints(), Zero)
72 ),
73 objFunctionFolder_
74 (
75 mesh.time().globalPath()/"optimisation"/"objective"/
76 mesh.time().timeName()
77 ),
78 meritFunctionFile_(nullptr),
79 mu_(Zero),
80 delta_
81 (
82 UpdateMethod.coeffsDict(type).getOrDefault<scalar>("delta", 0.1)
83 )
84{
85 // Read in old constraint derivatives if present
86 forAll(lamdas_, cI)
87 {
88 if (UpdateMethod.found("constraintDerivativesOld" + Foam::name(cI)))
89 {
92 (
93 "constraintDerivativesOld" + Foam::name(cI),
94 UpdateMethod,
96 );
97 }
98 }
99 // Create folder to merit function
100 if (Pstream::master())
101 {
103 }
104}
105
106
107// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
108
109bool Foam::SQPBase::addToFile(Ostream& os) const
110{
111 forAll(constraintDerivativesOld_, cI)
112 {
113 constraintDerivativesOld_[cI].
114 writeEntry("constraintDerivativesOld" + Foam::name(cI), os);
116 lamdas_.writeEntry("lamdas", os);
117
118 return true;
119}
120
121
122bool Foam::SQPBase::writeMeritFunction(const updateMethod& UpdateMethod)
123{
124 scalar objectivePart = UpdateMethod.getObjectiveValue();
125 scalar constraintPart = mu_*meritFunctionConstraintPart();
126 scalar merit = objectivePart + constraintPart;
127 const scalarField& cValues = UpdateMethod.getConstraintValues();
128 if (Pstream::master())
129 {
130 unsigned int width = IOstream::defaultPrecision() + 6;
131 unsigned int constraintsSize = lamdas_.size();
132 constraintsSize = constraintsSize*(width + 1) + 2;
133
134 // Open file and write header
135 if (!meritFunctionFile_)
136 {
137 meritFunctionFile_.reset
138 (
139 new OFstream(objFunctionFolder_/word("meritFunction"))
140 );
141
142 meritFunctionFile_()
143 << setw(1) << "#" << " "
144 << setw(width) << "merit" << " "
145 << setw(width) << "J" << " "
146 << setw(constraintsSize) << "lamdas" << " "
147 << setw(constraintsSize) << "constraints" << " "
148 << setw(width) << "mu" << " "
149 << setw(width) << "constraintContr" << endl;
150
151 }
152
153 meritFunctionFile_()
154 << setw(1) << UpdateMethod.getCycle() << " "
155 << setw(width) << merit << " "
156 << setw(width) << objectivePart << " "
157 << setw(1) << "(";
158
159 forAll(lamdas_, cI)
160 {
161 meritFunctionFile_()
162 << setw(width) << lamdas_[cI] << setw(1) << " ";
163 }
164 meritFunctionFile_() << setw(3) << ")(";
165 forAll(cValues, cI)
166 {
167 meritFunctionFile_()
168 << setw(width) << cValues[cI] << setw(1) << " ";
169 }
170 meritFunctionFile_() << setw(2) << ") ";
171 meritFunctionFile_() << setw(width) << mu_ << " ";
172 meritFunctionFile_() << setw(width) << constraintPart << endl;
173 }
174 return true;
175}
176
177
178// ************************************************************************* //
Istream and Ostream manipulators taking arguments.
bool found
Macros for easy insertion into run-time selection tables.
static unsigned int defaultPrecision() noexcept
Return the default precision.
Definition IOstream.H:437
Output to file stream as an OSstream, normally using std::ofstream for the actual output.
Definition OFstream.H:75
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
Base class for Sequantial Quadratic Programming (SQP) methods.
Definition SQPBase.H:53
scalarField lamdas_
Lagrange multipliers.
Definition SQPBase.H:71
virtual bool addToFile(Ostream &os) const
Write continuation info.
Definition SQPBase.C:102
scalar mu_
Penalty value for the merit function.
Definition SQPBase.H:86
virtual bool writeMeritFunction(const updateMethod &UpdateMethod)
Write info about the merit function.
Definition SQPBase.C:115
List< scalarField > constraintDerivativesOld_
The previous constraint derivatives.
Definition SQPBase.H:66
autoPtr< OFstream > meritFunctionFile_
File including the l1 merit function.
Definition SQPBase.H:81
scalar delta_
Safety factor.
Definition SQPBase.H:91
virtual scalar meritFunctionConstraintPart() const =0
Get the part the merit function that depends on the constraints.
fileName objFunctionFolder_
Name of the objective folder.
Definition SQPBase.H:76
scalarField LagrangianDerivatives_
Derivatives of the Lagrangian function.
Definition SQPBase.H:61
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
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition autoPtr.H:65
Abstract base class for optimisation methods supporting constraints. Does not add functionality to up...
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition dictionary.H:133
bool found(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Find an entry (const access) with the given keyword.
Mesh data needed to do the Finite Volume discretisation.
Definition fvMesh.H:85
Abstract base class for optimisation methods.
scalar getObjectiveValue() const
Get objective value.
label getCycle() const
Get optimisation cycle.
const scalarField & getConstraintValues() const
Get values of constraints.
A class for handling words, derived from Foam::string.
Definition word.H:66
#define defineTypeNameAndDebug(Type, DebugSwitch)
Define the typeName and debug information.
Definition className.H:142
dynamicFvMesh & mesh
OBJstream os(runTime.globalPath()/outputName)
word timeName
Definition getTimeIndex.H:3
Namespace for OpenFOAM.
bool mkDir(const fileName &pathName, mode_t mode=0777)
Make a directory and return an error if it could not be created.
Definition POSIX.C:616
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
static constexpr const zero Zero
Global zero (0).
Definition zero.H:127
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition exprTraits.C:127
dictionary dict
#define forAll(list, i)
Loop across all elements in list.
Definition stdFoam.H:299