File: drawel.c

package info (click to toggle)
viewmol 2.3-5
  • links: PTS
  • area: main
  • in suites: woody
  • size: 9,424 kB
  • ctags: 2,239
  • sloc: ansic: 29,098; sh: 909; makefile: 513; python: 238
file content (378 lines) | stat: -rw-r--r-- 13,949 bytes parent folder | download
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*******************************************************************************
*                                                                              *
*                                   Viewmol                                    *
*                                                                              *
*                               D R A W E L . C                                *
*                                                                              *
*                 Copyright (c) Joerg-R. Hill, December 2000                   *
*                                                                              *
********************************************************************************
*
* $Id: drawel.c,v 1.5 2000/12/10 15:05:02 jrh Exp $
* $Log: drawel.c,v $
* Revision 1.5  2000/12/10 15:05:02  jrh
* Release 2.3
*
* Revision 1.4  1999/05/24 01:25:12  jrh
* Release 2.2.1
*
* Revision 1.3  1999/02/07 21:47:09  jrh
* Release 2.2
*
* Revision 1.2  1998/01/26 00:47:29  jrh
* Release 2.1
*
* Revision 1.1  1996/12/10  18:40:27  jrh
* Initial revision
*
*/
#include<math.h>
#include<stdio.h>
#include<stdlib.h>
#include<X11/Intrinsic.h>
#include<X11/StringDefs.h>
#include<X11/keysym.h>
#include<Xm/Xm.h>
#include<GL/gl.h>
#include "viewmol.h"

void updateMOs(int);

extern double makeTics(double, double, double *, char *);
extern void setWindowColor(int, Pixel, const float*);
extern void (*drawBegin)(GLenum), (*drawEnd)(void), (*drawVertex2d)(double, double);
extern void (*drawString)(char *, double, double, double, double, GLuint);
extern void (*drawLineStipple)(GLint, GLushort);
extern void (*drawLineWidth)(GLfloat);
extern void (*drawDisable)(GLenum);
extern void printDialog(Widget, caddr_t, XmAnyCallbackStruct *);
extern void pixelToWorld(int, double *, double *);
extern void drawBackground(int, Pixel, float);
extern int StringWidth(XFontStruct *, char *);
extern void setMenuItem(int, int);
extern void fremem(void **);
extern void deleteGridObjects(void);
extern void redraw(int);
extern void annotateWavefunction(void);
extern char *getStringResource(Widget, char *);
extern void selectMolecule(Widget, caddr_t, XmToggleButtonCallbackStruct *);
extern int  makeAnnotation(int, int, float, float, float, int, const GLfloat *,
                           int, int, char *);
extern int existsGridObject(void);
extern void calcmo(void);

extern struct MOLECULE *molecules;
extern struct WINDOW windows[];
extern Widget topShell;
extern Pixel stdcol[9];
extern double denres, *grid;
extern int nmolecule, iwavef;
extern int unit, selectAtom, selectAtomAnnotation, automaticRecalculate;
extern int picking, swapBuffers;

void drawMODiagram(Widget w, caddr_t client_data, XmDrawingAreaCallbackStruct *data)
{
  char line[15], trans[15];
  char form[8], units[7];
  double scan[500];
  double eunit=1.0, e1h, e2h, e;
  double xpix, ypix, strl=0.0e0;
  double ticMark, ticSave, ticInc;
  double smax, shape, center, ehelp, fac;
  double off1, off2, off;
  const float red[4]   = {1.0, 0.0, 0.0, 0.0};
  const float blue[4]  = {0.0, 0.0, 1.0, 0.0};
  const float white[4] = {1.0, 1.0, 1.0, 0.0};
  int renderMode, imol;
  register double l;
  register int i, j;

/* This subroutine draws a MO energy diagram */

/* Convert units from Hartree/particle */
  switch (unit)
  {
    case HARTREE: eunit=1.0;             /* to Hartree/particle */
                  strcpy(units, "Eh");
                  break;
    case KJ_MOL:  eunit=2625.5001;       /* to kJ/mol */
                  strcpy(units, "kJ/mol");
                  break;
    case EV:      eunit=27.211611;       /* to eV */
                  strcpy(units, "eV");
                  break;
    case CM:      eunit=4.3598149e6/(6.626176*2.99792458);   /* to 1/cm (E/hc) */
                  strcpy(units, "1/cm");
                  break;
  }
  imol=windows[MO].set;
  glGetIntegerv(GL_RENDER_MODE, &renderMode);
  if (renderMode == GL_RENDER)
    glXMakeCurrent(XtDisplay(windows[MO].widget), XtWindow(windows[MO].widget), windows[MO].context);

  if (!picking)
  {
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
  }
  pixelToWorld(MO, &xpix, &ypix);
  drawBackground(MO, windows[MO].background, 0.0);
  setWindowColor(FOREGROUND, windows[MO].foreground, windows[MO].foreground_rgb);
  glOrtho(windows[MO].left, windows[MO].right, windows[MO].bottom,
          windows[MO].top, windows[MO].near, windows[MO].far);
  e1h=windows[MO].bottom*eunit;
  e2h=windows[MO].top*eunit;
  glShadeModel(GL_FLAT);

/* Calculate and draw tic marks */

  glPushName(imol);
  ticInc=makeTics(e1h, e2h, &ticMark, form);
  ticSave=ticMark;
  do
  {
    if (fabs(ticMark) < 1.0e-5) ticMark=0.0;
    sprintf(line, form, ticMark);
    l=(double)(StringWidth(windows[MO].font, line)+9.0);
    strl= strl > l ? strl : l;
    (*drawString)(line, 2.0*xpix, ticMark/eunit-5.0*ypix, 0.0, 0.0, windows[MO].GLfontId);
    ticMark+=ticInc;
  } while (ticMark < e2h);
  strl*=xpix;
  ticMark=ticSave;

  (*drawLineWidth)((GLfloat)1.);
  glEnable(GL_LINE_STIPPLE);                             /* Dotted lines */
  (*drawLineStipple)(1, 0x3333);
  (*drawBegin)(GL_LINES);
  (*drawVertex2d)(strl, 0.0);
  (*drawVertex2d)(1.0e0, 0.0);
  (*drawEnd)();
  (*drawDisable)(GL_LINE_STIPPLE);
  (*drawBegin)(GL_LINES);
  (*drawVertex2d)(strl, windows[MO].bottom);
  (*drawVertex2d)(strl, windows[MO].top);
  (*drawEnd)();

  do
  {
    (*drawBegin)(GL_LINES);
    (*drawVertex2d)(strl-5.0*xpix, ticMark/eunit);
    (*drawVertex2d)(strl+5.0*xpix, ticMark/eunit);
    (*drawEnd)();
    ticMark+=ticInc;
  } while (ticMark < e2h);
  (*drawString)(units, 2.0*xpix, (ticMark-5.0*ticInc/2.0)/eunit, 0.0, 0.0, windows[MO].GLfontId);

/* Draw energy levels */

  if (windows[MO].mode == DENSITY_OF_STATES)
  {
    e=windows[MO].bottom;
    smax=0.0;
    for (i=0; i<500; i++)
    {
      scan[i]=0.0;
      for (j=0; j<molecules[imol].nbasfu; j++)
      {
        shape=e-molecules[imol].orbitals[j].energy;
        scan[i]+=exp(-shape*shape/denres);
      }
      smax= smax > scan[i] ? smax : scan[i];
      e+=(windows[MO].top-windows[MO].bottom)/500.0;
    }
    (*drawBegin)(GL_LINE_STRIP);
    (*drawVertex2d)(scan[0]*(1.0-strl)/smax+strl, windows[MO].bottom);
    for (i=1; i<500; i++)
      (*drawVertex2d)(scan[i]*(1.0-strl)/smax+strl, windows[MO].bottom+
                 (double)i*(windows[MO].top-windows[MO].bottom)/500.0);
    (*drawEnd)();
  }
  else
  {
    for (i=0; i<molecules[imol].nbasfu; i++)
    {
      if (i != molecules[imol].imo)
      {
        glPushName(i+1);
        (*drawBegin)(GL_LINES);
        (*drawVertex2d)(strl+20.0*xpix, molecules[imol].orbitals[i].energy);
        (*drawVertex2d)(1.0-20.0*xpix, molecules[imol].orbitals[i].energy);
        (*drawEnd)();
        glPopName();
      }
    }

/* Draw the selected energy level with symmetry and energy
   in a centered box */

    if (molecules[imol].imo != (-1))
    {
      setWindowColor(FOREGROUND, stdcol[RED], red);
      glPushName(molecules[imol].imo+1);
      (*drawBegin)(GL_LINES);
      (*drawVertex2d)(strl+20.0*xpix, molecules[imol].orbitals[molecules[imol].imo].energy);
      (*drawVertex2d)(1.0-20.0*xpix, molecules[imol].orbitals[molecules[imol].imo].energy);
      (*drawEnd)();
      glPopName();
      center=0.5+strl*0.5;
      if (molecules[imol].imosave != (-1) && molecules[imol].imosave != molecules[imol].imo)
      {
        ehelp=molecules[imol].orbitals[molecules[imol].imo].energy-molecules[imol].orbitals[molecules[imol].imosave].energy;
        (*drawBegin)(GL_LINES);
        (*drawVertex2d)(strl+20.0*xpix, molecules[imol].orbitals[molecules[imol].imosave].energy);
        (*drawVertex2d)(1.0-20.0*xpix, molecules[imol].orbitals[molecules[imol].imosave].energy);
        (*drawEnd)();
        setWindowColor(FOREGROUND, stdcol[BLUE], blue);
        (*drawBegin)(GL_LINES);
        (*drawVertex2d)(center, molecules[imol].orbitals[molecules[imol].imosave].energy);
        (*drawVertex2d)(center, molecules[imol].orbitals[molecules[imol].imo].energy);
        (*drawEnd)();
        if (molecules[imol].imosave < molecules[imol].imo)
          fac=-10.0;
        else
          fac=10.0;
        (*drawBegin)(GL_LINE_STRIP);
        (*drawVertex2d)(center-3.0*xpix, molecules[imol].orbitals[molecules[imol].imo].energy+fac*ypix);
        (*drawVertex2d)(center, molecules[imol].orbitals[molecules[imol].imo].energy);
        (*drawVertex2d)(center+3.0*xpix, molecules[imol].orbitals[molecules[imol].imo].energy+fac*ypix);
        (*drawEnd)();
        sprintf(line, "%.6f", eunit*ehelp);
        sprintf(trans, "%s -> %s", molecules[imol].orbitals[molecules[imol].imosave].symmetry,
                molecules[imol].orbitals[molecules[imol].imo].symmetry);
        ehelp=fabs(ehelp)/2.0+molecules[imol].orbitals[molecules[imol].imo < molecules[imol].imosave ? molecules[imol].imo : molecules[imol].imosave].energy+13.*ypix;
      }
      else
      {
        ehelp=molecules[imol].orbitals[molecules[imol].imo].energy;
        sprintf(line, "%.6f", eunit*molecules[imol].orbitals[molecules[imol].imo].energy);
        sprintf(trans, "%s", molecules[imol].orbitals[molecules[imol].imo].symmetry);
      }
      off1=(double)(StringWidth(windows[MO].font, trans)+4)/2.0;
      off2=(double)(StringWidth(windows[MO].font, line)+4)/2.0;
      off=xpix*(off1 > off2 ? off1 : off2);
      setWindowColor(FOREGROUND, stdcol[WHITE], white);
      if (ehelp-windows[MO].bottom < 27.0*ypix)
      {
        glRectd(center-off, ehelp+27.0*ypix, center+off, ehelp);
        setWindowColor(FOREGROUND, windows[MO].foreground, windows[MO].foreground_rgb);
        (*drawBegin)(GL_LINE_LOOP);
        (*drawVertex2d)(center-off, ehelp+27.0*ypix);
        (*drawVertex2d)(center-off, ehelp);
        (*drawVertex2d)(center+off, ehelp);
        (*drawVertex2d)(center+off, ehelp+27.0*ypix);
        (*drawEnd)();
        (*drawString)(trans, center-(off1-2.0)*xpix, ehelp+16.0*ypix, 0.0, 0.0, windows[MO].GLfontId);
        (*drawString)(line, center-(off2-2.0)*xpix, ehelp+3.0*ypix, 0.0, 0.0, windows[MO].GLfontId);
      }
      else
      {
        glRectd(center-off, ehelp-27.0*ypix, center+off, ehelp);
        setWindowColor(FOREGROUND, windows[MO].foreground, windows[MO].foreground_rgb);
        (*drawBegin)(GL_LINE_LOOP);
        (*drawVertex2d)(center-off, ehelp-27.0*ypix);
        (*drawVertex2d)(center-off, ehelp);
        (*drawVertex2d)(center+off, ehelp);
        (*drawVertex2d)(center+off, ehelp-27.0*ypix);
        (*drawEnd)();
        (*drawString)(trans, center-(off1-2.0)*xpix, ehelp-11.0*ypix, 0.0, 0.0, windows[MO].GLfontId);
        (*drawString)(line, center-(off2-2.0)*xpix, ehelp-24.0*ypix, 0.0, 0.0, windows[MO].GLfontId);
      }
    }
  }
  glPopName();
  if (swapBuffers) glXSwapBuffers(XtDisplay(windows[MO].widget), XtWindow(windows[MO].widget));
}

void quitMODiagram(Widget w, caddr_t client_data, XmAnyCallbackStruct *data)
{
  register int imol;

  imol=windows[MO].set;
  deleteGridObjects();
  glXMakeCurrent(XtDisplay(windows[VIEWER].widget), XtWindow(windows[VIEWER].widget), windows[VIEWER].context);
  fremem((void **)&windows[MO].font);
  XtDestroyWidget(XtParent(windows[MO].widget));
  windows[MO].widget=0;
  setMenuItem(VIEWER_ENERGYLEVEL, True);
  if (nmolecule > 0)
  {
    molecules[imol].imo=(-1);
    molecules[imol].imosave=(-1);
    redraw(VIEWER);
  }
}

void setTransition(Widget w, caddr_t dummy, XmToggleButtonCallbackStruct *data)
{
  register int imol;

  imol=windows[MO].set;
  if (data->set)
    molecules[imol].imosave=molecules[imol].imo;
  else
    molecules[imol].imosave=(-1);
}

void MODiagramKeyAction(KeySym keysym)
{
  XmToggleButtonCallbackStruct data;
  struct MOLECULE *mol;
  int imoSave;

  mol=&molecules[windows[MO].set];
  imoSave=mol->imo;
  switch (keysym)
  {
    case XK_Print: printDialog((Widget)0, (caddr_t)MO, (XmAnyCallbackStruct *)0);
                   break;
    case XK_Down:  if (--mol->imo < 0) mol->imo=mol->nbasfu-1;
                   break;
    case XK_Up:    if (++mol->imo >= mol->nbasfu) mol->imo=0;
                   break;
    case XK_Tab:   if (nmolecule > 1)                                          
                   {                                                           
                     data.set=TRUE;                                            
                     do
                     {
                       windows[MO].set++;                                  
                       if (windows[MO].set >= nmolecule) windows[MO].set=0;
                     } while (molecules[windows[MO].set].orbitals == NULL);
                     (void)selectMolecule((Widget)0, (XtPointer)&windows[MO].selectMenu[windows[MO].set],
                                          &data);
                   }                   
                   break;
  }
  updateMOs(imoSave);
  annotateWavefunction();
}

void updateMOs(int imoSave)
{
  struct MOLECULE *mol;
  const GLfloat black[4] = {0.0, 0.0, 0.0, 0.0};
  char *word;

  mol=&molecules[windows[MO].set];
  if (automaticRecalculate)
  {
    switch (iwavef)
    {
      case BASIS_IN_MO:       word=getStringResource(topShell, "selectAtomTitle");
                              selectAtomAnnotation=makeAnnotation(selectAtomAnnotation,
                                                   CENTERED, 0.0, 1.0, 0.0, stdcol[BLACK],
                                                   black, 0, 0, word);
                              selectAtom=TRUE;
                              break;
      case MOLECULAR_ORBITAL: selectAtom=FALSE;
                              if (!existsGridObject()) calcmo();
                              break;
    }
  }
  if (mol->imo != imoSave && grid != NULL)
    fremem((void **)&grid);
  if (mol->orbitals[mol->imo].occupation == 0.0 && mol->needMoloch)
    setMenuItem(VIEWER_WAVEFUNCTION, False);
  else if (mol->basisset != NULL)
    setMenuItem(VIEWER_WAVEFUNCTION, True);
}