debug.C 8.35 KB
Newer Older
1
2
3
4
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
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
26
27
28
29
30
31

Description
    Class for handling debugging switches.

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

#include "debug.H"
#include "dictionary.H"
#include "IFstream.H"
32
#include "etcFiles.H"
33
34
35
#include "Ostream.H"
#include "demandDrivenData.H"
#include "simpleObjectRegistry.H"
36
37
38
39
40
41
42
43

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

namespace Foam
{
namespace debug
{

44
45
46
//! \cond ignoreDocumentation
//- Skip documentation : local scope only

47
48
49
50
dictionary* controlDictPtr_(nullptr);
dictionary* debugSwitchesPtr_(nullptr);
dictionary* infoSwitchesPtr_(nullptr);
dictionary* optimisationSwitchesPtr_(nullptr);
51

52
// Debug switch read and write callback tables.
53
54
55
56
57
simpleObjectRegistry* debugObjectsPtr_(nullptr);
simpleObjectRegistry* infoObjectsPtr_(nullptr);
simpleObjectRegistry* optimisationObjectsPtr_(nullptr);
simpleObjectRegistry* dimensionSetObjectsPtr_(nullptr);
simpleObjectRegistry* dimensionedConstantObjectsPtr_(nullptr);
58
59


60
// To ensure controlDictPtr_ is deleted at the end of the run
61
class deleteControlDictPtr
62
63
64
{
public:

65
    deleteControlDictPtr()
66
67
    {}

68
    ~deleteControlDictPtr()
69
70
71
72
    {
        deleteDemandDrivenData(debugObjectsPtr_);
        deleteDemandDrivenData(infoObjectsPtr_);
        deleteDemandDrivenData(optimisationObjectsPtr_);
mattijs's avatar
mattijs committed
73
        deleteDemandDrivenData(dimensionSetObjectsPtr_);
74
        deleteDemandDrivenData(dimensionedConstantObjectsPtr_);
75

76
77
78
        debugSwitchesPtr_ = nullptr;
        infoSwitchesPtr_ = nullptr;
        optimisationSwitchesPtr_ = nullptr;
79
        deleteDemandDrivenData(controlDictPtr_);
80
81
82
    }
};

83
deleteControlDictPtr deleteControlDictPtr_;
84
//! \endcond
85
86


87
88
} // End namespace debug
} // End namespace Foam
89

90
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
91

92
Foam::dictionary& Foam::debug::controlDict()
93
{
94
    if (!controlDictPtr_)
95
    {
96
97
98
99
        fileNameList controlDictFiles = findEtcFiles("controlDict", true);
        controlDictPtr_ = new dictionary();
        forAllReverse(controlDictFiles, cdfi)
        {
mattijs's avatar
mattijs committed
100
101
102
103
            IFstream ifs(controlDictFiles[cdfi]);

            if (!ifs.good())
            {
104
                SafeFatalIOErrorInFunction
mattijs's avatar
mattijs committed
105
106
107
108
109
110
                (
                    ifs,
                    "Cannot open controlDict"
                );
            }
            controlDictPtr_->merge(dictionary(ifs));
111
        }
112
113
    }

114
    return *controlDictPtr_;
115
116
117
}


118
119
120
121
122
Foam::dictionary& Foam::debug::switchSet
(
    const char* subDictName,
    dictionary*& subDictPtr
)
123
{
124
    if (!subDictPtr)
125
    {
126
127
128
129
        entry* ePtr = controlDict().lookupEntryPtr
        (
            subDictName, false, false
        );
130

131
        if (!ePtr || !ePtr->isDict())
132
        {
133
134
135
            cerr<< "debug::switchSet(const char*, dictionary*&):\n"
                << "    Cannot find " <<  subDictName << " in dictionary "
                << controlDict().name().c_str()
136
                << std::endl << std::endl;
137

138
139
140
            ::exit(1);
        }

141
        subDictPtr = &ePtr->dict();
142
143
    }

144
    return *subDictPtr;
145
146
147
}


148
Foam::dictionary& Foam::debug::debugSwitches()
149
150
151
152
153
{
    return switchSet("DebugSwitches", debugSwitchesPtr_);
}


154
Foam::dictionary& Foam::debug::infoSwitches()
155
{
156
    return switchSet("InfoSwitches", infoSwitchesPtr_);
157
158
159
}


160
Foam::dictionary& Foam::debug::optimisationSwitches()
161
{
162
    return switchSet("OptimisationSwitches", optimisationSwitchesPtr_);
163
164
165
}


166
int Foam::debug::debugSwitch(const char* name, const int defaultValue)
167
{
168
    return debugSwitches().lookupOrAddDefault
169
    (
170
        name, defaultValue, false, false
171
172
173
174
    );
}


175
int Foam::debug::infoSwitch(const char* name, const int defaultValue)
176
{
177
178
179
180
    return infoSwitches().lookupOrAddDefault
    (
        name, defaultValue, false, false
    );
181
182
183
}


184
int Foam::debug::optimisationSwitch(const char* name, const int defaultValue)
185
{
186
    return optimisationSwitches().lookupOrAddDefault
187
    (
188
        name, defaultValue, false, false
189
190
191
192
    );
}


193
194
195
196
197
198
199
200
201
202
203
204
205
float Foam::debug::floatOptimisationSwitch
(
    const char* name,
    const float defaultValue
)
{
    return optimisationSwitches().lookupOrAddDefault
    (
        name, defaultValue, false, false
    );
}


206
207
void Foam::debug::addDebugObject(const char* name, simpleRegIOobject* obj)
{
208
209
    simpleObjectRegistryEntry* ptr = debugObjects().lookupPtr(name);
    if (ptr)
mattijs's avatar
mattijs committed
210
    {
211
212
213
214
215
216
217
218
219
220
221
222
        ptr->append(obj);
    }
    else
    {
        debugObjects().append
        (
            name,
            new simpleObjectRegistryEntry
            (
                List<simpleRegIOobject*>(1, obj)
            )
        );
mattijs's avatar
mattijs committed
223
    }
224
225
226
227
228
}


void Foam::debug::addInfoObject(const char* name, simpleRegIOobject* obj)
{
229
230
231
232
233
234
    simpleObjectRegistryEntry* ptr = infoObjects().lookupPtr(name);
    if (ptr)
    {
        ptr->append(obj);
    }
    else
mattijs's avatar
mattijs committed
235
    {
236
237
238
239
240
241
242
243
        infoObjects().append
        (
            name,
            new simpleObjectRegistryEntry
            (
                List<simpleRegIOobject*>(1, obj)
            )
        );
mattijs's avatar
mattijs committed
244
    }
245
246
247
248
249
250
251
252
253
}


void Foam::debug::addOptimisationObject
(
    const char* name,
    simpleRegIOobject* obj
)
{
254
255
    simpleObjectRegistryEntry* ptr = optimisationObjects().lookupPtr(name);
    if (ptr)
mattijs's avatar
mattijs committed
256
    {
257
258
259
260
261
262
263
264
265
266
267
268
        ptr->append(obj);
    }
    else
    {
        optimisationObjects().append
        (
            name,
            new simpleObjectRegistryEntry
            (
                List<simpleRegIOobject*>(1, obj)
            )
        );
mattijs's avatar
mattijs committed
269
    }
270
271
272
}


mattijs's avatar
mattijs committed
273
274
275
276
277
278
void Foam::debug::addDimensionSetObject
(
    const char* name,
    simpleRegIOobject* obj
)
{
279
280
    simpleObjectRegistryEntry* ptr = dimensionSetObjects().lookupPtr(name);
    if (ptr)
mattijs's avatar
mattijs committed
281
    {
282
283
284
285
286
287
288
289
290
291
292
293
        ptr->append(obj);
    }
    else
    {
        dimensionSetObjects().append
        (
            name,
            new simpleObjectRegistryEntry
            (
                List<simpleRegIOobject*>(1, obj)
            )
        );
mattijs's avatar
mattijs committed
294
    }
mattijs's avatar
mattijs committed
295
296
297
}


298
299
300
301
302
303
void Foam::debug::addDimensionedConstantObject
(
    const char* name,
    simpleRegIOobject* obj
)
{
304
305
306
307
308
309
310
311
312
    simpleObjectRegistryEntry* ptr = dimensionedConstantObjects().lookupPtr
    (
        name
    );
    if (ptr)
    {
        ptr->append(obj);
    }
    else
313
    {
314
315
316
317
318
319
320
321
        dimensionedConstantObjects().append
        (
            name,
            new simpleObjectRegistryEntry
            (
                List<simpleRegIOobject*>(1, obj)
            )
        );
322
323
324
325
    }
}


326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
Foam::simpleObjectRegistry& Foam::debug::debugObjects()
{
    if (!debugObjectsPtr_)
    {
        debugObjectsPtr_ = new simpleObjectRegistry(1000);
    }

    return *debugObjectsPtr_;
}


Foam::simpleObjectRegistry& Foam::debug::infoObjects()
{
    if (!infoObjectsPtr_)
    {
341
        infoObjectsPtr_ = new simpleObjectRegistry(100);
342
343
344
345
346
347
348
349
350
351
    }

    return *infoObjectsPtr_;
}


Foam::simpleObjectRegistry& Foam::debug::optimisationObjects()
{
    if (!optimisationObjectsPtr_)
    {
352
        optimisationObjectsPtr_ = new simpleObjectRegistry(100);
353
354
355
356
357
358
    }

    return *optimisationObjectsPtr_;
}


mattijs's avatar
mattijs committed
359
360
361
362
Foam::simpleObjectRegistry& Foam::debug::dimensionSetObjects()
{
    if (!dimensionSetObjectsPtr_)
    {
363
        dimensionSetObjectsPtr_ = new simpleObjectRegistry(100);
mattijs's avatar
mattijs committed
364
365
366
367
368
369
    }

    return *dimensionSetObjectsPtr_;
}


370
371
372
373
374
375
376
377
378
379
380
Foam::simpleObjectRegistry& Foam::debug::dimensionedConstantObjects()
{
    if (!dimensionedConstantObjectsPtr_)
    {
        dimensionedConstantObjectsPtr_ = new simpleObjectRegistry(100);
    }

    return *dimensionedConstantObjectsPtr_;
}


381
// ************************************************************************* //