main.c 5.89 KB
Newer Older
Cassio Kirch's avatar
Cassio Kirch committed
1
2
#include <stdio.h>
#include <stdlib.h>
Cassio Kirch's avatar
Cassio Kirch committed
3
#include <string.h>
Cassio Kirch's avatar
Cassio Kirch committed
4
5
6
7
8
9
10
11
12

#include "define.h"
#include "comp_errors.h"
#include "files.h"
#include "boid.h"
#include "box.h"
#include "nextstep.h"
#include "main.h"

13
14
15
16
17
18
19
20
21
22
#ifdef ENDO_GAMMA
  #define GAMMA_FILE
#endif

#ifdef ECTO_GAMMA
  #ifndef GAMMA_FILE
    #define GAMMA_FILE
  #endif
#endif

23
#ifdef COUNT_NEIGHBORS
Cassio Kirch's avatar
Cassio Kirch committed
24
25
double
getAverageNeighborsNo(const struct Boid* const boid)
Cassio Kirch's avatar
Cassio Kirch committed
26
/* Where 'boid' is address of the first element of an array. */
Cassio Kirch's avatar
Cassio Kirch committed
27
28
29
30
{
  unsigned int boidCount;
  double averageNeighborsNo = 0.0;
  for (boidCount = 0; boidCount < N; ++boidCount)
Cassio Kirch's avatar
Cassio Kirch committed
31
32
33
    averageNeighborsNo += ((double)boid[boidCount].endoNeighbors +  \
                           (double)boid[boidCount].ectoNeighbors);
  averageNeighborsNo /= N;
Cassio Kirch's avatar
Cassio Kirch committed
34
35
  return averageNeighborsNo;
}
36
#endif
Cassio Kirch's avatar
Cassio Kirch committed
37

38
#if defined(ENDO_GAMMA) || defined(ECTO_GAMMA)
Cassio Kirch's avatar
Cassio Kirch committed
39
40
41
double
getGamma (const struct Boid* const boids)
{
42
43
44
  double gamma = 0.0;
  unsigned int boidCount;
  unsigned int endoBoids = (unsigned)(ENDOPROP*N);
Cassio Kirch's avatar
Cassio Kirch committed
45
  //  printf("EndoBoids: %u\n", endoBoids);
46
47
48
49
50
51
  
#ifdef ENDO_GAMMA
  for (boidCount = endoBoids; boidCount < N; --boidCount)
  /* Considering overflow. */
    gamma += boids[boidCount].gamma;
#endif
Cassio Kirch's avatar
Cassio Kirch committed
52
53
  //  printf("BoidCount: %u\n", boidCount);
  
54
55

#ifdef ECTO_GAMMA
Cassio Kirch's avatar
Cassio Kirch committed
56
  for (boidCount = endoBoids + 1; boidCount < N; ++boidCount)
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
    gamma += boids[boidCount].gamma;
#endif

#ifdef ENDO_GAMMA
  #ifdef ECTO_GAMMA
  gamma /= N;
  #else
  gamma /= endoBoids;
  #endif
#else
  #ifdef ECTO_GAMMA
  gamma /= (N-endoBoids);
  #endif
#endif
  
Cassio Kirch's avatar
Cassio Kirch committed
72
73
  return gamma;
}
74
#endif /* #if defined(ENDO_GAMMA) || defined(ECTO_GAMMA) */
Cassio Kirch's avatar
Cassio Kirch committed
75
76
77
78

void
one_system ()
{
Cassio Kirch's avatar
Cassio Kirch committed
79
80
  struct Boid  boids[N];
  struct Box   boxes[BOXES];
Cassio Kirch's avatar
Cassio Kirch committed
81
82
  unsigned int boidCount, boxID, threadCount;
  unsigned long long int step, continuousStep = 0;
Cassio Kirch's avatar
Cassio Kirch committed
83

84
  FILE* godFile = initializeGodFile();
Cassio Kirch's avatar
Cassio Kirch committed
85
  FILE* finalConfigurationFile;
Cassio Kirch's avatar
Cassio Kirch committed
86

Cassio Kirch's avatar
Cassio Kirch committed
87
88
89
90
#ifdef PLOT_EXIT_FILES
  FILE* endoFile;
  FILE* ectoFile;
#endif
Cassio Kirch's avatar
Cassio Kirch committed
91

Cassio Kirch's avatar
Cassio Kirch committed
92
#ifdef GAMMA_FILE
Cassio Kirch's avatar
Cassio Kirch committed
93
  FILE* gammaFile = initializeGammaFile();
Cassio Kirch's avatar
Cassio Kirch committed
94
#endif
95
96
97
98

#ifdef COUNT_NEIGHBORS
  FILE* averageNeighborsFile = initializeAverageNeighborsFile();
#endif
Cassio Kirch's avatar
Cassio Kirch committed
99
100
101
102
103
104
  
  /* Set the pthread_create parameters. */
  struct Parameters parametersStruct[NUM_THREADS];
  void* parameters[NUM_THREADS];

  for (threadCount=0; threadCount<NUM_THREADS; ++threadCount)
105
106
107
108
109
110
111
112
  {
    parametersStruct[threadCount].threadID = threadCount;
    parametersStruct[threadCount].left  = threadCount * \
      ((int)N/(int)NUM_THREADS);
    parametersStruct[threadCount].right = (threadCount+1) *     \
      ((int)N/(int)NUM_THREADS);
    if (threadCount == NUM_THREADS-1)
      parametersStruct[threadCount].right += N%NUM_THREADS;
Cassio Kirch's avatar
Cassio Kirch committed
113
114
    parametersStruct[threadCount].boids = boids;
    parametersStruct[threadCount].boxes  = boxes;
115
116
117
    parameters[threadCount] = &(parametersStruct[threadCount]);
  }
  
Cassio Kirch's avatar
Cassio Kirch committed
118
  for (boidCount=0; boidCount<N; boidCount++)
119
  {
Cassio Kirch's avatar
Cassio Kirch committed
120
121
    initializeBoid (&boids[boidCount]);
    checkLimits(&(boids[boidCount]));
122
  }
Cassio Kirch's avatar
Cassio Kirch committed
123
  
Cassio Kirch's avatar
Cassio Kirch committed
124
  initializeBoxes(boxes);
Cassio Kirch's avatar
Cassio Kirch committed
125
  for (boidCount=0; boidCount<N; boidCount++)
Cassio Kirch's avatar
Cassio Kirch committed
126
    appendBoid(&(boids[boidCount]), boxes);
127
  
Cassio Kirch's avatar
Cassio Kirch committed
128
  for (step=0; step <= STEPS; ++step)
129
130
131
  {
    nextStep(parameters);
    for (boidCount=0; boidCount<N; boidCount++)
Cassio Kirch's avatar
Cassio Kirch committed
132
    {
Cassio Kirch's avatar
Cassio Kirch committed
133
134
135
      checkLimits(&(boids[boidCount]));
      boxID = getBoxID(boids[boidCount]);
      if (boids[boidCount].boxID != boxID)
136
      {
Cassio Kirch's avatar
Cassio Kirch committed
137
138
        removeBoid(&boids[boidCount], boxes);
        appendBoid(&boids[boidCount], boxes);
139
140
141
142
143
      }
    }
    if (step%EXIT_INTERVAL == 0)
    {
      printf("Step: %llu\n", step);
Cassio Kirch's avatar
Cassio Kirch committed
144
145

#ifdef COUNT_NEIGHBORS
Cassio Kirch's avatar
Cassio Kirch committed
146
      fprintf(averageNeighborsFile, "%llu\t%lf\n", step, getAverageNeighborsNo(boids));
Cassio Kirch's avatar
Cassio Kirch committed
147
148
#endif
      
Cassio Kirch's avatar
Cassio Kirch committed
149
#ifdef PLOT_EXIT_FILES
150
151
152
153
154
155
      endoFile = initializeStepAndTypeFile (continuousStep, ENDODERM);
      ectoFile = initializeStepAndTypeFile (continuousStep, ECTODERM);
      for (boidCount=0; boidCount<N; boidCount++)
      {
        /*fprintf(godFile, "%llu\t" "%llu\t" "%u\t" "%u\t"              \
          "%lf\t" "%lf\n", continuousStep, step,                        \
Cassio Kirch's avatar
Cassio Kirch committed
156
157
158
          boids[boidCount].type, boidCount,                              \
          boids[boidCount].position[X],                                  \
          boids[boidCount].position[Y]);
159
160
        */
        
Cassio Kirch's avatar
Cassio Kirch committed
161
        if (boids[boidCount].type == ENDODERM)
162
          fprintf(endoFile, "%u\t" "%u\t" "%lf\t" "%lf\n",            \
Cassio Kirch's avatar
Cassio Kirch committed
163
164
165
                  boids[boidCount].type,                               \
                  boidCount, boids[boidCount].position[X],             \
                  boids[boidCount].position[Y]);
166
167
168
        
        else
          fprintf(ectoFile, "%u\t" "%u\t" "%lf\t" "%lf\n",            \
Cassio Kirch's avatar
Cassio Kirch committed
169
170
171
                  boids[boidCount].type,                               \
                  boidCount, boids[boidCount].position[X],             \
                  boids[boidCount].position[Y]);
172
173
174
      }
      fclose(endoFile);
      fclose(ectoFile);
Cassio Kirch's avatar
Cassio Kirch committed
175
#endif
Cassio Kirch's avatar
Cassio Kirch committed
176
#ifdef GAMMA_FILE
Cassio Kirch's avatar
Cassio Kirch committed
177
      fprintf(gammaFile, "%llu\t%lf\n", step, getGamma(boids));
Cassio Kirch's avatar
Cassio Kirch committed
178
#endif
179
      ++continuousStep;
Cassio Kirch's avatar
Cassio Kirch committed
180
    }
181
  }
Cassio Kirch's avatar
Cassio Kirch committed
182
#ifdef GAMMA_FILE
Cassio Kirch's avatar
Cassio Kirch committed
183
  fclose (gammaFile);
Cassio Kirch's avatar
Cassio Kirch committed
184
#endif
185
186
187
188

#ifdef COUNT_NEIGHBORS
  fclose(averageNeighborsFile);
#endif
189
  
190
191
  fclose (godFile);
    
192
  finalConfigurationFile = initializeFinalConfigurationFile();
Cassio Kirch's avatar
Cassio Kirch committed
193
  for (boidCount=0; boidCount<N; boidCount++)
194
195
196
  {
    fprintf(finalConfigurationFile,                                   \
            "%u\t" "%u\t" "%lf\t" "%lf\n",                            \
Cassio Kirch's avatar
Cassio Kirch committed
197
198
199
            boids[boidCount].type, boidCount,                          \
            boids[boidCount].position[X],                              \
            boids[boidCount].position[Y]);
200
  }
Cassio Kirch's avatar
Cassio Kirch committed
201
202
203
  fclose (finalConfigurationFile);
}

Cassio Kirch's avatar
Cassio Kirch committed
204
205
206
207
208
209
210
211
void
printHelp()
{
  printf("-h\t" "Show this help menu.\n");
  printf("-p\t" "Show the parameters compiled.\n");
  
}

Cassio Kirch's avatar
Cassio Kirch committed
212
int
Cassio Kirch's avatar
Cassio Kirch committed
213
main (int argc, char** argv)
Cassio Kirch's avatar
Cassio Kirch committed
214
{
215
  setDate();
216
  if (argc > 1)
217
218
219
220
221
222
  {
    if (strcmp(argv[1], "-p") == 0)
      printParameters();
    else
      printHelp();
  }
Cassio Kirch's avatar
Cassio Kirch committed
223
  else
224
225
226
227
228
229
230
  {
    int returned = checkParameters();
    if (returned != 0)
      return returned;
    srand(time(NULL));
    one_system();
  }
Cassio Kirch's avatar
Cassio Kirch committed
231
232
  return 0;
}