Advertisement
Guest User

Untitled

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