aboutsummaryrefslogtreecommitdiffstats
path: root/test/ccured_olden/power/build.c
blob: 881b8e60d20c952f888026698d82fb557b15b3e4 (plain)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/* For copyright information, see olden_v1.0/COPYRIGHT */

/* build.c
 *
 * By:  Martin C. Carlisle
 * 6/15/94
 * builds the tree for the Power Pricing problem
 *
 * based on code by:  Steve Lumetta, Sherry Li, and Ismail Khalil
 * University of California at Berkeley
 */

#include "power.h"

Root build_tree() 
{
  register int i;
  register Root t;
#ifdef FUTURES
  int j;
  future_cell_int fc[NUM_FEEDERS];
#else
  register Lateral l;
#endif
  
  t = (Root) ALLOC(0,sizeof(*t));
#ifdef FUTURES
  for (i=0,j=0; i<NUM_FEEDERS; i++,j+=LATERALS_PER_FEEDER) {
    FUTURE(j,LATERALS_PER_FEEDER,build_lateral,&fc[i]);
  }
  for (i=0; i<NUM_FEEDERS; i++) {
    TOUCH(&fc[i]);
    t->feeders[i]=(Lateral) fc[i].value;
  }
#else
  for (i=0; i<NUM_FEEDERS; i++) {
    /* Insert future here, split into two loops */
    l = build_lateral(i*LATERALS_PER_FEEDER,LATERALS_PER_FEEDER);
    t->feeders[i]=l;
  }
#endif
  t->theta_R = 0.8;
  t->theta_I = 0.16;
  return t;
}

Lateral build_lateral(int i, int num)
{
  register int j,k;
  register Lateral l;
  register Branch b;
#ifdef FUTURES
  future_cell_int fc;
#else
  register Lateral next;
#endif
 
  if (num == 0) return NULL;

#ifndef PLAIN
  { int x,m,q,proc;
  x = (i+num-1)*BRANCHES_PER_LATERAL+BRANCHES_PER_LATERAL-1;
  m = 1000%__NumNodes;
  q = 1000/__NumNodes;
  if (x<m*(q+1)) proc = x/(q+1);
  else proc = (x-m)/q;
  l = (Lateral) ALLOC(__NumNodes-1-proc,sizeof(*l));
  }
#else
  l = (Lateral) ALLOC(0,sizeof(*l));
#endif

#ifdef FUTURES
  FUTURE(i,num-1,build_lateral,&fc);
  b = build_branch(i*BRANCHES_PER_LATERAL,(num-1)*BRANCHES_PER_LATERAL,
    BRANCHES_PER_LATERAL);
#else
  next = build_lateral(i,num-1);
  b = build_branch(i*BRANCHES_PER_LATERAL,(num-1)*BRANCHES_PER_LATERAL,
    BRANCHES_PER_LATERAL);
#endif

#ifdef FUTURES
  TOUCH(&fc); 
  l->next_lateral = (Lateral) fc.value;
#else
  l->next_lateral = next;
#endif
  l->branch = b;
  l->R = 1/300000.0;
  l->X = 0.000001;
  l->alpha = 0.0;
  l->beta = 0.0;
  return l;
}

Branch build_branch(int i, int j, int num)
{
  register Leaf l;
  register Branch b;
#ifdef FUTURES
  future_cell_int fc;
#endif

  if (num == 0) return NULL;
  /* allocate branch */
#ifndef PLAIN
  { int x,m,q,proc;
  x = i+j+num-1;
  m = 1000%__NumNodes;
  q = 1000/__NumNodes;
  if (x<m*(q+1)) proc = x/(q+1);
  else proc = (x-m)/q;
  b = (Branch) ALLOC(__NumNodes-1-proc,sizeof(*b));
  }
#else
  b = (Branch) ALLOC(0,sizeof(*b));
#endif
  
  /* fill in children */
#ifndef FUTURES
  b->next_branch= build_branch(i,j,num-1);
#else
  FUTURE(i,j,num-1,build_branch,&fc);
#endif

  for (i=0; i<LEAVES_PER_BRANCH; i++) {
    l = build_leaf();
    b->leaves[i] = l;
  }
  
 
#ifdef FUTURES
  TOUCH(&fc);
  b->next_branch = (Branch) fc.value;
#endif
  
  /* fill in values */
  b->R = 0.0001;
  b->X = 0.00002;
  b->alpha = 0.0;
  b->beta = 0.0;
  return b;
}

Leaf build_leaf()
{
  register Leaf l;

  l = (Leaf) mymalloc(sizeof(*l));
  l->D.P = 1.0;
  l->D.Q = 1.0;
  return l;
}