Dyna3DFile.cpp 35.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
// ************************************************************************* //
//                            Dyna3DFile.C                                   //
// ************************************************************************* //

#include <Dyna3DFile.h>

#include <string.h>

#include <set>
#include <map>

#include <vtkAppendFilter.h>
#include <vtkCell.h>
#include <vtkCellType.h>
#include <vtkFloatArray.h>
#include <vtkIntArray.h>
#include <vtkUnstructuredGrid.h>

#include <avtVector.h>


#define ALL_LINES -1

#ifdef VISIT_DYNA
// We're building this code inside of VisIt.
#include <snprintf.h>
#include <DebugStream.h>
#define DEBUG_READER(A) A
#else
#define SNPRINTF snprintf
#define DEBUG_READER(A)
#endif

// ****************************************************************************
//  Method: Dyna3DFile constructor
//
//  Programmer: Brad Whitlock
//  Creation:   Mon Nov 27 16:25:13 PST 2006
//
//  Modifications:
//
// ****************************************************************************

Dyna3DFile::Dyna3DFile()
{
    ugrid = 0;
    memset(&cards, 0, sizeof(ControlCards_t));
    matNumbers = 0;
    line = 0;
    velocity = 0;
}

// ****************************************************************************
//  Method: Dyna3DFile copy constructor
//
//  Programmer: Brad Whitlock
//  Creation:   Thu Jun 10 14:11:46 PST 2010
//
//  Modifications:
//
// ****************************************************************************

Dyna3DFile::Dyna3DFile(const Dyna3DFile &obj) : ugrid(0), cards(obj.cards),
    materialCards(obj.materialCards), matNumbers(0), line(0), velocity(0)
{
    if(obj.ugrid != 0)
    {
        ugrid = vtkUnstructuredGrid::New();
        ugrid->DeepCopy(obj.ugrid);
    
        if(obj.matNumbers != 0)
        {
            matNumbers = new int[ugrid->GetNumberOfCells()];
            memcpy(matNumbers, obj.matNumbers, sizeof(int) * ugrid->GetNumberOfCells());
        }
        else
            matNumbers = 0;
 
        if(obj.velocity != 0)
        {
            velocity = vtkFloatArray::New();
            velocity->DeepCopy(obj.velocity);
        }
    }
}

// ****************************************************************************
// Method: Dyna3DFile::~Dyna3DFile
//
// Purpose: 
//   Destructor for Dyna3DFile class.
//
// Programmer: Brad Whitlock
// Creation:   Mon Nov 27 16:25:13 PST 2006
//
// Modifications:
//   
// ****************************************************************************

Dyna3DFile::~Dyna3DFile()
{
    FreeUpResources();
}

bool
Dyna3DFile::HasBeenRead() const
{
    return ugrid != 0;
}

bool
Dyna3DFile::Read(const std::string &filename)
{
    return ReadFile(filename, ALL_LINES);
}

bool
Dyna3DFile::Identify(const std::string &filename)
{
    return ReadFile(filename, 100);
}

// ****************************************************************************
//  Method: Dyna3DFile::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: Brad Whitlock
//  Creation:   Mon Nov 27 16:25:13 PST 2006
//
// ****************************************************************************

void
Dyna3DFile::FreeUpResources(void)
{
    if(ugrid)
    {
        ugrid->Delete();
        ugrid = 0;
    }

    if(matNumbers != 0)
    {
        delete [] matNumbers;
        matNumbers = 0;
    }

    if(line != 0)
    {
        delete [] line;
        line = 0;
    }

    if(velocity != 0)
    {
        velocity->Delete();
        velocity = 0;
    }
}

// ****************************************************************************
// Method: Dyna3DFile::GetMaterials
//
// Purpose: 
//   Get the material numbers and names.
//
// Arguments:
//
// Returns:    
//
// Note:       
//
// Programmer: Brad Whitlock
// Creation:   Fri Mar 19 10:36:42 PDT 2010
//
// Modifications:
//   
// ****************************************************************************

void
Dyna3DFile::GetMaterials(intVector &matnos, stringVector &matnames, doubleVector &matdens)
{
187
    for(size_t i = 0; i < materialCards.size(); ++i)
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
    {
        matnos.push_back(materialCards[i].materialNumber);
        matnames.push_back(materialCards[i].materialName);
        matdens.push_back(materialCards[i].density);
    }
}

const MaterialPropertiesVector &
Dyna3DFile::GetMaterials() const
{
    return materialCards;
}

int
Dyna3DFile::GetNumMaterials() const
{
bonnell's avatar
bonnell committed
204
    return (int)materialCards.size();
205 206 207 208 209 210
}

MaterialProperties
Dyna3DFile::GetMaterial(int i) const
{
    MaterialProperties mat;
211
    if(i >= 0 && i < (int)materialCards.size())
212 213 214 215 216 217 218 219 220
    {
        mat = materialCards[i];
    }
    return mat;  
}

void
Dyna3DFile::SetMaterial(int i, const MaterialProperties &mat)
{
221
    if(i >= 0 && i < (int)materialCards.size())
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
    {
        materialCards[i] = mat;
    }
}

// ****************************************************************************
// Method: Dyna3DFile::ReadControlCards
//
// Purpose: 
//   Reads all of the control cards.
//
// Arguments:
//   ifile : The file stream from which to read.
//
// Returns:    True if the file looks like DYNA3D; False otherwise.
//
// Note:       
//
// Programmer: Brad Whitlock
// Creation:   Mon Nov 27 16:23:44 PST 2006
//
// Modifications:
//   
// ****************************************************************************

bool
Dyna3DFile::ReadControlCards(ifstream &ifile)
{
    bool ret = ReadControlCard2(ifile);
    ReadControlCard3(ifile);
    ReadControlCard4(ifile);
    ReadControlCard5(ifile);
    ReadControlCard6(ifile);
    ReadControlCard7(ifile);
    ReadControlCard8(ifile);
    ReadControlCard9(ifile);
    return ret;
}

// ****************************************************************************
// Method: Dyna3DFile::ReadControlCardsNewFormat
//
// Purpose: 
//   Read the new format of control cards.
//
// Arguments:
//   ifile : The file stream from which to read.
//
// Returns:    True if the file looks like DYNA3D; False otherwise.
//
// Note:       
//
// Programmer: Brad Whitlock
// Creation:   Fri Dec 16 16:37:07 PST 2011
//
// Modifications:
//   
// ****************************************************************************

bool
Dyna3DFile::ReadControlCardsNewFormat(ifstream &ifile)
{
    DEBUG_READER(debug5 << "Dyna3DFile::ReadControlCardsNewFormat" << endl;);
    bool nmmat = false, numnp = false, numelh = false;
    int i = 2;
    do 
    {
        GetLine(ifile);
        DEBUG_READER(debug5 << "line " << (i++) << ": " << line << endl;)

        if(line[0] == '*')
            continue;

        char *ns = line;
        while(ns[0] == ' ')
            ns++;

        if(strstr(line, "nmmat") != NULL)
            nmmat |= (sscanf(ns, "nmmat %d", &cards.card2.nMaterials) == 1);
        else if(strstr(line, "numnp") != NULL)
            numnp |= (sscanf(ns, "numnp %d", &cards.card2.nPoints) == 1);
        else if(strstr(line, "numelh") != NULL)
            numelh |= (sscanf(ns, "numelh %d", &cards.card2.nSolidHexes) == 1);
        else if(strncmp(line, "endfree", 7) == 0)
            break;
    } while(ifile.good());

    return nmmat && numnp && numelh;
}

// ****************************************************************************
// Method: Dyna3DFile::GetLine
//
// Purpose: 
//   Reads a line from the file and puts it in the line member array.
//
// Arguments:
//   ifile : The stream from which to read lines.
//
// Programmer: Brad Whitlock
// Creation:   Tue Dec 5 11:53:23 PDT 2006
//
// Modifications:
//   
// ****************************************************************************

void
Dyna3DFile::GetLine(ifstream &ifile)
{
    ifile.getline(line, 1024);
}

// ****************************************************************************
// Method: Dyna3DFile::SkipComments
//
// Purpose: 
//   Skips comments.
//
// Arguments:
//   ifile       : The stream from which to read lines.
//   sectionName : The name of the last section to be read.
//
// Returns:    True if the name of the last section that was encountered
//             is sectionName; False otherwise.
//
// Note:       
//
// Programmer: Brad Whitlock
// Creation:   Mon Dec 4 17:09:36 PST 2006
//
// Modifications:
//   
// ****************************************************************************

void
Dyna3DFile::SkipComments(ifstream &ifile, const char *sectionName,
    bool &sectionNameFound, bool &sectionNameIsCurrent)
{
    sectionNameFound = false;
    sectionNameIsCurrent = false;
    DEBUG_READER(debug5 << "**************** SECTION begin *****************" << endl;)
    do
    {
        GetLine(ifile);
        if(strstr(line, "------") != NULL)
        {
            sectionNameIsCurrent = (strstr(line, sectionName) != NULL);
            sectionNameFound |= sectionNameIsCurrent;
        }
    } while(line[0] == '*');
    DEBUG_READER(debug5 << "**************** SECTION end *****************" << endl;)
}

// ****************************************************************************
// Method: Dyna3DFile::SkipToSection
//
// Purpose: 
//   Skip to the named section and return true if it was found.
//
// Arguments:
//   ifile   : The stream from which to read lines.
//   section : The name of the section to look for.
//
// Returns:    True if the section was found.
//
// Note:       
//
// Programmer: Brad Whitlock
// Creation:   Tue Dec 5 11:54:12 PDT 2006
//
// Modifications:
//   
// ****************************************************************************

bool
Dyna3DFile::SkipToSection(ifstream &ifile, const char *section)
{
    bool sectionFound = false;
    do
    {
        GetLine(ifile);
        sectionFound = (strstr(line, "------") != NULL &&
                        strstr(line, section) != NULL);
    } while(!ifile.eof() && !sectionFound);
    if(sectionFound)
    {
        do
        {
            GetLine(ifile);
        } while(line[0] == '*');
    }
    return sectionFound;
}

// ****************************************************************************
// Method: Dyna3DFile::ReadControlCard2
//
// Purpose: 
//   Read control card 2 since it contains information about the number
//   of points, cells, etc.
//
// Arguments:
//   ifile : The file stream from which to read.
//
// Returns:    True if the file looks like a DYNA3D file.
//
// Note:       
//
// Programmer: Brad Whitlock
// Creation:   Mon Nov 27 16:23:01 PST 2006
//
// Modifications:
//   
// ****************************************************************************

bool
Dyna3DFile::ReadControlCard2(ifstream &ifile)
{
    // Skip the comments but search them for "DYNA3D".
    bool isDyna, tmp;
    SkipComments(ifile, "DYNA3D", isDyna, tmp);

    // Process the card line.
    bool err = false;
    if(sscanf(line, "%5d%10d%10d%10d%10d%10d%10d+", 
        &cards.card2.nMaterials,
        &cards.card2.nPoints,
        &cards.card2.nSolidHexes,
        &cards.card2.nBeamElements,
        &cards.card2.nShellElements4,
        &cards.card2.nShellElements8,
        &cards.card2.nInterfaceSegments) != 7)
    {
        err = true;
    }

    if(sscanf(line+65, "%g %g", 
        &cards.card2.interfaceInterval,
        &cards.card2.shellTimestep) != 2)
    {
        err = true;
    }
 
    if(err)
    {
        DEBUG_READER(debug1 << "Error reading card 2:" << line << endl;)
    }

    DEBUG_READER(
    debug4 << "\tcards.card2.nMaterials=" << cards.card2.nMaterials << ",\n";
    debug4 << "\tcards.card2.nPoints=" << cards.card2.nPoints << ",\n";
    debug4 << "\tcards.card2.nSolidHexes=" << cards.card2.nSolidHexes << ",\n";
    debug4 << "\tcards.card2.nBeamElements=" << cards.card2.nBeamElements << ",\n";
    debug4 << "\tcards.card2.nShellElements4=" << cards.card2.nShellElements4 << ",\n";
    debug4 << "\tcards.card2.nShellElements8=" << cards.card2.nShellElements8 << ",\n";
    debug4 << "\tcards.card2.nInterfaceSegments=" << cards.card2.nInterfaceSegments << "\n";
    debug4 << "\tcards.card2.interfaceInterval=" << cards.card2.interfaceInterval << ",\n";
    debug4 << "\tcards.card2.shellTimestep=" << cards.card2.shellTimestep << "\n";
    )

    return isDyna;
}

//
// Fill these in later if we have to understand more control data.
//

void
Dyna3DFile::ReadControlCard3(ifstream &ifile)
{
    // Skip the comments
    bool b0,b1; SkipComments(ifile, "CONTROL CARD #3", b0, b1);

    // Process the card line.
}

void
Dyna3DFile::ReadControlCard4(ifstream &ifile)
{
    // Skip the comments
    bool b0,b1; SkipComments(ifile, "CONTROL CARD #4", b0, b1);

    // Process the card line.
}

void
Dyna3DFile::ReadControlCard5(ifstream &ifile)
{
    // Skip the comments
    bool b0,b1; SkipComments(ifile, "CONTROL CARD #5", b0, b1);

    // Process the card line.
}

void
Dyna3DFile::ReadControlCard6(ifstream &ifile)
{
    // Skip the comments
    bool b0,b1; SkipComments(ifile, "CONTROL CARD #6", b0, b1);

    // Process the card line.
}

void
Dyna3DFile::ReadControlCard7(ifstream &ifile)
{
    // Skip the comments
    bool b0,b1; SkipComments(ifile, "CONTROL CARD #7", b0, b1);

    // Process the card line.
}

void
Dyna3DFile::ReadControlCard8(ifstream &ifile)
{
    // Skip the comments
    bool b0,b1; SkipComments(ifile, "CONTROL CARD #8", b0, b1);

    // Process the card line.
}

void
Dyna3DFile::ReadControlCard9(ifstream &ifile)
{
    bool b0,b1; SkipComments(ifile, "CONTROL CARD #9", b0, b1);

    // If the next line is not a comment then assume that we have
    // some commands that will terminate with "endfree".
    GetLine(ifile);
    if(line[0] != '*')
    {
        do 
        {
            if(strncmp(line, "endfree", 7) == 0)
                break;
            GetLine(ifile);
        } while(!ifile.good());
    }
}

// ****************************************************************************
563
// Method: Dyna3DFile_WriteDebug4
564 565
//
// Purpose: 
566
//   This callback function writes a string to the debug logs.
567 568
//
// Arguments:
569 570
//   s : The string to write.
//   cbdata : The callback data.
571 572
//
// Programmer: Brad Whitlock
573
// Creation:   Thu Jan 26 13:58:33 PST 2012
574 575 576 577 578
//
// Modifications:
//   
// ****************************************************************************

579 580
static void
Dyna3DFile_WriteDebug5(const char *s, void *cbdata)
581
{
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
    DEBUG_READER(debug5 << s << endl;)
}

// ****************************************************************************
// Method: Dyna3DFile_ReadLine
//
// Purpose: 
//   This is a callback function that is used by the material properties to
//   get a line of input for the material card.
//
// Arguments:
//   buf    : The buffer we're filling.
//   buflen : The length of the buffer.
//   cbdata : Callback function data.
//
// Programmer: Brad Whitlock
// Creation:   Thu Jan 26 13:58:15 PST 2012
//
// Modifications:
//
// ****************************************************************************
603

604 605 606 607 608 609
struct Dyna3DFile_ReadLine_data
{
    ifstream *stream;
    char     *line;
    int       lineIndex;
};
610

611 612 613 614 615 616
static void 
Dyna3DFile_ReadLine(char *buf, int buflen, void *cbdata)
{
    Dyna3DFile_ReadLine_data *s = (Dyna3DFile_ReadLine_data *)cbdata;
    memset(buf, 0, buflen * sizeof(char));
    if(s->lineIndex == 0)
617
    {
618 619 620 621
        // The first time through, the ReadMaterialCards routine has 
        // already read the first line of the material card into the
        // line buffer. So, just return that.
        strncpy(buf, s->line, buflen);
622
    }
623
    else
624
    {
625
        s->stream->getline(buf, buflen);
626
    }
627
    s->lineIndex++;
628

629 630 631
    // If the line we read was a comment then try to read another line.
    if(buf[0] == '*')
        Dyna3DFile_ReadLine(buf, buflen, cbdata);
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
}

// ****************************************************************************
// Method: Dyna3DFile::ReadMaterialCards
//
// Purpose: 
//   Read the material cards.
//
// Arguments:
//   ifile : The file stream to read.
//
// Returns:    
//
// Note:       Only the names and material numbers are currently used.
//
// Programmer: Brad Whitlock
// Creation:   Mon Nov 27 16:22:08 PST 2006
//
// Modifications:
//   Brad Whitlock, Fri Aug  1 11:49:57 PDT 2008
//   Added code to read material strength.
//
//   Brad Whitlock, Mon Mar  9 16:31:41 PDT 2009
//   I rewrote the routine so it can handle variable line material cards.
//
//   Brad Whitlock, Mon Aug  9 14:22:41 PDT 2010
//   I added a little code to try and get us to the material cards section
//   if we're not already there by the time this routine gets called. This
//   makes the code a little more tolerant of funky control cards.
//
662 663 664
//   Brad Whitlock, Thu Jan 26 13:59:32 PST 2012
//   I rewrote the code that reads in single material cards.
//
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
// ****************************************************************************

void
Dyna3DFile::ReadMaterialCards(ifstream &ifile)
{
    // Skip the comments at the start of the section
    bool matCards, currentlyInMatCards;
    const char *matCardHeader = "MATERIAL CARDS";
    SkipComments(ifile, matCardHeader, matCards, currentlyInMatCards);
    if(!currentlyInMatCards)
    {
        DEBUG_READER(debug5 << "Trying to skip ahead to " << matCardHeader << endl;)
        if(!SkipToSection(ifile, matCardHeader))
        {
            DEBUG_READER(debug5 << "We must have been past " << matCardHeader
                   << ". Reset and try again" << endl;)
            ifile.seekg(0, ios::beg);
            if(!SkipToSection(ifile, matCardHeader))
            {
                DEBUG_READER(debug5 << "Returning without reading any materials." << endl;)
                return;
            }
        }
    }
    DEBUG_READER(debug5 << "Reading materials..." << endl;)

    std::set<std::string> uniqueNames;
    bool notFirstLine = false;
    bool keepReading = true;
    do
    {
        if(notFirstLine)
            GetLine(ifile);
        notFirstLine = true;

        // See if the line ends like a material
701
        //int len = (int)strlen(line);
702 703 704 705 706 707 708 709 710 711 712 713
        const char *end_of_matline = "0    0    0";
        bool endsLikeMat = (strcmp(line + 69, end_of_matline) == 0);
        bool startsLikeMat = false;
        if(!endsLikeMat)
        {
            // See if the line starts like a material.
            if(line[0] == ' ' && line[1] == ' ' && line[6] == ' ')
            {
                line[6] = '\0';
                int matno; 
                if(sscanf(line, "%d", &matno) == 1)
                {
714
                    startsLikeMat = (matno == (int)materialCards.size()+1);
715 716 717 718 719 720 721
                }
                line[6] = ' ';
            }
        }
        if(startsLikeMat || endsLikeMat)
        {
            DEBUG_READER(debug4 << "Reading material " << (materialCards.size()+1) << endl;)
722 723 724 725 726 727

            // Let the material properties object read the card.
            Dyna3DFile_ReadLine_data cbdata;
            cbdata.stream = &ifile;
            cbdata.line = line;
            cbdata.lineIndex = 0;
728
            MaterialProperties mat;
729
            mat.ReadCard(Dyna3DFile_ReadLine, &cbdata);
730 731 732

            // Make sure that the material name is unique.
            std::string matName(mat.materialName);
733 734
            if(matName.empty())
            {
735
                char matNameBuf[1025];
736 737 738
                SNPRINTF(matNameBuf, 1024, "%d", int(1 + materialCards.size()));
                matName = std::string(matNameBuf);
            }
739 740 741 742 743
            if(uniqueNames.find(matName) != uniqueNames.end())
            {
                // Find a unique name.
                int index = 2;
                std::string tmpName(matName);
744
                char *matNameBuf = new char[1025];
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761
                do
                {
                    SNPRINTF(matNameBuf, 1024, "%s %d", matName.c_str(), index++);
                    tmpName = std::string(matNameBuf);
                } while(uniqueNames.find(tmpName) != uniqueNames.end());
                matName = tmpName;
                delete [] matNameBuf;
            }
            uniqueNames.insert(matName);
            mat.materialName = matName;

            // Add to the list of materials.
            materialCards.push_back(mat);
        }

        // Read until we have the proper number of materials or we
        // run inth the "NODE DEFINITIONS" section.
762
        keepReading = ((int)materialCards.size() < cards.card2.nMaterials) &&
763 764 765 766 767
                      (strstr(line, "NODE DEFINITIONS") == NULL);
    } while(keepReading);

    // Fill out the material list in case we didn't get to read them all
    int n = 1;
768
    while((int)materialCards.size() < cards.card2.nMaterials)
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783
    {
        char badname[100];
        SNPRINTF(badname, 100, "invalid %d", n++);
        MaterialProperties mat;
        mat.materialName = badname;
        mat.materialNumber = (int)(materialCards.size() + 1);
        materialCards.push_back(mat);
    }

    // Check the material numbers. If the stored material number does not
    // match the material index then assume that the material numbers are
    // wrong and renumber them.
    size_t i, diff = 0;
    for(i = 0; i < materialCards.size(); ++i)
    {
784
        if(materialCards[i].materialNumber != (int)i+1)
785 786 787 788 789 790
            diff++;
    }
    if(diff > materialCards.size()/2)
    {
        DEBUG_READER(debug5 << "Renumbering materials" << endl;)
        for(i = 0; i < materialCards.size(); ++i)
bonnell's avatar
bonnell committed
791
            materialCards[i].materialNumber = (int)i+1;
792 793 794 795 796 797 798 799 800
    }

    DEBUG_READER(
    debug5 << "********************** MATERIALS *************************" << endl;
    for(i = 0; i < materialCards.size(); ++i)
    {
        debug5 << "Added material (" << materialCards[i].materialNumber << "): "
               << materialCards[i].materialName.c_str() << ", density="
               << materialCards[i].density << ", strength=" << materialCards[i].strength << endl;
801 802 803
        // Write the card back out so we can see if we read it right.
        materialCards[i].WriteCard(Dyna3DFile_WriteDebug5, NULL);
        debug5 << "--" << endl;
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 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 916 917
    }
    debug5 << "**********************************************************" << endl;
    )
}

// ****************************************************************************
// Method: Dyna3DFile::ReadFile
//
// Purpose: 
//   This method reads the Dyna3D file and constructs a dataset that gets
//   returned later in the GetMesh method.
//
// Arguments:
//   name   : The name of the file to read.
//   nLines : The max number of lines to read from the file.
//
// Returns:    True if the file looks like a Dyna3D bulk data file.
//
// Programmer: Brad Whitlock
// Creation:   Mon Nov 27 16:25:13 PST 2006
//
// Modifications:
//   Brad Whitlock, Mon Mar  9 16:32:16 PDT 2009
//   I changed how we do materials. I also put in some diagnostic code so we
//   can see the start and end of node and cell sequences.
//
//   Brad Whitlock, Thu Apr  2 16:26:10 PDT 2009
//   I changed the reader so it reopens the file after reading materials
//   just in case there was an error.
//
//   Brad Whitlock, Mon Aug  9 14:27:22 PDT 2010
//   I changed the code so it seeks to the start of the file instead of
//   reopening the file.
//
//   Brad Whitlock, Fri Dec 16 16:38:03 PST 2011
//   Read a new format of control cards.
//
// ****************************************************************************

bool
Dyna3DFile::ReadFile(const std::string &name, int nLines)
{
    const char *mName = "avtDyna3DFileFormat::ReadFile: ";
//    int total = visitTimer->StartTimer();
    DEBUG_READER(debug4 << mName << endl;)

    // Open the file.
    ifstream ifile(name.c_str());
    if (ifile.fail())
    {
        return false;
    }

    // Allocate a buffer that we'll use for reading lines.
    if(line == 0)
    {
        line = new char[1024];
        memset(line, 0, 1024 * sizeof(char));
    }

    // Skip line 1
    GetLine(ifile);
    DEBUG_READER(debug5 << "line1: " << line << endl;)

//    int readCards = visitTimer->StartTimer();
    bool recognized = false;
    if(strstr(line, "10 large") != NULL)
        recognized = ReadControlCardsNewFormat(ifile);
    else
        recognized = ReadControlCards(ifile);

//    visitTimer->StopTimer(readCards, "Reading control cards.");

    if(recognized)
    {
//        readCards = visitTimer->StartTimer();
        ReadMaterialCards(ifile);
//        visitTimer->StopTimer(readCards, "Reading material cards.");

        // Close and reopen the file and skip to the NODE DEFINITIONS section. This
        // prevents us from messing up the nodes if we happened to mess up the 
        // materials.
        ifile.seekg(0, ios::beg);
        SkipToSection(ifile, "NODE DEFINITIONS");

        if(nLines == ALL_LINES)
        {
//            int readingFile = visitTimer->StartTimer();
            int nPoints = cards.card2.nPoints;
            int nCells = cards.card2.nSolidHexes;
            DEBUG_READER(debug4 << mName << "File is DYNA3D and we're reading the mesh. "
                                << "nPoints=" << nPoints << ", nCells=" << nCells << endl;
            )

            vtkPoints *pts = vtkPoints::New();
            pts->Allocate(nPoints);

            ugrid = vtkUnstructuredGrid::New();
            ugrid->SetPoints(pts);
            ugrid->Allocate(nCells);
            pts->Delete();

            // Read nodes
            double pt[3];
            for(int node = 0; node < nPoints;)
            {
                // In the event that we skipped reading materials then
                // we can re-use the line that we read already.
                if(node > 0)
                    GetLine(ifile);

                if(line[0] != '*')
                {
                    DEBUG_READER(
918
                    if(node < 10 || node >= nPoints-10) {
919
                        debug5 << line << endl;
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
                    )

                    // It's a valid point line.
                    char *valstart = line + 53;
                    char *valend = valstart + 73;
                    *valend = '\0';
                    pt[2] = atof(valstart);
    
                    valstart -= 20;
                    valend   -= 20;
                    *valend = '\0';
                    pt[1] = atof(valstart);

                    valstart -= 20;
                    valend   -= 20;
                    *valend = '\0';
                    pt[0] = atof(valstart);

                    // Insert the point.
                    pts->InsertNextPoint(pt);
                    ++node;
                }
            }

            // Allocate material numbers.
            if(materialCards.size() > 0)
            {
                matNumbers = new int[nCells];
                memset(matNumbers, 0, sizeof(int) * nCells);
            }

            //
            // Read connectivity.
            //
            vtkIdType verts[8];
            for(int cellid = 0; cellid < nCells;)
            {
                GetLine(ifile);
                if(line[0] != '*')
                {
                    DEBUG_READER(
962
                    if(cellid < 10 || cellid >= nCells-10) {
963
                        debug5 << line << endl;
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
                    )

#define INDEX_FIELD_WIDTH 8
                    char *valstart = line + 69;
                    char *valend = valstart + INDEX_FIELD_WIDTH;
                    *valend = '\0';
                    verts[7] = atoi(valstart)-1;
    
                    valstart -= INDEX_FIELD_WIDTH;
                    valend -= INDEX_FIELD_WIDTH;
                    *valend = '\0';
                    verts[6] = atoi(valstart)-1;
    
                    valstart -= INDEX_FIELD_WIDTH;
                    valend -= INDEX_FIELD_WIDTH;
                    *valend = '\0';
                    verts[5] = atoi(valstart)-1;
    
                    valstart -= INDEX_FIELD_WIDTH;
                    valend -= INDEX_FIELD_WIDTH;
                    *valend = '\0';
                    verts[4] = atoi(valstart)-1;
    
                    valstart -= INDEX_FIELD_WIDTH;
                    valend -= INDEX_FIELD_WIDTH;
                    *valend = '\0';
                    verts[3] = atoi(valstart)-1;
    
                    valstart -= INDEX_FIELD_WIDTH;
                    valend -= INDEX_FIELD_WIDTH;
                    *valend = '\0';
                    verts[2] = atoi(valstart)-1;
    
                    valstart -= INDEX_FIELD_WIDTH;
                    valend -= INDEX_FIELD_WIDTH;
                    *valend = '\0';
                    verts[1] = atoi(valstart)-1;
    
                    valstart -= INDEX_FIELD_WIDTH;
                    valend -= INDEX_FIELD_WIDTH;
                    *valend = '\0';
                    verts[0] = atoi(valstart)-1;
    
                    // Insert the hex cell.
                    ugrid->InsertNextCell(VTK_HEXAHEDRON, 8, verts);
    
                    // Save the material number
                    if(matNumbers != 0)
                    {
                        line[13] = '\0';
                        matNumbers[cellid] = atoi(line+11);
                    }
                    ++cellid;
                }
            }

            // Skip ahead to the initial conditions section.
            bool inIC = SkipToSection(ifile, "INITIAL CONDITIONS");
            if(inIC)
            {
                DEBUG_READER(
                debug4 << mName << "Found INITIAL CONDITIONS section. "
                       "Creating velocities." << endl;
                )
                velocity = vtkFloatArray::New();
                velocity->SetNumberOfComponents(3);
                velocity->SetNumberOfTuples(nPoints);
                double vel[3];
                for(int node = 0; node < nPoints; ++node)
                {
                    if(node > 0)
                        GetLine(ifile);

#define VEL_FIELD_WIDTH 10
                    char *valstart = line + 28;
                    char *valend = valstart + VEL_FIELD_WIDTH;
                    *valend = '\0';
                    vel[2] = atof(valstart);
    
                    valstart -= VEL_FIELD_WIDTH;
                    valend -= VEL_FIELD_WIDTH;
                    *valend = '\0';
                    vel[1] = atof(valstart);
    
                    valstart -= VEL_FIELD_WIDTH;
                    valend -= VEL_FIELD_WIDTH;
                    *valend = '\0';
                    vel[0] = atof(valstart);

                    velocity->SetTuple(node, vel);
                }
            }
            else
            {
                DEBUG_READER(
                debug4 << mName << "Could not find INITIAL CONDITIONS section."
                       << endl;
                )
            }
        }
        else
        {
            DEBUG_READER(
            debug4 << mName << "File is DYNA3D." << endl;
            )
            // Skip ahead to the initial conditions section. If we can
            // then create a velocity object so we can add a velocity 
            // variable to the metadata.
            if(SkipToSection(ifile, "INITIAL CONDITIONS"))
            {
                DEBUG_READER(
                debug4 << mName << "Found INITIAL CONDITIONS section. "
                       "Creating velocities." << endl;
                )
                velocity = vtkFloatArray::New();
                velocity->SetNumberOfTuples(1);
            }
            else
            {
                DEBUG_READER(
                debug4 << mName << "Could not find INITIAL CONDITIONS section."
                       << endl;
                )
            }
        }
    }
    else
    {
        DEBUG_READER(
        debug4 << mName << "The file is not DYNA3D" << endl;
        )
    }

//    visitTimer->StopTimer(total, "Loading Dyna3D file");

    return recognized;
}

// ****************************************************************************
//  Method: Dyna3DFile::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:
//      meshname    The name of the mesh of interest.  This can be ignored if
//                  there is only one mesh.
//
//  Programmer: Brad Whitlock
//  Creation:   Mon Nov 27 16:25:13 PST 2006
//
// ****************************************************************************

vtkDataSet *
Dyna3DFile::GetMesh(const char *meshname)
{
    DEBUG_READER(debug4 << "Dyna3DFile::GetMesh" << endl;)
    vtkUnstructuredGrid *ugrid2 = vtkUnstructuredGrid::New();
    ugrid2->ShallowCopy(ugrid);

    return ugrid2;
}


// ****************************************************************************
//  Method: Dyna3DFile::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:
//      varname    The name of the variable requested.
//
//  Programmer: Brad Whitlock
//  Creation:   Mon Nov 27 16:25:13 PST 2006
//
//  Modifications:
//    Brad Whitlock, Fri Aug  1 14:10:33 PDT 2008
//    Added support for strength.
//
//    Brad Whitlock, Tue Sep 21 14:14:28 PST 2010
//    I added equivalentPlasticStrain.
//
1152 1153 1154
//    Brad Whitlock, Wed Jan 25 17:23:27 PST 2012
//    Call methods on the material cards to obtain values.
//
1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169
// ****************************************************************************

vtkDataArray *
Dyna3DFile::GetVar(const char *varname)
{
    DEBUG_READER(debug4 << "Dyna3DFile::GetVar" << endl;)
    vtkDataArray *ret = 0;

    if((strcmp(varname, "density") == 0 ||
        strcmp(varname, "strength") == 0 ||
        strcmp(varname, "equivalentPlasticStrain") == 0) &&
       matNumbers != 0 && 
       materialCards.size() > 0)
    {
        std::map<int, int> mat2card;
1170
        for(size_t i = 0; i < materialCards.size(); ++i)
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
            mat2card[materialCards[i].materialNumber] = i;

        vtkFloatArray *arr = vtkFloatArray::New();
        arr->SetNumberOfTuples(ugrid->GetNumberOfCells());
        float *fptr = (float *)arr->GetVoidPointer(0); 
        if(strcmp(varname, "density") == 0)
        {
            for(vtkIdType id = 0; id < ugrid->GetNumberOfCells(); ++id)
            {
               int cellMat = matNumbers[id];
               int card = mat2card[cellMat];
               *fptr++ = (float)materialCards[card].density;
            }
        }
        else if(strcmp(varname, "strength") == 0)
        {
            for(vtkIdType id = 0; id < ugrid->GetNumberOfCells(); ++id)
            {
               int cellMat = matNumbers[id];
               int card = mat2card[cellMat];
               *fptr++ = (float)materialCards[card].strength;
            }
        }
        else if(strcmp(varname, "equivalentPlasticStrain") == 0)
        {
            for(vtkIdType id = 0; id < ugrid->GetNumberOfCells(); ++id)
            {
               int cellMat = matNumbers[id];
               int card = mat2card[cellMat];
               *fptr++ = (float)materialCards[card].equivalentPlasticStrain;
            }
        }

        ret = arr;
    }
    else if((strcmp(varname, "material") == 0  || strcmp(varname, "matno") == 0) &&
            matNumbers != 0)
    {
        vtkIntArray *arr = vtkIntArray::New();
        arr->SetNumberOfTuples(ugrid->GetNumberOfCells());
        int *iptr = (int *)arr->GetVoidPointer(0);
        for(vtkIdType id = 0; id < ugrid->GetNumberOfCells(); ++id)
            *iptr++ = matNumbers[id];

        ret = arr;
    }

    if(ret != NULL)
        ret->SetName(varname);

    return ret;
}


// ****************************************************************************
//  Method: Dyna3DFile::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:
//      varname    The name of the variable requested.
//
//  Programmer: Brad Whitlock
//  Creation:   Mon Nov 27 16:25:13 PST 2006
//
// ****************************************************************************

vtkDataArray *
Dyna3DFile::GetVectorVar(const char *varname)
{
    DEBUG_READER(debug4 << "Dyna3DFile::GetVectorVar" << endl;)
    vtkDataArray *ret = 0;

    if(strcmp(varname, "velocity") == 0 &&
       velocity != 0)
    {
        //
        // The routine that calls this method is going to assume that it 
        // can call Delete on what is returned.  That means we better add 
        // an extra reference.
        //
        velocity->Register(NULL);
        ret = velocity;
    }

    return ret;
}