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];
25
printf("Usage: inp2dx datafile.inp datafile.dx\n");
30
fp_inp = fopen(argv[1],"r");
32
printf("Unable to open inp file %s for reading.\n",argv[1]);
37
fp_dx = fopen(argv[2],"w");
39
printf("Unable to open dx file %s for writing.\n",argv[2]);
43
/* Read data from inp file */
44
fscanf(fp_inp,"%d %d %d %d %d\n",&npoints,&nelems,&nvars,&dummy1,&dummy2);
45
printf("Number of points: %d\n",npoints);
46
printf("Number of elements: %d\n",nelems);
47
printf("Number of variables: %d\n",nvars);
49
/* Allocate memory for data */
50
data = (float *) malloc( nvars*sizeof(float) );
51
objects_size = (int *) malloc( nvars*sizeof(int) );
54
printf("I assume the elements are tetrahedrons...\n");
57
printf("Reading positions...");
59
fprintf(fp_dx,"# A list of all node positions\n");
60
fprintf(fp_dx,"object 1 class array type float rank 1 shape 3 items %d data follows\n",npoints);
62
for (i=0;i<npoints;i++){
65
fscanf(fp_inp,"%d %f %f %f\n",&dummy1,&x,&y,&z);
67
if ( i % (npoints/20) == 0 ){
73
fprintf(fp_dx,"%f %f %f\n",x,y,z);
78
printf("Reading elements....");
80
fprintf(fp_dx,"\n# A list of all elements\n");
81
fprintf(fp_dx,"object 2 class array type int rank 1 shape 4 items %d data follows\n",nelems);
82
for (i=0;i<nelems;i++){
85
fscanf(fp_inp,"%d %d %s",&dummy1,&dummy2,&element_type);
86
fscanf(fp_inp,"%d %d %d %d\n",&p1,&p2,&p3,&p4);
88
if ( i % (nelems/20) == 0 ){
94
fprintf(fp_dx,"%d %d %d %d\n",p1-1,p2-1,p3-1,p4-1);
97
fprintf(fp_dx,"attribute \"element type\" string \"tetrahedra\"\n");
98
fprintf(fp_dx,"attribute \"ref\" string \"positions\"\n");
101
/* Read object sizes */
102
fscanf(fp_inp,"%d",&objects_n);
103
if ( objects_n == 1 )
104
printf("Found %d object.\n",objects_n);
106
printf("Found %d objects.\n",objects_n);
107
for (i=0;i<(objects_n-1);i++){
108
fscanf(fp_inp,"%d",objects_size+i);
109
printf(" Object %d has %d components.\n",i+1,objects_size[i]);
111
fscanf(fp_inp,"%d\n",objects_size+i);
112
printf(" Object %d has %d components.\n",i+1,objects_size[i]);
113
printf("Warning: Skipping component information.\n");
115
/* Skip a few lines */
116
for (i=0;i<(objects_n);i++)
117
while ( getc(fp_inp) != '\n' );
120
printf("Reading values......");
122
fprintf(fp_dx,"\n# Values at nodal points\n");
123
fprintf(fp_dx,"object 3 class array type float rank %d shape %d items %d data follows\n",
124
(nvars > 1 ? 1 : 0 ),nvars,npoints);
125
for (i=0;i<npoints;i++){
128
fscanf(fp_inp,"%d",&dummy1);
129
for (j=0;j<(nvars-1);j++)
130
fscanf(fp_inp,"%f ",data+j);
131
fscanf(fp_inp,"%f\n",data+j);
133
if ( i % (npoints/20) == 0 ){
139
for (j=0;j<(nvars-1);j++)
140
fprintf(fp_dx,"%f ",data[j]);
141
fprintf(fp_dx,"%f\n",data[j]);
144
fprintf(fp_dx,"attribute \"dep\" string \"positions\"\n");
147
/* Write a few lines to make Open DX understand the data */
148
fprintf(fp_dx,"\n# The Open DX field\n");
149
fprintf(fp_dx,"object \"irregular positions irregular connections\" class field\n");
150
fprintf(fp_dx,"component \"positions\" value 1\n");
151
fprintf(fp_dx,"component \"connections\" value 2\n");
152
fprintf(fp_dx,"component \"data\" value 3\n");
153
fprintf(fp_dx,"end\n");
155
/* Delete memory for data */