|
| 1 | +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Kuan-Chih Wang <kuanchihw@ucar.edu> |
| 3 | +Date: Mon, 1 Apr 2024 16:46:50 -0600 |
| 4 | +Subject: [PATCH] Prefix all MPAS namelist group and option names |
| 5 | + |
| 6 | +The following transformations are performed for each MPAS namelist group and |
| 7 | +option name: |
| 8 | + |
| 9 | +1. Leading `config_` is removed recursively from the name. Case-insensitive. |
| 10 | +2. Leading `mpas_` is removed recursively from the name. Case-insensitive. |
| 11 | +3. Prepend `mpas_` to the name. |
| 12 | + |
| 13 | +As a result, it is easier to distinguish MPAS namelist groups and options from |
| 14 | +CAM-SIMA ones. Note that only namelist I/O is affected. Internally, MPAS still |
| 15 | +refers to its namelist options by their original names due to compatibility reasons. |
| 16 | + |
| 17 | +This downstream patch is maintained by CAM-SIMA for its particular use case of MPAS. |
| 18 | +--- |
| 19 | + src/tools/registry/gen_inc.c | 80 +++++++++++++++++++++++++++++++++++- |
| 20 | + src/tools/registry/gen_inc.h | 4 ++ |
| 21 | + 2 files changed, 82 insertions(+), 2 deletions(-) |
| 22 | + |
| 23 | +diff --git a/src/tools/registry/gen_inc.c b/src/tools/registry/gen_inc.c |
| 24 | +index 94f5f714..95b2502c 100644 |
| 25 | +--- a/src/tools/registry/gen_inc.c |
| 26 | ++++ b/src/tools/registry/gen_inc.c |
| 27 | +@@ -15,6 +15,10 @@ |
| 28 | + #include "fortprintf.h" |
| 29 | + #include "utility.h" |
| 30 | + |
| 31 | ++#ifdef MPAS_CAM_DYCORE |
| 32 | ++#include <ctype.h> |
| 33 | ++#endif |
| 34 | ++ |
| 35 | + void process_core_macro(const char *macro, const char *val, va_list ap); |
| 36 | + void process_domain_macro(const char *macro, const char *val, va_list ap); |
| 37 | + |
| 38 | +@@ -696,8 +700,18 @@ int parse_namelist_records_from_registry(ezxml_t registry)/*{{{*/ |
| 39 | + ezxml_t nmlrecs_xml, nmlopt_xml; |
| 40 | + |
| 41 | + const char *const_core; |
| 42 | +- const char *nmlrecname, *nmlrecindef, *nmlrecinsub; |
| 43 | +- const char *nmloptname, *nmlopttype, *nmloptval, *nmloptunits, *nmloptdesc, *nmloptposvals, *nmloptindef; |
| 44 | ++#ifdef MPAS_CAM_DYCORE |
| 45 | ++ const char *orinmlrecname; |
| 46 | ++ const char *orinmloptname; |
| 47 | ++ // Fortran variable names have a length limit of 63 characters. +1 for the terminating null character. |
| 48 | ++ char nmlrecname[64]; |
| 49 | ++ char nmloptname[64]; |
| 50 | ++#else |
| 51 | ++ const char *nmlrecname; |
| 52 | ++ const char *nmloptname; |
| 53 | ++#endif |
| 54 | ++ const char *nmlrecindef, *nmlrecinsub; |
| 55 | ++ const char *nmlopttype, *nmloptval, *nmloptunits, *nmloptdesc, *nmloptposvals, *nmloptindef; |
| 56 | + |
| 57 | + char pool_name[1024]; |
| 58 | + char core_string[1024]; |
| 59 | +@@ -743,7 +757,12 @@ int parse_namelist_records_from_registry(ezxml_t registry)/*{{{*/ |
| 60 | + |
| 61 | + // Parse Namelist Records |
| 62 | + for (nmlrecs_xml = ezxml_child(registry, "nml_record"); nmlrecs_xml; nmlrecs_xml = nmlrecs_xml->next){ |
| 63 | ++#ifdef MPAS_CAM_DYCORE |
| 64 | ++ orinmlrecname = ezxml_attr(nmlrecs_xml, "name"); |
| 65 | ++ transform_name(nmlrecname, sizeof(nmlrecname), orinmlrecname); |
| 66 | ++#else |
| 67 | + nmlrecname = ezxml_attr(nmlrecs_xml, "name"); |
| 68 | ++#endif |
| 69 | + nmlrecindef = ezxml_attr(nmlrecs_xml, "in_defaults"); |
| 70 | + nmlrecinsub = ezxml_attr(nmlrecs_xml, "in_subpool"); |
| 71 | + |
| 72 | +@@ -777,7 +796,12 @@ int parse_namelist_records_from_registry(ezxml_t registry)/*{{{*/ |
| 73 | + |
| 74 | + // Define variable definitions prior to reading the namelist in. |
| 75 | + for (nmlopt_xml = ezxml_child(nmlrecs_xml, "nml_option"); nmlopt_xml; nmlopt_xml = nmlopt_xml->next){ |
| 76 | ++#ifdef MPAS_CAM_DYCORE |
| 77 | ++ orinmloptname = ezxml_attr(nmlopt_xml, "name"); |
| 78 | ++ transform_name(nmloptname, sizeof(nmloptname), orinmloptname); |
| 79 | ++#else |
| 80 | + nmloptname = ezxml_attr(nmlopt_xml, "name"); |
| 81 | ++#endif |
| 82 | + nmlopttype = ezxml_attr(nmlopt_xml, "type"); |
| 83 | + nmloptval = ezxml_attr(nmlopt_xml, "default_value"); |
| 84 | + nmloptunits = ezxml_attr(nmlopt_xml, "units"); |
| 85 | +@@ -809,7 +833,12 @@ int parse_namelist_records_from_registry(ezxml_t registry)/*{{{*/ |
| 86 | + // Define the namelist block, to read the namelist record in. |
| 87 | + fortprintf(fd, " namelist /%s/ &\n", nmlrecname); |
| 88 | + for (nmlopt_xml = ezxml_child(nmlrecs_xml, "nml_option"); nmlopt_xml; nmlopt_xml = nmlopt_xml->next){ |
| 89 | ++#ifdef MPAS_CAM_DYCORE |
| 90 | ++ orinmloptname = ezxml_attr(nmlopt_xml, "name"); |
| 91 | ++ transform_name(nmloptname, sizeof(nmloptname), orinmloptname); |
| 92 | ++#else |
| 93 | + nmloptname = ezxml_attr(nmlopt_xml, "name"); |
| 94 | ++#endif |
| 95 | + if(nmlopt_xml->next){ |
| 96 | + fortprintf(fd, " %s, &\n", nmloptname); |
| 97 | + } else { |
| 98 | +@@ -840,7 +869,12 @@ int parse_namelist_records_from_registry(ezxml_t registry)/*{{{*/ |
| 99 | + // Define broadcast calls for namelist values. |
| 100 | + fortprintf(fd, " if (ierr <= 0) then\n"); |
| 101 | + for (nmlopt_xml = ezxml_child(nmlrecs_xml, "nml_option"); nmlopt_xml; nmlopt_xml = nmlopt_xml->next){ |
| 102 | ++#ifdef MPAS_CAM_DYCORE |
| 103 | ++ orinmloptname = ezxml_attr(nmlopt_xml, "name"); |
| 104 | ++ transform_name(nmloptname, sizeof(nmloptname), orinmloptname); |
| 105 | ++#else |
| 106 | + nmloptname = ezxml_attr(nmlopt_xml, "name"); |
| 107 | ++#endif |
| 108 | + nmlopttype = ezxml_attr(nmlopt_xml, "type"); |
| 109 | + |
| 110 | + if(strncmp(nmlopttype, "real", 1024) == 0){ |
| 111 | +@@ -858,7 +892,12 @@ int parse_namelist_records_from_registry(ezxml_t registry)/*{{{*/ |
| 112 | + fortprintf(fd, " call mpas_log_write(' The following values will be used for variables in this record:')\n"); |
| 113 | + fortprintf(fd, " call mpas_log_write(' ')\n"); |
| 114 | + for (nmlopt_xml = ezxml_child(nmlrecs_xml, "nml_option"); nmlopt_xml; nmlopt_xml = nmlopt_xml->next){ |
| 115 | ++#ifdef MPAS_CAM_DYCORE |
| 116 | ++ orinmloptname = ezxml_attr(nmlopt_xml, "name"); |
| 117 | ++ transform_name(nmloptname, sizeof(nmloptname), orinmloptname); |
| 118 | ++#else |
| 119 | + nmloptname = ezxml_attr(nmlopt_xml, "name"); |
| 120 | ++#endif |
| 121 | + nmlopttype = ezxml_attr(nmlopt_xml, "type"); |
| 122 | + |
| 123 | + if (strncmp(nmlopttype, "character", 1024) == 0) { |
| 124 | +@@ -885,10 +924,18 @@ int parse_namelist_records_from_registry(ezxml_t registry)/*{{{*/ |
| 125 | + fortprintf(fd, "\n"); |
| 126 | + |
| 127 | + for (nmlopt_xml = ezxml_child(nmlrecs_xml, "nml_option"); nmlopt_xml; nmlopt_xml = nmlopt_xml->next){ |
| 128 | ++#ifdef MPAS_CAM_DYCORE |
| 129 | ++ orinmloptname = ezxml_attr(nmlopt_xml, "name"); |
| 130 | ++ transform_name(nmloptname, sizeof(nmloptname), orinmloptname); |
| 131 | ++ |
| 132 | ++ fortprintf(fd, " call mpas_pool_add_config(%s, '%s', %s)\n", pool_name, orinmloptname, nmloptname); |
| 133 | ++ fortprintf(fcg, " call mpas_pool_get_config(configPool, '%s', %s)\n", orinmloptname, nmloptname); |
| 134 | ++#else |
| 135 | + nmloptname = ezxml_attr(nmlopt_xml, "name"); |
| 136 | + |
| 137 | + fortprintf(fd, " call mpas_pool_add_config(%s, '%s', %s)\n", pool_name, nmloptname, nmloptname); |
| 138 | + fortprintf(fcg, " call mpas_pool_get_config(configPool, '%s', %s)\n", nmloptname, nmloptname); |
| 139 | ++#endif |
| 140 | + } |
| 141 | + fortprintf(fd, "\n"); |
| 142 | + fortprintf(fcg, "\n"); |
| 143 | +@@ -2532,3 +2579,32 @@ int parse_structs_from_registry(ezxml_t registry)/*{{{*/ |
| 144 | + |
| 145 | + return 0; |
| 146 | + }/*}}}*/ |
| 147 | ++ |
| 148 | ++ |
| 149 | ++#ifdef MPAS_CAM_DYCORE |
| 150 | ++// Perform transformations for namelist group and option names. |
| 151 | ++void transform_name(char *new_name, const size_t new_name_size, const char *old_name) { |
| 152 | ++ const char *const new_prefix = "mpas_"; |
| 153 | ++ const char *const old_prefix = "config_"; |
| 154 | ++ size_t size = 0; |
| 155 | ++ |
| 156 | ++ if (!new_name || !old_name || new_name_size == 0) return; |
| 157 | ++ |
| 158 | ++ // Remove all leading whitespaces by moving pointer forward. |
| 159 | ++ while (*old_name != '\0' && isspace((unsigned char) *old_name)) old_name++; |
| 160 | ++ |
| 161 | ++ // Remove all leading `config_` by moving pointer forward. |
| 162 | ++ while (strncasecmp(old_name, old_prefix, strlen(old_prefix)) == 0) old_name += strlen(old_prefix); |
| 163 | ++ |
| 164 | ++ // Remove all leading `mpas_` by moving pointer forward. |
| 165 | ++ while (strncasecmp(old_name, new_prefix, strlen(new_prefix)) == 0) old_name += strlen(new_prefix); |
| 166 | ++ |
| 167 | ++ *new_name = '\0'; |
| 168 | ++ size = snprintf(NULL, 0, "%s%s", new_prefix, old_name) + 1; |
| 169 | ++ snprintf(new_name, size > new_name_size ? new_name_size : size, "%s%s", new_prefix, old_name); |
| 170 | ++ |
| 171 | ++ // Remove all trailing whitespaces by zeroing (nulling) out. |
| 172 | ++ new_name += strlen(new_name) - 1; |
| 173 | ++ while (*new_name != '\0' && isspace((unsigned char) *new_name)) *new_name-- = '\0'; |
| 174 | ++} |
| 175 | ++#endif |
| 176 | +diff --git a/src/tools/registry/gen_inc.h b/src/tools/registry/gen_inc.h |
| 177 | +index fc94e78b..69a76ace 100644 |
| 178 | +--- a/src/tools/registry/gen_inc.h |
| 179 | ++++ b/src/tools/registry/gen_inc.h |
| 180 | +@@ -38,3 +38,7 @@ int push_attributes(ezxml_t currentPosition); |
| 181 | + int merge_structs_and_var_arrays(ezxml_t currentPosition); |
| 182 | + int merge_streams(ezxml_t registry); |
| 183 | + int parse_structs_from_registry(ezxml_t registry); |
| 184 | ++ |
| 185 | ++#ifdef MPAS_CAM_DYCORE |
| 186 | ++void transform_name(char *new_name, const size_t new_name_size, const char *old_name); |
| 187 | ++#endif |
| 188 | +-- |
| 189 | +2.43.0 |
| 190 | + |
0 commit comments