vtkPVblockMesh.C 12.3 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
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
     \\/     M anipulation  |
-------------------------------------------------------------------------------
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/>.

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

26
27
#include "vtkPVblockMesh.H"
#include "vtkPVblockMeshReader.h"
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

// OpenFOAM includes
#include "blockMesh.H"
#include "Time.H"
#include "patchZones.H"
#include "OStringStream.H"

// VTK includes
#include "vtkDataArraySelection.h"
#include "vtkMultiBlockDataSet.h"
#include "vtkRenderer.h"
#include "vtkTextActor.h"
#include "vtkTextProperty.h"

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

namespace Foam
{
46
    defineTypeNameAndDebug(vtkPVblockMesh, 0);
47
48
49
50
51
}


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

52
void Foam::vtkPVblockMesh::resetCounters()
53
54
55
56
57
58
59
60
{
    // Reset mesh part ids and sizes
    arrayRangeBlocks_.reset();
    arrayRangeEdges_.reset();
    arrayRangeCorners_.reset();
}


61
void Foam::vtkPVblockMesh::updateInfoBlocks
62
63
64
65
66
67
(
    vtkDataArraySelection* arraySelection
)
{
    if (debug)
    {
68
        Info<< "<beg> Foam::vtkPVblockMesh::updateInfoBlocks"
69
            << " [meshPtr=" << (meshPtr_ ? "set" : "nullptr") << "]" << endl;
70
71
72
73
74
75
76
77
    }

    arrayRangeBlocks_.reset( arraySelection->GetNumberOfArrays() );

    const blockMesh& blkMesh = *meshPtr_;
    const int nBlocks = blkMesh.size();
    for (int blockI = 0; blockI < nBlocks; ++blockI)
    {
78
        const blockDescriptor& blockDef = blkMesh[blockI];
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98

        word partName = Foam::name(blockI);

        // append the (optional) zone name
        if (!blockDef.zoneName().empty())
        {
            partName += " - " + blockDef.zoneName();
        }

        // Add blockId and zoneName to GUI list
        arraySelection->AddArray(partName.c_str());
    }

    arrayRangeBlocks_ += nBlocks;

    if (debug)
    {
        // just for debug info
        getSelectedArrayEntries(arraySelection);

99
        Info<< "<end> Foam::vtkPVblockMesh::updateInfoBlocks" << endl;
100
101
102
103
    }
}


104
void Foam::vtkPVblockMesh::updateInfoEdges
105
106
107
108
109
110
(
    vtkDataArraySelection* arraySelection
)
{
    if (debug)
    {
111
        Info<< "<beg> Foam::vtkPVblockMesh::updateInfoEdges"
112
            << " [meshPtr=" << (meshPtr_ ? "set" : "nullptr") << "]" << endl;
113
114
115
116
117
    }

    arrayRangeEdges_.reset( arraySelection->GetNumberOfArrays() );

    const blockMesh& blkMesh = *meshPtr_;
118
    const blockEdgeList& edges = blkMesh.edges();
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138

    const int nEdges = edges.size();
    forAll(edges, edgeI)
    {
        OStringStream ostr;

        ostr<< edges[edgeI].start() << ":" << edges[edgeI].end() << " - "
            << edges[edgeI].type();

        // Add "beg:end - type" to GUI list
        arraySelection->AddArray(ostr.str().c_str());
    }

    arrayRangeEdges_ += nEdges;

    if (debug)
    {
        // just for debug info
        getSelectedArrayEntries(arraySelection);

139
        Info<< "<end> Foam::vtkPVblockMesh::updateInfoEdges" << endl;
140
141
142
143
144
145
    }
}


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

146
Foam::vtkPVblockMesh::vtkPVblockMesh
147
148
(
    const char* const FileName,
149
    vtkPVblockMeshReader* reader
150
151
152
)
:
    reader_(reader),
153
154
    dbPtr_(nullptr),
    meshPtr_(nullptr),
155
156
157
158
159
160
161
162
    meshRegion_(polyMesh::defaultRegion),
    meshDir_(polyMesh::meshSubDir),
    arrayRangeBlocks_("block"),
    arrayRangeEdges_("edges"),
    arrayRangeCorners_("corners")
{
    if (debug)
    {
163
        Info<< "Foam::vtkPVblockMesh::vtkPVblockMesh - "
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
            << FileName << endl;
    }

    // avoid argList and get rootPath/caseName directly from the file
    fileName fullCasePath(fileName(FileName).path());

    if (!isDir(fullCasePath))
    {
        return;
    }
    if (fullCasePath == ".")
    {
        fullCasePath = cwd();
    }

    // Set the case as an environment variable - some BCs might use this
    if (fullCasePath.name().find("processor", 0) == 0)
    {
        const fileName globalCase = fullCasePath.path();

        setEnv("FOAM_CASE", globalCase, true);
        setEnv("FOAM_CASENAME", globalCase.name(), true);
    }
    else
    {
        setEnv("FOAM_CASE", fullCasePath, true);
        setEnv("FOAM_CASENAME", fullCasePath.name(), true);
    }

    // look for 'case{region}.OpenFOAM'
    // could be stringent and insist the prefix match the directory name...
    // Note: cannot use fileName::name() due to the embedded '{}'
    string caseName(fileName(FileName).lessExt());
    string::size_type beg = caseName.find_last_of("/{");
    string::size_type end = caseName.find('}', beg);

    if
    (
        beg != string::npos && caseName[beg] == '{'
     && end != string::npos && end == caseName.size()-1
    )
    {
        meshRegion_ = caseName.substr(beg+1, end-beg-1);

        // some safety
        if (meshRegion_.empty())
        {
            meshRegion_ = polyMesh::defaultRegion;
        }

        if (meshRegion_ != polyMesh::defaultRegion)
        {
            meshDir_ = meshRegion_/polyMesh::meshSubDir;
        }
    }

    if (debug)
    {
        Info<< "fullCasePath=" << fullCasePath << nl
            << "FOAM_CASE=" << getEnv("FOAM_CASE") << nl
            << "FOAM_CASENAME=" << getEnv("FOAM_CASENAME") << endl;
    }

    // Create time object
    dbPtr_.reset
    (
        new Time
        (
            Time::controlDictName,
            fileName(fullCasePath.path()),
            fileName(fullCasePath.name())
        )
    );

    dbPtr_().functionObjects().off();

    updateInfo();
}


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

246
Foam::vtkPVblockMesh::~vtkPVblockMesh()
247
248
249
{
    if (debug)
    {
250
        Info<< "<end> Foam::vtkPVblockMesh::~vtkPVblockMesh" << endl;
251
252
253
254
    }

    // Hmm. pointNumberTextActors are not getting removed
    //
255
    forAll(pointNumberTextActorsPtrs_, pointi)
256
    {
257
        pointNumberTextActorsPtrs_[pointi]->Delete();
258
259
260
261
262
263
264
265
266
    }
    pointNumberTextActorsPtrs_.clear();

    delete meshPtr_;
}


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

267
void Foam::vtkPVblockMesh::updateInfo()
268
269
270
{
    if (debug)
    {
271
        Info<< "<beg> Foam::vtkPVblockMesh::updateInfo"
272
            << " [meshPtr=" << (meshPtr_ ? "set" : "nullptr") << "] " << endl;
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
    }

    resetCounters();

    vtkDataArraySelection* blockSelection = reader_->GetBlockSelection();
    vtkDataArraySelection* edgeSelection = reader_->GetCurvedEdgesSelection();

    // enable 'internalMesh' on the first call
    // or preserve the enabled selections
    stringList enabledParts;
    stringList enabledEdges;
    bool firstTime = false;
    if (!blockSelection->GetNumberOfArrays() && !meshPtr_)
    {
        firstTime = true;
    }
    else
    {
        enabledParts = getSelectedArrayEntries(blockSelection);
        enabledEdges = getSelectedArrayEntries(edgeSelection);
    }

    // Clear current mesh parts list
    blockSelection->RemoveAllArrays();
    edgeSelection->RemoveAllArrays();

    // need a blockMesh
    updateFoamMesh();

    // Update mesh parts list
    updateInfoBlocks( blockSelection );

    // Update curved edges list
    updateInfoEdges( edgeSelection );

    // restore the enabled selections
    if (!firstTime)
    {
        setSelectedArrayEntries(blockSelection, enabledParts);
        setSelectedArrayEntries(edgeSelection, enabledEdges);
    }

    if (debug)
    {
317
        Info<< "<end> Foam::vtkPVblockMesh::updateInfo" << endl;
318
319
320
321
    }
}


322
void Foam::vtkPVblockMesh::updateFoamMesh()
323
324
325
{
    if (debug)
    {
326
        Info<< "<beg> Foam::vtkPVblockMesh::updateFoamMesh" << endl;
327
328
329
330
331
332
333
334
335
336
337
    }

    // Check to see if the OpenFOAM mesh has been created
    if (!meshPtr_)
    {
        if (debug)
        {
            Info<< "Creating blockMesh at time=" << dbPtr_().timeName()
                << endl;
        }

338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
        // Set path for the blockMeshDict
        const word dictName("blockMeshDict");
        fileName dictPath(dbPtr_().system()/dictName);

        // Check if dictionary is present in the constant directory
        if
        (
            exists
            (
                dbPtr_().path()/dbPtr_().constant()
               /polyMesh::meshSubDir/dictName
            )
        )
        {
            dictPath = dbPtr_().constant()/polyMesh::meshSubDir/dictName;
        }

355
356
357
358
        IOdictionary meshDict
        (
            IOobject
            (
359
                dictPath,
360
361
362
363
364
365
366
367
368
369
370
371
372
                dbPtr_(),
                IOobject::MUST_READ_IF_MODIFIED,
                IOobject::NO_WRITE,
                false
            )
        );

        meshPtr_ = new blockMesh(meshDict, meshRegion_);
    }


    if (debug)
    {
373
        Info<< "<end> Foam::vtkPVblockMesh::updateFoamMesh" << endl;
374
375
376
377
    }
}


378
void Foam::vtkPVblockMesh::Update
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
(
    vtkMultiBlockDataSet* output
)
{
    reader_->UpdateProgress(0.1);

    // Set up mesh parts selection(s)
    updateBoolListStatus(blockStatus_, reader_->GetBlockSelection());

    // Set up curved edges selection(s)
    updateBoolListStatus(edgeStatus_, reader_->GetCurvedEdgesSelection());

    reader_->UpdateProgress(0.2);

    // Update the OpenFOAM mesh
    updateFoamMesh();
    reader_->UpdateProgress(0.5);

    // Convert mesh elemente
    int blockNo = 0;

    convertMeshCorners(output, blockNo);
    convertMeshBlocks(output, blockNo);
    convertMeshEdges(output, blockNo);

    reader_->UpdateProgress(0.8);

}


409
void Foam::vtkPVblockMesh::CleanUp()
410
411
412
413
414
{
    reader_->UpdateProgress(1.0);
}


415
void Foam::vtkPVblockMesh::renderPointNumbers
416
417
418
419
420
421
422
(
    vtkRenderer* renderer,
    const bool show
)
{
    // always remove old actors first

423
    forAll(pointNumberTextActorsPtrs_, pointi)
424
    {
425
426
        renderer->RemoveViewProp(pointNumberTextActorsPtrs_[pointi]);
        pointNumberTextActorsPtrs_[pointi]->Delete();
427
428
429
430
431
    }
    pointNumberTextActorsPtrs_.clear();

    if (show && meshPtr_)
    {
432
        const pointField& cornerPts = meshPtr_->vertices();
433
434
435
        const scalar scaleFactor = meshPtr_->scaleFactor();

        pointNumberTextActorsPtrs_.setSize(cornerPts.size());
436
        forAll(cornerPts, pointi)
437
438
439
        {
            vtkTextActor* txt = vtkTextActor::New();

440
            txt->SetInput(Foam::name(pointi).c_str());
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456

            // Set text properties
            vtkTextProperty* tprop = txt->GetTextProperty();
            tprop->SetFontFamilyToArial();
            tprop->BoldOn();
            tprop->ShadowOff();
            tprop->SetLineSpacing(1.0);
            tprop->SetFontSize(14);
            tprop->SetColor(1.0, 0.0, 1.0);
            tprop->SetJustificationToCentered();

            // Set text to use 3-D world co-ordinates
            txt->GetPositionCoordinate()->SetCoordinateSystemToWorld();

            txt->GetPositionCoordinate()->SetValue
            (
457
458
459
                cornerPts[pointi].x()*scaleFactor,
                cornerPts[pointi].y()*scaleFactor,
                cornerPts[pointi].z()*scaleFactor
460
461
462
463
464
465
466
            );

            // Add text to each renderer
            renderer->AddViewProp(txt);

            // Maintain a list of text labels added so that they can be
            // removed later
467
            pointNumberTextActorsPtrs_[pointi] = txt;
468
469
470
471
472
473
        }
    }
}



474
void Foam::vtkPVblockMesh::PrintSelf(ostream& os, vtkIndent indent) const
475
476
477
478
479
480
481
482
483
484
485
486
487
488
{
#if 0
    os  << indent << "Number of nodes: "
        << (meshPtr_ ? meshPtr_->nPoints() : 0) << "\n";

    os  << indent << "Number of cells: "
        << (meshPtr_ ? meshPtr_->nCells() : 0) << "\n";

    os  << indent << "Number of available time steps: "
        << (dbPtr_.valid() ? dbPtr_().times().size() : 0) << endl;
#endif
}

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