コード例 #1
0
void myRunInC(ESMC_GridComp gcomp, ESMC_State importState,
  ESMC_State exportState, ESMC_Clock *clock, int *rc){

  // local data
  ESMC_Array retrievedArray;
  int localrc;
  printf("I am in myRunInC()\n");


    localrc=ESMC_StateGetArray(exportState, "array1", &retrievedArray);
    double *ptr = (double *)ESMC_ArrayGetPtr(retrievedArray, 0, &localrc);

  // verify data values
     for (int j=0; j<2; j++){
       for (int i=0; i<5; i++){
         int ij= j*5 +i;
         if ( fabs(ptr[ij]-float((j+1)*10+i+1)) > 1.e-8 ){
           printf("ptr has wrong values at i=%d,j=%d,ij=%d\n", i,j,ij);
           *rc = ESMC_Finalize();
         }
       }
     }
     printf("Data values in exp state correct in myRunInC\n");

  // return successfully
  if (rc!=NULL) *rc = ESMF_SUCCESS;
}
コード例 #2
0
int main ( int argc, char **argv )
{
      // Local variables
      int i, j;
      ESMC_VM *vm;

//BOE
//\subsubsection{Common Code Arguments}

// Common Arguments used in the following code fragments:
//EOE

//BOC
      char fname [ESMF_MAXSTR] = "myResourceFile.rc";    // file name
      char fn1[10], fn2[10], fn3[10];
      int rc;             // error return code (0 is OK)
      ESMC_I4 n = 0;
      ESMC_R4 r = 0.0;
      ESMC_R4 table[7][3];

      ESMC_Config *cf;
//EOC

      int finalrc;
      finalrc = ESMF_SUCCESS;
  
      rc = ESMC_Initialize(NULL, ESMC_ArgLast);
      if (rc != ESMF_SUCCESS) {
        finalrc = ESMF_FAILURE;
        printf("'ESMC_Initialize' failed\n");
      }
  
      vm = ESMC_VMGetGlobal(&rc);
      if (rc != ESMF_SUCCESS) {
        finalrc = ESMF_FAILURE;
        printf("'ESMC_VMGetGlobal' failed\n");
      }

//-------------------------------------------------------------------------
//   // Example 1:
//   //

//BOE
//\subsubsection{Creation of a Config}

// The first step is to create the {\tt ESMC\_Config} and load the
// ASCII resource (rc) file into memory\footnote{See next section
// for a complete description of parameters for each routine/function}:
//EOE

//BOC  
      cf = ESMC_ConfigCreate(&rc);
//EOC

      if (rc != ESMF_SUCCESS) {
        finalrc = ESMF_FAILURE;
        printf("'ESMC_ConfigCreate' failed\n");
      }

//BOC
      rc = ESMC_ConfigLoadFile(cf, fname, ESMC_ArgLast);
//EOC

      if (rc != ESMF_SUCCESS) {
        finalrc = ESMF_FAILURE;
        printf("'ESMC_ConfigLoadFile' failed\n");
      }

//BOE
//\subsubsection{Retrieval of constants}

// The next step is to select the label (record) of interest, say
//EOE

//BOC
      rc = ESMC_ConfigFindLabel(cf, "constants:");
//EOC

      if (rc != ESMF_SUCCESS) {
        finalrc = ESMF_FAILURE;
        printf("'ESMC_ConfigFindLabel' failed\n");
      }

//BOE
// Two constants, r and n, can be retrieved with the following code
// fragment:
//EOE

//BOC
      rc = ESMC_ConfigGetAttribute(cf, &r, ESMC_TYPEKIND_R4,
                      ESMC_ArgLast);  // results in r = 3.1415
      rc = ESMC_ConfigGetAttribute(cf, &n, ESMC_TYPEKIND_I4,
                      ESMC_ArgLast);  // results in n = 25
//EOC
      if (rc != ESMF_SUCCESS) {
        finalrc = ESMF_FAILURE;
        printf("'ESMC_ConfigGetAttribute' failed\n");
      }

      float rtest = 3.1415;
      int ntest = 25;
      if (r != rtest || n != ntest) {
        finalrc = ESMF_FAILURE;
        printf("bad results from 'ESMC_ConfigGetAttribute'\n");
        printf("results expected: r = %f,  n = %i\n",rtest,ntest);
        printf("results obtained: r = %f,  n = %i\n",r,n);
      }

//BOE
//\subsubsection{Retrieval of file names}

// File names can be retrieved with the following code fragment:
//EOE

//BOC
      rc = ESMC_ConfigFindLabel(cf, "my_file_names:");
//EOC

      if (rc != ESMF_SUCCESS) {
        finalrc = ESMF_FAILURE;
        printf("'ESMC_ConfigFindLabel' failed\n");
      }

//BOC
      rc = ESMC_ConfigGetAttribute(cf, fn1, ESMC_TYPEKIND_CHARACTER,
                      ESMC_ArgLast);  // results in fn1 = 'jan87.dat'
      rc = ESMC_ConfigGetAttribute(cf, fn2, ESMC_TYPEKIND_CHARACTER,
                      ESMC_ArgLast);  // results in fn2 = 'jan88.dat'
      rc = ESMC_ConfigGetAttribute(cf, fn3, ESMC_TYPEKIND_CHARACTER,
                      ESMC_ArgLast);  // results in fn3 = 'jan89.dat'
//EOC
      if (rc != ESMF_SUCCESS) {
        finalrc = ESMF_FAILURE;
        printf("'ESMC_ConfigGetAttribute' failed\n");
      }

      if (strncmp("jan87.dat",fn1,9) != 0 ||
          strncmp("jan88.dat",fn2,9) != 0 ||
          strncmp("jan89.dat",fn3,9) != 0   ) {
        finalrc = ESMF_FAILURE;
        printf("bad results from ESMC_ConfigGetAttribute\n");
        printf("results obtained: fn1 = %s,  fn2 = %s,  fn3 = %s\n",fn1,fn2,fn3);
        printf("results expected: fn1 = %s,  fn2 = %s,  fn3 = %s\n",
               "jan87.dat","jan88.dat","jan89.dat");
      }

//BOE
//\subsubsection{Retrieval of tables}

// To access tabular data, the user first must use
// {\tt ESMC\_ConfigFindLabel()} to locate the beginning of the table, e.g.,
//EOE

//BOC
      rc = ESMC_ConfigFindLabel(cf, "my_table_name::");
//EOC

      if (rc != ESMF_SUCCESS) {
        finalrc = ESMF_FAILURE;
        printf("'ESMC_ConfigFindLabel' failed\n");
      }

//BOE
// Subsequently, {\tt call ESMC\_ConfigNextLine()} can be used to gain
// access to each row of the table. Here is a code fragment to read the above
// table (7 rows, 3 columns):
//EOE

//BOC
      for (i = 0; i < 7; i++) {
        rc = ESMC_ConfigNextLine(cf, ESMC_ArgLast);
        for (j = 0; j < 3; j++) {
          rc = ESMC_ConfigGetAttribute(cf, (&table[i][j]), ESMC_TYPEKIND_R4,
                          ESMC_ArgLast);
        }
      }
//EOC

//BOE
//\subsubsection{Destruction of a Config}

// The work with the configuration file {\tt cf} is finalized by call to
// {\tt ESMC\_ConfigDestroy()}:
//EOE

//BOC
      rc = ESMC_ConfigDestroy(cf);
//EOC
      if (rc != ESMF_SUCCESS) {
        finalrc = ESMF_FAILURE;
        printf("'ESMC_ConfigDestroy' failed\n");
      }

      if (finalrc == ESMF_SUCCESS)
        printf("PASS: ESMC_ConfigOverviewEx.C\n");
      else
        printf("FAIL: ESMC_ConfigOverviewEx.C\n");

      rc = ESMC_Finalize();

}
コード例 #3
0
ファイル: ESMC_SMM.c プロジェクト: rokuingh/esmf_dev
int main(void){

  // VM variables
  int localPet, petCount, rc;
  ESMC_VM vm;

  ESMC_RouteHandle routehandle;
  ESMC_Field srcfield, dstfield;
  ESMC_Grid srcgrid, dstgrid;
  int *maxIndex;
  ESMC_InterArrayInt i_maxIndex;

  ESMC_Initialize(NULL, ESMC_ArgLast);

  // Get parallel information
  vm=ESMC_VMGetGlobal(&rc);
  if (rc != ESMF_SUCCESS) return 0;

  rc=ESMC_VMGet(vm, &localPet, &petCount, (int *)NULL, (MPI_Comm *)NULL,
                (int *)NULL, (int *)NULL);
  if (rc != ESMF_SUCCESS) return 0;

  rc=ESMC_LogSet(true);

  //----------------------------------------------------------------------------
  //----------------------- GRID CREATION --------------------------------------
  //----------------------------------------------------------------------------

#if 0
  grid = ESMC_GridCreateFromFile("data/ll2.5deg_grid.nc",
                                 ESMC_FILEFORMAT_SCRIP, NULL, NULL,
                                 NULL, NULL, NULL, NULL, NULL, NULL, NULL, &rc);
  if (rc != ESMF_SUCCESS) return 0;

  mesh = ESMC_MeshCreateFromFile("data/mpas_uniform_10242_dual_counterclockwise.nc",
                                 ESMC_FILEFORMAT_ESMFMESH, NULL, NULL,
                                 NULL, NULL, NULL, &rc);
  if (rc != ESMF_SUCCESS) return 0;
#endif

  int m = 8;
  int n = 9;

  int dimcount = 2;
  maxIndex = (int *)malloc(dimcount*sizeof(int));
  maxIndex[0] = m;
  maxIndex[1] = m;
  rc = ESMC_InterArrayIntSet(&i_maxIndex, maxIndex, dimcount);

  ESMC_IndexFlag indexflag = ESMC_INDEX_GLOBAL;
  srcgrid = ESMC_GridCreateNoPeriDim(&i_maxIndex, NULL, NULL, &indexflag, &rc);
  if (rc != ESMF_SUCCESS) return 0;

  maxIndex[0] = m;
  maxIndex[1] = m;

  dstgrid = ESMC_GridCreateNoPeriDim(&i_maxIndex, NULL, NULL, &indexflag, &rc);
  if (rc != ESMF_SUCCESS) return 0;

    int *exLBound = NULL;
    int *exUBound = NULL;
    int p = 0;

    ESMC_GridAddCoord(srcgrid, ESMC_STAGGERLOC_CENTER);

    exLBound = (int *)malloc(dimcount*sizeof(int));
    exUBound = (int *)malloc(dimcount*sizeof(int));

    double *gridXCoord = (double *)ESMC_GridGetCoord(srcgrid, 1,
                                                     ESMC_STAGGERLOC_CENTER, NULL,
                                                     exLBound, exUBound, &rc);

    double *gridYCoord = (double *)ESMC_GridGetCoord(srcgrid, 2,
                                                     ESMC_STAGGERLOC_CENTER, NULL,
                                                     NULL, NULL, &rc);

    // printf("PET%d: lbnd, ubnd    %d    %d    %d    %d\n", localPet, exLBound[0], exLBound[1], exUBound[0], exUBound[1]);

    p = 0;
    for (int i1=exLBound[1]; i1<=exUBound[1]; ++i1) {
      for (int i0=exLBound[0]; i0<=exUBound[0]; ++i0) {
        gridXCoord[p]=i0;
        gridYCoord[p]=i1;
        ++p;
      }
    }

    ESMC_GridAddCoord(dstgrid, ESMC_STAGGERLOC_CENTER);

    exLBound = (int *)malloc(dimcount*sizeof(int));
    exUBound = (int *)malloc(dimcount*sizeof(int));

    gridXCoord = (double *)ESMC_GridGetCoord(dstgrid, 1,
                                                     ESMC_STAGGERLOC_CENTER, NULL,
                                                     exLBound, exUBound, &rc);

    gridYCoord = (double *)ESMC_GridGetCoord(dstgrid, 2,
                                                     ESMC_STAGGERLOC_CENTER, NULL,
                                                     NULL, NULL, &rc);

    // printf("exLBounds = [%d,%d]\n", exLBound[0], exLBound[1]);
    // printf("exUBounds = [%d,%d]\n", exUBound[0], exUBound[1]);

    p = 0;
    for (int i1=exLBound[1]; i1<=exUBound[1]; ++i1) {
      for (int i0=exLBound[0]; i0<=exUBound[0]; ++i0) {
        gridXCoord[p]=i0;
        gridYCoord[p]=i1;
        ++p;
      }
    }

  //----------------------------------------------------------------------------
  //---------------------- FIELD CREATION --------------------------------------
  //----------------------------------------------------------------------------

  srcfield = ESMC_FieldCreateGridTypeKind(srcgrid, ESMC_TYPEKIND_R8,
    ESMC_STAGGERLOC_CENTER, NULL, NULL, NULL, "srcfield", &rc);
  if (rc != ESMF_SUCCESS) return 0;

  dstfield = ESMC_FieldCreateGridTypeKind(dstgrid, ESMC_TYPEKIND_R8,
    ESMC_STAGGERLOC_CENTER, NULL, NULL, NULL, "dstfield", &rc);
  if (rc != ESMF_SUCCESS) return 0;


  // get and fill first coord array and computational bounds
  exLBound = (int *)malloc(dimcount*sizeof(int));
  exUBound = (int *)malloc(dimcount*sizeof(int));

  rc = ESMC_FieldGetBounds(srcfield, 0, exLBound, exUBound, dimcount);
  if (rc != ESMF_SUCCESS) return 0;

  double * srcfieldptr = (double *)ESMC_FieldGetPtr(srcfield, 0, &rc);
  if (rc != ESMF_SUCCESS) return 0;

  p = 0;
  for (int i1=exLBound[1]; i1<=exUBound[1]; ++i1) {
    for (int i0=exLBound[0]; i0<=exUBound[0]; ++i0) {
      srcfieldptr[p] = 42.0;
      p++;
    }
  }

  // get and fill first coord array and computational bounds
  int *exLBound2 = (int *)malloc(dimcount*sizeof(int));
  int *exUBound2 = (int *)malloc(dimcount*sizeof(int));

  rc = ESMC_FieldGetBounds(dstfield, 0, exLBound2, exUBound2, dimcount);
  if (rc != ESMF_SUCCESS) return 0;

  double * dstfieldptr = (double *)ESMC_FieldGetPtr(dstfield, 0, &rc);
  if (rc != ESMF_SUCCESS) return 0;

  // initialize destination field
  p = 0;
  for (int i1=exLBound2[1]; i1<=exUBound2[1]; ++i1) {
    for (int i0=exLBound2[0]; i0<=exUBound2[0]; ++i0) {
      dstfieldptr[p] = 0.0;
      p++;
    }
  }

  //----------------------------------------------------------------------------
  //-------------------------- REGRIDDING --------------------------------------
  //----------------------------------------------------------------------------

  rc = ESMC_FieldRegridStore(srcfield, dstfield, NULL, NULL, &routehandle,
                             NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
  if (rc != ESMF_SUCCESS) return 0;

#if 0
  rc = ESMC_FieldRegridStoreFile(srcfield, dstfield, "data/weights.nc", NULL, NULL,
                                 &routehandle, NULL, NULL, NULL, NULL, NULL,
                                 NULL, NULL, NULL, NULL);
  if (rc != ESMF_SUCCESS) return 0;

  printf("srcfield before smmstore = [\n");
  p = 0;
  for (int i1=exLBound[1]; i1<=exUBound[1]; ++i1) {
    for (int i0=exLBound[0]; i0<=exUBound[0]; ++i0) {
      printf("%f, ", srcfieldptr[p]);
      p++;
    }
  }
  printf("]\n");

  rc = ESMC_FieldSMMStore(srcfield, dstfield, "data/weights_generic.nc", &routehandle,
                          NULL, NULL, NULL, NULL);
  if (rc != ESMF_SUCCESS) return 0;

  printf("srcfield = [\n");
  p = 0;
  for (int i1=exLBound[1]; i1<=exUBound[1]; ++i1) {
    for (int i0=exLBound[0]; i0<=exUBound[0]; ++i0) {
      printf("%f, ", srcfieldptr[p]);
      p++;
    }
  }
  printf("]\n");

  //rc = ESMC_FieldRegrid(srcfield, dstfield, routehandle, NULL);
  if (rc != ESMF_SUCCESS) return 0;
#endif

  rc = ESMC_FieldRegridRelease(&routehandle);
  if (rc != ESMF_SUCCESS) return 0;

  free(exLBound);
  free(exUBound);
  free(exLBound2);
  free(exUBound2);

  rc = ESMC_FieldDestroy(&srcfield);
  rc = ESMC_FieldDestroy(&dstfield);
  rc = ESMC_GridDestroy(&srcgrid);
  rc = ESMC_GridDestroy(&dstgrid);

  ESMC_Finalize();
  
  return 0;
  
}