nullSpace.H
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) 2023 PCOpt/NTUA
9  Copyright (C) 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 Class
29  Foam::nullSpace
30 
31 Description
32  Update design variables using a null space approach.
33  Can handle inequality and bound constraints.
34 
35  Reference:
36  \verbatim
37  Feppon, F., Allaire, G., & Dapogny, C. (2020).
38  Null space gradient flows for constrained optimization with
39  applications to shape optimization.
40  ESAIM: COCV, 26, 90.
41  https://doi.org/10.1051/cocv/2020015
42  \endverbatim
43 
44 SourceFiles
45  nullSpace.C
46 
47 \*---------------------------------------------------------------------------*/
48 
49 #ifndef nullSpace_H
50 #define nullSpace_H
51 
53 #include "updateMethod.H"
54 
55 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
56 
57 namespace Foam
58 {
59 
60 /*---------------------------------------------------------------------------*\
61  Class nullSpace Declaration
62 \*---------------------------------------------------------------------------*/
63 
64 class nullSpace
65 :
67  public updateMethod
68 {
69 protected:
70 
71  // Protected data
72 
73  //- Are bound constraints included?
75 
76  //- Lagrange multipliers for flow-reated constraints
78 
79  //- Lagrange multipliers for the lower bound constraints
81 
82  //- Lagrange multipliers for the upper bound constraints
84 
85 
86  // Dual variables
87 
88  //- Solve the dual problem?
89  bool solveDualProblem_;
90 
91  //- Lagrange multipliers of the dual problem for flow-related
92  //- constraints
94 
95  //- Lagrange multipliers of the dual problem for the lower bound
96  //- constraints
98 
99  //- Lagrange multipliers of the dual problem for the upper bound
100  //- constraints
102 
103 
104  // Constraint subsets
105 
106  //- List of saturated or violated constraints
107  // List[0]: flow related constraints
108  // List[1]: lower bound constraints
109  // List[2]: upper bound constraints
110  // Sub-list meaning is the same for all labelListLists that follow
113  //- List of saturated or violated constraints (up to epsConstr_)
115 
116  //- List of constraints that must remain active
117  // Determines the null space update
119 
120  //- List of constraints the values of which need to be reduced
121  // Determines the range space update
123 
124 
125  // Fields holding the updates of the Lagrange multipliers of the primal
126  // and dual problems
127 
134 
135 
136  //- Infinitesimal quantity
137  // Updated during the inner iterations of the dual problem
138  scalar eps_;
139 
140  //- Maxmimum number of Newton iterations for the dual problem
141  label maxNewtonIters_;
142 
143  //- Maxmimum number of line search iterations for each Newton iteration
144  //- of the dual problem
145  label maxLineSearchIters_;
146 
147  //- Maxmimum number of CG iterations for obtaining the null space and
148  //- range space updates
149  label maxCGIters_;
151  //- Tolerance of the dual problem
155  // Constant parameters
156 
157  //- Value for considering a constraint as marginally active
158  // Used to avoid the frequent change of the active set of
159  // constraints
160  scalar epsConstr_;
161 
162  //- Value to perturb the design variables by, if all of them
163  //- lay on their bounds at the beginning of the optimisation
164  scalar epsPerturb_;
165 
166  //- Multiplier of the null space update
167  scalar aJ_;
169  //- Multiplier of the range space update
170  scalar aC_;
171 
172  //- Change aJ and aC adaptively
173  bool adaptiveStep_;
175  //- Last cycle on which to reset aJ
176  label lastAcceleratedCycle_;
177 
178  //- Max change of the design variables, pertaining to the objective
179  //- reduction.
180  // If adaptiveStep is set to true, aJ will be set in such a way
181  // to obtain a maxDVChange_ for the design variables, for all
182  // optimisation cycles up to lastAcceleratedCycle_
184 
185  //- Whether to impose maxDVChange strictly on all optimisation
186  //- cycles or just up to the lastAcceleratedCycle
187  bool strictMaxDVChange_;
188 
189  //- Target reduction of active constraints (range in [0-1])
190  // 1: active constraints will become zero (first-order approx)
191  // 0: no change in the constraints
193 
194  //- Downplay the importance of the bound constraint reduction
195  //- by this ammount
196  scalar bcMult_;
197 
198 
199  // Protected Member Functions
200 
201  //- Update sizes of fields related to the constraints
202  void initialise();
203 
204  //- Zero the updates computed in the previous optimisation cycle
205  void zeroUpdates();
206 
208  // Functions related to the solution of the dual subproblem
209 
210  //- Solve the dual problem for computing the Lagrange multiplier
211  //- using a Newton solver
213 
214  //- Compute and return residuals based on the current solution
216 
217  //- Compute direction for the Newton problem
218  void computeNewtonDirection();
219 
220  //- Perform line search and return max residual corresponding to
221  //- the updated solution
222  scalar lineSearch();
223 
224  //- Update the current solution using the known direction and the
225  //- given step length
226  void updateSolution(const scalar step);
227 
228  //- Adjust step to satisfy cireteria
229  void adjustStep
230  (
231  scalar& step,
232  const scalar value,
233  const scalar update
234  );
235 
236 
237  // Functions related to the computation of the design variables update
239  //- Update the constraint subsets
241 
242  //- Update the constraint subsets
244 
245  //- Update violated constraints indices (iTilda and iTildaEps)
247  (
248  const label i,
249  const scalarField& constraints
250  );
251 
252  //- Update constraint indices related to the null and range space
253  //- part of the correction
255  (
256  const label i,
257  const scalarField& LagrangeMults,
258  const scalarField& dual
259  );
260 
261  //- Compute the update of the design variables as a combination of
262  //- null space and range space parts
263  void correction();
264 
265  //- A & v
266  // where A is the Jacobian of all constraints, identified by
267  // the labelList addressings, w.r.t. the active design variables
269  (
270  const scalarField& v,
271  const labelListList& subsets
272  );
273 
274  //- A.T & v
275  // where A is the Jacobian of all constraints, identified by
276  // the labelList addressings, w.r.t. the active design variables
278  (
279  const scalarField& v,
280  const labelListList& subsets
281  );
282 
283  //- Collect all constraint values corresponding to the provided
284  //- indices
286  (
287  const labelListList& subsets
288  );
289 
290  //- Computes the parts of ksiJ and ksiC that require a system
291  //- solution
292  // Formulated in terms of the flow constraints, which are usually
293  // few
295  (
296  const scalarField& rhs,
297  const labelListList& subsets
298  );
299 
300  //- Print statistics on the number of flow- and bound-related
301  //- constraints included in the subset
302  void statistics
303  (
304  const labelListList& subset,
305  const word& description
306  );
307 
308 
309 private:
310 
311  // Private Member Functions
312 
313  //- No copy construct
314  nullSpace(const nullSpace&) = delete;
315 
316  //- No copy assignment
317  void operator=(const nullSpace&) = delete;
318 
319 
320 public:
321 
322  //- Runtime type information
323  TypeName("nullSpace");
324 
325 
326  // Constructors
327 
328  //- Construct from components
329  nullSpace
330  (
331  const fvMesh& mesh,
332  const dictionary& dict,
333  autoPtr<designVariables>& designVars,
334  const label nConstraints,
335  const word& type
336  );
337 
338 
339  //- Destructor
340  virtual ~nullSpace() = default;
341 
342 
343  // Member Functions
344 
345  //- Compute design variables correction
346  virtual void computeCorrection();
347 
348  //- Compute the merit function for line search
349  virtual scalar computeMeritFunction();
350 
351  //- Directional derivative of the merit function, in the direction of
352  //- the correction
353  virtual scalar meritFunctionDirectionalDerivative();
354 
355  //- Write useful quantities to files
356  virtual bool writeData(Ostream& os) const;
357 };
358 
359 
360 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
361 
362 } // End namespace Foam
363 
364 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
365 
366 #endif
367 
368 // ************************************************************************* //
scalarField deltaL_
Definition: nullSpace.H:151
void updateViolatedConstraintsSubsets()
Update the constraint subsets.
Definition: nullSpace.C:432
scalar lineSearch()
Perform line search and return max residual corresponding to the updated solution.
Definition: nullSpace.C:321
tmp< scalarField > activeConstraints(const labelListList &subsets)
Collect all constraint values corresponding to the provided indices.
Definition: nullSpace.C:732
dictionary dict
scalar targetConstraintReduction_
Target reduction of active constraints (range in [0-1])
Definition: nullSpace.H:246
TypeName("nullSpace")
Runtime type information.
scalar dualTolerance_
Tolerance of the dual problem.
Definition: nullSpace.H:185
tmp< scalarField > constraintRelatedUpdate(const scalarField &rhs, const labelListList &subsets)
Computes the parts of ksiJ and ksiC that require a system solution.
Definition: nullSpace.C:770
scalarField deltaDualMu_
Definition: nullSpace.H:153
void correction()
Compute the update of the design variables as a combination of null space and range space parts...
Definition: nullSpace.C:544
void initialise()
Update sizes of fields related to the constraints.
Definition: nullSpace.C:47
Abstract base class for optimisation methods supporting constraints. Does not add functionality to up...
label nConstraints() const
Get the number of constraints.
Definition: updateMethod.C:393
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
void computeNewtonDirection()
Compute direction for the Newton problem.
Definition: nullSpace.C:209
label maxLineSearchIters_
Maxmimum number of line search iterations for each Newton iteration of the dual problem.
Definition: nullSpace.H:174
label maxNewtonIters_
Maxmimum number of Newton iterations for the dual problem.
Definition: nullSpace.H:168
autoPtr< scalar > maxDVChange_
Max change of the design variables, pertaining to the objective reduction.
Definition: nullSpace.H:232
scalar eps_
Infinitesimal quantity.
Definition: nullSpace.H:163
Abstract base class for optimisation methods.
Definition: updateMethod.H:50
scalarField deltaDualL_
Definition: nullSpace.H:154
virtual ~nullSpace()=default
Destructor.
bool includeBoundConstraints_
Are bound constraints included?
Definition: nullSpace.H:71
void updateNullAndRangeSpaceSubsets()
Update the constraint subsets.
Definition: nullSpace.C:452
scalarField mu_
Lagrange multipliers for flow-reated constraints.
Definition: nullSpace.H:76
scalarField deltaMu_
Definition: nullSpace.H:150
Update design variables using a null space approach. Can handle inequality and bound constraints...
Definition: nullSpace.H:59
tmp< scalarField > Av(const scalarField &v, const labelListList &subsets)
A & v.
Definition: nullSpace.C:644
scalarField l_
Lagrange multipliers for the lower bound constraints.
Definition: nullSpace.H:81
scalarField u_
Lagrange multipliers for the upper bound constraints.
Definition: nullSpace.H:86
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: POSIX.C:799
dynamicFvMesh & mesh
bool solveDualProblem_
Solve the dual problem?
Definition: nullSpace.H:94
virtual void computeCorrection()
Compute design variables correction.
Definition: nullSpace.C:1065
A class for handling words, derived from Foam::string.
Definition: word.H:63
labelListList iTilda_
List of saturated or violated constraints.
Definition: nullSpace.H:125
void zeroUpdates()
Zero the updates computed in the previous optimisation cycle.
Definition: nullSpace.C:72
void updateSolution(const scalar step)
Update the current solution using the known direction and the given step length.
Definition: nullSpace.C:419
scalar epsPerturb_
Value to perturb the design variables by, if all of them lay on their bounds at the beginning of the ...
Definition: nullSpace.H:202
scalar epsConstr_
Value for considering a constraint as marginally active.
Definition: nullSpace.H:196
scalar aC_
Multiplier of the range space update.
Definition: nullSpace.H:212
label maxCGIters_
Maxmimum number of CG iterations for obtaining the null space and range space updates.
Definition: nullSpace.H:180
void updateCorrectionIndices(const label i, const scalarField &LagrangeMults, const scalarField &dual)
Update constraint indices related to the null and range space part of the correction.
Definition: nullSpace.C:504
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
scalar bcMult_
Downplay the importance of the bound constraint reduction by this ammount.
Definition: nullSpace.H:252
OBJstream os(runTime.globalPath()/outputName)
mesh update()
virtual scalar meritFunctionDirectionalDerivative()
Directional derivative of the merit function, in the direction of the correction. ...
Definition: nullSpace.C:1134
scalar aJ_
Multiplier of the null space update.
Definition: nullSpace.H:207
List< T > subset(const BoolListType &select, const UList< T > &input, const bool invert=false)
Extract elements of the input list when select is true.
tmp< scalarField > ATv(const scalarField &v, const labelListList &subsets)
A.T & v.
Definition: nullSpace.C:688
scalarField deltaU_
Definition: nullSpace.H:152
scalarField deltaDualU_
Definition: nullSpace.H:155
scalarField dualU_
Lagrange multipliers of the dual problem for the upper bound constraints.
Definition: nullSpace.H:112
tmp< scalarField > computeResiduals()
Compute and return residuals based on the current solution.
Definition: nullSpace.C:132
void solveDualProblem()
Solve the dual problem for computing the Lagrange multiplier using a Newton solver.
Definition: nullSpace.C:83
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:78
bool adaptiveStep_
Change aJ and aC adaptively.
Definition: nullSpace.H:217
labelListList iHat_
List of constraints that must remain active.
Definition: nullSpace.H:137
label lastAcceleratedCycle_
Last cycle on which to reset aJ.
Definition: nullSpace.H:222
virtual bool writeData(Ostream &os) const
Write useful quantities to files.
Definition: nullSpace.C:1157
scalarField dualMu_
Lagrange multipliers of the dual problem for flow-related constraints.
Definition: nullSpace.H:100
void updateViolatedIndices(const label i, const scalarField &constraints)
Update violated constraints indices (iTilda and iTildaEps)
Definition: nullSpace.C:472
void adjustStep(scalar &step, const scalar value, const scalar update)
Adjust step to satisfy cireteria.
Definition: nullSpace.C:406
bool strictMaxDVChange_
Whether to impose maxDVChange strictly on all optimisation cycles or just up to the lastAcceleratedCy...
Definition: nullSpace.H:238
labelListList iRangeSpace_
List of constraints the values of which need to be reduced.
Definition: nullSpace.H:144
A class for managing temporary objects.
Definition: HashPtrTable.H:50
virtual scalar computeMeritFunction()
Compute the merit function for line search.
Definition: nullSpace.C:1085
Namespace for OpenFOAM.
scalarField dualL_
Lagrange multipliers of the dual problem for the lower bound constraints.
Definition: nullSpace.H:106
void statistics(const labelListList &subset, const word &description)
Print statistics on the number of flow- and bound-related constraints included in the subset...
Definition: nullSpace.C:894
labelListList iTildaEps_
List of saturated or violated constraints (up to epsConstr_)
Definition: nullSpace.H:130