~njansson/dolfin/hpc

« back to all changes in this revision

Viewing changes to src/utils/inp2dx/inp2dxanim.c

  • Committer: Johannes Ring
  • Date: 2008-03-05 22:43:06 UTC
  • Revision ID: johannr@simula.no-20080305224306-2npsdyhfdpl2esji
The BIG commit!

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include <stdio.h>
2
 
#include <stdlib.h>
3
 
 
4
 
void checkerror(int error);
5
 
 
6
 
int main(int argc, char **argv)
7
 
{
8
 
  FILE *fp_inp; /* inp file pointer */
9
 
  FILE *fp_dx;  /* dx file pointer */
10
 
  int npoints;  /* number of points */
11
 
  int nelems;   /* number of elements */
12
 
  int nvars;    /* number of variables */
13
 
  int nconns;   /* number of connections */
14
 
  int dummy1, dummy2;
15
 
  float x,y,z;
16
 
  int p1,p2,p3,p4;
17
 
  int i,j,n;
18
 
  char element_type[64];
19
 
  float *data;
20
 
  int   *objects_size;
21
 
  int    objects_n;
22
 
  int n_frames;
23
 
  
24
 
  /* Check arguments */
25
 
  if ( argc < 4 ){
26
 
         printf("Usage: inp2dxanim datafile_1.inp ... datafile_n.inp datafile.dx\n");
27
 
         exit(1);
28
 
  }
29
 
 
30
 
  n_frames = argc - 2;
31
 
  
32
 
  /* Open inp file */
33
 
  fp_inp = fopen(argv[1],"r");
34
 
  if ( !fp_inp ){
35
 
         printf("Unable to open inp file %s for reading.\n",argv[1]);
36
 
         exit(1);
37
 
  }
38
 
  
39
 
  /* Open dx file */
40
 
  fp_dx = fopen(argv[argc-1],"w");
41
 
  if ( !fp_dx ){
42
 
         printf("Unable to open dx file %s for writing.\n",argv[argc-1]);
43
 
         exit(1);
44
 
  }
45
 
  
46
 
  /* Read data from inp file */
47
 
  fscanf(fp_inp,"%d %d %d %d %d\n",&npoints,&nelems,&nvars,&dummy1,&dummy2);
48
 
  printf("Number of points:    %d\n",npoints);
49
 
  printf("Number of elements:  %d\n",nelems);
50
 
  printf("Number of variables: %d\n",nvars);
51
 
 
52
 
  /* Allocate memory for data */
53
 
  data         = (float *) malloc( nvars*sizeof(float) );
54
 
  objects_size = (int *)   malloc( nvars*sizeof(int) );
55
 
  
56
 
  /* A warning */
57
 
  printf("I assume the elements are tetrahedrons...\n");
58
 
  
59
 
  /* Positions */
60
 
  printf("Reading positions...");
61
 
  fflush(stdout);
62
 
  fprintf(fp_dx,"# A list of all node positions\n");
63
 
  fprintf(fp_dx,"object 1 class array type float rank 1 shape 3 items %d data follows\n",npoints);
64
 
  
65
 
  for (i=0;i<npoints;i++){
66
 
         
67
 
         /* Read values */
68
 
         fscanf(fp_inp,"%d %f %f %f\n",&dummy1,&x,&y,&z);
69
 
         
70
 
         if ( i % (npoints/20) == 0 ){
71
 
                printf(".");
72
 
                fflush(stdout);
73
 
         }
74
 
         
75
 
         /* Save values */
76
 
         fprintf(fp_dx,"%f %f %f\n",x,y,z);
77
 
         
78
 
  }  printf("\n");
79
 
  
80
 
  /* Elements */
81
 
  printf("Reading elements....");
82
 
  fflush(stdout);
83
 
  fprintf(fp_dx,"\n# A list of all elements\n");
84
 
  fprintf(fp_dx,"object 2 class array type int rank 1 shape 4 items %d data follows\n",nelems);
85
 
  for (i=0;i<nelems;i++){
86
 
         
87
 
         /* Read values */
88
 
         fscanf(fp_inp,"%d %d %s",&dummy1,&dummy2,&element_type);
89
 
         fscanf(fp_inp,"%d %d %d %d\n",&p1,&p2,&p3,&p4);
90
 
         
91
 
         if ( i % (nelems/20) == 0 ){
92
 
                printf(".");
93
 
                fflush(stdout);
94
 
         }
95
 
         
96
 
         /* Save values */
97
 
         fprintf(fp_dx,"%d %d %d %d\n",p1-1,p2-1,p3-1,p4-1);
98
 
         
99
 
  }
100
 
  fprintf(fp_dx,"attribute \"element type\" string \"tetrahedra\"\n");
101
 
  fprintf(fp_dx,"attribute \"ref\" string \"positions\"\n");
102
 
  printf("\n");
103
 
  
104
 
  /* Read object sizes */
105
 
  fscanf(fp_inp,"%d",&objects_n);
106
 
  if ( objects_n == 1 )
107
 
         printf("Found %d object.\n",objects_n);
108
 
  else
109
 
         printf("Found %d objects.\n",objects_n);
110
 
  for (i=0;i<(objects_n-1);i++){
111
 
         fscanf(fp_inp,"%d",objects_size+i);
112
 
         printf("  Object %d has %d components.\n",i+1,objects_size[i]);
113
 
  }
114
 
  fscanf(fp_inp,"%d\n",objects_size+i);
115
 
  printf("  Object %d has %d components.\n",i+1,objects_size[i]);
116
 
  printf("Warning: Skipping component information.\n");
117
 
  
118
 
  /* Skip a few lines */
119
 
  for (i=0;i<(objects_n);i++)
120
 
         while ( getc(fp_inp) != '\n' );
121
 
 
122
 
  for (n=0;n<n_frames;n++){
123
 
 
124
 
         printf("Frame %d: %s\n",n+1,argv[n+1]);
125
 
 
126
 
         /* Open file and skip a few lines if this is not the first file */
127
 
         if ( n > 0 ){
128
 
                fp_inp = fopen(argv[n+1],"r");
129
 
                for (i=0;i<(1+npoints+nelems+1+nvars);i++)
130
 
                  while ( getc(fp_inp) != '\n' );
131
 
         }
132
 
         
133
 
         /* Values */  
134
 
         printf("Reading values......");
135
 
         fflush(stdout);
136
 
         fprintf(fp_dx,"\n# Values at nodal points, frame %d\n",n+1);
137
 
         fprintf(fp_dx,"object %d class array type float rank %d shape %d items %d data follows\n",
138
 
                                n+3,(nvars > 1 ? 1 : 0 ),nvars,npoints);
139
 
         for (i=0;i<npoints;i++){
140
 
                
141
 
                /* Read values */
142
 
                fscanf(fp_inp,"%d",&dummy1);
143
 
                for (j=0;j<(nvars-1);j++)
144
 
                  fscanf(fp_inp,"%f ",data+j);
145
 
                fscanf(fp_inp,"%f\n",data+j);
146
 
                
147
 
                if ( i % (npoints/20) == 0 ){
148
 
                  printf(".");
149
 
                  fflush(stdout);
150
 
                }
151
 
                
152
 
                /* Save values */
153
 
                for (j=0;j<(nvars-1);j++)
154
 
                  fprintf(fp_dx,"%f ",data[j]);
155
 
                fprintf(fp_dx,"%f\n",data[j]);
156
 
                
157
 
         }
158
 
         fprintf(fp_dx,"attribute \"dep\" string \"positions\"\n");
159
 
         printf("\n");
160
 
 
161
 
         /* Close the data file */
162
 
         
163
 
  }
164
 
 
165
 
  for (i=0;i<n_frames;i++){
166
 
         fprintf(fp_dx,"\n# Field for frame %d\n",i+1);
167
 
         fprintf(fp_dx,"object %d class field\n",2+n_frames+(i+1));
168
 
         fprintf(fp_dx,"component \"positions\" value 1\n");
169
 
         fprintf(fp_dx,"component \"connections\" value 2\n");
170
 
         fprintf(fp_dx,"component \"data\" value %d\n",2+i+1);
171
 
  }
172
 
  
173
 
  /* Write a few lines to make Open DX understand the data */
174
 
  fprintf(fp_dx,"\n# The time series\n");
175
 
  fprintf(fp_dx,"object \"series\" class series\n");
176
 
  for (i=0;i<n_frames;i++)
177
 
         fprintf(fp_dx,"member %d value %d position %d\n",i,2+n_frames+(i+1),i);
178
 
 
179
 
  fprintf(fp_dx,"\n");
180
 
  fprintf(fp_dx,"end\n");
181
 
 
182
 
  /* Delete memory for data */
183
 
  free(data);
184
 
  free(objects_size);
185
 
  
186
 
  /* Close files */
187
 
  fclose(fp_dx);
188
 
  
189
 
  return 0;
190
 
}