43 Foam::adjointSolverManager::adjointSolverManager
47 const word& managerType,
49 bool overrideUseSolverName
67 managerType_(managerType),
70 objectiveSolverIDs_(0),
71 oneSidedConstraintSolverIDs_(0),
72 doubleSidedConstraintSolverIDs_(0),
75 dict.getOrDefault<scalar>(
"operatingPointWeight", 1)
77 nActiveAdjointSolvers_(0),
78 designVars_(designVars)
83 const wordList adjSolverNames = adjointSolversDict.
toc();
91 forAll(adjSolverNames, namei)
94 adjointSolversDict.
subDict(adjSolverNames[namei]);
95 if (overrideUseSolverName)
97 solverDict.
add<
bool>(
"useSolverNameForFields",
true);
108 adjSolverNames[namei]
133 <<
" adjoint solvers acting as single-sided constraints" <<
endl;
136 <<
" adjoint solvers acting as double-sided constraints" <<
endl;
139 <<
" active adjoint solvers" <<
endl;
146 <<
"Number of adjoint solvers corresponding to objectives " 148 <<
"Consider aggregating your objectives to one\n" <<
endl;
162 for (adjointSolver& solver : adjointSolvers_)
179 return primalSolverName_;
192 return adjointSolvers_;
199 return adjointSolvers_;
206 forAll(adjointSolvers_, sI)
208 names[sI] = adjointSolvers_[sI].name();
216 return operatingPointWeight_;
222 return nActiveAdjointSolvers_;
232 const wordList adjSolverNames = adjointSolversDict.
toc();
235 forAll(adjSolverNames, namei)
237 active = adjointSolversDict.
subDict(adjSolverNames[namei]).
238 getOrDefault<bool>(
"active",
true);
250 return nOneSidedConstraints() + 2*nDoubleSidedConstraints();
256 return oneSidedConstraintSolverIDs_.size();
262 return doubleSidedConstraintSolverIDs_.size();
268 return objectiveSolverIDs_.size();
274 return nOneSidedConstraints() + nDoubleSidedConstraints() + nObjectives();
284 solver.updatePrimalBasedQuantities();
292 solver.computeObjectiveSensitivities(designVars_);
293 if (mesh_.time().writeTime())
305 auto& sens = tsens.ref();
308 for (
const label solveri : objectiveSolverIDs_)
312 adjointSolvers_[solveri].getObjectiveSensitivities(designVars_);
328 PtrList<scalarField> constraintSens(nConstraints());
331 for (
const label consI : oneSidedConstraintSolverIDs_)
337 (adjointSolvers_[consI].getObjectiveSensitivities(designVars_))
342 for (
const label consI : doubleSidedConstraintSolverIDs_)
345 (adjointSolvers_[consI].getObjectiveSensitivities(designVars_));
350 return constraintSens;
358 adjSolver.computeObjectiveSensitivities(designVars_);
367 adjSolver.clearSensitivities();
374 scalar objValue(
Zero);
375 for (
const label solveri : objectiveSolverIDs_)
377 objectiveManager& objManager =
378 adjointSolvers_[solveri].getObjectiveManager();
379 objValue += objManager.print();
389 scalarField& constraintValues = tconstraintValues.ref();
392 for (
const label consI : oneSidedConstraintSolverIDs_)
394 objectiveManager& objManager =
395 adjointSolvers_[consI].getObjectiveManager();
396 constraintValues[cI++] = objManager.print();
400 for (
const label consI : doubleSidedConstraintSolverIDs_)
402 objectiveManager& objManager =
403 adjointSolvers_[consI].getObjectiveManager();
404 constraintValues[cI++] = objManager.print(
false);
405 constraintValues[cI++] = objManager.print(
true);
408 return tconstraintValues;
414 if (primalSolverName_ ==
name)
416 for (adjointSolver& solver : adjointSolvers_)
418 solver.updatePrimalBasedQuantities();
426 return mesh_.lookupObject<primalSolver>(primalSolverName_).isMaster();
List< ReturnType > get(const UPtrList< T > &list, const AccessOp &aop)
List of values generated by applying the access operation to each list item.
label nOneSidedConstraints() const
Number of adjoint solvers corresponding to one-sided constraints.
void updatePrimalBasedQuantities(const word &name)
Update fields related to primal solution.
scalar objectiveValue()
Get objective value.
void size(const label n)
Older name for setAddressableSize.
labelList doubleSidedConstraintSolverIDs_
PtrList< scalarField > constraintSensitivities()
Get constraint sensitivities. One scalarField per constraint.
tmp< scalarField > constraintValues()
Get constraint values.
List< word > names(const UPtrList< T > &list, const UnaryMatchPredicate &matcher)
List of names generated by calling name() for each list item and filtered for matches.
label nObjectives() const
Number of adjoint solvers corresponding to objectives.
A list of keyword definitions, which are a keyword followed by a number of values (eg...
label nAdjointSolvers() const
Total number of adjoint solvers.
constexpr char nl
The newline '\n' character (0x0a)
scalar operatingPointWeight() const
Const access to adjoint solvers.
const word dictName("faMeshDefinition")
bool isMaster() const
Whether the primal solver corresponding to the adjointSolverManager is the master one...
const word primalSolverName_
Ostream & endl(Ostream &os)
Add newline and flush stream.
void clearSensitivities()
Clear sensitivity fields from all adjoint solvers.
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.
tmp< scalarField > aggregateSensitivities()
Aggregate sensitivities from various adjoint solvers.
Base class for adjoint solvers.
label nActiveAdjointSolvers_
entry * add(entry *entryPtr, bool mergeEntry=false)
Add a new entry.
const word & managerName() const
Const access to the manager name.
const dictionary & subDict(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Find and return a sub-dictionary.
labelList oneSidedConstraintSolverIDs_
wordList toc() const
Return the table of contents.
wordList adjointSolversNames() const
Return the names of all adjointSolvers.
PtrList< adjointSolver > adjointSolvers_
#define forAll(list, i)
Loop across all elements in list.
label nConstraints() const
Number of constraints.
void computeAllSensitivities()
Compute sensitivities for all adjoint solvers (both objective- and constraint-related ones) ...
static autoPtr< adjointSolver > New(fvMesh &mesh, const word &managerType, const dictionary &dict, const word &primalSolverName, const word &solverName)
Return a reference to the selected turbulence model.
void setSize(const label n)
Alias for resize()
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
A class for handling words, derived from Foam::string.
Field< scalar > scalarField
Specialisation of Field<T> for scalar.
label nDoubleSidedConstraints() const
Number of adjoint solvers corresponding to double-sided constraints.
const PtrList< adjointSolver > & adjointSolvers() const
Const access to adjoint solvers.
static tmp< T > New(Args &&... args)
Construct tmp with forwarding arguments.
label nActiveAdjointSolvers() const
Return number of active adjoint solvers, either corresponding.
const word & name() const
Name function is needed to disambiguate those inherited from regIOobject and dictionary.
const dictionary & dict() const
Const access to the construction dictionary.
virtual bool readDict(const dictionary &dict)
virtual void solve()=0
Main control loop.
defineTypeNameAndDebug(combustionModel, 0)
void solveAdjointEquations()
Update objective function-related values and solve adjoint equations.
Class for managing adjoint solvers, which may be more than one per operating point.
#define WarningInFunction
Report a warning using Foam::Warning.
bool readDict(const dictionary &dict)
Mesh data needed to do the Finite Volume discretisation.
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
messageStream Info
Information stream (stdout output on master, null elsewhere)
Pointer management similar to std::unique_ptr, with some additional methods and type checking...
int system(const std::string &command, const bool bg=false)
Execute the specified command via the shell.
A class for managing temporary objects.
const word & primalSolverName() const
Const access to the primal solver name.
Defines the attributes of an object for which implicit objectRegistry management is supported...
labelList objectiveSolverIDs_
static constexpr const zero Zero
Global zero (0)