sandbox/bugs/evolving_scalar_list_bug.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/**
'Bug' which causes scrambling of scalar list 'evolving'
This was a mistake on my behalf not initialising evolving correctly so that it was only a local variable in defaults.  The commented out line 15 was the incorrect initialisation.  When correctly initialised using list_copy (which uses malloc() to globally allocate memory) this works properly.
*/
#define MAXLEVEL 2

scalar zb[], h[], eta[];
vector u[];

#include "predictor-corrector.h"
scalar * evolving = NULL;
int Nlist=3;

event defaults (i = 0){
  //evolving =(scalar *) {h, u};
  evolving = list_copy({h, u});
  foreach()
    for (scalar s in evolving)
      s[]=0.;
  boundary(evolving);
  for (int ii=0; ii<Nlist; ii++) {
    printf (" evolving[ %d ]: %s ", ii, _attribute[evolving[ii].i].name); 
  }
  printf ("\n");
  //  free(evolving);
}

event init (i = 0){
  for (int ii=0; ii<Nlist; ii++) {
    foreach()
        h[]=0.;
    printf (" evolving[ %d ]: %s ", ii, _attribute[evolving[ii].i].name); 
  }
  printf ("\n");
  //  free(evolving);
}

int main()
{
  N = 1 << MAXLEVEL;

  run();
}