Example #1
0
void kernel::openKernelNetcdf () {
  
  // Open the kernel file output from the solver.
  
  using namespace netCDF;
  using namespace netCDF::exceptions;

  // Local mpi variables.
  int myRank = MPI::COMM_WORLD.Get_rank ();
  int worldSize = MPI::COMM_WORLD.Get_size ();
  
  if (myRank == 0)
    std::cout << "Opening kernel file: " << blu << fileName << rst << " with " << worldSize 
      << " processors." << std::flush << std::endl;
  
  try {

    // Open the file.
    NcFile dataFile (fileName, NcFile::read);
    
    // Get variable.
    NcVar NcKernel = dataFile.getVar ("rawKernel");
    
    // Get array sizes.
    NcDim procDim = NcKernel.getDim (0);
    NcDim kernDim = NcKernel.getDim (1);  
    numWroteProcs = procDim.getSize ();
    numGLLPoints  = kernDim.getSize ();
  
    if (myRank == 0)
      std::cout << mgn << "Number of solver processers:\t " << numWroteProcs
        << "\nNumber of GLL points:\t\t " << numGLLPoints << rst << "\n" << std::endl;
    
    // Set up the MPI read chunk array.
    std::vector<size_t> start;
    std::vector<size_t> count;
    start.resize(2);
    count.resize(2);
    
    // Row major MPI read. Start at [myRank, 0]
    start[0] = myRank;
    start[1] = 0;
    
    // Read until end of line [myrank, numGLLPoints]
    count[0] = 1;
    count[1] = numGLLPoints;
    
    // Of course only read in with the number of processors used to create the file.
    if (myRank < numWroteProcs) {
      rawKernel = new float [numGLLPoints];
      NcKernel.getVar (start, count, rawKernel);
    }
        
    // Destructor will close file.
    
  } catch (NcException &error) {
    
    std::cout << error.what() << std::endl;
    std::cout << red << "Failure reading: " << fileName << std::endl;
    std::exit (EXIT_FAILURE);
    
  }
    
}
Example #2
0
void kernel::openCoordNetcdf () {
  
  // Open the NetCDF coordinate file output from the solver.
  
  using namespace netCDF;
  using namespace netCDF::exceptions;

  // Local mpi variables.
  int myRank = MPI::COMM_WORLD.Get_rank ();
  int worldSize = MPI::COMM_WORLD.Get_size ();
  
  if (myRank == 0)
    std::cout << "Opening coordinate file: " << blu << "./krn/xyzCrustMantle.nc"  << rst 
      << " with " << worldSize << " processors." << std::flush << std::endl;
  
  try {

    std::string coordName = "./krn/xyzCrustMantle.nc";

    // Open the file.
    NcFile dataFile (coordName, NcFile::read);
    
    // Get variable.
    NcVar NcRadius = dataFile.getVar ("radius");
    NcVar NcTheta  = dataFile.getVar ("theta");
    NcVar NcPhi    = dataFile.getVar ("phi");
    
    // Get array sizes.
    NcDim procDim  = NcRadius.getDim (0);
    NcDim coordDim = NcRadius.getDim (1);  
    numWroteProcs  = procDim.getSize ();
    numGLLPoints   = coordDim.getSize ();
  
    if (myRank == 0)
      std::cout << mgn << "Number of solver processers:\t " << numWroteProcs
        << "\nNumber of GLL points:\t\t " << numGLLPoints << rst << "\n" << std::endl;
    
    // Current error handling. Only can have as many cores as we did for the simulation.
    if (worldSize > numWroteProcs) {
      
      if (myRank == 0)
        std::cout << red << "Currently, you can only use as many cores for processing as were used " 
          << "in the forward run. Exiting.\n" << rst << std::flush << std::endl;
      
      MPI::COMM_WORLD.Abort (EXIT_FAILURE);
      exit (EXIT_FAILURE);
      
    }
    
    // Set up the MPI read chunk array.
    std::vector<size_t> start;
    std::vector<size_t> count;
    start.resize(2);
    count.resize(2);
    
    // Row major MPI read. Start at [myRank, 0]
    start[0] = myRank;
    start[1] = 0;
    
    // Read until end of line [myrank, numGLLPoints]
    count[0] = 1;
    count[1] = numGLLPoints;
    
    // Preallocate cartesian arrays.
    xStore = new float [numGLLPoints];
    yStore = new float [numGLLPoints];
    zStore = new float [numGLLPoints];
    
    // Of course only read in with the number of processors used to create the file.
    if (myRank < numWroteProcs) {
      radius = new float [numGLLPoints];
      theta  = new float [numGLLPoints];
      phi    = new float [numGLLPoints];
      
      NcRadius.getVar (start, count, radius);
      NcTheta.getVar  (start, count, theta);
      NcPhi.getVar    (start, count, phi);
    }
    
    // Save original arrays.
    radiusOrig = new float [numGLLPoints];
    thetaOrig  = new float [numGLLPoints];
    phiOrig    = new float [numGLLPoints];    
    for (size_t i=0; i<numGLLPoints; i++) {
      
      radiusOrig[i] = radius[i];
      thetaOrig[i]  = theta[i];
      phiOrig[i]    = phi[i];
      
    }
    
    // Destructor will close file.
        
  } catch (NcException &error) {
    
    std::cout << error.what() << std::endl;
    std::cout << red << "Failure reading: " << fileName << std::endl;
    std::exit (EXIT_FAILURE);
    
  }
  
}