main.c 4.31 KB
Newer Older
Cassio Kirch's avatar
Cassio Kirch committed
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
#include <stdio.h>
#include <stdlib.h>

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

double
getGamma (const struct Boid* const boids)
{
  double gamma = 0.0;
  unsigned int endoBoids = (unsigned)(ENDOPROP*N);
  unsigned int boidCount = endoBoids;

  do
    {
      boidCount--;
      gamma += boids[boidCount].gamma/endoBoids;
    }
  while(boidCount != 0);

  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;
  FILE* dat = initializeSingleFile();
  FILE* finalConfigurationFile;
  char finalConfigurationFileName[FILENAME_SIZE];
#ifdef PLOT_EXIT_FILES
  FILE* endoFile;
  FILE* ectoFile;
#endif
  FILE* gammaFile = initializeGammaFile();
  
  /* Set the pthread_create parameters. */
  struct Parameters parametersStruct[NUM_THREADS];
  void* parameters[NUM_THREADS];

  for (threadCount=0; threadCount<NUM_THREADS; ++threadCount)
    {
      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]);
    }

  for (boidCount=0; boidCount<N; boidCount++)
    {
      initializeBoid (&boid[boidCount]);
      checkLimits(&(boid[boidCount]));
    }
  initializeBoxes(box);
  
  for (boidCount=0; boidCount<N; boidCount++)
    appendBoid(&(boid[boidCount]), box);
  for (step=0; step<STEPS; ++step)
    {
      nextStep(parameters);
      for (boidCount=0; boidCount<N; boidCount++)
        {
          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)
        {
#ifdef PLOT_EXIT_FILES
          endoFile = initializeStepAndTypeFile (continuousStep, ENDODERM);
          ectoFile = initializeStepAndTypeFile (continuousStep, ECTODERM);
          for (boidCount=0; boidCount<N; boidCount++)
            {
              /*fprintf(dat, "%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);
#endif
          fprintf(gammaFile, "%llu\t%lf\n", step, getGamma(boid));
          ++continuousStep;
        }
    }
  fclose (gammaFile);
  fclose (dat);
  sprintf(finalConfigurationFileName, "final_%s.dat", getDate());
  finalConfigurationFile = fopen(finalConfigurationFileName, "w");
  for (boidCount=0; boidCount<N; boidCount++)
    {
      fprintf(finalConfigurationFile,                                 \
              "%u\t" "%u\t" "%lf\t" "%lf\n",                          \
              boid[boidCount].type, boidCount,                        \
              boid[boidCount].position[X],                            \
              boid[boidCount].position[Y]);
    }
  fclose (finalConfigurationFile);
}

int
main ()
{
  int returned = checkParameters();
  if (returned != 0)
    return returned;
  setDate();
  srand(time(NULL));
  one_system();
  return 0;
}