/*!WARNING!WARNING!WARNING!WARNING!WARNING!WARNING!WARNING!WARNING!WARNING!*/
/*NING!WARNING!WARNING!WARNING!WARNING!WARNING!WARNING!WARNING!WARNING!WARN*/
/*!WARNING!WARNING!WARNING!WARNING!WARNING!WARNING!WARNING!WARNING!WARNING!*/
/*                                                                         */
/*  This file is used in prostar, star4 and comet.  It MUST BE EXACTLY     */
/*  the same for all systems.                                              */
/*                                                                         */
/*!WARNING!WARNING!WARNING!WARNING!WARNING!WARNING!WARNING!WARNING!WARNING!*/
/*NING!WARNING!WARNING!WARNING!WARNING!WARNING!WARNING!WARNING!WARNING!WARN*/
/*!WARNING!WARNING!WARNING!WARNING!WARNING!WARNING!WARNING!WARNING!WARNING!*/

/*
 *  Program: pro-am - polyfile
 *  Author:  Rajeev Palankar
 *  Date:    2002 September 27.
 *  Desc:    Performs all IO to/from face base model file.
 *
 *  pro-am Copyright (C) 2002 by adapco, Ltd.
 *
 *  This program is the property of adapco, Ltd. and contains
 *  confidential and proprietary information.  The unauthorized use,
 *  distribution, or duplication of this program is prohibited.
 *  All rights reserved.
 *
 *  $Id: polyfile.h,v 1.1 2004/01/26 19:12:05 prewett Exp $
 */

#ifndef __POLYFILE_H__
#define __POLYFILE_H__


#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <rpc/rpc.h>


/*
 * A description of the file format can be found at the end of this document.
 */


#ifdef __cplusplus
#define EXTERN
extern "C" {
#else
#define EXTERN extern
#endif

/*    Required for FORTRAN COMPILATION
 *    ALPHA - OSF1              __alpha or __alpha__
 *    Cray                      __cray (this is not official, if am using it
 *                                      now to clean up the code)
 *    HP - UX:                  hpux, __hpux or __hpux__
 *    IBM - AIX:                _AIX, _IBMR2 or __IBMR2__
 *    Linux:                    linux or __linux__
 *    Silicon Graphics - IRIX:  sgi   (gcc or cc -cckr)
 *                              __sgi (cc -cckr, cc -ansi or cc -ansiposix)
 *                              __sgi__
 *    Sun                       sun, __sun or __sun__
 *    VAX:                      __vax (this is not official, if am using it
 *                                     now to clean up the code)
 *    Windows 3.1               __win31 (this is not official, if am using it
*/
#if defined(__alpha) || defined(__alpha__) || defined(linux) || defined(__linux__)
#define FUNTRAILUND
#endif


#ifdef __cray
#define FUNUPPERCASE
#endif

#if defined(sgi) || defined(__sgi) || defined(__sgi__)
#define FUNTRAILUND
#endif

#if defined(sun) || defined(__sun) || defined(__sun__)
#define FUNTRAILUND
#endif



#ifdef FUNTRAILUND
#define polyfile_open polyfile_open_
#define polyfile_writevertex polyfile_writevertex_
#define polyfile_writeshell polyfile_writeshell_
#define polyfile_writeboundary polyfile_writeboundary_
#define polyfile_writefaces polyfile_writefaces_
#define polyfile_writecell polyfile_writecell_
#define polyfile_writecellsp polyfile_writecellsp_
#define polyfile_writevmap polyfile_writevmap_
#define polyfile_writesmap polyfile_writesmap_
#define polyfile_writebmap polyfile_writebmap_
#define polyfile_writefmap polyfile_writefmap_
#define polyfile_writecmap polyfile_writecmap_
#define polyfile_writecmapsp polyfile_writecmapsp_
#define polyfile_writedvertex polyfile_writedvertex_
#define polyfile_readheader polyfile_readheader_
#define polyfile_readvertex polyfile_readvertex_
#define polyfile_readshell polyfile_readshell_
#define polyfile_readboundary polyfile_readboundary_
#define polyfile_readfaces polyfile_readfaces_
#define polyfile_readcell polyfile_readcell_
#define polyfile_readcellsp polyfile_readcellsp_
#define polyfile_readdvertex polyfile_readdvertex_
#define polyfile_readvmap polyfile_readvmap_
#define polyfile_readsmap polyfile_readsmap_
#define polyfile_readbmap polyfile_readbmap_
#define polyfile_readfmap polyfile_readfmap_
#define polyfile_readcmap polyfile_readcmap_
#define polyfile_readcmapsp polyfile_readcmapsp_
#define polyfile_endtag polyfile_endtag_
#define polyfile_close polyfile_close_
#define polyfile_skipread polyfile_skipread_
#define polyfile_readinterface   polyfile_readinterface_
#define polyfile_writeinterface  polyfile_writeinterface_
#define polyfile_readscalar   polyfile_readscalar_
#define polyfile_writescalar  polyfile_writescalar_
#endif

#ifdef  FUNUPPERCASE
#define polyfile_open POLYFILE_OPEN
#define polyfile_writevertex POLYFILE_WRITEVERTEX
#define polyfile_writeshell POLYFILE_WRITESHELL
#define polyfile_writeboundary POLYFILE_WRITEBOUNDARY
#define polyfile_writefaces POLYFILE_WRITEFACES
#define polyfile_writecell POLYFILE_WRITECELL
#define polyfile_writecellsp POLYFILE_WRITECELLSP
#define polyfile_writevmap POLYFILE_WRITEVMAP
#define polyfile_writesmap POLYFILE_WRITESMAP
#define polyfile_writebmap POLYFILE_WRITEBMAP
#define polyfile_writefmap POLYFILE_WRITEFMAP
#define polyfile_writecmap POLYFILE_WRITECMAP
#define polyfile_writecmapsp POLYFILE_WRITECMAPSP
#define polyfile_writedvertex POLYFILE_WRITEDVERTEX
#define polyfile_readheader POLYFILE_READHEADER
#define polyfile_readvertex POLYFILE_READVERTEX
#define polyfile_readshell POLYFILE_READSHELL
#define polyfile_readboundary POLYFILE_READBOUNDARY
#define polyfile_readfaces POLYFILE_READFACES
#define polyfile_readcellsp POLYFILE_READCELLSP
#define polyfile_readdvertex POLYFILE_READDVERTEX
#define polyfile_readvmap POLYFILE_READVMAP
#define polyfile_readsmap POLYFILE_READSMAP
#define polyfile_readbmap POLYFILE_READBMAP
#define polyfile_readfmap POLYFILE_READFMAP
#define polyfile_readcmap POLYFILE_READCMAP
#define polyfile_readcmapsp POLYFILE_READCMAPSP
#define polyfile_endtag POLYFILE_ENDTAG
#define polyfile_close POLYFILE_CLOSE
#define polyfile_skipread POLYFILE_SKIPREAD
#define polyfile_readinterface   POLYFILE_READINTERFACE
#define polyfile_writeinterface  POLYFILE_WRITEINTERFACE
#define polyfile_readscalar   POLYFILE_READSCALAR
#define polyfile_writescalar  POLYFILE_WRITESCALAR
#endif

/*
 *  A note on the meaning of meaning.  Meaning is a character spring that
 *  can contains up to 511 characters.  In C, this means that you must define
 *  it as follows: char meaning[512].  In Fortran, define it as CHARACTER*512
 *  MEANING.  When writing meaning, only the first mlen characters are written.
 *  When reading meaning, mlen is set to the string size from the file and
 *  only the first mlen characters are set.
 */


EXTERN void polyfile_open (char *fname, int *flen, int *rw, int *ierr);
/*
open polyfile to read/write
*fname = filename               "test.ngeom"
*flen  = length of filename     "10  in above fname"
*rw    = read/write flag        "0=write,1=read,2=write coded (debug ONLY)"
*ierr  = error code             "0 if successfull else check below"
*/

EXTERN void polyfile_readheader    (int *type, int *tag, int *no, int *maxid, int *ierr);
/*
read header information from the file
*type   = type of information to follow
        =1  vertex
        =2  shell
        =3  boundary
        =4  face
        =5  cell
        =6  vertex map
        =7  shell map
        =8  boundary map
        =9  face map
        =10 cell map
        =11 vertex double precision
        =12 Interface Section
*tag    =tag value for type
*no     =no of items in the current tag/type
*maxid  =maximum id in the current tag/type
*ierr = error code              "0 if successfull else check below"
*/

EXTERN void polyfile_readvertex    (int *id, float *x, float *y, float *z,
                                     int *ierr);
EXTERN void polyfile_readdvertex    (int *id, double *x, double *y, double *z,
                                     int *ierr);
/*
read a vertex from the file
*id     =vertex id
*x      =x co-ord of the vertex
*y      =y co-ord of the vertex
*z      =z co-ord of the vertex
*ierr   = error code            "0 if successfull else check below"
*/

EXTERN void polyfile_readshell     (int *id, int *nvert, int **vlis,
                                     int *ierr);
/*
read a shell from the file
*id     =shell id
*nvert  =number of vertices in the array pointed by (*vlis)
**vlis  =shell information, the info must be copied into a
         different memory location before calling the read read.
*ierr   = error code            "0 if successfull else check below"
*/

EXTERN void polyfile_readboundary (int *id, int *ncellid, int *nfaceid,
                                   int *bndId, int *patch,
                                   int *nvert, int **vlis, int *ierr);
/*
read a boundary faces from the file
*id     =boundary id
*ncellid=cell associated with face
Added for version 2 of the file.  5-December-2002 WRO
*nfaceid=cell prostar face id
Added for version 2 of the file.  5-December-2002 WRO
*patch  =radiation patch id
*nvert  =number of vertices in the array pointed by (*vlis)
**vlis  =boundary vertex information, the info must be copied into a
         different memory location before calling the read read.
*ierr   = error code            "0 if successfull else check below"
*/

EXTERN void polyfile_readfaces     (int *id, int *lcell, int *lface,
                                             int *rcell, int *rface,
                                     int *nvert, int **vlis, int *ierr);
/*
read a face from the file
*id     =face id
*lcell  =1st cell associated with face
Added for version 2 of the file.  5-December-2002 WRO
*lface  =1st cell prostar face id
*rcell  =2nd cell associated with face
Added for version 2 of the file.  5-December-2002 WRO
*rface  =2st cell prostar face id
*nvert  =number of vertices in the array pointed by (*vlis)
**vlis  =face vertex information, the info must be copied into a
         different memory location before calling the read read.
*ierr   = error code            "0 if successfull else check below"
*/

EXTERN void polyfile_readcell      (int *id, int *ierr);
/*
read a cell from the file
*id     =cell id
*ierr   = error code            "0 if successfull else check below"
*/

EXTERN void polyfile_readcellsp      (int *id, int *info, int *ierr);
/*
read a cell from the file
*id     =cell id
*info   =cell info
*ierr   = error code            "0 if successfull else check below"
*/

EXTERN void polyfile_readvmap   (int *tag, char *meaning, int *mlen,
                                int *ierr);
/*
read vertex tag information
*tag    = tag information
*meaning= information regarding tag
*mlen   =length of *meaning
*ierr   = error code            "0 if successfull else check below"
*/

EXTERN void polyfile_readsmap      (int *tag, char *meaning, int *mlen,
                                        int *ierr);
/*
read shell tag information
*tag    = tag information
*meaning= information regarding tag
*mlen   =length of *meaning
*ierr   = error code            "0 if successfull else check below"
*/

EXTERN void polyfile_readbmap      (int *tag, char *meaning, int *mlen,
                                     int *ierr);
/*
read boundary tag information
*tag    = tag information
*meaning= information regarding tag
*mlen   =length of *meaning
*ierr   = error code            "0 if successfull else check below"
*/

EXTERN void polyfile_readfmap      (int *tag, char *meaning, int *mlen,
                                     int *ierr);
/*
read face tag information
*tag    = tag information
*meaning= information regarding tag
*mlen   =length of *meaning
*ierr   = error code            "0 if successfull else check below"
*/

EXTERN void polyfile_readcmap      (int *tag, char *meaning, int *mlen,
                                     int *ierr);
EXTERN void polyfile_readcmapsp    (int *tag, char *meaning, int *mlen,
                                     int *ierr);

/*
read cell tag information
*tag    = tag information
*meaning= information regarding tag
*mlen   =length of *meaning
*ierr   = error code            "0 if successfull else check below"
*/

EXTERN void polyfile_writevertex   (int *id, float *x, float *y, float *z,
                                    int *ierr);
EXTERN void polyfile_writedvertex  (int *id, double *x, double *y, double *z,
                                    int *ierr);
/*
write a vertex to file
*id     =vertex id
*x      =x co-ord of the vertex
*y      =y co-ord of the vertex
*z      =z co-ord of the vertex
*ierr   = error code            "0 if successfull else check below"
*/

EXTERN void polyfile_writeshell    (int *id, int *nvert, int *vlis, int *ierr);
/*
write a shell to file
*id     =shell id
*nvert  =number of vertices in the array pointed by vlis
*vlis   =shell information.
*ierr   = error code            "0 if successfull else check below"
*/


EXTERN void polyfile_writeboundary (int *id, int *ncellid, int *nfaceid,
                                    int *bndId, int *patch,
                                    int *nvert, int *vlis, int *ierr);
/*
write a boundary faces to file
*id     =boundary id
*ncellid=cell associated with face
Added for version 2 of the file.  5-December-2002 WRO
*nfaceid=cell prostar face id
Added for version 2 of the file.  12-December-2002 WRO
*bndId  =prostar id for this boundary.
Added for version 2 of the file.  5-December-2002 WRO
*patch  =radiation patch id
*nvert  =number of vertices in the array pointed by (*vlis)
*vlis   =boundary vertex information.
*ierr   = error code            "0 if successfull else check below"
*/

EXTERN void polyfile_writefaces    (int *id, int *lcell, int *lface,
                                             int *rcell, int *rface,
                                    int *nvert, int *vlis, int *ierr);
/*
write a face to file
*id     =face id
*lcell  =1st cell associated with face
Added for version 2 of the file.  5-December-2002 WRO
*lface  =1st cell prostar face id
*rcell  =2nd cell associated with face
Added for version 2 of the file.  5-December-2002 WRO
*rface  =2st cell prostar face id
*nvert  =number of vertices in the array pointed by (*vlis)
*vlis   =face vertex information.
*ierr   = error code            "0 if successfull else check below"
*/

EXTERN void polyfile_writecell     (int *id, int *ierr);
/*
write a cell to file
*id     =cell id
*ierr   = error code            "0 if successfull else check below"
*/

EXTERN void polyfile_writecellsp     (int *id, int *info, int *ierr);
/*
write a cell to file
*id     =cell id
*info   =cell info
*ierr   = error code            "0 if successfull else check below"
*/

EXTERN void polyfile_writevmap     (int *tag, char *meaning, int *mlen,
                                     int *ierr);
/*
write vertex tag information
*tag    = tag information
*meaning= information regarding tag
*mlen   =length of *meaning
*ierr   = error code            "0 if successfull else check below"
*/

EXTERN void polyfile_writesmap     (int *tag, char *meaning, int *mlen,
                             int *ierr);
/*
write shell tag information
*tag    = tag information
*meaning= information regarding tag
*mlen   =length of *meaning
*ierr   = error code            "0 if successfull else check below"
*/

EXTERN void polyfile_writebmap     (int *tag, char *meaning, int *mlen,
                             int *ierr);
/*
write boundary tag information
*tag    = tag information
*meaning= information regarding tag
*mlen   =length of *meaning
*ierr   = error code            "0 if successfull else check below"
*/

EXTERN void polyfile_writefmap     (int *tag, char *meaning, int *mlen,
                                     int *ierr);
/*
write face tag information
*tag    = tag information
*meaning= information regarding tag
*mlen   =length of *meaning
*ierr   = error code            "0 if successfull else check below"
*/

EXTERN void polyfile_writecmap     (int *tag, char *meaning, int *mlen,
                                     int *ierr);
EXTERN void polyfile_writecmapsp   (int *tag, char *meaning, int *mlen,
                                     int *ierr);
/*
read cell tag information
*tag    = tag information
*meaning= information regarding tag
*mlen   =length of *meaning
*ierr   = error code            "0 if successfull else check below"
*/

EXTERN void polyfile_endtag        (int *tag, int *ierr);
/*
mark the end of the tag
*tag    = tag number (required only in write mode)
*ierr   = error code            "0 if successfull else check below"
*/


EXTERN void polyfile_readinterface  (int *faceid1, int *bound1, int *patch1,
                                     int *faceid2, int *bound2, int *patch2,
                                     int *ierr);
EXTERN void polyfile_writeinterface (int *faceid1, int *bound1, int *patch1,
                                     int *faceid2, int *bound2, int *patch2,
                                     int *ierr);
/*
  Read/Write interface Section
*faceid1= Face number associated with the bound1.
*bound1 = Region Number associated with baffle boundary or "0" if baffle cell
Added for version 2 of the file.  5-December-2002 WRO
*patch1 = Radiation patch id associated with the bound1.
Added for version 2 of the file.  5-December-2002 WRO
*faceid2= Face number associated with the bound2.
*bound2 = Region Number associated with baffle boundary or "0" if baffle cell
Added for version 2 of the file.  5-December-2002 WRO
*patch2 = Radiation patch id associated with the bound2.
Added for version 2 of the file.  5-December-2002 WRO
*ierr   = error code            "0 if successfull else check below"
*/


EXTERN void polyfile_readscalar  (float *scale, int *maxUserVertId, int *ierr);
EXTERN void polyfile_writescalar (float *scale, int *maxUserVertId, int *ierr);
/*
  Read/Write scalar Section
*scale  = The model scale.  Note that the scale has ALREADY been applied to the
          vertex coordinates.
*maxUserVertId = The maximum vertex id defined by the user in the model.
         Note that there may be more vertices than this in the model since
         vertices can be created during CP match resolution.
*ierr   = error code            "0 if successfull else check below"
*/


EXTERN void polyfile_close         (int *ierr);
/*
close the polyfile file
*ierr = error code              "0 if successfull else check below"
*ierr   =0  No error
        =1  file read write error
        =2  xdr error
        =3  not of current type(only for reading)
        =4  file in read mode
        =5  file in write mode
        =6  reached end of type #tag
        =7  did not reached end of type #tag
        =8  memory error while allocating scratch memory
        =9  reached end of file
        =10 Invalid file header identification string
        =11 Unsupported file version
*/


#ifdef __cplusplus
}
#endif

#if 0
/*    Rajeev, I want you to put here sample code for reading and writing
the file in both C and Fortran.
*/
      SUBROUTINE WRITE_FILE_IN_FORTRAN()
      CHARACTER STR*32
      COMMON/NODES/C(3,1)
C   VERTEX INFO IN C
      COMMON/CELL/CELINFO(1)
C   CELL INFORMATION IN  CELINFO
      STR(1:9)='TEST.STAR'
      CALL POLYFILE_OPEN(STR,9,0,IERR)
C  Open file file for writing
C  STR contains the file name
C  9= length of str
C  0= Flag to open file for writing
C  IERR= return code
      IF(IERR.NE.0)RETURN
      DO I =1,50
C assuming first fifty vertices in tag 1
        CALL POLYFILE_WRITEVERTEX(I,C(1,I),C(2,I),C(3,I),IERR)
C write vertex to file

C  WRITE FIRST 50 VERTEX
C  I= vertex id
C  C(1,I)=X co-ord
C  C(2,I)=Y co-ord
C  C(3,I)=Z co-ord
        IF(IERR.NE.0)RETURN
      ENDDO
      CALL POLYFILE_ENDTAG(1,IERR)
C
C CLOSE THE TAG
C Will write the tag id and number of vertices in the tag
C
      IF(IERR.NE.0)RETURN
      DO I=1,MAXE
        CALL POLYFILE_WRITECELL(CELINFO(I),IERR)
C  WRITE ALL CELL INFORMATION
C  Similiar to vertex write
        IF(IERR.NE.0)RETURN
      ENDDO
      CALL POLYFILE_ENDTAG(2,IERR)
C CLOSE THE TAG
      DO I =51,MAXN
        CALL POLYFILE_WRITEVERTEX(I,C(1,I),C(2,I),C(3,I),IERR)
C  WRITE THE REMAINING VERTEX
        IF(IERR.NE.0)RETURN
      ENDDO
      CALL POLYFILE_ENDTAG(3,IERR)
C CLOSE THE TAG
      IF(IERR.NE.0)RETURN
      STR(1:5)="TYPE1"
      CALL POLYFILE_WRITEVMAP(1,STR,5,IERR)
      IF(IERR.NE.0)RETURN
      STR(1:5)="TYPE2"
      CALL POLYFILE_WRITEVMAP(2,STR,5,IERR)
      IF(IERR.NE.0)RETURN
      STR(1:9)="CELLTYPE1"
      CALL POLYFILE_WRITECMAP(3,STR,9,IERR)
      IF(IERR.NE.0)RETURN
      CALL POLYFILE_CLOSE(IERR)
      IF(IERR.NE.0)RETURN
      RETURN
      END

      SUBROUTINE READ_FILE_IN_FORTRAN()
      CHARACTER STR*32
      COMMON/NODES/C(3,1)
C   VERTEX INFO IN C
      COMMON/CELL/CELINFO(1)
      POINTER(P_VLIS,VLIS(1))
      COMMON/FACE/IFACEINFO(20,1)
C   CELL INFORMATION IN  CELINFO
      STR(1:9)='TEST.STAR'
      CALL POLYFILE_OPEN(STR,9,1,IERR)
      IF(IERR.NE.0)RETURN
10    CALL POLYFILE_READHEADER(ITYPE,JTAG,NITEM,MAXID,IERR)
C   Reads in the header information from the file
C  RETURN
C     ITYPE=type of item
C     JTAG= tag number
C     NITEM=no of items in the tag
C     IERR=ERROR code
C   ITYPE =
C         =1  vertex
C         =2  shell
C         =3  boundary
C         =4  face
C         =5  cell
C         =6  vertex map
C         =7  shell map
C         =8  boundary map
C         =9  face map
C         =10 cell  map
      IF(IERR.EQ.9)GOTO 1001
      IF(IERR.NE.0)RETURN
      SELECT CASE (ITYPE)
        CASE (1)
          DO I=1,NITEM
            CALL POLYFILE_READVERTEX(J,X,Y,Z,IERR)
            IF(IERR.NE.0)RETURN
            C(J,1)=X
            C(J,2)=Y
            C(J,3)=Z
          ENDDO
        CASE (2)
C  same as case 4
        CASE (3)
C  same as case 4
        CASE (4)
          DO I=1,NITEM
            CALL POLYFILE_READFACES(IFACEN,ILCELL,ILFACE,IRCELL,IRFACE,
     1        NVERT,P_VLIS,IERR)
C   Subroutine polyfile_readfaces polyfile_readshell return pointer
C   to integer array, the information from this array must be stored in
C   a different array before calling the next read statment or else the
C   information will be overwritten
C

            DO J=1,NVERT
              FACEINFO(J,IFACEN)=VLIS(J)
            ENDDO
            IF(IERR.NE.0)RETURN
          ENDDO
        CASE (5)
          DO I=1,NITEM
            CALL POLYFILE_READCELL(J,IERR)
            IF(IERR.NE.0)RETURN
            CELINFO(I)=J
          ENDDO
        CASE (6)
          DO I=1,NITEM
            CALL POLYFILE_READVMAP(J,STR,K,IERR)
            IF(IERR.NE.0)RETURN
            CELINFO(I)=J
          ENDDO
        CASE (7)
        CASE (8)
        CASE (9)
        CASE (10)
        CASE DEFAULT
          RETURN
      END SELECT
      GOTO 10
1001  CALL POLYFILE_CLOSE(IERR)
C
C should call polyfile_close to free all the allocated memory and
C close the file.
      RETURN
      END

void write_file_in_c()
{
  char str[40]="test.star";
  float *c[3];
  int   *celinfo;
  int i,j,k,ierr;
  i=9;j=0;
  polyfile_open(str,&i,&j,&ierr);
/*
C  Open file file for writing
C  STR contains the file name
C  9= length of str
C  0= Flag to open file for writing
C  IERR= return code
*/
  if(ierr!=0)return;
  for(i=0;i<50;i++)
  {
/*assuming first 50 vertices in tag 1*/
    j=i+1;
    polyfile_writevertex(&j,c[1][i],c[2][i],c[3][i],&ierr);
/*
C  WRITE FIRST 50 VERTEX
C  I= vertex id
C  C(1,I)=X co-ord
C  C(2,I)=Y co-ord
C  C(3,I)=Z co-ord
*/
  if(ierr!=0)return;
  }
  j=1;
  polyfile_endtag(&j,&ierr);
/*
C
C CLOSE THE TAG
C Will write the tag id and number of vertices in the tag
C
*/
  if(ierr!=0)return;
  for(i=0;i<maxe;i++)
  {
    j=i+1;
    polyfile_writecell(celinfo[i],&ierr);
/*
  write all the cell information similiar to vertices
*/
  if(ierr!=0)return;
  }
  j=2;
  polyfile_endtag(&j,&ierr);
  if(ierr!=0)return;
  for(i=50;i<maxn;i++)
  {
    j=i+1;
    polyfile_writevertex(&j,c[1][i],c[2][i],c[3][i],&ierr);
  if(ierr!=0)return;
  }
  j=3;
  polyfile_endtag(&j,&ierr);
  if(ierr!=0)return;
  strcpy(str,"type1");
  i=1;
  j=5;
  polyfile_writevmap(&i,str,&j,&ierr);
  if(ierr!=0)return;
  strcpy(str,"type2")
  i=3;
  polyfile_writevmap(&i,str,&j,&ierr);
  if(ierr!=0)return;
  strcpy(str,"celltype1");
  i=2;
   j=9;
 polyfile_writecmap(&i,str,&j,&ierr);
  if(ierr!=0)return;
  polyfile_close(&ierr);
}

void read_file_in_c()
{
  char str[40]="test.star";
  float *c[3];
  int   *celinfo;
  int i,j,k,ierr;
  int *vlis,*vlis1;
  int lcell,lface,rcell,rface;
  int **flis,amxid;
  int type,tag,nitem,l;
  float x,y,z;
  i=9;j=1;
  polyfile_open(str,&i,&j,&ierr);
  if(ierr!=0)return;
  do
  {
    polyfile_readheader(&type,&tag,&nitem,&maxid,&ierr);
/*
C   Reads in the header information from the file
C  RETURN
C     ITYPE=type of item
C     JTAG= tag number
C     NITEM=no of items in the tag
C     IERR=ERROR code
C   ITYPE =
C         =1  vertex
C         =2  shell
C         =3  boundary
C         =4  face
C         =5  cell
C         =6  vertex map
C         =7  shell map
C         =8  boundary map
C         =9  face map
C         =10 cell  map
*/
    if(ierr==9)break;
    switch(type)
    {
      case 1:
        for(i=0;i<nitem;i++)
        {
          polyfile_readvertex(&j,&x,&y,&z,&ierr);
          c[1][j-1]=x;
          c[2][j-1]=y;
          c[3][j-1]=z;
          if(ierr!=0)return;
        }
      break;
      case 2:
      /*similiar as 4*/
      break;
      case 3:
      /*similiar as 4*/
      break;
      case 4:
        for(i=0;i<nitem;i++)
        {
          polyfile_readfaces(&j,&lcell,&lface,&rcell,&rface,&k,&vlis,&ierr);
/*
C   Subroutine polyfile_readface polyfile_readshell return pointer
C   to integer array, the information from this array must be stored in
C   a different array before calling the next read statment or else the
C   information will be overwritten
C
*/
          vlis1=flis[j-1]=(int *)malloc(sizeof(int)*k);
          for(l=0;l<k;l++)vlis1[l]=vlis[l];
          if(ierr!=0)return;
        }

      break;
      case 5:
        for(i=0;i<nitem;i++)
        {
          polyfile_readcell(celinfo[i],&ierr);
          if(ierr!=0)return;
        }
      break;
      case 6:
        for(i=0;i<nitem;i++)
        {
          polyfile_readvmap(&j,str,&k,&ierr);
          if(ierr!=0)return;
        }
      break;
      case 7:
      /*similiar as 6*/
      break;
      case 8:
      /*similiar as 6*/
      break;
      case 9:
      /*similiar as 6*/
      break;
      case 10:
      /*similiar as 6*/
      break;
    }
  }while(ierr==0)
  polyfile_close(&ierr);
}
/*
ERROR CODES returned
*ierr   =0  No error
        =1  file read write error
        =2  xdr error
        =3  not of current type(only for reading)
        =4  file in read mode
        =5  file in write mode
        =6  reached end of type #tag
        =7  did not reached end of type #tag
        =8  memory error while allocating scratch memory
        =9  reached end of file(only for reading)
        =10 Invalid file header identification string
        =11 Unsupported file version
*/

#endif

#endif


/* +-----------------------------------------------------------------------+ */
/* +-----------------------------------------------------------------------+ */
/* !                                                                       ! */
/* !                       Format of the .ngeom file.                      ! */
/* !                                                                       ! */
/* +-----------------------------------------------------------------------+ */
/* +-----------------------------------------------------------------------+ */
/*

The .ngeom file is a sequential binary file (binary to save space).  However,
it must be created on a disk device since each header record is updated after
all the items have been added to a section.  Each piece of information is
written using XDR so that the file is transportable between any machine type.


File Identification:

The first bit of information written to the file is a string that identifies
the file and an integer version number.

  Field    Type     Comment
  id       string   Fixed value is "PROSTAR/STAR4 Polyfile"
  version  integer  Currently set to 2


File Sections:

Following the header record are what I describe as sections.  Each type of
section contains specific information about the model.  Each section starts
with a section header.

  Field    Type     Comment
  type     integer  type of information stored in section.
                     1 - Vertex Section - single precision
                     2 - Shell Section
                     3 - Boundary Section
                     4 - Internal Face Section
                     5 - Cell Section
                     6 - Vertex Map Section
                     7 - Shell Map Section
                     8 - Boundary Map Section
                     9 - Internal Face Map Section
                    10 - Cell Map Section
                    11 - Vertex Section - double precision
                    12 - Interface Section
                    13 - Scalar Section
  count    integer  Number of records in this section.
  tag      integer  Tag id to identify other characteristics of data in the
                    section, for example cell type or boundary type.

There can be any number of sections in any order.


Vertex Section - single precision (section header type is 1):

List of vertices, grouped according to some common feature (e.g. one may
distinguish between vertices defining the shells and vertices defining the
mesh etc.).  Note: vertex ids MUST be unique in all vertex sections.

  Field    Type     Comment
  id       integer  integer identifying vertex
  x        float    x coordinate
  y        float    y coordinate
  z        float    z coordinate


Vertex Section - double precision (section header type is 11):

Added on 10-October-2002.  This is the same as the previous vertex section
except the coordinates are stored as double precision numbers.

  Field    Type     Comment
  id       integer  integer identifying vertex
  x        double   x coordinate
  y        double   y coordinate
  z        double   z coordinate


Shell Section (section header type is 2):

List of shells (polygons which are not attached to the mesh; may be used to
describe the surface of the solution domain, so that the vertex coordinates
can be adjusted after an automatic refinement etc.), grouped according to
some common feature.  Note: shell ids MUST be unique in all shell sections.

  Field    Type     Comment
  id       integer  integer identifying shell
  count    integer  number of vertices in shell
  vertex   integer  count vertices that describe the shell


Boundary Section (section header type is 3):

List of boundary faces (including those at baffles etc, which are attached to
one Cell), grouped according to the boundary type or other criteria for
distinction.  Note: boundary ids MUST be unique in all boundary sections.

  Field    Type     Comment
  id       integer  integer identifying boundary
  neigh    integer  id of neighboring cell (must be the cell on the negative
                    side of the face)

  Added for version 2 of the file.  5-December-2002 WRO
  nfaceid  integer  prostar face id of neighboring cell.  The item is included
                    in order to facilitate:
                    1) Identification of the local cell directions used for
                       directional cell layer removal and addition, and the
                       porosity specified in the local coordinates.
                    2) Mapping between original ProStar and CPMATCH faces -
                       mapping will be used for post-processing to convert face
                       data.
                    The default value should be 0, what indicates that the
                    mapping is not available. 

  Added for version 2 of the file.  12-December-2002 WRO
  bndId    integer  prostar id for this boundary.  This item was added to make
                    the attachment boundary feature of event processing work.
                    the default value shoule be 0.

  Added for version 2 of the file.  5-December-2002 WRO
  patch    integer  radiation patch id.   The item is added as a way to group
                    boundary faces according to radiation patches they belong to.
                    If a radiation model based on the beam tracking is not
                    active, the value of *patch is irrelevant. Otherwise the
                    default value should be 0 - this indicates that the boundary
                    is not connected to any patch. 

  count    integer  number of vertices in boundary
  vertex   integer  count vertices that describe the boundary


Internal Face Section (section header type is 4):

List of internal faces which are common to two cells (cell1 and cell2),
grouped according to some common feature.  Note: internal face ids MUST be
unique in all internal face sections.

  Field    Type     Comment
  id       integer  integer identifying internal face
  cell1    integer  cell on positive side of face

  Added for version 2 of the file.  5-December-2002 WRO
  face1    integer  prostar face id of cell on positive side of face.  It is in
                    all respects equivalent to the argument nfaceid of
                    the boundary section.

  cell2    integer  cell on negative side of face

  Added for version 2 of the file.  5-December-2002 WRO
  face2    integer  prostar face id of cell on negative side of face.  It is in
                    all respects equivalent to the argument nfaceid of
                    the boundary section.
  count    integer  number of vertices in boundary
  vertex   integer  count vertices that describe the boundary


Cell Section (section header type is 5):

List of cells, grouped according to the region/domain or another property
(solid, fluid, porous etc.).  Note: cell ids MUST be unique in all cell
sections.

  Field    Type     Comment
  id       integer  integer identifying cell


Vertex Map Section (section header type is 6):
Shell Map Section (section header type is 7):
Boundary Map Section (section header type is 8):
Internal Face Map Section (section header type is 9):
Cell Map Section (section header type is 10):

All the map sections have the same format.  They are included to provide
additional information that identifies the entries in the section in question.
Currently, there is no format definition about what should be in this section.
An arbitrary information string is available to identify the information.

  Field    Type     Comment
  tag      integer  tag of associated entries
  meaning  string   Arbitrary identification information.  This information is
                    limited to 511 characters.


Interface Section (section header type is 12):

Interface has a dual nature - it is a face which connects two cells, but at the
same time some boundary or special conditions can be associated with it
(e.g. porous and solid baffles).


Added on 10-October-2002 - Internal faces can have special properties.
Examples are : interface between two continua, e.g. two fluid streams,
fluid/solid, or solid/solid interface; Solid baffle; and Porous baffle.

Modified on 5-December-2002 - For cyclic interfaces, we need another face id.
So now for internal interfaces, the face ids are the same and for cyclic
interfaces, the face ids are different.  We also need radiation patch ids for
each side.

  Field    Type     Comment
  faceid1  integer  face id corresponding to bound1
  bound1   integer  boundary tag associated with cell 1 of faceid

  Added for version 2 of the file.  5-December-2002 WRO
  patch1   integer  Radiation patch id associated with the bound1.  Since some
                    interfaces are not listed as boundary, but rather as internal
                    faces, at this point patch indices are associated with these
                    faces (similar to *bound1 and *bound2). All comments about
                    the argument patch of the bondary section apply also to
                    patch1 and patch2.

  Added for version 2 of the file.  5-December-2002 WRO
  faceid2  integer  face id corresponding to bound2.  In general interface can
                    be made of two faces (e.g. cyclic boundaries), and faceid2 is
                    the index of the second face which makes that interface.  If
                    interface is made of a single internal face, faceid2 is equal
                    to faceid1.

  bound2   integer  boundary tag associated with cell 2 of faceid

  Added for version 2 of the file.  5-December-2002 WRO
  patch2   integer  Radiation patch id associated with the bound2.  See patch1.


Scalar Section - Single value mesh parameters (section header type is 13):

List of parameters describing the mesh.

  Field    Type     Comment
  scale    float    The model scale.  Note that the scale has ALREADY been
                    applied to the vertex coordinates.
  maxUserVertId int The maximum vertex id defined by the user in the model.
                    Note that there may be more vertices than this in the model
                    since vertices can be created during CP match resolution.

*/
/* +-----------------------------------------------------------------------+ */
/* +-----------------------------------------------------------------------+ */


/* Automatic setting of emacs local variables. */
/* Local Variables: */
/* tab-width:2 */
/* End: */
