Skip to content

Commit

Permalink
Fix space redistribution (#368)
Browse files Browse the repository at this point in the history
- fix function `rgroup_Establish` so that input values of `space` do not need to sum to exactly 1 (based on activated resource groups), i.e., make inputs `space` and `on` in file `rgroup.in` independent from each other
--> calculate "used_space" instead of "unused_space" which missed values from de-activated resource groups and didn't adjust if the sum of space (`min_res_req`) was larger than 1

- I'm not sure what to do if `used_space = 0`: currently, the code sends a fatal signal (@kpalmqui - any suggestions?)
  • Loading branch information
dschlaep committed Jul 30, 2019
1 parent 43f180f commit fcec88f
Showing 1 changed file with 31 additions and 16 deletions.
47 changes: 31 additions & 16 deletions ST_resgroups.c
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,8 @@ void rgroup_Establish(void) {
/*------------------------------------------------------*/

IntS i, num_est; /* number of individuals of sp. that establish*/
RealF unused_space = 0; /* sums up all of the space that goes unused if a species does not establish */
RealF
used_space = 0; /* sums up all of the space that is currently used */
GrpIndex rg;
SppIndex sp;
GroupType *g;
Expand Down Expand Up @@ -669,29 +670,43 @@ void rgroup_Establish(void) {
} /* end ForEachGroupSpp() */
}

// if no species in this group established we need to redistribute min_res_req
if(g->est_count == 0){
unused_space += g->min_res_req;
// sum up min_res_req in case we need to redistribute min_res_req
if (g->est_count > 0) {
used_space += g->min_res_req;
} else {
// this group needs no resources because it is not established.
g->min_res_req = 0;
}
} /* end ForEachGroup() */

// If there is unused space we need to redistribute
if(unused_space > 0){
// printf("\nYear %d: unused_space = %f\n\n", Globals.currYear, unused_space);
ForEachGroup(rg){
g = RGroup[rg];
// If this group is established, give it some of the unused space.
if(g->est_count != 0){
// printf("%s before: %f\n", g->name, g->min_res_req);
/* This formula will proportionally redistribute the unused space to all groups that are established */
g->min_res_req = g->min_res_req / (1 - unused_space);
// printf("%s after: %f\n", g->name, g->min_res_req);
}

if (ZRO(used_space)) {
// We shouldn't have arrived here
LogError(stderr, LOGFATAL, "Invalid space values");
}

//RealF tmp = 0.;

// If there is unused (or too much used) space we need to redistribute
if (!EQ(used_space, 1.0)) {
//printf("\nYear %d: used_space = %f\n", Globals.currYear, used_space);

ForEachGroup(rg) {
g = RGroup[rg];
/* Redistribute the unused (or too much used) space to all groups that
are established */
if (g->est_count > 0) {
//printf("%s before: %f -- ", g->name, g->min_res_req);
g->min_res_req = g->min_res_req / used_space;
//printf("%s after: %f\n", g->name, g->min_res_req);
//tmp += g->min_res_req;
}
}
}

//printf("Sum of space after adjustment = %f\n", tmp);
}

/***********************************************************/
void rgroup_IncrAges(void)
{
Expand Down

0 comments on commit fcec88f

Please sign in to comment.