main.c 5.91 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

Cassio Kirch's avatar
Cassio Kirch committed
23
24
25
26
27
28
29
30
31
32
33
double
getAverageNeighborsNo(const struct Boid* const boid)
{
  unsigned int boidCount;
  double averageNeighborsNo = 0.0;
  for (boidCount = 0; boidCount < N; ++boidCount)
    averageNeighborsNo += (double)(boid[boidCount].endoNeighbors +   \
                                   boid[boidCount].ectoNeighbors) / N;
  return averageNeighborsNo;
}

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

#ifdef ECTO_GAMMA
Cassio Kirch's avatar
Cassio Kirch committed
51
  for (boidCount = endoBoids + 1; boidCount < N; ++boidCount)
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
    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
68
69
70
71
72
  double gamma = 0.0;
  unsigned int endoBoids = (unsigned)(ENDOPROP*N);
  unsigned int boidCount = endoBoids;

  do
73
74
75
76
77
  {
    boidCount--;
    if (boids[boidCount].gamma <= 1.01)
      gamma += boids[boidCount].gamma/endoBoids;
  }
Cassio Kirch's avatar
Cassio Kirch committed
78
  while(boidCount != 0);
79
*/
Cassio Kirch's avatar
Cassio Kirch committed
80
  
81
#ifdef DEBUG
82
83
84
for (boidCount = (unsigned)(ENDOPROP*N); boidCount < N; ++boidCount)
  if (boids[boidCount].type == ENDODERM)
    printf("Endo cell treated as a ecto one. %u\n", boidCount);
85
#endif
86

Cassio Kirch's avatar
Cassio Kirch committed
87
88
89
90
91
92
93
94
95
96
  return gamma;
}

void
one_system ()
{
  struct Boid  boid[N];
  struct Box   box[BOXES];
  unsigned int boidCount, boxID, threadCount;
  unsigned long long int step, continuousStep = 0;
Cassio Kirch's avatar
Cassio Kirch committed
97

98
  FILE* godFile = initializeSingleFile();
Cassio Kirch's avatar
Cassio Kirch committed
99
  FILE* finalConfigurationFile;
Cassio Kirch's avatar
Cassio Kirch committed
100

Cassio Kirch's avatar
Cassio Kirch committed
101
102
103
104
#ifdef PLOT_EXIT_FILES
  FILE* endoFile;
  FILE* ectoFile;
#endif
Cassio Kirch's avatar
Cassio Kirch committed
105

Cassio Kirch's avatar
Cassio Kirch committed
106
#ifdef GAMMA_FILE
Cassio Kirch's avatar
Cassio Kirch committed
107
  FILE* gammaFile = initializeGammaFile();
Cassio Kirch's avatar
Cassio Kirch committed
108
#endif
Cassio Kirch's avatar
Cassio Kirch committed
109
110
111
112
113
114
  
  /* Set the pthread_create parameters. */
  struct Parameters parametersStruct[NUM_THREADS];
  void* parameters[NUM_THREADS];

  for (threadCount=0; threadCount<NUM_THREADS; ++threadCount)
115
116
117
118
119
120
121
122
123
124
125
126
127
  {
    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;
    parametersStruct[threadCount].boid = boid;
    parametersStruct[threadCount].box  = box;
    parameters[threadCount] = &(parametersStruct[threadCount]);
  }
  
Cassio Kirch's avatar
Cassio Kirch committed
128
  for (boidCount=0; boidCount<N; boidCount++)
129
130
131
132
  {
    initializeBoid (&boid[boidCount]);
    checkLimits(&(boid[boidCount]));
  }
Cassio Kirch's avatar
Cassio Kirch committed
133
  
Cassio Kirch's avatar
Cassio Kirch committed
134
  initializeBoxes(box);
Cassio Kirch's avatar
Cassio Kirch committed
135
136
  for (boidCount=0; boidCount<N; boidCount++)
    appendBoid(&(boid[boidCount]), box);
137
  
Cassio Kirch's avatar
Cassio Kirch committed
138
  for (step=0; step<STEPS; ++step)
139
140
141
  {
    nextStep(parameters);
    for (boidCount=0; boidCount<N; boidCount++)
Cassio Kirch's avatar
Cassio Kirch committed
142
    {
143
144
145
146
147
148
149
150
151
152
153
      checkLimits(&(boid[boidCount]));
      boxID = getBoxID(boid[boidCount]);
      if (boid[boidCount].boxID != boxID)
      {
        removeBoid(&boid[boidCount], box);
        appendBoid(&boid[boidCount], box);
      }
    }
    if (step%EXIT_INTERVAL == 0)
    {
      printf("Step: %llu\n", step);
Cassio Kirch's avatar
Cassio Kirch committed
154
155
156
157
158

#ifdef COUNT_NEIGHBORS
      printf("Average neighbors: %lf\n", getAverageNeighborsNo(boid));
#endif
      
Cassio Kirch's avatar
Cassio Kirch committed
159
#ifdef PLOT_EXIT_FILES
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
      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,                        \
          boid[boidCount].type, boidCount,                              \
          boid[boidCount].position[X],                                  \
          boid[boidCount].position[Y]);
        */
        
        if (boid[boidCount].type == ENDODERM)
          fprintf(endoFile, "%u\t" "%u\t" "%lf\t" "%lf\n",            \
                  boid[boidCount].type,                               \
                  boidCount, boid[boidCount].position[X],             \
                  boid[boidCount].position[Y]);
        
        else
          fprintf(ectoFile, "%u\t" "%u\t" "%lf\t" "%lf\n",            \
                  boid[boidCount].type,                               \
                  boidCount, boid[boidCount].position[X],             \
                  boid[boidCount].position[Y]);
      }
      fclose(endoFile);
      fclose(ectoFile);
Cassio Kirch's avatar
Cassio Kirch committed
185
#endif
Cassio Kirch's avatar
Cassio Kirch committed
186
#ifdef GAMMA_FILE
187
      fprintf(gammaFile, "%llu\t%lf\n", step, getGamma(boid));
Cassio Kirch's avatar
Cassio Kirch committed
188
#endif
189
      ++continuousStep;
Cassio Kirch's avatar
Cassio Kirch committed
190
    }
191
  }
Cassio Kirch's avatar
Cassio Kirch committed
192
#ifdef GAMMA_FILE
Cassio Kirch's avatar
Cassio Kirch committed
193
  fclose (gammaFile);
Cassio Kirch's avatar
Cassio Kirch committed
194
#endif
195
196
  fclose (godFile);
  
197
198
  
  finalConfigurationFile = initializeFinalConfigurationFile();
Cassio Kirch's avatar
Cassio Kirch committed
199
  for (boidCount=0; boidCount<N; boidCount++)
200
201
202
203
204
205
206
  {
    fprintf(finalConfigurationFile,                                   \
            "%u\t" "%u\t" "%lf\t" "%lf\n",                            \
            boid[boidCount].type, boidCount,                          \
            boid[boidCount].position[X],                              \
            boid[boidCount].position[Y]);
  }
Cassio Kirch's avatar
Cassio Kirch committed
207
208
209
  fclose (finalConfigurationFile);
}

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