-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvtkTipsyReader.cxx
579 lines (550 loc) · 19.7 KB
/
vtkTipsyReader.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
/*=========================================================================
Program: AstroViz plugin for ParaView
Module: $RCSfile: vtkTipsyReader.cxx,v $
=========================================================================*/
#include "vtkTipsyReader.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkPolyData.h"
#include "vtkPoints.h"
#include "vtkCellArray.h"
#include "vtkFloatArray.h"
#include "vtkIntArray.h"
#include "vtkStreamingDemandDrivenPipeline.h"
#include "vtkDistributedDataFilter.h"
#include "vtkMultiProcessController.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkSmartPointer.h"
#include "vtkDataArraySelection.h"
#include <cmath>
#include <assert.h>
vtkCxxRevisionMacro(vtkTipsyReader, "$Revision: 1.0 $");
vtkStandardNewMacro(vtkTipsyReader);
//----------------------------------------------------------------------------
vtkSmartPointer<vtkFloatArray> AllocateDataArray(
vtkDataSet *output, const char* arrayName, int numComponents, unsigned long numTuples)
{
vtkSmartPointer<vtkFloatArray> dataArray=vtkSmartPointer<vtkFloatArray>::New();
dataArray->SetNumberOfComponents(numComponents);
dataArray->SetNumberOfTuples(numTuples);
dataArray->SetName(arrayName);
// initializes everything to zero
for (int i=0; i < numComponents; ++i) {
dataArray->FillComponent(i, 0.0);
}
output->GetPointData()->AddArray(dataArray);
return dataArray;
}
//----------------------------------------------------------------------------
vtkTipsyReader::vtkTipsyReader()
{
this->MarkFileName = 0; // this file is optional
this->FileName = 0;
this->DistributeDataOn = 1;
this->UpdatePiece = 0;
this->UpdateNumPieces = 0;
this->SetNumberOfInputPorts(0);
//
this->PointDataArraySelection = vtkDataArraySelection::New();
//
this->Positions = NULL;
this->Vertices = NULL;
this->GlobalIds = NULL;
this->ParticleIndex = 0;
this->Potential = NULL;
this->Mass = NULL;
this->EPS = NULL;
this->RHO = NULL;
this->Hsmooth = NULL;
this->Temperature = NULL;
this->Metals = NULL;
this->Tform = NULL;
this->Velocity = NULL;
}
//----------------------------------------------------------------------------
vtkTipsyReader::~vtkTipsyReader()
{
this->SetFileName(0);
this->SetMarkFileName(0);
this->SetDistributeDataOn(0);
this->PointDataArraySelection->Delete();
}
//----------------------------------------------------------------------------
void vtkTipsyReader::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "FileName: "
<< (this->FileName ? this->FileName : "(none)") << "\n"
<< indent << "MarkFileName: "
<< (this->MarkFileName ? this->MarkFileName : "(none)") << "\n";
}
//----------------------------------------------------------------------------
TipsyHeader vtkTipsyReader::ReadTipsyHeader(ifTipsy& tipsyInfile)
{
// Initializing for reading
TipsyHeader h;
// Reading in the header
tipsyInfile >> h;
return h;
}
//----------------------------------------------------------------------------
vtkstd::vector<unsigned long> vtkTipsyReader::ReadMarkedParticleIndices(
TipsyHeader& tipsyHeader,ifTipsy& tipsyInfile)
{
ifstream markInFile(this->MarkFileName);
vtkstd::vector<unsigned long> markedParticleIndices;
if(!markInFile)
{
vtkErrorMacro("Error opening marked particle file: "
<< this->MarkFileName
<< " please specify a valid mark file or none at all.\
For now reading all particles.");
return markedParticleIndices;
}
else
{
unsigned long mfIndex,mfBodies,mfGas,mfStar,mfDark,numBodies;
// first line of the mark file is of a different format:
// intNumBodies intNumGas intNumStars
if(markInFile >> mfBodies >> mfGas >> mfStar)
{
mfDark=mfBodies-mfGas-mfStar;
if(mfBodies!=tipsyHeader.h_nBodies || mfDark!=tipsyHeader.h_nDark \
|| mfGas!=tipsyHeader.h_nSph || mfStar!=tipsyHeader.h_nStar)
{
vtkErrorMacro("Error opening marked particle file, wrong format,\
number of particles do not match Tipsy file: "
<< this->MarkFileName
<< " please specify a valid mark file or none at all.\
For now reading all particles.");
}
else
{
// The rest of the file is is format: intMarkIndex\n
// Read in the rest of the file file, noting the marked particles
while(markInFile >> mfIndex)
{
// Insert the next marked particle index into the vector
// subtracting one as the indices in marked particle file
// begin at 1, not 0
markedParticleIndices.push_back(mfIndex-1);
}
// closing file
markInFile.close();
// read file successfully
vtkDebugMacro("Read " << numBodies<< " marked point indices.");
}
}
}
//empty if none were read, otherwise size gives number of marked particles
return markedParticleIndices;
}
//----------------------------------------------------------------------------
void vtkTipsyReader::ReadAllParticles(TipsyHeader& tipsyHeader,
ifTipsy& tipsyInfile,int piece,int numpieces,vtkPolyData* output)
{
unsigned long pieceSize = floor(tipsyHeader.h_nBodies*1./numpieces);
unsigned long beginIndex = piece*pieceSize;
unsigned long endIndex = (piece == numpieces - 1) ? \
tipsyHeader.h_nBodies : (piece+1)*pieceSize;
// Allocates vtk scalars and vector arrays to hold particle data,
this->AllocateAllTipsyVariableArrays(endIndex-beginIndex,output);
for(unsigned long i=beginIndex; i < endIndex; i++)
{
this->ReadParticle(i,tipsyHeader,tipsyInfile,output);
}
}
//----------------------------------------------------------------------------
void vtkTipsyReader::ReadMarkedParticles(
vtkstd::vector<unsigned long>& markedParticleIndices,
TipsyHeader& tipsyHeader,
ifTipsy& tipsyInfile,
vtkPolyData* output)
{
// Allocates vtk scalars and vector arrays to hold particle data,
// As marked file was read, only allocates numBodies which
// now equals the number of marked particles
this->AllocateAllTipsyVariableArrays(markedParticleIndices.size(),output);
unsigned long nextMarkedParticleIndex=0;
for(vtkstd::vector<unsigned long>::iterator it = \
markedParticleIndices.begin(); it != markedParticleIndices.end(); ++it)
{
nextMarkedParticleIndex=*it;
// reading in the particle
this->ReadParticle(nextMarkedParticleIndex,tipsyHeader,
tipsyInfile,output);
}
}
//----------------------------------------------------------------------------
tipsypos::section_type vtkTipsyReader::SeekToIndex(unsigned long index,
TipsyHeader& tipsyHeader, ifTipsy& tipsyInfile)
{
if(index < tipsyHeader.h_nSph)
{
// we are seeking a gas particle
tipsyInfile.seekg(tipsypos(tipsypos::gas,index));
return tipsypos::gas;
}
else if(index < tipsyHeader.h_nDark+tipsyHeader.h_nSph)
{
// we are seeking a dark particle, which begin at index 0 after
// gas particles
index-=tipsyHeader.h_nSph;
tipsyInfile.seekg(tipsypos(tipsypos::dark,index));
return tipsypos::dark;
}
else if(index < tipsyHeader.h_nBodies)
{
// we are seeking a star particle, which begin at index zero after gas
// and dark particles
index-=(tipsyHeader.h_nSph+tipsyHeader.h_nDark);
tipsyInfile.seekg(tipsypos(tipsypos::star,index));
return tipsypos::star;
}
else
{
vtkErrorMacro("An index is greater than the number of particles in the file, unable to read");
return tipsypos::invalid;
}
}
//----------------------------------------------------------------------------
//i,tipsyHeader,tipsyInfile,output)
vtkIdType vtkTipsyReader::ReadParticle(unsigned long index,
TipsyHeader& tipsyHeader, ifTipsy& tipsyInfile, vtkPolyData* output)
{
// allocating variables for reading
vtkIdType id;
TipsyGasParticle g;
TipsyDarkParticle d;
TipsyStarParticle s;
tipsypos::section_type particleSection=this->SeekToIndex(index,
tipsyHeader,tipsyInfile);
switch(particleSection)
{
case tipsypos::gas:
tipsyInfile >> g;
id=this->ReadGasParticle(output,g);
break;
case tipsypos::dark:
tipsyInfile >> d;
id=this->ReadDarkParticle(output,d);
break;
case tipsypos::star:
tipsyInfile >> s;
id=this->ReadStarParticle(output,s);
break;
default:
assert(0);
break;
}
this->GlobalIds->SetValue(id,index);
return id;
}
//----------------------------------------------------------------------------
vtkIdType vtkTipsyReader::ReadBaseParticle(vtkPolyData* output,
TipsyBaseParticle& b)
{
this->Positions->SetPoint(ParticleIndex, b.pos);
//
if (this->Velocity) this->Velocity->SetTuple(ParticleIndex, b.vel);
if (this->Mass) this->Mass->SetTuple1(ParticleIndex, b.mass);
if (this->Potential) this->Potential->SetTuple1(ParticleIndex, b.phi);
return ParticleIndex++;
}
//----------------------------------------------------------------------------
vtkIdType vtkTipsyReader::ReadGasParticle(vtkPolyData* output,
TipsyGasParticle& g)
{
vtkIdType id=ReadBaseParticle(output,g);
if (this->RHO) this->RHO->SetTuple(id, &g.rho);
if (this->Temperature) this->Temperature->SetTuple1(id, g.temp);
if (this->Hsmooth) this->Hsmooth->SetTuple1(id, g.hsmooth);
if (this->Metals) this->Metals->SetTuple1(id, g.metals);
if (this->Type) this->Type->SetTuple1(id, 0.0);
return id;
}
//----------------------------------------------------------------------------
vtkIdType vtkTipsyReader::ReadStarParticle(vtkPolyData* output,
TipsyStarParticle& s)
{
vtkIdType id=ReadBaseParticle(output,s);
if (this->EPS) this->EPS->SetTuple1(id, s.eps);
if (this->Metals) this->Metals->SetTuple1(id, s.metals);
if (this->Tform) this->Tform->SetTuple1(id, s.tform);
if (this->Type) this->Type->SetTuple1(id, 2.0);
return id;
}
//----------------------------------------------------------------------------
vtkIdType vtkTipsyReader::ReadDarkParticle(vtkPolyData* output,
TipsyDarkParticle& d)
{
vtkIdType id=this->ReadBaseParticle(output,d);
if (this->EPS) this->EPS->SetTuple1(id, d.eps);
if (this->Type) this->Type->SetTuple1(id, 1.0);
return id;
}
//----------------------------------------------------------------------------
void vtkTipsyReader::AllocateAllTipsyVariableArrays(vtkIdType numBodies,
vtkPolyData* output)
{
// Allocate objects to hold points and vertex cells.
this->Positions = vtkSmartPointer<vtkPoints>::New();
this->Positions->SetDataTypeToFloat();
this->Positions->SetNumberOfPoints(numBodies);
//
this->Vertices = vtkSmartPointer<vtkCellArray>::New();
vtkIdType *cells = this->Vertices->WritePointer(numBodies, numBodies*2);
for (vtkIdType i=0; i<numBodies; ++i) {
cells[i*2] = 1;
cells[i*2+1] = i;
}
//
this->GlobalIds = vtkSmartPointer<vtkIdTypeArray>::New();
this->GlobalIds->SetName("global_id");
this->GlobalIds->SetNumberOfTuples(numBodies);
// Storing the points and cells in the output data object.
output->SetPoints(this->Positions);
output->SetVerts(this->Vertices);
// allocate velocity first as it uses the most memory and on my win32 machine
// this helps load really big data without alloc failures.
if (this->GetPointArrayStatus("Velocity"))
this->Velocity = AllocateDataArray(output,"velocity",3,numBodies);
else
this->Velocity = NULL;
if (this->GetPointArrayStatus("Potential"))
this->Potential = AllocateDataArray(output,"potential",1,numBodies);
else
this->Potential = NULL;
if (this->GetPointArrayStatus("Mass"))
this->Mass = AllocateDataArray(output,"mass",1,numBodies);
else
this->Mass = NULL;
if (this->GetPointArrayStatus("Eps"))
this->EPS = AllocateDataArray(output,"eps",1,numBodies);
else
this->EPS = NULL;
if (this->GetPointArrayStatus("Rho"))
this->RHO = AllocateDataArray(output,"rho",1,numBodies);
else
this->RHO = NULL;
if (this->GetPointArrayStatus("Hsmooth"))
this->Hsmooth = AllocateDataArray(output,"hsmooth",1,numBodies);
else
this->Hsmooth = NULL;
if (this->GetPointArrayStatus("Temperature"))
this->Temperature = AllocateDataArray(output,"temperature",1,numBodies);
else
this->Temperature = NULL;
if (this->GetPointArrayStatus("Metals"))
this->Metals = AllocateDataArray(output,"metals",1,numBodies);
else
this->Metals = NULL;
if (this->GetPointArrayStatus("Tform"))
this->Tform = AllocateDataArray(output,"tform",1,numBodies);
else
this->Tform = NULL;
if (this->GetPointArrayStatus("Type"))
this->Type = AllocateDataArray(output,"type",1,numBodies);
else
this->Type = NULL;
}
//----------------------------------------------------------------------------
int vtkTipsyReader::RequestInformation(
vtkInformation* vtkNotUsed(request),
vtkInformationVector** vtkNotUsed(inputVector),
vtkInformationVector* outputVector)
{
vtkInformation* outInfo = outputVector->GetInformationObject(0);
// means that the data set can be divided into an arbitrary number of pieces
outInfo->Set(vtkStreamingDemandDrivenPipeline::MAXIMUM_NUMBER_OF_PIECES(),
-1);
this->PointDataArraySelection->AddArray("Potential");
this->PointDataArraySelection->AddArray("Mass");
this->PointDataArraySelection->AddArray("Eps");
this->PointDataArraySelection->AddArray("Rho");
this->PointDataArraySelection->AddArray("Hsmooth");
this->PointDataArraySelection->AddArray("Temperature");
this->PointDataArraySelection->AddArray("Metals");
this->PointDataArraySelection->AddArray("Tform");
this->PointDataArraySelection->AddArray("Type");
this->PointDataArraySelection->AddArray("Velocity");
return 1;
}
/*
* Reads a file, optionally only the marked particles from the file,
* in the following order:
* 1. Open Tipsy binary
* 2. Read Tipsy header (tells us the number of particles of each type we are
* dealing with)
* NOTE: steps 3, 5 are currently not parallel
* 3. Read mark file indices from marked particle file, if there is one
* 4. Read either marked particles only or all particles
* 5. If an attribute file is additionally specified, reads this additional
* attribute into a data array, reading only those marked if necessary.
*/
//----------------------------------------------------------------------------
int vtkTipsyReader::RequestData(vtkInformation*,
vtkInformationVector**,vtkInformationVector* outputVector)
{
//
// Make sure we have a file to read.
//
if(!this->FileName)
{
vtkErrorMacro("A FileName must be specified.");
return 0;
}
// Open the tipsy standard file and abort if there is an error.
ifTipsy tipsyInfile;
tipsyInfile.open(this->FileName,"standard");
if (!tipsyInfile.is_open())
{
vtkErrorMacro("Error opening file " << this->FileName);
return 0;
}
// Get output information
vtkInformation* outInfo = outputVector->GetInformationObject(0);
// get the output polydata
vtkPolyData *output = vtkPolyData::SafeDownCast(outInfo->Get(vtkDataObject::DATA_OBJECT()));
vtkSmartPointer<vtkPolyData> tipsyReadInitialOutput = vtkSmartPointer<vtkPolyData>::New();
// get this->UpdatePiece information
this->UpdatePiece = outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_PIECE_NUMBER());
this->UpdateNumPieces =outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_NUMBER_OF_PIECES());
// reset counter before reading
this->ParticleIndex = 0;
// Read the header from the input
TipsyHeader tipsyHeader=this->ReadTipsyHeader(tipsyInfile);
// Next considering whether to read in a mark file,
// and if so whether that reading was a success
vtkstd::vector<unsigned long> markedParticleIndices;
if(strcmp(this->MarkFileName,"")!=0)
{
// Reading only marked particles
// Make sure we are not running in parallel, this filter does not work in
// parallel
if(this->UpdateNumPieces>1)
{
vtkErrorMacro("Reading from a mark file is not supported in parallel.");
return 0;
}
vtkDebugMacro("Reading marked point indices from file:"
<< this->MarkFileName);
markedParticleIndices=this->ReadMarkedParticleIndices(tipsyHeader,
tipsyInfile);
}
// Read every particle and add their position to be displayed,
// as well as relevant scalars
if(markedParticleIndices.empty())
{
// no marked particle file or there was an error reading the mark file,
// so reading all particles
vtkDebugMacro("Reading all points from file " << this->FileName);
this->ReadAllParticles(tipsyHeader,tipsyInfile, this->UpdatePiece, this->UpdateNumPieces, tipsyReadInitialOutput);
}
else
{
//reading only marked particles
assert(this->UpdateNumPieces==1);
vtkDebugMacro("Reading only the marked points in file: " \
<< this->MarkFileName << " from file " << this->FileName);
this->ReadMarkedParticles(markedParticleIndices, tipsyHeader,tipsyInfile, tipsyReadInitialOutput);
}
// Close the tipsy in file.
tipsyInfile.close();
// If we need to, run D3 on the tipsyReadInitialOutput
// producing one level of ghost cells
if (this->GetDistributeDataOn() && this->UpdateNumPieces>1)
{
vtkSmartPointer<vtkDistributedDataFilter> d3 = \
vtkSmartPointer<vtkDistributedDataFilter>::New();
d3->AddInput(tipsyReadInitialOutput);
d3->UpdateInformation();
d3->Update();
// Changing output to output of d3
output->ShallowCopy(d3->GetOutput());
// create new vertices because the D3 outputs UnstructuredGrid
vtkIdType N = output->GetNumberOfPoints();
this->Vertices = vtkSmartPointer<vtkCellArray>::New();
vtkIdType *cells = this->Vertices->WritePointer(N, N*2);
for (vtkIdType i=0; i<N; ++i) {
cells[i*2] = 1;
cells[i*2+1] = i;
}
output->SetVerts(this->Vertices);
}
else
{
output->ShallowCopy(tipsyReadInitialOutput);
}
// Read Successfully
vtkDebugMacro("Read " << output->GetPoints()->GetNumberOfPoints() \
<< " points.");
//
// release memory smartpointers - just to play safe.
this->Vertices = NULL;
this->GlobalIds = NULL;
this->Positions = NULL;
this->Potential = NULL;
this->Mass = NULL;
this->EPS = NULL;
this->RHO = NULL;
this->Hsmooth = NULL;
this->Temperature = NULL;
this->Metals = NULL;
this->Tform = NULL;
this->Type = NULL;
this->Velocity = NULL;
//
return 1;
}
//----------------------------------------------------------------------------
// Below : Boiler plate code to handle selection of point arrays
//----------------------------------------------------------------------------
const char* vtkTipsyReader::GetPointArrayName(int index)
{
return this->PointDataArraySelection->GetArrayName(index);
}
//----------------------------------------------------------------------------
int vtkTipsyReader::GetPointArrayStatus(const char* name)
{
return this->PointDataArraySelection->ArrayIsEnabled(name);
}
//----------------------------------------------------------------------------
void vtkTipsyReader::SetPointArrayStatus(const char* name, int status)
{
if (status!=this->GetPointArrayStatus(name)) {
if (status) {
this->PointDataArraySelection->EnableArray(name);
}
else {
this->PointDataArraySelection->DisableArray(name);
}
this->Modified();
}
}
//----------------------------------------------------------------------------
void vtkTipsyReader::Enable(const char* name)
{
this->SetPointArrayStatus(name, 1);
}
//----------------------------------------------------------------------------
void vtkTipsyReader::Disable(const char* name)
{
this->SetPointArrayStatus(name, 0);
}
//----------------------------------------------------------------------------
void vtkTipsyReader::EnableAll()
{
this->PointDataArraySelection->EnableAllArrays();
}
//----------------------------------------------------------------------------
void vtkTipsyReader::DisableAll()
{
this->PointDataArraySelection->DisableAllArrays();
}
//----------------------------------------------------------------------------
int vtkTipsyReader::GetNumberOfPointArrays()
{
return this->PointDataArraySelection->GetNumberOfArrays();
}