forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkWindBladeReader.cxx
2168 lines (1917 loc) · 70.7 KB
/
vtkWindBladeReader.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
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
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
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
/*=========================================================================
Program: Visualization Toolkit
Module: vtkWindBladeReader.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkWindBladeReader.h"
#include "vtkCallbackCommand.h"
#include "vtkCell.h"
#include "vtkCellData.h"
#include "vtkCellType.h"
#include "vtkDataArraySelection.h"
#include "vtkFloatArray.h"
#include "vtkIdList.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkMath.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkPoints.h"
#include "vtkStructuredGrid.h"
#include "vtkUnstructuredGrid.h"
#include "vtkStreamingDemandDrivenPipeline.h"
#include "vtkStringArray.h"
#include "vtkFloatArray.h"
#include "vtkIntArray.h"
#include "vtkPoints.h"
#include "vtkStructuredGrid.h"
#include "vtkUnstructuredGrid.h"
#include "vtkMultiBlockDataSetAlgorithm.h"
#include "vtkToolkits.h"
#include "vtksys/SystemTools.hxx"
#include <algorithm>
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
//vtkStandardNewMacro(vtkWindBladeReader);
vtkObjectFactoryNewMacro(vtkWindBladeReader);
float vtkWindBladeReader::DRY_AIR_CONSTANT = 287.04;
int vtkWindBladeReader::NUM_PART_SIDES = 4; // Blade parts rhombus
const int vtkWindBladeReader::NUM_BASE_SIDES = 5; // Base pyramid
const int vtkWindBladeReader::LINE_SIZE = 256;
int vtkWindBladeReader::DIMENSION = 3;
int vtkWindBladeReader::BYTES_PER_DATA = 4;
int vtkWindBladeReader::SCALAR = 1;
int vtkWindBladeReader::VECTOR = 2;
int vtkWindBladeReader::FLOAT = 1;
int vtkWindBladeReader::INTEGER = 2;
class WindBladeReaderInternal
{
public:
FILE* FilePtr;
};
//----------------------------------------------------------------------------
// Constructor for WindBlade Reader
//----------------------------------------------------------------------------
vtkWindBladeReader::vtkWindBladeReader()
{
this->Filename = NULL;
this->SetNumberOfInputPorts(0);
// Set up three output ports for field, blade and ground
this->SetNumberOfOutputPorts(3);
// Irregularly spaced grid description for entire problem
this->Points = vtkPoints::New();
this->GPoints = vtkPoints::New();
this->XSpacing = vtkFloatArray::New();
this->YSpacing = vtkFloatArray::New();
this->ZSpacing = vtkFloatArray::New();
this->ZTopographicValues = NULL;
// Blade geometry
this->BPoints = vtkPoints::New();
this->NumberOfBladePoints = 0;
this->NumberOfBladeCells = 0;
// Static tower information
this->NumberOfBladeTowers = 0;
this->XPosition = vtkFloatArray::New();
this->YPosition = vtkFloatArray::New();
this->HubHeight = vtkFloatArray::New();
this->AngularVeloc = vtkFloatArray::New();
this->BladeLength = vtkFloatArray::New();
this->BladeCount = vtkIntArray::New();
// Options to include extra files for topography and turbines
this->UseTopographyFile = 0;
this->UseTurbineFile = 0;
// Setup selection callback to modify this object when array selection changes
this->SelectionObserver = vtkCallbackCommand::New();
this->SelectionObserver->SetCallback(&vtkWindBladeReader::SelectionCallback);
this->SelectionObserver->SetClientData(this);
this->PointDataArraySelection = vtkDataArraySelection::New();
this->PointDataArraySelection->AddObserver(vtkCommand::ModifiedEvent,
this->SelectionObserver);
// Variables need to be divided by density
this->NumberOfTimeSteps = 1;
this->TimeSteps = NULL;
this->NumberOfVariables = 0;
this->DivideVariables = vtkStringArray::New();
this->DivideVariables->InsertNextValue("UVW");
this->DivideVariables->InsertNextValue("A-scale turbulence");
this->DivideVariables->InsertNextValue("B-scale turbulence");
this->DivideVariables->InsertNextValue("Oxygen");
this->Data = NULL;
this->Internal = new WindBladeReaderInternal();
// by default don't skip any lines because normal wind files do not
// have a header
this->NumberOfLinesToSkip = 0;
this->VariableName = NULL;
this->VariableStruct = NULL;
this->VariableCompSize = NULL;
this->VariableBasicType = NULL;
this->VariableByteCount = NULL;
this->VariableOffset = NULL;
}
//----------------------------------------------------------------------------
// Destructor for WindBlade Reader
//----------------------------------------------------------------------------
vtkWindBladeReader::~vtkWindBladeReader()
{
this->SetFilename(NULL);
this->PointDataArraySelection->Delete();
this->DivideVariables->Delete();
this->XPosition->Delete();
this->YPosition->Delete();
this->HubHeight->Delete();
this->AngularVeloc->Delete();
this->BladeLength->Delete();
this->BladeCount->Delete();
this->XSpacing->Delete();
this->YSpacing->Delete();
this->ZSpacing->Delete();
delete [] this->ZTopographicValues;
this->Points->Delete();
this->GPoints->Delete();
this->BPoints->Delete();
if (this->Data)
{
for (int var = 0; var < this->NumberOfVariables; var++)
{
if (this->Data[var])
{
this->Data[var]->Delete();
}
}
delete [] this->Data;
}
this->SelectionObserver->Delete();
delete this->Internal;
delete [] this->TimeSteps;
delete[] this->VariableName;
delete[] this->VariableStruct;
delete[] this->VariableCompSize;
delete[] this->VariableBasicType;
delete[] this->VariableByteCount;
delete[] this->VariableOffset;
}
//----------------------------------------------------------------------------
int vtkWindBladeReader::ProcessRequest(vtkInformation* reqInfo,
vtkInformationVector** inputVector,
vtkInformationVector* outputVector)
{
#ifdef VTK_WORDS_BIGENDIAN
vtkWarningMacro
("Warning WindBlade reader does not yet work on big endian processors")
#endif
if(reqInfo->Has(vtkDemandDrivenPipeline::REQUEST_DATA_NOT_GENERATED()))
{
int port = reqInfo->Get(vtkDemandDrivenPipeline::FROM_OUTPUT_PORT());
if(port != 0)
{
vtkInformation* fieldInfo = outputVector->GetInformationObject(0);
fieldInfo->Set(vtkDemandDrivenPipeline::DATA_NOT_GENERATED(), 1);
}
if(port != 1)
{
vtkInformation* bladeInfo = outputVector->GetInformationObject(1);
bladeInfo->Set(vtkDemandDrivenPipeline::DATA_NOT_GENERATED(), 1);
}
if(port != 2)
{
vtkInformation* groundInfo = outputVector->GetInformationObject(2);
groundInfo->Set(vtkDemandDrivenPipeline::DATA_NOT_GENERATED(), 1);
}
return 1;
}
return this->Superclass::ProcessRequest(reqInfo, inputVector, outputVector);
}
//----------------------------------------------------------------------------
// RequestInformation supplies global meta information
//----------------------------------------------------------------------------
int vtkWindBladeReader::RequestInformation(vtkInformation* reqInfo,
vtkInformationVector** vtkNotUsed(inputVector),
vtkInformationVector* outputVector)
{
int port = reqInfo->Get(vtkDemandDrivenPipeline::FROM_OUTPUT_PORT());
if(port == 0)
{
vtkInformation* bladeInfo = outputVector->GetInformationObject(1);
bladeInfo->Set(vtkDemandDrivenPipeline::REQUEST_DATA_NOT_GENERATED());
vtkInformation* groundInfo = outputVector->GetInformationObject(2);
groundInfo->Set(vtkDemandDrivenPipeline::REQUEST_DATA_NOT_GENERATED());
}
else if(port == 1)
{
vtkInformation* fieldInfo = outputVector->GetInformationObject(0);
fieldInfo->Set(vtkDemandDrivenPipeline::REQUEST_DATA_NOT_GENERATED());
vtkInformation* groundInfo = outputVector->GetInformationObject(2);
groundInfo->Set(vtkDemandDrivenPipeline::REQUEST_DATA_NOT_GENERATED());
}
else if(port == 2)
{
vtkInformation* fieldInfo = outputVector->GetInformationObject(0);
fieldInfo->Set(vtkDemandDrivenPipeline::REQUEST_DATA_NOT_GENERATED());
vtkInformation* bladeInfo = outputVector->GetInformationObject(1);
bladeInfo->Set(vtkDemandDrivenPipeline::REQUEST_DATA_NOT_GENERATED());
}
// Verify that file exists
if ( !this->Filename )
{
vtkErrorMacro("No filename specified");
return 0;
}
// Get ParaView information and output pointers
vtkInformation* fieldInfo = outputVector->GetInformationObject(0);
vtkStructuredGrid *field = this->GetFieldOutput();
vtkInformation* bladeInfo = outputVector->GetInformationObject(1);
this->GetBladeOutput();
vtkInformation* groundInfo = outputVector->GetInformationObject(2);
vtkStructuredGrid *ground = this->GetGroundOutput();
// Read global size and variable information from input file one time
if (this->NumberOfVariables == 0)
{
// Read the size of the problem and variables in data set
if(this->ReadGlobalData() == false)
{
return 0;
}
// If turbine file exists setup number of cells and points in blades, towers
if (this->UseTurbineFile == 1)
{
this->SetupBladeData();
}
// Allocate the ParaView data arrays which will hold the variable data
this->Data = new vtkFloatArray*[this->NumberOfVariables];
for (int var = 0; var < this->NumberOfVariables; var++)
{
this->Data[var] = vtkFloatArray::New();
this->Data[var]->SetName(this->VariableName[var].c_str());
this->PointDataArraySelection->AddArray(this->VariableName[var].c_str());
}
// Set up extent information manually for now
this->WholeExtent[0] = this->WholeExtent[2] = this->WholeExtent[4] = 0;
this->WholeExtent[1] = this->Dimension[0] - 1;
this->WholeExtent[3] = this->Dimension[1] - 1;
this->WholeExtent[5] = this->Dimension[2] - 1;
// Ground is from level to topography of field, one cell thick
this->GDimension[0] = this->Dimension[0];
this->GDimension[1] = this->Dimension[1];
this->GDimension[2] = 2;
this->GExtent[0] = this->GExtent[2] = this->GExtent[4] = 0;
this->GExtent[1] = this->GDimension[0] - 1;
this->GExtent[3] = this->GDimension[1] - 1;
this->GExtent[5] = this->GDimension[2] - 1;
field->SetDimensions(this->Dimension);
fieldInfo->Set(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(),
this->WholeExtent, 6);
fieldInfo->Set(CAN_PRODUCE_SUB_EXTENT(),1);
ground->SetDimensions(this->GDimension);
groundInfo->Set(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(),
this->GExtent, 6);
groundInfo->Set(CAN_PRODUCE_SUB_EXTENT(),1);
bladeInfo->Set(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(),
this->WholeExtent, 6);
// Simply loads the blade on all processes
bladeInfo->Set(CAN_HANDLE_PIECE_REQUEST(), 1);
// Create the rectilinear coordinate spacing for entire problem
this->CreateCoordinates();
// Collect temporal information and attach to both output ports
delete [] this->TimeSteps;
this->TimeSteps = NULL;
if (this->NumberOfTimeSteps > 0)
{
this->TimeSteps = new double[this->NumberOfTimeSteps];
this->TimeSteps[0] = (double) this->TimeStepFirst;
for (int step = 1; step < this->NumberOfTimeSteps; step++)
{
this->TimeSteps[step] = this->TimeSteps[step-1] +
(double) this->TimeStepDelta;
}
// Tell the pipeline what steps are available
fieldInfo->Set(vtkStreamingDemandDrivenPipeline::TIME_STEPS(),
this->TimeSteps, this->NumberOfTimeSteps);
bladeInfo->Set(vtkStreamingDemandDrivenPipeline::TIME_STEPS(),
this->TimeSteps, this->NumberOfTimeSteps);
// Range is required to get GUI to show things
double tRange[2];
tRange[0] = this->TimeSteps[0];
tRange[1] = this->TimeSteps[this->NumberOfTimeSteps - 1];
fieldInfo->Set(vtkStreamingDemandDrivenPipeline::TIME_RANGE(), tRange, 2);
bladeInfo->Set(vtkStreamingDemandDrivenPipeline::TIME_RANGE(), tRange, 2);
}
else
{
fieldInfo->Remove(vtkStreamingDemandDrivenPipeline::TIME_STEPS());
fieldInfo->Set(vtkStreamingDemandDrivenPipeline::TIME_STEPS(),
this->TimeSteps, this->NumberOfTimeSteps);
bladeInfo->Remove(vtkStreamingDemandDrivenPipeline::TIME_STEPS());
bladeInfo->Set(vtkStreamingDemandDrivenPipeline::TIME_STEPS(),
this->TimeSteps, this->NumberOfTimeSteps);
}
}
return 1;
}
//----------------------------------------------------------------------------
// Print information about WindBlade Reader
//----------------------------------------------------------------------------
void vtkWindBladeReader::PrintSelf(ostream &os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "Filename: "
<< (this->Filename ? this->Filename : "(NULL)") << endl;
os << indent << "WholeExent: {" << this->WholeExtent[0] << ", "
<< this->WholeExtent[1] << ", " << this->WholeExtent[2] << ", "
<< this->WholeExtent[3] << ", " << this->WholeExtent[4] << ", "
<< this->WholeExtent[5] << "}" << endl;
os << indent << "SubExtent: {" << this->SubExtent[0] << ", "
<< this->SubExtent[1] << ", " << this->SubExtent[2] << ", "
<< this->SubExtent[3] << ", " << this->SubExtent[4] << ", "
<< this->SubExtent[5] << "}" << endl;
os << indent << "VariableArraySelection:" << endl;
this->PointDataArraySelection->PrintSelf(os, indent.GetNextIndent());
}
//----------------------------------------------------------------------------
// RequestData populates the output object with data for rendering
// Uses three output ports (field, turbine blades, and ground).
// Field data is parallel, blade and ground only on processor 0
//----------------------------------------------------------------------------
int vtkWindBladeReader::RequestData(
vtkInformation *reqInfo,
vtkInformationVector **vtkNotUsed(inVector),
vtkInformationVector *outVector)
{
int port = reqInfo->Get(vtkDemandDrivenPipeline::FROM_OUTPUT_PORT());
// Request data for field port
if (port == 0)
{
std::ostringstream fileName;
vtkStructuredGrid *field = this->GetFieldOutput();
this->InitFieldData(outVector, fileName, field);
this->Internal->FilePtr = fopen(fileName.str().c_str(), "rb");
if (this->Internal->FilePtr == NULL)
{
vtkWarningMacro(<< "Could not open file " << fileName.str());
return 0;
}
this->SetUpFieldVars(field);
// Close file after all data is read
fclose(this->Internal->FilePtr);
return 1;
}
// Request data is on blade
// Even if the blade is turned off, it must update with time along with field
else if (port == 1)
{
if (this->UseTurbineFile == 1)
{
this->InitBladeData(outVector);
}
return 1;
}
// Request data in on ground
else if (port == 2)
{
this->SetUpGroundData(outVector);
}
return 1;
}
//----------------------------------------------------------------------------
// Divide data variable by density for display
//----------------------------------------------------------------------------
void vtkWindBladeReader::DivideByDensity(const char* varName)
{
int var = this->PointDataArraySelection->GetArrayIndex(varName);
int density = this->PointDataArraySelection->GetArrayIndex("Density");
float* varData = this->Data[var]->GetPointer(0);
float* densityData = this->Data[density]->GetPointer(0);
int numberOfTuples = this->Data[var]->GetNumberOfTuples();
int numberOfComponents = this->Data[var]->GetNumberOfComponents();
int index = 0;
for (int i = 0; i < numberOfTuples; i++)
{
for (int j = 0; j < numberOfComponents; j++)
{
varData[index++] /= densityData[i];
}
}
}
//----------------------------------------------------------------------------
// Calculate pressure from tempg and density
// Calculate pressure - pre from pressure in first z position
// Requires that all data be present
//----------------------------------------------------------------------------
void vtkWindBladeReader::CalculatePressure(int pressure, int prespre,
int tempg, int density)
{
float *pressureData = NULL, *prespreData = NULL;
this->InitPressureData(pressure, prespre, pressureData, prespreData);
// Read tempg and Density components from file
float* tempgData = new float[this->BlockSize];
float* densityData = new float[this->BlockSize];
fseek(this->Internal->FilePtr, this->VariableOffset[tempg], SEEK_SET);
if (fread(tempgData, sizeof(float), this->BlockSize, this->Internal->FilePtr) != this->BlockSize)
{
// This is really an error, but for the time being we report a
// warning
vtkWarningMacro ("WindBladeReader error reading file: " << this->Filename
<< " Premature EOF while reading tempgData.");
}
fseek(this->Internal->FilePtr, this->VariableOffset[density], SEEK_SET);
if (fread(densityData, sizeof(float), this->BlockSize, this->Internal->FilePtr) != this->BlockSize)
{
// This is really an error, but for the time being we report a
// warning
vtkWarningMacro ("WindBladeReader error reading file: " << this->Filename
<< " Premature EOF while reading densityData.");
}
// Only the requested subextents are stored on this processor
this->SetUpPressureData(pressureData, prespreData, tempgData, densityData);
delete [] tempgData;
delete [] densityData;
}
//----------------------------------------------------------------------------
// Calculate vorticity from UVW
// Requires ghost cell information so fetch all data from files for now
//----------------------------------------------------------------------------
void vtkWindBladeReader::CalculateVorticity(int vort, int uvw, int density)
{
// Set the number of components and tuples for the requested data
this->Data[vort]->SetNumberOfComponents(1);
this->Data[vort]->SetNumberOfTuples(this->NumberOfTuples);
float* vortData = this->Data[vort]->GetPointer(0);
// Read U and V components (two int block sizes in between)
float* uData = new float[this->BlockSize];
float* vData = new float[this->BlockSize];
fseek(this->Internal->FilePtr, this->VariableOffset[uvw], SEEK_SET);
if (fread(uData, sizeof(float), this->BlockSize, this->Internal->FilePtr) != this->BlockSize)
{
// This is really an error, but for the time being we report a
// warning
vtkWarningMacro ("WindBladeReader error reading file: " << this->Filename
<< " Premature EOF while reading uData.");
}
fseek(this->Internal->FilePtr, (2 * sizeof(int)), SEEK_SET);
if (fread(vData, sizeof(float), this->BlockSize, this->Internal->FilePtr) != this->BlockSize)
{
// This is really an error, but for the time being we report a
// warning
vtkWarningMacro ("WindBladeReader error reading file: " << this->Filename
<< " Premature EOF while reading vData.");
}
// Read Density component
float* densityData = new float[this->BlockSize];
fseek(this->Internal->FilePtr, this->VariableOffset[density], SEEK_SET);
if (fread(densityData, sizeof(float), this->BlockSize, this->Internal->FilePtr) != this->BlockSize)
{
// This is really an error, but for the time being we report a
// warning
vtkWarningMacro ("WindBladeReader error reading file: " << this->Filename
<< " Premature EOF while reading densityData.");
}
this->SetUpVorticityData(uData, vData, densityData, vortData);
delete [] uData;
delete [] vData;
delete [] densityData;
}
//----------------------------------------------------------------------------
// Load one variable data array of BLOCK structure into ParaView
//----------------------------------------------------------------------------
void vtkWindBladeReader::LoadVariableData(int var)
{
this->Data[var]->Delete();
this->Data[var] = vtkFloatArray::New();
this->Data[var]->SetName(VariableName[var].c_str());
// Skip to the appropriate variable block and read byte count
// not used? int byteCount;
fseek(this->Internal->FilePtr, this->VariableOffset[var], SEEK_SET);
int numberOfComponents = 0, planeSize = 0, rowSize;
float *varData = NULL;
float* block = new float[this->BlockSize];
this->InitVariableData(var, numberOfComponents, varData, planeSize, rowSize);
for (int comp = 0; comp < numberOfComponents; comp++)
{
// Read the block of data
size_t cnt;
if ((cnt = fread(block, sizeof(float), this->BlockSize, this->Internal->FilePtr)) !=
this->BlockSize )
{
// This is really an error, but for the time being we report a
// warning
vtkWarningMacro ("WindBladeReader error reading file: " << this->Filename
<< " Premature EOF while reading block of data."
<< " Expected " << this->BlockSize << " but got " << cnt);
}
int pos = comp;
for (int k = this->SubExtent[4]; k <= this->SubExtent[5]; k++)
{
for (int j = this->SubExtent[2]; j <= this->SubExtent[3]; j++)
{
for (int i = this->SubExtent[0]; i <= this->SubExtent[1]; i++)
{
int index = (k * planeSize) + (j * rowSize) + i;
varData[pos] = block[index];
pos += numberOfComponents;
}
}
}
// Skip closing and opening byte sizes
fseek(this->Internal->FilePtr, (2 * sizeof(int)), SEEK_CUR);
}
delete [] block;
}
//----------------------------------------------------------------------------
// Load one variable data array of BLOCK structure into ParaView
//----------------------------------------------------------------------------
bool vtkWindBladeReader::ReadGlobalData()
{
//kwsys_stl::string fileName = this->Filename;
std::string fileName = this->Filename;
vtksys::SystemTools::ConvertToUnixSlashes(fileName);
std::ifstream inStrStream(fileName.c_str());
std::stringstream inStr;
std::copy(std::istreambuf_iterator<char>(inStrStream),
std::istreambuf_iterator<char>(),
std::ostreambuf_iterator<char>(inStr));
return this->SetUpGlobalData(fileName, inStr);
}
//----------------------------------------------------------------------------
//
// Read the field variable information
//
//----------------------------------------------------------------------------
void vtkWindBladeReader::ReadDataVariables(istream& inStr)
{
char inBuf[LINE_SIZE];
std::string structType, basicType;
// Derive Vorticity = f(UVW, Density)
// Derive Pressure = f(tempg, Density)
// Derive Pressure - pre = f(Pressure)
this->NumberOfDerivedVariables = 3;
this->NumberOfVariables = this->NumberOfFileVariables;
int totalVariables = this->NumberOfFileVariables +
this->NumberOfDerivedVariables;
delete[] this->VariableName;
this->VariableName = new vtkStdString[totalVariables];
delete[] this->VariableStruct;
delete[] this->VariableCompSize;
delete[] this->VariableBasicType;
delete[] this->VariableByteCount;
delete[] this->VariableOffset;
this->VariableStruct = new int[totalVariables];
this->VariableCompSize = new int[totalVariables];
this->VariableBasicType = new int[totalVariables];
this->VariableByteCount = new int[totalVariables];
this->VariableOffset = new long int[totalVariables];
bool hasUVW = false;
bool hasDensity = false;
bool hasTempg = false;
for (int i = 0; i < this->NumberOfFileVariables; i++)
{
inStr.getline(inBuf, LINE_SIZE);
// Variable name
std::string varLine(inBuf);
std::string::size_type lastPos = varLine.rfind('"');
this->VariableName[i] = varLine.substr(1, lastPos-1);
if (this->VariableName[i] == "UVW")
{
hasUVW = true;
}
if (this->VariableName[i] == "Density")
{
hasDensity = true;
}
if (this->VariableName[i] == "tempg")
{
hasTempg = true;
}
// Structure, number of components, type, number of bytes
std::string rest = varLine.substr(lastPos+1);
std::istringstream line(rest);
line >> structType;
line >> this->VariableCompSize[i];
if (structType == "SCALAR")
{
this->VariableStruct[i] = SCALAR;
}
else if (structType == "VECTOR")
{
this->VariableStruct[i] = VECTOR;
}
else
{
vtkWarningMacro("Error in structure type " << structType);
}
line >> basicType;
line >> this->VariableByteCount[i];
if (basicType == "FLOAT")
{
this->VariableBasicType[i] = FLOAT;
}
else if (basicType == "INTEGER")
{
this->VariableBasicType[i] = INTEGER;
}
else
{
vtkWarningMacro("Error in basic type " << basicType);
}
}
// Add any derived variables
if (hasUVW && hasDensity)
{
this->VariableName[this->NumberOfVariables] = "Vorticity";
this->NumberOfVariables++;
}
if (hasTempg && hasDensity)
{
this->VariableName[this->NumberOfVariables] = "Pressure";
this->NumberOfVariables++;
this->VariableName[this->NumberOfVariables] = "Pressure-Pre";
this->NumberOfVariables++;
}
}
//----------------------------------------------------------------------------
//
// Open the first data file and verify that the data is where is should be
// Each data block is enclosed by two ints which record the number of bytes
// Save the file offset for each varible
//
//----------------------------------------------------------------------------
bool vtkWindBladeReader::FindVariableOffsets()
{
// Open the first data file
std::ostringstream fileName;
fileName << this->RootDirectory << "/"
<< this->DataDirectory << "/"
<< this->DataBaseName << this->TimeStepFirst;
this->Internal->FilePtr = fopen(fileName.str().c_str(), "rb");
if (this->Internal->FilePtr == NULL)
{
vtkErrorMacro("Could not open file " << fileName.str());
return false;
}
// Scan file recording offsets which points to the first data value
int byteCount;
if (fread(&byteCount, sizeof(int), 1, this->Internal->FilePtr) != 1)
{
// This is really an error, but for the time being we report a
// warning
vtkWarningMacro ("WindBladeReader error reading file: " << this->Filename
<< " Premature EOF while reading byteCount.");
}
this->BlockSize = static_cast<size_t>(byteCount / BYTES_PER_DATA);
for (int var = 0; var < this->NumberOfFileVariables; var++)
{
this->VariableOffset[var] = ftell(this->Internal->FilePtr);
// Skip over the SCALAR or VECTOR components for this variable
int numberOfComponents = 1;
if (this->VariableStruct[var] == VECTOR)
{
numberOfComponents = DIMENSION;
}
for (int comp = 0; comp < numberOfComponents; comp++)
{
// Skip data plus two integer byte counts
fseek(this->Internal->FilePtr, (byteCount+(2 * sizeof(int))), SEEK_CUR);
}
}
fclose(this->Internal->FilePtr);
return true;
}
//----------------------------------------------------------------------------
// Fill in the rectilinear points for the requested subextents
//----------------------------------------------------------------------------
void vtkWindBladeReader::FillCoordinates()
{
this->Points->Delete();
this->Points = vtkPoints::New();
// If dataset is flat, x and y are constant spacing, z is stretched
if (this->UseTopographyFile == 0)
{
// Save vtkPoints instead of spacing coordinates because topography file
// requires this to be vtkStructuredGrid and not vtkRectilinearGrid
for (int k = this->SubExtent[4]; k <= this->SubExtent[5]; k++)
{
float z = this->ZSpacing->GetValue(k);
for (int j = this->SubExtent[2]; j <= this->SubExtent[3]; j++)
{
float y = this->YSpacing->GetValue(j);
for (int i = this->SubExtent[0]; i <= this->SubExtent[1]; i++)
{
float x = this->XSpacing->GetValue(i);
this->Points->InsertNextPoint(x, y, z);
}
}
}
}
// If dataset is topographic, x and y are constant spacing
// Z data is calculated from an x by y topographic data file
else
{
int planeSize = this->Dimension[0] * this->Dimension[1];
int rowSize = this->Dimension[0];
for (int k = this->SubExtent[4]; k <= this->SubExtent[5]; k++)
{
for (int j = this->SubExtent[2]; j <= this->SubExtent[3]; j++)
{
float y = this->YSpacing->GetValue(j);
for (int i = this->SubExtent[0]; i <= this->SubExtent[1]; i++)
{
float x = this->XSpacing->GetValue(i);
int index = (k * planeSize) + (j * rowSize) + i;
this->Points->InsertNextPoint(x, y, this->ZTopographicValues[index]);
}
}
}
}
}
//----------------------------------------------------------------------------
// Fill in the rectilinear points for the requested subextents
//----------------------------------------------------------------------------
void vtkWindBladeReader::FillGroundCoordinates()
{
this->GPoints->Delete();
this->GPoints = vtkPoints::New();
// If dataset is flat, x and y are constant spacing, z is stretched
if (this->UseTopographyFile == 0)
{
// Save vtkPoints instead of spacing coordinates because topography file
// requires this to be vtkStructuredGrid and not vtkRectilinearGrid
for (int k = this->GSubExtent[4]; k <= this->GSubExtent[5]; k++)
{
float z = this->ZMinValue;
for (int j = this->GSubExtent[2]; j <= this->GSubExtent[3]; j++)
{
float y = this->YSpacing->GetValue(j);
for (int i = this->GSubExtent[0]; i <= this->GSubExtent[1]; i++)
{
float x = this->XSpacing->GetValue(i);
this->GPoints->InsertNextPoint(x, y, z);
}
}
}
}
// If dataset is topographic, x and y are constant spacing
// Z data is calculated from an x by y topographic data file
else
{
int planeSize = this->GDimension[0] * this->GDimension[1];
int rowSize = this->GDimension[0];
for (int k = this->GSubExtent[4]; k <= this->GSubExtent[5]; k++)
{
for (int j = this->GSubExtent[2]; j <= this->GSubExtent[3]; j++)
{
float y = this->YSpacing->GetValue(j);
for (int i = this->GSubExtent[0]; i <= this->GSubExtent[1]; i++)
{
float x = this->XSpacing->GetValue(i);
if (k == 0)
{
this->GPoints->InsertNextPoint(x, y, this->ZMinValue);
}
else
{
int indx = ((k - 1) * planeSize) + (j * rowSize) + i;
this->GPoints->InsertNextPoint(x, y, this->ZTopographicValues[indx]);
}
}
}
}
}
}
//----------------------------------------------------------------------------
// Calculate the Points for flat Rectilinear type grid or topographic
// generalized StructuredGrid which is what is being created here
//----------------------------------------------------------------------------
void vtkWindBladeReader::CreateCoordinates()
{
// If dataset is flat, x and y are constant spacing, z is stretched
if (this->UseTopographyFile == 0)
{
for (int i = 0; i < this->Dimension[0]; i++)
{
this->XSpacing->InsertNextValue(i * this->Step[0]);
}
for (int j = 0; j < this->Dimension[1]; j++)
{
this->YSpacing->InsertNextValue(j * this->Step[1]);
}
double maxZ = this->Step[2] * this->Dimension[2];
for (int k = 0; k < this->Dimension[2]; k++)
{
double zcoord = (k * this->Step[2]) + (0.5 * this->Step[2]);
double zcartesian = GDeform(zcoord, maxZ, 0);
this->ZSpacing->InsertNextValue(zcartesian);
}
}
// If dataset is topographic, x and y are constant spacing
// Z data is calculated from an x by y topographic data file
else
{
for (int i = 0; i < this->Dimension[0]; i++)
{
this->XSpacing->InsertNextValue(i * this->Step[0]);
}
for (int j = 0; j < this->Dimension[1]; j++)
{
this->YSpacing->InsertNextValue(j * this->Step[1]);
}
this->ZTopographicValues = new float[this->BlockSize];
this->CreateZTopography(this->ZTopographicValues);
this->ZMinValue = this->ZTopographicValues[0];
for (size_t k = 0; k < this->BlockSize; k++)
{
if (this->ZMinValue > this->ZTopographicValues[k])
{
this->ZMinValue = this->ZTopographicValues[k];
}
}
}
// Set the ground minimum
if (this->UseTopographyFile == 0 || this->UseTurbineFile == 1)
{
this->ZMinValue = -1.0;
}
}
//----------------------------------------------------------------------------
// Create the z topography from 2D (x,y) elevations and return in zData
//----------------------------------------------------------------------------
void vtkWindBladeReader::CreateZTopography(float* zValues)
{
// Read the x,y topography data file
std::ostringstream fileName;
fileName << this->RootDirectory << "/"
<< this->TopographyFile;
int blockSize = this->Dimension[0] * this->Dimension[1];
float* topoData = new float[blockSize];
FILE* filePtr = fopen(fileName.str().c_str(), "rb");
fseek(filePtr, BYTES_PER_DATA, SEEK_SET); // Fortran byte count
if(fread(topoData, sizeof(float), blockSize, filePtr) != static_cast<size_t>(blockSize) )
{
// This is really an error, but for the time being we report a
// warning
vtkWarningMacro ("WindBladeReader error reading file: " << this->Filename
<< " Premature EOF while reading topoData.");
}
this->ProcessZCoords(topoData, zValues);
delete [] topoData;
fclose(filePtr);
}
//----------------------------------------------------------------------------
//
// Stretch the Z coordinate for flat topography
// If flag = 0 compute gdeform(z)
// If flag = 1 compute derivative of gdeform(z)
// Return cubic polynomial fit
//
//----------------------------------------------------------------------------
float vtkWindBladeReader::GDeform(float sigma, float sigmaMax, int flag)
{
float sigma_2 = sigma * sigma;
float sigma_3 = sigma_2 * sigma;
float f = this->Fit;
float aa1 = this->Compression;