main.c 5.01 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
13
14
15
16
17
18
19
20
21
22

#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--;
Cassio Kirch's avatar
Cassio Kirch committed
23
24
      if (boids[boidCount].gamma <= 1.01)
        gamma += boids[boidCount].gamma/endoBoids;
25
#ifdef DEBUG
26
27
      if (boids[boidCount].type == ECTODERM)
        printf("Ecto cell treated as a endo one. %u\n", boidCount);
28
#endif
Cassio Kirch's avatar
Cassio Kirch committed
29
30
31
    }
  while(boidCount != 0);

Cassio Kirch's avatar
Cassio Kirch committed
32
  
33
34
35
36
37
38
#ifdef DEBUG
  for (boidCount = endoBoids; boidCount < N; ++boidCount)
      if (boids[boidCount].type == ENDODERM)
        printf("Endo cell treated as a ecto one. %u\n", boidCount);
#endif
  
Cassio Kirch's avatar
Cassio Kirch committed
39
40
41
42
43
44
45
46
47
48
  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
49

Cassio Kirch's avatar
Cassio Kirch committed
50
  //FILE* dat = initializeSingleFile();
Cassio Kirch's avatar
Cassio Kirch committed
51
  FILE* finalConfigurationFile;
Cassio Kirch's avatar
Cassio Kirch committed
52

Cassio Kirch's avatar
Cassio Kirch committed
53
54
55
56
#ifdef PLOT_EXIT_FILES
  FILE* endoFile;
  FILE* ectoFile;
#endif
Cassio Kirch's avatar
Cassio Kirch committed
57

Cassio Kirch's avatar
Cassio Kirch committed
58
#ifdef GAMMA_FILE
Cassio Kirch's avatar
Cassio Kirch committed
59
  FILE* gammaFile = initializeGammaFile();
Cassio Kirch's avatar
Cassio Kirch committed
60
#endif
Cassio Kirch's avatar
Cassio Kirch committed
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
  
  /* 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]));
    }
  
Cassio Kirch's avatar
Cassio Kirch committed
86
  initializeBoxes(box);
Cassio Kirch's avatar
Cassio Kirch committed
87
88
  for (boidCount=0; boidCount<N; boidCount++)
    appendBoid(&(boid[boidCount]), box);
Cassio Kirch's avatar
Cassio Kirch committed
89

Cassio Kirch's avatar
Cassio Kirch committed
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
  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)
        {
105
          printf("Step: %llu\n", step);
Cassio Kirch's avatar
Cassio Kirch committed
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
#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
Cassio Kirch's avatar
Cassio Kirch committed
132
#ifdef GAMMA_FILE
Cassio Kirch's avatar
Cassio Kirch committed
133
          fprintf(gammaFile, "%llu\t%lf\n", step, getGamma(boid));
Cassio Kirch's avatar
Cassio Kirch committed
134
#endif
Cassio Kirch's avatar
Cassio Kirch committed
135
136
137
          ++continuousStep;
        }
    }
Cassio Kirch's avatar
Cassio Kirch committed
138
#ifdef GAMMA_FILE
Cassio Kirch's avatar
Cassio Kirch committed
139
  fclose (gammaFile);
Cassio Kirch's avatar
Cassio Kirch committed
140
141
142
#endif
  //fclose (dat);

143
144
  
  finalConfigurationFile = initializeFinalConfigurationFile();
Cassio Kirch's avatar
Cassio Kirch committed
145
146
147
148
149
150
151
152
153
154
155
  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);
}

Cassio Kirch's avatar
Cassio Kirch committed
156
157
158
159
160
161
162
163
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
164
int
Cassio Kirch's avatar
Cassio Kirch committed
165
main (int argc, char** argv)
Cassio Kirch's avatar
Cassio Kirch committed
166
{
Cassio Kirch's avatar
Cassio Kirch committed
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
  if (argc > 1u)
    {
      if (strcmp(argv[1], "-p") == 0)
        printParameters();
      else
        printHelp();
    }
  else
    {
      int returned = checkParameters();
      if (returned != 0)
        return returned;
      setDate();
      srand(time(NULL));
      one_system();
    }
Cassio Kirch's avatar
Cassio Kirch committed
183
184
  return 0;
}