00001 #include "FlowData.h"
00002 #include "reverseBytes.h"
00003
00004
00005 #include <iostream>
00006 #include <fstream>
00007
00008
00009 FlowData::FlowData()
00010 {
00012 geometry = new FlowGeometry();
00013
00014
00015 for(int i = 0; i < max_channels; i++)
00016 {
00017 channels[i] = NULL;
00018 freeChannel[i] = true;
00019 scalarChannel[i] = NONE;
00020 }
00021 }
00022
00023 FlowData::~FlowData()
00024 {
00026 delete geometry;
00027
00028
00029 for(int i = 0; i < max_channels; i++)
00030 {
00031 if (!freeChannel[i])
00032 {
00033 deleteChannel(i);
00034 }
00035 }
00036 }
00037
00038 bool FlowData::loadDataset( string filename, bool bigEndian )
00039 {
00040 FILE* griFile = NULL;
00041 FILE* datFile = NULL;
00042
00043 char header[40];
00044
00045
00046 std::cout << "\n\n ------- READ FILE ------- " << std::endl;
00047
00048
00049 int lastdot = (int) filename.find_last_of(".",filename.length()-1);
00050 if (lastdot != string::npos)
00051
00052 filename = filename.substr(0,lastdot);
00053
00054
00056
00058 string griName = filename + ".gri";
00059 std::cout << "- Loading grid file '" << griName << "' ... " << std::endl;
00060
00061
00062 griFile = fopen(griName.c_str(),"rb");
00063
00064 if (!griFile)
00065 {
00066 std::cerr << "+ Error loading grid file:" << griName << std::endl << std::endl;
00067 return false;
00068 }
00069
00070
00071 fread( header, 40, 1, griFile );
00072
00073
00074 if ( !geometry->readFromFile( header, griFile, bigEndian ) )
00075 return false;
00076
00077
00078 fclose( griFile );
00079
00080
00081
00082
00083
00085
00087
00088 std::cout << "\n\n ------- Loading data file ------- " << std::endl;
00089
00090 float DT;
00091
00092
00093 sscanf( header, "SN4DB %d %d %d %d %d %f", &dimX, &dimY, &dimZ, &numChannels, ×teps, &DT );
00094 std::cout << "Dimensions: (" << dimX << ", " << dimY << ", " << dimZ << ")" << std::endl;
00095 std::cout << "Channels: " << numChannels << "\nTimesteps: " << timesteps << std::endl;
00096
00097
00098 char suffix[16];
00099 sprintf(suffix,".%.5u.dat",0);
00100 string datName = filename.append(suffix);
00101 std::cout << "- Loading grid file '" << datName << "' ... " << std::endl;
00102
00103
00104
00105 datFile = fopen( datName.c_str(), "rb" );
00106 if ( !datFile )
00107 {
00108 std::cerr << "+ Error loading dat file:" << datName << std::endl << std::endl;
00109 return false;
00110 }
00111
00112 numScalarChannels = numChannels;
00113 numChannels += 3;
00114
00115
00116
00117
00118
00119 float* tmpArray = new float[ numChannels * geometry->getDimX() * geometry->getDimY() ];
00120
00121
00122 int result = (int) fread( tmpArray, sizeof(float), numChannels * geometry->getDimX() * geometry->getDimY(), datFile );
00123
00124
00125 if (result != numChannels * geometry->getDimX() * geometry->getDimY() )
00126 {
00127 std::cerr << "+ Error reading dat file:" << datName << std::endl << std::endl;
00128 return false;
00129 }
00130
00131
00132 fclose( datFile );
00133
00134
00135
00136 if ( bigEndian )
00137 for(int j = 0; j < numChannels*geometry->getDimX()*geometry->getDimY(); j++)
00138 tmpArray[j] = reverseBytes<float>(tmpArray[j]);
00139
00140
00141
00142
00143 int currentChannel = -1;
00144 string channelName = "unkonwn channel data";
00145
00146 int channelsToLoad = numChannels;
00147 numChannels = 0;
00148
00149
00150 for ( int j=0; j<channelsToLoad; j++ )
00151 {
00152
00153
00154 switch ( j )
00155 {
00156 case 0:
00157 channelName = "Velocity-X";
00158 break;
00159
00160 case 1:
00161 channelName = "Velocity-Y";
00162 break;
00163
00164 case 2:
00165 channelName = "Velocity-Z";
00166 break;
00167
00168 case 3:
00169 channelName = "Pressure";
00170 scalarChannel[j] = ORIGINAL;
00171 break;
00172
00173 case 4:
00174 channelName = "Vorticity";
00175 scalarChannel[j] = ORIGINAL;
00176 break;
00177
00178 default:
00179 channelName = "unknown data";
00180 break;
00181 }
00182
00183
00184 currentChannel = createChannel( channelName );
00185
00186
00187 channels[ currentChannel ]->copyValues( tmpArray, channelsToLoad, j );
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277 if ( geometry->getIsFlipped() )
00278 {
00279 std::cout << "Flipping data-channel..." << std::endl;
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302 float *newArray = new float[ dimX*dimY ];
00303 int newIndex = -1;
00304 int oldIndex = -1;
00305
00306 for ( int y=0; y<dimY; y++ )
00307 for ( int x=0; x<dimX; x++ )
00308 {
00309 newIndex = y + x * dimY;
00310 oldIndex = x + y * dimX;
00311
00312 newArray[ newIndex ] = channels[ currentChannel ]->getValue( oldIndex );
00313
00314
00315
00316 }
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331 channels[ currentChannel ]->copyValues( newArray );
00332
00333 delete [] newArray;
00334 }
00335
00336
00337 }
00338
00339
00340
00341
00342 if ( geometry->getIsFlipped() )
00343 {
00344 int temp = dimX;
00345 dimX = dimY;
00346 dimY = temp;
00347 }
00348
00349
00350
00351 for ( int i=3; i<(numScalarChannels+3); i++ )
00352 {
00353 std::cout << "Creating a new channel for normalized data of " << channelNames[i] << "..." << std::endl;
00354
00355
00356
00357 float *normArray = new float[ dimX*dimY ];
00358
00359 for ( int j=0; j<dimX*dimY; j++ )
00360 {
00361
00362 float data = channels[ i ]->getValue( j );
00363
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375 normArray[ j ] = channels[ i ]->normalizeValue( data );
00376 }
00377
00378
00379 int newChannel = createChannel( "Normalized " + channelNames[i] );
00380 channels[ newChannel ]->copyValues( normArray );
00381
00382
00383 scalarChannel[ newChannel ] = NORMALIZED;
00384
00385 delete [] normArray;
00386 }
00387
00388
00389
00390 velocityMagnitudeId = createChannelVectorLength( X, Y, Z );
00391
00392
00393 normalizeVelocityChannels( X, Y, Z );
00394
00395 delete[] tmpArray;
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410 std::cout << " ------- Done loading data file ------- \n\n" << std::endl;
00411
00412
00413 return true;
00414 }
00415
00416
00417
00418 void FlowData::printData()
00419 {
00420 std::cout << "Writing log-file (this may take a while) ... please be patient!";
00421
00422 ofstream dataLogFile;
00423
00424 dataLogFile.open ("data.log");
00425 dataLogFile << "Channel-Data with their corresponding indices in channels:\n";
00426
00427
00428 for ( int j=0; j<numChannels; j++ )
00429 {
00430 dataLogFile << "\nChannel " << j << " - " << channelNames[j] << ":\n";
00431
00432
00433 for ( int y=0; y<dimY; y++ )
00434 {
00435 dataLogFile << "\n\nY: " << y << ":\n";
00436
00437 for ( int x=0; x<dimX; x++ )
00438 {
00439 dataLogFile << x << ": " << channels[j]->getValue( y*dimX + x ) << "; ";
00440 if ( (x % 10) == 9 )
00441 dataLogFile << "\n";
00442 }
00443 }
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454 }
00455
00456 dataLogFile.close();
00457
00458 std::cout << " - done." << std::endl;
00459 }
00460
00461 ScalarDataT FlowData::getIsScalar( int i )
00462 {
00463 if( i >= max_channels )
00464 {
00465 return NONE;
00466 }
00467 return scalarChannel[i];
00468 }
00469
00470
00471 int FlowData::getChannelCount()
00472 {
00473 return numChannels;
00474 }
00475
00476
00477
00478
00479
00480
00481
00482
00483 int FlowData::createChannel( string channelName )
00484 {
00485
00486 int i = 0;
00487
00488 while ( (i<max_channels) && (!freeChannel[i]) ) i++;
00489
00490
00491 if ( i<max_channels )
00492 {
00493 std::cout << "Creating channel " << channelName << " at " << i << std::endl;
00494
00495
00496 channels[i] = new FlowChannel(geometry);
00497
00498
00499 freeChannel[i] = false;
00500
00501
00502 channelNames[i] = channelName;
00503
00504
00505 ++numChannels;
00506
00507
00508 return i;
00509 }
00510 else
00511 {
00512 std::cerr << "There is no free channel slot!" << std::endl;
00513 return -1;
00514 }
00515 }
00516
00517
00518 void FlowData::deleteChannel( int i )
00519 {
00520 if ( !freeChannel[i] )
00521 {
00522 std::cout << "Deleting channel at " << i << " ... ";
00523
00524
00525 delete channels[i];
00526
00527 channels[i] = NULL;
00528
00529
00530 freeChannel[i] = true;
00531 }
00532 else
00533 std::cout << "Tried to delete a non-existing channel at " << i << "." << std::endl;
00534 }
00535
00536
00537 FlowChannel* FlowData::getChannel( int i )
00538 {
00539 return channels[i];
00540 }
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554 int FlowData::createChannelVectorLength( FlowChannel* chX, FlowChannel* chY, FlowChannel* chZ )
00555 {
00556 int newChannel = createChannel( "Velocity-Length" );
00557
00558
00559 if (chZ)
00560 {
00561 for (int i = 0; i < geometry->getDimX()*geometry->getDimY(); i++)
00562 {
00563
00564 channels[newChannel]->setValue(i,sqrt(chX->getValue(i)*chX->getValue(i) + chY->getValue(i)*chY->getValue(i) + chZ->getValue(i)*chZ->getValue(i)));
00565 }
00566 }
00567 else
00568 {
00569 for (int i = 0; i < geometry->getDimX()*geometry->getDimY(); i++)
00570 {
00571 channels[newChannel]->setValue(i,sqrt(chX->getValue(i)*chX->getValue(i) + chY->getValue(i)*chY->getValue(i)));
00572 }
00573 }
00574 return newChannel;
00575 }
00576
00577
00578 int FlowData::createChannelVectorLength( int chX, int chY, int chZ )
00579 {
00580
00581 if (chZ >= 0)
00582 {
00583 return createChannelVectorLength(getChannel(chX),getChannel(chY),getChannel(chZ));
00584 }
00585 else
00586 {
00587 return createChannelVectorLength(getChannel(chX),getChannel(chY));
00588 }
00589 }
00590
00591
00592 void FlowData::normalizeVelocityChannels(FlowChannel* chX, FlowChannel* chY, FlowChannel* chZ)
00593 {
00594 if (chZ)
00595 {
00596 for (int i = 0; i < geometry->getDimX()*geometry->getDimY(); i++)
00597 {
00598
00599 vec3 velocity = vec3( chX->getValue(i), chY->getValue(i), chZ->getValue(i) );
00600
00601 velocity.normalize();
00602
00603 chX->setValue( i, velocity[X] );
00604 chY->setValue( i, velocity[Y] );
00605 chZ->setValue( i, velocity[Z] );
00606 }
00607 }
00608 else
00609 {
00610 for (int i = 0; i < geometry->getDimX()*geometry->getDimY(); i++)
00611 {
00612
00613 vec3 velocity = vec3( chX->getValue(i), chY->getValue(i) );
00614 velocity.normalize();
00615
00616 chX->setValue( i, velocity[X] );
00617 chY->setValue( i, velocity[Y] );
00618 }
00619 }
00620 }
00621
00622
00623 void FlowData::normalizeVelocityChannels(int chX, int chY, int chZ)
00624 {
00625 std::cout << "Normalizing (velocity-)channels "
00626 << chX << ", "
00627 << chY << ", "
00628 << chZ << "." << std::endl;
00629
00631 if (chZ >= 0)
00632 {
00633 normalizeVelocityChannels(getChannel(chX),getChannel(chY),getChannel(chZ));
00634 }
00635 else
00636 {
00637 normalizeVelocityChannels(getChannel(chX),getChannel(chY));
00638 }
00639 }
00640
00642 #if 0
00643 int FlowData::createChannelVectorLength(FlowChannel* chX, FlowChannel* chY, FlowChannel* chZ)
00644 {
00645 int result = createChannel();
00646 float mag;
00647 vec3 vel;
00648
00649 if (chZ)
00650 {
00651 for (int i = 0; i < geometry->getDimX() * geometry->getDimY(); i++)
00652 {
00653
00654 vel.v[X] = chX->getValue(i);
00655 vel.v[Y] = -chY->getValue(i);
00656 vel.v[Z] = chZ->getValue(i);
00657
00658
00659 mag = vel.length();
00660
00661
00662 velocity[i].magnitude = mag;
00663 channels[result]->setValue(i, mag);
00664
00665
00666 vel.normalize();
00667 velocity[i].normVelocity = vel;
00668 }
00669 }
00670 else
00671 {
00672 for (int i = 0; i < geometry->getDimX() * geometry->getDimY(); i++)
00673 {
00674
00675 vel.v[X] = chX->getValue(i);
00676 vel.v[Y] = chY->getValue(i);
00677
00678
00679 mag = vel.length();
00680
00681
00682 velocity[i].magnitude = mag;
00683 channels[result]->setValue(i, mag);
00684
00685
00686 velocity[i].normVelocity = vel.normalize();
00687 }
00688 }
00689
00690 return result;
00691 }
00692
00693 int FlowData::createChannelVectorLength(int chX, int chY, int chZ)
00694 {
00695
00696 if (chZ >= 0)
00697 {
00698 return createChannelVectorLength(getChannel(chX),getChannel(chY),getChannel(chZ));
00699 }
00700 else
00701 {
00702 return createChannelVectorLength(getChannel(chX),getChannel(chY));
00703 }
00704 }
00705 #endif
00706
00707
00708 int FlowData::getScalarChannelCount()
00709 {
00710 return numScalarChannels;
00711 }
00712
00713
00714 int FlowData::getNumTimesteps()
00715 {
00716 return timesteps;
00717 }
00718
00719
00720 FlowGeometry* FlowData::getGeometry() const
00721 {
00722 return geometry;
00723 }
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742
00743 int FlowData::getVelMagID ()
00744 {
00745 return velocityMagnitudeId;
00746 }
00747
00748
00749 void FlowData::rescaleScalarData()
00750 {
00751 }
00752