Advertisement
Guest User

Untitled

a guest
Apr 24th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.14 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <getopt.h>
  4. #include <stdlib.h>
  5. #include <sys/time.h>
  6.  
  7. #define MIN_NUM_OF_NEURONS  (1L)
  8. #define DEF_NUM_OF_NEURONS  (1000L)
  9.  
  10. #define MIN_NUM_OF_NEIGHBORS    (0L)
  11. #define DEF_NUM_OF_NEIGHBORS    (300L)
  12.  
  13. #define DEF_DT          (1.0e-04)
  14. #define DEF_MU          (1.0)
  15. #define DEF_UTH         (0.98)
  16. #define DEF_S_MIN       (0.7)
  17. #define DEF_S_MAX       (0.7)
  18. #define DEF_SIM_TIME        (20L)
  19. #define DEF_TTRANSIENT      (-1L)
  20.  
  21. static struct option long_options[] =
  22. {
  23.  {"dt",        required_argument, 0, 'a'},
  24.  {"mu",        required_argument, 0, 'b'},
  25.  {"uth",       required_argument, 0, 'c'},
  26.  {"time",      required_argument, 0, 'd'},
  27.  {"transient", required_argument, 0, 'e'},
  28.  {"s_min",     required_argument, 0, 'f'},
  29.  {"s_max",     required_argument, 0, 'g'},
  30.  {"n",         required_argument, 0, 'n'},
  31.  {"r",         required_argument, 0, 'r'},
  32.  {0, 0, 0, 0}
  33. };
  34.  
  35. int main(int argc, char *argv[])
  36. {
  37.     FILE        *output1, *output2;
  38.     long        n, r;
  39.     long        i, j;
  40.     long        it;
  41.     double      divide;
  42.     double      dt;
  43.     double      tstep;
  44.     long        ntstep;
  45.     long        sim_time;
  46.     long        ttransient;
  47.     long        itime;
  48.     double      uth;
  49.     double      mu;
  50.     double      s_min;
  51.     double      s_max;
  52.     double      *u, *uplus, *sigma, *omega, *omega1;
  53.     double      sum;
  54.     double      time;
  55.     struct timeval  global_start, global_end, IO_start, IO_end;
  56.     double      global_usec, IO_usec = 0.0;
  57.     int     c, option_index;
  58.     char        *end_ptr;
  59.  
  60.     n          = DEF_NUM_OF_NEURONS;
  61.     r          = DEF_NUM_OF_NEIGHBORS;
  62.     dt         = DEF_DT;
  63.     mu         = DEF_MU;
  64.     uth        = DEF_UTH;
  65.     s_min      = DEF_S_MIN;
  66.     s_max      = DEF_S_MAX;
  67.     sim_time   = DEF_SIM_TIME;
  68.     ttransient = DEF_TTRANSIENT;
  69.  
  70.     while (1) {
  71.         c = getopt_long (argc, argv, "+n:r:", long_options, &option_index);
  72.  
  73.         if (c == -1) {
  74.             break;
  75.         }
  76.  
  77.         switch (c) {
  78.             case 'a':
  79.                 dt = strtod(optarg, &end_ptr);
  80.                 if (*end_ptr != '\0') {
  81.                     printf("Option \"%s\": Invalid argument \"%s\".\n", long_options[option_index].name, optarg);
  82.                     exit(1);
  83.                 }
  84.                 if (dt <= 0.0) {
  85.                     printf("Option \"%s\": \"dt\" must be larger than zero.\n", long_options[option_index].name);
  86.                     exit(1);
  87.                 }
  88.                 break;
  89.             case 'b':
  90.                 mu = strtod(optarg, &end_ptr);
  91.                 if (*end_ptr != '\0') {
  92.                     printf("Option \"%s\": Invalid argument \"%s\".\n", long_options[option_index].name, optarg);
  93.                     exit(1);
  94.                 }
  95.                 if (mu <= 0.0) {
  96.                     printf("Option \"%s\": \"mu\" must be larger than zero.\n", long_options[option_index].name);
  97.                     exit(1);
  98.                 }
  99.                 break;
  100.             case 'c':
  101.                 uth = strtod(optarg, &end_ptr);
  102.                 if (*end_ptr != '\0') {
  103.                     printf("Option \"%s\": Invalid argument \"%s\".\n", long_options[option_index].name, optarg);
  104.                     exit(1);
  105.                 }
  106.                 if (uth <= 0.0) {
  107.                     printf("Option \"%s\": \"uth\" must be larger than zero.\n", long_options[option_index].name);
  108.                     exit(1);
  109.                 }
  110.                 break;
  111.             case 'd':
  112.                 sim_time = strtol(optarg, &end_ptr, 10);
  113.                 if (*end_ptr != '\0') {
  114.                     printf("Option \"%s\": Invalid argument \"%s\".\n", long_options[option_index].name, optarg);
  115.                     exit(1);
  116.                 }
  117.                 if (sim_time < 1) {
  118.                     printf("Option \"%s\": Total simulation time must be larger than zero.\n", long_options[option_index].name);
  119.                     exit(1);
  120.                 }
  121.                 break;
  122.             case 'e':
  123.                 ttransient = strtol(optarg, &end_ptr, 10);
  124.                 if (*end_ptr != '\0') {
  125.                     printf("Option \"%s\": Invalid argument \"%s\".\n", long_options[option_index].name, optarg);
  126.                     exit(1);
  127.                 }
  128.                 if (ttransient < 0) {
  129.                     printf("Option \"%s\": \"ttransient\" must be larger or equal than zero.\n", long_options[option_index].name);
  130.                     exit(1);
  131.                 }
  132.                 break;
  133.             case 'f':
  134.                 s_min = strtod(optarg, &end_ptr);
  135.                 if (*end_ptr != '\0') {
  136.                     printf("Option \"%s\": Invalid argument \"%s\".\n", long_options[option_index].name, optarg);
  137.                     exit(1);
  138.                 }
  139.                 if (s_min <= 0.0) {
  140.                     printf("Option \"%s\": \"s_min\" must be larger than zero.\n", long_options[option_index].name);
  141.                     exit(1);
  142.                 }
  143.                 break;
  144.             case 'g':
  145.                 s_max = strtod(optarg, &end_ptr);
  146.                 if (*end_ptr != '\0') {
  147.                     printf("Option \"%s\": Invalid argument \"%s\".\n", long_options[option_index].name, optarg);
  148.                     exit(1);
  149.                 }
  150.                 if (s_max <= 0.0) {
  151.                     printf("Option \"%s\": \"s_max\" must be larger than zero.\n", long_options[option_index].name);
  152.                     exit(1);
  153.                 }
  154.                 break;
  155.             case 'n':
  156.                 n = strtol(optarg, &end_ptr, 10);
  157.                 if (*end_ptr != '\0') {
  158.                     printf("Option \"%s\": Invalid argument \"%s\".\n", long_options[option_index].name, optarg);
  159.                     exit(1);
  160.                 }
  161.                 if (n < MIN_NUM_OF_NEURONS) {
  162.                     printf("Option \"%s\": Number of neurons must be at least %ld.\n", long_options[option_index].name, MIN_NUM_OF_NEURONS);
  163.                     exit(1);
  164.                 }
  165.                 break;
  166.             case 'r':
  167.                 r = strtol(optarg, &end_ptr, 10);
  168.                 if (*end_ptr != '\0') {
  169.                     printf("Option \"%s\": Invalid argument \"%s\".\n", long_options[option_index].name, optarg);
  170.                     exit(1);
  171.                 }
  172.                 if (r < MIN_NUM_OF_NEIGHBORS) {
  173.                     printf("Option \"%s\": Number of neighbors must be at least %ld.\n", long_options[option_index].name, MIN_NUM_OF_NEIGHBORS);
  174.                     exit(1);
  175.                 }
  176.                 break;
  177.             case '?':
  178.             default:
  179.                 exit(1);
  180.                 break;
  181.         }
  182.     }
  183.  
  184.     if (optind != argc) {
  185.         printf("Unknown option \"%s\".\n", argv[optind]);
  186.         exit(1);
  187.     }
  188.  
  189.     if (2 * r + 1 > n) {
  190.         printf("Total number of neighbors and reference neuron (2 * %ld + 1 = %ld) cannot exceed number of neurons (%ld).\n", r, 2 * r + 1, n);
  191.         exit(1);
  192.     }
  193.  
  194.     if (s_min > s_max) {
  195.         printf("s_min (%17.15f) must be smaller or equal than s_max (%17.15f).\n", s_min, s_max);
  196.         exit(1);
  197.     }
  198.  
  199.     divide = (double)(2 * r);
  200.     tstep = 1.0 / dt;
  201.     ntstep = (long)tstep;
  202.     if (ttransient == DEF_TTRANSIENT) {
  203.         ttransient = (sim_time * ntstep) / 2;
  204.     } else {
  205.         ttransient *= ntstep;
  206.     }
  207.     itime = sim_time * ntstep;
  208.  
  209.     printf("Running simulation with following parameters:\n");
  210.     printf("  Number of neurons   : %ld\n", n);
  211.     printf("  Numger of neighbours: %ld\n", r);
  212.     printf("  Simulation time     : %ld seconds (%ld time steps)\n", sim_time, itime);
  213.     printf("  Transient time      : %ld seconds (%ld time steps)\n", ttransient / ntstep, ttransient);
  214.     printf("  dt                  : %.1e seconds \n", dt);
  215.     printf("  mu                  : %17.15f\n", mu);
  216.     printf("  uth                 : %17.15f\n", uth);
  217.     printf("  s_min               : %17.15f\n", s_min);
  218.     printf("  s_max               : %17.15f\n", s_max);
  219.  
  220.     output1 = fopen("spacetime.out", "w");
  221.     if (output1 == NULL) {
  222.         printf("Could not open file \"spacetime.out\"");
  223.         exit(1);
  224.     }
  225.  
  226.     output2 = fopen("omega.out", "w");
  227.     if (output2 == NULL) {
  228.         printf("Could not open file \"omega.out\"");
  229.         exit(1);
  230.     }
  231.  
  232.     u = (double *)calloc(n, sizeof(double));
  233.     if (u == NULL) {
  234.         printf("Could not allocate memory for \"u\".\n");
  235.         exit(1);
  236.     }
  237.  
  238.     uplus = (double *)calloc(n, sizeof(double));
  239.     if (uplus == NULL) {
  240.         printf("Could not allocate memory for \"uplus\".\n");
  241.         exit(1);
  242.     }
  243.  
  244.     sigma = (double *)calloc(n * n, sizeof(double));
  245.     if (sigma == NULL) {
  246.         printf("Could not allocate memory for \"sigma\".\n");
  247.         exit(1);
  248.     }
  249.  
  250.     omega = (double *)calloc(n, sizeof(double));
  251.     if (omega == NULL) {
  252.         printf("Could not allocate memory for \"omega\".\n");
  253.         exit(1);
  254.     }
  255.  
  256.     omega1 = (double *)calloc(n, sizeof(double));
  257.     if (omega1 == NULL) {
  258.         printf("Could not allocate memory for \"omega1\".\n");
  259.         exit(1);
  260.     }
  261.  
  262.     /*
  263.      * Initialize elements of array u[i] with random numbers.
  264.      */
  265.     for (i = 0; i < n; i++ ) {
  266.         u[i] = drand48();
  267.         printf("%ld\t%f\n", i, u[i]);
  268.     }
  269.  
  270.     /*
  271.      * Read connectivity matrix sigma[n][n] from file or
  272.      * construct connectivity matrix.
  273.      */
  274.     for (i = 0; i < r; i++) {
  275.         for (j = 0; j < i + r + 1; j++) {
  276.             sigma[i * n + j] = s_min + (s_max - s_min) * drand48();
  277.         }
  278.         for (j = n - r + i; j < n; j++) {
  279.             sigma[i * n + j] = s_min + (s_max - s_min) * drand48();
  280.         }
  281.     }
  282.  
  283.     for (i = r; i < n - r; i++) {
  284.         for (j = 0; j < 2 * r + 1; j++) {
  285.             sigma[i * n + j + i - r]  = s_min + (s_max - s_min) * drand48();
  286.         }
  287.     }
  288.  
  289.     for (i = n - r; i < n; i++) {
  290.         for (j = 0; j < i - n + r + 1; j++) {
  291.             sigma[i * n + j] = s_min + (s_max - s_min) * drand48();
  292.         }
  293.         for (j = i - r; j < n; j++) {
  294.             sigma[i * n + j] = s_min + (s_max - s_min) * drand48();
  295.         }
  296.     }
  297. #if 0
  298.     for (i = 0; i < n; i++) {
  299.         for (j = 0; j < n; j++) {
  300.             printf("%4.1f", sigma[i * n + j]);
  301.         }
  302.         printf("\n");
  303.     }
  304. #endif
  305.  
  306.     /*
  307.      * Temporal iteration.
  308.      */
  309.     gettimeofday(&global_start, NULL);
  310.     for (it = 0; it < itime; it++) {
  311.         /*
  312.          * Iteration over elements.
  313.          */
  314.         for (i = 0; i < n; i++) {
  315.             uplus[i] = u[i] + dt * (mu - u[i]);
  316.             sum = 0.0;
  317.             /*
  318.              * Iteration over neighbouring neurons.
  319.              */
  320.             for (j = 0; j < n; j++) {
  321.                 sum += sigma[i * n + j] * (u[j] - u[i]);
  322.             }
  323.             uplus[i] += dt * sum / divide;
  324.         }
  325.  
  326.         /*
  327.          * Update network elements and set u[i] = 0 if u[i] > uth
  328.          */
  329.         for (i = 0; i < n; i++) {
  330.             u[i] = uplus[i];
  331.             if (u[i] > uth) {
  332.                 u[i] = 0.0;
  333.                 /*
  334.                  * Calculate omega's.
  335.                  */
  336.                 if (it >= ttransient) {
  337.                     omega1[i] += 1.0;
  338.                 }
  339.             }
  340.         }
  341.  
  342.         /*
  343.          * Print out of results.
  344.          */
  345. #if !defined(ALL_RESULTS)
  346.         if (it % ntstep == 0) {
  347. #endif
  348.             printf("Time is %ld\n", it);
  349.  
  350.             gettimeofday(&IO_start, NULL);
  351.             fprintf(output1, "%ld\t", it);
  352.             for (i = 0; i < n; i++) {
  353.                 fprintf(output1, "%19.15f", u[i]);
  354.             }
  355.             fprintf(output1, "\n");
  356.  
  357.             time = (double)it * dt;
  358.             fprintf(output2, "%ld\t", it);
  359.             for (i = 0; i < n; i++) {
  360.                 omega[i] = 2.0 * M_PI * omega1[i] / (time - ttransient * dt);
  361.                 fprintf(output2, "%19.15f", omega[i]);
  362.             }
  363.             fprintf(output2, "\n");
  364.             gettimeofday(&IO_end, NULL);
  365.             IO_usec += ((IO_end.tv_sec - IO_start.tv_sec) * 1000000.0 + (IO_end.tv_usec - IO_start.tv_usec));
  366. #if !defined(ALL_RESULTS)
  367.         }
  368. #endif
  369.     }
  370.     gettimeofday(&global_end, NULL);
  371.     global_usec = ((global_end.tv_sec - global_start.tv_sec) * 1000000.0 + (global_end.tv_usec - global_start.tv_usec));
  372.  
  373.     printf("Time for calculations = %13.6f sec\n", (global_usec - IO_usec) / 1000000.0);
  374.     printf("Time for I/O          = %13.6f sec\n", IO_usec / 1000000.0);
  375.     printf("Total execution time  = %13.6f sec\n", global_usec / 1000000.0);
  376.  
  377.     fclose(output1);
  378.     fclose(output2);
  379.  
  380.     return 0;
  381. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement