writeObjects.C 5.26 KB
Newer Older
Andrew Heather's avatar
Andrew Heather committed
1
2
3
4
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
5
    \\  /    A nd           | Copyright (C) 2011-2016 OpenFOAM Foundation
6
     \\/     M anipulation  | Copyright (C) 2016-2019 OpenCFD Ltd.
Andrew Heather's avatar
Andrew Heather committed
7
8
9
10
-------------------------------------------------------------------------------
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.
Andrew Heather's avatar
Andrew Heather committed
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/>.
Andrew Heather's avatar
Andrew Heather committed
23
24
25

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

26
#include "writeObjects.H"
Andrew Heather's avatar
Andrew Heather committed
27
#include "Time.H"
28
#include "polyMesh.H"
29
#include "ListOps.H"
30
#include "addToRunTimeSelectionTable.H"
Andrew Heather's avatar
Andrew Heather committed
31
32
33

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

34
35
namespace Foam
{
36
37
namespace functionObjects
{
38
    defineTypeNameAndDebug(writeObjects, 0);
39
40
41
42

    addToRunTimeSelectionTable
    (
        functionObject,
43
        writeObjects,
44
45
        dictionary
    );
46
}
47
}
Andrew Heather's avatar
Andrew Heather committed
48

49
const Foam::Enum
50
<
51
52
53
    Foam::functionObjects::writeObjects::writeOption
>
Foam::functionObjects::writeObjects::writeOptionNames_
Mark OLESEN's avatar
Mark OLESEN committed
54
({
55
56
57
    { writeOption::AUTO_WRITE, "autoWrite" },
    { writeOption::NO_WRITE, "noWrite" },
    { writeOption::ANY_WRITE, "anyWrite" },
Mark OLESEN's avatar
Mark OLESEN committed
58
});
59

Andrew Heather's avatar
Andrew Heather committed
60
61
62

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

63
Foam::functionObjects::writeObjects::writeObjects
Andrew Heather's avatar
Andrew Heather committed
64
65
(
    const word& name,
66
67
    const Time& runTime,
    const dictionary& dict
Andrew Heather's avatar
Andrew Heather committed
68
69
)
:
70
71
72
73
74
75
76
77
    functionObject(name),
    obr_
    (
        runTime.lookupObject<objectRegistry>
        (
            dict.lookupOrDefault("region", polyMesh::defaultRegion)
        )
    ),
78
    writeOption_(ANY_WRITE),
Andrew Heather's avatar
Andrew Heather committed
79
80
81
82
83
84
85
86
    objectNames_()
{
    read(dict);
}


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

87
bool Foam::functionObjects::writeObjects::read(const dictionary& dict)
Andrew Heather's avatar
Andrew Heather committed
88
{
89
90
    functionObject::read(dict);

91
92
    if (dict.found("field"))
    {
Mark OLESEN's avatar
Mark OLESEN committed
93
94
        objectNames_.resize(1);
        dict.readEntry("field", objectNames_.first());
95
96
97
    }
    else if (dict.found("fields"))
    {
98
        dict.readEntry("fields", objectNames_);
99
100
101
    }
    else
    {
102
        dict.readEntry("objects", objectNames_);
103
104
    }

105
106
107
108
109
110
    writeOption_ = writeOptionNames_.lookupOrDefault
    (
        "writeOption",
        dict,
        writeOption::ANY_WRITE
    );
Andrew Heather's avatar
Andrew Heather committed
111

112
113
    return true;
}
Andrew Heather's avatar
Andrew Heather committed
114
115


116
bool Foam::functionObjects::writeObjects::execute()
117
118
119
{
    return true;
}
120
121


122
bool Foam::functionObjects::writeObjects::write()
Andrew Heather's avatar
Andrew Heather committed
123
{
124
    Log << type() << " " << name() << " write:" << nl;
125
126
127
128
129

    if (!obr_.time().writeTime())
    {
        obr_.time().writeTimeDict();
    }
andy's avatar
andy committed
130

131
132
    // Get selection
    const wordList selectedNames(obr_.sortedNames<regIOobject>(objectNames_));
andy's avatar
andy committed
133

134
135
136
137
138
139
140
    // Warning if anything was missed
    bitSet missed(objectNames_.size());

    label index = 0;
    for (const wordRe& select : objectNames_)
    {
        if (!ListOps::found(selectedNames, select))
141
        {
142
            missed.set(index);
143
        }
144
        ++index;
Andrew Heather's avatar
Andrew Heather committed
145
    }
146

147
148
149
150
151
152
153
154
155
156
157
    if (missed.any())
    {
        WarningInFunction
            << "No corresponding selection for "
            << flatOutput(subset(missed, objectNames_)) << nl
            << "Available objects in database:"
            << nl << obr_.sortedToc()
            << endl;
    }

    for (const word& objName : selectedNames)
158
    {
159
        regIOobject& obj = obr_.lookupObjectRef<regIOobject>(objName);
160

161
        switch (writeOption_)
162
        {
163
            case AUTO_WRITE:
164
            {
165
                if (obj.writeOpt() != IOobject::AUTO_WRITE)
166
167
168
169
                {
                    continue;
                }

170
171
                break;
            }
172
            case NO_WRITE:
173
            {
174
                if (obj.writeOpt() != IOobject::NO_WRITE)
175
176
177
178
                {
                    continue;
                }

179
180
                break;
            }
181
            case ANY_WRITE:
182
            {
183
                break;
184
            }
185
            default:
186
187
188
189
            {
                FatalErrorInFunction
                    << "Unknown writeOption "
                    << writeOptionNames_[writeOption_]
Mark OLESEN's avatar
Mark OLESEN committed
190
191
                    << ". Valid writeOption types are "
                    << writeOptionNames_
192
                    << exit(FatalError);
193
194
195

                continue;
                break;
196
            }
197
198
        }

199
200
201
202
203
204
        if
        (
            obj.writeOpt() == IOobject::AUTO_WRITE
         && obr_.time().writeTime()
        )
        {
205
            Log << "    automatically written object " << obj.name() << endl;
206
207
208
        }
        else
        {
209
            Log << "    writing object " << obj.name() << endl;
210

211
212
            obj.write();
        }
213
    }
214
215

    return true;
Andrew Heather's avatar
Andrew Heather committed
216
217
218
219
}


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