File: line_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 (473 lines) | stat: -rw-r--r-- 12,418 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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/* Write a 1D file. */

/* Copyright (c) 2014-2022 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<ctype.h>
#include<math.h>
#include<string.h>

#include "c2xsf.h"

/* line_spec must be AtN:AtN:n
 * with At - atomic symbol of element present
 *      N  - index of required atom within species (1 assumed if omitted)
 *      n  - number of points
 */

void interpolate1d(struct grid *gptr, double st[3], double end[3],
                   int n, double *pts);

double interpolate0d(struct grid *gptr,double x_in[3]);
void interpolate3d(struct grid *old_grid, struct grid *new_grid);
void  interpolate_r(struct grid *gptr, struct unit_cell *c, double *start,
		    double len, int n, double *points, char mode);
void interpolate_spherical(struct grid *gptr, struct unit_cell *c,
			   double *origin_frac, double len, int n,
			   double *points, char mode);
/* lscan: parse "(x,y,z)" or "AtN" or "At"
 *        expects termination by : or null
 *        used by main() for point value, and this file for line
 *
 *        accept 0 as shorthand for (0,0,0)
 *        accept M as shorthand for (0.5,0.5,0.5)
 */
 
void lscan(char **p, struct contents *m, double x[3]){
  char *ptr,sym[5];
  int i,j,n,atno,ns,found;

  ptr=*p;

  if (*ptr=='('){
    /*
    ptr++;
    if (sscanf(ptr,"%lf,%lf,%lf",x,x+1,x+2)!=3){
      fprintf(stderr,"Malformed line_spec: %s\n",ptr);
      exit(1);
    }
    while(*ptr&&(*ptr!=')')) ptr++;
    if (*ptr!=')'){
      fprintf(stderr,"Malformed line_spec: %s\n",ptr);
      exit(1);
    }
    ptr++;
    */
    if (point_scan(ptr,x,&n)!=1){
      fprintf(stderr,"Malformed line_spec: %s\n",ptr);
      exit(1);
    }
    ptr+=n;
  }
  else if ((*ptr=='0')&&((*(ptr+1)==':')||(*(ptr+1)==0))){
    ptr++;
    x[0]=x[1]=x[2]=0;
  }
  else if ((*ptr=='M')&&((*(ptr+1)==':')||(*(ptr+1)==0))){
    ptr++;
    x[0]=x[1]=x[2]=0.5;
  }
  else{
    for(i=0;i<4;i++){
      if(isalpha(*ptr)) sym[i]=*ptr;
      else break;
      ptr++;
    }

    sym[i]=0;
    if ((!isdigit(*ptr))&&(*ptr!=':')&&(*ptr!=0)){
      fprintf(stderr,"Malformed line_spec: %s\n",ptr);
      exit(1);
    }
    atno=atsym2no(sym);
    if (atno==0) error_exit("Invalid atom in line_spec");
    if ((*ptr==':')||(*ptr==0))
      ns=1;
    else
      if(sscanf(ptr,"%d",&ns)!=1)
	error_exit("Invalid number in atom spec in line_spec");

    if (ns<1) error_exit("Invalid atom number in line_spec");
    if (m->n<1) error_exit("Atom position specified, but no atoms read");
    
    found=0;
    for(i=0;i<m->n;i++){
      if (m->atoms[i].atno==atno){
        found++;
        if (found==ns){
          for(j=0;j<3;j++) x[j]=m->atoms[i].frac[j];
          break;
        }
      }
    }
    if (found!=ns) error_exit("Failed to find atom in line_spec");
    while(*ptr&&(*ptr!=':')) ptr++;
  }

  *p=ptr;

}

/* lflags&1 -> use gnuplot syntax */

void line_write(FILE* outfile, struct unit_cell *c,
                struct contents *m, struct grid *gptr, char *line_spec,
                int lflags){
  char *ptr,*ptr2,mode;
  int i,j,n,radial,tmp;
  double start[3],end[3],v[3],v2[3],x,*points,len;
  struct grid *g;
  char *fmt;
  struct cmt *comment;

  mode=' ';
  
  if (!gptr||(!gptr->data))
    error_exit("No data found to plot");

  if (flags&HIPREC)
    fmt="%.12g %.12g\n";
  else
    fmt="%8g %g\n";

  /* parse line spec */

  radial=0;
  ptr=line_spec;

  if (!strcmp(line_spec,"a")){
    start[0]=start[1]=start[2]=0;
    end[0]=1;
    end[1]=end[2]=0;
    n=gptr->size[0]+1;
  }
  else if (!strcmp(line_spec,"b")){
    start[0]=start[1]=start[2]=0;
    end[1]=1;
    end[0]=end[2]=0;
    n=gptr->size[1]+1;
  }
  else if (!strcmp(line_spec,"c")){
    start[0]=start[1]=start[2]=0;
    end[2]=1;
    end[0]=end[1]=0;
    n=gptr->size[2]+1;
  }
  else{
  
    lscan(&ptr,m,start);

    if (*ptr!=':') error_exit("Failed to find first colon in line_spec");
    ptr++;

    if ((*ptr=='r')||(*ptr=='R')){
      radial=1;
      if (*ptr=='R') radial=2;
      ptr++;
      sscanf(ptr,"%lf%n",&len,&n);
      ptr+=n;
      if (*ptr=='B'){
	len*=BOHR;
	ptr++;
      }
    }
    else
      lscan(&ptr,m,end);

    if (*ptr!=':') error_exit("Failed to find second colon in line_spec");
    ptr++;

    if(sscanf(ptr,"%d%n",&n,&i)!=1)
      error_exit("Invalid number of points in line_spec");

    if (ptr[i]=='w') mode='w';
    else if (ptr[i]=='a') mode='a';
    else if ((ptr[i])&&(!isspace(ptr[i])))
      fprintf(stderr,"Warning: unexpected trailing characters in line_spec\n");
  }

  if ((radial==2)&&(len==0)){ /* maximal sphere requested -- what is it? */
    /* centre of cell in abs co-ords */
    for(i=0;i<3;i++){
      v[i]=0;
      for(j=0;j<3;j++)
	v[i]+=0.5*c->basis[j][i];
    }

    for(i=0;i<3;i++){
      /* for each of the cell's faces, find perp unit vector */
      vcross(c->basis[(i+1)%3],c->basis[(i+2)%3],v2);
      x=sqrt(vmod2(v2));
      for(j=0;j<3;j++) v2[j]/=x;
      /* dot with vector to centre of cell to find perp dist */
      x=0;
      for(j=0;j<3;j++) x+=v[j]*v2[j];
      if (i==0)
	len=x;
      else
	len=min(len,x);
    }
  }

  if ((radial==0)&&(n==0)){
    for(i=0;i<3;i++){
      if (start[i]==end[i]) continue;
      if (aeq(start[i]*gptr->size[i],floor(start[i]*gptr->size[i]+0.5))&&
	  aeq(end[i]*gptr->size[i],floor(end[i]*gptr->size[i]+0.5))){
	tmp=1+floor((end[i]-start[i])*gptr->size[i]+0.5);
	if (n==0) n=tmp;
	else if (tmp!=n){
	  n=0;
	  break;
	}
      }
    }
    if (n){
      for(i=0;i<3;i++){
	if (aeq(start[i]*gptr->size[i],floor(start[i]*gptr->size[i]+0.5)))
	  start[i]=floor(start[i]*gptr->size[i]+0.5)/gptr->size[i];
	if (aeq(end[i]*gptr->size[i],floor(end[i]*gptr->size[i]+0.5)))
	  end[i]=floor(end[i]*gptr->size[i]+0.5)/gptr->size[i];
      }
      fprintf(stderr,"Line chosen (%f,%f,%f):(%f,%f,%f):%d\n",
	      start[0],start[1],start[2],end[0],end[1],end[2],n);
    }
  }

  if (n<1){
    fprintf(stderr,"Invalid number of points for line. Have %d\n",n);
    exit(1);
  }
  
  points=malloc(n*sizeof(double));
  if(!points) error_exit("Malloc error for points in line_write");

  if (debug){
    if (radial)
      fprintf(stderr,
	      "Requested line centre (%f,%f,%f) %s radius %f A"
	      " with %d points.\n",
	      start[0],start[1],start[2],(radial==1)?"cylinder":"sphere",
	      len,n);
    else
      fprintf(stderr,
	      "Requested line (%f,%f,%f) to (%f,%f,%f) with %d points.\n",
	      start[0],start[1],start[2],end[0],end[1],end[2],n);
  }
  
  /* Find length of line */

  /* Convert to absolute co-ords */

  if (!radial){
    for(i=0;i<3;i++){
      v[i]=0;
      for(j=0;j<3;j++)
	v[i]+=(end[j]-start[j])*c->basis[j][i];
    }

    len=0;
    for(i=0;i<3;i++) len+=v[i]*v[i];
    len=sqrt(len);
    if (flags&AU) len=len/BOHR;
  }
  
  if (m->title) fprintf(outfile,"# %s\n\n",m->title);

  fprintf(outfile,"# %s\n",line_spec);
  if (radial)
    fprintf(outfile,"# centre (%f,%f,%f) length %f with %d points\n",
	    start[0],start[1],start[2],len,n);
  else
    fprintf(outfile,"# (%f,%f,%f) to (%f,%f,%f) with %d points\n",
	    start[0],start[1],start[2],end[0],end[1],end[2],n);
  fprintf(outfile,"# distance in %s\n",(flags&AU)?"Bohr":"Angstrom");
  if (m->comment->txt){
    fprintf(outfile,"\n");
    comment=m->comment;
    while((comment)&&(comment->txt)){
      fprintf(outfile,"# %s\n",comment->txt);
      comment=comment->next;
    }
    fprintf(outfile,"\n");
  }


  if (lflags&1){
    fprintf(outfile,"set xlabel \"%s\"\n",(flags&AU)?"Bohr":"Angstrom");
    fprintf(outfile,"set title \"%s (length %g %s)",line_spec,len,
            (flags&AU)?"Bohr":"A");
    if ((mode=='w')||(mode=='a')) fprintf(outfile,", %s weighted",
			   (radial==1)?"2pi r":"4pi r^2");
    if (mode=='a') fprintf(outfile,", cumulative");
    fprintf(outfile,"\"\n");
    fprintf(outfile,"plot [0:%g] ",len);
    g=gptr;
    ptr=NULL;
    while(g&&(g->data)){ 
      if (ptr) free(ptr); /* Remove _ as gnuplot treats as subscript */
      ptr=malloc(strlen(g->name)+1);
      if (!ptr) error_exit("malloc error for grid title!");
      strcpy(ptr,g->name);
      while((ptr2=strchr(ptr,'_'))) *ptr2=' ';
      fprintf(outfile,"'-' w lp title \"%s\"",ptr);
      g=g->next;
      if (g&&(g->data)) fprintf(outfile,",");
    }
    if (ptr) free(ptr);
    fprintf(outfile,"\n");
  }

  while(gptr&&(gptr->data)){
    if (radial==1)
      interpolate_r(gptr,c,start,len,n,points,mode);
    else if (radial==2)
      interpolate_spherical(gptr,c,start,len,n,points,mode);
    else
      interpolate1d(gptr,start,end,n,points);

    fprintf(outfile,"# %s\n",gptr->name);

    for(i=0;i<n;i++) fprintf(outfile,fmt,i*len/(n-1),points[i]);

    if (lflags&1) fprintf(outfile,"e\n");

    gptr=gptr->next;

    if (gptr&&(gptr->data)) fprintf(outfile,"\n");
  }

  if (lflags&1) fprintf(outfile,"pause -1 \"Press return to exit\"\n");

  free(points);
}

void  interpolate_r(struct grid *gptr, struct unit_cell *c, double *start,
		    double len, int n, double *points, char mode){
  int i,j,jj,k,jmax;
  double abc[6],origin[3],pt[3],frac[3],radius,sum,integral;
  struct grid ng;
  
  basis2abc(c->basis,abc);
  if ((!(aeq(abc[3],90)))||(!(aeq(abc[4],90))))
    error_exit("c not perpendicular to ab plane\n");

  /* Reduce grid */

  if (gptr->size[2]!=1){
    for(i=0;i<2;i++) ng.size[i]=gptr->size[i];
    ng.size[2]=1;
    interpolate3d(gptr,&ng);
    free(gptr->data);
    gptr->data=ng.data;
    for(i=0;i<3;i++) gptr->size[i]=ng.size[i];
  }

  for(i=0;i<2;i++)
    origin[i]=start[0]*c->basis[0][i]+start[1]*c->basis[1][i];
  origin[2]=0;

  if (debug)
    fprintf(stderr,"Origin, abs coords: (%f,%f,%f)\n",origin[0],
	    origin[1],origin[2]);

  
  integral=0;
  for(i=0;i<n;i++){
    radius=i*len/n;
    jmax=2+2*M_PI*n*radius/len;
    sum=0;
    for(j=0;j<jmax;j++){
      pt[0]=origin[0]+radius*cos((2*M_PI*j)/jmax);
      pt[1]=origin[1]+radius*sin((2*M_PI*j)/jmax);
      pt[2]=0;
      for(jj=0;jj<3;jj++){
	frac[jj]=0;
	for(k=0;k<3;k++)
	  frac[jj]+=pt[k]*c->recip[jj][k];
      }
      sum+=interpolate0d(gptr,frac);
    }
    points[i]=sum/jmax;
    integral+=points[i]*2*M_PI*radius;
    if ((mode=='w')||(mode=='a')) points[i]*=2*M_PI*radius;
    if (mode=='a') {
      points[i]*=len/n;
      if (i) points[i]+=points[i-1];
    }
  }
  integral*=len/n;
  integral*=abc[2];
  if (debug)
    fprintf(stderr,"Radial integral: %f\n",integral);
  
}

/* Average over spherical shells using Fibonacci spiral to sample shell */

void interpolate_spherical(struct grid *gptr, struct unit_cell *c,
			   double *origin_frac, double len, int n,
			   double *points, char mode){
  int i,j,jj,k,jmax;
  double origin[3],pt[3],frac[3],radius,sum,integral,golden,r,x,y,z,theta;
  
  for(i=0;i<3;i++)
    origin[i]=origin_frac[0]*c->basis[0][i]+origin_frac[1]*c->basis[1][i]+
      origin_frac[2]*c->basis[2][i];

   if (debug)
    fprintf(stderr,"Origin, abs coords: (%f,%f,%f)\n",origin[0],
	    origin[1],origin[2]);
  
  integral=0;
  golden=(1+sqrt(5.0))/2;
  for(i=0;i<n;i++){
    radius=i*len/n;
    jmax=2+4*M_PI*n*n*radius*radius/(len*len);
    sum=0;
    for(j=0;j<jmax;j++){
      z=1-2*(j+0.5)/jmax;
      r=sqrt(1-z*z);
      theta=2*M_PI*j/golden;
      x=r*cos(theta);
      y=r*sin(theta);
      pt[0]=origin[0]+x*radius;
      pt[1]=origin[1]+y*radius;
      pt[2]=origin[2]+z*radius;
      for(jj=0;jj<3;jj++){
	frac[jj]=0;
	for(k=0;k<3;k++)
	  frac[jj]+=pt[k]*c->recip[jj][k];
      }
      sum+=interpolate0d(gptr,frac);
    }
    points[i]=sum/jmax;
    integral+=points[i]*4*M_PI*radius*radius;
    if ((mode=='w')||(mode=='a')) points[i]*=4*M_PI*radius*radius;
    if (mode=='a') {
      points[i]*=len/n;
      if (i) points[i]+=points[i-1];
    }
  }

  integral*=len/n;

  if (debug)
    fprintf(stderr,"Spherical integral: %f\n",integral);

}