objectivePartialVolume.C 4.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  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
10
    Copyright (C) 2019-2020 OpenCFD Ltd.
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
-------------------------------------------------------------------------------
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 "objectivePartialVolume.H"
#include "createZeroField.H"
#include "IOmanip.H"
#include "addToRunTimeSelectionTable.H"

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

namespace Foam
{

namespace objectives
{

// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //

defineTypeNameAndDebug(objectivePartialVolume, 1);
addToRunTimeSelectionTable
(
    objectiveIncompressible,
    objectivePartialVolume,
    dictionary
);


// * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //

objectivePartialVolume::objectivePartialVolume
(
    const fvMesh& mesh,
    const dictionary& dict,
    const word& adjointSolverName,
    const word& primalSolverName
)
:
    objectiveIncompressible(mesh, dict, adjointSolverName, primalSolverName),
    initVol_(Zero),
    objectivePatches_
    (
        mesh_.boundaryMesh().patchSet
        (
70
71
            dict.get<wordRes>("patches")
        ).sortedToc()
72
73
74
    )
{
    // Read target volume if present. Else use the current one as a target
75
    if (!dict.readIfPresent("initialVolume", initVol_))
76
77
    {
        const scalar oneThird(1.0/3.0);
78
        for (const label patchi : objectivePatches_)
79
        {
80
            const fvPatch& patch = mesh_.boundary()[patchi];
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
            initVol_ -= oneThird*gSum(patch.Sf() & patch.Cf());
        }
    }
    // Allocate boundary field pointers
    bdxdbDirectMultPtr_.reset(createZeroBoundaryPtr<vector>(mesh_));
    bdSdbMultPtr_.reset(createZeroBoundaryPtr<vector>(mesh_));
}


// * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //


scalar objectivePartialVolume::J()
{
    J_ = Zero;
    const scalar oneThird(1.0/3.0);
97
    for (const label patchi : objectivePatches_)
98
    {
99
        const fvPatch& patch = mesh_.boundary()[patchi];
100
101
102
103
104
105
106
107
108
109
110
        J_ -= oneThird*gSum(patch.Sf() & patch.Cf());
    }
    J_ -= initVol_;
    J_ /= initVol_;
    return J_;
}


void objectivePartialVolume::update_dxdbDirectMultiplier()
{
    const scalar oneThird(1.0/3.0);
111
    for (const label patchi : objectivePatches_)
112
    {
113
        const fvPatch& patch = mesh_.boundary()[patchi];
114
115
        tmp<vectorField> tnf = patch.nf();
        const vectorField& nf = tnf();
116
        bdxdbDirectMultPtr_()[patchi] = -oneThird*nf/initVol_;
117
118
119
120
121
122
123
    }
}


void objectivePartialVolume::update_dSdbMultiplier()
{
    const scalar oneThird(1.0/3.0);
124
    for (const label patchi : objectivePatches_)
125
    {
126
127
        const fvPatch& patch = mesh_.boundary()[patchi];
        bdSdbMultPtr_()[patchi] = -oneThird*patch.Cf()/initVol_;
128
129
130
131
    }
}


132
bool objectivePartialVolume::write(const bool valid) const
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
{
    if (Pstream::master())
    {
        // File is opened only upon invocation of the write function
        // in order to avoid various instantiations of the same objective
        // opening the same file
        unsigned int width = IOstream::defaultPrecision() + 6;
        if (objFunctionFilePtr_.empty())
        {
            setObjectiveFilePtr();
            objFunctionFilePtr_() << setw(4)     << "#"               << " ";
            objFunctionFilePtr_() << setw(width) << "(V - VInit)/VInit" << " ";
            objFunctionFilePtr_() << setw(width) << "VInit" << endl;
        }

        objFunctionFilePtr_() << setw(4)     << mesh_.time().value() << " ";
        objFunctionFilePtr_() << setw(width) << J_ << " ";
        objFunctionFilePtr_() << setw(width) << initVol_ << endl;
    }
152
153

    return true;
154
155
156
157
158
159
160
161
162
}


// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

} // End namespace objectives
} // End namespace Foam

// ************************************************************************* //