ensightgold_writecbin.c [SRC] [CPP] [JOB] [SCAN]
TOOLS / EXTERNAL



   1 | /* -*- Mode: c -*-
   2 |  * 
   3 |  * $Id: ensightgold_writecbin.c,v 1.1 2007/01/30 10:43:12 avbp Exp $
   4 |  *
   5 |  * AUTHOR:       Charles MARTIN
   6 |  * ORG:          CERFACS (http://www.cerfacs.fr)
   7 |  * E-MAIL:       martin@cerfacs.fr
   8 |  *
   9 |  * ORIG-DATE:     8-Sep-04 at 17:56:04
  10 |  * LAST-MOD:     11-2006 by J. Amaya, O. CabritA>, A. Roux
  11 |  *
  12 |  * DESCRIPTION:  
  13 |  * DESCRIP-END.
  14 |  */
  15 | 
  16 | #include <stdlib.h>
  17 | #include <stdio.h>
  18 | #include <math.h>
  19 | #include <string.h> 
  20 | 
  21 | #define STRINGLEN 80
  22 | #define TRUE 1
  23 | #define FALSE 0
  24 | 
  25 | /* Swtich to BigEndian if necessary */
  26 | #if defined (SEKHMET) 
  27 | #  define FWRITE fwrite_byteswap
  28 | #  define BYTESWAPIO TRUE
  29 | #elif defined (IMHOTEP)
  30 | #  define FWRITE fwrite_byteswap
  31 | #  define BYTESWAPIO TRUE
  32 | #elif defined (CORAIL)
  33 | #  define FWRITE fwrite_byteswap
  34 | #  define BYTESWAPIO TRUE
  35 | #elif defined (PCLINUX)|defined(KALI)
  36 | #  define FWRITE fwrite_byteswap
  37 | #  define BYTESWAPIO TRUE
  38 | #else
  39 | #  define FWRITE fwrite
  40 | #  define BYTESWAPIO FALSE
  41 | #endif
  42 |  
  43 | 
  44 | #if defined (NO_UNDERSCORE)
  45 | #  define ENSIGHTGOLD_WRITE_GEOCOOR_BIN ensightgold_write_geocoor_bin
  46 | #  define ENSIGHTGOLD_WRITE_GEOELT_BIN ensightgold_write_geoelt_bin
  47 | #  define WRITE_REAL_SCALAR_BIN write_real_scalar_bin
  48 | #  define WRITE_REAL_VECTOR_BIN write_real_vector_bin
  49 | #else
  50 | #  define ENSIGHTGOLD_WRITE_GEOCOOR_BIN ensightgold_write_geocoor_bin_
  51 | #  define ENSIGHTGOLD_WRITE_GEOELT_BIN ensightgold_write_geoelt_bin_
  52 | #  define WRITE_REAL_SCALAR_BIN write_real_scalar_bin_
  53 | #  define WRITE_REAL_VECTOR_BIN write_real_vector_bin_
  54 | #endif
  55 | 
  56 | 
  57 | /*#define ENSIGHTGOLD_WRITE_GEOCOOR_BIN ensightgold_write_geocoor_bin_
  58 |   #define ENSIGHTGOLD_WRITE_GEOELT_BIN ensightgold_write_geoelt_bin_ */
  59 | /*===========================================================================*/
  60 | int fwrite_byteswap ( const void* pTo, size_t size, int mItems, FILE *binFile ) {
  61 | /* Thanks to Jens Muller for this Routine */
  62 | /* It does not swap when it's  a character string */
  63 |   static char *pData ;
  64 |   static int k ;
  65 |   
  66 |   if ( size%2 )
  67 |     /* Size is odd. No swapping. The only case I could see is 1 for char. */
  68 |     return ( fwrite ( pTo, size, mItems, binFile ) ) ;
  69 |   else {
  70 |     /* Perform swapping. */
  71 |     for ( pData = ( char * ) pTo ;
  72 |           pData < ( char * ) pTo + size*mItems ; pData += size )
  73 |       for ( k = size-1 ; k >= 0 ; k-- )
  74 |         if ( !fwrite ( pData+k, 1, 1, binFile ) )
  75 |           /* FWRITE failed. */
  76 |           return ( 0 ) ;
  77 |     
  78 |     return ( mItems ) ;
  79 |   }
  80 | }
  81 | 
  82 | /*=================================================================*/
  83 |  void clearstring(char *string2clear, int stringlength)
  84 | {
  85 |   int i;
  86 |   for (i=0; i<stringlength; i++)
  87 |     string2clear[i]=0;
  88 |   return;
  89 |   } 
  90 | 
  91 | /*=================================================================*/
  92 | void ENSIGHTGOLD_WRITE_GEOCOOR_BIN (const char filename[STRINGLEN],
  93 |                                 const int* ln,
  94 |                                 const int* nnode,
  95 |                                 float *coorx,
  96 |                                 float *coory,
  97 |                                 float *coorz)
  98 | {
  99 |   int i;
 100 |   char cfilename[STRINGLEN+1];
 101 |   char buffer[80];
 102 |   int int_bufel;
 103 |   FILE *fptr;
 104 |   
 105 |   /* string conversion Fortran -> C */
 106 |   for (i=0; i<STRINGLEN; i++)
 107 |     cfilename[i]=filename[i];
 108 |   cfilename[*ln]='\0';
 109 |   /* if (BYTESWAPIO)
 110 |      printf("****** Warning Swapping I/O Endianess *******\n"); */
 111 | 	
 112 |   /* opening file */
 113 |   fptr = fopen(cfilename,"wb"); 
 114 | 
 115 |  /* writing header */
 116 |   clearstring(buffer,STRINGLEN);
 117 |   strcpy(buffer,"C binary");
 118 |   FWRITE(buffer,sizeof(char),STRINGLEN,fptr);
 119 | 
 120 |   clearstring(buffer,STRINGLEN);
 121 |   strcpy(buffer,"Ensight Gold Geometry File");
 122 |   FWRITE(buffer,sizeof(char),STRINGLEN,fptr);
 123 |   
 124 |   clearstring(buffer,STRINGLEN);
 125 |   strcpy(buffer,"Generated by a2gold translator");
 126 |   FWRITE(buffer,sizeof(char),STRINGLEN,fptr);
 127 | 
 128 |   clearstring(buffer,STRINGLEN);
 129 |   strcpy(buffer,"node id assign");
 130 |   FWRITE(buffer,sizeof(char),STRINGLEN,fptr);
 131 |   
 132 |   clearstring(buffer,STRINGLEN);
 133 |   strcpy(buffer,"element id assign");
 134 |   FWRITE(buffer,sizeof(char),STRINGLEN,fptr);
 135 |   
 136 |   clearstring(buffer,STRINGLEN);
 137 |   strcpy(buffer,"part");
 138 |   FWRITE(buffer,sizeof(char),STRINGLEN,fptr);
 139 | 
 140 |   int_bufel= 1;
 141 |   FWRITE(&int_bufel,sizeof(int),1,fptr);
 142 | 
 143 |   clearstring(buffer,STRINGLEN);
 144 |   strcpy(buffer,"First part");
 145 |   FWRITE(buffer,sizeof(char),STRINGLEN,fptr);
 146 | 
 147 |   clearstring(buffer,STRINGLEN);
 148 |   strcpy(buffer,"coordinates");
 149 |   FWRITE(buffer,sizeof(char),STRINGLEN,fptr);
 150 |   
 151 |   int_bufel= *nnode;
 152 |   FWRITE(&int_bufel,sizeof(int),1,fptr);
 153 |   /* Write node coordinates */
 154 |   FWRITE(coorx,sizeof(float),*nnode,fptr);
 155 |   FWRITE(coory,sizeof(float),*nnode,fptr);
 156 |   FWRITE(coorz,sizeof(float),*nnode,fptr);
 157 |   
 158 | 
 159 |   
 160 | 
 161 |   /* closing file */
 162 |   fclose(fptr);
 163 | 
 164 |   return;
 165 | }
 166 | 
 167 | /*=================================================================*/
 168 | void ENSIGHTGOLD_WRITE_GEOELT_BIN  (const char filename[STRINGLEN],
 169 |                                    const int* ln,
 170 |                                    const char elt_type[STRINGLEN],
 171 |                                    const int* ln_etyp,
 172 |                                     const int* nodeperelt,
 173 |                                    const int* nelement,
 174 |                                    const int  *iel2nod)
 175 |  {
 176 |    int i;
 177 |    char cfilename[STRINGLEN+1];
 178 |    char celt_type[STRINGLEN+1];
 179 |    char buffer[80];
 180 |    int int_bufel;
 181 |    FILE *fptr;
 182 | 
 183 |    /* string conversion Fortran -> C */
 184 |    for (i=0; i<STRINGLEN; i++)
 185 |      {
 186 |        cfilename[i]=filename[i];
 187 |        celt_type[i]=elt_type[i];
 188 |      }
 189 |    cfilename[*ln]='\0';
 190 |    celt_type[*ln_etyp]='\0';
 191 |    
 192 |    /* opening file */
 193 |    fptr = fopen(cfilename,"ab"); 
 194 | 
 195 |   /* Element section */
 196 |   clearstring(buffer,STRINGLEN);
 197 |   strcpy(buffer,celt_type);
 198 |   FWRITE(buffer,sizeof(char),STRINGLEN,fptr);
 199 |  
 200 |   int_bufel= *nelement;
 201 |   FWRITE(&int_bufel,sizeof(int),1,fptr);
 202 |   /* swaping if necessary and cast int4 to long8 */
 203 |   for (i=0;i<(*nelement)*(*nodeperelt);i++)
 204 |     {
 205 |       int_bufel= iel2nod[i];
 206 |       FWRITE(&int_bufel,sizeof(int),1,fptr);
 207 |     }
 208 |   
 209 |       fclose(fptr);
 210 |       return;
 211 |  }
 212 | 
 213 | void WRITE_REAL_SCALAR_BIN  (const char filename[STRINGLEN],
 214 |                              const int* ln,
 215 |                              const char field_name[STRINGLEN],
 216 |                              const int* fln,
 217 |                              float *scalarfield,
 218 |                              const int* nnode)
 219 |  {
 220 |    int i;
 221 |    char cfilename[STRINGLEN+1];
 222 |    char cfield_name[STRINGLEN+1];
 223 |    char buffer[80];
 224 |    int int_bufel;
 225 |    FILE *fptr;
 226 | 
 227 |    /* string conversion Fortran -> C */
 228 |    for (i=0; i<STRINGLEN; i++)
 229 |      {
 230 |        cfilename[i]=filename[i];
 231 |        cfield_name[i]=field_name[i];
 232 |      }
 233 |    cfilename[*ln]='\0';
 234 |    cfield_name[*fln]='\0';
 235 |    
 236 |    /* opening file */
 237 |    fptr = fopen(cfilename,"wb");
 238 |    /* write field name */
 239 |    clearstring(buffer,STRINGLEN);
 240 |    strcpy(buffer,cfield_name);
 241 |    FWRITE(buffer,sizeof(char),STRINGLEN,fptr);
 242 | 
 243 |    clearstring(buffer,STRINGLEN);
 244 |    strcpy(buffer,"part");
 245 |    FWRITE(buffer,sizeof(char),STRINGLEN,fptr);
 246 |    
 247 |    int_bufel= 1;
 248 |    FWRITE(&int_bufel,sizeof(int),1,fptr);   
 249 |    
 250 |    clearstring(buffer,STRINGLEN);
 251 |    strcpy(buffer,"coordinates");
 252 |    FWRITE(buffer,sizeof(char),STRINGLEN,fptr);
 253 |    
 254 |    FWRITE(scalarfield,sizeof(float),*nnode,fptr);
 255 |    
 256 |    fclose(fptr);
 257 |    return;
 258 |  }
 259 | /*===========================================================================*/
 260 | void WRITE_REAL_VECTOR_BIN  (const char filename[STRINGLEN],
 261 |                              const int* ln,        
 262 |                              const char field_name[STRINGLEN],
 263 |                              const int* fln,
 264 |                              float *component1,
 265 |                              float *component2,
 266 |                              float *component3,
 267 |                              const int* nnode)
 268 |  {
 269 |    int i;
 270 |    char cfilename[STRINGLEN+1];
 271 |    char cfield_name[STRINGLEN+1];
 272 |    char buffer[80];
 273 |    int int_bufel;
 274 |    FILE *fptr;
 275 | 
 276 |    /* string conversion Fortran -> C */
 277 |    for (i=0; i<STRINGLEN; i++)
 278 |      {
 279 |        cfilename[i]=filename[i];
 280 |        cfield_name[i]=field_name[i];
 281 |      }
 282 |    cfilename[*ln]='\0';
 283 |    cfield_name[*fln]='\0';
 284 |    
 285 |    /* opening file */
 286 |    fptr = fopen(cfilename,"wb");
 287 |    /* write field name */
 288 |    clearstring(buffer,STRINGLEN);
 289 |    strcpy(buffer,cfield_name);
 290 |    FWRITE(buffer,sizeof(char),STRINGLEN,fptr);
 291 | 
 292 |    clearstring(buffer,STRINGLEN);
 293 |    strcpy(buffer,"part");
 294 |    FWRITE(buffer,sizeof(char),STRINGLEN,fptr);
 295 |    
 296 |    int_bufel= 1;
 297 |    FWRITE(&int_bufel,sizeof(int),1,fptr);   
 298 |    
 299 |    clearstring(buffer,STRINGLEN);
 300 |    strcpy(buffer,"coordinates");
 301 |    FWRITE(buffer,sizeof(char),STRINGLEN,fptr);
 302 |    
 303 |    FWRITE(component1,sizeof(float),*nnode,fptr);
 304 |    FWRITE(component2,sizeof(float),*nnode,fptr);
 305 |    FWRITE(component3,sizeof(float),*nnode,fptr);   
 306 |    
 307 |    fclose(fptr);
 308 |    return;
 309 |  }
 310 | 
 311 | 
 312 |