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 -------------------------------------------------------------------------------
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 "SQPBase.H"
30 #include "IOmanip.H"
31 #include "updateMethod.H"
33 
34 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
35 
36 namespace Foam
37 {
39 }
40 
41 
42 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
43 
44 Foam::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 
109 bool Foam::SQPBase::addToFile(Ostream& os) const
110 {
111  forAll(constraintDerivativesOld_, cI)
112  {
113  constraintDerivativesOld_[cI].
114  writeEntry("constraintDerivativesOld" + Foam::name(cI), os);
115  }
116  lamdas_.writeEntry("lamdas", os);
117 
118  return true;
119 }
120 
121 
122 bool 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 // ************************************************************************* //
dictionary dict
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:116
type
Types of root.
Definition: Roots.H:52
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:129
Abstract base class for optimisation methods.
Definition: updateMethod.H:50
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
virtual bool addToFile(Ostream &os) const
Write continuation info.
Definition: SQPBase.C:102
static unsigned int defaultPrecision() noexcept
Return the default precision.
Definition: IOstream.H:423
scalarField LagrangianDerivatives_
Derivatives of the Lagrangian function.
Definition: SQPBase.H:61
Macros for easy insertion into run-time selection tables.
bool found(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Find an entry (const access) with the given keyword.
Definition: dictionaryI.H:104
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:421
word timeName
Definition: getTimeIndex.H:3
dynamicFvMesh & mesh
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:614
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
A class for handling words, derived from Foam::string.
Definition: word.H:63
Field< scalar > scalarField
Specialisation of Field<T> for scalar.
List< scalarField > constraintDerivativesOld_
The previous constraint derivatives.
Definition: SQPBase.H:66
Base class for Sequantial Quadratic Programming (SQP) methods.
Definition: SQPBase.H:50
Istream and Ostream manipulators taking arguments.
OBJstream os(runTime.globalPath()/outputName)
defineTypeNameAndDebug(combustionModel, 0)
virtual bool writeMeritFunction(const updateMethod &UpdateMethod)
Write info about the merit function.
Definition: SQPBase.C:115
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:78
static bool master(const label communicator=worldComm)
True if process corresponds to the master rank in the communicator.
Definition: UPstream.H:1082
scalarField lamdas_
Lagrange multipliers.
Definition: SQPBase.H:71
Pointer management similar to std::unique_ptr, with some additional methods and type checking...
Definition: HashPtrTable.H:48
Omanip< int > setw(const int i)
Definition: IOmanip.H:199
fileName objFunctionFolder_
Name of the objective folder.
Definition: SQPBase.H:76
bool found
Namespace for OpenFOAM.
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:127