49Foam::conjugateGradient::conjugateGradient
54 const label nConstraints,
60 dxOld_(readOrZeroField(
"dxOld", activeDesignVars_.size())),
61 sOld_(readOrZeroField(
"sOld", activeDesignVars_.size())),
62 betaType_(coeffsDict(
type).getOrDefault<
word>(
"betaType",
"FletcherReeves"))
69 && !(
betaType_ ==
"PolakRibiereRestarted")
73 <<
"Invalid betaType " <<
betaType_ <<
". Valid options are "
74 <<
"FletcherReeves, PolakRibiere, PolakRibiereRestarted"
87 Info<<
"Using steepest descent for the first iteration" <<
endl;
88 for (
const label varI : activeDesignVars_)
90 correction_[varI] = -eta_*objectiveDerivatives_[varI];
93 dxOld_.map(-objectiveDerivatives_, activeDesignVars_);
99 dx.map(-objectiveDerivatives_, activeDesignVars_);
102 if (betaType_ ==
"FletcherReeves")
104 beta = globalSum(dx*dx)/globalSum(dxOld_ * dxOld_);
106 else if (betaType_ ==
"PolakRibiere")
108 beta = globalSum(dx*(dx - dxOld_))/globalSum(dxOld_ * dxOld_);
110 else if (betaType_ ==
"PolakRibiereRestarted")
116 globalSum(dx*(dx - dxOld_))/globalSum(dxOld_ * dxOld_)
118 if (
beta == scalar(0))
120 Info<<
"Computed negative beta. Resetting to zero" <<
endl;
127 forAll(activeDesignVars_, varI)
129 correction_[activeDesignVars_[varI]] = eta_*
s[varI];
154 dxOld_.writeEntry(
"dxOld",
os);
155 sOld_.writeEntry(
"sOld",
os);
Macros for easy insertion into run-time selection tables.
#define addToRunTimeSelectionTable(baseType, thisType, argNames)
Add to construction table with typeName as the key.
label size() const noexcept
The number of elements in list.
void map(const UList< Type > &mapF, const labelUList &mapAddressing)
1 to 1 map from the given field
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
The Conjugate Gradient formula.
void computeCorrection()
Compute design variables correction.
virtual bool writeData(Ostream &os) const
Write useful quantities to files.
virtual void updateOldCorrection(const scalarField &oldCorrection)
Update old correction. For use when eta has been changed externally.
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
T getOrDefault(const word &keyword, const T &deflt, enum keyType::option matchOpt=keyType::REGEX) const
Find and return a T, or return the given default value. FatalIOError if it is found and the number of...
Mesh data needed to do the Finite Volume discretisation.
Abstract base class for optimisation methods.
scalarField objectiveDerivatives_
Derivatives of the objective functions.
scalarField correction_
Design variables correction.
dictionary coeffsDict(const word &type) const
Return optional dictionary with parameters specific to each method.
scalar globalSum(const scalarField &field)
Compute either global or local sum, based on globalSum flag.
virtual bool writeData(Ostream &os) const
Write continuation data under uniform.
tmp< scalarField > readOrZeroField(const word &name, const label size)
Helper function to either read a scalarField of certain size from a dictionary, or construct a zero f...
const labelList & activeDesignVars_
Map to active design variables.
label counter_
Optimisation cycle count.
label nConstraints() const
Get the number of constraints.
scalar eta_
Step multiplying the correction.
A class for handling words, derived from Foam::string.
#define defineTypeNameAndDebug(Type, DebugSwitch)
Define the typeName and debug information.
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
OBJstream os(runTime.globalPath()/outputName)
gmvFile<< "tracers "<< particles.size()<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().x()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().y()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
messageStream Info
Information stream (stdout output on master, null elsewhere).
Field< scalar > scalarField
Specialisation of Field<T> for scalar.
Ostream & endl(Ostream &os)
Add newline and flush stream.
static constexpr const zero Zero
Global zero (0).
error FatalError
Error stream (stdout output on all processes), with additional 'FOAM FATAL ERROR' header text and sta...
errorManipArg< error, int > exit(error &err, const int errNo=1)
constexpr char nl
The newline '\n' character (0x0a).
dimensionedScalar beta("beta", dimless/dimTemperature, laminarTransport)
#define forAll(list, i)
Loop across all elements in list.