diff options
author | rideout <rideout@6a38eb6e-646e-4a02-a296-d141613ad6c4> | 2003-01-06 21:37:45 +0000 |
---|---|---|
committer | rideout <rideout@6a38eb6e-646e-4a02-a296-d141613ad6c4> | 2003-01-06 21:37:45 +0000 |
commit | fe4da9e6d8d846bbeda3350a99822e543d749962 (patch) | |
tree | 9c1e5921707182cf87f94cf828913492576894a8 | |
parent | f83e20b34998c8529ba12a6993975b32f1251442 (diff) |
Include Boundary.h (for BndNone()).
Remove unneeded STRBUFFSIZE macro.
Use BndNone() when NULL function pointer passed to
Boundary_RegisterPhysicalBC() (for non-local physical bcs).
Precompute return value of Boundary_SelectedGVs(), so it will be much
faster when called with array_size=0.
Fix memory leak in Boundary_ApplyPhysicalBCs().
Fix some improperly aligned curly braces.
git-svn-id: http://svn.cactuscode.org/arrangements/CactusBase/Boundary/trunk@191 6a38eb6e-646e-4a02-a296-d141613ad6c4
-rw-r--r-- | src/Boundary.c | 169 |
1 files changed, 107 insertions, 62 deletions
diff --git a/src/Boundary.c b/src/Boundary.c index f000108..233d1ed 100644 --- a/src/Boundary.c +++ b/src/Boundary.c @@ -15,6 +15,7 @@ #include "cctk_Arguments.h" #include "util_Table.h" #include "util_String.h" +#include "Boundary.h" static const char *rcsid = "$Header$"; @@ -23,14 +24,9 @@ CCTK_FILEVERSION(CactusBase_Boundary_Boundary_c); /******************************************************************** ********************* Local Data Types *********************** ********************************************************************/ -/*#define DEBUG 1*/ -#ifdef STRBUFFSIZE -Size of string buffers. Max length of variable name is 28+32+2. -#else -#define STRBUFFSIZE 63 -#endif +/* #define DEBUG 1 */ -/* pointer to function which implements the boundary condition: */ +/* pointer to function which implements a physical boundary condition: */ typedef int (*phys_bc_fn_ptr)(const cGH *, int, int *, int *); /* Linked list for holding variables selected for a bc: @@ -180,13 +176,21 @@ CCTK_INT Boundary_Boundary_RegisterPhysicalBC(CCTK_POINTER GH, /* Ignore GH for now */ GH = GH; + /* Check if NULL has been passed for fn_pointer */ + if (!fn_pointer) + { + /* Use dummy function if NULL function registered (e.g. for + non-local physical bcs) */ + fn_pointer = (CCTK_FPOINTER) &BndNone; + } + /* Check input arguments */ - if (!fn_pointer) + if (!fn_pointer) { CCTK_VWarn(2, __LINE__, __FILE__, CCTK_THORNSTRING, "Null pointer passed to Boundary_RegisterPhysicalBC. " "Is this intentional?"); - } + } /* Create the registered routines table if necessary */ if (physbc_table_handle == -1) @@ -271,7 +275,8 @@ CCTK_INT Boundary_Boundary_SelectVarForBC(CCTK_POINTER GH, retval = 0; var_index = CCTK_VarIndex(var_name); - if (var_index<0) { + if (var_index<0) + { CCTK_VWarn(1, __LINE__, __FILE__, CCTK_THORNSTRING, "Invalid variable name"); retval = -3; @@ -464,8 +469,8 @@ CCTK_INT Boundary_Boundary_SelectVarForBCI(CCTK_POINTER GH, bcdata_list->bc_name = bc_name; bcdata_list->start = new_entry; bcdata_list->num = 1; /* This is incremented upon creation, since we - are about to exit this function() */ - } + are about to exit this function() */ + } return retval; } @@ -474,7 +479,7 @@ CCTK_INT Boundary_Boundary_SelectVarForBCI(CCTK_POINTER GH, @routine Boundary_Boundary_SelectGroupForBC @date Thu Dec 26 21:45:34 CET 2002 @author David Rideout - @desc + @desc Used to select a Cactus variable group to have boundary conditions applied, using the group name. @enddesc @@ -511,7 +516,8 @@ CCTK_INT Boundary_Boundary_SelectVarForBCI(CCTK_POINTER GH, CCTK_INT Boundary_Boundary_SelectGroupForBC(CCTK_POINTER GH, CCTK_INT table_handle, CCTK_STRING group_name, - CCTK_STRING bc_name) { + CCTK_STRING bc_name) +{ int retval, gi; retval = 0; @@ -574,7 +580,8 @@ CCTK_INT Boundary_Boundary_SelectGroupForBC(CCTK_POINTER GH, CCTK_INT Boundary_Boundary_SelectGroupForBCI(CCTK_POINTER GH, CCTK_INT table_handle, CCTK_INT group_index, - CCTK_STRING bc_name) { + CCTK_STRING bc_name) +{ int num_vars, vi, max_vi, retval; /* Get var indices from group name */ @@ -644,8 +651,9 @@ CCTK_INT Boundary_Boundary_SelectedGVs(CCTK_POINTER GH, CCTK_INT array_size, CCTK_POINTER var_indices, CCTK_POINTER table_handles, - CCTK_STRING bc_name) { - int retval; + CCTK_STRING bc_name) +{ + int retval, i; struct BCVAR *current; struct BCDATA *current_bcdata; @@ -658,35 +666,36 @@ CCTK_INT Boundary_Boundary_SelectedGVs(CCTK_POINTER GH, #ifdef DEBUG printf("Boundary_Boundary_SelectedGVs: called with bc_name=\"%s\" array_size=%d\n", bc_name, array_size); #endif - if (bc_name) + + /* Step through bcdata list to find starting point, and to compute return + value */ + for (current_bcdata = bcdata_list; + current_bcdata; + current_bcdata = current_bcdata->next) { - /* If bc_name is non-NULL, use bcdata list to find starting point */ - for (current_bcdata = bcdata_list; - current_bcdata; - current_bcdata = current_bcdata->next) - { #ifdef DEBUG - printf("Boundary_Boundary_SelectedGVs: looping through bcdata list\n" - " current_bcdata->bc_name=\"%s\"\n", - current_bcdata->bc_name); + printf("Boundary_Boundary_SelectedGVs: looping through bcdata list\n" + " current_bcdata->bc_name=\"%s\"\n", + current_bcdata->bc_name); #endif - if (CCTK_Equals(current_bcdata->bc_name,bc_name)) - { - + if (!bc_name || CCTK_Equals(current_bcdata->bc_name,bc_name)) + { #ifdef DEBUG - printf("Boundary_Boundary_SelectedGVs: strings match, using this start value\n"); + printf("Boundary_Boundary_SelectedGVs: strings match, using this start value\n"); #endif - /* If bc_name matches, use this entry to set current */ - current = current_bcdata->start; + /* If bc_name matches, use this entry to set current */ + current = current_bcdata->start; + retval += current_bcdata->num; + if (bc_name) + { + /* No more needs be added to retval if a specific bc_name has + been requested. */ break; } } -#ifdef DEBUG - printf("Boundary_Boundary_SelectedGVs: initializing current to %p\n", - current); -#endif + } /* Do we want to return an error code if an 'invalid' bc_name is used? * if (!current) * { @@ -695,16 +704,23 @@ CCTK_INT Boundary_Boundary_SelectedGVs(CCTK_POINTER GH, * retval = -1; * } */ - } else + if (!bc_name) { /* If bc_name is NULL then start from the beginning */ current = selections_list; } +#ifdef DEBUG + printf("Boundary_Boundary_SelectedGVs: initializing current to %p\n", + current); +#endif /* If current is still NULL, bc_name did not match any entry in the * bcdata list, i.e. no variables were selected for this bc. + * + * If array_size is zero, then just exit immediately, because there + * is nothing more to do. */ - if (current) + if (current && array_size) { /* loop through selections list, grabbing entries with bc_name, or * all if bc_name==NULL @@ -712,20 +728,28 @@ CCTK_INT Boundary_Boundary_SelectedGVs(CCTK_POINTER GH, * could save a string comparison at each iteration if we also * stored bcdata->end */ - for (; + for (i=0; /* i indexes the location in the returned arrays, + and is incremented after use */ current; current = current->next_entry) { - if (!bc_name || CCTK_Equals(current->bc_name, bc_name)) + if (!bc_name || CCTK_Equals(current->bc_name, bc_name)) { - if (retval < array_size) + if (i < array_size) { - ((int *) table_handles)[retval] = current->table; - ((int *) var_indices)[retval] = current->var_index; + ((int *) table_handles)[i] = current->table; + ((int *) var_indices)[i] = current->var_index; } - ++retval; - } else if (retval) + ++i; + } else { + if (i==0) + { + printf("Boundary_Boundary_SelectedGVs: Internal error: I am " + "past the relevant section of the selections list, yet no " + "vars have been 'selected'.\n"); + } + /* can stop looking if we are past the relevant section of the list */ break; @@ -761,7 +785,8 @@ CCTK_INT Boundary_Boundary_SelectedGVs(CCTK_POINTER GH, @endreturndesc @@*/ -void Boundary_ApplyPhysicalBCs(CCTK_ARGUMENTS) { +void Boundary_ApplyPhysicalBCs(CCTK_ARGUMENTS) +{ DECLARE_CCTK_ARGUMENTS; phys_bc_fn_ptr bc_fn; int num_vars, *vars, *tables, err, max_num_vars; @@ -783,16 +808,26 @@ void Boundary_ApplyPhysicalBCs(CCTK_ARGUMENTS) { /* Allocate memory to hold selected bcs */ num_vars = current_bcdata->num; - if (num_vars > max_num_vars) { - max_num_vars = num_vars; + if (num_vars > max_num_vars) + { + max_num_vars = num_vars; /* store new maximum */ + if (vars) + { + /* reallocate vars and tables if necessary */ + realloc(vars, num_vars*sizeof(int)); + realloc(tables, num_vars*sizeof(int)); + } else + { vars = (int *) malloc(num_vars*sizeof(int)); tables = (int *) malloc(num_vars*sizeof(int)); + } } /* get selected vars for this bc_name*/ err = Boundary_Boundary_SelectedGVs(cctkGH, num_vars, vars, tables, current_bcdata->bc_name); - if (err<0) /* This is a redundant test for now, Boundary_Boundary_SelectedGVs never returns <0 */ + if (err<0) /* This is a redundant test for now, + Boundary_Boundary_SelectedGVs never returns <0 */ { CCTK_VWarn(1, __LINE__, __FILE__, CCTK_THORNSTRING, "Error in Boundary_SelectedGVs for %s boundary condition", @@ -805,7 +840,7 @@ void Boundary_ApplyPhysicalBCs(CCTK_ARGUMENTS) { current_bcdata->bc_name, num_vars); } - /* Get the fn ptr for the bc*/ + /* Get the fn ptr for the bc */ #ifdef DEBUG printf("Boundary_ApplyPhysicalBCs: current_bcdata->bc_name=\"%s\"\n", current_bcdata->bc_name); @@ -821,8 +856,8 @@ void Boundary_ApplyPhysicalBCs(CCTK_ARGUMENTS) { /* Apply bc to vi */ #ifdef DEBUG - printf("Attempting to call boundary condition!\n" - "Using function pointer %p with arguments %p, %d, vars, tables\n", + printf("Boundary_ApplyPhysicalBCs: Attempting to call boundary condition\n" + " Using function pointer %p with arguments cctkGH %p, num_vars %d, vars, tables\n", (void *) bc_fn, (const void *) cctkGH, num_vars); #endif err = (*bc_fn)(cctkGH, num_vars, vars, tables); @@ -854,13 +889,15 @@ void Boundary_ApplyPhysicalBCs(CCTK_ARGUMENTS) { @endhistory @@*/ -void Boundary_ClearSelection(void) { +void Boundary_ClearSelection(void) +{ struct BCVAR *current, *next; struct BCDATA *current_bcdata, *next_bcdata; /* Free selections list */ next = selections_list; - for (current = selections_list; next; current = next) { + for (current = selections_list; next; current = next) + { next = current->next_entry; free(current->bc_name); free(current); @@ -919,25 +956,33 @@ void Boundary_ClearSelection(void) { has smaller variable index @endreturndesc @@*/ -int entry_less_than(struct BCVAR *new, struct BCVAR *current) { +int entry_less_than(struct BCVAR *new, struct BCVAR *current) +{ int retval; /* can assume both arguments are valid (non-null) */ /* Perhaps this can be done more intelligently... */ retval = Util_StrCmpi(new->bc_name, current->bc_name); - if (retval < 0) { + if (retval < 0) + { retval = 1; /* new < current */ - } else if (retval>0) { + } else if (retval>0) + { retval = 0; - } else if (new->table < current->table) { + } else if (new->table < current->table) + { retval = 2; - } else if (new->table > current->table) { + } else if (new->table > current->table) + { retval = 0; - } else if (new->var_index < current->var_index) { + } else if (new->var_index < current->var_index) + { retval = 3; - } else if (new->var_index > current->var_index) { + } else if (new->var_index > current->var_index) + { retval = 0; - } else { + } else + { CCTK_VWarn(1, __LINE__, __FILE__, CCTK_THORNSTRING, "Duplicate entry %s marked for boundary condition %s, " "with table handle %d\n", CCTK_VarName(new->var_index), |