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

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