~njansson/dolfin/hpc

« back to all changes in this revision

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

  • Committer: logg
  • Date: 2002-09-13 12:55:37 UTC
  • Revision ID: devnull@localhost-20020913125537-gz6ry1id9xsvu6np
Tailorized "2002-09-13 07:55:37 by logg"
Initial revision

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;
 
18
  char element_type[64];
 
19
  float *data;
 
20
  int   *objects_size;
 
21
  int    objects_n;
 
22
  
 
23
  /* Check arguments */
 
24
  if ( argc != 3 ){
 
25
         printf("Usage: inp2dx datafile.inp datafile.dx\n");
 
26
         exit(1);
 
27
  }
 
28
 
 
29
  /* Open inp file */
 
30
  fp_inp = fopen(argv[1],"r");
 
31
  if ( !fp_inp ){
 
32
         printf("Unable to open inp file %s for reading.\n",argv[1]);
 
33
         exit(1);
 
34
  }
 
35
 
 
36
  /* Open dx file */
 
37
  fp_dx = fopen(argv[2],"w");
 
38
  if ( !fp_dx ){
 
39
         printf("Unable to open dx file %s for writing.\n",argv[2]);
 
40
         exit(1);
 
41
  }
 
42
  
 
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);
 
48
 
 
49
  /* Allocate memory for data */
 
50
  data         = (float *) malloc( nvars*sizeof(float) );
 
51
  objects_size = (int *)   malloc( nvars*sizeof(int) );
 
52
  
 
53
  /* A warning */
 
54
  printf("I assume the elements are tetrahedrons...\n");
 
55
  
 
56
  /* Positions */
 
57
  printf("Reading positions...");
 
58
  fflush(stdout);
 
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);
 
61
  
 
62
  for (i=0;i<npoints;i++){
 
63
         
 
64
         /* Read values */
 
65
         fscanf(fp_inp,"%d %f %f %f\n",&dummy1,&x,&y,&z);
 
66
         
 
67
         if ( i % (npoints/20) == 0 ){
 
68
                printf(".");
 
69
                fflush(stdout);
 
70
         }
 
71
         
 
72
         /* Save values */
 
73
         fprintf(fp_dx,"%f %f %f\n",x,y,z);
 
74
         
 
75
  }  printf("\n");
 
76
  
 
77
  /* Elements */
 
78
  printf("Reading elements....");
 
79
  fflush(stdout);
 
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++){
 
83
         
 
84
         /* Read values */
 
85
         fscanf(fp_inp,"%d %d %s",&dummy1,&dummy2,&element_type);
 
86
         fscanf(fp_inp,"%d %d %d %d\n",&p1,&p2,&p3,&p4);
 
87
         
 
88
         if ( i % (nelems/20) == 0 ){
 
89
                printf(".");
 
90
                fflush(stdout);
 
91
         }
 
92
         
 
93
         /* Save values */
 
94
         fprintf(fp_dx,"%d %d %d %d\n",p1-1,p2-1,p3-1,p4-1);
 
95
         
 
96
  }
 
97
  fprintf(fp_dx,"attribute \"element type\" string \"tetrahedra\"\n");
 
98
  fprintf(fp_dx,"attribute \"ref\" string \"positions\"\n");
 
99
  printf("\n");
 
100
  
 
101
  /* Read object sizes */
 
102
  fscanf(fp_inp,"%d",&objects_n);
 
103
  if ( objects_n == 1 )
 
104
         printf("Found %d object.\n",objects_n);
 
105
  else
 
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]);
 
110
  }
 
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");
 
114
  
 
115
  /* Skip a few lines */
 
116
  for (i=0;i<(objects_n);i++)
 
117
         while ( getc(fp_inp) != '\n' );
 
118
  
 
119
  /* Values */  
 
120
  printf("Reading values......");
 
121
  fflush(stdout);
 
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++){
 
126
         
 
127
         /* Read values */
 
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);
 
132
         
 
133
         if ( i % (npoints/20) == 0 ){
 
134
                printf(".");
 
135
                fflush(stdout);
 
136
         }
 
137
         
 
138
         /* Save values */
 
139
         for (j=0;j<(nvars-1);j++)
 
140
                fprintf(fp_dx,"%f ",data[j]);
 
141
         fprintf(fp_dx,"%f\n",data[j]);
 
142
         
 
143
  }
 
144
  fprintf(fp_dx,"attribute \"dep\" string \"positions\"\n");
 
145
  printf("\n");
 
146
 
 
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");
 
154
 
 
155
  /* Delete memory for data */
 
156
  free(data);
 
157
  free(objects_size);
 
158
  
 
159
  /* Close files */
 
160
  fclose(fp_inp);
 
161
  fclose(fp_dx);
 
162
  
 
163
  return 0;
 
164
}