~ubuntu-branches/ubuntu/trusty/gabedit/trusty-proposed

« back to all changes in this revision

Viewing changes to src/Display/OrbitalsNBO.c

  • Committer: Package Import Robot
  • Author(s): Daniel Leidert (dale)
  • Date: 2012-03-20 23:23:56 UTC
  • mfrom: (1.3.19)
  • Revision ID: package-import@ubuntu.com-20120320232356-40hlj06cauc6pm5o
Tags: 2.4.2-1
* New upstream release.
* debian/control: Used wrap-and-sort.
  (Standards-Version): Bumped to 3.9.3.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* OrbitalsNBO.c */
 
2
/**********************************************************************************************************
 
3
Copyright (c) 2002-2012 Abdul-Rahman Allouche. All rights reserved
 
4
 
 
5
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 
6
documentation files (the Gabedit), to deal in the Software without restriction, including without limitation
 
7
the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
 
8
and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 
9
 
 
10
  The above copyright notice and this permission notice shall be included in all copies or substantial portions
 
11
  of the Software.
 
12
 
 
13
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 
14
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 
15
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
 
16
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 
17
DEALINGS IN THE SOFTWARE.
 
18
************************************************************************************************************/
 
19
 
 
20
#include "../../Config.h"
 
21
#include "GlobalOrb.h"
 
22
#include "../Utils/AtomsProp.h"
 
23
#include "../Utils/UtilsInterface.h"
 
24
#include "../Utils/Utils.h"
 
25
#include "../Utils/Zlm.h"
 
26
#include "../Utils/Constants.h"
 
27
#include "../Geometry/GeomGlobal.h"
 
28
#include "GeomDraw.h"
 
29
#include "GLArea.h"
 
30
#include "UtilsOrb.h"
 
31
#include "Basis.h"
 
32
#include "GeomOrbXYZ.h"
 
33
#include "AtomicOrbitals.h"
 
34
#include "StatusOrb.h"
 
35
#include "Basis.h"
 
36
#include "Orbitals.h"
 
37
#include "GeomOrbXYZ.h"
 
38
#include "BondsOrb.h"
 
39
#include "LabelsGL.h"
 
40
 
 
41
/********************************************************************************/
 
42
typedef enum
 
43
{
 
44
  GABEDIT_ORBLOCALTYPE_BOYS=0,
 
45
  GABEDIT_ORBLOCALTYPE_EDMISTON,
 
46
  GABEDIT_ORBLOCALTYPE_PIPEK,
 
47
  GABEDIT_ORBLOCALTYPE_UNKNOWN
 
48
} GabEditOrbLocalType;
 
49
 
 
50
typedef enum
 
51
{
 
52
  GABEDIT_ORBTYPE_ALPHA = 0,
 
53
  GABEDIT_ORBTYPE_BETA,
 
54
  GABEDIT_ORBTYPE_MOLECULAR,
 
55
  GABEDIT_ORBTYPE_MCSCF,
 
56
  GABEDIT_ORBTYPE_EIGENVECTORS,
 
57
  GABEDIT_ORBTYPE_BOYS_ALPHA,
 
58
  GABEDIT_ORBTYPE_BOYS_BETA,
 
59
  GABEDIT_ORBTYPE_BOYS,
 
60
  GABEDIT_ORBTYPE_EDMISTON_ALPHA,
 
61
  GABEDIT_ORBTYPE_EDMISTON_BETA,
 
62
  GABEDIT_ORBTYPE_EDMISTON,
 
63
  GABEDIT_ORBTYPE_PIPEK_ALPHA,
 
64
  GABEDIT_ORBTYPE_PIPEK_BETA,
 
65
  GABEDIT_ORBTYPE_PIPEK,
 
66
} GabEditOrbType;
 
67
/********************************************************************************/
 
68
static gboolean sphericalBasis = TRUE;
 
69
/********************************************************************************/
 
70
static void get_charges_from_nbo_output_file(FILE* file,gint nAtoms)
 
71
{
 
72
/* charges not available in a nbo file. I set it to 0.0 */
 
73
        gint i;
 
74
        for(i=0;i<nAtoms;i++)
 
75
                GeomOrb[i].partialCharge = 0.0;
 
76
}
 
77
/********************************************************************************/
 
78
static gboolean goToLine(FILE* file,char* nextString)
 
79
{
 
80
        static char t[BSIZE];
 
81
        while(!feof(file))
 
82
        {
 
83
                if(!fgets(t,BSIZE,file))break;
 
84
                if (strstr(t,nextString)) return TRUE;
 
85
        }
 
86
        return FALSE;
 
87
}
 
88
/********************************************************************************/
 
89
static void setTitle(FILE* file)
 
90
{
 
91
        gchar t[BSIZE];
 
92
        
 
93
        if(!file)
 
94
        {
 
95
                set_label_title("",0,0);
 
96
                return;
 
97
        }
 
98
        rewind(file);
 
99
        if(fgets(t,BSIZE,file))
 
100
        if(fgets(t,BSIZE,file))
 
101
                set_label_title(t,0,0);
 
102
}
 
103
/********************************************************************************/
 
104
static gboolean read_geomorb_nbo_file_geom(gchar *fileName)
 
105
{
 
106
        gchar *tmp = NULL;
 
107
        FILE *file;
 
108
        gint i;
 
109
        gint k;
 
110
        gint j=0;
 
111
        gint uni=1;
 
112
        gint z = 0;
 
113
        static gchar t[BSIZE];
 
114
        gint nAtoms, nShell, nExp;
 
115
  
 
116
        if ((!fileName) || (strcmp(fileName,"") == 0))
 
117
        {
 
118
                Message(_("Sorry\n No file selected"),_("Error"),TRUE);
 
119
                return FALSE;
 
120
        }
 
121
 
 
122
        file = FOpen(fileName, "rb");
 
123
        if(file == NULL)
 
124
        {
 
125
                Message(_("Sorry\nI cannot open this file"),_("Error"),TRUE);
 
126
                return FALSE;
 
127
        }
 
128
        if(!goToLine(file,"--------")) return FALSE;
 
129
        if(!fgets(t,BSIZE,file)) return FALSE;
 
130
        sscanf(t,"%d %d %d",&nAtoms,&nShell,&nExp);
 
131
        if(!goToLine(file,"--------")) return FALSE;
 
132
        if(nAtoms<1) return FALSE;
 
133
        Dipole.def = FALSE;
 
134
        GeomOrb=g_malloc(nAtoms*sizeof(TypeGeomOrb));
 
135
        uni = 1;
 
136
 
 
137
        tmp = get_name_file(fileName);
 
138
        set_status_label_info(_("File name"),tmp);
 
139
        g_free(tmp);
 
140
        set_status_label_info(_("File type"),"NBO");
 
141
        set_status_label_info(_("Geometry"),_("Reading"));
 
142
 
 
143
        j = 0;
 
144
        for(k=0;k<nAtoms;k++) 
 
145
        {
 
146
                if(!fgets(t,BSIZE,file)) break;
 
147
                sscanf(t,"%d %lf %lf %lf",&z, &GeomOrb[j].C[0], &GeomOrb[j].C[1], &GeomOrb[j].C[2]);
 
148
                if(z<=0) GeomOrb[j].Symb=g_strdup("X");
 
149
                else GeomOrb[j].Symb=get_symbol_using_z(z);
 
150
                if(uni==1) for(i=0;i<3;i++) GeomOrb[j].C[i] *= ANG_TO_BOHR;
 
151
                GeomOrb[j].Prop = prop_atom_get(GeomOrb[j].Symb);
 
152
                GeomOrb[j].nuclearCharge = get_atomic_number_from_symbol(GeomOrb[j].Symb);
 
153
                GeomOrb[j].variable = TRUE;
 
154
                j++;
 
155
                
 
156
        }
 
157
        Ncenters = 0;
 
158
        if(k==nAtoms) Ncenters = nAtoms;
 
159
        if(Ncenters !=0) get_charges_from_nbo_output_file(file,Ncenters);
 
160
        fclose(file);
 
161
        if(Ncenters == 0 )
 
162
        {
 
163
                g_free(GeomOrb);
 
164
                sprintf(t,_("Sorry, I can not read this format from '%s' file\n"),fileName);
 
165
                Message(t,_("Error"),TRUE);
 
166
                set_status_label_info(_("File name"),_("Nothing"));
 
167
                set_status_label_info(_("File type"),_("Nothing"));
 
168
                set_status_label_info(_("Mol. Orb."),_("Nothing"));
 
169
                RebuildGeom = TRUE;
 
170
                return FALSE;
 
171
        }
 
172
        else
 
173
        {
 
174
                /* DefineType();*/
 
175
                gint i;
 
176
                if(Type)
 
177
                for(i=0;i<Ntype;i++)
 
178
                {
 
179
                        if(Type[i].Ao != NULL)
 
180
                        {
 
181
                                for(j=0;j<Type[i].Norb;j++)
 
182
                                {
 
183
                                        if(Type[i].Ao[j].Ex != NULL) g_free(Type[i].Ao[j].Ex);
 
184
                                        if(Type[i].Ao[j].Coef != NULL) g_free(Type[i].Ao[j].Coef);
 
185
                                }
 
186
                                g_free(Type[i].Ao);
 
187
                        }
 
188
                }
 
189
                if(Type) g_free(Type);
 
190
                Ntype =Ncenters;
 
191
                Type = g_malloc(Ntype*sizeof(TYPE));
 
192
                for(i=0;i<Ncenters;i++) 
 
193
                {
 
194
                        GeomOrb[i].NumType = i;
 
195
                        Type[i].Symb=g_strdup(GeomOrb[i].Symb);
 
196
                        Type[i].N=GetNelectrons(GeomOrb[i].Symb);
 
197
                        Type[i].Norb = 0;
 
198
                        Type[i].Ao = NULL;
 
199
                }
 
200
                buildBondsOrb();
 
201
                RebuildGeom = TRUE;
 
202
                return TRUE;
 
203
        }
 
204
        return TRUE;
 
205
}
 
206
/**********************************************/
 
207
static void DefineNBOSphericalBasis()
 
208
{
 
209
        gint i,j,k;
 
210
        gint c;
 
211
        gint kl;
 
212
        gint L,M;
 
213
        CGTF *temp;
 
214
        Zlm Stemp;
 
215
        gint N,Nc,n;
 
216
        gint inc;
 
217
        gint  klbeg;
 
218
        gint  klend;
 
219
        gint  klinc;
 
220
 
 
221
 
 
222
        NOrb = 0;
 
223
        for(i=0;i<Ncenters;i++)
 
224
        {
 
225
                for(j=0;j<Type[GeomOrb[i].NumType].Norb;j++)
 
226
                {
 
227
                        L=Type[GeomOrb[i].NumType].Ao[j].L;
 
228
                        NOrb += 2*L+1;
 
229
                }
 
230
        }
 
231
 
 
232
        temp  = g_malloc(NOrb*sizeof(CGTF));
 
233
 
 
234
        k=-1;
 
235
        for(i=0;i<Ncenters;i++)
 
236
        for(j=0;j<Type[GeomOrb[i].NumType].Norb;j++)
 
237
        {
 
238
                L =Type[GeomOrb[i].NumType].Ao[j].L;
 
239
                /* Debug("L=%d \n",L);*/
 
240
 
 
241
                /*Debug("L =%d \n",L);*/
 
242
                if(L==1)
 
243
                {
 
244
                        klbeg = 0;
 
245
                        klend = L;
 
246
                        klinc = +1;
 
247
                }
 
248
                else
 
249
                {
 
250
                        klbeg = 0;
 
251
                        klend = L;
 
252
                        klinc = +1;
 
253
                }
 
254
                for(kl = klbeg;(klbeg == 0 && kl<=klend) || (klbeg == L && kl>=klend);kl +=klinc)
 
255
                {
 
256
                        if(kl!=0) inc = 2*kl;   
 
257
                        else inc = 1;
 
258
                        for(M=kl;M>=-kl;M -=inc)
 
259
                        {
 
260
                                /*Debug("L =%d kl=%d M=%d \n",L,kl,M);*/
 
261
                                k++;
 
262
                                Stemp =  getZlm(L,M);
 
263
 
 
264
                                temp[k].numberOfFunctions=Stemp.numberOfCoefficients*Type[GeomOrb[i].NumType].Ao[j].N;
 
265
                                temp[k].NumCenter=i;
 
266
                                /* Debug("M=%d N=%d\n",M,temp[k].N);*/
 
267
                                temp[k].Gtf =g_malloc(temp[k].numberOfFunctions*sizeof(GTF));
 
268
                                Nc=-1;
 
269
                                for(N=0;N<Type[GeomOrb[i].NumType].Ao[j].N;N++)
 
270
                                for(n=0;n<Stemp.numberOfCoefficients;n++)
 
271
                                {
 
272
                                        Nc++;
 
273
                                        temp[k].Gtf[Nc].Ex   = Type[GeomOrb[i].NumType].Ao[j].Ex[N];
 
274
                                        temp[k].Gtf[Nc].Coef = Type[GeomOrb[i].NumType].Ao[j].Coef[N]*Stemp.lxyz[n].Coef;
 
275
                                        for(c=0;c<3;c++)
 
276
                                        {
 
277
                                                temp[k].Gtf[Nc].C[c] = GeomOrb[i].C[c];
 
278
                                                temp[k].Gtf[Nc].l[c] = Stemp.lxyz[n].l[c];
 
279
                                        }
 
280
                                }
 
281
                                if(L==0) break;
 
282
                        }
 
283
                        if(L==0) break;
 
284
              }
 
285
        }
 
286
         for(i=0;i<NAOrb;i++) g_free(AOrb[i].Gtf);
 
287
        g_free(AOrb);
 
288
        NAOrb = NOrb;
 
289
        AOrb = temp;
 
290
        if(SAOrb) g_free(SAOrb);
 
291
        SAOrb = NULL;
 
292
        DefineAtomicNumOrb();
 
293
}
 
294
 
 
295
/********************************************************************************/
 
296
static void DefineNBOCartBasis()
 
297
{
 
298
 gint i,j,k,n;
 
299
 gint l1,l2,l3;
 
300
 gint L;
 
301
 gint *l[3]={NULL,NULL,NULL};
 
302
 gint m;
 
303
 
 
304
 NAOrb = 0;
 
305
 for(i=0;i<Ncenters;i++)
 
306
 {
 
307
         for(j=0;j<Type[GeomOrb[i].NumType].Norb;j++)
 
308
         {
 
309
                L=Type[GeomOrb[i].NumType].Ao[j].L;
 
310
                NAOrb += (L+1)*(L+2)/2;
 
311
         }
 
312
 }
 
313
 
 
314
 AOrb = g_malloc(NAOrb*sizeof(CGTF));
 
315
 if(SAOrb) g_free(SAOrb);
 
316
 SAOrb = NULL;
 
317
 
 
318
 k=-1;
 
319
 for(i=0;i<Ncenters;i++)
 
320
         for(j=0;j<Type[GeomOrb[i].NumType].Norb;j++)
 
321
 {
 
322
        L = Type[GeomOrb[i].NumType].Ao[j].L;
 
323
 
 
324
        for(m=0;m<3;m++)
 
325
        {
 
326
                if(l[m])
 
327
                   g_free(l[m]);
 
328
                l[m] = g_malloc((L+1)*(L+2)/2*sizeof(gint));
 
329
        }
 
330
        switch(L)
 
331
        {
 
332
          case 1 :
 
333
                 m=0;
 
334
                 l[0][m] = 1;l[1][m] = 0;l[2][m] = 0; /* X */
 
335
                 m++;
 
336
                 l[0][m] = 0;l[1][m] = 1;l[2][m] = 0; /* Y */
 
337
                 m++;
 
338
                 l[0][m] = 0;l[1][m] = 0;l[2][m] = 1; /* Z */
 
339
                 break;
 
340
          case 2 :
 
341
                 m=0;
 
342
                 l[0][m] = 2;l[1][m] = 0;l[2][m] = 0; /* XX */
 
343
                 m++;
 
344
                 l[0][m] = 0;l[1][m] = 2;l[2][m] = 0; /* YY */
 
345
                 m++;
 
346
                 l[0][m] = 0;l[1][m] = 0;l[2][m] = 2; /* ZZ */
 
347
                 m++;
 
348
                 l[0][m] = 1;l[1][m] = 1;l[2][m] = 0; /* XY */
 
349
                 m++;
 
350
                 l[0][m] = 1;l[1][m] = 0;l[2][m] = 1; /* XZ */
 
351
                 m++;
 
352
                 l[0][m] = 0;l[1][m] = 1;l[2][m] = 1; /* YZ */
 
353
                 break;
 
354
          case 3 :
 
355
                 m=0;
 
356
                 l[0][m] = 3;l[1][m] = 0;l[2][m] = 0; /* XXX */
 
357
                 m++;
 
358
                 l[0][m] = 0;l[1][m] = 3;l[2][m] = 0; /* YYY */
 
359
                 m++;
 
360
                 l[0][m] = 0;l[1][m] = 0;l[2][m] = 3; /* ZZZ */
 
361
                 m++;
 
362
                 l[0][m] = 2;l[1][m] = 1;l[2][m] = 0; /* XXY */
 
363
                 m++;
 
364
                 l[0][m] = 2;l[1][m] = 0;l[2][m] = 1; /* XXZ */
 
365
                 m++;
 
366
                 l[0][m] = 1;l[1][m] = 2;l[2][m] = 0; /* YYX */
 
367
                 m++;
 
368
                 l[0][m] = 0;l[1][m] = 2;l[2][m] = 1; /* YYZ */
 
369
                 m++;
 
370
                 l[0][m] = 1;l[1][m] = 0;l[2][m] = 2; /* ZZX */
 
371
                 m++;
 
372
                 l[0][m] = 0;l[1][m] = 1;l[2][m] = 2; /* ZZY */
 
373
                 m++;
 
374
                 l[0][m] = 1;l[1][m] = 1;l[2][m] = 1; /* XYZ */
 
375
                 break;
 
376
          case 4 :
 
377
                 m=0; l[0][m] = 4;l[1][m] = 0;l[2][m] = 0; /* XXXX */
 
378
                 m++; l[0][m] = 0;l[1][m] = 4;l[2][m] = 0; /* YYYY */
 
379
                 m++; l[0][m] = 0;l[1][m] = 0;l[2][m] = 4; /* ZZZZ */
 
380
                 m++; l[0][m] = 3;l[1][m] = 1;l[2][m] = 0; /* XXXY */
 
381
                 m++; l[0][m] = 3;l[1][m] = 0;l[2][m] = 1; /* XXXZ */
 
382
                 m++; l[0][m] = 1;l[1][m] = 3;l[2][m] = 0; /* YYYX */
 
383
                 m++; l[0][m] = 0;l[1][m] = 3;l[2][m] = 1; /* YYYZ */
 
384
                 m++; l[0][m] = 1;l[1][m] = 0;l[2][m] = 3; /* ZZZX */
 
385
                 m++; l[0][m] = 0;l[1][m] = 1;l[2][m] = 3; /* ZZZY */
 
386
                 m++; l[0][m] = 2;l[1][m] = 2;l[2][m] = 0; /* XXYY */
 
387
                 m++; l[0][m] = 2;l[1][m] = 0;l[2][m] = 2; /* XXZZ */
 
388
                 m++; l[0][m] = 0;l[1][m] = 2;l[2][m] = 2; /* YYZZ */
 
389
                 m++; l[0][m] = 2;l[1][m] = 1;l[2][m] = 1; /* XXYZ */
 
390
                 m++; l[0][m] = 1;l[1][m] = 2;l[2][m] = 1; /* YYXZ */
 
391
                 m++; l[0][m] = 1;l[1][m] = 1;l[2][m] = 2; /* ZZXY */
 
392
                 break;
 
393
          default :
 
394
                m=0;
 
395
                for(l3=Type[GeomOrb[i].NumType].Ao[j].L;l3>=0;l3--)
 
396
                        for(l2=Type[GeomOrb[i].NumType].Ao[j].L-l3;l2>=0;l2--)
 
397
                {
 
398
                        l1 = Type[GeomOrb[i].NumType].Ao[j].L-l2-l3;
 
399
                        l[0][m] = l1;
 
400
                        l[1][m] = l2;
 
401
                        l[2][m] = l3;
 
402
                        m++;
 
403
                }
 
404
        }
 
405
                for(m=0;m<(L+1)*(L+2)/2;m++)
 
406
                {
 
407
                        l1 = l[0][m];
 
408
                        l2 = l[1][m];
 
409
                        l3 = l[2][m];
 
410
                        k++;
 
411
                        AOrb[k].numberOfFunctions=Type[GeomOrb[i].NumType].Ao[j].N;
 
412
                        AOrb[k].NumCenter = i;
 
413
                        AOrb[k].Gtf =g_malloc(AOrb[k].numberOfFunctions*sizeof(GTF));
 
414
                        for(n=0;n<AOrb[k].numberOfFunctions;n++)
 
415
                        {
 
416
                                AOrb[k].Gtf[n].Ex   = Type[GeomOrb[i].NumType].Ao[j].Ex[n];
 
417
                                AOrb[k].Gtf[n].Coef = Type[GeomOrb[i].NumType].Ao[j].Coef[n];
 
418
                                AOrb[k].Gtf[n].C[0] = GeomOrb[i].C[0];
 
419
                                AOrb[k].Gtf[n].C[1] = GeomOrb[i].C[1];
 
420
                                AOrb[k].Gtf[n].C[2] = GeomOrb[i].C[2];
 
421
                                AOrb[k].Gtf[n].l[0] = l1;
 
422
                                AOrb[k].Gtf[n].l[1] = l2;
 
423
                                AOrb[k].Gtf[n].l[2] = l3;
 
424
                        }
 
425
         
 
426
                }
 
427
}
 
428
 
 
429
NOrb = NAOrb;
 
430
DefineAtomicNumOrb();
 
431
/* DefineNorb();*/
 
432
}
 
433
/********************************************************************************/
 
434
static gboolean read_basis_from_a_nbo_output_file(gchar *fileName)
 
435
{
 
436
        static gchar t[BSIZE];
 
437
        FILE *file;
 
438
        gint nAtoms = 0, nShell = 0, nExp = 0;
 
439
        gdouble* expo = NULL;
 
440
        gdouble** coefs = NULL;
 
441
        gint* numCenters = NULL;
 
442
        gint* nOrbs = NULL;
 
443
        gint** numTypes = NULL;
 
444
        gint* iPointers = NULL;
 
445
        gint* nGauss = NULL; 
 
446
        gint i;
 
447
        gint j;
 
448
        gint k;
 
449
        gint is;
 
450
        gint ie;
 
451
        gint jj;
 
452
        gint lmax = 0;
 
453
 
 
454
        
 
455
        if ((!fileName) || (strcmp(fileName,"") == 0))
 
456
        {
 
457
                Message(_("Sorry No file selected\n"),_("Error"),TRUE);
 
458
                return FALSE;
 
459
        }
 
460
 
 
461
        file = FOpen(fileName, "rb");
 
462
        if(file == NULL)
 
463
        {
 
464
                gchar buffer[BSIZE];
 
465
                sprintf(buffer,_("Sorry, I can not open '%s' file\n"),fileName);
 
466
                Message(buffer,_("Error"),TRUE);
 
467
                return FALSE;
 
468
        }
 
469
        if(!goToLine(file,"--------")) return FALSE;
 
470
        if(!fgets(t,BSIZE,file)) return FALSE;
 
471
        sscanf(t,"%d %d %d",&nAtoms,&nShell,&nExp);
 
472
        if(nAtoms!=Ncenters || nAtoms <= 0 || nShell <= 0 || nExp <= 0) return FALSE;
 
473
        if(!goToLine(file,"--------")) return FALSE;
 
474
        if(!goToLine(file,"--------")) return FALSE;
 
475
 
 
476
        numCenters = g_malloc(nShell*sizeof(gint));
 
477
        nOrbs = g_malloc(nShell*sizeof(gint));
 
478
        numTypes = g_malloc(nShell*sizeof(gint*));
 
479
        for(j=0;j<nShell;j++) numTypes[j] = g_malloc(nShell*sizeof(gint));
 
480
        iPointers = g_malloc(nShell*sizeof(gint));
 
481
        nGauss = g_malloc(nShell*sizeof(gint));
 
482
 
 
483
        lmax = 0;
 
484
        for(is = 0; is<nShell; is++)
 
485
        {
 
486
                //fgets(t,BSIZE,file);
 
487
                //printf("%s\n",t);
 
488
                if(1!=fscanf(file,"%d",&numCenters[is])) break;
 
489
                numCenters[is]--;
 
490
                if(1!=fscanf(file,"%d",&nOrbs[is])) break;
 
491
                if(1!=fscanf(file,"%d",&iPointers[is])) break;
 
492
                if(1!=fscanf(file,"%d",&nGauss[is])) break;
 
493
                for(k=0;k<nOrbs[is];k++)
 
494
                {
 
495
                        if(1!=fscanf(file,"%d",&numTypes[is][k]))break;
 
496
                        gint l = numTypes[is][k]/100;
 
497
                        if(lmax<l) lmax = l;
 
498
                }
 
499
                if(k!=nOrbs[is]) break;
 
500
        }
 
501
        if(is!=nShell) 
 
502
        {
 
503
                if(numCenters) g_free(numCenters);
 
504
                if(nOrbs) g_free(nOrbs);
 
505
                if(numTypes) for(j=0;j<nShell;j++) if(numTypes[j]) g_free(numTypes[j]);
 
506
                if(numTypes) g_free(numTypes);
 
507
                if(iPointers) g_free(iPointers);
 
508
                if(nGauss) g_free(nGauss);
 
509
                return FALSE;
 
510
        }
 
511
        if(!goToLine(file,"--------"))
 
512
        {
 
513
                if(numCenters) g_free(numCenters);
 
514
                if(nOrbs) g_free(nOrbs);
 
515
                if(numTypes) for(j=0;j<nShell;j++) if(numTypes[j]) g_free(numTypes[j]);
 
516
                if(numTypes) g_free(numTypes);
 
517
                if(iPointers) g_free(iPointers);
 
518
                if(nGauss) g_free(nGauss);
 
519
                return FALSE;
 
520
        }
 
521
 
 
522
        expo = g_malloc(nExp*sizeof(gdouble));
 
523
        coefs = g_malloc((lmax+1)*sizeof(gdouble*));
 
524
        for(i=0;i<=lmax;i++) 
 
525
        {
 
526
                coefs[i] = g_malloc(nExp*sizeof(gdouble));
 
527
                for(j=0;j<nExp;j++)  coefs[i][j] = 0.0;
 
528
        }
 
529
        for(ie = 0; ie<nExp; ie++)
 
530
        {
 
531
                fscanf(file,"%lf",&expo[ie]);
 
532
        }
 
533
        if(!fgets(t,BSIZE,file))
 
534
        {
 
535
                if(numCenters) g_free(numCenters);
 
536
                if(nOrbs) g_free(nOrbs);
 
537
                if(numTypes) for(j=0;j<nShell;j++) if(numTypes[j]) g_free(numTypes[j]);
 
538
                if(numTypes) g_free(numTypes);
 
539
                if(iPointers) g_free(iPointers);
 
540
                if(nGauss) g_free(nGauss);
 
541
                if(expo) g_free(expo);
 
542
                if(coefs)
 
543
                {
 
544
                        for(i=0;i<=lmax;i++) if(coefs[i]) g_free(coefs[i]);
 
545
                        g_free(coefs);
 
546
                }
 
547
                return FALSE;
 
548
        }
 
549
        
 
550
        for(i=0;i<=lmax;i++) 
 
551
        {
 
552
                for(ie = 0; ie<nExp; ie++)
 
553
                        if(1!=fscanf(file,"%lf",&coefs[i][ie])) break;
 
554
                if(!fgets(t,BSIZE,file)) break; /* f orb is not always available */
 
555
        }
 
556
        for(i=0;i<Ncenters;i++) 
 
557
        {
 
558
                Type[i].Norb = 0;
 
559
                Type[i].Ao = NULL;
 
560
        }
 
561
        for(is = 0; is<nShell; is++)
 
562
        {
 
563
                i = numCenters[is];
 
564
                Type[i].Norb++;
 
565
 
 
566
                for(k=1;k<nOrbs[is];k++) 
 
567
                        if(numTypes[is][k]/100 != numTypes[is][k-1]/100) Type[i].Norb++;
 
568
        }
 
569
 
 
570
        for(i=0;i<Ncenters;i++) 
 
571
        {
 
572
                Type[i].Ao=g_malloc(Type[i].Norb*sizeof(AO));
 
573
                for(j=0;j< Type[i].Norb;j++)
 
574
                {
 
575
                        Type[i].Ao[j].Ex = NULL;
 
576
                        Type[i].Ao[j].Coef = NULL;
 
577
                }
 
578
                j = 0;
 
579
                for(is = 0; is<nShell; is++)
 
580
                {
 
581
                        gint ncont = nGauss[is];
 
582
                        if(i != numCenters[is]) continue;
 
583
                        for(k=0;k<nOrbs[is];k++) 
 
584
                        {
 
585
                                gint l = numTypes[is][k]/100;
 
586
                                if(k>0 && numTypes[is][k]/100 == numTypes[is][k-1]/100) continue;
 
587
                                Type[i].Ao[j].L = l;
 
588
                                Type[i].Ao[j].N = ncont;
 
589
                                Type[i].Ao[j].Ex=g_malloc(Type[i].Ao[j].N*sizeof(gdouble));
 
590
                                Type[i].Ao[j].Coef=g_malloc(Type[i].Ao[j].N*sizeof(gdouble));
 
591
                                for(jj=0;jj<Type[i].Ao[j].N;jj++)
 
592
                                {
 
593
                                        gint jjP = iPointers[is] - 1 + jj;
 
594
                                        Type[i].Ao[j].Ex[jj] = expo[jjP];
 
595
                                        Type[i].Ao[j].Coef[jj] = coefs[l][jjP];
 
596
                                }
 
597
                                j++;
 
598
                        }
 
599
                }
 
600
 
 
601
        }
 
602
/* free tables */
 
603
        if(numCenters) g_free(numCenters);
 
604
        if(nOrbs) g_free(nOrbs);
 
605
        if(expo) g_free(expo);
 
606
        if(coefs)
 
607
        {
 
608
                for(i=0;i<=lmax;i++) if(coefs[i]) g_free(coefs[i]);
 
609
                g_free(coefs);
 
610
        }
 
611
        
 
612
        if(numTypes)
 
613
        for(j=0;j<nShell;j++) if(numTypes[j]) g_free(numTypes[j]);
 
614
        if(numTypes) g_free(numTypes);
 
615
        if(iPointers) g_free(iPointers);
 
616
        if(nGauss) g_free(nGauss);
 
617
 
 
618
        fclose(file);
 
619
        return TRUE;
 
620
}
 
621
/********************************************************************************/
 
622
/* typeobr = ALPHA or BETA */
 
623
static gboolean read_orbitals_in_nbo_file_alpha_or_beta(gchar *fileName,gint nAlpha, gint nBeta, gchar* typeOrb)
 
624
{
 
625
        FILE *file;
 
626
        gint i;
 
627
        gint k;
 
628
        gint j;
 
629
        gdouble **CoefOrbitals;
 
630
        gdouble *EnerOrbitals;
 
631
        gchar **SymOrbitals;
 
632
        gboolean om = FALSE;
 
633
        gdouble dum;
 
634
        
 
635
        if ((!fileName) || (strcmp(fileName,"") == 0))
 
636
        {
 
637
                Message(_("Sorry No file selected\n"),_("Error"),TRUE);
 
638
                return FALSE;
 
639
        }
 
640
 
 
641
        file = FOpen(fileName, "rb");
 
642
        if(file ==NULL)
 
643
        {
 
644
                gchar buffer[BSIZE];
 
645
                sprintf(buffer,_("Sorry, I can not open '%s' file\n"),fileName);
 
646
                Message(buffer,_("Error"),TRUE);
 
647
                return FALSE;
 
648
        }
 
649
        {
 
650
                gchar t[BSIZE];
 
651
                fgets(t,BSIZE,file);
 
652
                fgets(t,BSIZE,file);
 
653
                if(strstr(t," MOs ")) om = TRUE;
 
654
        }
 
655
 
 
656
        if(!goToLine(file,"--------")) return FALSE;
 
657
        if(!goToLine(file,typeOrb)) return FALSE;
 
658
 
 
659
  
 
660
        CoefOrbitals = CreateTable2(NOrb);
 
661
        EnerOrbitals = g_malloc(NOrb*sizeof(gdouble));
 
662
        SymOrbitals = g_malloc(NOrb*sizeof(gchar*));
 
663
 
 
664
        for(j=0;j<NOrb;j++)
 
665
        {
 
666
                EnerOrbitals[j]=0.0;
 
667
                for(i=0;i<NOrb;i++)
 
668
                {
 
669
                        if(1!=fscanf(file,"%lf",&dum))break;
 
670
                        if(om) CoefOrbitals[i][j] = dum;
 
671
                        else CoefOrbitals[j][i] = dum;
 
672
                }
 
673
                if(i!=NOrb)
 
674
                {
 
675
                        for(k=j;k<NOrb;k++) SymOrbitals[k] = g_strdup("DELETED");
 
676
                        break;
 
677
                }
 
678
                SymOrbitals[j] = g_strdup("UNK");
 
679
        }
 
680
        /*
 
681
        printf("norb read=%d\n",j);
 
682
        */
 
683
        if(strstr(typeOrb,"ALPHA"))
 
684
        {
 
685
        CoefAlphaOrbitals = CoefOrbitals;
 
686
        EnerAlphaOrbitals = EnerOrbitals;
 
687
        SymAlphaOrbitals = SymOrbitals;
 
688
        OccAlphaOrbitals = g_malloc(NOrb*sizeof(gdouble));
 
689
        for(i=0;i<nAlpha;i++) OccAlphaOrbitals[i] = 1.0;
 
690
        for(i=nAlpha;i<NOrb;i++) OccAlphaOrbitals[i] = 0.0;
 
691
        }
 
692
        else{
 
693
        CoefBetaOrbitals = CoefOrbitals;
 
694
        EnerBetaOrbitals = EnerOrbitals;
 
695
        OccBetaOrbitals = OccAlphaOrbitals;
 
696
        SymBetaOrbitals = SymOrbitals;
 
697
        NAlphaOcc = nAlpha;
 
698
        NBetaOcc = nBeta;
 
699
        NAlphaOrb = NOrb;
 
700
        NBetaOrb = NOrb;
 
701
        }
 
702
        setTitle(file);
 
703
        fclose(file);
 
704
 
 
705
        return TRUE;
 
706
}
 
707
/********************************************************************************/
 
708
static gboolean read_orbitals_in_nbo_file_all(gchar *fileName,gint nAlpha, gint nBeta)
 
709
{
 
710
        FILE *file;
 
711
        gint i;
 
712
        gint k;
 
713
        gint j;
 
714
        gdouble **CoefOrbitals;
 
715
        gdouble *EnerOrbitals;
 
716
        gchar **SymOrbitals;
 
717
        gboolean om = FALSE;
 
718
        gdouble dum;
 
719
        
 
720
        if ((!fileName) || (strcmp(fileName,"") == 0))
 
721
        {
 
722
                Message(_("Sorry No file selected\n"),_("Error"),TRUE);
 
723
                return FALSE;
 
724
        }
 
725
 
 
726
        file = FOpen(fileName, "rb");
 
727
        if(file ==NULL)
 
728
        {
 
729
                gchar buffer[BSIZE];
 
730
                sprintf(buffer,_("Sorry, I can not open '%s' file\n"),fileName);
 
731
                Message(buffer,_("Error"),TRUE);
 
732
                return FALSE;
 
733
        }
 
734
        {
 
735
                gchar t[BSIZE];
 
736
                fgets(t,BSIZE,file);
 
737
                fgets(t,BSIZE,file);
 
738
                if(strstr(t," MOs ")) om = TRUE;
 
739
        }
 
740
        if(!goToLine(file,"--------")) return FALSE;
 
741
 
 
742
  
 
743
        CoefOrbitals = CreateTable2(NOrb);
 
744
        EnerOrbitals = g_malloc(NOrb*sizeof(gdouble));
 
745
        SymOrbitals = g_malloc(NOrb*sizeof(gchar*));
 
746
 
 
747
        for(j=0;j<NOrb;j++)
 
748
        {
 
749
                EnerOrbitals[j]=0.0;
 
750
                for(i=0;i<NOrb;i++)
 
751
                {
 
752
                        if(1!=fscanf(file,"%lf",&dum))break;
 
753
                        if(om) CoefOrbitals[i][j] = dum;
 
754
                        else CoefOrbitals[j][i] = dum;
 
755
                }
 
756
                if(i!=NOrb)
 
757
                {
 
758
                        for(k=j;k<NOrb;k++) SymOrbitals[k] = g_strdup("DELETED");
 
759
                        break;
 
760
                }
 
761
                SymOrbitals[j] = g_strdup("UNK");
 
762
        }
 
763
        /*
 
764
        printf("norb read=%d\n",j);
 
765
        */
 
766
        CoefAlphaOrbitals = CoefOrbitals;
 
767
        EnerAlphaOrbitals = EnerOrbitals;
 
768
        SymAlphaOrbitals = SymOrbitals;
 
769
        OccAlphaOrbitals = g_malloc(NOrb*sizeof(gdouble));
 
770
        nAlpha = nBeta;
 
771
        for(i=0;i<nAlpha;i++) OccAlphaOrbitals[i] = 1.0;
 
772
        for(i=nAlpha;i<NOrb;i++) OccAlphaOrbitals[i] = 0.0;
 
773
 
 
774
        CoefBetaOrbitals = CoefOrbitals;
 
775
        EnerBetaOrbitals = EnerOrbitals;
 
776
        OccBetaOrbitals = OccAlphaOrbitals;
 
777
        SymBetaOrbitals = SymOrbitals;
 
778
        NAlphaOcc = nAlpha;
 
779
        NBetaOcc = nBeta;
 
780
        NAlphaOrb = NOrb;
 
781
        NBetaOrb = NOrb;
 
782
        setTitle(file);
 
783
        fclose(file);
 
784
 
 
785
        return TRUE;
 
786
}
 
787
/********************************************************************************/
 
788
static gboolean read_orbitals_in_nbo_file(gchar *fileName,gint nAlpha, gint nBeta)
 
789
{
 
790
        if(read_orbitals_in_nbo_file_alpha_or_beta(fileName, nAlpha,  nBeta, "ALPHA"))
 
791
        {
 
792
                return read_orbitals_in_nbo_file_alpha_or_beta(fileName, nAlpha,  nBeta, "BETA");
 
793
        }
 
794
        else
 
795
                return read_orbitals_in_nbo_file_all(fileName, nAlpha,  nBeta);
 
796
 
 
797
        return TRUE;
 
798
}
 
799
/********************************************************************************/
 
800
void read_nbo_orbitals(gchar* fileName)
 
801
{
 
802
        gint typefile;
 
803
        /* gint typebasis=1;*/ /* NBO print OM in cartezian presentation even ISPHER=0 or 1 or -1 */
 
804
        gchar *t = NULL;
 
805
        gint i;
 
806
        gint nAlpha;
 
807
        gint nBeta;
 
808
        gint typebasis = 0;
 
809
        gboolean Ok;
 
810
        gchar* fileName31 = NULL;
 
811
 
 
812
 
 
813
        typefile =get_type_file_orb(fileName);
 
814
        if(typefile==GABEDIT_TYPEFILE_UNKNOWN) return;
 
815
 
 
816
 
 
817
        if(typefile != GABEDIT_TYPEFILE_NBO)
 
818
        {
 
819
                gchar buffer[BSIZE];
 
820
                sprintf(buffer,_("Sorry, I can not read this format from '%s' file\n"),fileName);
 
821
                Message(buffer,_("Error"),TRUE);
 
822
                return ;
 
823
        }
 
824
        fileName31 = g_strdup_printf("%s.31",get_suffix_name_file(fileName));
 
825
 
 
826
        free_data_all();
 
827
        t = get_name_file(fileName31);
 
828
        set_status_label_info(_("File name"),t);
 
829
        g_free(t);
 
830
        set_status_label_info(_("File type"),"NBO");
 
831
        set_status_label_info(_("Mol. Orb."),_("Reading"));
 
832
        
 
833
        free_orbitals();        
 
834
 
 
835
        /* typebasis =get_type_basis_in_nbo_file(fileName);
 
836
        if(typebasis == -1)
 
837
        {
 
838
                gchar buffer[BSIZE];
 
839
                sprintf(buffer,
 
840
                                "Sorry, Gabedit does not support spherical basis with contaminant cartezian function\n\n"
 
841
                                "Use ISPHER=-1 or ISPHER=1 in CONTROL block"
 
842
                       );
 
843
                Message(buffer,_("Error"),TRUE);
 
844
                set_status_label_info(_("File name"),_("Nothing"));
 
845
                set_status_label_info(_("File type"),_("Nothing"));
 
846
                set_status_label_info(_("Mol. Orb."),_("Nothing"));
 
847
                return;
 
848
        }
 
849
        */
 
850
 
 
851
        if(!read_geomorb_nbo_file_geom(fileName31))
 
852
        {
 
853
                free_geometry();
 
854
                set_status_label_info(_("File name"),_("Nothing"));
 
855
                set_status_label_info(_("File type"),_("Nothing"));
 
856
                set_status_label_info(_("Mol. Orb."),_("Nothing"));
 
857
                return;
 
858
        }
 
859
        set_status_label_info(_("Geometry"),_("Ok"));
 
860
        glarea_rafresh(GLArea); /* for geometry*/
 
861
        init_atomic_orbitals();
 
862
        if(!read_basis_from_a_nbo_output_file(fileName31))
 
863
        {
 
864
                gchar buffer[BSIZE];
 
865
                sprintf(buffer,_("Sorry, I cannot read basis from '%s' file\n"),fileName31);
 
866
                Message(buffer,_("Error"),TRUE);
 
867
                if(GeomOrb)
 
868
                {
 
869
                        init_atomic_orbitals();
 
870
                        for(i=0;i<Ncenters;i++) GeomOrb[i].Prop = prop_atom_get("H");
 
871
                        free_geometry();
 
872
                }
 
873
                set_status_label_info(_("File name"),_("Nothing"));
 
874
                set_status_label_info(_("File type"),_("Nothing"));
 
875
                set_status_label_info(_("Mol. Orb."),_("Nothing"));
 
876
                return;
 
877
        }
 
878
 
 
879
        g_free(fileName31);
 
880
 
 
881
        t = get_name_file(fileName);
 
882
        set_status_label_info(_("File name"),t);
 
883
        g_free(t);
 
884
        set_status_label_info(_("Mol. Orb."),_("Reading"));
 
885
        InitializeAll();
 
886
        buildBondsOrb();
 
887
        RebuildGeom = TRUE;
 
888
        reset_grid_limits();
 
889
        init_atomic_orbitals();
 
890
 
 
891
 
 
892
        if(typebasis == 0)
 
893
        {
 
894
                DefineNBOSphericalBasis();
 
895
                sphericalBasis = TRUE;
 
896
        }
 
897
        else
 
898
        {
 
899
                DefineNBOCartBasis();
 
900
                sphericalBasis = FALSE;
 
901
        }
 
902
        
 
903
        /* 
 
904
        printf("Not normalized basis\n");
 
905
        PrintAllBasis();
 
906
        */
 
907
        /* NormaliseAllBasis();*/
 
908
        NormaliseAllNoRadBasis();
 
909
        /*
 
910
        printf("Normalized basis\n");
 
911
        PrintAllBasis();
 
912
        */
 
913
        DefineNOccs();
 
914
 
 
915
        nAlpha = NOrb;
 
916
        nBeta = NOrb;
 
917
        nAlpha = NAlphaOcc;
 
918
        nBeta = NBetaOcc;
 
919
 
 
920
        /*
 
921
        printf("Number of ALPHA occ = %d\n",nAlpha);
 
922
        printf("Number of BETA  occ = %d\n",nBeta);
 
923
        printf("NOrb = %d\n",NOrb);
 
924
        */
 
925
        Ok = read_orbitals_in_nbo_file(fileName,nAlpha,nBeta);
 
926
 
 
927
        if(Ok)
 
928
        {
 
929
                /*PrintAllOrb(CoefAlphaOrbitals);*/
 
930
                set_status_label_info(_("Mol. Orb."),_("Ok"));
 
931
                glarea_rafresh(GLArea); /* for geometry*/
 
932
                NumSelOrb = NAlphaOcc-1;
 
933
                create_list_orbitals();
 
934
        }
 
935
        else
 
936
        {
 
937
                free_orbitals();        
 
938
                set_status_label_info(_("File name"),_("Nothing"));
 
939
                set_status_label_info(_("File type"),_("Nothing"));
 
940
                set_status_label_info(_("Mol. Orb."),_("Nothing"));
 
941
        }
 
942
 
 
943