File: molcaslib.c

package info (click to toggle)
gabedit 2.4.8-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 14,768 kB
  • ctags: 14,922
  • sloc: ansic: 291,210; cpp: 2,081; sh: 1,111; makefile: 521; csh: 181
file content (261 lines) | stat: -rw-r--r-- 6,773 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/****************************************************************************
 * program for get list of basis available for an atoms using Molcas pacckage     
 * molcaslib H  and enter for obtain all basis for H atom 
 * molcaslib    and enter for obtain all basis for all atoms
*****************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <ctype.h>
#include <sys/stat.h>
#include <dirent.h>

#define NATOMS 120
#define BSIZE 1024

typedef struct _Atom
{
	char symb[10];
	int nbas;
	char basisName[NATOMS][BSIZE];
	char author[NATOMS][BSIZE];
	char primitive[NATOMS][BSIZE];
	char contraction[NATOMS][BSIZE];
	char ecpType[NATOMS][BSIZE];
}Atom;
/***********************************************************************************/
#define NROW 18
#define NCOL 10
char *SymbAtoms[18][10]={
		{"H" ,"Li","Na","K" ,"Rb","Cs","Fr","00","00","00"},
		{"00","Be","Mg","Ca","Sr","Ba","Ra","00","X","00"},
		{"00","00","00","Sc","Y" ,"La","Ac","00","00","00"},
		{"00","00","00","Ti","Zr","Hf","00","00","00","00"},
		{"00","00","00","V" ,"Nb","Ta","00","Ce","Th","00"},
		{"00","00","00","Cr","Mo","W" ,"00","Pr","Pa","00"},
		{"00","00","00","Mn","Tc","Re","00","Nd","U" ,"00"},
		{"00","00","00","Fe","Ru","Os","00","Pm","Np","00"},
		{"00","00","00","Co","Rh","Ir","00","Sm","Pu","00"},
		{"00","00","00","Ni","Pd","Pt","00","Eu","Am","00"},
		{"00","00","00","Cu","Ag","Au","00","Gd","Cm","00"},
		{"00","00","00","Zn","Cd","Hg","00","Tb","Bk","00"},
		{"00","B" ,"Al","Ga","In","Tl","00","Dy","Cf","00"},
		{"00","C" ,"Si","Ge","Sn","Pb","00","Ho","Es","00"},
		{"00","N" ,"P" ,"As","Sb","Bi","00","Er","Fm","00"},
		{"00","O" ,"S" ,"Se","Te","Po","00","Tm","Md","00"},
		{"00","F" ,"Cl","Br","I" ,"At","00","Yb","No","00"},
		{"He","Ne","Ar","Kr","Xe","Rn","00","Lu","Lr","00"},
		};
/***********************************************************************************/
char** createListFiles(char* dirname, int* nFiles)
{
	DIR* dir;
	struct stat st;
	struct dirent *File;
	char** allName = NULL;

	*nFiles = 0;

	dir = opendir(dirname);
	if(dir)
	{
		File = readdir(dir);
		while(File)
		{

			if( strlen(File->d_name)>0 && File->d_name[0] != '.' )
			{
				char all_name[BSIZE];

				sprintf(all_name,"%s/%s",dirname,File->d_name);
				stat(all_name,&st);
				if(!(st.st_mode & S_IFDIR))
				{
					allName = realloc(allName, (*nFiles+1)*sizeof(char*));
					allName[*nFiles] = malloc(BSIZE*sizeof(char));
					sprintf(allName[*nFiles],all_name);
					(*nFiles)++;
				}
			}
			File = readdir(dir);
		}
		closedir(dir);
	}
	return allName;
}
/***********************************************************************************/
void initBasis(Atom* atom, char* symb)
{
	atom->nbas = 0;
	sprintf(atom->symb,"%s",symb);
}
/***********************************************************************************/
void AddBasisForAnAtomFromAFile(Atom* atom, FILE* file)
{
	char buffer[BSIZE];
	char buffer2[BSIZE];
	char symb[BSIZE];
	char tmp[BSIZE];
	int nbas;
	int i;
	int n;
	int len=0;
	int k;

	sprintf(symb,"/%s.",atom->symb);
	nbas = atom->nbas;
	/* printf("symb = %s\n",symb);*/

	while(!feof(file))
	{
		if(!fgets(buffer,BSIZE,file)) return;
		if(!strstr(buffer,symb)) continue;
		if(buffer[0]=='*') continue;
		len = strlen(buffer);
		k = 0;
		for(i=0; i<len; i++)
		{
			if(buffer[i]!='.')
			{
				buffer2[k] = buffer[i];
				k++;
			}
			else
			{
				if(i<len-1 && buffer[i+1]=='.')
				{
					buffer2[k] = ' ';
					k++;
					buffer2[k] = 'U';
					k++;
					buffer2[k] = 'N';
					k++;
					buffer2[k] = 'K';
					k++;
				}
				else
				{
					buffer2[k] = ' ';
					k++;
				}
			}
		}
		buffer2[k] = '\0';

		/* printf("Buffer2 = %s",buffer2);*/
		n = sscanf(buffer2, "%s %s %s %s %s %s ",tmp,atom->basisName[nbas], atom->author[nbas],atom->primitive[nbas],atom->contraction[nbas], atom->ecpType[nbas]);
		/* printf("n = %d\n",n);*/
		if(n<6) sprintf(atom->ecpType[nbas],"UNK");
		if(n<5) sprintf(atom->contraction[nbas],"UNK");
		if(n<4) sprintf(atom->primitive[nbas],"UNK");
		if(n<3) sprintf(atom->author[nbas],"UNK");
		if(n<2) continue;

		atom->nbas++;
		nbas = atom->nbas;
	}
}
/***********************************************************************************/
void printBasisForAnAtom(Atom* atom)
{
	int nbas = 0;
	int i;

	nbas = atom->nbas;
	if(nbas>0)
		printf("Basis List for atom %s\n",atom->symb);
	else
		printf("No Basis available for %s atom \n",atom->symb);

	printf("-------------------------------\n");
	for(i=0; i<nbas; i++)
	{
		printf("\t%s.",atom->symb);
		printf("%s.",atom->basisName[i]);
		if(strstr(atom->author[i],"UNK")) printf(".");
		else printf("%s.",atom->author[i]);
		if(strstr(atom->primitive[i],"UNK")) printf(".");
		else printf("%s.",atom->primitive[i]);
		if(strstr(atom->contraction[i],"UNK")) printf(".");
		else printf("%s.",atom->contraction[i]);
		if(strstr(atom->ecpType[i],"UNK")) printf(".");
		else printf("%s.",atom->ecpType[i]);
		printf("\n");
	}
	printf("============================================================\n");
}
/***********************************************************************************/
void getBasisForAnAtom(Atom* atom, int nFiles, char** allFiles)
{
	FILE* fin;
	FILE* fout;
	int i;
	for(i=0;i<nFiles; i++)
	{
		fin = fopen(allFiles[i],"r");
		if(!fin) continue;
		AddBasisForAnAtomFromAFile(atom, fin);
		fclose(fin);
	}
}
/***********************************************************************************/
char** getFileNames(int *nFiles)
{
	FILE* file;
	char* dirMolcas;
	char dirMolcasBasis[BSIZE];
	char** allFiles = NULL;
	int i;

	*nFiles = 0;
	dirMolcas = getenv ("MOLCAS");

	if(!dirMolcas)
	{
	 	fprintf(stderr,"Sorry, I can not locate molcas directory\n,please set MOLCAS system variable\n");
	 	return NULL;
	}
	sprintf(dirMolcasBasis,"%s/basis_library",dirMolcas);

        allFiles = createListFiles(dirMolcasBasis, nFiles);
	/*
	printf("File Names\n");
	for(i=0; i<*nFiles; i++)
		printf("%s\n",allFiles[i]);
		*/
	return allFiles;
}
/**********************************************************************************/
int main(int argc,char* argv[])
{
	Atom atom;
	int nFiles;
	char** allFiles = NULL;
	int i;
	int j;

	allFiles = getFileNames(&nFiles);
	if(nFiles<1) return 1;
	if(argc>=2)
	{
		char symb[BSIZE];
		sprintf(symb,argv[1]);
		if(strlen(symb)>1)
			for(i=1;i<strlen(symb);i++)
				symb[i] =  tolower(argv[1][i]);
		initBasis(&atom, symb);
		getBasisForAnAtom(&atom, nFiles, allFiles);
		printBasisForAnAtom(&atom);
		return 0;
	}
	for(i=0;i<NROW;i++)
		for(j=0;j<NCOL;j++)
		{
			if(strstr(SymbAtoms[i][j],"00"))continue;
			initBasis(&atom, SymbAtoms[i][j]);
			getBasisForAnAtom(&atom, nFiles, allFiles);
			printBasisForAnAtom(&atom);
		}
	return 0;
}