72 y.map(objectiveDerivatives_ - derivativesOld_, activeDesignVars_);
73 s.map(correctionOld_, activeDesignVars_);
75 scalar ys = globalSum(
s*
y);
77 if (counter_ == 1 && scaleFirstHessian_)
79 scalar scaleFactor = ys/globalSum(
y*
y);
80 Info<<
"Scaling Hessian with factor " << scaleFactor <<
endl;
81 forAll(activeDesignVars_, varI)
83 HessianOld_[varI][varI] /= scaleFactor;
87 scalar sBs = globalSum(leftMult(
s, HessianOld_)*
s);
94 <<
" y*s is below threshold. Using damped form" <<
endl;
95 theta = (scalar(1)-gamma_)*sBs/(sBs - ys);
99 <<
"Hessian curvature index " << ys <<
endl;
101 scalarField r(theta*
y + (scalar(1)-theta)*rightMult(HessianOld_,
s));
106 - outerProd(rightMult(HessianOld_,
s), leftMult(
s/sBs, HessianOld_))
107 + outerProd(r, r/globalSum(
s*r));
117 if (counter_ < nSteepestDescent_)
119 Info<<
"Using steepest descent to update design variables" <<
endl;
120 correction_ = -eta_*objectiveDerivatives_;
127 activeDerivs.
map(objectiveDerivatives_, activeDesignVars_);
130 -etaHessian_*rightMult(HessianInv, activeDerivs)
135 forAll(activeDesignVars_, varI)
137 correction_[activeDesignVars_[varI]] = activeCorrection[varI];
142 derivativesOld_ = objectiveDerivatives_;
143 correctionOld_ = correction_;
144 HessianOld_ = Hessian_;
150 if (optMethodIODict_.headerOk())
152 optMethodIODict_.readEntry(
"HessianOld", HessianOld_);
153 optMethodIODict_.readEntry(
"derivativesOld", derivativesOld_);
154 optMethodIODict_.readEntry(
"correctionOld", correctionOld_);
155 optMethodIODict_.readEntry(
"counter", counter_);
156 optMethodIODict_.readEntry(
"eta", eta_);
158 label
n = HessianOld_.n();
162 if (activeDesignVars_.empty())
183 coeffsDict().getOrDefault<scalar>(
"etaHessian", 1)
187 coeffsDict().getOrDefault<label>(
"nSteepestDescent", 1)
189 activeDesignVars_(0),
192 coeffsDict().getOrDefault<
bool>(
"scaleFirstHessian", false)
196 coeffsDict().getOrDefault<scalar>(
"curvatureThreshold", 1
e-10)
203 gamma_(coeffsDict().getOrDefault<scalar>(
"gamma", 0.2))
213 Info<<
"\t Did not find explicit definition of active design variables."
214 " Treating all available ones as active " <<
endl;
243 correctionOld_ = oldCorrection;
250 optMethodIODict_.add<
scalarField>(
"derivativesOld", derivativesOld_,
true);
251 optMethodIODict_.add<
scalarField>(
"correctionOld", correctionOld_,
true);
252 optMethodIODict_.add<label>(
"counter", counter_,
true);
Macros for easy insertion into run-time selection tables.
#define addToRunTimeSelectionTable(baseType, thisType, argNames)
Add to construction table with typeName as the key.
The quasi-Newton BFGS formula with the dampening proposed by Powell.
void computeCorrection()
Compute design variables correction.
SquareMatrix< scalar > HessianOld_
The previous Hessian.
void readFromDict()
Read old info from dict.
labelList activeDesignVars_
Map to active design variables.
void allocateMatrices()
Allocate matrices in the first optimisation cycle.
SquareMatrix< scalar > Hessian_
The Hessian. Should have the size of the active design variables.
virtual void write()
Write old info to dict.
virtual void updateOldCorrection(const scalarField &oldCorrection)
void update()
Update design variables.
void updateHessian()
Update approximation of the inverse Hessian.
void map(const UList< Type > &mapF, const labelUList &mapAddressing)
1 to 1 map from the given field
A templated (N x N) square matrix of objects of <Type>, containing N*N elements, derived from Matrix.
bool empty() const noexcept
True if the UList is empty (ie, size() is zero)
void size(const label n)
Older name for setAddressableSize.
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Mesh data needed to do the Finite Volume discretisation.
Abstract base class for optimisation methods.
scalarField objectiveDerivatives_
Derivatives of the objective functions.
dictionary coeffsDict()
Return optional dictionary with parameters specific to each method.
virtual void write()
Write useful quantities to files.
virtual void updateOldCorrection(const scalarField &oldCorrection)
#define defineTypeNameAndDebug(Type, DebugSwitch)
Define the typeName and debug information.
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))
#define DebugInfo
Report an information message using Foam::Info.
#define WarningInFunction
Report a warning using Foam::Warning.
labelList identity(const label len, label start=0)
Return an identity map of the given length with (map[i] == i)
messageStream Info
Information stream (stdout output on master, null elsewhere)
static const Identity< scalar > I
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)
dimensionedSphericalTensor inv(const dimensionedSphericalTensor &dt)
#define forAll(list, i)
Loop across all elements in list.
propsDict readIfPresent("fields", acceptFields)