Updates will be applied April 15th at 12pm EDT (UTC-0400). GitLab could be a little slow between 12 - 12:45pm EDT.

NetworkManager.C 55.2 KB
Newer Older
hrchilds's avatar
hrchilds committed
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
#include <NetworkManager.h>
#include <DataNetwork.h>
#include <DebugStream.h>
#include <avtDatabaseFactory.h>
#include <LoadBalancer.h>
#include <MaterialAttributes.h>
#include <ImproperUseException.h>
#include <DatabaseException.h>
#include <NoInputException.h>
#include <avtPluginFilter.h>
#include <OperatorPluginManager.h>
#include <OperatorPluginInfo.h>
#include <PickAttributes.h>
#include <avtBinaryAddFilter.h>
#include <avtBinarySubtractFilter.h>
#include <avtBinaryMultiplyFilter.h>
#include <avtBinaryDivideFilter.h>
#include <avtBinaryPowerFilter.h>
#include <avtSinFilter.h>
#include <avtArctanFilter.h>
#include <avtArcsinFilter.h>
#include <avtArccosFilter.h>
#include <avtCosFilter.h>
#include <avtDegreeToRadianFilter.h>
#include <avtGradientFilter.h>
#include <avtMagnitudeFilter.h>
#include <avtNeighborFilter.h>
#include <avtNodeDegreeFilter.h>
#include <avtNullData.h>
#include <avtRadianToDegreeFilter.h>
#include <avtRevolvedSurfaceArea.h>
#include <avtRevolvedVolume.h>
#include <avtTanFilter.h>
#include <avtUnaryMinusFilter.h>
#include <avtAbsValFilter.h>
#include <avtNaturalLogFilter.h>
#include <avtBase10LogFilter.h>
#include <avtSquareRootFilter.h>
#include <avtSquareFilter.h>
#include <avtPolarCoordinatesFilter.h>
hrchilds's avatar
hrchilds committed
41
#include <avtVectorCrossProductFilter.h>
hrchilds's avatar
hrchilds committed
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 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 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 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 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
#include <avtVectorComponent1Filter.h>
#include <avtVectorComponent2Filter.h>
#include <avtVectorComponent3Filter.h>
#include <avtMeshXCoordinateFilter.h>
#include <avtMeshYCoordinateFilter.h>
#include <avtMeshZCoordinateFilter.h>
#include <avtProcessorIdFilter.h>
#include <avtRandomFilter.h>
#include <avtDataObjectQuery.h>
#include <avtCompactnessQuery.h>
#include <avtEulerianQuery.h>
#include <avtLocateCellQuery.h>
#include <avtPickQuery.h>
#include <avtSourceFromAVTImage.h>
#include <avtSourceFromImage.h>
#include <avtSourceFromNullData.h>
#include <avtTotalSurfaceAreaQuery.h>
#include <avtTotalVolumeQuery.h>
#include <avtTotalRevolvedVolumeQuery.h>
#include <avtTotalRevolvedSurfaceAreaQuery.h>
#include <avtWholeImageCompositer.h>
#include <avtPlot.h>
#include <avtDegreeFilter.h>
#include <avtVMetrics.h>
#include <VisWindow.h>
#include <PlotPluginManager.h>
#include <PlotPluginInfo.h>

// ****************************************************************************
//  Method: NetworkManager default constructor
//
//  Programmer: Jeremy Meredith
//  Creation:   September 29, 2000
//
//  Modifications:
//
//    Kathleen Bonnell, Tue Nov 20 12:35:54 PST 2001
//    Initialize requireOriginalCells. 
//
// ****************************************************************************
NetworkManager::NetworkManager(void) : virtualDatabases()
{
    workingNet = NULL;
    loadBalancer = NULL;
    requireOriginalCells = false;

    // stuff to support scalable rendering
    {
       viswin = new VisWindow(true);
       AnnotationAttributes newAtts = *(viswin->GetAnnotationAtts());
       newAtts.SetUserInfoFlag(false);
       newAtts.SetDatabaseInfoFlag(false);
       newAtts.SetLegendInfoFlag(false);
       newAtts.SetTriadFlag(false);
       newAtts.SetBboxFlag(false);
       newAtts.SetAxesFlag(false);
       viswin->SetAnnotationAtts(&newAtts);
       viswin->DisableUpdates();
    }
}

// ****************************************************************************
//  Method: NetworkManager destructor
//
//  Programmer: Jeremy Meredith
//  Creation:   September 29, 2000
//
//  Modifications:
//      Sean Ahern, Fri May 24 11:12:25 PDT 2002
//      Got rid of deletion of the database cache, since all DBs in there
//      will be deleted by deleting the network cache.
//
// ****************************************************************************
NetworkManager::~NetworkManager(void)
{
    for (int i = 0; i < networkCache.size(); i++)
        delete networkCache[i];
}

// ****************************************************************************
//  Method: NetworkManager::ClearAllNetworks
//
//  Purpose:
//      Clears out the caches.  Really only used for checking for memory leaks.
//
//  Programmer: Hank Childs
//  Creation:   January 7, 2001
//
//  Modifications:
//      Sean Ahern, Tue May 14 14:35:42 PDT 2002
//      Retrofitted for my new network management techniques.
//
//      Sean Ahern, Tue May 21 16:09:56 PDT 2002
//      Told the underlying objects to free their data.
//
// ****************************************************************************
void
NetworkManager::ClearAllNetworks(void)
{
    int i;

    for (i = 0; i < databaseCache.size(); i++)
        delete databaseCache[i];
    databaseCache.clear();

    for (i = 0; i < networkCache.size(); i++)
        delete networkCache[i];
    networkCache.clear();

}

// ****************************************************************************
//  Method: NetworkManager::AddDB
//
//  Purpose:
//      Add a DB to a network.  Use the existing network, if one exists.
//
//  Programmer: Jeremy Meredith
//  Creation:   September 29, 2000
//
//  Modifications:
//
//    Jeremy Meredith, Thu Nov  2 14:07:34 PST 2000
//    Changed the file initialization.  Apparently substr() may not portably 
//    be a const method of strings....
//
//    Hank Childs, Tue Jun 12 20:01:55 PDT 2001
//    Incorporate data specifications and the load balancer.
//
//    Jeremy Meredith, Thu Jul 26 12:08:42 PDT 2001
//    Fixed a bug passing the data file name to the load balancer.
//
//    Hank Childs, Thu Jul 26 20:17:56 PDT 2001
//    Re-enable caching by checking data specifications.
//
//    Brad Whitlock, Mon Oct 22 18:33:37 PST 2001
//    Changed the exception keywords to macros.
//
//    Jeremy Meredith, Fri Nov  9 10:24:23 PST 2001
//    Replaced netPool with netMRU, and simply made it release its
//    data, not delete the whole network. Made is only set the data
//    specification, not the pipeline specification yet.
//
//    Hank Childs, Fri Dec 14 17:39:36 PST 2001
//    Add support for using a more compact version of sil attributes.
//
//    Sean Ahern, Mon May 13 17:24:25 PDT 2002
//    Refactored to start support for a tree-based network scheme.
//
//    Sean Ahern, Tue May 28 09:47:21 PDT 2002
//    Added tree-based networks.
//
//    Brad Whitlock, Fri Jun 28 14:03:49 PST 2002
//    I renamed Network to DataNetwork to make the Windows compiler happy.
//
//    Hank Childs, Thu Sep 12 11:11:16 PDT 2002
//    There was an issue with multiple networks sharing NetnodeDBs, so I
//    made them all have their own reference.
//
//    Jeremy Meredith, Thu Oct 24 16:03:39 PDT 2002
//    Added material options.
//
//    Brad Whitlock, Wed May 14 12:56:16 PDT 2003
//    I made it get the metadata and the SIL if the time is greater than 0.
//    This ensures that we pick up the metadata and SIL for the time state
//    that we're intersted in.
//
// ****************************************************************************

void
NetworkManager::AddDB(const string &filename, const string &var, int time,
                      const CompactSILRestrictionAttributes &atts,
                      const MaterialAttributes &matopts)
{
    //
    // We gotta have a load balancer
    //
    if (loadBalancer == NULL)
    {
        debug1 << "Internal error: A load balancer was never registered." <<
            endl;
        EXCEPTION0(ImproperUseException);
    }

    //
    // If this is the first thing on the network, then we're starting a new
    // network.
    //
    bool newNetwork = false;
    if (workingNet == NULL)
    {
        // Create a new network.
        workingNet = new DataNetwork;
        newNetwork = true;
    }

    //
    // Figure out how to get a database
    //

    // Generate the list of matching databases.
    std::vector<NetnodeDB*> databaseMatches;
    for (int i = 0; i < databaseCache.size(); i++)
    {
        if (databaseCache[i]->GetFilename() == filename)
            databaseMatches.push_back(databaseCache[i]);
    }

    // If this is not a new network, we better have matched a DB in the
    // cache.  If we didn't, the user is trying to use different databases,
    // something that we don't yet support.
    if (!newNetwork && databaseMatches.empty())
    {
        char error[] = "Using more than one database in an expression is not supported.";
        debug1 << error << endl;
        EXCEPTION1(ImproperUseException,error);
    }

    // Only if we have a new network do we need to load a new database.  If it's
    // an existing network, we already have the database set.
    if (newNetwork)
    {
        // databaseMatches now contains the matching databases.  If it's empty,
        // we have to create a new database.
        if (!databaseMatches.empty())
        {
            debug3 << "Using database in cache:"
                   << " filename:" << databaseMatches[0]->GetFilename().c_str()
                   << endl;

            NetnodeDB *netDB =new NetnodeDB(databaseMatches[0]->GetDatabase());
            workingNet->GetDBs().push_back(netDB);
            workingNet->AddNode(netDB);

            netDB->SetDBSpec(filename, var, time);
            databaseCache.push_back(netDB);
        }
        else
        {
            debug3 << "Loading new database" << endl;
            TRY
            {
                avtDatabase *db = NULL;
                NetnodeDB *netDB = NULL;
                const char *filename_c = filename.c_str();
                if (filename.substr(filename.length() - 6) == ".visit")
                    db = avtDatabaseFactory::VisitFile(filename_c);
                else
                    db = avtDatabaseFactory::FileList(&filename_c, 1);

                // If we want to open the file at a later timestep, get the
                // metadata and the SIL so that it contains the right data.
                if(time > 0)
                {
                    debug2 << "NetworkManager::AddDB: We were instructed to open "
                           << filename.c_str() << " at timestate=" << time
                           << " so we're reading the MetaData and SIL early."
                           << endl;
                    db->GetMetaData(time);
                    db->GetSIL(time);
                }

                netDB = new NetnodeDB(db);
                workingNet->GetDBs().push_back(netDB);
                workingNet->AddNode(netDB);
                databaseCache.push_back(netDB);

                netDB->SetDBSpec(filename, var, time);
                const   avtIOInformation & ioinfo = db->GetIOInformation();
                loadBalancer->AddDatabase(filename, ioinfo);
            }
            CATCH(DatabaseException)
            {
                debug1 << "ERROR - could not create database " << filename.c_str()
                    << endl;
                RETHROW;
            }
            ENDTRY
        }

        // Push the db onto the working list.
        workingNetnodeList.push_back(workingNet->GetDBs().back());
    }

    // Push the name onto the name stack.
    nameStack.push_back(var);
    debug5 << "NetworkManager::AddDB: Adding " << var.c_str()
           << " to the name stack" << endl;

    // Set up the data spec.
    if (newNetwork)
    {
        avtSILRestriction_p silr =
            new avtSILRestriction(workingNet->GetDBs()[0]->GetDB()->GetSIL(), atts);
        avtDataSpecification *dspec = new avtDataSpecification(var.c_str(), time, silr);
        // Set up some options from the data specification
        dspec->SetNeedMixedVariableReconstruction(matopts.GetForceMIR());
        dspec->SetNeedSmoothMaterialInterfaces(matopts.GetSmoothing());
        dspec->SetNeedCleanZonesOnly(matopts.GetCleanZonesOnly());
        workingNet->SetDataSpec(dspec);
    }
    else
    {
        // Check if the dspec already knows about our variable.
        avtDataSpecification_p dspec = workingNet->GetDataSpec();
        bool found = false;

        // First, check the normal variable.
        if (strcmp(dspec->GetVariable(), var.c_str()) == 0)
            found = true;
        // Now check the secondary variables.
        if (dspec->HasSecondaryVariable(var.c_str()))
            found = true;
        // If we didn't find it in the dspec, add it.
        if (!found)
            dspec->AddSecondaryVariable(var.c_str());
    }

    // The plot starts out as NULL.
    workingNet->SetPlot(NULL);
}

// ****************************************************************************
// Method: NetworkManager::AddDB
//
// Purpose: 
//    Open a DB and add it to the database cache.
//
// Arguments:
//    filename : The database filename.
//    time     : The timestep that we want to examine.
//
// Note:       This code was adapted from the other AddDB method.
//
// Programmer: Brad Whitlock
// Creation:   Tue Dec 10 15:02:22 PST 2002
//
// Modifications:
//   Brad Whitlock, Wed May 14 12:56:16 PDT 2003
//   I made it get the metadata and the SIL if the time is greater than 0.
//   This ensures that we pick up the metadata and SIL for the time state
//   that we're intersted in.
//   
// ****************************************************************************
void
NetworkManager::AddDB(const string &filename, int time)
{
    //
    // We gotta have a load balancer
    //
    if (loadBalancer == NULL)
    {
        debug1 << "Internal error: A load balancer was never registered."
               << endl;
        EXCEPTION0(ImproperUseException);
    }

    // Generate the list of matching databases.
    std::vector<NetnodeDB*> databaseMatches;
    for (int i = 0; i < databaseCache.size(); i++)
    {
        if (databaseCache[i]->GetFilename() == filename)
            databaseMatches.push_back(databaseCache[i]);
    }

    // If no cached databases matched the filename, open a new database.
    if (databaseMatches.empty())
    {
        debug3 << "Loading new database" << endl;
        TRY
        {
            // Open the database
            avtDatabase *db = NULL;
            const char *filename_c = filename.c_str();
            if (filename.substr(filename.length() - 6) == ".visit")
                db = avtDatabaseFactory::VisitFile(filename_c);
            else
                db = avtDatabaseFactory::FileList(&filename_c, 1);

            // If we want to open the file at a later timestep, get the
            // metadata and the SIL so that it contains the right data.
            if(time > 0)
            {
                debug2 << "NetworkManager::AddDB: We were instructed to open "
                       << filename.c_str() << " at timestate=" << time
                       << " so we're reading the MetaData and SIL early."
                       << endl;
                db->GetMetaData(time);
                db->GetSIL(time);
            }

            // Create a netnode using the opened database and stick it in
            // the database cache.
            NetnodeDB *netDB = new NetnodeDB(db);
            netDB->SetDBSpec(filename, "", time);
            databaseCache.push_back(netDB);

            // Add the database to the load balancer.
            const   avtIOInformation & ioinfo = db->GetIOInformation();
            loadBalancer->AddDatabase(filename, ioinfo);
        }
        CATCH(DatabaseException)
        {
            debug1 << "ERROR - could not create database " << filename.c_str()
                   << endl;
            RETHROW;
        }
        ENDTRY
    }
}

// ****************************************************************************
// Method: NetworkManager::DefineDB
//
// Purpose: 
//   Defines a new database with the specified name and list of files.
//
// Arguments:
//    dbName   : The database name.
//    files    : The list of files that make up the database.
//    time     : The timestep that we want to examine.
//   
// Programmer: Brad Whitlock
// Creation:   Tue Mar 25 13:41:08 PST 2003
//
// Modifications:
//   Brad Whitlock, Wed May 14 12:56:16 PDT 2003
//   I made it get the metadata and the SIL if the time is greater than 0.
//   This ensures that we pick up the metadata and SIL for the time state
//   that we're intersted in.
//
// ****************************************************************************
void
NetworkManager::DefineDB(const string &dbName, const string &dbPath,
    const stringVector &files, int time)
{
    //
    // We gotta have a load balancer
    //
    if (loadBalancer == NULL)
    {
        debug1 << "Internal error: A load balancer was never registered."
               << endl;
        EXCEPTION0(ImproperUseException);
    }

    // Prepend the path to the files.
    stringVector filesWithPath;
    filesWithPath.reserve(files.size());
    int i;
    for(i = 0; i < files.size(); ++i)
        filesWithPath.push_back(dbPath + files[i]);

    //
    // If the database is in the virtualDatabases map, then check the
    // new list of files against the old list of files.
    //
    StringVectorMap::const_iterator it = virtualDatabases.find(dbName);
    if(it != virtualDatabases.end())
    {
        // If the list of files is the same, return because we don't
        // need to change the list of files. If the list of files differ
        // then replace the list of files.
        if(filesWithPath == it->second)
        {
            debug3 << "NetworkManager::DefineDB: No need to change "
                   << "virtual database definition for "
                   << dbName.c_str() << endl;

            // The definition is the same so we don't need to change it
            // but we have to make sure that it exists in the database
            // cache or else we'll have to define it. It's possible that
            // a clear cache rpc could have purged it from the database
            // cache.
            bool found = false;
            for(i = 0; i < databaseCache.size() && !found; ++i)
                found = (databaseCache[i]->GetFilename() == dbName);

            // If the database was in the database cache, we can return.
            if(found)
                return;
        }
        else
        {
            debug3 << "NetworkManager::DefineDB: Changed "
                   << "virtual database definition for "
                   << dbName.c_str() << endl;
            virtualDatabases[dbName] = filesWithPath;
        }
    }

    //
    // Remove from the database cache databases that have the same name.
    //
    std::vector<NetnodeDB*> databaseBadMatches;
    for (i = 0; i < databaseCache.size(); i++)
    {
        if (databaseCache[i]->GetFilename() != dbName)
            databaseBadMatches.push_back(databaseCache[i]);
    }
    databaseCache = databaseBadMatches;

    const char **names = 0;
    TRY
    {
        // Open the database
        avtDatabase *db = NULL;
        const char *dbName_c = dbName.c_str();

        if (filesWithPath.size() > 0)
        {
            // Make an array of pointers that we can pass to the database
            // factory so we can create a database based on a virtual
            // file.
            names = new const char *[filesWithPath.size()];
            for(int i = 0; i < filesWithPath.size(); ++i)
                names[i] = filesWithPath[i].c_str();
            db = avtDatabaseFactory::FileList(names, filesWithPath.size());
            delete [] names;
            names = 0;

            // Add the virtual database to the virtual database map.
            virtualDatabases[dbName] = filesWithPath;
            debug3 << "NetworkManager::DefineDB: Added new virtual database "
                   << "definition for " << dbName.c_str() << endl;
        }
        else if (dbName.substr(dbName.length() - 6) == ".visit")
            db = avtDatabaseFactory::VisitFile(dbName_c);
        else
            db = avtDatabaseFactory::FileList(&dbName_c, 1);

        // If we want to open the file at a later timestep, get the
        // metadata and the SIL so that it contains the right data.
        if(time > 0)
        {
            debug2 << "NetworkManager::DefineDB: We were instructed to define "
                       << dbName.c_str() << " at timestate=" << time
                       << " so we're reading the MetaData and SIL early."
                       << endl;
            db->GetMetaData(time);
            db->GetSIL(time);
        }

        // Create a netnode using the opened database and stick it in
        // the database cache.
        NetnodeDB *netDB = new NetnodeDB(db);
        netDB->SetDBSpec(dbName, "", time);
        databaseCache.push_back(netDB);

        // Add the database to the load balancer.
        const   avtIOInformation & ioinfo = db->GetIOInformation();
        loadBalancer->AddDatabase(dbName, ioinfo);
    }
    CATCH(DatabaseException)
    {
        delete [] names;
        debug1 << "ERROR - could not define database " << dbName.c_str()
               << endl;
        RETHROW;
    }
    ENDTRY 
}

// ****************************************************************************
//  Method: NetworkManager::AddFilter
//
//  Purpose:
//      Create a new filter and add it to the network
//
//  Programmer: Jeremy Meredith
//  Creation:   February 28, 2001
//
//  Modifications:
//    Jeremy Meredith, Sun Mar  4 16:58:13 PST 2001
//    Made it use (a new) PluginManager to create the operator.
//
//    Hank Childs, Thu Apr 12 15:34:22 PDT 2001
//    Added check to make sure that there were enough filters to compare to.
//
//    Jeremy Meredith, Thu Jul 26 03:39:21 PDT 2001
//    Added support for the new (real) operator plugin manager.
//
//    Jeremy Meredith, Fri Sep 28 13:32:56 PDT 2001
//    Made use of plugin manager keying off of ids instead of indexes.
//
//    Jeremy Meredith, Fri Nov  9 10:25:46 PST 2001
//    Added an error check, and renamed type to filtertype.
//
//    Sean Ahern, Tue May 28 08:52:48 PDT 2002
//    Added binary operators.
//
// ****************************************************************************
void
NetworkManager::AddFilter(const string &filtertype,
                          const AttributeGroup *atts,
                          const unsigned int nInputs)
{
    // Check that we have a network to work on.
    if (workingNet == NULL)
    {
        debug1 << "Adding a filter to a non-existent network." << endl;
        EXCEPTION0(ImproperUseException);
    }

    avtPluginFilter *f = OperatorPluginManager::Instance()->
        GetEnginePluginInfo(filtertype)->AllocAvtPluginFilter();
    f->SetAtts(atts);
    if (nInputs != 1)
    {
        debug1 << "Unable to handle filters with nInputs (" << nInputs << ") > 1"
            << endl;
        EXCEPTION0(ImproperUseException);
    }

    NetnodeFilter *filt = new NetnodeFilter(f, filtertype);
    std::vector<Netnode*> &filtInputs = filt->GetInputNodes();
    for (int i = 0; i < nInputs; i++)
    {
        // Pull a node off the working list, and push it onto the filter
        // inputs.
        Netnode *n = workingNetnodeList.back();
        workingNetnodeList.pop_back();
        filtInputs.push_back(n);
    }
    // Push the filter onto the working list.
    workingNetnodeList.push_back(filt);
    workingNet->AddNode(filt);
}

// ****************************************************************************
//  Method: NetworkManager::AddNamedFunction
//
//  Purpose:
//     Determines what filter to apply based on a function name and a
//     number of arguments.
//
//  Programmer: Sean Ahern
//  Creation:   Fri Apr 19 13:46:15 PDT 2002
//
//  Modifications:
//    Hank Childs, Fri Jun  7 14:20:41 PDT 2002
//    Added expression for degrees.
//
//    Brad Whitlock, Fri Jun 28 14:11:51 PST 2002
//    Made it work on Windows.
//
//    Akira Haddox, Thu Aug 15 16:29:30 PDT 2002
//    Added expressions for gradient, magnitude.
//
//    Akira Haddox Mon Aug 19 15:40:17 PDT 2002
//    Added verdict mesh quality expressions.
//
//    Akira Haddox, Thu Aug 22 08:37:28 PDT 2002
//    Added expressions for eulerian, neighbor, nodeDegree.
//
//    Hank Childs, Sun Sep  8 21:42:23 PDT 2002
//    Added revolved volume.
//
//    Kathleen Bonnell, Fri Nov 15 17:05:43 PST 2002 
//    Removed eulerian, it is now a query, not an expression
//
//    Hank Childs, Mon Nov 18 11:57:52 PST 2002
//    Added degree to radian, radian to degree.
//
//    Hank Childs, Mon Nov 18 13:51:24 PST 2002
//    Added polar coordinates, and vector component expressions.
//
//    Hank Childs, Fri Mar  7 09:43:21 PST 2003
//    Added 'random' expression.
//
//    Hank Childs, Tue Mar 18 21:36:26 PST 2003
//    Added revolved surface area.
//
// ****************************************************************************
void
NetworkManager::AddNamedFunction(const std::string &functionName, const int nargs)
{
    // Can we even add filters?
    if (workingNet == NULL)
    {
        debug1 << "Trying to add a filter to a non-existent network." << endl;
        EXCEPTION0(ImproperUseException);
    }

    // We know we can add a filter.  Figure out which one.
    avtExpressionFilter *f = NULL;
    if (nargs == 1)
    {
        avtSingleInputExpressionFilter *single = NULL;
        if (functionName == "-")
            single = new avtUnaryMinusFilter();
        else if (functionName == "sin")
            single = new avtSinFilter();
        else if (functionName == "cos")
            single = new avtCosFilter();
        else if (functionName == "tan")
            single = new avtTanFilter();
        else if (functionName == "atan")
            single = new avtArctanFilter();
        else if (functionName == "asin")
            single = new avtArcsinFilter();
        else if (functionName == "acos")
            single = new avtArccosFilter();
        else if (functionName == "deg2rad")
            single = new avtDegreeToRadianFilter();
        else if (functionName == "rad2deg")
            single = new avtRadianToDegreeFilter();
        else if (functionName == "abs")
            single = new avtAbsValFilter();
        else if (functionName == "ln")
            single = new avtNaturalLogFilter();
        else if ((functionName == "log") || (functionName == "log10"))
            single = new avtBase10LogFilter();
        else if (functionName == "sqrt")
            single = new avtSquareRootFilter();
        else if ((functionName == "sq") || (functionName == "sqr"))
            single = new avtSquareFilter();
        else if (functionName == "degree")
            single = new avtDegreeFilter();
        else if (functionName == "polar")
            single = new avtPolarCoordinatesFilter();
        else if (functionName == "coord0" || functionName == "X")
            single = new avtMeshXCoordinateFilter();
        else if (functionName == "coord1" || functionName == "Y")
            single = new avtMeshYCoordinateFilter();
        else if (functionName == "coord2" || functionName == "Z")
            single = new avtMeshZCoordinateFilter();
        else if (functionName == "comp0")
            single = new avtVectorComponent1Filter();
        else if (functionName == "comp1")
            single = new avtVectorComponent2Filter();
        else if (functionName == "comp2")
            single = new avtVectorComponent3Filter();
        else if (functionName == "procid")
            single = new avtProcessorIdFilter();
        else if (functionName == "random")
            single = new avtRandomFilter();
        else if (functionName == "gradient")
            single = new avtGradientFilter();
        else if (functionName == "magnitude")
            single = new avtMagnitudeFilter();
        else if (functionName == "neighbor")
            single = new avtNeighborFilter();
        else if (functionName == "node_degree")
            single = new avtNodeDegreeFilter();
        // Begin Verdict Metrics
        else if (functionName == "area")
            single = new avtVMetricArea();
        else if (functionName == "aspect")
            single = new avtVMetricAspectRatio();
        else if (functionName == "skew")
            single = new avtVMetricSkew();
        else if (functionName == "taper")
            single = new avtVMetricTaper();
        else if (functionName == "volume")
            single = new avtVMetricVolume();
        else if (functionName == "stretch")
            single = new avtVMetricStretch();
        else if (functionName == "diagonal")
            single = new avtVMetricDiagonal();
        else if (functionName == "dimension")
            single = new avtVMetricDimension();
        else if (functionName == "oddy")
            single = new avtVMetricOddy();
        else if (functionName == "condition")
            single = new avtVMetricCondition();
        else if (functionName == "jacobian")
            single = new avtVMetricJacobian();
        else if (functionName == "scaled_jacobian")
            single = new avtVMetricScaledJacobian();
        else if (functionName == "shear")
            single = new avtVMetricShear();
        else if (functionName == "shape")
            single = new avtVMetricShape();
        else if (functionName == "relative_size")
            single = new avtVMetricRelativeSize();
        else if (functionName == "shape_and_size")
            single = new avtVMetricShapeAndSize();
        else if (functionName == "aspect_gamma")
            single = new avtVMetricAspectGamma();
        else if (functionName == "warpage")
            single = new avtVMetricWarpage();
        else if (functionName == "largest_angle")
            single = new avtVMetricLargestAngle();
        else if (functionName == "smallest_angle")
            single = new avtVMetricSmallestAngle();
        else if (functionName == "revolved_volume")
            single = new avtRevolvedVolume;
        else if (functionName == "revolved_surface_area")
            single = new avtRevolvedSurfaceArea;
        else
        {
            std::string error;
            error = std::string("NetworkManager::AddNamedFunction: "
                                "Unknown function: \"") + 
                    functionName + std::string("\".");
            EXCEPTION1(VisItException, error);
        }

        // Set the variable the expression should process
        string inputName = nameStack.back();
        nameStack.pop_back();
        debug5 << "NetworkManager::AddNamedFunction: Pulling "
               << inputName.c_str() << " from the name stack" << endl;
        single->AddInputVariableName(inputName.c_str());

        // Set the variable the expression should output
        string outputName = functionName + "(" + inputName + ")";
        single->SetOutputVariableName(outputName.c_str());
        nameStack.push_back(outputName);
        debug5 << "NetworkManager::AddNamedFunction: Adding "
               << outputName.c_str() << " to the name stack" << endl;

        f = single;
    }
    else if (nargs == 2)
    {
        avtMultipleInputExpressionFilter *multiple = NULL;
        if (functionName == "+")
            multiple = new avtBinaryAddFilter();
        else if (functionName == "-")
            multiple = new avtBinarySubtractFilter();
        else if (functionName == "*")
            multiple = new avtBinaryMultiplyFilter();
        else if (functionName == "/")
            multiple = new avtBinaryDivideFilter();
        else if (functionName == "^")
            multiple = new avtBinaryPowerFilter();
hrchilds's avatar
hrchilds committed
870 871
        else if (functionName == "cross")
            multiple = new avtVectorCrossProductFilter();
hrchilds's avatar
hrchilds committed
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 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 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 982 983 984 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 1015 1016 1017 1018 1019 1020 1021 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 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383
        else
        {
            std::string error;
            error = std::string("NetworkManager::AddNamedFunction: "
                                "Unknown function: \"")
                    + functionName + std::string("\".");
            EXCEPTION1(VisItException, error);
        }

        // Set the variables the expression should process
        string inputName2 = nameStack.back();
        nameStack.pop_back();
        debug5 << "NetworkManager::AddNamedFunction: Pulling "
               << inputName2.c_str() << " from the name stack" << endl;
        string inputName1 = nameStack.back();
        nameStack.pop_back();
        debug5 << "NetworkManager::AddNamedFunction: Pulling "
               << inputName1.c_str() << " from the name stack" << endl;
        multiple->AddInputVariableName(inputName1.c_str());
        multiple->AddInputVariableName(inputName2.c_str());

        // Set the variable the expression should output
        string outputName = inputName1 + functionName + inputName2;
        multiple->SetOutputVariableName(outputName.c_str());
        nameStack.push_back(outputName);
        debug5 << "NetworkManager::AddNamedFunction: Adding "
               << outputName.c_str() << " to the name stack" << endl;

        f = multiple;
    }
    else
    {
        char num[20];
        sprintf(num, "%d", nargs);
        std::string error;
        error = std::string("NetworkManager::AddNamedFunction: "
                            "No filters named \"") + 
                functionName + std::string("\" that support ") +
                std::string(num) + std::string(" arguments.");
        EXCEPTION1(VisItException, error);
    }

    NetnodeFilter *filt = new NetnodeFilter(f, functionName);
    std::vector<Netnode*> &filtInputs = filt->GetInputNodes();

#if 0
    for(int i=0;i<nargs;i++)
    {
#endif
        Netnode *n = workingNetnodeList.back();
        workingNetnodeList.pop_back();
        filtInputs.push_back(n);
#if 0
    }
#endif

    workingNetnodeList.push_back(filt);
}

// ****************************************************************************
//  Method:  NetworkManager::MakePlot
//
//  Purpose:
//    Create the plot for the network.
//
//  Programmer:  Jeremy Meredith
//  Creation:    March  1, 2001
//
//  Modifications:
//    Jeremy Meredith, Sun Mar  4 16:58:13 PST 2001
//    Made it use (a new) PluginManager to create the plot.
//
//    Jeremy Meredith, Tue Mar 20 12:05:50 PST 2001
//    Made it use the new plugin manager.  For now we do a reverse lookup
//    of the name to an index.
//
//    Jeremy Meredith, Fri Sep 28 13:32:56 PDT 2001
//    Made use of plugin manager keying off of ids instead of indexes.
//
//    Jeremy Meredith, Fri Nov  9 10:26:18 PST 2001
//    Allowed it to reuse plots so we can cache whole networks again.
//
//    Jeremy Meredith, Fri Feb  8 10:37:59 PST 2002
//    Made sure the total number of filters was the same when matching
//    networks (since when a plot is applied, no more filter can be).
//
// ****************************************************************************
void
NetworkManager::MakePlot(const string &id, const AttributeGroup *atts)
{
    if (workingNet == NULL)
    {
        debug1 << "Adding a plot to a non-existent network." << endl;
        EXCEPTION0(ImproperUseException);
    }

    if (*workingNet->GetPlot() != NULL)
    {
        debug1 << "Trying to stipulate plot type more than once." << endl;
        EXCEPTION0(ImproperUseException);
    }

    // Checking to see if the network has been built successfully.
    if (workingNetnodeList.size() != 1)
    {
        debug1 << "Network still-in progress.  Filter required." << endl;
        EXCEPTION0(ImproperUseException);
    }

    avtPlot *p = PlotPluginManager::Instance()->
                                GetEnginePluginInfo(id)->AllocAvtPlot();
    workingNet->SetPlot(p);
    workingNet->GetPlot()->SetAtts(atts);
    workingNet->SetPlottype(id);
}

// ****************************************************************************
//  Method:  NetworkManager::EndNetwork
//
//  Purpose:
//    Closes off the new network, assigns a pipeline specification, and a
//    network id.
//
//  Programmer:  Jeremy Meredith
//  Creation:    November  9, 2001
//
// ****************************************************************************
int
NetworkManager::EndNetwork(void)
{
    // Checking to see if the network has been built successfully.
    if (workingNetnodeList.size() != 1)
    {
        debug1 << "Network building still in progress.  Filter required to "
               << "absorb" << workingNetnodeList.size() << " nodes."  << endl;

        EXCEPTION0(ImproperUseException);
    }

    // set the pipeline specification
    int pipelineIndex =
        loadBalancer->AddPipeline(workingNet->GetDBs()[0]->GetFilename());
    avtPipelineSpecification_p pspec =
        new avtPipelineSpecification(workingNet->GetDataSpec(), pipelineIndex);
    workingNet->SetPipelineSpec(pspec);

    // Connect the workingNet to the workingNetnodeList.
    workingNet->SetTerminalNode(workingNetnodeList[0]);

    // Push the working net onto the network caches.
    workingNet->SetID(networkCache.size());
    networkCache.push_back(workingNet);

    return workingNet->GetID();
}

// ****************************************************************************
//  Method:  NetworkManager::UseNetwork
//
//  Purpose:
//    Allows a whole network to be reused automatically, without
//    needing to specify a database, filters, or plot.
//
//  Arguments:
//    id         the id of the network to reuse.
//
//  Programmer:  Jeremy Meredith
//  Creation:    November  9, 2001
//
//  Modifications:
//
//    Hank Childs, Wed Nov 21 11:41:59 PST 2001
//    Tell the load balancer to reset the pipeline if we re-use the network.
//
// ****************************************************************************
void
NetworkManager::UseNetwork(int id)
{
    if (workingNet)
    {
        debug1 << "Internal error: UseNetwork called with an open network" << endl;
        EXCEPTION0(ImproperUseException);
    }

    if (id >= networkCache.size())
    {
        debug1 << "Internal error: asked to reuse network ID (" << id
            << " >= num saved networks (" << networkCache.size() << ")"
            << endl;
        EXCEPTION0(ImproperUseException);
    }

    // re-access the given network
    workingNet = networkCache[id];
    int pipelineIndex = workingNet->GetPipelineSpec()->GetPipelineIndex();
    loadBalancer->ResetPipeline(pipelineIndex);

    if (id != workingNet->GetID())
    {
        debug1 << "Internal error: network at position[" << id <<
            "] does not have same id (" << workingNet->GetID() << ")"
            << endl;
        EXCEPTION0(ImproperUseException);
    }
}

// ****************************************************************************
//  Method: NetworkManager::GetPlot
//
//  Purpose:
//      Gets the plot for the current working network 
//
//  Programmer: Mark C. Miller 
//  Creation:   November 19, 2002
//
// ****************************************************************************
avtPlot_p
NetworkManager::GetPlot(void)
{
    if (workingNet == NULL)
    {
        EXCEPTION0(ImproperUseException);
    }

    return workingNet->GetPlot();
}

// ****************************************************************************
//  Method: NetworkManager::GetCurrentNetworkId
//
//  Purpose:
//      Gets the network id of the current network.
//
//  Programmer: Hank Childs
//  Creation:   September 9, 2002
//
// ****************************************************************************
int
NetworkManager::GetCurrentNetworkId(void)
{
    if (workingNet == NULL)
    {
        EXCEPTION0(ImproperUseException);
    }

    return workingNet->GetID();
}

// ****************************************************************************
//  Method: NetworkManager::DoneWithNetwork
//
//  Purpose:
//      Indicates that we are done with a network -- we can clean up memory, 
//      etc.
//
//  Programmer: Hank Childs
//  Creation:   September 9, 2002
//
// ****************************************************************************
void
NetworkManager::DoneWithNetwork(int id)
{
    if (id >= networkCache.size())
    {
        debug1 << "Internal error: asked to reuse network ID (" << id
            << " >= num saved networks (" << networkCache.size() << ")"
            << endl;
        EXCEPTION0(ImproperUseException);
    }

    networkCache[id]->ReleaseData();

}

// ****************************************************************************
//  Method: NetworkManager::UpdatePlotAtts
//
//  Purpose:
//      Updates the plot attributes for a network.
//
//  Arguments:
//      id      The id of the network.
//      atts    The new attributes for the plot.
//
//  Programmer: Hank Childs
//  Creation:   November 30, 2001
//
// ****************************************************************************
void
NetworkManager::UpdatePlotAtts(int id, const AttributeGroup *atts)
{
    if (id >= networkCache.size())
    {
        debug1 << "Internal error: asked to reuse network ID (" << id
            << ") >= num saved networks (" << networkCache.size() << ")"
            << endl;
        EXCEPTION0(ImproperUseException);
    }

    if (networkCache[id]->GetID() != id)
    {
        debug1 << "Internal error: network at position[" << id
            << "] does not have " << "the same id ("
            << networkCache[id]->GetID() << ")" << endl;
        EXCEPTION0(ImproperUseException);
    }

    networkCache[id]->GetPlot()->SetAtts(atts);
}

// ****************************************************************************
//  Method: NetworkManager::GetOutput
//
//  Purpose:
//      Get the output of the network and close the network.
//      Also, flush LRU items from the cache
//
//  Programmer: Jeremy Meredith
//  Creation:   September 29, 2000
//
//  Modifications:
//    Jeremy Meredith, Thu Nov  2 14:10:51 PST 2000
//    Added another sanity check.
//
//    Hank Childs, Wed Jan 17 11:05:20 PST 2001
//    Added plots.
//
//    Jeremy Meredith, Thu Mar  1 13:41:13 PST 2001
//    Disable the section of code which allows re-using of entire networks.
//    This allows us to continue caching networks, but avoids the temporary
//    problem where different networks' plots might use different values
//    but we have not checked to see if the plots are equivalent.
//    Also....  I changed the way the networks are hooked up to allow for
//    zero-length pipelines.
//
//    Hank Childs, Wed Mar  7 11:33:51 PST 2001
//    Timestep now an argument to the database instead of set as a global.
//
//    Kathleen Bonnell, Tue May  1 16:57:02 PDT 2001 
//    Added try-catch block so that exceptions could be rethrown. 
//
//    Hank Childs, Tue Jun 12 14:55:38 PDT 2001
//    Incorporate data specifications.
//
//    Jeremy Meredith, Tue Jul  3 15:18:55 PDT 2001
//    Modified to better handle interruption of pipeline execution.
//
//    Brad Whitlock, Mon Oct 22 18:33:37 PST 2001
//    Changed the exception keywords to macros.
//
//    Jeremy Meredith, Fri Nov  9 10:29:17 PST 2001
//    Moved code to finish network matching into EndNetwork.
//    Added sending of window attributes into plot->Execute.
//
//    Kathleen Bonnell, Tue Nov 20 12:35:54 PST 2001
//    Set flag in dataSpec based on value of requireOriginalCells. 
//
//    Hank Childs, Tue Jun 18 16:49:01 PDT 2002
//    Set MayRequireZones before Executing.
//
//    Mark C. Miller, 10Nov02
//    Made changes to support scalable rendering
//
//    Brad Whitlock, Thu Jan 16 11:07:10 PDT 2003
//    I replaced the return in the TRY/CATCH block with CATCH_RETURN2 so that
//    fake exceptions work again.
//
// ****************************************************************************
avtDataObjectWriter_p
NetworkManager::GetOutput(bool respondWithNullData)
{

    // Is the network complete?
    if (*(workingNet->GetPlot()) == NULL)
    {
        debug1 << "Never told what type of plot to use." << endl;
        EXCEPTION0(ImproperUseException);
    }

    TRY
    {
        // Hook up the network
        avtDataObject_p output = workingNet->GetOutput();

        workingNet->GetPipelineSpec()->GetDataSpecification()->
            SetMayRequireZones(requireOriginalCells); 

        avtDataObjectWriter_p writer = workingNet->GetWriter(output,
                                          workingNet->GetPipelineSpec(),
                                          &windowAttributes);

        if (respondWithNullData)
        {
           // get the data object at the input to the writer
           avtDataObject_p writerInput = writer->GetInput();
           avtDataset_p origDataset;
           CopyTo(origDataset, writerInput);

           // make a copy of the dataset without the data
           avtDataset_p dummyDataset = new avtDataset(origDataset, true); 
           avtDataObject_p dummyDob;
           CopyTo(dummyDob, dummyDataset);
           writer = dummyDob->InstantiateWriter();
           writer->SetInput(dummyDob);
        }


        // Zero out the workingNet.  Remember that we've already pushed it
        // onto the cache in EndNetwork.
        workingNet = NULL;
        workingNetnodeList.clear();

        // return it
        CATCH_RETURN2(1, writer);
    }
    CATCHALL(...)
    {
        // Zero out the workingNet to ensure that the exception doesn't
        // cause a crash next time we try to build a network.  Remember
        // that we've already pushed it onto the cache in EndNetwork.
        workingNet = NULL;
        workingNetnodeList.clear();

        // rethrow
        RETHROW;
    }
    ENDTRY
}

// ****************************************************************************
//  Method:  NetworkManager::Render
//
//  Purpose: do a software scalable render
//
//  Programmer:  Mark C. Miller 
//  Creation:    08Apr03 
// ****************************************************************************
avtDataObjectWriter_p
NetworkManager::Render(intVector plotIds, bool getZBuffer)
{
    DataNetwork *origWorkingNet = workingNet;

    TRY
    {
       avtDataObjectWriter_p writer;

       // put all the plot objects into the VisWindow
       viswin->DisableUpdates();
       for (int i = 0; i < plotIds.size(); i++)
       {
          // get the network output as we would normally
          workingNet = NULL;
          UseNetwork(plotIds[i]);
          DataNetwork *workingNetSaved = workingNet;
          avtDataObject_p dob = GetOutput(false)->GetInput();

          // do the part of the execute we'd do in the viewer
          avtActor_p anActor = workingNetSaved->GetPlot()->Execute(NULL, dob);

          viswin->AddPlot(anActor);
       }

       debug5 << "Starting render of " << viswin->GetNumTriangles()
              << " triangles" << endl;

       // render the image and capture it
       viswin->EnableUpdates();
       avtImage_p theImage = viswin->ScreenCapture(true);
       viswin->DisableUpdates();
       viswin->ClearPlots();

       // do the parallel composite using a 1 stage pipeline
       int imageRows, imageCols;
       viswin->GetSize(imageCols, imageRows);
       avtWholeImageCompositer imageCompositer;
       //imageCompositer.SetChunkSize(1000);
       imageCompositer.SetOutputImageSize(imageRows, imageCols);
       imageCompositer.AddImageInput(theImage, 0, 0);
       imageCompositer.SetShouldOutputZBuffer(getZBuffer);
       imageCompositer.Execute();
       avtDataObject_p compositedImageAsDataObject = imageCompositer.GetOutput();
       writer = compositedImageAsDataObject->InstantiateWriter();
       writer->SetInput(compositedImageAsDataObject);

       // return it
       CATCH_RETURN2(1, writer);

    }
    CATCHALL(...)
    {
        // rethrow
        RETHROW;
    }
    ENDTRY

    workingNet = origWorkingNet;
}

// ****************************************************************************
//  Method:  NetworkManager::SetWindowAttributes
//
//  Purpose:
//    Set the window attributes for the next plot execution.
//
//  Arguments:
//    atts       the new window attributes
//
//  Programmer:  Jeremy Meredith
//  Creation:    November  9, 2001
//
//  Modifications:
//
hrchilds's avatar
hrchilds committed
1384 1385 1386 1387 1388
//    Mark C. Miller, Mon Dec  9 17:19:02 PST 2002
//    added calls to set various parameters on the VisWindow object
//
//    Eric Brugger, Tue Jun 10 15:35:20 PDT 2003
//    I renamed camera to view normal in the view attributes.
hrchilds's avatar
hrchilds committed
1389
//
hrchilds's avatar
hrchilds committed
1390 1391 1392 1393 1394
//    Brad Whitlock, Tue Jul 1 14:19:20 PST 2003
//    I changed the code that converts ViewAttributes to avtView3D so it
//    uses convenience methods so we don't have to trapse all over the code
//    when the ViewAttributes change field names.
//
hrchilds's avatar
hrchilds committed
1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436
// ****************************************************************************
void
NetworkManager::SetWindowAttributes(const WindowAttributes &atts)
{
    viswin->SetSize(atts.GetSize()[0], atts.GetSize()[1]);

#if 0
    // What are bounds? 
    viswin->SetBounds();
    // What about 2D views?
    viswin->SetView2D();
#endif

    {  const LightList& lights = atts.GetLights();
       viswin->SetLightList(&lights);
    }

    viswin->SetBackgroundColor(atts.GetBackground()[0]/255.0,
                               atts.GetBackground()[1]/255.0,
                               atts.GetBackground()[2]/255.0);
    viswin->SetForegroundColor(atts.GetForeground()[0]/255.0,
                               atts.GetForeground()[1]/255.0,
                               atts.GetForeground()[2]/255.0);

    int bgMode = atts.GetBackgroundMode();
    if (bgMode == 0)
       viswin->SetBackgroundMode(0);
    else
    {
       viswin->SetBackgroundMode(1);
       viswin->SetGradientBackgroundColors(bgMode-1, atts.GetGradBG1()[0],
                                                     atts.GetGradBG1()[1],
                                                     atts.GetGradBG1()[2],
                                                     atts.GetGradBG2()[0],
                                                     atts.GetGradBG2()[1],
                                                     atts.GetGradBG2()[2]);
    }

    // Set the 3D view
    {
        const ViewAttributes& viewAtts = atts.GetView();
        avtView3D view3d;
hrchilds's avatar
hrchilds committed
1437
        view3d.SetFromViewAttributes(&viewAtts);
hrchilds's avatar
hrchilds committed
1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504
        viswin->SetView3D(view3d);
    }

    viswin->SetViewport(atts.GetView().GetViewportCoords()[0],
                        atts.GetView().GetViewportCoords()[1],
                        atts.GetView().GetViewportCoords()[2],
                        atts.GetView().GetViewportCoords()[3]);

    windowAttributes = atts;
}

// ****************************************************************************
//  Method:  NetworkManager::StartPick
//
//  Purpose:
//    Set the zone numbers flag in data specification so that original cell
//    numbers are propagated through the pipeline.  
//
//  Programmer:  Kathleen Bonnell 
//  Creation:    November 19, 2001
//
// ****************************************************************************
void
NetworkManager::StartPickMode(void)
{
    requireOriginalCells = true;
}

// ****************************************************************************
//  Method:  Network::StopPick
//
//  Purpose:
//    Set the zone numbers flag in data specification so that original cell
//    numbers will no longer be propagated through the pipeline.
//
//  Programmer:  Kathleen Bonnell 
//  Creation:    November 19, 2001
//
// ****************************************************************************
void
NetworkManager::StopPickMode(void)
{
    requireOriginalCells = false;
}

// ****************************************************************************
//  Method:  Network::Pick
//
//  Purpose:
//    Performs a query on the database with the PickAttributes.
//
//  Arguments:
//    id         The network to use.
//    pa         The pick attributes.
//
//  Programmer:  Kathleen Bonnell 
//  Creation:    November 20, 2001
//
//  Modifications:
//    Kathleen Bonnell, Fri Nov 15 09:07:36 PST 2002  
//    Use LocateCellQuery and PickQuery, instead of Query call to database.
//    This allows correct distribution of work in parallel.
//
//    Kathleen Bonnell, Fri Jan 31 09:36:54 PST 2003   
//    Make call to LocateCellQuery unconditional.  Put all code in 
//    one try-catch block. 
//    
hrchilds's avatar
hrchilds committed
1505 1506 1507
//    Kathleen Bonnell, Wed Jun 25 13:45:04 PDT 2003  
//    Reflect new pickAtts naming convention:  ZoneNumber is now ElementNumber. 
//
hrchilds's avatar
hrchilds committed
1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545
// ****************************************************************************
void
NetworkManager::Pick(const int id, PickAttributes *pa)
{
    if (id >= networkCache.size())
    {
        debug1 << "Internal error:  asked to use network ID (" << id << ") >= "
               << "num saved networks (" << networkCache.size() << ")" << endl;
        EXCEPTION0(ImproperUseException);
    }

    if (id != networkCache[id]->GetID())
    {
        debug1 << "Internal error: network at position[" << id << "] "
               << "does not have same id (" << networkCache[id]->GetID()
               << ")" << endl;
        EXCEPTION0(ImproperUseException);
    }

    avtDataObject_p queryInput = 
        networkCache[id]->GetPlot()->GetIntermediateDataObject();

    if (*queryInput == NULL)
    {
        debug1 << "Could not retrieve query input." << endl;
        EXCEPTION0(NoInputException);
    }

    avtLocateCellQuery *lcQ = new avtLocateCellQuery;
    avtPickQuery *pQ = new avtPickQuery;
    TRY
    {
        QueryAttributes qa;
        qa.SetRayPoint1(pa->GetRayPoint1());
        qa.SetRayPoint2(pa->GetRayPoint2());
        lcQ->SetInput(queryInput);
        lcQ->PerformQuery(&qa); 
        pa->SetDomain(qa.GetDomain());
hrchilds's avatar
hrchilds committed
1546
        pa->SetElementNumber(qa.GetZone());
hrchilds's avatar
hrchilds committed
1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666
        pa->SetCellPoint(qa.GetCellPoint());
        pa->SetPickPoint(qa.GetWorldPoint());
        delete lcQ;

        pQ->SetInput(queryInput);
        pQ->SetPickAtts(pa);
        pQ->PerformQuery(&qa); 
        *pa = *(pQ->GetPickAtts());
        delete pQ;
    }
    CATCHALL( ... )
    {
        if (lcQ != NULL)
            delete lcQ;
        if (pQ != NULL)
            delete pQ;
        RETHROW;
    }
    ENDTRY
}

// ****************************************************************************
//  Method:  Network::Query
//
//  Purpose:
//    Performs a query on the database. 
//
//  Arguments:
//    id         The network to use.
//    queryname  The name of the query to perform. 
//
//  Programmer:  Kathleen Bonnell
//  Creation:    September 16, 2002
//
//  Modifications:
//    Kathleen Bonnell, Fri Nov 15 09:07:36 PST 2002   
//    Removed unnecessary debug, cerr statements.  Added Exception if
//    query input could not be retrieved.
//
//    Kathleen Bonnell, Mon Nov 18 09:42:12 PST 2002 
//    Add Eulerian query. 
//    
//    Hank Childs, Tue Mar 18 21:36:26 PST 2003
//    Added Total Revolved Surface Area query.
//
//    Jeremy Meredith, Sat Apr 12 11:31:04 PDT 2003
//    Added compactness queries.
//
// ****************************************************************************
void
NetworkManager::Query(const int id, QueryAttributes *qa)
{
    if (id >= networkCache.size())
    {
        debug1 << "Internal error:  asked to use network ID (" << id << ") >= "
               << "num saved networks (" << networkCache.size() << ")" << endl;
        EXCEPTION0(ImproperUseException);
    }
 
    if (id != networkCache[id]->GetID())
    {
        debug1 << "Internal error: network at position[" << id << "] "
               << "does not have same id (" << networkCache[id]->GetID()
               << ")" << endl;
        EXCEPTION0(ImproperUseException);
    }

    avtDataObject_p queryInput = 
        networkCache[id]->GetPlot()->GetIntermediateDataObject();

    if (*queryInput == NULL)
    {
        debug1 << "Could not retrieve query input." << endl;
        EXCEPTION0(NoInputException);
    }

    std::string queryName = qa->GetName();
    avtDataObjectQuery *query = NULL;

    TRY
    {
        if (strcmp(queryName.c_str(), "Surface area") == 0)
        {
            query = new avtTotalSurfaceAreaQuery();
        }
        else if (strcmp(queryName.c_str(), "Volume") == 0)
        {
            query = new avtTotalVolumeQuery();
        }
        else if (strcmp(queryName.c_str(), "Revolved volume") == 0)
        {
            query = new avtTotalRevolvedVolumeQuery();
        }
        else if (strcmp(queryName.c_str(), "Revolved surface area") == 0)
        {
            query = new avtTotalRevolvedSurfaceAreaQuery();
        }
        else if (strcmp(queryName.c_str(), "Eulerian") == 0)
        {
            query = new avtEulerianQuery();
        }
        else if (strcmp(queryName.c_str(), "Compactness") == 0)
        {
            query = new avtCompactnessQuery();
        }
        if (query != NULL)
        {
            query->SetInput(queryInput);
            query->PerformQuery(qa);
            delete query;
        }
    }
    CATCHALL( ... )
    {
        if (query != NULL)
            delete query;
        RETHROW;
    }
    ENDTRY
}