home *** CD-ROM | disk | FTP | other *** search
/ Lion Share / lionsharecd.iso / dos_misc / crest500.zip / OXMOLES.C < prev    next >
Text File  |  1991-08-07  |  8KB  |  303 lines

  1. /* OXMOLES will compute the variation in the products of hydrocarbon
  2.            combustion with the number of moles of Oxygen
  3.  
  4.    V1.0 (09/05/90): original version
  5.    V1.1 (10/17/90): make interactive and run TPLOT
  6.    V1.2 (08/07/91): update for CREST/V5.00
  7. */
  8. #include <stdio.h>
  9. #include <process.h>
  10. #include <float.h>
  11.  
  12. #define elements 4
  13. #define compounds 10
  14. #define products (elements+compounds)
  15. char *element[elements]={"C","H","S","O"};
  16. char *compound[compounds]={"Odia","Cmonox","Cdiox","Hdia","Hydroxl",
  17.               "Water","Smonox","Sdiox","Striox","Sulfuricacid"};
  18. float rmoles[elements]={5.,4.,.1,0.};
  19. float pfract[products];
  20. #define O rmoles[3]
  21.  
  22. main()
  23.   {
  24.   FILE *file1,*file2;
  25.   int i,l,n,N;
  26.   char c,cbuf[81];
  27.   #define filename static char
  28.   filename data[]    ={"CREST.GAS"};
  29.   filename reaction[]={"OXMOLES.REA"};
  30.   filename spool[]   ={"OXMOLES.SPL"};
  31.   filename output[]  ={"OXMOLES.OUT"};
  32.   filename plot[]    ={"OXMOLES.PLT"};
  33.   float P,T,Omin,Omax;
  34.  
  35. /* list heading */
  36.  
  37.   printf("OXMOLES/V1.2: hydrocarbon combustion products vs. moles oxygen\n");
  38.  
  39. /* get temperature from user */
  40.  
  41.   printf("enter temperature in °R ");
  42.   if(gets(cbuf)==NULL)quit("no user input");
  43.   if(sscanf(cbuf,"%hf",&T)!=1)quit("scan error");
  44.   breaktest();
  45.   if(T<=0.)quit("temperature out of range");
  46.   if(T>9000.)quit("temperature out of range");
  47.  
  48. /* get pressure from user */
  49.  
  50.   printf("enter pressure in psia ");
  51.   if(gets(cbuf)==NULL)quit("no user input");
  52.   if(sscanf(cbuf,"%hf",&P)!=1)quit("scan error");
  53.   breaktest();
  54.   if(P<=0.)quit("pressure out of range");
  55.  
  56. /* get minimum moles O from user */
  57.  
  58.   printf("enter minimum moles oxygen (try 5) ");
  59.   if(gets(cbuf)==NULL)quit("no user input");
  60.   if(sscanf(cbuf,"%hf",&Omin)!=1)quit("scan error");
  61.   breaktest();
  62.   if(Omin<=0.)quit("minimum moles oxygen out of range");
  63.  
  64. /* get maximum moles O from user */
  65.  
  66.   printf("enter maximum moles oxygen (try 20) ");
  67.   if(gets(cbuf)==NULL)quit("no user input");
  68.   if(sscanf(cbuf,"%hf",&Omax)!=1)quit("scan error");
  69.   breaktest();
  70.   if(Omax<=Omin)quit("maximum moles oxygen out of range");
  71.  
  72. /* number of steps */
  73.  
  74.   printf("The number of computations, N, is the number of times you want to run\n");
  75.   printf("CREST, each time with a different number of moles of oxygen.  If, for\n");
  76.   printf("instance, you selected Omin=5 and Omax=20 moles,  and  then  selected\n");
  77.   printf("N=4, the computations would be done at O=5,10,15,20.  If you selected\n");
  78.   printf("N=16, the computations would be done 5,6,7,...,18,19,20.\n");
  79.   printf("enter number of computations, N (try 16) ");
  80.   if(gets(cbuf)==NULL)quit("no user input");
  81.   if(sscanf(cbuf,"%i",&N)!=1)quit("scan error");
  82.   breaktest();
  83.   if(N<2)quit("too few (minumum=2)");
  84.   if(N>100)quit("too many (maxumum=100)");
  85.  
  86. /* open output file */
  87.  
  88.   if((file1=fopen(output,"wt"))==NULL)quit("unable to create output file");
  89.  
  90. /* step through moles of O range */
  91.  
  92.   for(n=0;n<N;n++)
  93.     {
  94.     O=Omin+(Omax-Omin)*(float)n/(float)(N-1);
  95.  
  96. /* test for user break */
  97.  
  98.     if(breaktest())quit("break detected at keyboard");
  99.  
  100. /* create reaction file */
  101.  
  102.     if((file2=fopen(reaction,"wt"))==NULL)quit("unable to create reaction file");
  103.     l=fprintf(file2,"%hG%s",rmoles[0],element[0]);
  104.     for(i=1;i<elements;i++)
  105.       {
  106.       l=l+fprintf(file2,"+%hG%s",rmoles[i],element[i]);
  107.       if(l>60)
  108.         {
  109.         fprintf(file2,"\n");
  110.         l=0;
  111.         }
  112.       }
  113.     c='=';
  114.     for(i=0;i<products;i++)
  115.       {
  116.       if(i<elements)
  117.         l=l+fprintf(file2,"%c%s",c,element[i]);
  118.       else
  119.         l=l+fprintf(file2,"%c%s",c,compound[i-elements]);
  120.       if(l>60)
  121.         {
  122.         fprintf(file2,"\n");
  123.         l=0;
  124.         }
  125.       c='+';
  126.       }
  127.     fprintf(file2,"\n%c",26);
  128.     fclose(file2);
  129.  
  130. /* remove existing spool file (just in case) */
  131.  
  132.     remove(spool);
  133.  
  134. /* run CREST and pass it the necessary files and data */
  135.  
  136.     sprintf(cbuf,"D=%s R=%s S=%s B=P:T:%hG:%hG:%hG",data,reaction,spool,T,P,T);
  137.     if(prgexe("CREST.EXE",cbuf))quit("unable to run CREST");
  138.  
  139. /* read the results from the spool file and save in the output file */
  140.  
  141.     if((file2=fopen(spool,"rt"))==NULL)quit("unable to open spool file");
  142.     look1:
  143.     if(freads(file2,cbuf,81)==0)quit("read error in spool file");
  144.     if(cbuf[0]!='=')goto look1;
  145.     fprintf(file1,"%hG",O);
  146.     for(i=0;i<products;i++)
  147.       {
  148.       if(freads(file2,cbuf,81)==0)quit("read error in spool file");
  149.       if(sscanf(cbuf,"%*s %*s %*hf %hf",&pfract[i])!=1)
  150.         quit("scan error in spool file");
  151.       encode(pfract[i],cbuf);
  152.       fprintf(file1," %s",cbuf);
  153.       }
  154.     fprintf(file1,"\n");
  155.     fclose(file2);
  156.     }
  157.  
  158. /* end and close the output file */
  159.  
  160.   fprintf(file1,"%c",26);
  161.   fclose(file1);
  162.  
  163. /* create the plot command file */
  164.  
  165.   if((file1=fopen(plot,"wt"))==NULL)quit("unable to create plot command file");
  166.   fprintf(file1,"TPLOT illustrate batch running CREST by OXMOLES.C\n");
  167.   fprintf(file1,"???\n");
  168.   fprintf(file1,"!STAND\n");
  169.   fprintf(file1,"!MAP\n");
  170.   fprintf(file1,"15 1 2 3 4 5 6 7 8 9 10 11 12 13 14\n");
  171.   fprintf(file1,"!ALTER\n");
  172.   fprintf(file1,"!PLOT\n");
  173.   fprintf(file1,"SUPPRESS\n");
  174.   fprintf(file1,"TICKS\n");
  175.   fprintf(file1,"ASPECT\n");
  176.   fprintf(file1,"2*1.25\n");
  177.   fprintf(file1,"LOGY\n");
  178.   fprintf(file1,"ABOVE\n");
  179.   fprintf(file1,"OXMOLES: T=%hG^oR, P=%hG psia\n",T,P);
  180.   fprintf(file1,"MOLES OF O\n");
  181.   fprintf(file1,"%hG 0 %hG 0 0\n",Omin,Omax);
  182.   fprintf(file1,"MOLE FRACTION OF PRODUCTS\n");
  183.   fprintf(file1,"-6 1 2 2 0 0 2\n");
  184.   fprintf(file1,"%s\n",output);
  185.   fprintf(file1,"%i %i %i\n",products+1,products,(products+1)/2);
  186.   for(i=0;i<products;i++)
  187.     {
  188.     fprintf(file1,"1 %i 1 3 %i %i\n",i+2,(i+1)%9+1,i+1);
  189.       if(i<elements)
  190.         fprintf(file1,"%s\n",element[i]);
  191.       else
  192.         fprintf(file1,"%s\n",compound[i-elements]);
  193.     }
  194.   fprintf(file1,"!END\n");
  195.   fprintf(file1,"%c",26);
  196.   fclose(file1);
  197.  
  198. /* run the plot program, TPLOT */
  199.  
  200.   prgexe("TPLOT.EXE",plot);
  201.   printf("\nfor hardcopy or modifications, run PLOTS\n");
  202.   }
  203.  
  204. encode(f,buf)                  /* encode and compress flt into string */
  205.   float f;                     /* It's incredible that one must go to */
  206.   char *buf;                   /* such lengths in order to do a simple*/
  207.   {                            /* little thing like suppress all of   */
  208.   char b;                      /* the unnecessary stuff in a floating */
  209.   int i,j;                     /* number like--> 1E+007 or 1E-005 !   */
  210.   sprintf(buf,"%hG",f);
  211.   if(buf[0]=='0'&&buf[1]=='.')
  212.     {
  213.     i=1;
  214.     while(1)
  215.       {
  216.       b=buf[i];
  217.       buf[i-1]=b;
  218.       if(b==0)break;
  219.       i++;
  220.       }
  221.     }
  222.   i=0;
  223.   while(b=buf[i])
  224.     {
  225.     if(b=='E'&&(buf[i+1]=='+'||buf[i+1]=='-')&&buf[i+2]=='0')
  226.       {
  227.       j=i+3;
  228.       while(1)
  229.         {
  230.         b=buf[j];
  231.         buf[j-1]=b;
  232.         if(b==0)break;
  233.         j++;
  234.         }
  235.       }
  236.     else
  237.       i++;
  238.     }
  239.   i=0;
  240.   while(b=buf[i])
  241.     {
  242.     if(b=='E'&&buf[i+1]=='+')
  243.       {
  244.       i+=2;
  245.       while(1)
  246.         {
  247.         b=buf[i];
  248.         buf[i-1]=b;
  249.         if(b==0)break;
  250.         i++;
  251.         }
  252.       break;
  253.       }
  254.     i++;
  255.     }
  256.   }
  257.  
  258. breaktest()                      /* test for an unsolicited keystroke */
  259.   {
  260.   if(!kbhit())return(0);
  261.   if(!getch())getch();
  262.   return(1);
  263.   }
  264.  
  265. freads(stream,string,bytes)               /* It's hard to believe     */
  266.   char *string;                           /* that one must go through */
  267.   int bytes;                              /* all this trouble just to */
  268.   FILE *stream;                           /* read a file!             */
  269.   {
  270.   char *eol,buf2[2];
  271.   int b;
  272.   eol=fgets(string,bytes,stream);
  273.   if(eol==NULL)return(0);
  274.   for(b=0;b<bytes;b++)
  275.     {
  276.     if(string[b]=='\n')
  277.       {
  278.       string[b]=0;
  279.       return(b+1);
  280.       }
  281.     }
  282.   string[bytes-1]=0;
  283.   more:
  284.   eol=fgets(buf2,2,stream);
  285.   if((eol==NULL)||(!buf2[0])||(buf2[0]=='\n'))return(bytes);
  286.   goto more;
  287.   }
  288.  
  289. prgexe(prog,string)                                  /* run a program */
  290.   char prog[],string[];
  291.   {
  292.   printf("%s %s\n",prog,string);
  293.   return(spawnlp(0,prog,prog,string,NULL));
  294.   }
  295.  
  296. quit(message)                            /* write a message and abort */
  297.   char message[];
  298.   {
  299.   fcloseall();
  300.   printf("\n%s\r",message);
  301.   abort();
  302.   }
  303.