├── commiter.yml ├── README.md ├── chain_main.c ├── all ├── mixed │ ├── chain_main.c │ ├── chain_states.c │ ├── main.c │ └── chain_hamiltonian.c └── pure │ ├── particles_generate_states.c │ ├── main.c │ └── main_hamiltonian.c ├── .gitignore ├── chain_states.c ├── main.c └── chain_hamiltonian.c /commiter.yml: -------------------------------------------------------------------------------- 1 | convention: symphony 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Calculating Random and Linear entanglement entropy in disordered interacting spin half chain 2 | 3 | This program will calculate the following entropy 4 | 5 | 1. Random Entanglement Entropy (or [von Neumann entropy](https://en.wikipedia.org/wiki/Von_Neumann_entropy)) 6 | 2. [Linear Entanglement Entropy](https://en.wikipedia.org/wiki/Purity_(quantum_mechanics)#Linear_entropy) 7 | 8 | [GNU Scientific Library](https://www.gnu.org/software/gsl/) is used for handling matrix manipulations. 9 | -------------------------------------------------------------------------------- /chain_main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: Aakash Gajjar 3 | * @Date: 2017-02-15 13:33:38 4 | * @Last Modified by: Aakash Gajjar 5 | * @Last Modified time: 2017-02-15 14:10:30 6 | * @Subject: 7 | */ 8 | 9 | /* Program to print all permutations of a string in sorted order.*/ 10 | 11 | #include 12 | #include "chain_states.c" 13 | #include "chain_hamiltonian.c" 14 | 15 | int main(int argc, const char *argv[]) { 16 | int make_binary_file = 0; 17 | if (argc > 0) { 18 | make_binary_file = atoi(argv[1]); 19 | } 20 | 21 | basis(); 22 | hamiltonian(make_binary_file); 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /all/mixed/chain_main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: Aakash Gajjar 3 | * @Date: 2017-02-15 13:33:38 4 | * @Last Modified by: Aakash Gajjar 5 | * @Last Modified time: 2017-02-15 14:10:30 6 | * @Subject: 7 | */ 8 | 9 | /* Program to print all permutations of a string in sorted order.*/ 10 | 11 | #include 12 | #include "chain_states.c" 13 | #include "chain_hamiltonian.c" 14 | 15 | int main(int argc, const char *argv[]) { 16 | int make_binary_file = 0; 17 | if (argc > 0) { 18 | make_binary_file = atoi(argv[1]); 19 | } 20 | 21 | basis(); 22 | hamiltonian(make_binary_file); 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | # Created by https://www.toptal.com/developers/gitignore/api/c 3 | # Edit at https://www.toptal.com/developers/gitignore?templates=c 4 | 5 | ### C ### 6 | # Prerequisites 7 | *.d 8 | 9 | # Object files 10 | *.o 11 | *.ko 12 | *.obj 13 | *.elf 14 | 15 | # Linker output 16 | *.ilk 17 | *.map 18 | *.exp 19 | 20 | # Precompiled Headers 21 | *.gch 22 | *.pch 23 | 24 | # Libraries 25 | *.lib 26 | *.a 27 | *.la 28 | *.lo 29 | 30 | # Shared objects (inc. Windows DLLs) 31 | *.dll 32 | *.so 33 | *.so.* 34 | *.dylib 35 | 36 | # Executables 37 | *.exe 38 | *.out 39 | *.app 40 | *.i*86 41 | *.x86_64 42 | *.hex 43 | 44 | # Debug files 45 | *.dSYM/ 46 | *.su 47 | *.idb 48 | *.pdb 49 | 50 | # Kernel Module Compile Results 51 | *.mod* 52 | *.cmd 53 | .tmp_versions/ 54 | modules.order 55 | Module.symvers 56 | Mkfile.old 57 | dkms.conf 58 | 59 | # End of https://www.toptal.com/developers/gitignore/api/c 60 | 61 | data* 62 | *.txt 63 | *.rb 64 | *.docx 65 | *.txt* 66 | *.cpp 67 | -------------------------------------------------------------------------------- /all/pure/particles_generate_states.c: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: Aakash Gajjar 3 | * @Date: 2017-01-26 23:40:31 4 | * @Last Modified by: Aakash Gajjar 5 | * @Last Modified time: 2017-03-09 16:53:23 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | void fast_dec_bin(int length, unsigned long x, int * c) { 14 | int i; 15 | for (i = 0; i < length; i++) 16 | *(c++) = (x >> i) & 0x1; 17 | } 18 | 19 | int fillConfigs(int rows, int cols, FILE *file) { 20 | unsigned long i; 21 | int j; 22 | int *state = calloc(cols , sizeof(state)); 23 | 24 | for (i = 0; i < rows; i++) { 25 | fast_dec_bin(cols, i, state); 26 | for (j = cols - 1; j >= 0; j--) { 27 | fprintf(file, "%d", state[j]); 28 | } 29 | fprintf(file, "\n"); 30 | } 31 | return 0; 32 | } 33 | 34 | int main() { 35 | int total_particles; 36 | int total_states; 37 | FILE *file; 38 | clock_t start, end; 39 | 40 | printf("Enter number of particles:"); 41 | scanf("%d", &total_particles); 42 | printf("Generating states for: %d particles ", total_particles); 43 | 44 | start = clock(); 45 | 46 | total_states = pow(2 , total_particles); 47 | printf("with total %d states\n", total_states); 48 | 49 | file = fopen("states.txt", "w+"); 50 | if (ferror(file)) { 51 | printf("Error opening file\n"); 52 | return 1; 53 | } 54 | 55 | fprintf(file, "%d %d\n", total_particles, total_states); 56 | fillConfigs(total_states, total_particles, file); 57 | close(file); 58 | 59 | end = clock(); 60 | printf("Which are saved in 'states.txt', it took %f secs.\n", (double)(end - start) / CLOCKS_PER_SEC); 61 | 62 | return 0; 63 | } -------------------------------------------------------------------------------- /chain_states.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This program requires user input of 3 | * Length of Chain(Total number of prticles) 4 | * Number of UP Particles 5 | * Omega 6 | * The Interation Strength (same in all direction) 7 | * 8 | * The Output file format will be 9 | * Length UpParticles 10 | * TotalStates 11 | * Omega InterationStrength 12 | * States on each new line 13 | * 14 | */ 15 | 16 | #include 17 | #include 18 | #include 19 | #include 20 | 21 | #define bool int 22 | #define true 1 23 | #define false 0 24 | 25 | int get_states_state_num(int length, char from[]) { 26 | int i; 27 | int answer = 0; 28 | for (i = 0; i < length; i++) { 29 | answer += (from[i] - 48) * pow(2, length - i - 1); 30 | } 31 | return answer; 32 | } 33 | 34 | 35 | unsigned long long int nCr(int n, int r) { 36 | if (r > n / 2) { 37 | r = n - r; 38 | } 39 | unsigned long long int ans = 1; 40 | int i; 41 | 42 | for (i = 1; i <= r; i++) { 43 | ans *= n - r + i; 44 | ans /= i; 45 | } 46 | return ans; 47 | } 48 | 49 | /* Following function is needed for library function qsort(). 50 | */ 51 | int compare(const void *a, const void * b) { 52 | return ( *(char *)a - * (char *)b ); 53 | } 54 | 55 | /* A utility function two swap two characters a and b 56 | */ 57 | void swap(char* a, char* b) { 58 | char t = *a; 59 | *a = *b; 60 | *b = t; 61 | } 62 | 63 | /* This function finds the index of the smallest character 64 | * which is greater than 'first' and is present in str[l..h] 65 | */ 66 | int findCeil(char str[], char first, int l, int h) { 67 | /* initialize index of ceiling element*/ 68 | int ceilIndex = l; 69 | int i; 70 | 71 | /* Now iterate through rest of the elements and find 72 | * the smallest character greater than 'first' 73 | */ 74 | for (i = l + 1; i <= h; i++) 75 | if (str[i] > first && str[i] < str[ceilIndex]) 76 | ceilIndex = i; 77 | 78 | return ceilIndex; 79 | } 80 | 81 | /* Print all permutations of str in sorted order*/ 82 | void sortedPermutations(char str[], FILE *file, FILE *same) { 83 | /* Get size of string*/ 84 | int size = strlen(str); 85 | 86 | /* Sort the string in increasing order*/ 87 | qsort(str, size, sizeof( str[0] ), compare); 88 | 89 | /* Print permutations one by one*/ 90 | bool isFinished = false; 91 | while (!isFinished) { 92 | /* print this permutation*/ 93 | fprintf(file, "%d %s\n", get_states_state_num(size, str), str); 94 | fprintf(same, "%d %s\n", get_states_state_num(size, str), str); 95 | 96 | /* Find the rightmost character which is smaller than its next 97 | * character. Let us call it 'first char' 98 | */ 99 | int i; 100 | for (i = size - 2; i >= 0; --i) { 101 | if (str[i] < str[i + 1]) { 102 | break; 103 | } 104 | } 105 | 106 | /* If there is no such chracter, all are sorted in decreasing order, 107 | * means we just printed the last permutation and we are done. 108 | */ 109 | if (i == -1) { 110 | isFinished = true; 111 | } else { 112 | /* Find the ceil of 'first char' in right of first character. 113 | * Ceil of a character is the smallest character greater than it 114 | */ 115 | int ceilIndex = findCeil(str, str[i], i + 1, size - 1); 116 | 117 | /* Swap first and second characters*/ 118 | swap(&str[i], &str[ceilIndex]); 119 | 120 | /* Sort the string on right of 'first char'*/ 121 | qsort(str + i + 1, size - i - 1, sizeof(str[0]), compare); 122 | } 123 | } 124 | } 125 | 126 | void generate_states(int length, int minUp, float Omega, float J_interaction) { 127 | int i; 128 | char *str = malloc(sizeof(*str) * length + 1); 129 | char *name = calloc(20, sizeof(*name)); 130 | char *str_length = calloc(10, sizeof(*str_length)); 131 | char *str_minUp = calloc(10, sizeof(*str_minUp)); 132 | char ch[] = "_"; 133 | char ext[] = ".txt"; 134 | char base[] = "states_"; 135 | 136 | sprintf(str_length, "%d", length); 137 | sprintf(str_minUp, "%d", minUp); 138 | 139 | strcat(name, base); 140 | strcat(name, str_length); 141 | strcat(name, ch); 142 | strcat(name, str_minUp); 143 | strcat(name, ext); 144 | 145 | /*Assign Data to str 146 | * 0000....1111 147 | */ 148 | for (i = 0; i < length; i++) { 149 | if (i < minUp) 150 | str[i] = '1'; 151 | else 152 | str[i] = '0'; 153 | } 154 | /*Null terminator*/ 155 | str[i] = '\0'; 156 | 157 | FILE *file, *same; 158 | file = fopen(name, "w"); 159 | same = fopen("states.txt", "w"); 160 | if (file == NULL) { 161 | printf("Error opening file\n"); 162 | } 163 | 164 | /* We should print L & minUp 165 | * with total number of basis state 166 | */ 167 | fprintf(file, "%d %d\n%llu\n%f %f\n", length, minUp, nCr(length, minUp), Omega, J_interaction); 168 | fprintf(same, "%d %d\n%llu\n%f %f\n", length, minUp, nCr(length, minUp), Omega, J_interaction); 169 | 170 | /* call function for permutations. 171 | * two arguments string: str 172 | * FILE: file 173 | */ 174 | sortedPermutations(str, file, same); 175 | 176 | fclose(file); 177 | fclose(same); 178 | } 179 | 180 | void basis() { 181 | int length; 182 | int minUp; 183 | float Omega; 184 | float J_interaction; 185 | printf("Enter length of chain: "); 186 | scanf("%d", &length); 187 | printf("Enter number of spin UP particles: "); 188 | scanf("%d", &minUp); 189 | printf("Enter Omega: "); 190 | scanf("%f", &Omega); 191 | printf("Enter interaction strength between particles: "); 192 | scanf("%f", &J_interaction); 193 | if (minUp > length) 194 | fprintf(stderr, "Input Error: minUp should be less than length of chain\n"); 195 | generate_states(length, minUp, Omega, J_interaction); 196 | printf("Configurations of chain are generated.\n"); 197 | } -------------------------------------------------------------------------------- /all/mixed/chain_states.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This program requires user input of 3 | * Length of Chain(Total number of prticles) 4 | * Number of UP Particles 5 | * Omega 6 | * The Interation Strength (same in all direction) 7 | * 8 | * The Output file format will be 9 | * Length UpParticles 10 | * TotalStates 11 | * Omega InterationStrength 12 | * States on each new line 13 | * 14 | */ 15 | 16 | #include 17 | #include 18 | #include 19 | #include 20 | 21 | #define bool int 22 | #define true 1 23 | #define false 0 24 | 25 | int get_states_state_num(int length, char from[]) { 26 | int i; 27 | int answer = 0; 28 | for (i = 0; i < length; i++) { 29 | answer += (from[i] - 48) * pow(2, length - i - 1); 30 | } 31 | return answer; 32 | } 33 | 34 | 35 | unsigned long long int nCr(int n, int r) { 36 | if (r > n / 2) { 37 | r = n - r; 38 | } 39 | unsigned long long int ans = 1; 40 | int i; 41 | 42 | for (i = 1; i <= r; i++) { 43 | ans *= n - r + i; 44 | ans /= i; 45 | } 46 | return ans; 47 | } 48 | 49 | /* Following function is needed for library function qsort(). 50 | */ 51 | int compare(const void *a, const void * b) { 52 | return ( *(char *)a - * (char *)b ); 53 | } 54 | 55 | /* A utility function two swap two characters a and b 56 | */ 57 | void swap(char* a, char* b) { 58 | char t = *a; 59 | *a = *b; 60 | *b = t; 61 | } 62 | 63 | /* This function finds the index of the smallest character 64 | * which is greater than 'first' and is present in str[l..h] 65 | */ 66 | int findCeil(char str[], char first, int l, int h) { 67 | /* initialize index of ceiling element*/ 68 | int ceilIndex = l; 69 | int i; 70 | 71 | /* Now iterate through rest of the elements and find 72 | * the smallest character greater than 'first' 73 | */ 74 | for (i = l + 1; i <= h; i++) 75 | if (str[i] > first && str[i] < str[ceilIndex]) 76 | ceilIndex = i; 77 | 78 | return ceilIndex; 79 | } 80 | 81 | /* Print all permutations of str in sorted order*/ 82 | void sortedPermutations(char str[], FILE *file, FILE *same) { 83 | /* Get size of string*/ 84 | int size = strlen(str); 85 | 86 | /* Sort the string in increasing order*/ 87 | qsort(str, size, sizeof( str[0] ), compare); 88 | 89 | /* Print permutations one by one*/ 90 | bool isFinished = false; 91 | while (!isFinished) { 92 | /* print this permutation*/ 93 | fprintf(file, "%d %s\n", get_states_state_num(size, str), str); 94 | fprintf(same, "%d %s\n", get_states_state_num(size, str), str); 95 | 96 | /* Find the rightmost character which is smaller than its next 97 | * character. Let us call it 'first char' 98 | */ 99 | int i; 100 | for (i = size - 2; i >= 0; --i) { 101 | if (str[i] < str[i + 1]) { 102 | break; 103 | } 104 | } 105 | 106 | /* If there is no such chracter, all are sorted in decreasing order, 107 | * means we just printed the last permutation and we are done. 108 | */ 109 | if (i == -1) { 110 | isFinished = true; 111 | } else { 112 | /* Find the ceil of 'first char' in right of first character. 113 | * Ceil of a character is the smallest character greater than it 114 | */ 115 | int ceilIndex = findCeil(str, str[i], i + 1, size - 1); 116 | 117 | /* Swap first and second characters*/ 118 | swap(&str[i], &str[ceilIndex]); 119 | 120 | /* Sort the string on right of 'first char'*/ 121 | qsort(str + i + 1, size - i - 1, sizeof(str[0]), compare); 122 | } 123 | } 124 | } 125 | 126 | void generate_states(int length, int minUp, float Omega, float J_interaction) { 127 | int i; 128 | char *str = malloc(sizeof(*str) * length + 1); 129 | char *name = calloc(20, sizeof(*name)); 130 | char *str_length = calloc(10, sizeof(*str_length)); 131 | char *str_minUp = calloc(10, sizeof(*str_minUp)); 132 | char ch[] = "_"; 133 | char ext[] = ".txt"; 134 | char base[] = "states_"; 135 | 136 | sprintf(str_length, "%d", length); 137 | sprintf(str_minUp, "%d", minUp); 138 | 139 | strcat(name, base); 140 | strcat(name, str_length); 141 | strcat(name, ch); 142 | strcat(name, str_minUp); 143 | strcat(name, ext); 144 | 145 | /*Assign Data to str 146 | * 0000....1111 147 | */ 148 | for (i = 0; i < length; i++) { 149 | if (i < minUp) 150 | str[i] = '1'; 151 | else 152 | str[i] = '0'; 153 | } 154 | /*Null terminator*/ 155 | str[i] = '\0'; 156 | 157 | FILE *file, *same; 158 | file = fopen(name, "w"); 159 | same = fopen("states.txt", "w"); 160 | if (file == NULL) { 161 | printf("Error opening file\n"); 162 | } 163 | 164 | /* We should print L & minUp 165 | * with total number of basis state 166 | */ 167 | fprintf(file, "%d %d\n%llu\n%f %f\n", length, minUp, nCr(length, minUp), Omega, J_interaction); 168 | fprintf(same, "%d %d\n%llu\n%f %f\n", length, minUp, nCr(length, minUp), Omega, J_interaction); 169 | 170 | /* call function for permutations. 171 | * two arguments string: str 172 | * FILE: file 173 | */ 174 | sortedPermutations(str, file, same); 175 | 176 | fclose(file); 177 | fclose(same); 178 | } 179 | 180 | void basis() { 181 | int length; 182 | int minUp; 183 | float Omega; 184 | float J_interaction; 185 | printf("Enter length of chain: "); 186 | scanf("%d", &length); 187 | printf("Enter number of spin UP particles: "); 188 | scanf("%d", &minUp); 189 | printf("Enter Omega: "); 190 | scanf("%f", &Omega); 191 | printf("Enter interaction strength between particles: "); 192 | scanf("%f", &J_interaction); 193 | if (minUp > length) 194 | fprintf(stderr, "Input Error: minUp should be less than length of chain\n"); 195 | generate_states(length, minUp, Omega, J_interaction); 196 | printf("Configurations of chain are generated.\n"); 197 | } -------------------------------------------------------------------------------- /all/pure/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: Aakash Gajjar 3 | * @Date: 2017-02-16 15:09:50 4 | * @Last Modified by: Aakash Gajjar 5 | * @Last Modified time: 2017-03-09 16:38:12 6 | * @Subject: 7 | */ 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | #define DEBUG 0 18 | #define HAMILTONIAN 1 19 | #define DENSITYMATRIX 1 20 | #define RED_DENSITYMATRIX 1 21 | #define RED_DENSITYMATRIX_EIGEN 1 22 | 23 | void init_hamiltonian(int states, gsl_matrix *Hamiltonian) { 24 | int i, j; 25 | for (i = 0; i < states; i++) 26 | for (j = 0; j < states; j++) 27 | gsl_matrix_set(Hamiltonian, i, j, 0.0); 28 | } 29 | 30 | void print_matrix_to_stdout(int states, gsl_matrix *Hamiltonian, const char *msg) { 31 | int i, j; 32 | printf("\n====================================\n"); 33 | printf("%s:\n", msg); 34 | for (i = 0; i < states; i++) 35 | for (printf("\n"), j = 0; j < states; j++) 36 | printf("%10.2g ", gsl_matrix_get(Hamiltonian, i, j)); 37 | printf("\n====================================\n"); 38 | } 39 | 40 | int main() { 41 | int length; 42 | int states; 43 | float Omega, J_interaction; 44 | int to_read; 45 | int i, j; 46 | double value; 47 | //double *eigen_vector; 48 | //clock_t start, end; 49 | FILE *file; 50 | 51 | file = fopen("Hamiltonian.txt", "r"); 52 | if (ferror(file)) { 53 | fprintf(stderr, "Error opening file Hamiltonian.txt\n"); 54 | exit(1); 55 | } 56 | 57 | fscanf(file, "%d\n%d\n%f %f\n%d", 58 | &length, &states, 59 | &Omega, &J_interaction, 60 | &to_read); 61 | 62 | gsl_matrix *Hamiltonian = gsl_matrix_alloc(states, states); 63 | gsl_matrix *density_matrix = gsl_matrix_alloc(states, states); 64 | gsl_matrix *reduced_density_matrix = gsl_matrix_alloc(states / 2, states / 2); 65 | 66 | init_hamiltonian(states, Hamiltonian); 67 | while (fscanf(file, "%d %d %lf", &i, &j, &value) == 3) { 68 | gsl_matrix_set(Hamiltonian, i, j, value); 69 | gsl_matrix_set(Hamiltonian, j, i, value); 70 | } 71 | fclose(file); 72 | 73 | #if DEBUG == 1 && HAMILTONIAN == 1 74 | print_matrix_to_stdout(states, Hamiltonian, "Hamiltonian"); 75 | #endif 76 | 77 | gsl_vector *EigenValue_ham = gsl_vector_alloc (states); 78 | gsl_matrix *EigenVector_ham = gsl_matrix_alloc (states, states); 79 | gsl_vector *EigenValue_density = gsl_vector_alloc (states); 80 | gsl_matrix *EigenVector_density = gsl_matrix_alloc (states, states); 81 | gsl_vector *EigenValue_reduced_density = gsl_vector_alloc (states / 2); 82 | gsl_matrix *EigenVector_reduced_density = gsl_matrix_alloc (states / 2, states / 2); 83 | 84 | gsl_eigen_symmv_workspace * workspace_ham = gsl_eigen_symmv_alloc (states); 85 | gsl_eigen_symmv_workspace * workspace_density = gsl_eigen_symmv_alloc (states); 86 | gsl_eigen_symmv_workspace * workspace_reduced_density = gsl_eigen_symmv_alloc (states / 2); 87 | 88 | //eigen_vector = malloc(states * sizeof(eigen_vector)); 89 | 90 | gsl_eigen_symmv (Hamiltonian, EigenValue_ham, EigenVector_ham, workspace_ham); 91 | 92 | gsl_eigen_symmv_sort (EigenValue_ham, EigenVector_ham, GSL_EIGEN_SORT_ABS_ASC); { 93 | int eigen_i, eigen_j; 94 | for (i = 0; i < states; i++) { // Eigenvalues of each state 95 | // for (j = 0; j < states; j++) { // Get Eigen Values 96 | // eigen_vector[j] = gsl_matrix_get(EigenVector, i, j); 97 | // } 98 | // Construct Density MAtrix 99 | for (eigen_i = 0; eigen_i < states; eigen_i++) { 100 | //printf("%f\n", gsl_matrix_get(EigenVector_ham, i, eigen_i)); 101 | for (eigen_j = eigen_i; eigen_j < states; eigen_j++) { 102 | // value = eigen_vector[eigen_i] * eigen_vector[eigen_j]; 103 | // gsl_matrix_set(density_matrix, eigen_i, eigen_j, value); 104 | gsl_matrix_set(density_matrix, eigen_i, eigen_j, 105 | gsl_matrix_get(EigenVector_ham, i, eigen_i) * 106 | gsl_matrix_get(EigenVector_ham, i, eigen_j)); 107 | gsl_matrix_set(density_matrix, eigen_j, eigen_i, 108 | gsl_matrix_get(EigenVector_ham, i, eigen_i) * 109 | gsl_matrix_get(EigenVector_ham, i, eigen_j)); 110 | } 111 | } 112 | 113 | #if DEBUG == 1 && DENSITYMATRIX == 1 114 | value = 0.0; 115 | for (eigen_i = 0; eigen_i < states; eigen_i++) { 116 | value += gsl_matrix_get(density_matrix, eigen_i, eigen_i); 117 | } 118 | print_matrix_to_stdout(states, density_matrix, "Density Matrix"); 119 | printf("\nTrace: %5.2f", value); 120 | printf("\n====================================\n"); 121 | #endif 122 | // gsl_eigen_symmv(density_matrix, EigenValue_density,EigenVector_density, workspace_density); 123 | //{ 124 | for (eigen_i = 0; eigen_i < states / 2; eigen_i++) { 125 | for (eigen_j = 0; eigen_j < states / 2; eigen_j++) { 126 | gsl_matrix_set(reduced_density_matrix, eigen_i, eigen_j, 127 | gsl_matrix_get(density_matrix, 2 * eigen_i, 2 * eigen_j)); 128 | } 129 | } 130 | #if DEBUG == 1 && RED_DENSITYMATRIX == 1 131 | print_matrix_to_stdout(states / 2, reduced_density_matrix, "Reduced Density Matrix"); 132 | #endif 133 | gsl_eigen_symmv(reduced_density_matrix, EigenValue_reduced_density, 134 | EigenVector_reduced_density, workspace_reduced_density); { 135 | value = 0; 136 | for (eigen_i = 0; eigen_i < states / 2; eigen_i++) { 137 | //value += gsl_pow_2(gsl_vector_get(EigenValue_reduced_density, eigen_i)); 138 | printf("\nEN: %.18e", gsl_vector_get(EigenValue_reduced_density, eigen_i)); 139 | // value += gsl_vector_get(EigenValue_reduced_density, eigen_i) * 140 | // gsl_sf_log(gsl_vector_get(EigenValue_reduced_density, eigen_i)); 141 | } 142 | //printf("Entropy:%f\n", value); 143 | } 144 | //} 145 | } 146 | } 147 | 148 | return 0; 149 | } -------------------------------------------------------------------------------- /all/mixed/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: Aakash Gajjar 3 | * @Date: 2017-02-16 15:09:50 4 | * @Last Modified by: Aakash Gajjar 5 | * @Last Modified time: 2017-03-24 00:16:04 6 | * @Subject: 7 | */ 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | #define DEBUG 1 18 | #define HAMILTONIAN 1 19 | #define DENSITYMATRIX 1 20 | #define RED_DENSITYMATRIX 1 21 | #define RED_DENSITYMATRIX_EIGEN 1 22 | 23 | void init_hamiltonian(int states, gsl_matrix *Hamiltonian) { 24 | int i, j; 25 | for (i = 0; i < states; i++) 26 | for (j = 0; j < states; j++) 27 | gsl_matrix_set(Hamiltonian, i, j, 0.0); 28 | } 29 | 30 | void print_matrix_to_stdout(int states, gsl_matrix *Hamiltonian, const char *msg) { 31 | int i, j; 32 | printf("\n====================================\n"); 33 | printf("%s:\n", msg); 34 | for (i = 0; i < states; i++) 35 | for (printf("\n"), j = 0; j < states; j++) 36 | printf("%10.2g ", gsl_matrix_get(Hamiltonian, i, j)); 37 | printf("\n====================================\n"); 38 | } 39 | 40 | int main() { 41 | int length; 42 | int states; 43 | float Omega, J_interaction; 44 | int to_read; 45 | int i, j; 46 | double value; 47 | //double *eigen_vector; 48 | //clock_t start, end; 49 | FILE *file; 50 | 51 | file = fopen("Hamiltonian.txt", "r"); 52 | if (ferror(file)) { 53 | fprintf(stderr, "Error opening file Hamiltonian.txt\n"); 54 | exit(1); 55 | } 56 | 57 | fscanf(file, "%d\n%d\n%f %f\n%d", 58 | &length, &states, 59 | &Omega, &J_interaction, 60 | &to_read); 61 | 62 | gsl_matrix *Hamiltonian = gsl_matrix_alloc(states, states); 63 | gsl_matrix *density_matrix = gsl_matrix_alloc(states, states); 64 | gsl_matrix *reduced_density_matrix = gsl_matrix_alloc(states / 2, states / 2); 65 | 66 | init_hamiltonian(states, Hamiltonian); 67 | while (fscanf(file, "%d %d %lf", &i, &j, &value) == 3) { 68 | gsl_matrix_set(Hamiltonian, i, j, value); 69 | gsl_matrix_set(Hamiltonian, j, i, value); 70 | } 71 | fclose(file); 72 | 73 | #if DEBUG == 1 && HAMILTONIAN == 1 74 | print_matrix_to_stdout(states, Hamiltonian, "Hamiltonian"); 75 | #endif 76 | 77 | gsl_vector *EigenValue_ham = gsl_vector_alloc (states); 78 | gsl_matrix *EigenVector_ham = gsl_matrix_alloc (states, states); 79 | gsl_vector *EigenValue_density = gsl_vector_alloc (states); 80 | gsl_matrix *EigenVector_density = gsl_matrix_alloc (states, states); 81 | gsl_vector *EigenValue_reduced_density = gsl_vector_alloc (states / 2); 82 | gsl_matrix *EigenVector_reduced_density = gsl_matrix_alloc (states / 2, states / 2); 83 | 84 | gsl_eigen_symmv_workspace * workspace_ham = gsl_eigen_symmv_alloc (states); 85 | gsl_eigen_symmv_workspace * workspace_density = gsl_eigen_symmv_alloc (states); 86 | gsl_eigen_symmv_workspace * workspace_reduced_density = gsl_eigen_symmv_alloc (states / 2); 87 | 88 | //eigen_vector = malloc(states * sizeof(eigen_vector)); 89 | 90 | gsl_eigen_symmv (Hamiltonian, EigenValue_ham, EigenVector_ham, workspace_ham); 91 | 92 | //gsl_eigen_symmv_sort (EigenValue_ham, EigenVector_ham, GSL_EIGEN_SORT_ABS_ASC); 93 | { 94 | int eigen_i, eigen_j; 95 | for (i = 0; i < states; i++) { // Eigenvalues of each state 96 | for (j = 0; j < states; j++) { // Get Eigen Values 97 | printf("H:%f\n", gsl_matrix_get(EigenVector_ham, i, j)); 98 | } 99 | // Construct Density MAtrix 100 | for (eigen_i = 0; eigen_i < states; eigen_i++) { 101 | //printf("%f\n", gsl_matrix_get(EigenVector_ham, i, eigen_i)); 102 | for (eigen_j = eigen_i; eigen_j < states; eigen_j++) { 103 | // value = eigen_vector[eigen_i] * eigen_vector[eigen_j]; 104 | // gsl_matrix_set(density_matrix, eigen_i, eigen_j, value); 105 | gsl_matrix_set(density_matrix, eigen_i, eigen_j, 106 | gsl_matrix_get(EigenVector_ham, i, eigen_i) * 107 | gsl_matrix_get(EigenVector_ham, i, eigen_j)); 108 | gsl_matrix_set(density_matrix, eigen_j, eigen_i, 109 | gsl_matrix_get(EigenVector_ham, i, eigen_i) * 110 | gsl_matrix_get(EigenVector_ham, i, eigen_j)); 111 | } 112 | } 113 | 114 | #if DEBUG == 1 && DENSITYMATRIX == 1 115 | value = 0.0; 116 | for (eigen_i = 0; eigen_i < states; eigen_i++) { 117 | value += gsl_matrix_get(density_matrix, eigen_i, eigen_i); 118 | } 119 | print_matrix_to_stdout(states, density_matrix, "Density Matrix"); 120 | printf("\nTrace: %5.2f", value); 121 | printf("\n====================================\n"); 122 | #endif 123 | for (eigen_i = 0; eigen_i < states / 2; eigen_i++) { 124 | for (eigen_j = 0; eigen_j < states / 2; eigen_j++) { 125 | gsl_matrix_set(reduced_density_matrix, eigen_i, eigen_j, 126 | gsl_matrix_get(density_matrix, 2 * eigen_i, 2 * eigen_j)); 127 | } 128 | } 129 | #if DEBUG == 1 && RED_DENSITYMATRIX == 1 130 | print_matrix_to_stdout(states / 2, reduced_density_matrix, "Reduced Density Matrix"); 131 | #endif 132 | gsl_eigen_symmv(reduced_density_matrix, EigenValue_reduced_density, 133 | EigenVector_reduced_density, workspace_reduced_density); 134 | gsl_eigen_symmv_sort (EigenValue_reduced_density, 135 | EigenVector_reduced_density, 136 | GSL_EIGEN_SORT_ABS_ASC); { 137 | value = 0; 138 | for (eigen_i = 0; eigen_i < states / 2; eigen_i++) { 139 | //value += gsl_pow_2(gsl_vector_get(EigenValue_reduced_density, eigen_i)); 140 | printf("\nEN: %.18e", gsl_vector_get(EigenValue_reduced_density, eigen_i)); 141 | // value += gsl_vector_get(EigenValue_reduced_density, eigen_i) * 142 | // gsl_sf_log(gsl_vector_get(EigenValue_reduced_density, eigen_i)); 143 | } 144 | //printf("Entropy:%f\n", value); 145 | } 146 | } 147 | } 148 | 149 | return 0; 150 | } -------------------------------------------------------------------------------- /main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: Aakash Gajjar 3 | * @Date: 2017-02-16 15:09:50 4 | * @Last Modified by: Aakash Gajjar 5 | * @Last Modified time: 2017-03-24 00:16:04 6 | * @Subject: 7 | */ 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | #define DEBUG 1 18 | #define HAMILTONIAN 1 19 | #define DENSITYMATRIX 1 20 | #define RED_DENSITYMATRIX 1 21 | #define RED_DENSITYMATRIX_EIGEN 1 22 | 23 | void init_hamiltonian(int states, gsl_matrix *Hamiltonian) { 24 | int i, j; 25 | for (i = 0; i < states; i++) 26 | for (j = 0; j < states; j++) 27 | gsl_matrix_set(Hamiltonian, i, j, 0.0); 28 | } 29 | 30 | void print_matrix_to_stdout(int states, gsl_matrix *Hamiltonian, const char *msg) { 31 | int i, j; 32 | printf("\n====================================\n"); 33 | printf("%s:\n", msg); 34 | for (i = 0; i < states; i++) 35 | for (printf("\n"), j = 0; j < states; j++) 36 | printf("%10.2g ", gsl_matrix_get(Hamiltonian, i, j)); 37 | printf("\n====================================\n"); 38 | } 39 | 40 | int main() { 41 | int length; 42 | int states; 43 | float Omega, J_interaction; 44 | int to_read; 45 | int i, j; 46 | double value; 47 | //double *eigen_vector; 48 | //clock_t start, end; 49 | FILE *file; 50 | 51 | file = fopen("Hamiltonian.txt", "r"); 52 | if (ferror(file)) { 53 | fprintf(stderr, "Error opening file Hamiltonian.txt\n"); 54 | exit(1); 55 | } 56 | 57 | fscanf(file, "%d\n%d\n%f %f\n%d", 58 | &length, &states, 59 | &Omega, &J_interaction, 60 | &to_read); 61 | 62 | gsl_matrix *Hamiltonian = gsl_matrix_alloc(states, states); 63 | gsl_matrix *density_matrix = gsl_matrix_alloc(states, states); 64 | gsl_matrix *reduced_density_matrix = gsl_matrix_alloc(states / 2, states / 2); 65 | 66 | init_hamiltonian(states, Hamiltonian); 67 | while (fscanf(file, "%d %d %lf", &i, &j, &value) == 3) { 68 | gsl_matrix_set(Hamiltonian, i, j, value); 69 | gsl_matrix_set(Hamiltonian, j, i, value); 70 | } 71 | fclose(file); 72 | 73 | #if DEBUG == 1 && HAMILTONIAN == 1 74 | print_matrix_to_stdout(states, Hamiltonian, "Hamiltonian"); 75 | #endif 76 | 77 | gsl_vector *EigenValue_ham = gsl_vector_alloc (states); 78 | gsl_matrix *EigenVector_ham = gsl_matrix_alloc (states, states); 79 | gsl_vector *EigenValue_density = gsl_vector_alloc (states); 80 | gsl_matrix *EigenVector_density = gsl_matrix_alloc (states, states); 81 | gsl_vector *EigenValue_reduced_density = gsl_vector_alloc (states / 2); 82 | gsl_matrix *EigenVector_reduced_density = gsl_matrix_alloc (states / 2, states / 2); 83 | 84 | gsl_eigen_symmv_workspace * workspace_ham = gsl_eigen_symmv_alloc (states); 85 | gsl_eigen_symmv_workspace * workspace_density = gsl_eigen_symmv_alloc (states); 86 | gsl_eigen_symmv_workspace * workspace_reduced_density = gsl_eigen_symmv_alloc (states / 2); 87 | 88 | //eigen_vector = malloc(states * sizeof(eigen_vector)); 89 | 90 | gsl_eigen_symmv (Hamiltonian, EigenValue_ham, EigenVector_ham, workspace_ham); 91 | //gsl_eigen_symmv_sort (EigenValue_ham, EigenVector_ham, GSL_EIGEN_SORT_ABS_ASC); 92 | { 93 | int eigen_i, eigen_j; 94 | for (i = 0; i < states; i++) { // Eigenvalues of each state 95 | for (j = 0; j < states; j++) { // Get Eigen Values 96 | printf("H:%f\n", gsl_matrix_get(EigenVector_ham, i, j)); 97 | } 98 | // Construct Density MAtrix 99 | for (eigen_i = 0; eigen_i < states; eigen_i++) { 100 | //printf("%f\n", gsl_matrix_get(EigenVector_ham, i, eigen_i)); 101 | for (eigen_j = eigen_i; eigen_j < states; eigen_j++) { 102 | // value = eigen_vector[eigen_i] * eigen_vector[eigen_j]; 103 | // gsl_matrix_set(density_matrix, eigen_i, eigen_j, value); 104 | gsl_matrix_set(density_matrix, eigen_i, eigen_j, 105 | gsl_matrix_get(EigenVector_ham, i, eigen_i) * 106 | gsl_matrix_get(EigenVector_ham, i, eigen_j)); 107 | gsl_matrix_set(density_matrix, eigen_j, eigen_i, 108 | gsl_matrix_get(EigenVector_ham, i, eigen_i) * 109 | gsl_matrix_get(EigenVector_ham, i, eigen_j)); 110 | } 111 | } 112 | 113 | #if DEBUG == 1 && DENSITYMATRIX == 1 114 | value = 0.0; 115 | for (eigen_i = 0; eigen_i < states; eigen_i++) { 116 | value += gsl_matrix_get(density_matrix, eigen_i, eigen_i); 117 | } 118 | print_matrix_to_stdout(states, density_matrix, "Density Matrix"); 119 | printf("\nTrace: %5.2f", value); 120 | printf("\n====================================\n"); 121 | #endif 122 | for (eigen_i = 0; eigen_i < states / 2; eigen_i++) { 123 | for (eigen_j = 0; eigen_j < states / 2; eigen_j++) { 124 | gsl_matrix_set(reduced_density_matrix, eigen_i, eigen_j, 125 | gsl_matrix_get(density_matrix, 2 * eigen_i, 2 * eigen_j)); 126 | } 127 | } 128 | #if DEBUG == 1 && RED_DENSITYMATRIX == 1 129 | print_matrix_to_stdout(states / 2, reduced_density_matrix, "Reduced Density Matrix"); 130 | #endif 131 | gsl_eigen_symmv(reduced_density_matrix, EigenValue_reduced_density, 132 | EigenVector_reduced_density, workspace_reduced_density); 133 | gsl_eigen_symmv_sort (EigenValue_reduced_density, 134 | EigenVector_reduced_density, 135 | GSL_EIGEN_SORT_ABS_ASC); { 136 | value = 0; 137 | for (eigen_i = 0; eigen_i < states / 2; eigen_i++) { 138 | //value += gsl_pow_2(gsl_vector_get(EigenValue_reduced_density, eigen_i)); 139 | printf("\nEN: %.18e", gsl_vector_get(EigenValue_reduced_density, eigen_i)); 140 | // value += gsl_vector_get(EigenValue_reduced_density, eigen_i) * 141 | // gsl_sf_log(gsl_vector_get(EigenValue_reduced_density, eigen_i)); 142 | } 143 | //printf("Entropy:%f\n", value); 144 | printf("\n"); 145 | } 146 | } 147 | } 148 | 149 | return 0; 150 | } -------------------------------------------------------------------------------- /all/pure/main_hamiltonian.c: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: Aakash Gajjar 3 | * @Date: 2017-02-11 22:17:47 4 | * @Last Modified by: Aakash Gajjar 5 | * @Last Modified time: 2017-03-09 17:04:27 6 | * @Subject: 7 | */ 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | #define true 1 15 | #define false 0 16 | 17 | char *make_file_name(int states, float Omega, float J_interaction, int make_binary_file) { 18 | char name[] = "Hamiltonian"; 19 | char saperator[] = "_"; 20 | char *interaction = malloc(4 * sizeof(char)); 21 | char *omega_str = malloc(4 * sizeof(char)); 22 | char ext[] = ".txt"; 23 | char *bin = calloc(8, sizeof(char)); 24 | char binary[] = "binary"; 25 | char redable[] = "redable"; 26 | char *states_str = malloc(sizeof(states) * 4); 27 | char *file_name = calloc(sizeof(name) + sizeof(interaction) + sizeof(omega_str) 28 | + sizeof(ext) + sizeof(states_str) + sizeof(bin) + 4, 29 | sizeof(char)); 30 | 31 | sprintf(states_str, "%d", states); 32 | strcat(file_name, name); 33 | strcat(file_name, saperator); 34 | sprintf(omega_str, "%4.2f", Omega); 35 | strcat(file_name, omega_str); 36 | strcat(file_name, saperator); 37 | sprintf(interaction, "%4.2f", J_interaction); 38 | strcat(file_name, interaction); 39 | strcat(file_name, saperator); 40 | 41 | if (make_binary_file) { 42 | strcat(file_name, binary); 43 | } else { 44 | strcat(file_name, redable); 45 | } 46 | 47 | /*strcat(file_name, states_str);*/ 48 | strcat(file_name, ext); 49 | return file_name; 50 | } 51 | 52 | void printSTDOUT(int states, float **hamiltonian) { 53 | int i, j; 54 | for (i = 0; i < states; i++) { 55 | printf("\n"); 56 | for (j = 0; j < states; j++) { 57 | printf("%5.2f", hamiltonian[i][j]); 58 | } 59 | } 60 | printf("\n"); 61 | } 62 | 63 | int non_zero_element(int states, float **hamiltonian) { 64 | int i, j; 65 | int count = 0; 66 | for (i = 0; i < states; i++) { 67 | for (j = 0; j < states; j++) { 68 | if (hamiltonian[i][j] != 0) 69 | count++; 70 | } 71 | } 72 | return count; 73 | } 74 | 75 | void printHamiltonianFile(int length, int states, float **hamiltonian, float Omega, float J_interaction, int make_binary_file) { 76 | FILE *file0, *file1; 77 | char *file_name = make_file_name(states, Omega, J_interaction, make_binary_file); 78 | char buffer[256]; 79 | int i, j; 80 | if (make_binary_file) { 81 | file0 = fopen(file_name, "wb"); 82 | file1 = fopen("Hamiltonian.bin", "wb"); 83 | } else { 84 | file0 = fopen(file_name, "w"); 85 | file1 = fopen("Hamiltonian.txt", "w"); 86 | } 87 | 88 | sprintf(buffer, "%d\n%d\n%f %f\n%d\n", length, states, Omega, J_interaction, non_zero_element(states, hamiltonian)); 89 | fwrite(buffer, 1, strlen(buffer), file0); 90 | fwrite(buffer, 1, strlen(buffer), file1); 91 | for (i = 0; i < states; i++) { 92 | printf("\rPrinted Row:%d/%d", i + 1, states); 93 | for (j = i; j < states; j++) { 94 | if (hamiltonian[i][j] != 0) { 95 | if (make_binary_file) { 96 | fwrite(&i, sizeof (i), 1, file0); 97 | fwrite(&j, sizeof (j), 1, file1); 98 | fwrite(&i, sizeof (i), 1, file0); 99 | fwrite(&j, sizeof (j), 1, file1); 100 | fwrite(&hamiltonian[i][j], sizeof (hamiltonian[i][j]), 1, file0); 101 | fwrite(&hamiltonian[i][j], sizeof (hamiltonian[i][j]), 1, file1); 102 | } else { 103 | sprintf(buffer, "%d %d %4.2f\n", i, j, hamiltonian[i][j]); 104 | fwrite(buffer, 1, strlen(buffer), file0); 105 | fwrite(buffer, 1, strlen(buffer), file1); 106 | } 107 | } 108 | } 109 | } 110 | free(file_name); 111 | fclose(file0); 112 | fclose(file1); 113 | } 114 | 115 | int get_state_num(int length, int from[], int swapA, int swapB) { 116 | int i; 117 | int answer = 0; 118 | for (i = 0; i < length; i++) { 119 | if (i == swapA) { 120 | answer += from[swapB] * pow(2, length - i - 1); 121 | } else if (i == swapB) { 122 | answer += from[swapA] * pow(2, length - i - 1); 123 | } else { 124 | answer += from[i] * pow(2, length - i - 1); 125 | } 126 | } 127 | return answer; 128 | } 129 | 130 | int multiply_Sz_terms(int states, int length, int **basis, float **hamiltonian, float J) { 131 | int i, j, k; 132 | float answer; 133 | for (i = 0; i < states; i++) { 134 | for (j = 0; j < length - 1; j += 2) { 135 | answer = 1.0; 136 | for (k = j; k <= j + 1; k++) { 137 | if (basis[i][k] == 1) { 138 | answer *= -0.5; 139 | } else { 140 | answer *= 0.5; 141 | } 142 | } 143 | hamiltonian[i][i] += answer; 144 | } 145 | hamiltonian[i][i] *= 2.0 * J; 146 | } 147 | return 0; 148 | } 149 | 150 | int multiply_SS_terms(int states, int length, int **basis, float **hamiltonian, float J) { 151 | int i, j, k; 152 | int raise[] = {}; 153 | int work_state[length]; 154 | int current_state_num, getting_state_num; 155 | float answer; 156 | for (j = 0; j < length - 1; j++) { 157 | for (i = 0; i < states; i++) { 158 | if (basis[i][j]^basis[i][j + 1]) { 159 | current_state_num = i; 160 | getting_state_num = get_state_num(length, basis[i], j, j + 1); 161 | hamiltonian[current_state_num][getting_state_num] = 0.5 * j; 162 | hamiltonian[getting_state_num][current_state_num] = 0.5 * J; 163 | } 164 | } 165 | } 166 | multiply_Sz_terms(states, length, basis, hamiltonian, J); 167 | return 0; 168 | } 169 | 170 | int Summing_Sz_terms(int states, int length, int **basis, float **hamiltonian, float w) { 171 | int i, j; 172 | float answer = 0.0; 173 | for (i = 0; i < states; i++) { 174 | answer = 0.0; 175 | for (j = 0; j < length; j++) { 176 | if (basis[i][j] == 0) { 177 | answer += -1.0; 178 | } else { 179 | answer += 1.0; 180 | } 181 | } 182 | hamiltonian[i][i] = answer * w; 183 | } 184 | return 0; 185 | } 186 | 187 | void readBasis(int states, int length, int **basis, FILE *file) { 188 | int i, j; 189 | for (i = 0; i < states; i++) { 190 | for (j = 0; j < length; j++) { 191 | fscanf(file, "%1d", &basis[i][j]); 192 | } 193 | } 194 | } 195 | 196 | int main() { 197 | int i, j; 198 | int states; 199 | int length; 200 | FILE *file; 201 | 202 | float J = 1.0; 203 | float w = 1.0; 204 | 205 | const int make_binary_file = false; 206 | 207 | scanf("Enter Interaction Strength: %f", &J); 208 | 209 | file = fopen("states.txt", "r"); 210 | fscanf(file, "%d %d", &length, &states); 211 | 212 | float **hamiltonian = calloc(states , sizeof * hamiltonian); 213 | int **basis = calloc(states , sizeof * basis); 214 | for (i = 0; i < states; i++) { 215 | hamiltonian[i] = calloc(states, sizeof * (hamiltonian[i])); 216 | basis[i] = calloc(length, sizeof * (basis[i])); 217 | } 218 | printf("Basis and Hamiltonian Matrix Allocated.\n"); 219 | 220 | readBasis(states, length, basis, file); 221 | fclose(file); 222 | printf("Basis Read from file.\n"); 223 | 224 | printf("Applying ESz.\n"); 225 | Summing_Sz_terms(states, length, basis, hamiltonian, J); 226 | printf("Applying SS.\n"); 227 | multiply_SS_terms(states, length, basis, hamiltonian, J); 228 | 229 | printf("Printing Hamiltonian to a file.\n"); 230 | printHamiltonianFile(length, states, hamiltonian, w, J, make_binary_file); 231 | 232 | free(hamiltonian); 233 | return 0; 234 | } 235 | -------------------------------------------------------------------------------- /chain_hamiltonian.c: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: Aakash Gajjar 3 | * @Date: 2017-02-11 22:17:47 4 | * @Last Modified by: Aakash Gajjar 5 | * @Last Modified time: 2017-03-09 16:47:47 6 | * @Subject: 7 | */ 8 | 9 | #include 10 | #include 11 | #include 12 | 13 | #define true 1 14 | #define false 0 15 | 16 | #define S_EIGEN 0.5 17 | #define S_COEFF 2.0 18 | 19 | char *make_file_name(int states, float Omega, float J_interaction, int make_binary_file) { 20 | char name[] = "Hamiltonian"; 21 | char saperator[] = "_"; 22 | char *interaction = malloc(4 * sizeof(char)); 23 | char *omega_str = malloc(4 * sizeof(char)); 24 | char ext[] = ".txt"; 25 | char *bin = calloc(8, sizeof(char)); 26 | char binary[] = "binary"; 27 | char redable[] = "redable"; 28 | char *states_str = malloc(sizeof(states) * 4); 29 | char *file_name = calloc(sizeof(name) + sizeof(interaction) + sizeof(omega_str) 30 | + sizeof(ext) + sizeof(states_str) + sizeof(bin) + 4, 31 | sizeof(char)); 32 | 33 | sprintf(states_str, "%d", states); 34 | strcat(file_name, name); 35 | strcat(file_name, saperator); 36 | sprintf(omega_str, "%4.2f", Omega); 37 | strcat(file_name, omega_str); 38 | strcat(file_name, saperator); 39 | sprintf(interaction, "%4.2f", J_interaction); 40 | strcat(file_name, interaction); 41 | strcat(file_name, saperator); 42 | 43 | if (make_binary_file) { 44 | strcat(file_name, binary); 45 | } else { 46 | strcat(file_name, redable); 47 | } 48 | 49 | /*strcat(file_name, states_str);*/ 50 | strcat(file_name, ext); 51 | return file_name; 52 | } 53 | 54 | void printSTDOUT(int states, float **hamiltonian) { 55 | int i, j; 56 | for (i = 0; i < states; i++) { 57 | printf("\n"); 58 | for (j = 0; j < states; j++) { 59 | printf("%5.2f", hamiltonian[i][j]); 60 | } 61 | } 62 | printf("\n"); 63 | } 64 | 65 | int non_zero_element(int states, float **hamiltonian) { 66 | int i, j; 67 | int count = 0; 68 | for (i = 0; i < states; i++) { 69 | for (j = 0; j < states; j++) { 70 | if (hamiltonian[i][j] != 0) 71 | count++; 72 | } 73 | } 74 | return count; 75 | } 76 | 77 | void printHamiltonianFile(int length, int states, float **hamiltonian, float Omega, float J_interaction, int make_binary_file) { 78 | FILE *file0, *file1; 79 | char *file_name = make_file_name(states, Omega, J_interaction, make_binary_file); 80 | char buffer[256]; 81 | int i, j; 82 | if (make_binary_file) { 83 | file0 = fopen(file_name, "wb"); 84 | file1 = fopen("Hamiltonian.bin", "wb"); 85 | } else { 86 | file0 = fopen(file_name, "w"); 87 | file1 = fopen("Hamiltonian.txt", "w"); 88 | } 89 | 90 | sprintf(buffer, "%d\n%d\n%f %f\n%d\n", length, states, Omega, J_interaction, non_zero_element(states, hamiltonian)); 91 | fwrite(buffer, 1, strlen(buffer), file0); 92 | fwrite(buffer, 1, strlen(buffer), file1); 93 | for (i = 0; i < states; i++) { 94 | printf("\rPrinted Row:%d/%d", i + 1, states); 95 | for (j = i; j < states; j++) { 96 | if (hamiltonian[i][j] != 0) { 97 | if (make_binary_file) { 98 | fwrite(&i, sizeof (i), 1, file0); 99 | fwrite(&j, sizeof (j), 1, file1); 100 | fwrite(&i, sizeof (i), 1, file0); 101 | fwrite(&j, sizeof (j), 1, file1); 102 | fwrite(&hamiltonian[i][j], sizeof (hamiltonian[i][j]), 1, file0); 103 | fwrite(&hamiltonian[i][j], sizeof (hamiltonian[i][j]), 1, file1); 104 | } else { 105 | sprintf(buffer, "%d %d %4.2f\n", i, j, hamiltonian[i][j]); 106 | fwrite(buffer, 1, strlen(buffer), file0); 107 | fwrite(buffer, 1, strlen(buffer), file1); 108 | } 109 | } 110 | } 111 | } 112 | free(file_name); 113 | fclose(file0); 114 | fclose(file1); 115 | } 116 | 117 | void multiply_Sz_terms(int states, int length, int **basis, float **hamiltonian, float J_interaction) { 118 | int i, j, k; 119 | float answer; 120 | for (i = 0; i < states; i++) { 121 | for (j = 0; j < length - 1; j += 2) { 122 | answer = 1.0; 123 | for (k = j; k <= j + 1; k++) { 124 | if (basis[i][k] == 1) { 125 | answer *= -S_EIGEN; 126 | } else { 127 | answer *= S_EIGEN; 128 | } 129 | } 130 | hamiltonian[i][i] += answer; 131 | } 132 | hamiltonian[i][i] *= (S_COEFF * J_interaction); 133 | } 134 | } 135 | 136 | int get_state_num(int length, int from[], int swapA, int swapB) { 137 | int i; 138 | int answer = 0; 139 | for (i = 0; i < length; i++) { 140 | if (i == swapA) { 141 | answer += from[swapB] * pow(2, length - i - 1); 142 | } else if (i == swapB) { 143 | answer += from[swapA] * pow(2, length - i - 1); 144 | } else { 145 | answer += from[i] * pow(2, length - i - 1); 146 | } 147 | } 148 | return answer; 149 | } 150 | 151 | int find_state_num(int length, int find, int * inthis) { 152 | int i; 153 | for (i = 0; i < length; i++) { 154 | if (inthis[i] == find) 155 | return i; 156 | } 157 | fprintf(stderr, "Error cannot find %d\n", find); 158 | exit(1); 159 | } 160 | 161 | int multiply_SS_terms(int states, int length, int **basis, int *basis_num, float **hamiltonian, float J_interaction) { 162 | int i, j; 163 | int current_state_num, getting_state_num; 164 | for (j = 0; j < length - 1; j++) { 165 | for (i = 0; i < states; i++) { 166 | if (basis[i][j]^basis[i][j + 1]) { 167 | current_state_num = i; 168 | getting_state_num = get_state_num(length, basis[i], j, j + 1); 169 | getting_state_num = find_state_num(states, getting_state_num, basis_num); 170 | hamiltonian[current_state_num][getting_state_num] += S_EIGEN * J_interaction; 171 | hamiltonian[getting_state_num][current_state_num] += S_EIGEN * J_interaction; 172 | } 173 | } 174 | } 175 | multiply_Sz_terms(states, length, basis, hamiltonian, J_interaction); 176 | return 0; 177 | } 178 | 179 | int Summing_Sz_terms(int states, int length, int **basis, float **hamiltonian, float Omega) { 180 | int i, j; 181 | float answer = 0.0; 182 | for (i = 0; i < states; i++) { 183 | answer = 0.0; 184 | for (j = 0; j < length; j++) { 185 | if (basis[i][j] == 0) { 186 | answer += -S_EIGEN * Omega; 187 | } else { 188 | answer += S_EIGEN * Omega; 189 | } 190 | } 191 | hamiltonian[i][i] += answer; 192 | } 193 | return 0; 194 | } 195 | 196 | void readBasis(int states, int length, int **basis, int *basis_num, FILE *file) { 197 | int i, j; 198 | for (i = 0; i < states; i++) { 199 | fscanf(file, "%d", &basis_num[i]); 200 | for (j = 0; j < length; j++) { 201 | fscanf(file, "%1d", &basis[i][j]); 202 | } 203 | } 204 | } 205 | 206 | void hamiltonian(int make_binary_file) { 207 | int i; 208 | int states; 209 | int length, minUp; 210 | float J_interaction; 211 | float Omega; 212 | FILE *file; 213 | 214 | file = fopen("states.txt", "r"); 215 | fscanf(file, "%d %d\n%d\n%f %f\n", &length, &minUp, &states, &Omega, &J_interaction); 216 | /*printf("%d %d %d %f %f\n", length, minUp, states, Omega, J_interaction);*/ 217 | 218 | float **hamiltonian = calloc(states , sizeof * hamiltonian); 219 | int **basis = calloc(states , sizeof * basis); 220 | int *basis_num = calloc(states, sizeof * basis_num); 221 | for (i = 0; i < states; i++) { 222 | hamiltonian[i] = calloc(states, sizeof * (hamiltonian[i])); 223 | basis[i] = calloc(length, sizeof * (basis[i])); 224 | } 225 | printf("Basis and Hamiltonian Matrix Allocated.\n"); 226 | 227 | readBasis(states, length, basis, basis_num, file); 228 | fclose(file); 229 | printf("Basis Read from file.\n"); 230 | 231 | /*printf("Applying ESz.\n"); 232 | Summing_Sz_terms(states, length, basis, hamiltonian, Omega);*/ 233 | printf("Applying SS.\n"); 234 | multiply_SS_terms(states, length, basis, basis_num, hamiltonian, J_interaction); 235 | 236 | printf("Printing Hamiltonian to a file.\n"); 237 | printHamiltonianFile(length, states, hamiltonian, Omega, J_interaction, make_binary_file); 238 | printSTDOUT(states, hamiltonian); 239 | free(hamiltonian); 240 | printf("\rFinished! "); 241 | } 242 | -------------------------------------------------------------------------------- /all/mixed/chain_hamiltonian.c: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: Aakash Gajjar 3 | * @Date: 2017-02-11 22:17:47 4 | * @Last Modified by: Aakash Gajjar 5 | * @Last Modified time: 2017-03-09 16:47:47 6 | * @Subject: 7 | */ 8 | 9 | #include 10 | #include 11 | #include 12 | 13 | #define true 1 14 | #define false 0 15 | 16 | #define S_EIGEN 0.5 17 | #define S_COEFF 2.0 18 | 19 | char *make_file_name(int states, float Omega, float J_interaction, int make_binary_file) { 20 | char name[] = "Hamiltonian"; 21 | char saperator[] = "_"; 22 | char *interaction = malloc(4 * sizeof(char)); 23 | char *omega_str = malloc(4 * sizeof(char)); 24 | char ext[] = ".txt"; 25 | char *bin = calloc(8, sizeof(char)); 26 | char binary[] = "binary"; 27 | char redable[] = "redable"; 28 | char *states_str = malloc(sizeof(states) * 4); 29 | char *file_name = calloc(sizeof(name) + sizeof(interaction) + sizeof(omega_str) 30 | + sizeof(ext) + sizeof(states_str) + sizeof(bin) + 4, 31 | sizeof(char)); 32 | 33 | sprintf(states_str, "%d", states); 34 | strcat(file_name, name); 35 | strcat(file_name, saperator); 36 | sprintf(omega_str, "%4.2f", Omega); 37 | strcat(file_name, omega_str); 38 | strcat(file_name, saperator); 39 | sprintf(interaction, "%4.2f", J_interaction); 40 | strcat(file_name, interaction); 41 | strcat(file_name, saperator); 42 | 43 | if (make_binary_file) { 44 | strcat(file_name, binary); 45 | } else { 46 | strcat(file_name, redable); 47 | } 48 | 49 | /*strcat(file_name, states_str);*/ 50 | strcat(file_name, ext); 51 | return file_name; 52 | } 53 | 54 | void printSTDOUT(int states, float **hamiltonian) { 55 | int i, j; 56 | for (i = 0; i < states; i++) { 57 | printf("\n"); 58 | for (j = 0; j < states; j++) { 59 | printf("%5.2f", hamiltonian[i][j]); 60 | } 61 | } 62 | printf("\n"); 63 | } 64 | 65 | int non_zero_element(int states, float **hamiltonian) { 66 | int i, j; 67 | int count = 0; 68 | for (i = 0; i < states; i++) { 69 | for (j = 0; j < states; j++) { 70 | if (hamiltonian[i][j] != 0) 71 | count++; 72 | } 73 | } 74 | return count; 75 | } 76 | 77 | void printHamiltonianFile(int length, int states, float **hamiltonian, float Omega, float J_interaction, int make_binary_file) { 78 | FILE *file0, *file1; 79 | char *file_name = make_file_name(states, Omega, J_interaction, make_binary_file); 80 | char buffer[256]; 81 | int i, j; 82 | if (make_binary_file) { 83 | file0 = fopen(file_name, "wb"); 84 | file1 = fopen("Hamiltonian.bin", "wb"); 85 | } else { 86 | file0 = fopen(file_name, "w"); 87 | file1 = fopen("Hamiltonian.txt", "w"); 88 | } 89 | 90 | sprintf(buffer, "%d\n%d\n%f %f\n%d\n", length, states, Omega, J_interaction, non_zero_element(states, hamiltonian)); 91 | fwrite(buffer, 1, strlen(buffer), file0); 92 | fwrite(buffer, 1, strlen(buffer), file1); 93 | for (i = 0; i < states; i++) { 94 | printf("\rPrinted Row:%d/%d", i + 1, states); 95 | for (j = i; j < states; j++) { 96 | if (hamiltonian[i][j] != 0) { 97 | if (make_binary_file) { 98 | fwrite(&i, sizeof (i), 1, file0); 99 | fwrite(&j, sizeof (j), 1, file1); 100 | fwrite(&i, sizeof (i), 1, file0); 101 | fwrite(&j, sizeof (j), 1, file1); 102 | fwrite(&hamiltonian[i][j], sizeof (hamiltonian[i][j]), 1, file0); 103 | fwrite(&hamiltonian[i][j], sizeof (hamiltonian[i][j]), 1, file1); 104 | } else { 105 | sprintf(buffer, "%d %d %4.2f\n", i, j, hamiltonian[i][j]); 106 | fwrite(buffer, 1, strlen(buffer), file0); 107 | fwrite(buffer, 1, strlen(buffer), file1); 108 | } 109 | } 110 | } 111 | } 112 | free(file_name); 113 | fclose(file0); 114 | fclose(file1); 115 | } 116 | 117 | void multiply_Sz_terms(int states, int length, int **basis, float **hamiltonian, float J_interaction) { 118 | int i, j, k; 119 | float answer; 120 | for (i = 0; i < states; i++) { 121 | for (j = 0; j < length - 1; j += 2) { 122 | answer = 1.0; 123 | for (k = j; k <= j + 1; k++) { 124 | if (basis[i][k] == 1) { 125 | answer *= -S_EIGEN; 126 | } else { 127 | answer *= S_EIGEN; 128 | } 129 | } 130 | hamiltonian[i][i] += answer; 131 | } 132 | hamiltonian[i][i] *= (S_COEFF * J_interaction); 133 | } 134 | } 135 | 136 | int get_state_num(int length, int from[], int swapA, int swapB) { 137 | int i; 138 | int answer = 0; 139 | for (i = 0; i < length; i++) { 140 | if (i == swapA) { 141 | answer += from[swapB] * pow(2, length - i - 1); 142 | } else if (i == swapB) { 143 | answer += from[swapA] * pow(2, length - i - 1); 144 | } else { 145 | answer += from[i] * pow(2, length - i - 1); 146 | } 147 | } 148 | return answer; 149 | } 150 | 151 | int find_state_num(int length, int find, int * inthis) { 152 | int i; 153 | for (i = 0; i < length; i++) { 154 | if (inthis[i] == find) 155 | return i; 156 | } 157 | fprintf(stderr, "Error cannot find %d\n", find); 158 | exit(1); 159 | } 160 | 161 | int multiply_SS_terms(int states, int length, int **basis, int *basis_num, float **hamiltonian, float J_interaction) { 162 | int i, j; 163 | int current_state_num, getting_state_num; 164 | for (j = 0; j < length - 1; j++) { 165 | for (i = 0; i < states; i++) { 166 | if (basis[i][j]^basis[i][j + 1]) { 167 | current_state_num = i; 168 | getting_state_num = get_state_num(length, basis[i], j, j + 1); 169 | getting_state_num = find_state_num(states, getting_state_num, basis_num); 170 | hamiltonian[current_state_num][getting_state_num] += S_EIGEN * J_interaction; 171 | hamiltonian[getting_state_num][current_state_num] += S_EIGEN * J_interaction; 172 | } 173 | } 174 | } 175 | multiply_Sz_terms(states, length, basis, hamiltonian, J_interaction); 176 | return 0; 177 | } 178 | 179 | int Summing_Sz_terms(int states, int length, int **basis, float **hamiltonian, float Omega) { 180 | int i, j; 181 | float answer = 0.0; 182 | for (i = 0; i < states; i++) { 183 | answer = 0.0; 184 | for (j = 0; j < length; j++) { 185 | if (basis[i][j] == 0) { 186 | answer += -S_EIGEN * Omega; 187 | } else { 188 | answer += S_EIGEN * Omega; 189 | } 190 | } 191 | hamiltonian[i][i] += answer; 192 | } 193 | return 0; 194 | } 195 | 196 | void readBasis(int states, int length, int **basis, int *basis_num, FILE *file) { 197 | int i, j; 198 | for (i = 0; i < states; i++) { 199 | fscanf(file, "%d", &basis_num[i]); 200 | for (j = 0; j < length; j++) { 201 | fscanf(file, "%1d", &basis[i][j]); 202 | } 203 | } 204 | } 205 | 206 | void hamiltonian(int make_binary_file) { 207 | int i; 208 | int states; 209 | int length, minUp; 210 | float J_interaction; 211 | float Omega; 212 | FILE *file; 213 | 214 | file = fopen("states.txt", "r"); 215 | fscanf(file, "%d %d\n%d\n%f %f\n", &length, &minUp, &states, &Omega, &J_interaction); 216 | /*printf("%d %d %d %f %f\n", length, minUp, states, Omega, J_interaction);*/ 217 | 218 | float **hamiltonian = calloc(states , sizeof * hamiltonian); 219 | int **basis = calloc(states , sizeof * basis); 220 | int *basis_num = calloc(states, sizeof * basis_num); 221 | for (i = 0; i < states; i++) { 222 | hamiltonian[i] = calloc(states, sizeof * (hamiltonian[i])); 223 | basis[i] = calloc(length, sizeof * (basis[i])); 224 | } 225 | printf("Basis and Hamiltonian Matrix Allocated.\n"); 226 | 227 | readBasis(states, length, basis, basis_num, file); 228 | fclose(file); 229 | printf("Basis Read from file.\n"); 230 | 231 | /*printf("Applying ESz.\n"); 232 | Summing_Sz_terms(states, length, basis, hamiltonian, Omega);*/ 233 | printf("Applying SS.\n"); 234 | multiply_SS_terms(states, length, basis, basis_num, hamiltonian, J_interaction); 235 | 236 | printf("Printing Hamiltonian to a file.\n"); 237 | printHamiltonianFile(length, states, hamiltonian, Omega, J_interaction, make_binary_file); 238 | printSTDOUT(states, hamiltonian); 239 | free(hamiltonian); 240 | printf("\rFinished! "); 241 | } 242 | --------------------------------------------------------------------------------