vtkPVblockMesh.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2023 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  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 #include "vtkPVblockMesh.H"
27 #include "vtkPVblockMeshReader.h"
28 
29 // OpenFOAM includes
30 #include "blockMesh.H"
31 #include "Time.H"
32 #include "patchZones.H"
33 #include "OStringStream.H"
34 #include "OSspecific.H"
35 
36 // VTK includes
37 #include "vtkDataArraySelection.h"
38 #include "vtkMultiBlockDataSet.h"
39 #include "vtkRenderer.h"
40 #include "vtkTextActor.h"
41 #include "vtkTextProperty.h"
42 
43 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
44 
45 namespace Foam
46 {
47  defineTypeNameAndDebug(vtkPVblockMesh, 0);
48 }
49 
50 
51 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
52 
53 void Foam::vtkPVblockMesh::resetCounters()
54 {
55  // Reset mesh part ids and sizes
56  arrayRangeBlocks_.reset();
57  arrayRangeEdges_.reset();
58  arrayRangeCorners_.reset();
59 }
60 
61 
62 void Foam::vtkPVblockMesh::updateInfoBlocks
63 (
64  vtkDataArraySelection* arraySelection
65 )
66 {
67  if (debug)
68  {
70  << " [meshPtr=" << (meshPtr_ ? "set" : "nullptr") << "]" << endl;
71  }
72 
73  arrayRangeBlocks_.reset( arraySelection->GetNumberOfArrays() );
74 
75  const blockMesh& blkMesh = *meshPtr_;
76 
77  const int nBlocks = blkMesh.size();
78  for (int blockI = 0; blockI < nBlocks; ++blockI)
79  {
80  const blockDescriptor& blockDef = blkMesh[blockI];
81 
82  // Display either blockI as a number or with its name
83  // (looked up from blockMeshDict)
84  OStringStream os;
85  blockDescriptor::write(os, blockI, blkMesh.meshDict());
86  word partName(os.str());
87 
88  // append the (optional) zone name
89  if (!blockDef.zoneName().empty())
90  {
91  partName += " - " + blockDef.zoneName();
92  }
93 
94  // Add blockId and zoneName to GUI list
95  arraySelection->AddArray(partName.c_str());
96  }
97 
98  arrayRangeBlocks_ += nBlocks;
99 
100  if (debug)
101  {
102  // Just for debug info
103  getSelectedArrayEntries(arraySelection);
104  }
105 }
106 
107 
108 void Foam::vtkPVblockMesh::updateInfoEdges
109 (
110  vtkDataArraySelection* arraySelection
111 )
112 {
113  if (debug)
114  {
116  << " [meshPtr=" << (meshPtr_ ? "set" : "nullptr") << "]" << endl;
117  }
118 
119  arrayRangeEdges_.reset( arraySelection->GetNumberOfArrays() );
120 
121  const blockMesh& blkMesh = *meshPtr_;
122  const blockEdgeList& edges = blkMesh.edges();
123 
124  const int nEdges = edges.size();
125  forAll(edges, edgeI)
126  {
127  OStringStream ostr;
128  blockVertex::write(ostr, edges[edgeI].start(), blkMesh.meshDict());
129  ostr<< ":";
130  blockVertex::write(ostr, edges[edgeI].end(), blkMesh.meshDict());
131  ostr << " - " << edges[edgeI].type();
132 
133  // Add "beg:end - type" to GUI list
134  arraySelection->AddArray(ostr.str().c_str());
135  }
136 
137  arrayRangeEdges_ += nEdges;
138 
139  if (debug)
140  {
141  // Just for debug info
142  getSelectedArrayEntries(arraySelection);
143  }
144 }
145 
146 
147 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
148 
150 (
151  const char* const FileName,
152  vtkPVblockMeshReader* reader
153 )
154 :
155  reader_(reader),
156  dbPtr_(nullptr),
157  meshPtr_(nullptr),
158  meshRegion_(polyMesh::defaultRegion),
159  meshDir_(polyMesh::meshSubDir),
160  arrayRangeBlocks_("block"),
161  arrayRangeEdges_("edges"),
162  arrayRangeCorners_("corners")
163 {
164  if (debug)
165  {
166  InfoInFunction<< " - " << FileName << endl;
167  }
168 
169  // Avoid argList and get rootPath/caseName directly from the file
170  fileName fullCasePath(fileName(FileName).path());
171 
172  if (!isDir(fullCasePath))
173  {
174  return;
175  }
176  if (fullCasePath == ".")
177  {
178  fullCasePath = cwd();
179  }
180 
181  // Set the case as an environment variable - some BCs might use this
182  if (fullCasePath.name().find("processor", 0) == 0)
183  {
184  const fileName globalCase = fullCasePath.path();
185 
186  setEnv("FOAM_CASE", globalCase, true);
187  setEnv("FOAM_CASENAME", globalCase.name(), true);
188  }
189  else
190  {
191  setEnv("FOAM_CASE", fullCasePath, true);
192  setEnv("FOAM_CASENAME", fullCasePath.name(), true);
193  }
194 
195  // Look for 'case{region}.OpenFOAM'
196  // could be stringent and insist the prefix match the directory name...
197  // Note: cannot use fileName::name() due to the embedded '{}'
198  string caseName(fileName(FileName).lessExt());
199  string::size_type beg = caseName.find_last_of("/{");
200  string::size_type end = caseName.find('}', beg);
201 
202  if
203  (
204  beg != string::npos && caseName[beg] == '{'
205  && end != string::npos && end == caseName.size()-1
206  )
207  {
208  meshRegion_ = caseName.substr(beg+1, end-beg-1);
209 
210  // Some safety
211  if (meshRegion_.empty())
212  {
213  meshRegion_ = polyMesh::defaultRegion;
214  }
215 
216  if (meshRegion_ != polyMesh::defaultRegion)
217  {
218  meshDir_ = meshRegion_/polyMesh::meshSubDir;
219  }
220  }
221 
222  if (debug)
223  {
224  Info<< " fullCasePath=" << fullCasePath << nl
225  << " FOAM_CASE=" << getEnv("FOAM_CASE") << nl
226  << " FOAM_CASENAME=" << getEnv("FOAM_CASENAME") << endl;
227  }
228 
229  // Create time object
230  dbPtr_.reset
231  (
232  new Time
233  (
234  Time::controlDictName,
235  fileName(fullCasePath.path()),
236  fileName(fullCasePath.name()),
237  false
238  )
239  );
240 
241  updateInfo();
242 }
243 
244 
245 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
246 
248 {
249  if (debug)
250  {
251  InfoInFunction << endl;
252  }
253 
254  // Hmm. pointNumberTextActors are not getting removed
255  //
256  forAll(pointNumberTextActorsPtrs_, pointi)
257  {
258  pointNumberTextActorsPtrs_[pointi]->Delete();
259  }
260  pointNumberTextActorsPtrs_.clear();
261 
262  delete meshPtr_;
263 }
264 
265 
266 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
267 
269 {
270  if (debug)
271  {
273  << " [meshPtr=" << (meshPtr_ ? "set" : "nullptr") << "] " << endl;
274  }
275 
276  resetCounters();
277 
278  vtkDataArraySelection* blockSelection = reader_->GetBlockSelection();
279  vtkDataArraySelection* edgeSelection = reader_->GetCurvedEdgesSelection();
280 
281  // Enable 'internalMesh' on the first call
282  // or preserve the enabled selections
283  stringList enabledParts;
284  stringList enabledEdges;
285  bool firstTime = false;
286  if (!blockSelection->GetNumberOfArrays() && !meshPtr_)
287  {
288  firstTime = true;
289  }
290  else
291  {
292  enabledParts = getSelectedArrayEntries(blockSelection);
293  enabledEdges = getSelectedArrayEntries(edgeSelection);
294  }
295 
296  // Clear current mesh parts list
297  blockSelection->RemoveAllArrays();
298  edgeSelection->RemoveAllArrays();
299 
300  // Need a blockMesh
301  updateFoamMesh();
302 
303  // Update mesh parts list
304  updateInfoBlocks( blockSelection );
305 
306  // Update curved edges list
307  updateInfoEdges( edgeSelection );
308 
309  // Restore the enabled selections
310  if (!firstTime)
311  {
312  setSelectedArrayEntries(blockSelection, enabledParts);
313  setSelectedArrayEntries(edgeSelection, enabledEdges);
314  }
315 }
316 
317 
318 void Foam::vtkPVblockMesh::updateFoamMesh()
319 {
320  if (debug)
321  {
322  InfoInFunction << endl;
323  }
324 
325  // Check to see if the OpenFOAM mesh has been created
326  if (!meshPtr_)
327  {
328  if (debug)
329  {
331  << "Creating blockMesh at time=" << dbPtr_().name() << endl;
332  }
333 
334  // Set path for the blockMeshDict
335  const word dictName("blockMeshDict");
336  fileName dictPath(dbPtr_().system()/dictName);
337 
338  // Check if dictionary is present in the constant directory
339  if
340  (
341  exists
342  (
343  dbPtr_().path()/dbPtr_().constant()
344  /polyMesh::meshSubDir/dictName
345  )
346  )
347  {
348  dictPath = dbPtr_().constant()/polyMesh::meshSubDir/dictName;
349  }
350 
351  // Store dictionary since is used as database inside blockMesh class
352  // for names of vertices and blocks
353  IOdictionary* meshDictPtr = new IOdictionary
354  (
355  IOobject
356  (
357  dictPath,
358  dbPtr_(),
361  true
362  )
363  );
364  meshDictPtr->store();
365 
366  meshPtr_ = new blockMesh(*meshDictPtr, meshRegion_);
367  }
368 }
369 
370 
372 (
373  vtkMultiBlockDataSet* output
374 )
375 {
376  reader_->UpdateProgress(0.1);
377 
378  // Set up mesh parts selection(s)
379  updateBoolListStatus(blockStatus_, reader_->GetBlockSelection());
380 
381  // Set up curved edges selection(s)
382  updateBoolListStatus(edgeStatus_, reader_->GetCurvedEdgesSelection());
383 
384  reader_->UpdateProgress(0.2);
385 
386  // Update the OpenFOAM mesh
387  updateFoamMesh();
388  reader_->UpdateProgress(0.5);
389 
390  // Convert mesh element
391  int blockNo = 0;
392 
393  convertMeshCorners(output, blockNo);
394  convertMeshBlocks(output, blockNo);
395  convertMeshEdges(output, blockNo);
396 
397  reader_->UpdateProgress(0.8);
398 
399 }
400 
401 
403 {
404  reader_->UpdateProgress(1.0);
405 }
406 
407 
409 (
410  vtkRenderer* renderer,
411  const bool show
412 )
413 {
414  // Always remove old actors first
415 
416  forAll(pointNumberTextActorsPtrs_, pointi)
417  {
418  renderer->RemoveViewProp(pointNumberTextActorsPtrs_[pointi]);
419  pointNumberTextActorsPtrs_[pointi]->Delete();
420  }
421  pointNumberTextActorsPtrs_.clear();
422 
423  if (show && meshPtr_)
424  {
425  const blockMesh& blkMesh = *meshPtr_;
426  const pointField& cornerPts = blkMesh.vertices();
427  const scalar scaleFactor = blkMesh.scaleFactor();
428 
429  pointNumberTextActorsPtrs_.setSize(cornerPts.size());
430  forAll(cornerPts, pointi)
431  {
432  vtkTextActor* txt = vtkTextActor::New();
433 
434  // Display either pointi as a number or with its name
435  // (looked up from blockMeshDict)
436  {
437  OStringStream os;
438  blockVertex::write(os, pointi, blkMesh.meshDict());
439  txt->SetInput(os.str().c_str());
440  }
441 
442  // Set text properties
443  vtkTextProperty* tprop = txt->GetTextProperty();
444  tprop->SetFontFamilyToArial();
445  tprop->BoldOn();
446  tprop->ShadowOff();
447  tprop->SetLineSpacing(1.0);
448  tprop->SetFontSize(14);
449  tprop->SetColor(1.0, 0.0, 1.0);
450  tprop->SetJustificationToCentered();
451 
452  // Set text to use 3-D world co-ordinates
453  txt->GetPositionCoordinate()->SetCoordinateSystemToWorld();
454 
455  txt->GetPositionCoordinate()->SetValue
456  (
457  cornerPts[pointi].x()*scaleFactor,
458  cornerPts[pointi].y()*scaleFactor,
459  cornerPts[pointi].z()*scaleFactor
460  );
461 
462  // Add text to each renderer
463  renderer->AddViewProp(txt);
464 
465  // Maintain a list of text labels added so that they can be
466  // removed later
467  pointNumberTextActorsPtrs_[pointi] = txt;
468  }
469  }
470 }
471 
472 
473 
474 void Foam::vtkPVblockMesh::PrintSelf(ostream& os, vtkIndent indent) const
475 {
476 #if 0
477  os << indent << "Number of nodes: "
478  << (meshPtr_ ? meshPtr_->nPoints() : 0) << "\n";
479 
480  os << indent << "Number of cells: "
481  << (meshPtr_ ? meshPtr_->nCells() : 0) << "\n";
482 
483  os << indent << "Number of available time steps: "
484  << (dbPtr_.valid() ? dbPtr_().times().size() : 0) << endl;
485 #endif
486 }
487 
488 // ************************************************************************* //
scalar y
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
graph_traits< Graph >::vertices_size_type size_type
Definition: SloanRenumber.C:73
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
@ MUST_READ_IF_MODIFIED
Definition: IOobject.H:119
void clear()
Clear the list, i.e. set size to zero.
Definition: ListI.H:125
void setSize(const label)
Reset size of List.
Definition: List.C:281
label size() const
Return the number of elements in the UPtrList.
Definition: UPtrListI.H:29
static void write(Ostream &, const label blocki, const dictionary &)
Write block index with dictionary lookup.
static void write(Ostream &, const label, const dictionary &)
Write vertex index with optional name backsubstitution.
Definition: blockVertex.C:120
static word meshSubDir
Return the mesh sub-directory name (usually "polyMesh")
Definition: polyMesh.H:271
~vtkPVblockMesh()
Destructor.
void Update(vtkMultiBlockDataSet *output)
void PrintSelf(ostream &, vtkIndent) const
Debug information.
void CleanUp()
Clean any storage.
void renderPointNumbers(vtkRenderer *, const bool show)
Add/remove point numbers to/from the view.
vtkPVblockMesh(const char *const FileName, vtkPVblockMeshReader *reader)
Construct from components.
void updateInfo()
Update.
#define InfoInFunction
Report an information message using Foam::Info.
autoPtr< CompressibleMomentumTransportModel > New(const volScalarField &rho, const volVectorField &U, const surfaceScalarField &phi, const viscosity &viscosity)
Namespace for OpenFOAM.
fileName cwd()
Return current working directory path name.
Definition: POSIX.C:241
int system(const std::string &command)
Execute the specified command.
Definition: POSIX.C:1230
bool setEnv(const word &name, const std::string &value, const bool overwrite)
Set an environment variable.
Definition: POSIX.C:115
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
bool exists(const fileName &, const bool checkVariants=true, const bool followLink=true)
Does the name exist (as directory or file) in the file system?
Definition: POSIX.C:520
messageStream Info
vectorField pointField
pointField is a vectorField.
Definition: pointFieldFwd.H:42
PtrList< blockEdge > blockEdgeList
A PtrList of blockEdges.
Definition: blockEdgeList.H:45
defineTypeNameAndDebug(combustionModel, 0)
bool isDir(const fileName &, const bool followLink=true)
Does the name exist as a directory in the file system?
Definition: POSIX.C:539
string getEnv(const word &)
Return environment variable of given name.
Definition: POSIX.C:97
List< string > stringList
A List of strings.
Definition: stringList.H:50
Ostream & indent(Ostream &os)
Indent stream.
Definition: Ostream.H:221
static const char nl
Definition: Ostream.H:260