1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 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 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 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 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 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 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
|
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
-------------------------------------------------------------------------------
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/>.
\*---------------------------------------------------------------------------*/
#include "vtkPVFoamReader.h"
#include "pqApplicationCore.h"
#include "pqRenderView.h"
#include "pqServerManagerModel.h"
// VTK includes
#include "vtkCallbackCommand.h"
#include "vtkDataArraySelection.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkInformationDoubleVectorKey.h"
#include "vtkMultiBlockDataSet.h"
#include "vtkObjectFactory.h"
#include "vtkSMRenderViewProxy.h"
#include "vtkStreamingDemandDrivenPipeline.h"
#include "vtkStringArray.h"
#include "vtkSmartPointer.h"
// OpenFOAM includes
#include "vtkPVFoam.H"
// STL includes
#include <vector>
#undef VTKPVFOAM_DUALPORT
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
vtkStandardNewMacro(vtkPVFoamReader);
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
vtkPVFoamReader::vtkPVFoamReader()
{
Debug = 0;
vtkDebugMacro(<<"Constructor");
SetNumberOfInputPorts(0);
FileName = nullptr;
backend_ = nullptr;
#ifdef VTKPVFOAM_DUALPORT
// Add second output for the Lagrangian
this->SetNumberOfOutputPorts(2);
auto lagrangian = vtkSmartPointer<vtkMultiBlockDataSet>::New();
lagrangian->ReleaseData();
this->GetExecutive()->SetOutputData(1, lagrangian);
#endif
TimeStepRange[0] = 0;
TimeStepRange[1] = 0;
MeshCaching = 3; // fvMesh+vtk
SkipZeroTime = true;
ExtrapolatePatches = false;
UseVTKPolyhedron = false;
IncludeSets = false;
IncludeZones = false;
ShowPatchNames = false;
ShowGroupsOnly = false;
InterpolateVolFields = true;
UpdateGUI = false;
PartSelection = vtkDataArraySelection::New();
VolFieldSelection = vtkDataArraySelection::New();
PointFieldSelection = vtkDataArraySelection::New();
LagrangianFieldSelection = vtkDataArraySelection::New();
// Setup the selection callback to modify this object when an array
// selection is changed.
SelectionObserver = vtkCallbackCommand::New();
SelectionObserver->SetCallback
(
&vtkPVFoamReader::SelectionModifiedCallback
);
SelectionObserver->SetClientData(this);
PartSelection->AddObserver
(
vtkCommand::ModifiedEvent,
this->SelectionObserver
);
VolFieldSelection->AddObserver
(
vtkCommand::ModifiedEvent,
this->SelectionObserver
);
PointFieldSelection->AddObserver
(
vtkCommand::ModifiedEvent,
this->SelectionObserver
);
LagrangianFieldSelection->AddObserver
(
vtkCommand::ModifiedEvent,
this->SelectionObserver
);
}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
vtkPVFoamReader::~vtkPVFoamReader()
{
vtkDebugMacro(<<"Deconstructor");
if (backend_)
{
// Remove text actors
updatePatchNamesView(false);
delete backend_;
backend_ = nullptr;
}
if (FileName)
{
delete[] FileName;
}
PartSelection->RemoveAllObservers();
VolFieldSelection->RemoveAllObservers();
PointFieldSelection->RemoveAllObservers();
LagrangianFieldSelection->RemoveAllObservers();
SelectionObserver->Delete();
PartSelection->Delete();
VolFieldSelection->Delete();
PointFieldSelection->Delete();
LagrangianFieldSelection->Delete();
}
// * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
// Do everything except set the output info
int vtkPVFoamReader::RequestInformation
(
vtkInformation* vtkNotUsed(request),
vtkInformationVector** vtkNotUsed(inputVector),
vtkInformationVector* outputVector
)
{
vtkDebugMacro(<<"RequestInformation");
if (Foam::vtkPVFoam::debug)
{
cout<<"REQUEST_INFORMATION\n";
}
if (!FileName)
{
vtkErrorMacro("FileName has to be specified!");
return 0;
}
const int nInfo = outputVector->GetNumberOfInformationObjects();
if (Foam::vtkPVFoam::debug)
{
cout<<"RequestInformation with " << nInfo << " item(s)\n";
for (int infoI = 0; infoI < nInfo; ++infoI)
{
outputVector->GetInformationObject(infoI)->Print(cout);
}
}
if (backend_)
{
backend_->updateInfo();
}
else
{
backend_ = new Foam::vtkPVFoam(FileName, this);
}
std::vector<double> times = backend_->findTimes(this->SkipZeroTime);
if (times.empty())
{
vtkErrorMacro("could not find valid OpenFOAM mesh");
// delete backend handler and flag it as fatal error
delete backend_;
backend_ = nullptr;
return 0;
}
// set identical time steps for all ports
for (int infoI = 0; infoI < nInfo; ++infoI)
{
vtkInformation *outInfo = outputVector->GetInformationObject(infoI);
outInfo->Set
(
vtkStreamingDemandDrivenPipeline::TIME_STEPS(),
times.data(),
static_cast<int>(times.size())
);
// Something like this may be useful:
// outInfo->Set
// (
// vtkStreamingDemandDrivenPipeline::TIME_DEPENDENT_INFORMATION(),
// 1
// );
}
if (times.size())
{
double timeRange[2]{ times.front(), times.back() };
if (Foam::vtkPVFoam::debug > 1)
{
cout
<<"nInfo " << nInfo << "\n"
<<"time-range " << times.front() << ':' << times.back() << "\n"
<<"times " << times.size() << "(";
for (auto val : times)
{
cout<< ' ' << val;
}
cout << " )" << std::endl;
}
for (int infoI = 0; infoI < nInfo; ++infoI)
{
vtkInformation *outInfo = outputVector->GetInformationObject(infoI);
outInfo->Set
(
vtkStreamingDemandDrivenPipeline::TIME_RANGE(),
timeRange,
2
);
}
}
return 1;
}
// Set the output info
int vtkPVFoamReader::RequestData
(
vtkInformation* vtkNotUsed(request),
vtkInformationVector** vtkNotUsed(inputVector),
vtkInformationVector* outputVector
)
{
vtkDebugMacro(<<"RequestData");
if (!FileName)
{
vtkErrorMacro("FileName must be specified!");
return 0;
}
if (!backend_)
{
// Catch some previous error
vtkErrorMacro("Reader failed - perhaps no mesh?");
return 0;
}
const int nInfo = outputVector->GetNumberOfInformationObjects();
if (Foam::vtkPVFoam::debug)
{
cout<<"RequestData with " << nInfo << " item(s)\n";
for (int infoI = 0; infoI < nInfo; ++infoI)
{
outputVector->GetInformationObject(infoI)->Print(cout);
}
}
// Get the requested time step.
// We only support requests for a single time step
std::vector<double> requestTime;
requestTime.reserve(nInfo);
// taking port0 as the lead for other outputs would be nice, but fails when
// a filter is added - we need to check everything
// but since PREVIOUS_UPDATE_TIME_STEPS() is protected, relay the logic
// to the vtkPVFoam::setTime() method
for (int infoi = 0; infoi < nInfo; ++infoi)
{
vtkInformation *outInfo = outputVector->GetInformationObject(infoi);
const int nsteps =
outInfo->Length(vtkStreamingDemandDrivenPipeline::TIME_STEPS());
if
(
outInfo->Has(vtkStreamingDemandDrivenPipeline::UPDATE_TIME_STEP())
&& nsteps > 0
)
{
const double timeValue =
(
1 == nsteps
// Only one time-step available, UPDATE_TIME_STEP is unreliable
? outInfo->Get(vtkStreamingDemandDrivenPipeline::TIME_STEPS(), 0)
: outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_TIME_STEP())
);
// outInfo->Set(vtkDataObject::DATA_TIME_STEP(), timeValue);
// this->SetTimeValue(timeValue);
requestTime.push_back(timeValue);
}
}
backend_->setTime(requestTime);
vtkMultiBlockDataSet* output = vtkMultiBlockDataSet::SafeDownCast
(
outputVector->GetInformationObject(0)->Get
(
vtkMultiBlockDataSet::DATA_OBJECT()
)
);
#ifdef VTKPVFOAM_DUALPORT
vtkMultiBlockDataSet* output1 = vtkMultiBlockDataSet::SafeDownCast
(
outputVector->GetInformationObject(1)->Get
(
vtkMultiBlockDataSet::DATA_OBJECT()
)
);
backend_->Update(output, output1);
#else
backend_->Update(output, nullptr);
#endif
updatePatchNamesView(ShowPatchNames);
backend_->UpdateFinalize();
return 1;
}
void vtkPVFoamReader::PrintInfo()
{
if (backend_)
{
backend_->printInfo();
}
else
{
cout
<<"OpenFOAM reader not initialized\n"
<< std::flush;
}
}
void vtkPVFoamReader::Refresh()
{
Modified();
}
void vtkPVFoamReader::SetIncludeSets(bool val)
{
if (IncludeSets != val)
{
IncludeSets = val;
if (backend_)
{
backend_->updateInfo();
}
}
}
void vtkPVFoamReader::SetIncludeZones(bool val)
{
if (IncludeZones != val)
{
IncludeZones = val;
if (backend_)
{
backend_->updateInfo();
}
}
}
void vtkPVFoamReader::SetShowPatchNames(bool val)
{
if (ShowPatchNames != val)
{
ShowPatchNames = val;
updatePatchNamesView(ShowPatchNames);
}
}
void vtkPVFoamReader::SetShowGroupsOnly(bool val)
{
if (ShowGroupsOnly != val)
{
ShowGroupsOnly = val;
if (backend_)
{
backend_->updateInfo();
}
}
}
void vtkPVFoamReader::updatePatchNamesView(const bool show)
{
pqApplicationCore* appCore = pqApplicationCore::instance();
// need to check this, since our destructor calls this
if (!appCore)
{
return;
}
// Server manager model for querying items in the server manager
pqServerManagerModel* smModel = appCore->getServerManagerModel();
if (!smModel || !backend_)
{
return;
}
// Get all the pqRenderView instances
for (auto view : smModel->findItems<pqRenderView*>())
{
backend_->renderPatchNames
(
view->getRenderViewProxy()->GetRenderer(),
show
);
}
// Use refresh here?
}
void vtkPVFoamReader::PrintSelf(ostream& os, vtkIndent indent)
{
vtkDebugMacro(<<"PrintSelf");
this->Superclass::PrintSelf(os,indent);
os << indent << "File name: "
<< (this->FileName ? this->FileName : "(none)") << "\n";
backend_->PrintSelf(os, indent);
os << indent << "Time step range: "
<< this->TimeStepRange[0] << " - " << this->TimeStepRange[1] << "\n"
<< indent << "Time step: " << this->GetTimeStep() << endl;
}
int vtkPVFoamReader::GetTimeStep()
{
return backend_ ? backend_->timeIndex() : -1;
}
// ----------------------------------------------------------------------
// Parts selection list control
vtkDataArraySelection* vtkPVFoamReader::GetPartSelection()
{
return PartSelection;
}
int vtkPVFoamReader::GetNumberOfPartArrays()
{
return PartSelection->GetNumberOfArrays();
}
const char* vtkPVFoamReader::GetPartArrayName(int index)
{
return PartSelection->GetArrayName(index);
}
int vtkPVFoamReader::GetPartArrayStatus(const char* name)
{
return PartSelection->ArrayIsEnabled(name);
}
void vtkPVFoamReader::SetPartArrayStatus(const char* name, int status)
{
vtkDebugMacro("Set mesh part \"" << name << "\" status to: " << status);
if (status)
{
PartSelection->EnableArray(name);
}
else
{
PartSelection->DisableArray(name);
}
}
// ----------------------------------------------------------------------
// volField selection list control
vtkDataArraySelection* vtkPVFoamReader::GetVolFieldSelection()
{
return VolFieldSelection;
}
int vtkPVFoamReader::GetNumberOfVolFieldArrays()
{
return VolFieldSelection->GetNumberOfArrays();
}
const char* vtkPVFoamReader::GetVolFieldArrayName(int index)
{
return VolFieldSelection->GetArrayName(index);
}
int vtkPVFoamReader::GetVolFieldArrayStatus(const char* name)
{
return VolFieldSelection->ArrayIsEnabled(name);
}
void vtkPVFoamReader::SetVolFieldArrayStatus(const char* name, int status)
{
if (status)
{
VolFieldSelection->EnableArray(name);
}
else
{
VolFieldSelection->DisableArray(name);
}
}
// ----------------------------------------------------------------------
// pointField selection list control
vtkDataArraySelection* vtkPVFoamReader::GetPointFieldSelection()
{
return PointFieldSelection;
}
int vtkPVFoamReader::GetNumberOfPointFieldArrays()
{
return PointFieldSelection->GetNumberOfArrays();
}
const char* vtkPVFoamReader::GetPointFieldArrayName(int index)
{
return PointFieldSelection->GetArrayName(index);
}
int vtkPVFoamReader::GetPointFieldArrayStatus(const char* name)
{
return PointFieldSelection->ArrayIsEnabled(name);
}
void vtkPVFoamReader::SetPointFieldArrayStatus(const char* name, int status)
{
if (status)
{
PointFieldSelection->EnableArray(name);
}
else
{
PointFieldSelection->DisableArray(name);
}
}
// ----------------------------------------------------------------------
// lagrangianField selection list control
vtkDataArraySelection* vtkPVFoamReader::GetLagrangianFieldSelection()
{
return LagrangianFieldSelection;
}
int vtkPVFoamReader::GetNumberOfLagrangianFieldArrays()
{
return LagrangianFieldSelection->GetNumberOfArrays();
}
const char* vtkPVFoamReader::GetLagrangianFieldArrayName(int index)
{
return LagrangianFieldSelection->GetArrayName(index);
}
int vtkPVFoamReader::GetLagrangianFieldArrayStatus(const char* name)
{
return LagrangianFieldSelection->ArrayIsEnabled(name);
}
void vtkPVFoamReader::SetLagrangianFieldArrayStatus
(
const char* name,
int status
)
{
if (status)
{
LagrangianFieldSelection->EnableArray(name);
}
else
{
LagrangianFieldSelection->DisableArray(name);
}
}
// ----------------------------------------------------------------------
void vtkPVFoamReader::SelectionModifiedCallback
(
vtkObject*,
unsigned long,
void* clientdata,
void*
)
{
static_cast<vtkPVFoamReader*>(clientdata)->SelectionModified();
}
void vtkPVFoamReader::SelectionModified()
{
vtkDebugMacro(<<"SelectionModified");
Modified();
}
int vtkPVFoamReader::FillOutputPortInformation
(
int port,
vtkInformation* info
)
{
if (port == 0)
{
return this->Superclass::FillOutputPortInformation(port, info);
}
info->Set(vtkDataObject::DATA_TYPE_NAME(), "vtkMultiBlockDataSet");
return 1;
}
// ************************************************************************* //
|