Skip to content

Commit 930e9f9

Browse files
committed
Prefix all namelist group and option names for MPAS dycore
When MPAS is used as a dynamical core, the following transformations are performed for each namelist group and option name: 1. Leading `config_` is removed recursively from the name. Case-insensitive. 2. Leading `mpas_` is removed recursively from the name. Case-insensitive. 3. Prepend `mpas_` to the name. As a result, it is now easier to distinguish MPAS namelist groups and options from CAM-SIMA ones. Additionally, the possibility of name collisions with CAM-SIMA ones is also resolved once and for all. Note that only namelist I/O is affected. Internally, MPAS still refers to its namelist options by their original names due to compatibility reasons.
1 parent 37aa961 commit 930e9f9

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

src/tools/registry/gen_inc.c

+87
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
#include "fortprintf.h"
1616
#include "utility.h"
1717

18+
#ifdef MPAS_CAM_DYCORE
19+
#include <ctype.h>
20+
#endif
21+
1822
void process_core_macro(const char *macro, const char *val, va_list ap);
1923
void process_domain_macro(const char *macro, const char *val, va_list ap);
2024

@@ -699,6 +703,14 @@ int parse_namelist_records_from_registry(ezxml_t registry)/*{{{*/
699703
const char *nmlrecname, *nmlrecindef, *nmlrecinsub;
700704
const char *nmloptname, *nmlopttype, *nmloptval, *nmloptunits, *nmloptdesc, *nmloptposvals, *nmloptindef;
701705

706+
#ifdef MPAS_CAM_DYCORE
707+
// Fortran variable names have a length limit of 63 characters. + 1 for the terminating null character.
708+
char new_nmlrecname[64];
709+
char new_nmloptname[64];
710+
const char *old_nmlrecname;
711+
const char *old_nmloptname;
712+
#endif
713+
702714
char pool_name[1024];
703715
char core_string[1024];
704716

@@ -743,7 +755,14 @@ int parse_namelist_records_from_registry(ezxml_t registry)/*{{{*/
743755

744756
// Parse Namelist Records
745757
for (nmlrecs_xml = ezxml_child(registry, "nml_record"); nmlrecs_xml; nmlrecs_xml = nmlrecs_xml->next){
758+
#ifdef MPAS_CAM_DYCORE
759+
old_nmlrecname = ezxml_attr(nmlrecs_xml, "name");
760+
transform_name(new_nmlrecname, sizeof(new_nmlrecname), old_nmlrecname);
761+
762+
nmlrecname = new_nmlrecname;
763+
#else
746764
nmlrecname = ezxml_attr(nmlrecs_xml, "name");
765+
#endif
747766
nmlrecindef = ezxml_attr(nmlrecs_xml, "in_defaults");
748767
nmlrecinsub = ezxml_attr(nmlrecs_xml, "in_subpool");
749768

@@ -777,7 +796,14 @@ int parse_namelist_records_from_registry(ezxml_t registry)/*{{{*/
777796

778797
// Define variable definitions prior to reading the namelist in.
779798
for (nmlopt_xml = ezxml_child(nmlrecs_xml, "nml_option"); nmlopt_xml; nmlopt_xml = nmlopt_xml->next){
799+
#ifdef MPAS_CAM_DYCORE
800+
old_nmloptname = ezxml_attr(nmlopt_xml, "name");
801+
transform_name(new_nmloptname, sizeof(new_nmloptname), old_nmloptname);
802+
803+
nmloptname = new_nmloptname;
804+
#else
780805
nmloptname = ezxml_attr(nmlopt_xml, "name");
806+
#endif
781807
nmlopttype = ezxml_attr(nmlopt_xml, "type");
782808
nmloptval = ezxml_attr(nmlopt_xml, "default_value");
783809
nmloptunits = ezxml_attr(nmlopt_xml, "units");
@@ -809,7 +835,14 @@ int parse_namelist_records_from_registry(ezxml_t registry)/*{{{*/
809835
// Define the namelist block, to read the namelist record in.
810836
fortprintf(fd, " namelist /%s/ &\n", nmlrecname);
811837
for (nmlopt_xml = ezxml_child(nmlrecs_xml, "nml_option"); nmlopt_xml; nmlopt_xml = nmlopt_xml->next){
838+
#ifdef MPAS_CAM_DYCORE
839+
old_nmloptname = ezxml_attr(nmlopt_xml, "name");
840+
transform_name(new_nmloptname, sizeof(new_nmloptname), old_nmloptname);
841+
842+
nmloptname = new_nmloptname;
843+
#else
812844
nmloptname = ezxml_attr(nmlopt_xml, "name");
845+
#endif
813846
if(nmlopt_xml->next){
814847
fortprintf(fd, " %s, &\n", nmloptname);
815848
} else {
@@ -840,7 +873,14 @@ int parse_namelist_records_from_registry(ezxml_t registry)/*{{{*/
840873
// Define broadcast calls for namelist values.
841874
fortprintf(fd, " if (ierr <= 0) then\n");
842875
for (nmlopt_xml = ezxml_child(nmlrecs_xml, "nml_option"); nmlopt_xml; nmlopt_xml = nmlopt_xml->next){
876+
#ifdef MPAS_CAM_DYCORE
877+
old_nmloptname = ezxml_attr(nmlopt_xml, "name");
878+
transform_name(new_nmloptname, sizeof(new_nmloptname), old_nmloptname);
879+
880+
nmloptname = new_nmloptname;
881+
#else
843882
nmloptname = ezxml_attr(nmlopt_xml, "name");
883+
#endif
844884
nmlopttype = ezxml_attr(nmlopt_xml, "type");
845885

846886
if(strncmp(nmlopttype, "real", 1024) == 0){
@@ -858,7 +898,14 @@ int parse_namelist_records_from_registry(ezxml_t registry)/*{{{*/
858898
fortprintf(fd, " call mpas_log_write(' The following values will be used for variables in this record:')\n");
859899
fortprintf(fd, " call mpas_log_write(' ')\n");
860900
for (nmlopt_xml = ezxml_child(nmlrecs_xml, "nml_option"); nmlopt_xml; nmlopt_xml = nmlopt_xml->next){
901+
#ifdef MPAS_CAM_DYCORE
902+
old_nmloptname = ezxml_attr(nmlopt_xml, "name");
903+
transform_name(new_nmloptname, sizeof(new_nmloptname), old_nmloptname);
904+
905+
nmloptname = new_nmloptname;
906+
#else
861907
nmloptname = ezxml_attr(nmlopt_xml, "name");
908+
#endif
862909
nmlopttype = ezxml_attr(nmlopt_xml, "type");
863910

864911
if (strncmp(nmlopttype, "character", 1024) == 0) {
@@ -885,10 +932,21 @@ int parse_namelist_records_from_registry(ezxml_t registry)/*{{{*/
885932
fortprintf(fd, "\n");
886933

887934
for (nmlopt_xml = ezxml_child(nmlrecs_xml, "nml_option"); nmlopt_xml; nmlopt_xml = nmlopt_xml->next){
935+
#ifdef MPAS_CAM_DYCORE
936+
old_nmloptname = ezxml_attr(nmlopt_xml, "name");
937+
transform_name(new_nmloptname, sizeof(new_nmloptname), old_nmloptname);
938+
939+
nmloptname = new_nmloptname;
940+
941+
// Keep namelist options to their original names in MPAS pools for compatibility reasons.
942+
fortprintf(fd, " call mpas_pool_add_config(%s, '%s', %s)\n", pool_name, old_nmloptname, nmloptname);
943+
fortprintf(fcg, " call mpas_pool_get_config(configPool, '%s', %s)\n", old_nmloptname, nmloptname);
944+
#else
888945
nmloptname = ezxml_attr(nmlopt_xml, "name");
889946

890947
fortprintf(fd, " call mpas_pool_add_config(%s, '%s', %s)\n", pool_name, nmloptname, nmloptname);
891948
fortprintf(fcg, " call mpas_pool_get_config(configPool, '%s', %s)\n", nmloptname, nmloptname);
949+
#endif
892950
}
893951
fortprintf(fd, "\n");
894952
fortprintf(fcg, "\n");
@@ -2532,3 +2590,32 @@ int parse_structs_from_registry(ezxml_t registry)/*{{{*/
25322590

25332591
return 0;
25342592
}/*}}}*/
2593+
2594+
2595+
#ifdef MPAS_CAM_DYCORE
2596+
// Perform transformations for namelist group and option names.
2597+
void transform_name(char *new_name, const size_t new_name_size, const char *old_name) {
2598+
const char *const new_prefix = "mpas_";
2599+
const char *const old_prefix = "config_";
2600+
size_t size = 0;
2601+
2602+
if (!new_name || !old_name || new_name_size == 0) return;
2603+
2604+
// Remove all leading whitespaces by moving pointer forward.
2605+
while (*old_name != '\0' && isspace((unsigned char) *old_name)) old_name++;
2606+
2607+
// Remove all leading "config_" by moving pointer forward.
2608+
while (strncasecmp(old_name, old_prefix, strlen(old_prefix)) == 0) old_name += strlen(old_prefix);
2609+
2610+
// Remove all leading "mpas_" by moving pointer forward.
2611+
while (strncasecmp(old_name, new_prefix, strlen(new_prefix)) == 0) old_name += strlen(new_prefix);
2612+
2613+
*new_name = '\0';
2614+
size = snprintf(NULL, 0, "%s%s", new_prefix, old_name) + 1;
2615+
snprintf(new_name, size > new_name_size ? new_name_size : size, "%s%s", new_prefix, old_name);
2616+
2617+
// Remove all trailing whitespaces by zeroing (nulling) out.
2618+
new_name += strlen(new_name) - 1;
2619+
while (*new_name != '\0' && isspace((unsigned char) *new_name)) *new_name-- = '\0';
2620+
}
2621+
#endif

src/tools/registry/gen_inc.h

+4
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,7 @@ int push_attributes(ezxml_t currentPosition);
3838
int merge_structs_and_var_arrays(ezxml_t currentPosition);
3939
int merge_streams(ezxml_t registry);
4040
int parse_structs_from_registry(ezxml_t registry);
41+
42+
#ifdef MPAS_CAM_DYCORE
43+
void transform_name(char *new_name, const size_t new_name_size, const char *old_name);
44+
#endif

0 commit comments

Comments
 (0)