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; }
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(); }
int main(int argc, char *argv[]) { // cumulative result: count failures; no failures equals "all pass" int result = 0; // individual test result code int rc; // individual test name char name[ESMF_MAXSTR]; // individual test failure message char failMsg[ESMF_MAXSTR]; // for dynamically allocated Route's ESMC_Route *route_ptr; // for statically allocated Route's // tests default constructor; add args to test other constructors ESMC_Route route; // get the global VM ESMC_VM *vm = ESMC_VMGetGlobal(&rc); // test dynamic allocation of ESMC_Route // also tests default constructor route_ptr = ESMC_RouteCreate(vm, &rc); sprintf(failMsg, "rc = %d, route_ptr = %p", rc, route_ptr); ESMC_Test((route_ptr!=0 && rc==ESMF_SUCCESS), name, failMsg, &result, ESMF_SRCLINE); // test internal dynamic allocation within statically allocated // ESMC_Route rc = route_ptr->ESMC_RouteConstruct(vm); sprintf(failMsg, "rc = %d", rc); ESMC_Test((rc==ESMF_SUCCESS), name, failMsg, &result, ESMF_SRCLINE); // test setting of configuration values //ESMC_RouteConfig config_set; //rc = route_ptr->ESMC_RouteSetConfig(config_set); //sprintf(failMsg, "rc = %d, config_set = %f", rc, config_set); //ESMC_Test((rc==ESMF_SUCCESS), // name, failMsg, &result, ESMF_SRCLINE); // test getting of configuration values, // compare to values set previously //ESMC_RouteConfig config_get; //rc = route_ptr->ESMC_RouteGetConfig(&config_get); //sprintf(failMsg, "rc = %d, config_get = %f", rc, config_get); //ESMC_Test((rc==ESMF_SUCCESS && config_get == config_set), // name, failMsg, &result, ESMF_SRCLINE); // test setting of ESMC_Route members values int value_set; //<value type> value_set; //rc = route_ptr->ESMC_RouteSet<Value>(value_set); //sprintf(failMsg, "rc = %d, value_set = %f", rc, value_set); //ESMC_Test((rc==ESMF_SUCCESS), // name, failMsg, &result, ESMF_SRCLINE); // test getting of ESMC_Route members values, // compare to values set previously int value_get; //<value type> value_get; //rc = route_ptr->ESMC_RouteGet<Value>(&value_get); //sprintf(failMsg, "rc = %d, value_get = %f", rc, value_get); //ESMC_Test((rc==ESMF_SUCCESS && value_get == value_set), // name, failMsg, &result, ESMF_SRCLINE); // test validate method via option string char validate_options[ESMF_MAXSTR]; rc = route_ptr->ESMC_RouteValidate(validate_options); sprintf(failMsg, "rc = %d, validate_options = %s", rc, validate_options); ESMC_Test((rc==ESMF_SUCCESS), name, failMsg, &result, ESMF_SRCLINE); // test print method via option string char print_options[ESMF_MAXSTR]; rc = route_ptr->ESMC_RoutePrint(print_options); sprintf(failMsg, "rc = %d, print_options = %s", rc, print_options); ESMC_Test((rc==ESMF_SUCCESS), name, failMsg, &result, ESMF_SRCLINE); // test internal dynamic deallocation within statically allocated // ESMC_Route rc = route_ptr->ESMC_RouteDestruct(); sprintf(failMsg, "rc = %d", rc); ESMC_Test((rc==ESMF_SUCCESS), name, failMsg, &result, ESMF_SRCLINE); // test dynamic deallocation of ESMC_Route // also tests destructor rc = ESMC_RouteDestroy(route_ptr); sprintf(failMsg, "rc = %d, route_ptr = %p", rc, route_ptr); ESMC_Test((rc==ESMF_SUCCESS), name, failMsg, &result, ESMF_SRCLINE); // return number of failures to environment; 0 = success (all pass) return(result); } // end unit test main()