Example #1
0
    def __init__(self, esmfGrid, name, datatype, staggerloc=CENTER):
        """
        Creator for structured ESMF Field
        @param esmfGrid instance of an ESMF
        @param name field name (must be unique)
        @param datatype data type, one of 'float64', 'float32', 'int64', or 'int32'
                        (or equivalent numpy dtype)
        @param staggerloc ESMF.StaggerLoc.CENTER
                          ESMF.StaggerLoc.CORNER
        """
        # field object
        self.field = None
        # the local processor rank
        self.pe = 0
        # the number of processors
        self.nprocs = 1
        # associated grid
        self.grid = esmfGrid
        # staggering
        self.staggerloc = staggerloc
        # communicator
        self.comm = None

        try:
            from mpi4py import MPI
            self.comm = MPI.COMM_WORLD
        except BaseException:
            pass

        etype = None
        sdatatype = str(datatype)  # in case user passes a numpy dtype
        if re.search('float64', sdatatype):
            etype = R8
        elif re.search('float32', sdatatype):
            etype = R4
        elif re.search('int64', sdatatype):
            etype = I8
        elif re.search('int32', sdatatype):
            etype = I4
        else:
            msg = 'esmf.EsmfStructField.__init__: ERROR invalid type %s' % datatype
            raise RegridError(msg)

        self.field = ESMF.Field(grid=esmfGrid.grid,
                                name=name,
                                typekind=etype,
                                staggerloc=staggerloc)
        vm = ESMF.ESMP_VMGetGlobal()
        self.pe, self.nprocs = ESMF.ESMP_VMGet(vm)
Example #2
0
    def __init__(self, numTopoDims, numSpaceDims):
        # handle to the grid object
        self.grid = None
        # whether or not nodes were added
        self.nodesAdded = False
        # whether or not cells were added
        self.cellsAdded = False
        # the local processor rank
        self.pe = 0
        # number of processors
        self.nprocs = 1
        # communicator
        self.comm = None

        vm = ESMF.ESMP_VMGetGlobal()
        self.pe, self.nprocs = ESMF.ESMP_VMGet(vm)

        self.grid = ESMF.Mesh(parametric_dim=numTopoDims,
                              spatial_dim=numSpaceDims)
Example #3
0
    def __init__(self, esmfGrid, name, datatype, staggerloc=CENTER):
        """

        """
        # field object
        self.field = None
        # the local processor rank
        self.pe = 0
        # the number of processors
        self.nprocs = 1
        # associated grid
        self.grid = esmfGrid
        # staggering
        self.staggerloc = staggerloc
        # communicator
        self.comm = None

        try:
            from mpi4py import MPI
            self.comm = MPI.COMM_WORLD
        except BaseException:
            pass

        etype = None
        sdatatype = str(datatype)  # in case user passes a numpy dtype
        if re.search('float64', sdatatype):
            etype = R8
        elif re.search('float32', sdatatype):
            etype = R4
        elif re.search('int64', sdatatype):
            etype = I8
        elif re.search('int32', sdatatype):
            etype = I4
        else:
            msg = 'esmf.EsmfStructField.__init__: ERROR invalid type %s' % datatype
            raise RegridError(msg)

        self.field = ESMF.Field(grid=esmfGrid.grid,
                                name=name,
                                typekind=etype,
                                staggerloc=staggerloc)
        vm = ESMF.ESMP_VMGetGlobal()
        self.pe, self.nprocs = ESMF.ESMP_VMGet(vm)
Example #4
0
def create_grid(bounds, domask):
  '''
  PRECONDITIONS: ESMPy has been initialized, 'bounds' contains the number of
                 indices required for the first two dimensions of a Grid.
                 'domask' is a boolean value that gives the option to put
                 a mask on this Grid.\n
  POSTCONDITIONS: An Grid has been created.\n
  RETURN VALUES: \n Grid :: grid \n
  '''

  nx = float(bounds[0])
  ny = float(bounds[1])

  dx = 360.0/nx
  dy = 180.0/ny

  DEG2RAD = 3.141592653589793/180.0

  maxIndex = np.array([nx,ny], dtype=np.int32)

  staggerLocs = [ESMF.StaggerLoc.CORNER, ESMF.StaggerLoc.CENTER]
  grid = ESMF.Grid(maxIndex, num_peri_dims=1, staggerloc=staggerLocs)

  # VM
  vm = ESMF.ESMP_VMGetGlobal()
  localPet, petCount = ESMF.ESMP_VMGet(vm)
  
 # get the coordinate pointers and set the coordinates
  [x,y] = [0, 1]
  gridXCorner = grid.get_coords(x, ESMF.StaggerLoc.CORNER)
  gridYCorner = grid.get_coords(y, ESMF.StaggerLoc.CORNER)
  
  for i in xrange(gridXCorner.shape[x]):
    gridXCorner[i, :] = float(i)*dx - 180.0
 
  for j in xrange(gridYCorner.shape[y]):
    gridYCorner[:, j] = float(j)*dy - 90.0
  
  ##   CENTERS

  # get the coordinate pointers and set the coordinates
  [x,y] = [0, 1]
  gridXCenter = grid.get_coords(x, ESMF.StaggerLoc.CENTER)
  gridYCenter = grid.get_coords(y, ESMF.StaggerLoc.CENTER)
  
  for i in xrange(gridXCenter.shape[x]):
    gridXCenter[i, :] = float(i)*dx + 0.5*dx - 180.0

  for j in xrange(gridYCenter.shape[y]):
    y = (float(j)*dy - 90.0)
    yp1 = (float(j+1)*dy - 90.0)
    gridYCenter[:, j] = (y+yp1)/2.0

  mask = 0
  if domask:
    # set up the grid mask
    grid.add_item(ESMF.GridItem.MASK)
    mask = grid.get_item(ESMF.GridItem.MASK)
    
    [x,y] = [0, 1]
    for i in range(mask.shape[x]):
      if (i == 2.0):
        mask[i, :] = 1
      else:
        mask[i, :] = 0

  return grid
Example #5
0
def compare_fields(interp_field, exact_field, dstfracfield, srcmass, dstmass, \
                   parallel):
  '''
  PRECONDITIONS: 'interp_field' is a Field that holds the values resulting
                  from a regridding operation, 'exact_field' is a Field
                  containing the values of the exact solution that is 
                  expected, and 'dstfracfield' is a Field containing the
                  fractions of the 'interp_field' which contributed to the
                  regridding product.  'srcmass' and 'dstmass' are the 
                  mass values for the source and destination data fields.
                  'parallel' is an internal variable used to determine if this
                  run should be done in serial or parallel.\n
  POSTCONDITIONS: The interpolation accuracy of a regridding operation is
                  determined by comparing the 'interp_field' to the 
                  'exact_field'.  The mass conservation is validated by
                  comparing the 'srcmass' to the 'dstmass'.\n
  RETURN VALUES: None \n
  '''
  
  vm = ESMF.ESMP_VMGetGlobal()
  localPet, _ = ESMF.ESMP_VMGet(vm)
  
  if (interp_field.data.shape != exact_field.data.shape):
    raise TypeError('compare_fields: Fields must be the same size!')

  # initialize to True, and check for False point values
  [x, y] = [0, 1]
  total_error = 0.0
  max_error = 0.0
  min_error = 1000000.0
  for i in range(interp_field.data.shape[x]):
    for j in range(interp_field.data.shape[y]):
      if (exact_field.data[i, j] != 0.0):
        err = abs(interp_field.data[i, j] / dstfracfield.data[i, j] - \
              exact_field.data[i, j]) / abs(exact_field.data[i, j])
      else:
        err = abs(interp_field.data[i, j] / dstfracfield.data[i, j] - \
              exact_field.data[i, j])
      total_error = total_error + err
      if (err > max_error): 
        max_error = err
      if (err < min_error): 
        min_error = err

  if parallel:
    # use mpi4py to collect values
    from mpi4py import MPI
 
    comm = MPI.COMM_WORLD
    rank = comm.Get_rank()
 
    max_error_global = comm.reduce(max_error, op=MPI.MAX)
    min_error_global = comm.reduce(min_error, op=MPI.MIN)
    total_error_global = comm.reduce(total_error, op=MPI.SUM)
    srcmass_global = comm.reduce(srcmass, op=MPI.SUM)
    dstmass_global = comm.reduce(dstmass, op=MPI.SUM)
 
    if rank == 0:
      # check the mass
      csrv = False
      csrv_error = abs(dstmass_global - srcmass_global)/srcmass_global
      if (csrv_error < 10e-12):
        csrv = True
      itrp = False
      if (max_error_global < 10E-2):
        itrp = True
    
      if (itrp and csrv):
        print "PASS"
      else:
        print "FAIL"
      print "       Total error = "+str(total_error_global)
      print "       Max error   = "+str(max_error_global)
      print "       Min error   = "+str(min_error_global)
      print "       Csrv error  = "+str(csrv_error)
      print "       srcmass     = "+str(srcmass_global)
      print "       dstmass     = "+str(dstmass_global)
  else:
    # check the mass
    csrv = False
    csrv_error = abs(dstmass - srcmass)/srcmass
    if (csrv_error < 10e-12):
      csrv = True
    itrp = False
    if (max_error < 10E-2):
      itrp = True
  
    if (itrp and csrv):
      print "PASS"
    else:
      print "FAIL"
    print "       Total error = "+str(total_error)
    print "       Max error   = "+str(max_error)
    print "       Min error   = "+str(min_error)
    print "       Csrv error  = "+str(csrv_error)
    print "       srcmass     = "+str(srcmass)
    print "       dstmass     = "+str(dstmass)
  
  return