From 14ebce78f4d5d1f36f5c594c2bee66a1d6be3624 Mon Sep 17 00:00:00 2001 From: rideout Date: Wed, 8 Jan 2003 19:34:46 +0000 Subject: Major bug fixes to Boundary_Boundary_SelectVarForBCI(). Debug statements reformatted and added. Many clarifying comments added. git-svn-id: http://svn.cactuscode.org/arrangements/CactusBase/Boundary/trunk@192 6a38eb6e-646e-4a02-a296-d141613ad6c4 --- src/Boundary.c | 172 ++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 108 insertions(+), 64 deletions(-) (limited to 'src') diff --git a/src/Boundary.c b/src/Boundary.c index 233d1ed..fbbe352 100644 --- a/src/Boundary.c +++ b/src/Boundary.c @@ -326,7 +326,6 @@ CCTK_INT Boundary_Boundary_SelectVarForBC(CCTK_POINTER GH, -1 error allocating memory for new entry in selected variables database -2 no such physical boundary condition registered -4 error allocating memory for new entry in 'bcdata' database - -5 error allocating memory for new entry in 'bcdata' database @endreturndesc @@*/ CCTK_INT Boundary_Boundary_SelectVarForBCI(CCTK_POINTER GH, @@ -339,13 +338,15 @@ CCTK_INT Boundary_Boundary_SelectVarForBCI(CCTK_POINTER GH, struct BCVAR *current; struct BCVAR *previous; struct BCDATA *current_bcdata; + struct BCDATA *previous_bcdata; retval = 0; current = NULL; previous = NULL; #ifdef DEBUG - printf("Boundary_Boundary_SelectVarForBCI: called with table_handle=%d, var_index=%d, bc_name=%s\n", table_handle, var_index, bc_name); + printf("Boundary_SelectVarForBCI: called with table_handle=%d, var_index=%d, bc_name=%s\n", table_handle, var_index, bc_name); + printf(" vi %d corresponds to %s\n", var_index, CCTK_VarName(var_index)); #endif /* Ignore GH */ @@ -371,107 +372,139 @@ CCTK_INT Boundary_Boundary_SelectVarForBCI(CCTK_POINTER GH, retval = -1; } #ifdef DEBUG - printf("Boundary_Boundary_SelectVarForBCI: new entry in list will be stored at %p\n", new_entry); + printf("Boundary_SelectVarForBCI: new entry in list will be stored at %p\n", new_entry); #endif /* populate new entry with data */ new_entry -> bc_name = Util_Strdup(bc_name); new_entry -> table = table_handle; new_entry -> var_index = var_index; + /* new_entry -> next_entry will be filled in later */ #ifdef DEBUG - printf("Boundary_Boundary_SelectVarForBCI: sucessfully stored data in new entry\n"); + printf("Boundary_SelectVarForBCI: sucessfully stored data in new entry\n"); #endif /* Use bcdata list to jump to appropriate 'sector' of selections list */ for (current_bcdata = bcdata_list; current_bcdata; - current_bcdata = current_bcdata->next) + previous_bcdata=current_bcdata, current_bcdata = current_bcdata->next) { #ifdef DEBUG - printf("Boundary_Boundary_SelectVarForBCI: looping through bcdata list, at current_bcdata=%p\n",current_bcdata); + printf("Boundary_SelectVarForBCI: looping through bcdata list, at " + "current_bcdata for %s\n",current_bcdata->bc_name); #endif - if (CCTK_Equals(current_bcdata->bc_name,bc_name)) + if (CCTK_Equals(current_bcdata->bc_name,bc_name)) { - current = bcdata_list->start; + current = current_bcdata->start; + current_bcdata->num++; +#ifdef DEBUG + printf("Boundary_SelectVarForBCI: var %s brings bc %s to %d vars\n", + CCTK_VarName(var_index),bc_name, current_bcdata->num); +#endif } } +#ifdef DEBUG + printf("Boundary_SelectVarForBCI: current starting at %p\n", current); +#endif - if (!current && current_bcdata) /* bc_name was not found in bcdata_list, - and bc_data list exists */ + /* If current has not been initialized yet, this is a new bc_name + * that does not appear in the bcdata list. + */ + if (!current) /* bc_name was not found in bcdata_list */ { - /* new bc_name. Add entry to bcdata list. */ - bcdata_list = (struct BCDATA *) malloc(sizeof(struct BCDATA)); - if (!bcdata_list) { +#ifdef DEBUG + printf("Boundary_SelectVarForBCI: adding new entry to bcdata list\n"); +#endif + + /* new bc_name. Add entry to end of bcdata list. */ + current_bcdata = (struct BCDATA *) malloc(sizeof(struct BCDATA)); + if (!current_bcdata) { CCTK_VWarn(1, __LINE__, __FILE__, CCTK_THORNSTRING, "Unable to allocate memory for internal 'bcdata' list"); retval = -4; } else { - current_bcdata->next = bcdata_list; - bcdata_list->next = NULL; - bcdata_list->bc_name = bc_name; - bcdata_list->start = selections_list; /* start it at the beginning of - selections list for now */ - bcdata_list->num = 0; /* This gets incremented after the for(;;) loop - below */ + current_bcdata->next = NULL; + current_bcdata->bc_name = Util_Strdup(bc_name); + current_bcdata->start = NULL; /* this will be filled in below */ + current_bcdata->num = 1; } - } else - { + + /* Place entry into bcdata list */ + if (bcdata_list) /* this is not the first entry */ + { + previous_bcdata->next = current_bcdata; + } else + { + bcdata_list = current_bcdata; + } +#ifdef DEBUG + printf("bcdata_list is now %p\n", bcdata_list); + printf("bcdata_list->bc_name is %s\n", bcdata_list->bc_name); + printf("current_bcdata->bc_name is %s\n", current_bcdata->bc_name); +#endif + /* bc_name does not appear in bcdata_list, so start at beginning */ current = selections_list; } /* enter into correct location in linked list */ + /* Note that this loop is skipped if current is NULL. */ for (; /* starting value for current is selected using the bcdata list, above */ - current && entry_less_than(new_entry,current); - previous = current, current = current->next_entry) + current && !entry_less_than(new_entry,current); + /* continue if not at end of list, and new_entry is greater than + current entry */ + previous = current, current = current->next_entry) + /* store previous value for later use */ { } - /* check to see if this is the first entry */ - if (selections_list) + /* The possibilities: + * 1 nothing NULL: new_entry goes between previous and current + * 2 previous NULL, but current non-null : new_entry goes at the start of the + * selections list + * 3 current NULL, previous non-NULL : new_entry goes at the end of the + * selections list + * 4 both NULL: selections list is empty + */ + if (previous) /* case 1 or 3 */ { - /* enter new_entry here */ - if (previous) + if (current) /* case 1 : goes in middle of list */ { - /* previous==NULL if this is put in the front of a one entry list */ previous->next_entry = new_entry; - } - new_entry->next_entry = current; - if (!previous) + new_entry->next_entry = current; + } else /* case 3 : goes at end of list */ { - /* previous==NULL ==> never got to for(;;) increment step ==> - goes in front of bcdata->start for this bc_name */ - bcdata_list->start = new_entry; - bcdata_list->num++; + previous->next_entry = new_entry; + new_entry->next_entry = NULL; } - } else + } else /* case 2 or 4 */ { + if (current) /* case 2 : goes at start of list*/ + { + selections_list = new_entry; + new_entry->next_entry = current; + } else /* case 4 : starts list */ + { #ifdef DEBUG - printf("Boundary_Boundary_SelectVarForBCI: starting new selections list\n"); + printf("Boundary_SelectVarForBCI: starting new selections list\n"); #endif - - /* first entry */ - new_entry-> next_entry = NULL; - selections_list = new_entry; - - /* create bcdata list too */ - bcdata_list = (struct BCDATA *) malloc(sizeof(struct BCDATA)); - if (!bcdata_list) - { - CCTK_VWarn(1, __LINE__, __FILE__, CCTK_THORNSTRING, - "Unable to allocate memory for internal 'bcdata' list"); - retval = -5; + new_entry->next_entry = NULL; + selections_list = new_entry; } - bcdata_list->next = NULL; - 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() */ } + /* If new bcdata entry, update current_bcdata->start as promised above */ + if (current_bcdata && !current_bcdata->start) + { + current_bcdata->start = new_entry; + } + +#ifdef DEBUG + printf("\n"); +#endif return retval; } @@ -522,6 +555,10 @@ CCTK_INT Boundary_Boundary_SelectGroupForBC(CCTK_POINTER GH, retval = 0; +#ifdef DEBUG + printf("Boundary_SelectGroupForBC: called for group %s\n", group_name); +#endif + /* get group index */ gi = CCTK_GroupIndex(group_name); if (!gi) @@ -664,7 +701,7 @@ CCTK_INT Boundary_Boundary_SelectedGVs(CCTK_POINTER GH, GH = GH; #ifdef DEBUG - printf("Boundary_Boundary_SelectedGVs: called with bc_name=\"%s\" array_size=%d\n", bc_name, array_size); + printf("Boundary_SelectedGVs: called with bc_name=\"%s\" array_size=%d\n", bc_name, array_size); #endif /* Step through bcdata list to find starting point, and to compute return @@ -675,7 +712,7 @@ CCTK_INT Boundary_Boundary_SelectedGVs(CCTK_POINTER GH, { #ifdef DEBUG - printf("Boundary_Boundary_SelectedGVs: looping through bcdata list\n" + printf("Boundary_SelectedGVs: looping through bcdata list\n" " current_bcdata->bc_name=\"%s\"\n", current_bcdata->bc_name); #endif @@ -683,7 +720,7 @@ CCTK_INT Boundary_Boundary_SelectedGVs(CCTK_POINTER GH, 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_SelectedGVs: strings match, using this start value\n"); #endif /* If bc_name matches, use this entry to set current */ current = current_bcdata->start; @@ -711,7 +748,7 @@ CCTK_INT Boundary_Boundary_SelectedGVs(CCTK_POINTER GH, } #ifdef DEBUG - printf("Boundary_Boundary_SelectedGVs: initializing current to %p\n", + printf("Boundary_SelectedGVs: initializing current to %p\n", current); #endif /* If current is still NULL, bc_name did not match any entry in the @@ -733,6 +770,11 @@ CCTK_INT Boundary_Boundary_SelectedGVs(CCTK_POINTER GH, current; current = current->next_entry) { +#ifdef DEBUG + printf("Boundary_SelectedGVs: looping through selected vars, at current->var_index = %d\n",current->var_index); + printf(" current->next_entry is %p\n",current->next_entry); +#endif + if (!bc_name || CCTK_Equals(current->bc_name, bc_name)) { if (i < array_size) @@ -745,7 +787,7 @@ CCTK_INT Boundary_Boundary_SelectedGVs(CCTK_POINTER GH, { if (i==0) { - printf("Boundary_Boundary_SelectedGVs: Internal error: I am " + printf("Boundary_SelectedGVs: Internal error: I am " "past the relevant section of the selections list, yet no " "vars have been 'selected'.\n"); } @@ -801,7 +843,8 @@ void Boundary_ApplyPhysicalBCs(CCTK_ARGUMENTS) current_bcdata = current_bcdata->next) { #ifdef DEBUG - printf("Boundary_ApplyPhysicalBCs: current_bcdata %p holds: next=%p bc_name=%s start=%p num=%d\n", + printf("Boundary_ApplyPhysicalBCs: current_bcdata %p holds: next=%p " + "bc_name=%s start=%p num=%d\n", current_bcdata, current_bcdata->next, current_bcdata->bc_name, current_bcdata->start, current_bcdata->num); #endif @@ -818,8 +861,8 @@ void Boundary_ApplyPhysicalBCs(CCTK_ARGUMENTS) realloc(tables, num_vars*sizeof(int)); } else { - vars = (int *) malloc(num_vars*sizeof(int)); - tables = (int *) malloc(num_vars*sizeof(int)); + vars = (int *) malloc(num_vars*sizeof(int)); + tables = (int *) malloc(num_vars*sizeof(int)); } } @@ -857,7 +900,8 @@ void Boundary_ApplyPhysicalBCs(CCTK_ARGUMENTS) /* Apply bc to vi */ #ifdef DEBUG printf("Boundary_ApplyPhysicalBCs: Attempting to call boundary condition\n" - " Using function pointer %p with arguments cctkGH %p, num_vars %d, vars, tables\n", + " Using function pointer %p with arguments\n" + " 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); -- cgit v1.2.3