objectivePartialVolume.C
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) 2007-2019 PCOpt/NTUA
9  Copyright (C) 2013-2019 FOSS GP
10  Copyright (C) 2019 OpenCFD Ltd.
11 -------------------------------------------------------------------------------
12 License
13  This file is part of OpenFOAM.
14 
15  OpenFOAM is free software: you can redistribute it and/or modify it
16  under the terms of the GNU General Public License as published by
17  the Free Software Foundation, either version 3 of the License, or
18  (at your option) any later version.
19 
20  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
21  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
22  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
23  for more details.
24 
25  You should have received a copy of the GNU General Public License
26  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
27 
28 \*---------------------------------------------------------------------------*/
29 
30 #include "objectivePartialVolume.H"
31 #include "createZeroField.H"
32 #include "IOmanip.H"
34 
35 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
36 
37 namespace Foam
38 {
39 
40 namespace objectives
41 {
42 
43 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
44 
45 defineTypeNameAndDebug(objectivePartialVolume, 1);
47 (
48  objectiveIncompressible,
49  objectivePartialVolume,
50  dictionary
51 );
52 
53 
54 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
55 
57 (
58  const fvMesh& mesh,
59  const dictionary& dict,
60  const word& adjointSolverName,
61  const word& primalSolverName
62 )
63 :
64  objectiveIncompressible(mesh, dict, adjointSolverName, primalSolverName),
65  initVol_(Zero),
66  objectivePatches_
67  (
68  mesh_.boundaryMesh().patchSet
69  (
70  wordReList(dict.get<wordRes>("patches"))
71  )
72  )
73 {
74  // Read target volume if present. Else use the current one as a target
75  if (dict.found("initialVolume"))
76  {
77  initVol_ = dict.get<scalar>("initialVolume");
78  }
79  else
80  {
81  const scalar oneThird(1.0/3.0);
82  forAllConstIters(objectivePatches_, iter)
83  {
84  label patchI = iter.key();
85  const fvPatch& patch = mesh_.boundary()[patchI];
86  initVol_ -= oneThird*gSum(patch.Sf() & patch.Cf());
87  }
88  }
89  // Allocate boundary field pointers
90  bdxdbDirectMultPtr_.reset(createZeroBoundaryPtr<vector>(mesh_));
91  bdSdbMultPtr_.reset(createZeroBoundaryPtr<vector>(mesh_));
92 }
93 
94 
95 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
96 
97 
99 {
100  J_ = Zero;
101  const scalar oneThird(1.0/3.0);
102  forAllConstIters(objectivePatches_, iter)
103  {
104  label patchI = iter.key();
105  const fvPatch& patch = mesh_.boundary()[patchI];
106  J_ -= oneThird*gSum(patch.Sf() & patch.Cf());
107  }
108  J_ -= initVol_;
109  J_ /= initVol_;
110  return J_;
111 }
112 
113 
115 {
116  const scalar oneThird(1.0/3.0);
117  forAllConstIter(labelHashSet, objectivePatches_, iter)
118  {
119  label pI = iter.key();
120  const fvPatch& patch = mesh_.boundary()[pI];
121  tmp<vectorField> tnf = patch.nf();
122  const vectorField& nf = tnf();
123  bdxdbDirectMultPtr_()[pI] = -oneThird*nf/initVol_;
124  }
125 }
126 
127 
129 {
130  const scalar oneThird(1.0/3.0);
131  forAllConstIter(labelHashSet, objectivePatches_, iter)
132  {
133  label pI = iter.key();
134  const fvPatch& patch = mesh_.boundary()[pI];
135  bdSdbMultPtr_()[pI] = -oneThird*patch.Cf()/initVol_;
136  }
137 }
138 
139 
141 {
142  if (Pstream::master())
143  {
144  // File is opened only upon invocation of the write function
145  // in order to avoid various instantiations of the same objective
146  // opening the same file
147  unsigned int width = IOstream::defaultPrecision() + 6;
148  if (objFunctionFilePtr_.empty())
149  {
151  objFunctionFilePtr_() << setw(4) << "#" << " ";
152  objFunctionFilePtr_() << setw(width) << "(V - VInit)/VInit" << " ";
153  objFunctionFilePtr_() << setw(width) << "VInit" << endl;
154  }
155 
156  objFunctionFilePtr_() << setw(4) << mesh_.time().value() << " ";
157  objFunctionFilePtr_() << setw(width) << J_ << " ";
158  objFunctionFilePtr_() << setw(width) << initVol_ << endl;
159  }
160 }
161 
162 
163 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
164 
165 } // End namespace objectives
166 } // End namespace Foam
167 
168 // ************************************************************************* //
Foam::objectives::objectivePartialVolume::J
scalar J()
Return the objective function value.
Definition: objectivePartialVolume.C:98
Foam::objective::bdxdbDirectMultPtr_
autoPtr< boundaryVectorField > bdxdbDirectMultPtr_
Definition: objective.H:105
Foam::oneThird
static constexpr scalar oneThird
Definition: colourTools.C:42
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::tmp
A class for managing temporary objects.
Definition: PtrList.H:59
Foam::objectives::objectivePartialVolume::objectivePartialVolume
objectivePartialVolume(const fvMesh &mesh, const dictionary &dict, const word &adjointSolverName, const word &primalSolverName)
from components
Definition: objectivePartialVolume.C:57
Foam::Zero
static constexpr const zero Zero
Global zero.
Definition: zero.H:128
Foam::objectives::addToRunTimeSelectionTable
addToRunTimeSelectionTable(objectiveIncompressible, objectiveForce, dictionary)
objectivePartialVolume.H
Foam::wordReList
List< wordRe > wordReList
A List of wordRe (word or regular expression)
Definition: wordReList.H:52
Foam::objective::setObjectiveFilePtr
void setObjectiveFilePtr() const
Set the output file ptr.
Definition: objective.C:58
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:337
Foam::dimensioned::value
const Type & value() const
Return const reference to value.
Definition: dimensionedType.C:404
Foam::gSum
Type gSum(const FieldField< Field, Type > &f)
Definition: FieldFieldFunctions.C:594
Foam::HashSet< label, Hash< label > >
Foam::objective::objFunctionFilePtr_
autoPtr< OFstream > objFunctionFilePtr_
File to keep the objective values after the end of the primal solver.
Definition: objective.H:129
forAllConstIter
#define forAllConstIter(Container, container, iter)
Iterate across all elements in the container object.
Definition: stdFoam.H:338
Foam::objectives::defineTypeNameAndDebug
defineTypeNameAndDebug(objectiveForce, 0)
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:62
Foam::Field< vector >
Foam::objective::J_
scalar J_
Definition: objective.H:73
createZeroField.H
Foam::fvPatch
A finiteVolume patch using a polyPatch and a fvBoundaryMesh.
Definition: fvPatch.H:63
IOmanip.H
Istream and Ostream manipulators taking arguments.
Foam::objectiveIncompressible
Abstract base class for objective functions in incompressible flows.
Definition: objectiveIncompressible.H:54
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
Foam::objectives::objectivePartialVolume::update_dSdbMultiplier
void update_dSdbMultiplier()
Definition: objectivePartialVolume.C:128
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
addToRunTimeSelectionTable.H
Macros for easy insertion into run-time selection tables.
Foam::fvMesh
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:84
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::setw
Omanip< int > setw(const int i)
Definition: IOmanip.H:199
Foam::objective::bdSdbMultPtr_
autoPtr< boundaryVectorField > bdSdbMultPtr_
Term multiplying delta(n dS)/delta b.
Definition: objective.H:94
Foam::UPstream::master
static bool master(const label communicator=0)
Am I the master process.
Definition: UPstream.H:438
Foam::fvMesh::boundary
const fvBoundaryMesh & boundary() const
Return reference to boundary mesh.
Definition: fvMesh.C:589
Foam::objectives::objectivePartialVolume::update_dxdbDirectMultiplier
void update_dxdbDirectMultiplier()
Definition: objectivePartialVolume.C:114
forAllConstIters
forAllConstIters(mixture.phases(), phase)
Definition: pEqn.H:28
Foam::IOstream::defaultPrecision
static unsigned int defaultPrecision()
Return the default precision.
Definition: IOstream.H:325
Foam::foamVersion::patch
const std::string patch
OpenFOAM patch number as a std::string.
Foam::objective::mesh_
const fvMesh & mesh_
Definition: objective.H:64
Foam::wordRes
A List of wordRe with additional matching capabilities.
Definition: wordRes.H:51
Foam::fvMesh::time
const Time & time() const
Return the top-level database.
Definition: fvMesh.H:246
Foam::objectives::objectivePartialVolume::write
void write() const
Write objective function history.
Definition: objectivePartialVolume.C:140