File: TRAN_Read.c

package info (click to toggle)
openmx 3.7.6-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 325,856 kB
  • ctags: 3,575
  • sloc: ansic: 152,655; f90: 2,080; python: 876; makefile: 675; sh: 25; perl: 18
file content (147 lines) | stat: -rw-r--r-- 2,876 bytes parent folder | download | duplicates (2)
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "mpi.h"
#include "tran_prototypes.h"

#define LEN  256

void TRAN_Read_double(char *str, int n1,int n2, double *a)
#define a_ref(i,j) a[n1*((j)-1)+(i)-1]
{

  int i,j;
  int m1,m2,d1,d2;
   double x1,x2;
  FILE *fp;
  char buf[LEN];

  if  (   (fp=fopen(str,"r"))==NULL ) {
    printf("cannot open %s and continue\n", str);
    return;
  }

  printf("TRAN_Read_double> Read data from flle=%s\n",str);
  
  /* read header */
  fgets(buf, LEN,fp);
  sscanf(buf,"%d %d %d %d",&m1,&m2,&d1,&d2);

  /* check consistency of the size */
  if ( m1=!n1  || m2!=n2 ) {
     printf("\n\nERROR, size is different, %s , %d %d %d %d\n\n\n",str, n1,n2,m1,m2);
     goto last;   
  }

  /* initialize */
  for (i=0;i<n1*n2; i++) {
     a[i]=0.0;
  }

  /* read and set */
  while (1) {
    if (fgets(buf, LEN,fp)==NULL) { goto last; }
    if ( sscanf(buf,"%d %d %lf %lf", &i,&j, &x1,&x2)==0 ) { goto last; }
    a_ref(i,j) = x1;   
  }

  /* finally close the file */
last:
  fclose(fp);
}

void TRAN_Read_dcomplex(char *str, int n1,int n2, dcomplex *a)
#define a_ref(i,j) a[n1*((j)-1)+(i)-1]
{

  int i,j;
  int m1,m2,d1,d2;
   double x1,x2;
  FILE *fp;
  char buf[LEN];

  if  (   (fp=fopen(str,"r"))==NULL ) {
    printf("cannot open %s and continue\n", str);
    return;
  }

  printf("TRAN_Read_dcomplex> Read data from flle=%s\n",str);

  /* read header */
  fgets(buf, LEN,fp);
  sscanf(buf,"%d %d %d %d",&m1,&m2,&d1,&d2);

  /* check consistency of the size */
  if ( m1=!n1  || m2!=n2 ) {
     printf("\n\nERROR, size is different, %s , %d %d %d %d\n\n\n",str, n1,n2,m1,m2);
     goto last;
  }

  /* initialize */
  for (i=0;i<n1*n2; i++) {
     a[i].r=0.0;
     a[i].i=0.0;
  }

  /* read and set */
  while (1) {
    if (fgets(buf, LEN,fp)==NULL) { goto last; }
    if ( sscanf(buf,"%d %d %lf %lf", &i,&j, &x1,&x2)==0 ) { goto last; }
    a_ref(i,j).r = x1;
    a_ref(i,j).i = x2;
  }

  /* finally close the file */
last:
  fclose(fp);


}



void TRAN_FRead2_binary_double(FILE *fp, int n1, int n2, double *gc)
#define gc_ref(i,j) gc[ n1*(j)+i ]
{

   int i,j;
   double *v;
   int iv[10];
   int i1,i2;

   printf("n=%d %d\n",n1,n2);

   v=(double*)malloc(sizeof(double)*n1);

   fread(iv,sizeof(int),2,fp);
   i1=iv[0];
   i2=iv[1];
   if (i1!=n1 || i2!=n2) {
      printf(" i1!=n1 || i2!=n2,  i1=%d i2=%d n1=%d n2=%d\n",i1,i2,n1,n2);
      exit(0);
   }

   for (i=0;i<n1*n2;i++) gc[i]=0.0;

   for (j=0;j<n2;j++) {
      fread(iv,sizeof(int),3,fp);
      if (iv[0]!=j) {
        printf("data format error j=%d iv[0]=%d\n",j,iv[0]);
        exit(0); 
      }
      i1=iv[1];
      i2=iv[2];
/*      printf("%d of %d i=[%d:%d]\n",j,n2,i1,i2); */
      if ( i1<=i2) {
      fread(v,sizeof(double),i2-i1+1,fp);
      for (i=i1;i<=i2;i++) {
         gc_ref(i,j) = v[i-i1];
      }
      }
   }

  free(v);

}