1 /* Ergo, version 3.8, a program for linear scaling electronic structure
2 * calculations.
3 * Copyright (C) 2019 Elias Rudberg, Emanuel H. Rubensson, Pawel Salek,
4 * and Anastasia Kruchinina.
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * Primary academic reference:
20 * Ergo: An open-source program for linear-scaling electronic structure
21 * calculations,
22 * Elias Rudberg, Emanuel H. Rubensson, Pawel Salek, and Anastasia
23 * Kruchinina,
24 * SoftwareX 7, 107 (2018),
25 * <http://dx.doi.org/10.1016/j.softx.2018.03.005>
26 *
27 * For further information about Ergo, see <http://www.ergoscf.org>.
28 */
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <stdio.h>
33
34
35 typedef double ergo_real;
36
37
38 #define MAX_NO_OF_CONTR_GAUSSIANS 20
39
40 #define ergo_malloc malloc
41 #define ergo_free free
42
43
44
45 const int DENSITY_FILE_VERSION_NUMBER_OLD = 10001;
46 const int DENSITY_FILE_VERSION_NUMBER_NEW = 10002;
47
48
49 typedef struct
50 {
51 int densityFileVersion;
52 int typeOfMatrixStorage;
53 int noOfShells;
54 int noOfBasisFuncs;
55 int noOfDensityMatrices;
56 int matrixSize_1;
57 int matrixSize_2;
58 int fileSizeInBytes;
59 } densityFileHeaderStruct_old;
60
61
62 typedef struct
63 {
64 int densityFileVersion;
65 int typeOfMatrixStorage;
66 int noOfShells;
67 int noOfBasisFuncs;
68 int noOfDensityMatrices;
69 int dummy_int;
70 double matrixSize_1;
71 double matrixSize_2;
72 double fileSizeInBytes;
73 } densityFileHeaderStruct_new;
74
75
76
77
78
main(int argc,char * argv[])79 int main(int argc, char* argv[])
80 {
81 if(argc != 3)
82 {
83 printf("usage: ./x old_density_file new_density_file\n");
84 return 0;
85 }
86
87 printf("standalone_density_file_conversion_program_2 version 1.1\n");
88
89 char* fileName_old = argv[1];
90 char* fileName_new = argv[2];
91
92 // open old density file
93 FILE* f_in = fopen(fileName_old, "rb");
94 if(f_in == NULL)
95 {
96 printf("error opening file '%s'\n", fileName_old);
97 return -1;
98 }
99
100 // check file size
101 fseeko(f_in, 0L, SEEK_END);
102 off_t fileSize_old = ftello(f_in);
103 if(fileSize_old < sizeof(densityFileHeaderStruct_old))
104 {
105 printf("error: (fileSize_old < sizeof(densityFileHeaderStruct_old))\n");
106 return -1;
107 }
108 fseeko(f_in, 0L, SEEK_SET);
109 char* buffer_old = (char*)ergo_malloc(fileSize_old);
110 if(buffer_old == NULL)
111 {
112 printf("error in malloc.\n");
113 return -1;
114 }
115 size_t readsz_old = fread(buffer_old, 1, fileSize_old, f_in);
116 fclose(f_in);
117 if(readsz_old != fileSize_old)
118 {
119 printf("error reading file '%s'\n", fileName_old);
120 ergo_free(buffer_old);
121 return -1;
122 }
123
124 // OK, old density file contents are now in buffer_old.
125 densityFileHeaderStruct_old* headerPtr_old = (densityFileHeaderStruct_old*)buffer_old;
126 if(headerPtr_old->fileSizeInBytes != fileSize_old)
127 {
128 printf("error: (headerPtr_old->fileSizeInBytes != fileSize_old)\n");
129 return -1;
130 }
131 int noOfDensityMatrices = headerPtr_old->noOfDensityMatrices;
132 if(noOfDensityMatrices != 1 && noOfDensityMatrices != 2)
133 {
134 printf("error: (noOfDensityMatrices != 1 && noOfDensityMatrices != 2)\n");
135 return -1;
136 }
137
138 int n = headerPtr_old->noOfBasisFuncs;
139 int nShells = headerPtr_old->noOfShells;
140
141 printf("nShells = %i, n = %i\n", nShells, n);
142
143 if(n <= 0 || nShells <= 0)
144 {
145 printf("error: (n <= 0 || nShells <= 0)\n");
146 return -1;
147 }
148
149 printf("OK, file '%s' seems to contain a proper old-style density description, noOfDensityMatrices = %i\n",
150 fileName_old, noOfDensityMatrices);
151 printf("Now attempting to create new density description file '%s'\n", fileName_new);
152
153 char* p = buffer_old + sizeof(densityFileHeaderStruct_old);
154
155 densityFileHeaderStruct_new densityFileHeader_new;
156 densityFileHeader_new.densityFileVersion = DENSITY_FILE_VERSION_NUMBER_NEW;
157 densityFileHeader_new.typeOfMatrixStorage = headerPtr_old->typeOfMatrixStorage;
158 densityFileHeader_new.noOfShells = headerPtr_old->noOfShells;
159 densityFileHeader_new.noOfBasisFuncs = headerPtr_old->noOfBasisFuncs;
160 densityFileHeader_new.noOfDensityMatrices = headerPtr_old->noOfDensityMatrices;
161 densityFileHeader_new.matrixSize_1 = (double)headerPtr_old->matrixSize_1;
162 densityFileHeader_new.matrixSize_2 = (double)headerPtr_old->matrixSize_2;
163 densityFileHeader_new.fileSizeInBytes = (double)(headerPtr_old->fileSizeInBytes + sizeof(densityFileHeaderStruct_new) - sizeof(densityFileHeaderStruct_old));
164
165 // write to file
166 FILE* f_out = fopen(fileName_new, "wb");
167 if(f_out == NULL)
168 {
169 printf("error opening file '%s' for writing\n", fileName_new);
170 return -1;
171 }
172
173 size_t noOfBytesWritten_1 = fwrite(&densityFileHeader_new, 1, sizeof(densityFileHeaderStruct_new), f_out);
174 if(noOfBytesWritten_1 != sizeof(densityFileHeaderStruct_new))
175 {
176 printf("error in fwrite\n");
177 return -1;
178 }
179 int restSize = densityFileHeader_new.fileSizeInBytes - sizeof(densityFileHeaderStruct_new);
180 size_t noOfBytesWritten_2 = fwrite(p, 1, restSize, f_out);
181 if(noOfBytesWritten_2 != restSize)
182 {
183 printf("error in fwrite\n");
184 return -1;
185 }
186 fclose(f_out);
187
188 ergo_free(buffer_old);
189
190 printf("OK, density description file '%s' created, %i bytes.\n", fileName_new, (int)densityFileHeader_new.fileSizeInBytes);
191
192 return 0;
193 }
194
195