Commit 95748b01 authored by Vaggelis Papoutsis's avatar Vaggelis Papoutsis Committed by Andrew Heather
Browse files

ENH: deprecation of fvOptionsAdjoint

fvOptionsAdjoint was needlessly duplicating a lot of the functionality
of fvOptions in order to add an interface for computing sensitivity
contributions emerging from fvOptions. To reduce this code duplication:

- fvOptionsAdjoint was removed
- the corresponding sensitivity contributions have moved to fvOptions through
  virtual functions (returning a zero contribution in the base so
  backwards compatibility is retained)
- all sensitivity classes that were using fvOptionsAdjoint have been
  modified appropriately
- all adjoint solvers are now grabbing a reference to an fvOptionList
  from the database instead of constructing an fvOptionsAdjointList

Hence, all fvOptions contributions to the adjoint equations
or the sensitivity derivatives can be given through system/fvOptions,
removing the need for separate sub-dictionaries within optimisationDict.
parent 7d83fb79
......@@ -7,6 +7,8 @@
-------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2019-2020 OpenCFD Ltd.
Copyright (C) 2020 PCOpt/NTUA
Copyright (C) 2020 FOSS GP
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -324,4 +326,31 @@ void Foam::fv::option::correct(volTensorField& field)
{}
void Foam::fv::option::postProcessSens
(
scalarField& sensField,
const word& fieldName,
const word& designVariablesName
)
{}
void Foam::fv::option::postProcessSens
(
vectorField& sensField,
const word& fieldName,
const word& designVariablesName
)
{}
void Foam::fv::option::postProcessSens
(
tensorField& sensField,
const word& fieldName,
const word& designVariablesName
)
{}
// ************************************************************************* //
......@@ -7,6 +7,8 @@
-------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2019-2020 OpenCFD Ltd.
Copyright (C) 2020 PCOpt/NTUA
Copyright (C) 2020 FOSS GP
------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -70,6 +72,7 @@ SourceFiles
#define fvOption_H
#include "fvMatricesFwd.H"
#include "primitiveFieldsFwd.H"
#include "volFieldsFwd.H"
#include "dictionary.H"
#include "Switch.H"
......@@ -407,6 +410,28 @@ public:
virtual void correct(volTensorField& field);
// Post process sensitivity field related to the fvOption
virtual void postProcessSens
(
scalarField& sensField,
const word& fieldName = word::null,
const word& designVariablesName = word::null
);
virtual void postProcessSens
(
vectorField& sensField,
const word& fieldName = word::null,
const word& designVariablesName = word::null
);
virtual void postProcessSens
(
tensorField& sensField,
const word& fieldName = word::null,
const word& designVariablesName = word::null
);
// IO
//- Write the source header information
......
......@@ -7,6 +7,8 @@
-------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2019 OpenCFD Ltd.
Copyright (C) 2020 PCOpt/NTUA
Copyright (C) 2020 FOSS GP
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -245,6 +247,16 @@ public:
void correct(GeometricField<Type, fvPatchField, volMesh>& field);
//- Post process sensitivity field related to the fvOption
template<class Type>
void postProcessSens
(
Field<Type>& sensField,
const word& fieldName = word::null,
const word& designVariablesName = word::null
);
// IO
//- Read dictionary
......
......@@ -7,6 +7,8 @@
-------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2016 OpenCFD Ltd.
Copyright (C) 2020 PCOpt/NTUA
Copyright (C) 2020 FOSS GP
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -351,4 +353,43 @@ void Foam::fv::optionList::correct
}
template<class Type>
void Foam::fv::optionList::postProcessSens
(
Field<Type>& sensField,
const word& fieldName,
const word& designVariablesName
)
{
forAll(*this, i)
{
option& source = this->operator[](i);
label fieldi = source.applyToField(fieldName);
if (fieldi != -1)
{
addProfiling(fvopt, "fvOption::postProcessSens." + source.name());
if (source.isActive())
{
if (debug)
{
Info<< "Post processing sensitivity from source "
<< source.name()
<< " for field " << fieldName << endl;
}
source.postProcessSens
(
sensField,
fieldName,
designVariablesName
);
}
}
}
}
// ************************************************************************* //
......@@ -42,11 +42,6 @@ ATCModel/ATCstandard/ATCstandard.C
ATCModel/ATCUaGradU/ATCUaGradU.C
ATCModel/cancelATC/cancelATC.C
/* ADJOINT FVOPTIONS */
fvOptionsAdjoint/fvOptions/fvOptionAdjoint.C
fvOptionsAdjoint/fvOptions/fvOptionAdjointList.C
fvOptionsAdjoint/fvOptions/fvIOoptionListAdjoint.C
/* OBJECTIVES */
objectives/objective/objective.C
objectives/incompressible/objectiveIncompressible/objectiveIncompressible.C
......
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2007-2019 PCOpt/NTUA
Copyright (C) 2013-2019 FOSS GP
Copyright (C) 2019 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
\*---------------------------------------------------------------------------*/
#include "fvIOoptionListAdjoint.H"
#include "fvMesh.H"
#include "Time.H"
// * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
Foam::IOobject Foam::fv::IOoptionListAdjoint::createIOobject
(
const fvMesh& mesh
) const
{
IOobject io
(
"fvOptionsAdjoint",
mesh.time().system(),
mesh,
IOobject::MUST_READ,
IOobject::NO_WRITE
);
if (io.typeHeaderOk<IOdictionary>(true))
{
Info<< "Creating fintite volume adjoint options from " << io.name()
<< nl << endl;
io.readOpt() = IOobject::MUST_READ_IF_MODIFIED;
return io;
}
else
{
Info<< "No finite volume adjoint options present" << nl << endl;
io.readOpt() = IOobject::NO_READ;
return io;
}
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::fv::IOoptionListAdjoint::IOoptionListAdjoint
(
const fvMesh& mesh
)
:
IOdictionary(createIOobject(mesh)),
optionList(mesh, *this)
{}
bool Foam::fv::IOoptionListAdjoint::read()
{
if (regIOobject::read())
{
optionList::read(*this);
return true;
}
else
{
return false;
}
}
// ************************************************************************* //
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2007-2019 PCOpt/NTUA
Copyright (C) 2013-2019 FOSS GP
Copyright (C) 2019 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Class
Foam::fv::IOoptionListAdjoint
Description
IOoptionListAdjoint
SourceFiles
IOoptionListAdjoint.C
\*---------------------------------------------------------------------------*/
#ifndef IOoptionListAdjoint_H
#define IOoptionListAdjoint_H
#include "fvOptionList.H"
#include "IOdictionary.H"
#include "autoPtr.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
namespace fv
{
/*---------------------------------------------------------------------------*\
Class IOoptionListAdjoint Declaration
\*---------------------------------------------------------------------------*/
class IOoptionListAdjoint
:
public IOdictionary,
public optionList
{
private:
// Private Member Functions
//- Create IO object if dictionary is present
IOobject createIOobject(const fvMesh& mesh) const;
//- No copy construct
IOoptionListAdjoint(const IOoptionListAdjoint&) = delete;
//- No copy assignment
void operator=(const IOoptionListAdjoint&) = delete;
public:
// Constructors
//- Construct from components with list of field names
IOoptionListAdjoint(const fvMesh& mesh);
//- Destructor
virtual ~IOoptionListAdjoint() = default;
// Member Functions
//- Read dictionary
virtual bool read();
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace fv
} // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2007-2019 PCOpt/NTUA
Copyright (C) 2013-2019 FOSS GP
Copyright (C) 2019 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
\*---------------------------------------------------------------------------*/
#include "fvOptionAdjoint.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
namespace Foam
{
namespace fv
{
defineTypeNameAndDebug(optionAdjoint, 0);
defineRunTimeSelectionTable(optionAdjoint, dictionary);
}
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::fv::optionAdjoint::optionAdjoint
(
const word& name,
const word& modelType,
const dictionary& dict,
const fvMesh& mesh
)
:
option(name, modelType, dict, mesh)
{}
// * * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * //
Foam::autoPtr<Foam::fv::optionAdjoint> Foam::fv::optionAdjoint::New
(
const word& name,
const dictionary& coeffs,
const fvMesh& mesh
)
{
const word modelType(coeffs.get<word>("type"));
Info<< indent
<< "Selecting finite volume options model type " << modelType << endl;
auto cstrIter = dictionaryConstructorTablePtr_->cfind(modelType);
if (!cstrIter.found())
{
FatalIOErrorInLookup
(
coeffs,
"model",
modelType,
*dictionaryConstructorTablePtr_
) << exit(FatalIOError);
}
return autoPtr<optionAdjoint>(cstrIter()(name, modelType, coeffs, mesh));
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
Foam::tmp<Foam::volVectorField> Foam::fv::optionAdjoint::dxdbMult
(
const incompressibleAdjointVars&
)
{
tmp<volVectorField> tdxdbMult
(
new volVectorField
(
IOobject
(
"fvOptionAdj::dxdbMult",
mesh_.time().timeName(),
mesh_,
IOobject::NO_READ,
IOobject::NO_WRITE
),
mesh_,
dimensionedVector(dimLength/pow3(dimTime), Zero)
)
);
return tdxdbMult;
}
// ************************************************************************* //
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2007-2019 PCOpt/NTUA
Copyright (C) 2013-2019 FOSS GP
Copyright (C) 2019 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Class
Foam::fv::optionAdjoint
Description
Similar to fv::option but with additional functionality to contribute to
the sensitivity deriavtives
SourceFiles
fvOptionAdjoint.C
\*---------------------------------------------------------------------------*/
#ifndef fvOptionAdjoint_H
#define fvOptionAdjoint_H
#include "fvOption.H"
#include "incompressibleAdjointVars.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
namespace fv
{
/*---------------------------------------------------------------------------*\
Class optionAdjoint Declaration
\*---------------------------------------------------------------------------*/
class optionAdjoint
:
public option
{
public:
//- Runtime type information
TypeName("optionAdjoint");
// Constructors
//- Construct from components
optionAdjoint
(
const word& name,
const word& modelType,
const dictionary& dict,
const fvMesh& mesh
);
//- Return clone
autoPtr<optionAdjoint> clone() const
{
NotImplemented;
return nullptr;
}
// Declare run-time constructor selection table
declareRunTimeSelectionTable