File: denfmt_write.c

package info (click to toggle)
c2x 2.40.e%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,136 kB
  • sloc: ansic: 22,418; makefile: 56; sh: 1
file content (131 lines) | stat: -rw-r--r-- 4,027 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
/* Write a CASTEP den_fmt file
 * will write grids until the first change in grid size
 */


/* Copyright (c) 2017 MJ Rutter 
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3
 * of the Licence, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see http://www.gnu.org/licenses/
 */ 

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#include "c2xsf.h"

void denfmt_write(FILE* outfile, struct unit_cell *c, struct contents *m,
                struct grid *g){
  double abc[6],scale;
  unsigned int i,j,k,ii,offset,ngrids,nfft[3],same;
  char *fmt;
  struct grid *gptr;

  if (flags&HIPREC){
    fmt="  %.15f";
  }
  else{
    fmt="  % 12.6f";
  }
  
  cart2abc(c,NULL,abc,NULL);
  
  fprintf(outfile," BEGIN header\n \n");

  fprintf(outfile,"           Real Lattice(A)               "
          "Lattice parameters(A)    Cell Angles\n");

  fprintf(outfile," %11.7f %11.7f %11.7f     a = %11.6f  alpha = %11.6f\n",
          c->basis[0][0],c->basis[0][1],c->basis[0][2],abc[0],abc[3]);
  fprintf(outfile," %11.7f %11.7f %11.7f     b = %11.6f  beta  = %11.6f\n",
          c->basis[1][0],c->basis[1][1],c->basis[1][2],abc[1],abc[4]);
  fprintf(outfile," %11.7f %11.7f %11.7f     c = %11.6f  gamma = %11.6f\n",
          c->basis[2][0],c->basis[2][1],c->basis[2][2],abc[2],abc[5]);

  fprintf(outfile," \n");

  ngrids=0;
  gptr=g;
  for(i=0;i<3;i++) nfft[i]=g->size[i];
  same=1;
  while(gptr){
    if (gptr->data){
      for(i=0;i<3;i++) if (gptr->size[i]!=nfft[i]) same=0;
      if (same) ngrids++;
      else break;
    }
    gptr=gptr->next;
  }
  if (!same) fprintf(stderr,"Some grids missed as different sizes present\n");
/* Jmol detects den_fmt files by the existence of the string "! nspins",
 * so we cannot use the more accurate comment "! ngrids"
 */
/*  fprintf(outfile,"   %d             ! ngrids\n",ngrids); */
  fprintf(outfile,"   %d             ! nspins (really ngrids)\n",ngrids);
  fprintf(outfile,"  %d   %d   %d   ! grid size along <a,b,c>\n",nfft[0],
          nfft[1],nfft[2]);
  fprintf(outfile," END header: data is \"<a b c>");

  if (debug) fprintf(stderr,"Writing %d grids:",ngrids);
  gptr=g;
  for (i=0;i<ngrids;i++){
    if (gptr->name){
      if (!strncasecmp(gptr->name,"Density",7)){
        fprintf(outfile," charge");
        if (debug) fprintf(stderr," charge");
      }
      else if (!strncasecmp(gptr->name,"Spin",4)){
        fprintf(outfile," spin");
        if (debug) fprintf(stderr," spin");
      }
      else{
        fprintf(outfile," (%s)",gptr->name);
        if (debug) fprintf(stderr," (%s)",gptr->name);
      }
    }
    else{
        fprintf(outfile," (unknown)");
        if (debug) fprintf(stderr," (unknown)");
    }
    gptr=gptr->next;
  }
  fprintf(outfile,"\"\n");
  if (debug) fprintf(stderr,"\n");
  fprintf(outfile," \n");

  /* Now write the data */

  scale=c->vol;
  if (g->name){
    if ((ngrids<=2)&&(!strncmp(g->name,"ELF",3))) scale=1;
    if ((ngrids<=2)&&(!strncmp(g->name,"ESP",3))) scale=1/H_eV;
    if ((ngrids<=2)&&(!strncmp(g->name,"Potential",9))) scale=1/H_eV;
  }
  if (flags&RAW) scale=1;

  if (debug) fprintf(stderr,"Scaling on writing by %lf\n",scale);
  
  for(i=1;i<=nfft[2];i++)
    for(j=1;j<=nfft[1];j++)
      for(k=1;k<=nfft[0];k++){
        fprintf(outfile,"  %4d  %4d  %4d  ",k,j,i);
        gptr=g;
        offset=(i-1)+(j-1)*nfft[2]+(k-1)*nfft[2]*nfft[1];
        for(ii=0;ii<ngrids;ii++){
          fprintf(outfile,fmt,gptr->data[offset]*scale);
          gptr=gptr->next;
        }
        fprintf(outfile,"\n");
      }
}