avth5partFileFormat.C 17.5 KB
Newer Older
gsell's avatar
gsell committed
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
// ************************************************************************* //
//                            avth5partFileFormat.C                           //
// ************************************************************************* //

#include <avth5partFileFormat.h>

#include <string>
#include <vector>

#include <vtkFloatArray.h>
#include <vtkRectilinearGrid.h>
#include <vtkStructuredGrid.h>
#include <vtkUnstructuredGrid.h>

#include <avtDatabaseMetaData.h>

#include <Expression.h>

#include <InvalidVariableException.h>
#include <InvalidFilesException.h>
#include <BadIndexException.h>
#include <vtkCellType.h>
#include <vtkPolyData.h>


//h5part specific
#include <H5Part.h>
#include <stdio.h>
#include <stdlib.h>

#include <fstream>
#include <iomanip>
#include <iostream>



#ifdef PARALLEL_IO
#include <mpi.h>
#include <avtParallel.h>
#endif

using namespace std;

// ****************************************************************************
//  Method: avth5part constructor
//
//  Programmer: cristina -- generated by xml2avt
//  Creation:   Mon Feb 27 13:47:07 PST 2006
//
// ****************************************************************************

avth5partFileFormat::avth5partFileFormat(const char *filename)
    : avtMTMDFileFormat(filename)
{
    // INITIALIZE DATA MEMBERS


    H5PartFile *file;
    fname = filename;

    file = H5PartOpenFile(filename,H5PART_READ);

    if (!file)
      EXCEPTION1(InvalidFilesException, filename);


    int i, j;
    int npoints, npointvars;
    int nspace = 3;

    H5PartSetStep(file,0);
    //points
    npoints= (int) H5PartGetNumParticles(file);
    if (npoints ==  0)
      EXCEPTION1(VisItException, "npoints is zero");
    points.resize(npoints*nspace);
    cout << "constructor: npoints: " << npoints << "\n";

    //point vars
    npointvars= (int) H5PartGetNumDatasets(file); /* get number of datasets in timestep 0 */
    pointvars.resize(npointvars);
    pointvarnames.resize(npointvars);
    cout << "constructor: nvariables: " << npointvars << "\n";

		char name[64];
		int status;
    for (j=0; j < npointvars; j++){
      status = H5PartGetDatasetName(file,j, name,64);
			if (status != 1){
				EXCEPTION1(VisItException, "could not read a variable name");
			}
      pointvarnames[j] = name;
		}

		H5PartCloseFile(file);
}


// ****************************************************************************
//  Method: avtEMSTDFileFormat::GetNTimesteps
//
//  Purpose:
//      Tells the rest of the code how many timesteps there are in this file.
//
//  Programmer: cristina -- generated by xml2avt
//  Creation:   Mon Feb 27 13:47:07 PST 2006
//
// ****************************************************************************

int
avth5partFileFormat::GetNTimesteps(void)
{
    int nt;
    H5PartFile *file;
    file = H5PartOpenFile(fname.c_str(),H5PART_READ);
    H5PartSetStep(file,0);
    nt=H5PartGetNumSteps(file); /* get number of steps in file */
    H5PartCloseFile(file);
    return nt;
}


// ****************************************************************************
//  Method: avth5partFileFormat::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: cristina -- generated by xml2avt
//  Creation:   Mon Feb 27 13:47:07 PST 2006
//
// ****************************************************************************

void
avth5partFileFormat::FreeUpResources(void)
{
}


// ****************************************************************************
//  Method: avth5partFileFormat::PopulateDatabaseMetaData
//
//  Purpose:
//      This database meta-data object is like a table of contents for the
//      file.  By populating it, you are telling the rest of VisIt what
//      information it can request from you.
//
//  Programmer: cristina -- generated by xml2avt
//  Creation:   Mon Feb 27 13:47:07 PST 2006
//
// ****************************************************************************

void
avth5partFileFormat::PopulateDatabaseMetaData(avtDatabaseMetaData *md, int timeState)
{
    //
    // CODE TO ADD A MESH
    //
    // string meshname = ...
    //
    // AVT_RECTILINEAR_MESH, AVT_CURVILINEAR_MESH, AVT_UNSTRUCTURED_MESH,
    // AVT_POINT_MESH, AVT_SURFACE_MESH, AVT_UNKNOWN_MESH
    // avtMeshType mt = AVT_RECTILINEAR_MESH;
    //
    // int nblocks = YOU_MUST_DECIDE;
    // int block_origin = 0;
    // int spatial_dimension = 2;
    // int topological_dimension = 2;
    // float *extents = NULL;
    //
    // Here's the call that tells the meta-data object that we have a mesh:
    //
    // AddMeshToMetaData(md, meshname, mt, extents, nblocks, block_origin,
    //                   spatial_dimension, topological_dimension);
    //

    //
    // CODE TO ADD A SCALAR VARIABLE
    //
    // string mesh_for_this_var = meshname; // ??? -- could be multiple meshes
    // string varname = ...
    //
    // AVT_NODECENT, AVT_ZONECENT, AVT_UNKNOWN_CENT
    // avtCentering cent = AVT_NODECENT;
    //
    //
    // Here's the call that tells the meta-data object that we have a var:
    //
    // AddScalarVarToMetaData(md, varname, mesh_for_this_var, cent);
    //

    //
    // CODE TO ADD A VECTOR VARIABLE
    //
    // string mesh_for_this_var = meshname; // ??? -- could be multiple meshes
    // string varname = ...
    // int vector_dim = 2;
    //
    // AVT_NODECENT, AVT_ZONECENT, AVT_UNKNOWN_CENT
    // avtCentering cent = AVT_NODECENT;
    //
    //
    // Here's the call that tells the meta-data object that we have a var:
    //
    // AddVectorVarToMetaData(md, varname, mesh_for_this_var, cent,vector_dim);
    //

    //
    // CODE TO ADD A TENSOR VARIABLE
    //
    // string mesh_for_this_var = meshname; // ??? -- could be multiple meshes
    // string varname = ...
    // int tensor_dim = 9;
    //
    // AVT_NODECENT, AVT_ZONECENT, AVT_UNKNOWN_CENT
    // avtCentering cent = AVT_NODECENT;
    //
    //
    // Here's the call that tells the meta-data object that we have a var:
    //
    // AddTensorVarToMetaData(md, varname, mesh_for_this_var, cent,tensor_dim);
    //

    //
    // CODE TO ADD A MATERIAL
    //
    // string mesh_for_mat = meshname; // ??? -- could be multiple meshes
    // string matname = ...
    // int nmats = ...;
    // vector<string> mnames;
    // for (int i = 0 ; i < nmats ; i++)
    // {
    //     char str[32];
    //     sprintf(str, "mat%d", i);
    //     -- or -- 
    //     strcpy(str, "Aluminum");
    //     mnames.push_back(str);
    // }
    // 
    // Here's the call that tells the meta-data object that we have a mat:
    //
    // AddMaterialToMetaData(md, matname, mesh_for_mat, nmats, mnames);
    //
    //
    // Here's the way to add expressions:
    //Expression momentum_expr;
    //momentum_expr.SetName("momentum");
    //momentum_expr.SetDefinition("{u, v}");
    //momentum_expr.SetType(Expression::VectorMeshVar);
    //md->AddExpression(&momentum_expr);
    //Expression KineticEnergy_expr;
    //KineticEnergy_expr.SetName("KineticEnergy");
    //KineticEnergy_expr.SetDefinition("0.5*(momentum*momentum)/(rho*rho)");
    //KineticEnergy_expr.SetType(Expression::ScalarMeshVar);
    //md->AddExpression(&KineticEnergy_expr);
    //
		int size;
		size = 1;
#ifdef PARALLEL_IO
		size = PAR_Size();
#endif
	

    if (!points.size()) {
      EXCEPTION1(InvalidFilesException, "Number of points is zero");
    }

		cout << "Populate: size, : " << size << "\n";

    avtMeshMetaData *pmesh = new avtMeshMetaData;

    int dimension = 3;
    pmesh->name = "particles";
    pmesh->originalName = "particles";
    pmesh->meshType = AVT_POINT_MESH;
    pmesh->topologicalDimension = 0;
    pmesh->spatialDimension = dimension;
		pmesh->numBlocks = size;
		pmesh->blockTitle = "subset";
		pmesh->blockPieceName = "subset";
		pmesh->hasSpatialExtents = false; 

    md->Add(pmesh);

    int i;
    for (i=0; i < pointvarnames.size(); i++){
      AddScalarVarToMetaData(md, pointvarnames[i], "particles", AVT_NODECENT);
    }

}


// ****************************************************************************
//  Method: avth5partFileFormat::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:
//      timestate   The index of the timestate.  If GetNTimesteps returned
//                  'N' time steps, this is guaranteed to be between 0 and N-1.
//      domain      The index of the domain.  If there are NDomains, this
//                  value is guaranteed to be between 0 and NDomains-1,
//                  regardless of block origin.
//      meshname    The name of the mesh of interest.  This can be ignored if
//                  there is only one mesh.
//
//  Programmer: cristina -- generated by xml2avt
//  Creation:   Mon Feb 27 13:47:07 PST 2006
//
// ****************************************************************************

vtkDataSet *
avth5partFileFormat::GetMesh(int timestate, int domain, const char *meshname)
{
	cout << "GetMesh domain: " << domain << "\n";

  H5PartFile *file;
  file = H5PartOpenFile(fname.c_str(),H5PART_READ);

  if (!file)
    EXCEPTION1(InvalidFilesException, fname.c_str());

  long int tnpoints, npoints;
	int npointvars;
  int nspace = 3;
	int nprocs = 1;
#ifdef PARALLEL_IO
 	nprocs = PAR_Size();	
#endif
  H5PartSetStep(file,timestate);

  //points
  tnpoints= (int) H5PartGetNumParticles(file);
  unsigned long long idStart = ((int)(tnpoints/nprocs))*domain;
	unsigned long long idEnd;	
	if (domain < nprocs-1) 
  	idEnd   = ((unsigned long int)(tnpoints/nprocs))*(domain+1);
	else if (domain == nprocs - 1)
  	idEnd   = tnpoints;


  H5PartSetView(file,idStart,idEnd);

  //points
  npoints= (long int) H5PartGetNumParticles(file);
	cout << "GetMesh: npoints for domain " << domain << ": " << npoints << "\n";

  if (strcmp(meshname, "particles") != 0){
    EXCEPTION1(InvalidVariableException, meshname);
  }
  if (npoints ==  0)
    EXCEPTION1(VisItException, "npoints is zero");

  points.resize(npoints*nspace);
  double *x, *y, *z;
  x = (double *) malloc(sizeof(double)*npoints);
  y = (double *) malloc(sizeof(double)*npoints);
  z = (double *) malloc(sizeof(double)*npoints);


  int status = 0;
  status = H5PartReadDataFloat64(file, "x", x);
  if (status != 1)
  EXCEPTION1(VisItException, "Could not read x coordinates");
  status = H5PartReadDataFloat64(file, "y", y);
  if (status != 1)
  EXCEPTION1(VisItException, "Could not read y coordinates");
  status = H5PartReadDataFloat64(file, "z", z);
  if (status != 1)
  EXCEPTION1(VisItException, "Could not read z coordinates");
  for (long int i = 0; i < npoints; i++){
    points[nspace*i] = (float) x[i];
    points[nspace*i+1] = (float) y[i];
    points[nspace*i+2] = (float) z[i];
  }
  free(x);
  free(y);
  free(z);

  H5PartSetView(file,-1, -1);

  vtkPolyData *dataset = vtkPolyData::New();
  vtkPoints *vtkpoints = vtkPoints::New();
  vtkpoints->SetNumberOfPoints((vtkIdType) npoints);

  float *pts = (float *) vtkpoints->GetVoidPointer(0);

  for (long int i=0; i < npoints*nspace; i++){
    pts[i] = points[i];
  }

  dataset->Allocate(npoints*nspace);
  for (long int i=0; i < npoints; i++){
    vtkIdType onevertex = (vtkIdType) i;
    dataset->InsertNextCell(VTK_VERTEX, 1, &onevertex);
  }
  dataset->SetPoints(vtkpoints);
  vtkpoints->Delete();


  H5PartCloseFile(file);
  fprintf(stderr,"proc[%u]:  done\n", domain);

  return dataset;
}


// ****************************************************************************
//  Method: avth5partFileFormat::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:
//      timestate  The index of the timestate.  If GetNTimesteps returned
//                 'N' time steps, this is guaranteed to be between 0 and N-1.
//      domain     The index of the domain.  If there are NDomains, this
//                 value is guaranteed to be between 0 and NDomains-1,
//                 regardless of block origin.
//      varname    The name of the variable requested.
//
//  Programmer: cristina -- generated by xml2avt
//  Creation:   Mon Feb 27 13:47:07 PST 2006
//
// ****************************************************************************

vtkDataArray *
avth5partFileFormat::GetVar(int timestate, int domain, const char *varname)
{
    //
    // If you have a file format where variables don't apply (for example a
    // strictly polygonal format like the STL (Stereo Lithography) format,
    // then uncomment the code below.
    //
    // EXCEPTION1(InvalidVariableException, varname);
    //

    //
    // If you do have a scalar variable, here is some code that may be helpful.
    //
    // int ntuples = XXX; // this is the number of entries in the variable.
    // vtkFloatArray *rv = vtkFloatArray::New();
    // rv->SetNumberOfTuples(ntuples);
    // for (int i = 0 ; i < ntuples ; i++)
    // {
    //      rv->SetTuple1(i, VAL);  // you must determine value for ith entry.
    // }
    //
    // return rv;
    //

  H5PartFile *file;

	file = H5PartOpenFile(fname.c_str(),H5PART_READ);

  if (!file)
    EXCEPTION1(InvalidFilesException, fname.c_str());

  int status;
  long int tnpoints, npoints;
	int npointvars;
  int nspace = 3;
	int nprocs = 1;
#ifdef PARALLEL_IO
  nprocs = PAR_Size(); 
#endif

  H5PartSetStep(file,timestate);
  //points
  tnpoints= (long int) H5PartGetNumParticles(file);
  //point vars

  char name[64];
  long long *idvar;
  double *data;
	unsigned long long idStart = ((int)(tnpoints/nprocs))*domain;
  unsigned long long idEnd; 
  if (domain < nprocs-1) 
    idEnd   = ((unsigned long int)(tnpoints/nprocs))*(domain+1);
  else if (domain == nprocs - 1)
    idEnd   = tnpoints;

  H5PartSetView(file,idStart,idEnd);
	npoints= (int) H5PartGetNumParticles(file);
	cout << "GetVar: npoints for domain " << domain << ": " << npoints << "\n"; 

  for (int j=0; j < pointvarnames.size(); j++){
    status = H5PartGetDatasetName(file,j, name,64);
    if (pointvarnames[j] == name) {
      if (strstr(name, "id") != NULL){
        idvar = (long long *) malloc(sizeof(long long)*npoints);
        status = H5PartReadDataInt64(file, name, idvar);
        if (status != 1)
          EXCEPTION1(VisItException, "Could not read dataset");
        pointvars[j].resize(npoints);
        for (long int i=0; i < npoints; i++){
          pointvars[j][i] = (float) idvar[i];
        }
        if (idvar != NULL)
          free(idvar);
      } else {
        data = (double *) malloc(sizeof(double)*npoints);
        status = H5PartReadDataFloat64(file, name, data);
        if (status != 1)
          EXCEPTION1(VisItException, "Could not read dataset");
        pointvars[j].resize(npoints);
        for (long int i=0; i < npoints; i++){
          pointvars[j][i] = (float) data[i];
        }
        if (data != NULL)
          free(data);
      }
    }
  }
	H5PartSetView(file,-1, -1);

  for (int i=0; i < pointvarnames.size(); i++){
    if (pointvarnames[i] == string(varname)){
      vtkFloatArray *scalars = vtkFloatArray::New();
      scalars->SetNumberOfTuples(npoints);
      float *ptr = (float*) scalars->GetVoidPointer(0);
      memcpy(ptr, &pointvars[i][0], sizeof(float)*npoints);
      return scalars;
    }
  }
  H5PartCloseFile(file);
  EXCEPTION1(InvalidVariableException, varname);


}


// ****************************************************************************
//  Method: avth5partFileFormat::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:
//      timestate  The index of the timestate.  If GetNTimesteps returned
//                 'N' time steps, this is guaranteed to be between 0 and N-1.
//      domain     The index of the domain.  If there are NDomains, this
//                 value is guaranteed to be between 0 and NDomains-1,
//                 regardless of block origin.
//      varname    The name of the variable requested.
//
//  Programmer: cristina -- generated by xml2avt
//  Creation:   Mon Feb 27 13:47:07 PST 2006
//
// ****************************************************************************

vtkDataArray *
avth5partFileFormat::GetVectorVar(int timestate, int domain,const char *varname)
{
    //
    // If you have a file format where variables don't apply (for example a
    // strictly polygonal format like the STL (Stereo Lithography) format,
    // then uncomment the code below.
    //
    // EXCEPTION1(InvalidVariableException, varname);
    //

    //
    // If you do have a vector variable, here is some code that may be helpful.
    //
    // int ncomps = YYY;  // This is the rank of the vector - typically 2 or 3.
    // int ntuples = XXX; // this is the number of entries in the variable.
    // vtkFloatArray *rv = vtkFloatArray::New();
    // int ucomps = (ncomps == 2 ? 3 : ncomps);
    // rv->SetNumberOfComponents(ucomps);
    // rv->SetNumberOfTuples(ntuples);
    // float *one_entry = new float[ucomps];
    // for (int i = 0 ; i < ntuples ; i++)
    // {
    //      int j;
    //      for (j = 0 ; j < ncomps ; j++)
    //           one_entry[j] = ...
    //      for (j = ncomps ; j < ucomps ; j++)
    //           one_entry[j] = 0.;
    //      rv->SetTuple(i, one_entry); 
    // }
    //
    // delete [] one_entry;
    // return rv;
    //
		return NULL;
}