avtAMRFileFormat.C 34 KB
Newer Older
1 2
/*****************************************************************************
*
bonnell's avatar
bonnell committed
3
* Copyright (c) 2000 - 2015, Lawrence Livermore National Security, LLC
4
* Produced at the Lawrence Livermore National Laboratory
bonnell's avatar
bonnell committed
5
* LLNL-CODE-442911
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
* All rights reserved.
*
* This file is  part of VisIt. For  details, see https://visit.llnl.gov/.  The
* full copyright notice is contained in the file COPYRIGHT located at the root
* of the VisIt distribution or at http://www.llnl.gov/visit/copyright.html.
*
* Redistribution  and  use  in  source  and  binary  forms,  with  or  without
* modification, are permitted provided that the following conditions are met:
*
*  - Redistributions of  source code must  retain the above  copyright notice,
*    this list of conditions and the disclaimer below.
*  - Redistributions in binary form must reproduce the above copyright notice,
*    this  list of  conditions  and  the  disclaimer (as noted below)  in  the
*    documentation and/or other materials provided with the distribution.
*  - Neither the name of  the LLNS/LLNL nor the names of  its contributors may
*    be used to endorse or promote products derived from this software without
*    specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT  HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR  IMPLIED WARRANTIES, INCLUDING,  BUT NOT  LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND  FITNESS FOR A PARTICULAR  PURPOSE
* ARE  DISCLAIMED. IN  NO EVENT  SHALL LAWRENCE  LIVERMORE NATIONAL  SECURITY,
* LLC, THE  U.S.  DEPARTMENT OF  ENERGY  OR  CONTRIBUTORS BE  LIABLE  FOR  ANY
* DIRECT,  INDIRECT,   INCIDENTAL,   SPECIAL,   EXEMPLARY,  OR   CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT  LIMITED TO, PROCUREMENT OF  SUBSTITUTE GOODS OR
* SERVICES; LOSS OF  USE, DATA, OR PROFITS; OR  BUSINESS INTERRUPTION) HOWEVER
* CAUSED  AND  ON  ANY  THEORY  OF  LIABILITY,  WHETHER  IN  CONTRACT,  STRICT
* LIABILITY, OR TORT  (INCLUDING NEGLIGENCE OR OTHERWISE)  ARISING IN ANY  WAY
* OUT OF THE  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
*****************************************************************************/

// ************************************************************************* //
whitlocb's avatar
whitlocb committed
40
//                            avtAMRFileFormat.C                             //
41 42 43 44
// ************************************************************************* //
#include <avtAMRFileFormat.h>

#include <vtkCellType.h>
whitlocb's avatar
whitlocb committed
45
#include <vtkFieldData.h>
46
#include <vtkFloatArray.h>
whitlocb's avatar
whitlocb committed
47
#include <vtkIntArray.h>
48 49 50 51
#include <vtkRectilinearGrid.h>
#include <vtkUnstructuredGrid.h>

#include <avtDatabaseMetaData.h>
whitlocb's avatar
whitlocb committed
52 53 54 55
#include <avtIntervalTree.h>
#include <avtStructuredDomainBoundaries.h>
#include <avtStructuredDomainNesting.h>
#include <avtVariableCache.h>
56

whitlocb's avatar
whitlocb committed
57
#include <DebugStream.h>
58 59 60
#include <Expression.h>
#include <InvalidDBTypeException.h>
#include <InvalidVariableException.h>
whitlocb's avatar
whitlocb committed
61 62 63


#include <AMRreaderWithLevels.h>
64

65
#include <string>
whitlocb's avatar
whitlocb committed
66
#include <stdlib.h>
67

whitlocb's avatar
whitlocb committed
68 69 70 71 72 73
#ifdef ENGINE
#define DO_DOMAIN_NESTING
#define DO_DOMAIN_BOUNDARIES
#endif

#define DEBUG_PRINT
74

whitlocb's avatar
whitlocb committed
75 76 77
const std::string avtAMRFileFormat::amr_name     ="AMR";
const std::string avtAMRFileFormat::intf_name    ="Interface";
const std::string avtAMRFileFormat::invalid_name ="Invalid";
78

whitlocb's avatar
whitlocb committed
79 80
std::string avtAMRFileFormat::
composeName( const std::string& m, const std::string& v, const char app )
81
{
whitlocb's avatar
whitlocb committed
82
    return m+app+v;
83 84 85 86
}


void avtAMRFileFormat::
whitlocb's avatar
whitlocb committed
87
decomposeName( const std::string& s, std::string& m, std::string& v )
88
{
whitlocb's avatar
whitlocb committed
89 90 91 92 93 94 95 96 97 98 99
    if( s.compare( 0, amr_name.size(), amr_name )==0 )
        m = amr_name;
    else if( s.compare( 0, intf_name.size(), intf_name )==0 )
        m = intf_name;
    else
        m = invalid_name;

    if( m!=invalid_name )
        v = s.substr( m.size()+1 );
    else
        v = s;
100 101 102 103 104 105 106
}



int avtAMRFileFormat::
GetCycle()
{
whitlocb's avatar
whitlocb committed
107
    return GetReader(false)->GetNumCycles();
108 109 110 111 112 113
}


double avtAMRFileFormat::
GetTime()
{
whitlocb's avatar
whitlocb committed
114
    return GetReader(false)->GetSimuTime();
115 116 117 118 119 120 121 122 123 124
}



// ****************************************************************************
//  Method: avtAMRFileFormat constructor
//
//  Programmer: hpan -- generated by xml2avt
//  Creation:   Mon Apr 26 10:26:32 PDT 2010
//
whitlocb's avatar
whitlocb committed
125 126 127 128 129
// Modifications:
//   Brad Whitlock, Wed May 28 16:15:20 PDT 2014
//   Change how the reader is instantiated. Add enableAMR to indicate whether
//   new AMR enhancements will be enabled.
//
130 131 132 133 134
// ****************************************************************************

avtAMRFileFormat::avtAMRFileFormat(const char *filename)
    : avtSTMDFileFormat(&filename, 1)
{
whitlocb's avatar
whitlocb committed
135
    reader_ = NULL;
136

whitlocb's avatar
whitlocb committed
137 138 139 140 141 142 143
    // Enable the new features by default unless we're purposefully disabling it.
    enableAMR = getenv("AMRDISABLENEW")==NULL;

    // Check the file only if we're in strict mode. This will prevent many files
    // from being opened upfront.
    if(this->GetStrictMode())
        GetReader(false);
144 145 146 147 148
}


avtAMRFileFormat::~avtAMRFileFormat()
{
whitlocb's avatar
whitlocb committed
149 150 151 152 153
    debug2 << "closed avtAMRFileFormat\n.";
    if(reader_ != NULL)
    {
        delete reader_;
    }
154 155
}

whitlocb's avatar
whitlocb committed
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
// ****************************************************************************
// Method: avtAMRFileFormat::GetReader
//
// Purpose:
//   Get the reader object.
//
// Note:       We can defer creation of the real object until we access it from
//             calling this method.
//
// Programmer: Brad Whitlock
// Creation:   Wed May 28 16:13:58 PDT 2014
//
// Modifications:
//
// ****************************************************************************

AMRreaderInterface *
avtAMRFileFormat::GetReader(bool heavyweightInit)
{
    if(reader_ == NULL)
    {
        // Create the reader that will read the file.
        if(enableAMR)
        {
            reader_ = new AMRreaderWithLevels;
        }
        else
        {
            reader_ = InstantiateAMRreader();
        }

        // Read the file information.
        int ierr = reader_->getInfo(filenames[0]);
        if( ierr!=0 )
        {
            EXCEPTION1(InvalidDBTypeException,
                       "The file could not be opened");
        }
    }

    // Do more time-consuming stuff only when we need to.
    if(heavyweightInit)
    {
        reader_->BuildMetaData();
    }
201

whitlocb's avatar
whitlocb committed
202 203
    return reader_;
}
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222


// ****************************************************************************
//  Method: avtAMRFileFormat::FreeUpResources
//
//  Purpose:
//      When VisIt is done focusing on a particular timestep, it asks that
//      timestep to free up any resources (memory, file descriptors) that
//      it has associated with it.  This method is the mechanism for doing
//      that.
//
//  Programmer: hpan -- generated by xml2avt
//  Creation:   Mon Apr 26 10:26:32 PDT 2010
//
// ****************************************************************************

void
avtAMRFileFormat::FreeUpResources(void)
{
whitlocb's avatar
whitlocb committed
223 224 225 226 227 228 229 230
    debug2 << "Now free resource.\n";
    if(reader_ != NULL)
    {
        //reader_->freedata();
        delete reader_;
        reader_ = NULL;
    }
    debug2 << "Now done free resource.\n";
231 232 233 234 235 236 237 238 239 240 241 242 243 244
}


// ****************************************************************************
//  Method: avtAMRFileFormat::PopulateDatabaseMetaData
//
//  Purpose:
//      This database meta-data object is like a table of contents for the
//      file.  By populating it, you are telling the rest of VisIt what
//      information it can request from you.
//
//  Programmer: hpan -- generated by xml2avt
//  Creation:   Mon Apr 26 10:26:32 PDT 2010
//
whitlocb's avatar
whitlocb committed
245 246 247 248 249 250
//  Modifications:
//    Brad Whitlock, Tue May 27 11:10:58 PDT 2014
//    I changed the AMR mesh into an AMR mesh. I also added domain nesting and
//    domain boundaries so VisIt can ghost AMR levels and remove internal faces
//    within levels.
//
251 252 253 254 255
// ****************************************************************************

void
avtAMRFileFormat::PopulateDatabaseMetaData(avtDatabaseMetaData *md)
{
whitlocb's avatar
whitlocb committed
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
    // meshes
    int nblks = GetReader()->GetNumberOfBlocks();
    if( nblks>0 )
    {
        // Basic mesh metadata
        avtMeshMetaData *mmd = new avtMeshMetaData;
        mmd->name = amr_name;
        mmd->spatialDimension = 3;
        mmd->topologicalDimension = 3;
        mmd->meshType = enableAMR ? AVT_AMR_MESH : AVT_RECTILINEAR_MESH;
        mmd->numBlocks = nblks;

        if(enableAMR)
        {
            int nLevels = GetReader()->GetNumberOfLevels();
#ifdef DEBUG_PRINT
            debug1 << "Num levels: " << nLevels << endl;
#endif
            // Create ratios for avtStructuredDomainNesting.
#ifdef DO_DOMAIN_NESTING
            avtStructuredDomainNesting *dn = new avtStructuredDomainNesting(nblks, nLevels);
            dn->SetNumDimensions(3);
            bool *levelCSset = new bool[nLevels];
            for(int level = 0; level < nLevels; ++level)
            {
                std::vector<int> ratios;
                int r = (level == 0) ? 1 : 2;
                ratios.push_back(r);
                ratios.push_back(r);
                ratios.push_back(r);
                dn->SetLevelRefinementRatios(level, ratios);
                levelCSset[level] = false;
            }
#endif
#ifdef DO_DOMAIN_BOUNDARIES
            // Create ratios for avtRectilinearDomainBoundaries
            avtStructuredDomainBoundaries *sdb = new avtRectilinearDomainBoundaries(true);
            sdb->SetNumDomains(nblks);
#endif

            // Use information about each block to fill in metadata, domain nesting,
            // and domain boundaries.
            std::vector<int>         groupIds(nblks);
            std::vector<std::string> blockPieceNames(nblks);
            int localPatchNumber[20];
            memset(localPatchNumber, 0, sizeof(int) * 20);
            for(int bid = 0; bid < nblks; ++bid)
            {
                // Get the level and level-appropriate IJK indexing for the block.
                int level = 0, ijk_start[3] = {0,0,0}, ijk_end[3] = {0,0,0};
                GetReader()->GetBlockHierarchicalIndices(bid, &level, ijk_start, ijk_end);

                //
                // Fill in some fields for the metadata.
                // Use the level and local patch number to make up a name for the patch.
                //
                // COMMENT: If this naming scheme introduces too much shifting
                //          from time step to time step, maybe identifying the patch
                //          using a representation of its block key could be better.
                //
                char tmpName[128];
                groupIds[bid] = level;
                SNPRINTF(tmpName, 128, "level%02d,patch%04d", level, localPatchNumber[level]++);
                blockPieceNames[bid] = tmpName;
#ifdef DEBUG_PRINT
                debug1 << "Block " << bid << " level: " << groupIds[bid]
                       << ", name: " << blockPieceNames[bid] << endl;
#endif
                //
                // Fill in avtStructuredDomainNesting.
                //
#ifdef DO_DOMAIN_NESTING
                if(!levelCSset[level])
                {
                    float xs[3], dx[3];
                    GetReader()->GetBlockMesh(bid, xs, dx);
                    std::vector<double> cs;
                    cs.push_back(dx[0]);
                    cs.push_back(dx[1]);
                    cs.push_back(dx[2]);
                    dn->SetLevelCellSizes(level, cs);
                    levelCSset[level] = true;
                }

                std::vector<int> logicalExtents(6);
                logicalExtents[0] = ijk_start[0];
                logicalExtents[1] = ijk_start[1];
                logicalExtents[2] = ijk_start[2];
                logicalExtents[3] = ijk_end[0];
                logicalExtents[4] = ijk_end[1];
                logicalExtents[5] = ijk_end[2];
#ifdef DEBUG_PRINT
                debug1 << "Block " << bid << " logical indices: "
                       << logicalExtents[0] << ", "
                       << logicalExtents[1] << ", "
                       << logicalExtents[2] << ", "
                       << logicalExtents[3] << ", "
                       << logicalExtents[4] << ", "
                       << logicalExtents[5] << endl;
#endif
                // Get the child patches. The patches are not necessarily ordered in the list but
                // all patches (except 0) will have a direct parent.
                std::vector<int> childPatches;
                if(bid == 0)
                {
                    for(int cbid = 1; cbid < nblks; ++cbid)
                    {
                        if(OctKey_NumLevels(GetReader()->GetBlockKey(cbid)) == 2)
                            childPatches.push_back(cbid);
                    }
                }
                else
                {
                    OctKey thisKey(GetReader()->GetBlockKey(bid));
                    for(int cbid = 1; cbid < nblks; ++cbid)
                    {
                        if(cbid != bid && OctKey_HasImmediateParent(GetReader()->GetBlockKey(cbid), thisKey))
                            childPatches.push_back(cbid);
                    }
                }
#ifdef DEBUG_PRINT
                debug1 << "Block " << bid << " child patches: ";
378
                for(size_t q = 0; q < childPatches.size(); ++q)
whitlocb's avatar
whitlocb committed
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
                    debug1 << childPatches[q] << ", ";
                debug1 << endl;
#endif
                dn->SetNestingForDomain(bid, level, childPatches, logicalExtents);
#endif

                //
                // Fill in avtRectilinearDomainBoundaries.
                //
#ifdef DO_DOMAIN_BOUNDARIES
                int e[6];
                e[0] = ijk_start[0];
                e[1] = ijk_end[0] + 1;
                e[2] = ijk_start[1];
                e[3] = ijk_end[1] + 1;
                e[4] = ijk_start[2];
                e[5] = ijk_end[2] + 1;
                sdb->SetIndicesForAMRPatch(bid, level, e);
#endif
            }

#ifdef DO_DOMAIN_NESTING
            delete [] levelCSset;
            debug2 << "Caching domain nesting." << endl;
            void_ref_ptr vr = void_ref_ptr(dn, avtStructuredDomainNesting::Destruct);
            cache->CacheVoidRef(amr_name.c_str(), AUXILIARY_DATA_DOMAIN_NESTING_INFORMATION,
                                timestep, -1, vr);
#endif
#ifdef DO_DOMAIN_BOUNDARIES
            debug2 << "Caching domain boundaries." << endl;
            sdb->CalculateBoundaries();
            void_ref_ptr vsdb = void_ref_ptr(sdb,avtStructuredDomainBoundaries::Destruct);
            // HACK: VisIt won't use this information unless "any_mesh" is used.
            cache->CacheVoidRef("any_mesh", AUXILIARY_DATA_DOMAIN_BOUNDARY_INFORMATION,
                                timestep, -1, vsdb);
#endif

            // AMR mesh metadata
            mmd->blockTitle = "patches";
            mmd->blockPieceName = "patch";
            mmd->blockNames = blockPieceNames;

            mmd->numGroups = nLevels;
            mmd->groupTitle = "levels";
            mmd->groupPieceName = "level";
            mmd->groupIds = groupIds;
        }

        // Add the mesh metadata.
        md->Add(mmd);
    }

    int np,ne;
    GetReader()->GetInterfaceSizes( &np, &ne );
    if( np>0 && ne>0 )
    {
        avtMeshMetaData *mmd = new avtMeshMetaData;
        mmd->name = intf_name;
        mmd->spatialDimension = 3;
        mmd->topologicalDimension = 2;
        mmd->meshType = AVT_UNSTRUCTURED_MESH;
        mmd->numBlocks = 1;
        md->Add(mmd);
    }

    // AMR variable
    if( nblks>0 )
    {
        // scalar
        avtScalarMetaData *smd;

        smd = new avtScalarMetaData;
        smd->name = composeName( amr_name, "density");
        smd->meshName = amr_name;
        smd->centering = AVT_ZONECENT;
        smd->hasUnits = false;
        md->Add(smd);

        smd = new avtScalarMetaData;
        smd->name = composeName( amr_name, "u velocity");
        smd->meshName = amr_name;
        smd->centering = AVT_ZONECENT;
        smd->hasUnits = false;
        md->Add(smd);

        smd = new avtScalarMetaData;
        smd->name = composeName( amr_name, "v velocity");
        smd->meshName = amr_name;
        smd->centering = AVT_ZONECENT;
        smd->hasUnits = false;
        md->Add(smd);

        smd = new avtScalarMetaData;
        smd->name = composeName( amr_name, "w velocity");
        smd->meshName = amr_name;
        smd->centering = AVT_ZONECENT;
        smd->hasUnits = false;
        md->Add(smd);

        smd = new avtScalarMetaData;
        smd->name = composeName( amr_name, "pressure");
        smd->meshName = amr_name;
        smd->centering = AVT_ZONECENT;
        smd->hasUnits = false;
        md->Add(smd);

        smd = new avtScalarMetaData;
        smd->name = composeName( amr_name, "temperature");
        smd->meshName = amr_name;
        smd->centering = AVT_ZONECENT;
        smd->hasUnits = false;
        md->Add(smd);

        smd = new avtScalarMetaData;
        smd->name = composeName( amr_name, "sound speed");
        smd->meshName = amr_name;
        smd->centering = AVT_ZONECENT;
        smd->hasUnits = false;
        md->Add(smd);

        smd = new avtScalarMetaData;
        smd->name = composeName( amr_name, "internal energy");
        smd->meshName = amr_name;
        smd->centering = AVT_ZONECENT;
        smd->hasUnits = false;
        md->Add(smd);

        smd = new avtScalarMetaData;
        smd->name = composeName( amr_name, "kinetic energy");
        smd->meshName = amr_name;
        smd->centering = AVT_ZONECENT;
        smd->hasUnits = false;
        md->Add(smd);

        smd = new avtScalarMetaData;
        smd->name = composeName( amr_name, "level");
        smd->meshName = amr_name;
        smd->centering = AVT_ZONECENT;
        smd->hasUnits = false;
        md->Add(smd);

        // vector
        avtVectorMetaData *vmd;
        vmd = new avtVectorMetaData;
        vmd->name = composeName( amr_name, "velocity");
        vmd->meshName = amr_name;
        vmd->centering = AVT_ZONECENT;
        vmd->hasUnits = false;
        // ??? is it necessary
        //vmd->varDim = 3;
        md->Add(vmd);

        //tag
        if( GetReader()->HasTag() )
        {
            smd = new avtScalarMetaData;
            smd->name = composeName( amr_name, "cell tag");
            smd->meshName = amr_name;
            smd->centering = AVT_ZONECENT;
            smd->hasUnits = false;
            md->Add(smd);
        }
    }



    // interface
    if( np>0 && ne>0 )
    {
        // scalar
        avtScalarMetaData *smd;

        smd = new avtScalarMetaData;
        smd->name = composeName( intf_name, "u velocity");
        smd->meshName = intf_name;
        smd->centering = AVT_NODECENT;
        smd->hasUnits = false;
        md->Add(smd);

        smd = new avtScalarMetaData;
        smd->name = composeName( intf_name, "v velocity");
        smd->meshName = intf_name;
        smd->centering = AVT_NODECENT;
        smd->hasUnits = false;
        md->Add(smd);

        smd = new avtScalarMetaData;
        smd->name = composeName( intf_name, "w velocity");
        smd->meshName = intf_name;
        smd->centering = AVT_NODECENT;
        smd->hasUnits = false;
        md->Add(smd);

        smd = new avtScalarMetaData;
        smd->name = composeName( intf_name, "pressure");
        smd->meshName = intf_name;
        smd->centering = AVT_ZONECENT;
        smd->hasUnits = false;
        md->Add(smd);

        smd = new avtScalarMetaData;
        smd->name = composeName( intf_name, "material");
        smd->meshName = intf_name;
        smd->centering = AVT_ZONECENT;
        smd->hasUnits = false;
        md->Add(smd);

        // vector
        avtVectorMetaData *vmd;
        vmd = new avtVectorMetaData;
        vmd->name = composeName( intf_name, "velocity");
        vmd->meshName = intf_name;
        vmd->centering = AVT_NODECENT;
        vmd->hasUnits = false;
        // ??? is it necessary
        //vmd->varDim = 3;
        md->Add(vmd);
    }
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
}


// ****************************************************************************
//  Method: avtAMRFileFormat::GetMesh
//
//  Purpose:
//      Gets the mesh associated with this file.  The mesh is returned as a
//      derived type of vtkDataSet (ie vtkRectilinearGrid, vtkStructuredGrid,
//      vtkUnstructuredGrid, etc).
//
//  Arguments:
//      domain      The index of the domain.  If there are NDomains, this
//                  value is guaranteed to be between 0 and NDomains-1,
//                  regardless of block origin.
//      meshname    The name of the mesh of interest.  This can be ignored if
//                  there is only one mesh.
//
//  Programmer: hpan -- generated by xml2avt
//  Creation:   Mon Apr 26 10:26:32 PDT 2010
//
// ****************************************************************************

vtkDataSet *
avtAMRFileFormat::GetMesh(int domain, const char *meshname)
{
whitlocb's avatar
whitlocb committed
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
    // amr
    if( amr_name.compare(meshname)==0 )
    {
        int blkdim[3];
        GetReader()->GetBlockDimensions( domain, blkdim );
        // count the # of points
        ++blkdim[0];
        ++blkdim[1];
        ++blkdim[2];

        float xs[3],dx[3];
        int ierr = GetReader()->GetBlockMesh( domain, xs, dx );
        if( ierr!=0 )
        {
            EXCEPTION1( InvalidVariableException,
                        "Failed to get given block mesh.\n" );
        }

        vtkFloatArray *crd[3];
        for( int as=0; as<3; as++ )
        {
            crd[as] = vtkFloatArray::New();
            crd[as]->SetNumberOfTuples( blkdim[as] );
            float* vv = (float*)crd[as]->GetVoidPointer(0);
            for( int i=0; i<blkdim[as]; i++ )
                vv[i] = xs[as] + (float)i*dx[as];
        }

        vtkRectilinearGrid *grd = vtkRectilinearGrid::New();
        grd->SetDimensions( blkdim );
        grd->SetXCoordinates( crd[0] );
        grd->SetYCoordinates( crd[1] );
        grd->SetZCoordinates( crd[2] );

        for( int as=0; as<3; as++ )
            crd[as]->Delete();

#if 0
        // Needed for AMR Stitch Cell support...
        if(enableAMR)
        {
            // Get the level and level-appropriate IJK indexing for the block.
            int level = 0, ijk_start[3] = {0,0,0}, ijk_end[3] = {0,0,0};
            GetReader()->GetBlockHierarchicalIndices(domain, &level, ijk_start, ijk_end);

            vtkIntArray *arr = vtkIntArray::New();
            arr->SetNumberOfTuples(3);
            arr->SetValue(0, ijk_start[0]);
            arr->SetValue(1, ijk_start[1]);
            arr->SetValue(2, ijk_start[2]);
            arr->SetName("base_index");
            grd->GetFieldData()->AddArray(arr);
            arr->Delete();
        }
677 678
#endif

whitlocb's avatar
whitlocb committed
679
        return grd;
680 681
    }

whitlocb's avatar
whitlocb committed
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
    // interface
    if( intf_name.compare(meshname)==0 )
    {
        int np,ne;
        GetReader()->GetInterfaceSizes( &np, &ne );

        if(np <= 0 || ne <= 0)
        {
            debug2 << "InterfaceSizes returned bad values: np=" << np << ", ne=" << ne << endl;
            return NULL;
        }

        vtkPoints* crd = vtkPoints::New();
        crd->SetNumberOfPoints(np);
        float *pts = (float *) crd->GetVoidPointer(0);
        int ierr = GetReader()->GetInterfaceVariable( AMRreaderInterface::i_coor, pts );
        if( ierr!=0 )
        {
            crd->Delete();
            EXCEPTION1( InvalidVariableException,
                        "Failed to read in interface coordinates.\n" );
        }

        int *elmt = new int[ 4*ne ];
        ierr = GetReader()->GetInterfaceVariable( AMRreaderInterface::i_segt, elmt );
        if( ierr!=0 )
        {
            crd->Delete();
            delete [] elmt;
            std::string msg="Failed to read in interface elements of ";
            msg+=meshname;
            EXCEPTION1( InvalidVariableException, msg );
        }

        vtkUnstructuredGrid *ugrid = vtkUnstructuredGrid::New();
        ugrid->SetPoints( crd );
        ugrid->Allocate( ne );

        vtkIdType verts[4];
        int* cnt=elmt;
        for( int i=0; i<ne; i++ )
        {
            verts[0] = *cnt++;
            verts[0]-=1;
            verts[1] = *cnt++;
            verts[1]-=1;
            verts[2] = *cnt++;
            verts[2]-=1;
            verts[3] = *cnt++;
            verts[3]-=1;
            ugrid->InsertNextCell( VTK_QUAD, 4, verts );
        }

        delete [] elmt;

        return ugrid;
738 739
    }

whitlocb's avatar
whitlocb committed
740 741 742
    std::string msg="Unrecognized mesh name: ";
    msg+=meshname;
    EXCEPTION1( InvalidVariableException, msg );
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762
}


// ****************************************************************************
//  Method: avtAMRFileFormat::GetVar
//
//  Purpose:
//      Gets a scalar variable associated with this file.  Although VTK has
//      support for many different types, the best bet is vtkFloatArray, since
//      that is supported everywhere through VisIt.
//
//  Arguments:
//      domain     The index of the domain.  If there are NDomains, this
//                 value is guaranteed to be between 0 and NDomains-1,
//                 regardless of block origin.
//      varname    The name of the variable requested.
//
//  Programmer: hpan -- generated by xml2avt
//  Creation:   Mon Apr 26 10:26:32 PDT 2010
//
whitlocb's avatar
whitlocb committed
763 764 765 766
// Modifications:
//   Brad Whitlock, Fri Jun  6 11:15:50 PDT 2014
//   Free objects before throwing exceptions.
//
767 768 769 770 771
// ****************************************************************************

vtkDataArray *
avtAMRFileFormat::GetVar(int domain, const char *name)
{
whitlocb's avatar
whitlocb committed
772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812
    std::string mesh,varname;
    decomposeName( name, mesh, varname );
    debug2 << "geting variable " << varname << " on mesh " << mesh << "...\n";

    // amr
    if( mesh.compare(amr_name)==0 )
    {
        int vid;
        if( varname.compare("density")==0 )
            vid=AMRreaderInterface::v_dens;
        else if( varname.compare("u velocity")==0 )
            vid=AMRreaderInterface::v_uvel;
        else if( varname.compare("v velocity")==0 )
            vid=AMRreaderInterface::v_vvel;
        else if( varname.compare("w velocity")==0 )
            vid=AMRreaderInterface::v_wvel;
        else if( varname.compare("pressure")==0 )
            vid=AMRreaderInterface::v_pres;
        else if( varname.compare("temperature")==0 )
            vid=AMRreaderInterface::v_temp;
        else if( varname.compare("sound speed")==0 )
            vid=AMRreaderInterface::v_sndv;
        else if( varname.compare("internal energy")==0 )
            vid=AMRreaderInterface::v_eint;
        else if( varname.compare( "kinetic energy")==0 )
            vid=AMRreaderInterface::v_eknt;
        else if( varname.compare( "cell tag")==0 )
            vid=AMRreaderInterface::v_tags;
#if 1
        else if( enableAMR && varname.compare( "level")==0 )
        {
            int sz = GetReader()->GetBlockSize( domain );
            vtkFloatArray* var = vtkFloatArray::New();
            var->SetNumberOfTuples( sz );

            OctKey k = GetReader()->GetBlockKey(domain);
            int level = OctKey_NumLevels(k)-1;
            for(int i = 0; i < sz; ++i)
                var->SetTuple1(i, level);
            return var;
        }
813
#endif
whitlocb's avatar
whitlocb committed
814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836
        else
        {
            std::string msg = "Unknown scalar variable ";
            msg+=name;
            msg+=" is specified.";
            EXCEPTION1( InvalidVariableException, msg );
        }

        int sz = GetReader()->GetBlockSize( domain );
        debug5 << "vid=" << vid << "  domain=" << domain << "  sz=" << sz << "\n";
        vtkFloatArray* var = vtkFloatArray::New();
        var->SetNumberOfTuples( sz );
        float *val = (float *)var->GetVoidPointer(0);

        int err = GetReader()->GetBlockVariable( domain, vid, val );
        if( err!=0 )
        {
            var->Delete();
            std::string msg = "Failed to read in scalar variable ";
            msg+=name;
            EXCEPTION1( InvalidVariableException, msg );
        }
        return var;
837 838
    }

whitlocb's avatar
whitlocb committed
839 840 841 842 843 844
    // interface
    if( mesh.compare(intf_name)==0 )
    {
        int np,ne;
        GetReader()->GetInterfaceSizes( &np, &ne );

miller86's avatar
miller86 committed
845
        int vid=0,cmp=0,sz=0;
whitlocb's avatar
whitlocb committed
846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915
        if( varname.compare("u velocity")==0 )
        {
            vid=AMRreaderInterface::i_velo;
            cmp=0;
            sz=np;
        }
        else if( varname.compare("v velocity")==0 )
        {
            vid=AMRreaderInterface::i_velo;
            cmp=1;
            sz=np;
        }
        else if( varname.compare("w velocity")==0 )
        {
            vid=AMRreaderInterface::i_velo;
            cmp=2;
            sz=np;
        }
        else if( varname.compare("pressure")==0 )
        {
            vid=AMRreaderInterface::i_pres;
            sz=ne;
        }
        else if( varname.compare("material")==0 )
        {
            vid=AMRreaderInterface::i_matl;
            sz=ne;
        }
        else
        {
            std::string msg = "Unknown scalar variable ";
            msg+=name;
            msg+=" is specified.";
            EXCEPTION1( InvalidVariableException, msg );
        }

        vtkFloatArray* var = vtkFloatArray::New();
        var->SetNumberOfTuples( sz );
        float *val = (float *)var->GetVoidPointer(0);

        if( vid==AMRreaderInterface::i_velo )
        {
            float* buf = new float[sz*3];
            int err = GetReader()->GetInterfaceVariable( vid, buf );
            if( err!=0 )
            {
                delete [] buf;
                var->Delete();
                std::string msg = "Failed to read in scalar variable ";
                msg+=name;
                EXCEPTION1( InvalidVariableException, msg );
            }

            for( int i=0; i<np; i++ )
                val[i] = buf[3*i+cmp];

            delete [] buf;
        }
        else
        {
            int err = GetReader()->GetInterfaceVariable( vid, val );
            if( err!=0 )
            {
                var->Delete();
                std::string msg = "Failed to read in scalar variable ";
                msg+=name;
                EXCEPTION1( InvalidVariableException, msg );
            }
        }
        return var;
916 917
    }

whitlocb's avatar
whitlocb committed
918 919
    std::string msg="Unrecongnized mesh for scalar variable ";
    msg+=name;
920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940
    EXCEPTION1( InvalidVariableException, msg );
}


// ****************************************************************************
//  Method: avtAMRFileFormat::GetVectorVar
//
//  Purpose:
//      Gets a vector variable associated with this file.  Although VTK has
//      support for many different types, the best bet is vtkFloatArray, since
//      that is supported everywhere through VisIt.
//
//  Arguments:
//      domain     The index of the domain.  If there are NDomains, this
//                 value is guaranteed to be between 0 and NDomains-1,
//                 regardless of block origin.
//      varname    The name of the variable requested.
//
//  Programmer: hpan -- generated by xml2avt
//  Creation:   Mon Apr 26 10:26:32 PDT 2010
//
whitlocb's avatar
whitlocb committed
941 942 943 944
// Modifications:
//   Brad Whitlock, Fri Jun  6 11:15:50 PDT 2014
//   Free objects before throwing exceptions.
//
945 946 947 948 949
// ****************************************************************************

vtkDataArray *
avtAMRFileFormat::GetVectorVar(int domain, const char *name)
{
whitlocb's avatar
whitlocb committed
950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981
    std::string mesh,varname;
    decomposeName( name, mesh, varname );
    debug2 << "geting variable " << varname << " on mesh " << mesh << "...\n";

    if( mesh.compare(amr_name)==0 )
    {
        int vid;
        if( varname.compare("velocity")==0 )
            vid=AMRreaderInterface::v_velo;
        else
        {
            std::string msg = "Unknown vector variable ";
            msg+=name;
            msg+=" is specified.";
            EXCEPTION1( InvalidVariableException, msg );
        }

        int sz = GetReader()->GetBlockSize( domain );
        vtkFloatArray* var = vtkFloatArray::New();
        var->SetNumberOfComponents(3);
        var->SetNumberOfTuples( sz );
        float *val = (float *)var->GetVoidPointer(0);

        int err = GetReader()->GetBlockVariable( domain, vid, val );
        if( err!=0 )
        {
            var->Delete();
            std::string msg="Failed to read in vector variable ";
            msg+=name;
            EXCEPTION1( InvalidVariableException, msg );
        }
        return var;
982 983 984
    }


whitlocb's avatar
whitlocb committed
985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
    if( mesh.compare(intf_name)==0 )
    {
        int np,ne;
        GetReader()->GetInterfaceSizes( &np, &ne );

        int vid;
        if( varname.compare("velocity")==0 )
            vid=AMRreaderInterface::i_velo;
        else
        {
            std::string msg = "Unknown vector variable ";
            msg+=name;
            msg+=" is specified.";
            EXCEPTION1( InvalidVariableException, msg );
        }

        vtkFloatArray* var = vtkFloatArray::New();
        var->SetNumberOfComponents(3);
        var->SetNumberOfTuples( np );
        float *val = (float *)var->GetVoidPointer(0);

        int err = GetReader()->GetInterfaceVariable( vid, val );
        if( err!=0 )
        {
            var->Delete();
            std::string msg="Failed to read in vector variable ";
            msg+=name;
            EXCEPTION1( InvalidVariableException, msg );
        }
        return var;
1015 1016
    }

whitlocb's avatar
whitlocb committed
1017 1018 1019 1020
    std::string msg="Unrecognized mesh for vector variable ";
    msg+=name;
    EXCEPTION1( InvalidVariableException, msg );
}
1021

whitlocb's avatar
whitlocb committed
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
// ****************************************************************************
// Method: avtAMRFileFormat::GetAuxiliaryData
//
// Purpose:
//   Return spatial metadata.
//
// Arguments:
//
//
// Returns:
//
// Note:
//
// Programmer: Brad Whitlock
// Creation:   Fri May 23 10:53:20 PDT 2014
//
// Modifications:
//
// ****************************************************************************

void *
avtAMRFileFormat::GetAuxiliaryData(const char *var, int domain,
                                   const char *type, void *args, DestructorFunction &df)
{
    void *retval = 0;

    if(enableAMR && amr_name.compare(var) == 0)
    {
        if(strcmp(type, AUXILIARY_DATA_SPATIAL_EXTENTS) == 0)
        {
            avtIntervalTree *itree = new avtIntervalTree(GetReader()->GetNumberOfBlocks(), 3);
            for(int dom = 0; dom < GetReader()->GetNumberOfBlocks(); ++dom)
            {
                int blkdim[3]= {0,0,0};
                GetReader()->GetBlockDimensions(dom, blkdim);

                float xs[3]= {0.f,0.f,0.f}, dx[3]= {0.f,0.f,0.f};
miller86's avatar
miller86 committed
1059
                GetReader()->GetBlockMesh(dom, xs, dx);
whitlocb's avatar
whitlocb committed
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074

                double extents[6];
                extents[0] = xs[0];
                extents[1] = xs[0] + blkdim[0] * dx[0];
                extents[2] = xs[1];
                extents[3] = xs[1] + blkdim[1] * dx[1];
                extents[4] = xs[2];
                extents[5] = xs[2] + blkdim[2] * dx[2];

                itree->AddElement(dom, extents);
            }
            itree->Calculate(true);
            retval = (void *)itree;
            df = avtIntervalTree::Destruct;
        }
1075 1076
    }

whitlocb's avatar
whitlocb committed
1077 1078
    return retval;

1079
}