313{
314 int i;
315 vtkIdType numLines = seedIds->GetNumberOfIds();
316 double propagation = inPropagation;
317 vtkIdType numSteps = inNumSteps;
318
319
320 vtkDataSetAttributes* outputPD = output->GetPointData();
321 vtkDataSetAttributes* outputCD = output->GetCellData();
322 vtkPointData* inputPD;
323 vtkDataSet* input;
324
325
326 int direction=1;
327
328 double* weights = 0;
329 if ( maxCellSize > 0 )
330 {
331 weights = new double[maxCellSize];
332 }
333
334 if (this->GetIntegrator() == 0)
335 {
336 vtkErrorMacro("No integrator is specified.");
337 return;
338 }
339
340
341 vtkGenericCell* cell = vtkGenericCell::New();
342
343
344 vtkInitialValueProblemSolver* integrator =
345 this->GetIntegrator()->NewInstance();
346 integrator->SetFunctionSet(func);
347
348
349
350
351
352
353
354 vtkPoints* outputPoints = vtkPoints::New();
355 vtkCellArray* outputLines = vtkCellArray::New();
356
357
358 vtkDoubleArray* time = vtkDoubleArray::New();
359 time->SetName("IntegrationTime");
360
361
362 vtkIntArray* retVals = vtkIntArray::New();
363 retVals->SetName("ReasonForTermination");
364
365 vtkDoubleArray* cellVectors = 0;
366 vtkDoubleArray* velocityArray = 0;
367 vtkDoubleArray* speedArray = 0;
368 vtkDoubleArray* vorticity = 0;
369 vtkDoubleArray* rotation = 0;
370 vtkDoubleArray* angularVel = 0;
371
372 velocityArray = vtkDoubleArray::New();
373 velocityArray->SetName("Velocity");
374 velocityArray->SetNumberOfComponents(3);
375
376 speedArray = vtkDoubleArray::New();
377 speedArray->SetName("Speed");
378
379 if (this->ComputeVorticity)
380 {
381 cellVectors = vtkDoubleArray::New();
382 cellVectors->SetNumberOfComponents(3);
383 cellVectors->Allocate(3*VTK_CELL_SIZE);
384
385 vorticity = vtkDoubleArray::New();
386 vorticity->SetName("Vorticity");
387 vorticity->SetNumberOfComponents(3);
388
389 rotation = vtkDoubleArray::New();
390 rotation->SetName("Rotation");
391
392 angularVel = vtkDoubleArray::New();
393 angularVel->SetName("AngularVelocity");
394 }
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416 vtkIdType numPtsTotal=0;
417 double velocity[3];
418
419 int shouldAbort = 0;
420
421 for(int currentLine = 0; currentLine < numLines; currentLine++)
422 {
423 double progress = static_cast<double>(currentLine)/numLines;
424 this->UpdateProgress(progress);
425
426 switch (integrationDirections->GetValue(currentLine))
427 {
428 case FORWARD:
429 direction = 1;
430 break;
431 case BACKWARD:
432 direction = -1;
433 break;
434 }
435
436
437 double point1[3], point2[3], pcoords[3], vort[3], omega;
438 vtkIdType index, numPts=0;
439
440
441
442 func->ClearLastCellId();
443
444 double startTime = startTimes->GetValue(currentLine);
445
446
447 seedSource->GetTuple(seedIds->GetId(currentLine), point1);
448 memcpy(point2, point1, 3*sizeof(double));
449
450 double point1t[4];
451 memcpy(point1t, point1, 3*sizeof(double));
452 point1t[3] = startTime;
453
454 if (!func->FunctionValues(point1t, velocity))
455 {
456 continue;
457 }
458
459 if ( propagation >= this->MaximumPropagation ||
460 numSteps > this->MaximumNumberOfSteps)
461 {
462 continue;
463 }
464
465 numPts++;
466 numPtsTotal++;
467 vtkIdType nextPoint = outputPoints->InsertNextPoint(point1);
468
469 time->InsertNextValue(startTime);
470
471
472
473
475 stepSize.Unit = LENGTH_UNIT;
476 stepSize.Interval = 0;
478 aStep.Unit = LENGTH_UNIT;
479 double step, minStep=0, maxStep=0;
480 double stepTaken, accumTime=startTime;
481 double speed;
482 double cellLength;
483 int retVal=OUT_OF_LENGTH, tmp;
484
485
486 input = func->GetLastDataSet();
487 inputPD = input->GetPointData();
488
489
490 input->GetCell(func->GetLastCellId(), cell);
491 cellLength = sqrt(static_cast<double>(cell->GetLength2()));
492 speed = vtkMath::Norm(velocity);
493
494 if ( speed != 0.0 )
495 {
496 this->ConvertIntervals( stepSize.Interval, minStep, maxStep,
497 direction, cellLength );
498 }
499
500
501 func->GetLastWeights(weights);
502
503
504
505 velocityArray->InsertNextTuple(velocity);
506 speedArray->InsertNextValue(speed);
507
508
509
510 if (this->ComputeVorticity)
511 {
512
513
514 func->GetLastLocalCoordinates(pcoords);
515 vort[0] = vort[1] = vort[2] = 0.0;
516
517 vorticity->InsertNextTuple(vort);
518
519
520 if (speed != 0.0)
521 {
522 omega = vtkMath::Dot(vort, velocity);
523 omega /= speed;
524 omega *= this->RotationScale;
525 }
526 else
527 {
528 omega = 0.0;
529 }
530 angularVel->InsertNextValue(omega);
531 rotation->InsertNextValue(0.0);
532 }
533
534 double error = 0;
535
536
537
538 while ( propagation < this->MaximumPropagation )
539 {
540
541 if (numSteps > this->MaximumNumberOfSteps)
542 {
543 retVal = OUT_OF_STEPS;
544 break;
545 }
546
547 if ( numSteps++ % 1000 == 1 )
548 {
549 progress =
550 ( currentLine + propagation / this->MaximumPropagation ) / numLines;
551 this->UpdateProgress(progress);
552
553 if (this->GetAbortExecute())
554 {
555 shouldAbort = 1;
556 break;
557 }
558 }
559
560
561 if ( (speed == 0) || (speed <= this->TerminalSpeed) )
562 {
563 retVal = STAGNATION;
564 break;
565 }
566
567
568
569 aStep.Interval = fabs( stepSize.Interval );
570
571 if ( ( propagation + aStep.Interval ) > this->MaximumPropagation )
572 {
573 aStep.Interval = this->MaximumPropagation - propagation;
574 if ( stepSize.Interval >= 0 )
575 {
576#if VMTK_USE_LEGACY_INTERVAL_INFORMATION
577 stepSize.Interval = this->ConvertToLength( aStep, cellLength );
578#else
579 stepSize.Interval = vtkIntervalInformation::ConvertToLength(aStep, cellLength);
580#endif
581 }
582 else
583 {
584#if VMTK_USE_LEGACY_INTERVAL_INFORMATION
585 stepSize.Interval = this->ConvertToLength(aStep, cellLength) * (-1.0);
586#else
587 stepSize.Interval = vtkIntervalInformation::ConvertToLength(aStep, cellLength) * (-1.0);
588#endif
589 }
590 maxStep = stepSize.Interval;
591 }
592 this->LastUsedStepSize = stepSize.Interval;
593
594
595
596 func->SetNormalizeVector( true );
597 tmp = integrator->ComputeNextStep( point1, point2, accumTime, stepSize.Interval,
598 stepTaken, minStep, maxStep,
599 this->MaximumError, error );
600 func->SetNormalizeVector( false );
601 if ( tmp != 0 )
602 {
603 retVal = tmp;
604 memcpy(lastPoint, point2, 3*sizeof(double));
605 break;
606 }
607
608
609
610
611 double disp[3];
612 for (i=0; i<3; i++)
613 {
614 disp[i] = point2[i] - point1[i];
615 }
616 if ( (stepSize.Interval == 0) ||
617 (vtkMath::Norm(disp) / fabs(stepSize.Interval) <= this->TerminalSpeed) )
618 {
619 retVal = STAGNATION;
620 break;
621 }
622
623 accumTime += stepTaken / speed;
624
625 propagation += fabs( stepSize.Interval );
626
627
628 for(i=0; i<3; i++)
629 {
630 point1[i] = point2[i];
631 }
632
633 double point2t[4];
634 memcpy(point2t, point2, 3*sizeof(double));
635 point2t[3] = accumTime;
636
637
638 if ( !func->FunctionValues(point2t, velocity) )
639 {
640 retVal = OUT_OF_DOMAIN;
641 memcpy(lastPoint, point2, 3*sizeof(double));
642 break;
643 }
644
645 input = func->GetLastDataSet();
646 inputPD = input->GetPointData();
647
648
649 numPts++;
650 numPtsTotal++;
651 nextPoint = outputPoints->InsertNextPoint(point1);
652 time->InsertNextValue(accumTime);
653
654
655 input->GetCell(func->GetLastCellId(), cell);
656 cellLength = sqrt(static_cast<double>(cell->GetLength2()));
657
658 velocityArray->InsertNextTuple(velocity);
659
660 speed = vtkMath::Norm(velocity);
661
662 speedArray->InsertNextValue(speed);
663
664
665 func->GetLastWeights(weights);
666
667
668
669
670
671 if (this->ComputeVorticity)
672 {
673
674
675 func->GetLastLocalCoordinates(pcoords);
676 vort[0] = vort[1] = vort[2] = 0.0;
677
678 vorticity->InsertNextTuple(vort);
679
680
681
682 omega = vtkMath::Dot(vort, velocity);
683 omega /= speed;
684 omega *= this->RotationScale;
685 index = angularVel->InsertNextValue(omega);
686 rotation->InsertNextValue(rotation->GetValue(index-1) +
687 (angularVel->GetValue(index-1) + omega)/2 *
688 (accumTime - time->GetValue(index-1)));
689 }
690
691
692 if ( (speed == 0) || (speed <= this->TerminalSpeed) )
693 {
694 retVal = STAGNATION;
695 break;
696 }
697
698
699 this->ConvertIntervals( step, minStep, maxStep, direction, cellLength );
700
701
702
703
704
705
706
707 if (integrator->IsAdaptive())
708 {
709 if (fabs(stepSize.Interval) < fabs(minStep))
710 {
711 stepSize.Interval = fabs( minStep ) *
712 stepSize.Interval / fabs( stepSize.Interval );
713 }
714 else if (fabs(stepSize.Interval) > fabs(maxStep))
715 {
716 stepSize.Interval = fabs( maxStep ) *
717 stepSize.Interval / fabs( stepSize.Interval );
718 }
719 }
720 else
721 {
722 stepSize.Interval = step;
723 }
724
725
726 }
727
728 if (shouldAbort)
729 {
730 break;
731 }
732
733 if (numPts > 1)
734 {
735 outputLines->InsertNextCell(numPts);
736 for (i=numPtsTotal-numPts; i<numPtsTotal; i++)
737 {
738 outputLines->InsertCellPoint(i);
739 }
740 retVals->InsertNextValue(retVal);
741 }
742
743
744
745
746 inPropagation = propagation;
747 inNumSteps = numSteps;
748
749 propagation = 0;
750 numSteps = 0;
751 }
752
753 if (!shouldAbort)
754 {
755
756 output->SetPoints(outputPoints);
757 outputPD->AddArray(time);
758 outputPD->AddArray(velocityArray);
759 outputPD->AddArray(speedArray);
760 if (vorticity)
761 {
762 outputPD->AddArray(vorticity);
763 outputPD->AddArray(rotation);
764 outputPD->AddArray(angularVel);
765 }
766
767 vtkIdType numPts = outputPoints->GetNumberOfPoints();
768 if ( numPts > 1 )
769 {
770
771 output->SetLines(outputLines);
772 if (this->GenerateNormalsInIntegrate)
773 {
775
776 }
777
778 outputCD->AddArray(retVals);
779 }
780 }
781
782 velocityArray->Delete();
783 speedArray->Delete();
784
785 if (vorticity)
786 {
787 vorticity->Delete();
788 rotation->Delete();
789 angularVel->Delete();
790 }
791
792 if (cellVectors)
793 {
794 cellVectors->Delete();
795 }
796 retVals->Delete();
797
798 outputPoints->Delete();
799 outputLines->Delete();
800
801 time->Delete();
802
803
804 integrator->Delete();
805 cell->Delete();
806
807 delete[] weights;
808
809 output->Squeeze();
810 return;
811}
#define vmtkIntervalInformation