addSubtract.C 7.27 KB
Newer Older
1
2
3
4
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
Henry Weller's avatar
Henry Weller committed
5
    \\  /    A nd           | Copyright (C) 2011-2016 OpenFOAM Foundation
6
7
8
9
10
     \\/     M anipulation  |
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

11
12
13
14
    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.
15
16
17
18
19
20
21

    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
22
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
23
24
25

\*---------------------------------------------------------------------------*/

Andrew Heather's avatar
Andrew Heather committed
26
#include "addSubtract.H"
27
28
29
30
31
32
33
34
#include "addToRunTimeSelectionTable.H"

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

namespace Foam
{
    namespace calcTypes
    {
Andrew Heather's avatar
Andrew Heather committed
35
36
        defineTypeNameAndDebug(addSubtract, 0);
        addToRunTimeSelectionTable(calcType, addSubtract, dictionary);
37
38
39
40
41
42
    }
}


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

Andrew Heather's avatar
Andrew Heather committed
43
void Foam::calcTypes::addSubtract::writeAddSubtractFields
44
45
46
47
48
49
50
51
(
    const Time& runTime,
    const fvMesh& mesh,
    const IOobject& baseFieldHeader
)
{
    bool processed = false;

Andrew Heather's avatar
Andrew Heather committed
52
    IOobject addSubtractFieldHeader
53
    (
Andrew Heather's avatar
Andrew Heather committed
54
        addSubtractFieldName_,
55
56
57
58
59
        runTime.timeName(),
        mesh,
        IOobject::MUST_READ
    );

Andrew Heather's avatar
Andrew Heather committed
60
    if (addSubtractFieldHeader.headerOk())
61
    {
Andrew Heather's avatar
Andrew Heather committed
62
        writeAddSubtractField<scalar>
63
64
        (
            baseFieldHeader,
Andrew Heather's avatar
Andrew Heather committed
65
            addSubtractFieldHeader,
66
67
68
            mesh,
            processed
        );
Andrew Heather's avatar
Andrew Heather committed
69
        writeAddSubtractField<vector>
70
71
        (
            baseFieldHeader,
Andrew Heather's avatar
Andrew Heather committed
72
            addSubtractFieldHeader,
73
74
75
            mesh,
            processed
        );
Andrew Heather's avatar
Andrew Heather committed
76
        writeAddSubtractField<sphericalTensor>
77
78
        (
            baseFieldHeader,
Andrew Heather's avatar
Andrew Heather committed
79
            addSubtractFieldHeader,
80
81
82
            mesh,
            processed
        );
Andrew Heather's avatar
Andrew Heather committed
83
        writeAddSubtractField<symmTensor>
84
85
        (
            baseFieldHeader,
Andrew Heather's avatar
Andrew Heather committed
86
            addSubtractFieldHeader,
87
88
89
            mesh,
            processed
        );
Andrew Heather's avatar
Andrew Heather committed
90
        writeAddSubtractField<tensor>
91
92
        (
            baseFieldHeader,
Andrew Heather's avatar
Andrew Heather committed
93
            addSubtractFieldHeader,
94
95
96
97
98
99
100
101
            mesh,
            processed
        );

        if (!processed)
        {
            FatalError
                << "Unable to process " << baseFieldName_
Andrew Heather's avatar
Andrew Heather committed
102
103
                << " + " << addSubtractFieldName_ << nl
                << "No call to addSubtract for fields of type "
104
                << baseFieldHeader.headerClassName() << " + "
Andrew Heather's avatar
Andrew Heather committed
105
                << addSubtractFieldHeader.headerClassName() << nl << nl
106
107
108
109
110
                << exit(FatalError);
        }
    }
    else
    {
111
        FatalErrorInFunction
Andrew Heather's avatar
Andrew Heather committed
112
            << "Unable to read addSubtract field: " << addSubtractFieldName_
113
114
115
116
117
            << nl << exit(FatalError);
    }
}


Andrew Heather's avatar
Andrew Heather committed
118
void Foam::calcTypes::addSubtract::writeAddSubtractValues
119
120
121
122
123
124
125
126
(
    const Time& runTime,
    const fvMesh& mesh,
    const IOobject& baseFieldHeader
)
{
    bool processed = false;

Andrew Heather's avatar
Andrew Heather committed
127
    writeAddSubtractValue<scalar>
128
129
    (
        baseFieldHeader,
Andrew Heather's avatar
Andrew Heather committed
130
        addSubtractValueStr_,
131
132
133
        mesh,
        processed
    );
Andrew Heather's avatar
Andrew Heather committed
134
    writeAddSubtractValue<vector>
135
136
    (
        baseFieldHeader,
Andrew Heather's avatar
Andrew Heather committed
137
        addSubtractValueStr_,
138
139
140
        mesh,
        processed
    );
Andrew Heather's avatar
Andrew Heather committed
141
    writeAddSubtractValue<sphericalTensor>
142
143
    (
        baseFieldHeader,
Andrew Heather's avatar
Andrew Heather committed
144
        addSubtractValueStr_,
145
146
147
        mesh,
        processed
    );
Andrew Heather's avatar
Andrew Heather committed
148
    writeAddSubtractValue<symmTensor>
149
150
    (
        baseFieldHeader,
Andrew Heather's avatar
Andrew Heather committed
151
        addSubtractValueStr_,
152
153
154
        mesh,
        processed
    );
Andrew Heather's avatar
Andrew Heather committed
155
    writeAddSubtractValue<tensor>
156
157
    (
        baseFieldHeader,
Andrew Heather's avatar
Andrew Heather committed
158
        addSubtractValueStr_,
159
160
161
162
163
164
        mesh,
        processed
    );

    if (!processed)
    {
165
        FatalErrorInFunction
166
            << "Unable to process " << baseFieldName_
Andrew Heather's avatar
Andrew Heather committed
167
168
            << " + " << addSubtractValueStr_ << nl
            << "No call to addSubtract for fields of type "
169
170
171
172
173
174
175
176
            << baseFieldHeader.headerClassName() << nl << nl
            << exit(FatalError);
    }
}


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

Andrew Heather's avatar
Andrew Heather committed
177
Foam::calcTypes::addSubtract::addSubtract()
178
179
180
181
:
    calcType(),
    baseFieldName_(""),
    calcType_(FIELD),
Andrew Heather's avatar
Andrew Heather committed
182
183
184
185
    addSubtractFieldName_(""),
    addSubtractValueStr_(""),
    resultName_(""),
    calcMode_(ADD)
186
187
188
189
190
{}


// * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //

Andrew Heather's avatar
Andrew Heather committed
191
Foam::calcTypes::addSubtract::~addSubtract()
192
193
194
195
196
{}


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

Andrew Heather's avatar
Andrew Heather committed
197
void Foam::calcTypes::addSubtract::init()
198
199
200
{
    argList::validArgs.append("add");
    argList::validArgs.append("baseField");
Andrew Heather's avatar
Andrew Heather committed
201
    argList::validArgs.append("calcMode");
202
    argList::validOptions.insert("field", "field");
203
    argList::validOptions.insert("value", "valueString");
204
    argList::validOptions.insert("result", "field");
205
206
207
}


Andrew Heather's avatar
Andrew Heather committed
208
void Foam::calcTypes::addSubtract::preCalc
209
210
211
212
213
214
(
    const argList& args,
    const Time& runTime,
    const fvMesh& mesh
)
{
215
216
    baseFieldName_ = args[2];
    const word calcModeName = args[3];
Andrew Heather's avatar
Andrew Heather committed
217
218
219
220
221
222
223
224
225
226
227

    if (calcModeName == "add")
    {
        calcMode_ = ADD;
    }
    else if (calcModeName == "subtract")
    {
        calcMode_ = SUBTRACT;
    }
    else
    {
228
        FatalErrorInFunction
Andrew Heather's avatar
Andrew Heather committed
229
230
231
232
            << "Invalid calcMode: " << calcModeName << nl
            << "    Valid calcModes are add and subtract" << nl
            << exit(FatalError);
    }
233

234
    if (args.optionReadIfPresent("field", addSubtractFieldName_))
235
236
237
    {
        calcType_ = FIELD;
    }
238
    else if (args.optionReadIfPresent("value", addSubtractValueStr_))
239
240
241
242
243
    {
        calcType_ = VALUE;
    }
    else
    {
244
        FatalErrorInFunction
Andrew Heather's avatar
Andrew Heather committed
245
            << "addSubtract requires either -field or -value option"
246
247
248
            << nl << exit(FatalError);
    }

249
    args.optionReadIfPresent("resultName", resultName_);
250
251
252
}


Andrew Heather's avatar
Andrew Heather committed
253
void Foam::calcTypes::addSubtract::calc
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
(
    const argList& args,
    const Time& runTime,
    const fvMesh& mesh
)
{
    IOobject baseFieldHeader
    (
        baseFieldName_,
        runTime.timeName(),
        mesh,
        IOobject::MUST_READ
    );

    if (baseFieldHeader.headerOk())
    {
        switch (calcType_)
        {
            case FIELD:
            {
Andrew Heather's avatar
Andrew Heather committed
274
                writeAddSubtractFields(runTime, mesh, baseFieldHeader);
275
276
277
278
                break;
            }
            case VALUE:
            {
Andrew Heather's avatar
Andrew Heather committed
279
                writeAddSubtractValues(runTime, mesh, baseFieldHeader);
280
281
282
283
                break;
            }
            default:
            {
284
                FatalErrorInFunction
285
286
287
288
289
290
291
                    << "unknown calcType " << calcType_ << nl
                    << abort(FatalError);
            }
        }
    }
    else
    {
292
        FatalErrorInFunction
293
294
295
296
297
298
299
            << "Unable to read base field: " << baseFieldName_
            << nl << exit(FatalError);
    }
}


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