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

92
#ifdef GAMMA_FILE
Cassio Kirch's avatar
Cassio Kirch committed
93
  FILE* gammaFile = initializeGammaFile();
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
      boxID = getBoxID(boids[boidCount]);
      if (boids[boidCount].boxID != boxID)
135
      {
Cassio Kirch's avatar
Cassio Kirch committed
136 137
        removeBoid(&boids[boidCount], boxes);
        appendBoid(&boids[boidCount], boxes);
138 139 140 141 142
      }
    }
    if (step%EXIT_INTERVAL == 0)
    {
      printf("Step: %llu\n", step);
Cassio Kirch's avatar
Cassio Kirch committed
143 144

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

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

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