Advertisement
shinemic

last_chunk.sh

Jan 16th, 2024
702
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.90 KB | None | 0 0
  1. #!/usr/bin/bash
  2.  
  3. N=10000
  4.  
  5. echo "raw file:"
  6. ls -lh OUTCAR; wc -l OUTCAR
  7. echo
  8.  
  9. > NEW_OUTCAR
  10.  
  11. for _ in $(seq $N); do
  12.     cat OUTCAR >> NEW_OUTCAR
  13. done
  14.  
  15. cat >> NEW_OUTCAR <<'EOF'
  16.  
  17.  magnetization (z)
  18.  
  19. # of ion       s       p       d       f      tot
  20. --------------------------------------------------
  21.     1        0.037   0.030   4.519   5.000   9.586
  22.     2       -0.037  -0.030  -4.519  -5.000  -9.586
  23.     3        0.000   0.000   0.000   0.000   0.000
  24.     4        0.000   0.000   0.000   0.000   0.000
  25.     5        0.000   0.000  -0.000  -0.000   0.000
  26.     6        0.000   0.000  -0.000  -0.000   0.000
  27.     7        0.000   0.000  -0.000  -0.000   0.000
  28.     8        0.000   0.000  -0.000  -0.000   0.000
  29.     9        0.000   0.000  -0.000  -0.000   0.000
  30.    10        0.001   0.001  -0.000  -0.000   0.002
  31. ------------------------------------------------------
  32. tot          0.001   0.002   0.000   0.000   0.003
  33. EOF
  34.  
  35. echo "huge file:"
  36. ls -lh NEW_OUTCAR; wc -l NEW_OUTCAR
  37. echo
  38.  
  39. echo -n ">>> awk with getline ... "
  40. /usr/bin/time -f %es awk '
  41.    /magnetization \(z\)/ {
  42.        data=""; getline; getline; getline;
  43.        for (i=1; i<=10; i++) {
  44.            getline
  45.            data = data sprintf("%8.3f\n", $NF)
  46.        }
  47.    }
  48.    END {printf data}
  49. ' NEW_OUTCAR > res.awk_with_getline
  50. echo
  51.  
  52. echo -n ">>> awk without getline ... "
  53. /usr/bin/time -f %es awk '
  54.    {
  55.        if ($0 ~ /magnetization \(z\)/) { f = 1; c = "" }
  56.        if (f == 1) {
  57.            if ($1 ~ /^[0-9]+$/) c = c sprintf("%8.3f\n", $NF)
  58.            if ($1 ~ /tot/) f = 0
  59.        }
  60.    }
  61.    END { printf c }
  62. ' NEW_OUTCAR > res.awk_no_getline
  63. echo
  64.  
  65. echo -n ">>> perl ... "
  66. /usr/bin/time -f %es perl -00ne '
  67.    /magnetization \(z\)/ and $f=1 and next;
  68.    $f==1 and ($c,$f)=$_,0;
  69.    }{ map {/^\s*\d+\b/ and printf "%8.3f\n", (split " ")[-1]}
  70.           split /\n/, $c
  71. ' NEW_OUTCAR > res.perl
  72. echo
  73.  
  74. echo -n ">>> python ... "
  75. /usr/bin/time -f %es python -c '
  76. import sys
  77. f, c = 0, []
  78. with open(sys.argv[1]) as input_file:
  79.    for line in input_file:
  80.        if "magnetization (z)" in line:
  81.            f = 1
  82.            c = []
  83.        elif f == 1 and line.strip():
  84.            first_field = line.strip().split()[0]
  85.            if first_field.isdigit():
  86.                last_field = line.strip().split()[-1]
  87.                c.append(format(float(last_field), "8.3f"))
  88.            elif first_field == "tot":
  89.                f = 0
  90. print("\n".join(c))
  91. ' NEW_OUTCAR > res.python
  92. echo
  93.  
  94. echo -n ">>> C ... "
  95. cat > manip.c <<'EOF'
  96. #include <stdio.h>
  97. #include <stdlib.h>
  98. #include <string.h>
  99.  
  100. int main(int argc, char *argv[]) {
  101.     int f = 0;
  102.     char c[1001] = {0};
  103.  
  104.     if (argc < 2) {
  105.         printf("Please provide a filename as a command-line argument.\n");
  106.         return 1;
  107.     }
  108.  
  109.     FILE *file = fopen(argv[1], "r");
  110.     if (file == NULL) {
  111.         printf("Failed to open the file.\n");
  112.         return 1;
  113.     }
  114.  
  115.     char line[1001];
  116.     while (fgets(line, sizeof(line), file) != NULL) {
  117.         if (strstr(line, "magnetization (z)") != NULL) {
  118.             f = 1;
  119.             c[0] = '\0';
  120.         } else if (strncmp(line, "tot", 3) == 0) {
  121.             f = 0;
  122.         } else if (f == 1) {
  123.             char *token;
  124.             char *lastToken = NULL;
  125.             token = strtok(line, " ");
  126.             while (token != NULL) {
  127.                 lastToken = token;
  128.                 token = strtok(NULL, " ");
  129.             }
  130.             char *endptr;
  131.             double value = strtod(lastToken, &endptr);
  132.             if (endptr != lastToken) {
  133.                 char temp[10];
  134.                 snprintf(temp, sizeof(temp), "%8.3f\n", value);
  135.                 strcat(c, temp);
  136.             }
  137.         }
  138.     }
  139.  
  140.     fclose(file);
  141.  
  142.     printf("%s", c);
  143.  
  144.     return 0;
  145. }
  146. EOF
  147. gcc -O2 -o manip manip.c
  148. /usr/bin/time -f %es ./manip NEW_OUTCAR > res.c
  149. echo
  150.  
  151. echo "answer check:"
  152. sha256sum res.*
  153.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement