4
void checkerror(int error);
6
int main(int argc, char **argv)
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 */
18
char element_type[64];
26
printf("Usage: inp2dxanim datafile_1.inp ... datafile_n.inp datafile.dx\n");
33
fp_inp = fopen(argv[1],"r");
35
printf("Unable to open inp file %s for reading.\n",argv[1]);
40
fp_dx = fopen(argv[argc-1],"w");
42
printf("Unable to open dx file %s for writing.\n",argv[argc-1]);
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);
52
/* Allocate memory for data */
53
data = (float *) malloc( nvars*sizeof(float) );
54
objects_size = (int *) malloc( nvars*sizeof(int) );
57
printf("I assume the elements are tetrahedrons...\n");
60
printf("Reading positions...");
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);
65
for (i=0;i<npoints;i++){
68
fscanf(fp_inp,"%d %f %f %f\n",&dummy1,&x,&y,&z);
70
if ( i % (npoints/20) == 0 ){
76
fprintf(fp_dx,"%f %f %f\n",x,y,z);
81
printf("Reading elements....");
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++){
88
fscanf(fp_inp,"%d %d %s",&dummy1,&dummy2,&element_type);
89
fscanf(fp_inp,"%d %d %d %d\n",&p1,&p2,&p3,&p4);
91
if ( i % (nelems/20) == 0 ){
97
fprintf(fp_dx,"%d %d %d %d\n",p1-1,p2-1,p3-1,p4-1);
100
fprintf(fp_dx,"attribute \"element type\" string \"tetrahedra\"\n");
101
fprintf(fp_dx,"attribute \"ref\" string \"positions\"\n");
104
/* Read object sizes */
105
fscanf(fp_inp,"%d",&objects_n);
106
if ( objects_n == 1 )
107
printf("Found %d object.\n",objects_n);
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]);
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");
118
/* Skip a few lines */
119
for (i=0;i<(objects_n);i++)
120
while ( getc(fp_inp) != '\n' );
122
for (n=0;n<n_frames;n++){
124
printf("Frame %d: %s\n",n+1,argv[n+1]);
126
/* Open file and skip a few lines if this is not the first file */
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' );
134
printf("Reading values......");
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++){
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);
147
if ( i % (npoints/20) == 0 ){
153
for (j=0;j<(nvars-1);j++)
154
fprintf(fp_dx,"%f ",data[j]);
155
fprintf(fp_dx,"%f\n",data[j]);
158
fprintf(fp_dx,"attribute \"dep\" string \"positions\"\n");
161
/* Close the data file */
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);
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);
180
fprintf(fp_dx,"end\n");
182
/* Delete memory for data */