home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / biology / gsrc208a.zip / GETALL.C < prev    next >
C/C++ Source or Header  |  1993-08-25  |  27KB  |  880 lines

  1. #include "copyleft.h"
  2.  
  3. /*
  4.     GEPASI - a simulator of metabolic pathways and other dynamical systems
  5.     Copyright (C) 1989, 1992, 1993  Pedro Mendes
  6. */
  7.  
  8. /*************************************/
  9. /*                                   */
  10. /*             data input            */
  11. /*                                   */
  12. /*        Zortech C/C++ 3.0 r4       */
  13. /*          MICROSOFT C 6.00         */
  14. /*          Visual C/C++ 1.0         */
  15. /*           QuickC/WIN 1.0          */
  16. /*             ULTRIX cc             */
  17. /*              GNU gcc              */
  18. /*                                   */
  19. /*   (include here compilers that    */
  20. /*   compiled GEPASI successfully)   */
  21. /*                                   */
  22. /*************************************/
  23.  
  24.  
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #ifdef MSDOS
  29. #include <io.h>
  30. #endif
  31. #include <fcntl.h>
  32. #include <sys/types.h>
  33. #include <sys/stat.h>
  34. #include <math.h>
  35. #include "globals.h"
  36. #include "globvar.h"
  37. #include "datab.h"
  38. #include "newton.h"
  39. #include "strtbl.h"
  40. #include "gauss.h"
  41. #include "kinetics.h"
  42. #include "pmu.h"                                    /* several utilities     */
  43. #include "heapchk.h"
  44.  
  45. #if ( _MSC_VER >= 610 )
  46.  #define open _open
  47.  #define close _close
  48.  #define stat _stat
  49.  #define fstat _fstat
  50.  #define read _read
  51.  #define write _write
  52.  #define O_RDONLY _O_RDONLY
  53. #endif
  54.  
  55. #ifdef _ZTC
  56. #define MEM_DEBUG 1
  57. #include "mem.h"
  58. #else
  59. #define mem_malloc malloc
  60. #define mem_free free
  61. #define mem_realloc realloc
  62. #endif
  63.  
  64. /*
  65.     get the list of files to process
  66. */
  67.  
  68. void getlist( char *inpf )
  69. {
  70.  int i;
  71.  FILE *chg;
  72.  
  73.  if ( inpf )
  74.  {
  75.   if (!(chg=fopen(inpf,"r")))
  76.   {
  77.    printf(errormsg[6],inpf);
  78.    term_gepasi();
  79.   }
  80.  }
  81.  else
  82.  {
  83.   chg = stdin;
  84.   fprintf( stderr, "\n\n%s\n\n", gepasi );
  85.   fprintf( stderr, "input filenames one at a line, a dot followed by ENTER ends.\n\n");
  86.  }
  87.  for(i=0;i<MAX_FIL;i++)                              /* stop at MAX_FIL     */
  88.  {
  89.   fgets( filename[i], PWIDTH, chg );
  90.   if (   ( filename[i][0] == '.' )
  91.       || ( feof( chg ) )
  92.      ) break;                                        /* stop if EOF or .    */
  93.   filename[i][strlen(filename[i])-1] = 0;            /* put /0 at the end   */
  94.   fixext( filename[i], '.', ".sim" );
  95.  }
  96.  nrunfiles = i;
  97.  if ( inpf ) fclose( chg );
  98.  return;
  99. }
  100.  
  101.  
  102.  
  103. /*
  104.     read topology from file
  105. */
  106.  
  107. int BufToTop( char *Buff, char **endoftop )
  108. {
  109.  int i,j;
  110.  int daux;
  111.  unsigned char uaux;
  112.  char *paux;
  113.  char saux[NAME_L];
  114.  
  115. /* reset the version number to zero                                                    */
  116.  ver_no = 0;
  117.  /* copy the first line                                                                                 */
  118.  paux = strchr( Buff, '\n' );
  119.  if( paux == NULL ) return IDS_ERR_BAD_TOPNAM;
  120.  /* take care of CR if one existed before LF                                            */
  121.  if( *(paux-1)=='\r' ) *(paux-1) = '\0';
  122.  *paux = '\0';
  123.  if( strlen( Buff ) > 255 ) Buff[255] = '\0';
  124.  strcpy( topname, Buff );
  125.  Buff = paux+1;
  126.  /* is this line a version number ?                                                                     */
  127.  sscanf( topname, "version %f", &ver_no );
  128.  if( ver_no != 0 )
  129.  {
  130.   /* YES! so let's read another line, with the title                            */
  131.   paux = strchr( Buff, '\n' );
  132.   if( paux == NULL ) return IDS_ERR_BAD_TOPNAM;
  133.   if( *(paux-1)=='\r' ) *(paux-1) = '\0';
  134.   *paux = '\0';
  135.   if( strlen( Buff ) > 255 ) Buff[255] = '\0';
  136.   strcpy( topname, Buff );
  137.   Buff = paux+1;
  138.  }
  139.  
  140.  /* read nstep from the buffer                                                                          */
  141.  if ( ( sscanf( Buff, " %d", &daux ) < 1) ||
  142.       ( daux < 1 ) || ( daux > MAX_STEP ) )
  143.   return IDS_ERR_BAD_NSTEP;
  144.  nsteps = daux;
  145.  
  146.  Buff = strchr( Buff, ' ' );
  147.  /* read totmet from the buffer                                                                 */
  148.  if( (Buff == NULL ) || ( sscanf( ++Buff, " %d", &daux ) < 1) ||
  149.      ( daux < 1 )   || ( daux > MAX_MET ) )
  150.   return IDS_ERR_BAD_TOTMET;
  151.  totmet = daux;
  152.  
  153.  /* read stoiu from the buffer                                                                          */
  154.  for(i=0;i<totmet;i++)
  155.   for(j=0;j<nsteps;j++)
  156.   {
  157.    if(j==0)
  158.    {
  159.     Buff = strchr( Buff, '\n' );
  160.     if( Buff != NULL )
  161.     {
  162.      Buff++;
  163.      for( ;*Buff == ' '; Buff++);
  164.     }
  165.    }
  166.    else
  167.    {
  168.     Buff = strchr( Buff, ' ' );
  169.     if( Buff != NULL ) for( ;*Buff == ' '; Buff++);
  170.    }
  171.    if ( (Buff == NULL) || (sscanf( Buff, "%d", &daux ) < 1) )
  172.     return IDS_ERR_BAD_STOI;
  173.    stoiu[i][j] = daux;
  174.   }
  175.  Buff = strchr( Buff, '\n' );
  176.  if( Buff == NULL ) return IDS_ERR_BAD_KINTYPE;
  177.  Buff++;
  178.  /* read kinetu, revers and metname from the buffer                             */
  179.  for( i=0; i<nsteps; i++ )
  180.  {
  181.   for( ;*Buff == ' '; Buff++);
  182.   if ( (Buff == NULL) || (sscanf( Buff, "%d", &daux ) < 1)
  183.        || (daux < -1) )
  184.    return IDS_ERR_BAD_KINTYPE;
  185.   kinetu[i] = daux;
  186.   Buff = strchr( Buff, ' ' );
  187.   if( Buff != NULL ) for( ;*Buff == ' '; Buff++);
  188.   if ( (Buff == NULL) || (sscanf( Buff, "%d", &daux ) < 1) )
  189.    return IDS_ERR_BAD_KINTYPE;
  190.   revers[i] = daux;
  191.  
  192.   Buff = strchr( Buff, ' ' );
  193.   if( Buff != NULL ) for( ;*Buff == ' '; Buff++);
  194.   paux = strchr( Buff, '\n' );
  195.   if( paux == NULL ) return IDS_ERR_BAD_KINTYPE;
  196.   /* take care of CR if one existed before LF                                           */
  197.   if( *(paux-1)=='\r' ) *(paux-1) = '\0';
  198.   *paux = '\0';
  199.   if( strlen( Buff ) >= NAME_L ) Buff[NAME_L-1] = '\0';
  200.   strcpy( stepname[i], Buff );
  201.   Buff = paux+1;
  202.  }
  203.  
  204.  *(--Buff) = '\n';
  205.  /* read rstr from the buffer                                                                           */
  206.  for(i=0;i<nsteps;i++)
  207.   for(j=0;j<MAX_MOL;j++)
  208.   {
  209.    if(j==0)
  210.    {
  211.     Buff = strchr( Buff, '\n' );
  212.     if( Buff != NULL )
  213.     {
  214.      Buff++;
  215.      for( ;*Buff == ' '; Buff++);
  216.     }
  217.    }
  218.    else
  219.    {
  220.     Buff = strchr( Buff, ' ' );
  221.     if( Buff != NULL ) for( ;*Buff == ' '; Buff++);
  222.    }
  223.    if ( (Buff == NULL) || (sscanf( Buff,"%d", &daux ) < 1) )
  224.     return IDS_ERR_BAD_LOOP;
  225.    (*rstr)[i][j] = (signed char) daux;
  226.   }
  227.  
  228.  /* read loop from the buffer                                                                           */
  229.  for(i=0;i<nsteps;i++)
  230.   for(j=0;j<totmet;j++)
  231.   {
  232.    if(j==0)
  233.    {
  234.     Buff = strchr( Buff, '\n' );
  235.     if( Buff != NULL )
  236.     {
  237.      Buff++;
  238.      for( ;*Buff == ' '; Buff++);
  239.     }
  240.    }
  241.    else
  242.    {
  243.     Buff = strchr( Buff, ' ' );
  244.     if( Buff != NULL ) for( ;*Buff == ' '; Buff++);
  245.    }
  246.    if ( (Buff == NULL) || (sscanf( Buff,"%u", &uaux ) < 1)
  247.      || (uaux < 0) )
  248.     return IDS_ERR_BAD_LOOP;
  249.    (*loop)[i][j] = uaux;
  250.   }
  251.  
  252.  /* read metabolite status and names from the buffer                            */
  253.  for(i=0;i<totmet;i++)
  254.  {
  255.   Buff = strchr( Buff, '\n' );
  256.   if ( (Buff == NULL) || (sscanf( ++Buff, "%d %21s", &daux, saux ) < 2) )
  257.    return IDS_ERR_BAD_INTMET;
  258.   intmet[i] = daux;
  259.   strcpy( metname[i], saux );
  260.  }
  261.  
  262.  /* point endofbuf to the end of the topology section                           */
  263.  if( endoftop != NULL )
  264.   *endoftop = Buff;
  265.  /* return and signal that function was successful                                      */
  266.  return 0;
  267. }
  268.  
  269. int BufToTree( char *Buff, char **endoftree )
  270. {
  271.  int i;
  272.  char  *paux;
  273.  int daux, daux1, daux2;
  274.  char caux[10];
  275.  float faux;
  276.  
  277.  /* copy the title from the buffer                                                              */
  278.  paux = strchr( Buff, '\n' );
  279.  if( paux == NULL ) return IDS_ERR_LOAD;
  280.  /* take care of CR if one existed before LF                                            */
  281.  if( *(paux-1)=='\r' ) *(paux-1) = '\0';
  282.  *paux = '\0';
  283.  if( strlen( Buff ) > 63 ) Buff[63] = '\0';
  284.  strcpy( tr.descr, Buff );
  285.  Buff = paux+1;
  286.  
  287.  /* read nnode from the buffer                                                                          */
  288.  if ( sscanf( Buff, "%d", &daux ) < 1 )
  289.   return IDS_ERR_LOAD;
  290.  tr.nnode = daux;
  291.  
  292.  Buff = strchr( Buff, ' ' );
  293.  /* read nid from the buffer                                                                            */
  294.  if( (Buff == NULL ) || ( sscanf( ++Buff, "%d", &daux ) < 1 ) )
  295.   return IDS_ERR_LOAD;
  296.  tr.nid = daux;
  297.  
  298.  Buff = strchr( Buff, ' ' );
  299.  /* read nnum from the buffer                                                                           */
  300.  if( (Buff == NULL ) || ( sscanf( ++Buff, "%d", &daux ) < 1 ) )
  301.   return IDS_ERR_LOAD;
  302.  tr.nnum = daux;
  303.  
  304.  Buff = strchr( Buff, ' ' );
  305.  /* read nconst from the buffer                                                                 */
  306.  if( (Buff == NULL ) || ( sscanf( ++Buff, "%d", &daux ) < 1 ) )
  307.   return IDS_ERR_LOAD;
  308.  tr.nconst = daux;
  309.  
  310.  Buff = strchr( Buff, ' ' );
  311.  /* read nsub from the buffer                                                                           */
  312.  if( (Buff == NULL ) || ( sscanf( ++Buff, "%d", &daux ) < 1 ) )
  313.   return IDS_ERR_LOAD;
  314.  tr.nsub = daux;
  315.  
  316.  Buff = strchr( Buff, ' ' );
  317.  /* read npro from the buffer                                                                           */
  318.  if( (Buff == NULL ) || ( sscanf( ++Buff, "%d", &daux ) < 1 ) )
  319.   return IDS_ERR_LOAD;
  320.  tr.npro = daux;
  321.  
  322.  Buff = strchr( Buff, ' ' );
  323.  /* read nmodf from the buffer                                                                  */
  324.  if( (Buff == NULL ) || ( sscanf( ++Buff, "%d", &daux ) < 1 ) )
  325.   return IDS_ERR_LOAD;
  326.  tr.nmodf = daux;
  327.  
  328.  Buff = strchr( Buff, ' ' );
  329.  /* read revers from the buffer                                                                 */
  330.  if( (Buff == NULL ) || ( sscanf( ++Buff, "%d", &daux ) < 1 ) )
  331.   return IDS_ERR_LOAD;
  332.  tr.revers = daux;
  333.  
  334.  Buff = strchr( Buff, '\n' );
  335.  if( Buff==NULL ) return IDS_ERR_LOAD;
  336.  Buff++;
  337.  
  338.  for( i=0; i<tr.nnode; i++ )
  339.  {
  340.   if( ( sscanf( Buff, "%c %d %d %d", &caux[0],
  341.                      &daux,
  342.                      &daux1,
  343.                      &daux2 ) < 4 ) )
  344.    return IDS_ERR_LOAD;
  345.   tr.node[i].item  = caux[0];
  346.   tr.node[i].val   = (unsigned char) daux;
  347.   tr.node[i].left  = (unsigned char) daux1;
  348.   tr.node[i].right = (unsigned char) daux2;
  349.   Buff = strchr( Buff, ',' );
  350.   if( Buff==NULL ) return IDS_ERR_LOAD;
  351.   Buff++;
  352.  }
  353.  for( i=0; i<tr.nid; i++ )
  354.  {
  355.   Buff = strchr( Buff, '\n' );
  356.   if( Buff==NULL ) return IDS_ERR_LOAD;
  357.   Buff++;
  358.   if( sscanf( Buff, "%d %8s", &daux, caux ) < 2 )
  359.    return IDS_ERR_LOAD;
  360.   strcpy( tr.id[i], caux );
  361.   tr.id[i][8] = '\0';
  362.   tr.id[i][9] = (char) daux;
  363.  }
  364.  for( i=0; i<tr.nnum; i++ )
  365.  {
  366.   Buff = strchr( Buff, '\n' );
  367.   if( Buff==NULL ) return IDS_ERR_LOAD;
  368.   Buff++;
  369.   if( sscanf( Buff, "%g", &faux ) < 1 )
  370.    return IDS_ERR_LOAD;
  371.   tr.constant[i] = faux;
  372.  }
  373.  /* point endofbuf to the end of the topology section                           */
  374.  if( endoftree != NULL )
  375.   *endoftree = Buff;
  376.   /**endoftree = strchr( Buff, '\n' );*/
  377.  return 0;
  378. }
  379.  
  380.  
  381. int BufToSim( char *Buff )
  382. {
  383.  int i,j, dens, log, lkt, op, adj;
  384.  double faux, low, high, fac;
  385.  char  *ptr;
  386.  char  *paux;
  387.  
  388.   /* read the kinetic constants from the buffer, one line per step      */
  389.   for(i=0;i<nsteps;i++)
  390.   {
  391.    Buff = strchr( Buff, '\n' );
  392.    if( Buff != NULL ) Buff++;
  393.    for( j=0; j<(int)ktype[kinetype[i]].nconst; j++ )
  394.    {
  395.     Buff = strchr( Buff, ' ' );
  396.     if( Buff != NULL ) for( ;*Buff == ' '; Buff++);
  397.     if ( (Buff == NULL) || (sscanf( Buff,"%le", &faux ) < 1) )
  398.      return IDS_ERR_BAD_KPARAM;
  399.     *(params[i]+j) = faux;
  400.    }
  401.   }
  402.   /* read the concentrations, one per line                                                      */
  403.   for(i=0;i<totmet;i++)
  404.   {
  405.    Buff = strchr( Buff, '\n' );
  406.    if( Buff != NULL )
  407.     Buff++;
  408.    if ( (Buff == NULL) || (sscanf( Buff,"%le", &faux ) < 1) )
  409.     return IDS_ERR_BAD_CONCENT;
  410.    x0[i] = x[i] = faux;
  411.   }
  412.  
  413.   /* read the units in one line                                                                                 */
  414.   Buff = strchr( Buff, '\n' );
  415.   if( Buff != NULL ) Buff++;
  416.   if ( (Buff == NULL) || (sscanf( Buff,"%s %s", &options.concu[0], &options.timeu[0] ) < 2) )
  417.    return IDS_ERR_BAD_UNITS;
  418.  
  419.   /* read log, dyn and ss options in one line                                                   */
  420.   Buff = strchr( Buff, '\n' );
  421.   if( Buff != NULL )
  422.    Buff++;
  423.   if ( (Buff == NULL) || (sscanf( Buff,"%d %d %ld %le %le %le %le %d %d %d",
  424.       &options.debug, &options.dyn, &options.pfo, &options.endtime,
  425.       &options.reltol, &options.abstol, &options.hrcz, &options.adams,
  426.       &options.bdf, &options.ss ) < 10 ) )
  427.    return IDS_ERR_BAD_OPTIONS;
  428.   endtime = options.endtime;
  429.  
  430.   /* read txt options in one line                                                                               */
  431.   Buff = strchr( Buff, '\n' );
  432.   if( Buff != NULL )
  433.    Buff++;
  434.   if ( (Buff == NULL) || (sscanf( Buff,"%d %d %d %d %d %d %d",
  435.       &options.txt, &options.structan, &options.staban,
  436.       &options.stdela, &options.nonela, &options.stdcc,
  437.       &options.noncc ) < 7 ) )
  438.    return IDS_ERR_BAD_OPTIONS;
  439.   /* read the dat options in one line                                                                   */
  440.   Buff = strchr( Buff, '\n' );
  441.   if( Buff != NULL ) Buff++;
  442.   if ( (Buff == NULL) || (sscanf( Buff,"%d %d %d %d %d %d %d %d %d",
  443.     &options.dat, &options.datsep, &options.datwidth,
  444.     &options.dattit, &options.datmca, &options.datss,
  445.     &totsel, &options.append, &options.quotes ) < 9 ) )
  446.    return IDS_ERR_BAD_OPTIONS;
  447.   /* read the dat filename in one line                                                                  */
  448.   Buff = strchr( Buff, '\n' );
  449.   if( Buff != NULL )
  450.    Buff++;
  451.   if ( (Buff == NULL) || (sscanf( Buff,"%s", &options.datname[0] ) != 1 ) )
  452.    return IDS_ERR_BAD_OPTIONS;
  453.   paux = strchr( Buff, '\n' );
  454.   if( paux == NULL) return IDS_ERR_BAD_OPTIONS;
  455.   /* anything selected for output?    */
  456.  
  457.   if( totsel )
  458.   {
  459.    /* allocate enough memory for output pointers and titles    */
  460.    poutelem = (PDBL *) realloc( poutelem, totsel * sizeof( PDBL ) );
  461.    outtit = (char *) realloc( outtit, totsel * (options.datwidth+1) * sizeof( char ) );
  462.    if( (poutelem == NULL) || (outtit == NULL) )
  463.     return IDS_ERR_NOEXEC;
  464.    /* read each item in one line (index and title)                                              */
  465.    if( ver_no < (float) 2.05 ) adj = totmet;
  466.    else adj = 0;
  467.    for( i=0, ptr=outtit; i<totsel; i++ )
  468.    {
  469.     Buff = paux+1;
  470.     if ( (Buff == NULL) || (sscanf( Buff,"%d", &j) < 1 ) )
  471.      return IDS_ERR_BAD_OPTIONS;
  472.  
  473.     poutelem[i] = outpel[j+adj];
  474.  
  475.     Buff = strchr( Buff, ' ' );
  476.     if( Buff != NULL ) for( ;*Buff == ' '; Buff++);
  477.     paux = strchr( Buff, '\n' );
  478.     if( paux == NULL ) return IDS_ERR_BAD_OPTIONS;
  479.     /* take care of CR if one existed before LF                                         */
  480.     if( *(paux-1)=='\r' ) *(paux-1) = '\0';
  481.     *paux = '\0';
  482.     if( strlen( Buff ) > (size_t) options.datwidth ) Buff[options.datwidth] = '\0';
  483.     strcpy( ptr, Buff );
  484.     ptr += strlen( ptr )+1;
  485.    }
  486.   }
  487.   /* read the scan options in one line                                                                  */
  488.   Buff = paux+1;
  489.   if ( (Buff == NULL) || (sscanf( Buff, "%d %d %d %d %d", &options.scan,
  490.               &options.scandens, &options.scanlog,
  491.               &totscan, &nlinks) < 5 ) )
  492.    return IDS_ERR_BAD_OPTIONS;
  493.  
  494.   /* read each scan item in one line                 */
  495.   for( i=0; i<totscan; i++ )
  496.   {
  497.    Buff = strchr( Buff, '\n' );
  498.    if( Buff != NULL )
  499.     Buff++;
  500.    if ( (Buff == NULL) || (sscanf( Buff,"%d %le %le %d %d", &(scindex[i]),
  501.                    &low, &high, &dens, &log ) < 5 ) )
  502.     return IDS_ERR_BAD_OPTIONS;
  503.    sparam[scindex[i]].low  = low;
  504.    sparam[scindex[i]].high = high;
  505.    if( log )
  506.     sparam[scindex[i]].ampl = log10( high ) - log10( low );
  507.    else
  508.     sparam[scindex[i]].ampl = high - low;
  509.    sparam[scindex[i]].dens = dens;
  510.    sparam[scindex[i]].log  = log;
  511.   }
  512.  
  513.   /* read each link in one line                 */
  514.   for( i=0; i<nlinks; i++ )
  515.   {
  516.    Buff = strchr( Buff, '\n' );
  517.    if( Buff != NULL )
  518.     Buff++;
  519.    if ( (Buff == NULL) || (sscanf( Buff,"%d %d %d %le", &(lindex[i]),
  520.                    &lkt, &op, &fac ) < 4 ) )
  521.     return IDS_ERR_BAD_OPTIONS;
  522.    sparam[lindex[i]].linkedto  = sparam[lkt].var;
  523.    sparam[lindex[i]].operation = op;
  524.    sparam[lindex[i]].factor = fac;
  525.   }
  526.  
  527.   return 0;
  528. }
  529.  
  530.  
  531. /*
  532.     read a simulation file
  533. */
  534.  
  535. int ReadSim( char *FName )
  536. {
  537.  int i, j, k, ty, fl, ch1, nRc;
  538.  char *Buff;
  539.  char *ptr;
  540.  unsigned int bufsize;
  541.  struct stat fst;
  542.  unsigned char  nmod;
  543.  BOOL   badtop;
  544.  
  545.  /* open the file                                                                                                       */
  546.  if( (ch1 = open( FName, O_RDONLY )) != -1 )
  547.  {
  548.   /* get the file statistics                                                                            */
  549.   fstat( ch1, &fst );
  550.  
  551.   /* set the buffer to the size of the file plus one                            */
  552.   bufsize = (unsigned int) fst.st_size;
  553.   if( bufsize<1024 ) bufsize=1024;
  554.  
  555.   /* allocate space for the read buffer and lock it                                     */
  556.   ptr = (char  *) mem_malloc( bufsize+1 );
  557.   if( ptr == NULL )
  558.    return IDS_ERR_NOEXEC;
  559.   Buff = ptr;
  560.  
  561.   /* read the file and close it                                                                         */
  562.   read( ch1, Buff, (unsigned int) fst.st_size );
  563.   close( ch1 );
  564.  
  565.   /* read the topology part                                                                                     */
  566.   if( nRc = BufToTop( Buff, &Buff ) )
  567.   {
  568.    mem_free( ptr );
  569.    return nRc;
  570.   }
  571.  
  572.   /* set the number of user-def functions to zero                                       */
  573.   nudf = 0;
  574.   nrateq = MAX_TYP;
  575.   sizetr = 4;
  576.   treestr = (char  *) mem_realloc( treestr, sizetr * sizeof( char ) );
  577.   if( treestr == NULL )
  578.   {
  579.    mem_free( ptr );
  580.    return IDS_ERR_NOEXEC;
  581.   }
  582.   treeptr = treestr;
  583.  
  584.   /* reset kinetic type flags                                                                           */
  585.   for( i=0; i<nsteps; i++ ) kfl[i] = 1;
  586.   /* check if model contains user-defined kinetic types                         */
  587.   /* if there are any read them                                                                         */
  588.   for( i=0; i<nsteps; i++ )
  589.    if( (kinetu[i] >= MAX_TYP) && (kfl[i]) )
  590.    {
  591.     /* look for a form-feed                                                                                     */
  592.     Buff = strchr( Buff, '\f' );
  593.     Buff++;
  594.     if( (nRc=BufToTree( Buff, &Buff )) != 0)
  595.     {
  596.      mem_free( ptr );
  597.      term_gepasi();
  598.     }
  599.  
  600.     for( j=0, ty=-1; j<nudf; j++ )
  601.      if( strcmp( tree[j].descr, tr.descr ) == 0 )
  602.      {
  603.       ty = j;
  604.       break;
  605.      }
  606.     if( ty == -1 )
  607.     {
  608.      if( ( nRc=addtr( i ) ) )
  609.      {
  610.       mem_free( ptr );
  611.       return nRc;
  612.      }
  613.      fl = 1;
  614.     }
  615.     else
  616.      for( j=i,k=kinetu[i]; j<nsteps; j++ )
  617.       if( (kinetu[j]==k) && (kfl[j]) )
  618.       {
  619.        kinetu[j] = MAX_TYP+ty;
  620.        kfl[j] = 0;
  621.       }
  622.    }
  623.  
  624.   badtop = 0;
  625.   if( (nsteps<=0) || (nsteps>MAX_STEP) ) badtop = IDS_ERR_STEP_EXCEED;
  626.   if( (totmet<=0) || (totmet>MAX_MET)  ) badtop = IDS_ERR_MET_EXCEED;
  627.   /* check if topology is complete                                                      */
  628.   for( i=0; i<nsteps; i++ )
  629.   {
  630.    if( kinetu[i]==NOT ) badtop = IDS_ERR_BAD_TOP;
  631.    /* count the number of substrates in rstr    */
  632.    for( j=k=0; j<MAX_MOL; j++ )
  633.     if( (*rstr)[i][j] < 0 ) k++;
  634.    /* and check if it is the same or less than in ktype    */
  635.    if( ktype[kinetu[i]].nsub > k ) badtop = IDS_ERR_BAD_KINTYPE;
  636.    /* if the type is mass action, then it must be equal    */
  637.    if( ktype[kinetu[i]].nsub < k )
  638.     if( !strcmp(ktype[kinetu[i]].descr,"mass action") ) badtop = IDS_ERR_BAD_KINTYPE;
  639.    /* count the number of products in rstr    */
  640.    for( j=k=0; j<MAX_MOL; j++ )
  641.     if( (*rstr)[i][j] > 0 ) k++;
  642.    /* and check if it is the same or less than in ktype    */
  643.    if( ktype[kinetu[i]].npro > k ) badtop = IDS_ERR_BAD_KINTYPE;
  644.    /* if the type is mass action, then it must be equal    */
  645.    if( ktype[kinetu[i]].npro < k )
  646.     if( !strcmp(ktype[kinetu[i]].descr,"mass action") ) badtop = IDS_ERR_BAD_KINTYPE;
  647.    if( ktype[kinetu[i]].nmodf > 0 )
  648.    {
  649.     /* count the number of assigned modfs for each reaction     */
  650.     for( j=0, nmod=0; j<totmet; j++ )
  651.      if( (*loop)[i][j] != 0 ) nmod++;
  652.     if( ktype[kinetu[i]].nmodf != nmod ) badtop = IDS_ERR_BAD_TOP;;
  653.    }
  654.   }
  655.  
  656.   /* if topology is incomplete, signal and return                               */
  657.   if( badtop )
  658.   {
  659.    mem_free( ptr );
  660.    return badtop;
  661.   }
  662.  
  663.   /* reduce the stoicheiometry matrix                                           */
  664.   reduce();
  665.  
  666.   if( SetParams() == -1 )
  667.   {
  668.    mem_free( ptr );
  669.    return IDS_ERR_NOEXEC;
  670.   }
  671.  
  672.   if( SetEff() == -1 )
  673.   {
  674.    mem_free( ptr );
  675.    return IDS_ERR_NOEXEC;
  676.   }
  677.  
  678.   /* setup the output and scanning elements                             */
  679.   if( SetOutpEl() == -1 )
  680.   {
  681.    mem_free( ptr );
  682.    return IDS_ERR_NOEXEC;
  683.   }
  684.  
  685.   nRc = BufToSim( Buff );
  686.   mem_free( ptr );
  687.  
  688.   return nRc;
  689.  }
  690.  else return IDS_ERR_LOAD;
  691. }
  692.  
  693.  
  694. void eff_rateq( void )
  695. {
  696.  int i,j,k,l;
  697.  
  698.  for( i=0; i<nsteps; i++)
  699.  {
  700.   l = 0;
  701.   /* assign indexes to the substrates           */
  702.   for( k=0; k<MAX_MOL; k++ )
  703.    if( (*rstr)[i][k] < 0 )
  704.     eff[i][l++] = -(*rstr)[i][k]-1;
  705.   /* assign indexes to the products                     */
  706.   for( k=0; k<MAX_MOL; k++ )
  707.    if( (*rstr)[i][k] > 0 )
  708.     eff[i][l++] = (*rstr)[i][k]-1;
  709.   /* assign indexes to the modifiers            */
  710.   for( j=0; j < (int) ktype[kinetype[i]].nmodf; j++ )
  711.    for( k=0; k<totmet; k++ )
  712.     if( (int) (*loop)[i][k] == (j+1) )
  713.      eff[i][l++] = k;
  714.  }
  715.  
  716.  for( i=0; i<nsteps; i++ )                        /* first approach: all    */
  717.   for( j=0; j<totmet; j++ )                       /* derivatives are zero   */
  718.    partder[i][j] = ret_zero;
  719.  
  720.  for( i=0; i<nsteps; i++ )                        /* now point to the funcs */
  721.  {
  722.   switch (kinetype[i])                            /* that are != zero       */
  723.   {
  724.    case I01:
  725.    case I10:  rateq[i]  = i0;
  726.           break;
  727.    case I11:  rateq[i]  = i11;
  728.           partder[i][eff[i][0]] = d1Xs1;
  729.           break;
  730.    case R11:  rateq[i] = r11;
  731.           partder[i][eff[i][0]] = d1Xs1;
  732.           partder[i][eff[i][1]] = dX1p1;
  733.           break;
  734.    case I12:  rateq[i] = i12;
  735.           partder[i][eff[i][0]] = d1Xs1;
  736.           break;
  737.    case R12:  rateq[i] = r12;
  738.           partder[i][eff[i][0]] = d1Xs1;
  739.           partder[i][eff[i][1]] = dX2p1s1;
  740.           partder[i][eff[i][2]] = dX2p2s1;
  741.           break;
  742.    case I13:  rateq[i] = i13;
  743.           partder[i][eff[i][0]] = d1Xs1;
  744.           break;
  745.    case R13:  rateq[i] = r13;
  746.           partder[i][eff[i][0]] = d1Xs1;
  747.           partder[i][eff[i][1]] = dX3p1s1;
  748.           partder[i][eff[i][2]] = dX3p2s1;
  749.           partder[i][eff[i][3]] = dX3p3s1;
  750.           break;
  751.    case I21:  rateq[i] = i21;
  752.           partder[i][eff[i][0]] = d2Xs1;
  753.           partder[i][eff[i][1]] = d2Xs2;
  754.           break;
  755.    case R21:  rateq[i] = r21;
  756.           partder[i][eff[i][0]] = d2Xs1;
  757.           partder[i][eff[i][1]] = d2Xs2;
  758.           partder[i][eff[i][2]] = dX1p1;
  759.           break;
  760.    case I22:  rateq[i] = i22;
  761.           partder[i][eff[i][0]] = d2Xs1;
  762.           partder[i][eff[i][1]] = d2Xs2;
  763.           break;
  764.    case R22:  rateq[i] = r22;
  765.           partder[i][eff[i][0]] = d2Xs1;
  766.           partder[i][eff[i][1]] = d2Xs2;
  767.           partder[i][eff[i][2]] = dX2p1s2;
  768.           partder[i][eff[i][3]] = dX2p2s2;
  769.           break;
  770.    case I23:  rateq[i] = i23;
  771.           partder[i][eff[i][0]] = d2Xs1;
  772.           partder[i][eff[i][1]] = d2Xs2;
  773.           break;
  774.    case R23:  rateq[i] = r23;
  775.           partder[i][eff[i][0]] = d2Xs1;
  776.           partder[i][eff[i][1]] = d2Xs2;
  777.           partder[i][eff[i][2]] = dX3p1s2;
  778.           partder[i][eff[i][3]] = dX3p2s2;
  779.           partder[i][eff[i][4]] = dX3p3s2;
  780.           break;
  781.    case I31:  rateq[i] = i31;
  782.           partder[i][eff[i][0]] = d3Xs1;
  783.           partder[i][eff[i][1]] = d3Xs2;
  784.           partder[i][eff[i][2]] = d3Xs3;
  785.           break;
  786.    case R31:  rateq[i] = r31;
  787.           partder[i][eff[i][0]] = d3Xs1;
  788.           partder[i][eff[i][1]] = d3Xs2;
  789.           partder[i][eff[i][2]] = d3Xs3;
  790.           partder[i][eff[i][3]] = dX1p1;
  791.           break;
  792.    case I32:  rateq[i] = i32;
  793.           partder[i][eff[i][0]] = d3Xs1;
  794.           partder[i][eff[i][1]] = d3Xs2;
  795.           partder[i][eff[i][2]] = d3Xs3;
  796.           break;
  797.    case R32:  rateq[i] = r32;
  798.           partder[i][eff[i][0]] = d3Xs1;
  799.           partder[i][eff[i][1]] = d3Xs2;
  800.           partder[i][eff[i][2]] = d3Xs3;
  801.           partder[i][eff[i][3]] = dX2p1s3;
  802.           partder[i][eff[i][4]] = dX2p2s3;
  803.           break;
  804.    case I33:  rateq[i] = i33;
  805.           partder[i][eff[i][0]] = d3Xs1;
  806.           partder[i][eff[i][1]] = d3Xs2;
  807.           partder[i][eff[i][2]] = d3Xs3;
  808.           break;
  809.    case R33:  rateq[i] = r33;
  810.           partder[i][eff[i][0]] = d3Xs1;
  811.           partder[i][eff[i][1]] = d3Xs2;
  812.           partder[i][eff[i][2]] = d3Xs3;
  813.           partder[i][eff[i][3]] = dX3p1s3;
  814.           partder[i][eff[i][4]] = dX3p2s3;
  815.           partder[i][eff[i][5]] = dX3p3s3;
  816.           break;
  817.    case IMM:  rateq[i] = ihmm;
  818.           partder[i][eff[i][0]] = dihmms;
  819.           break;
  820.    case RMM:  rateq[i] = hmm;
  821.           partder[i][eff[i][0]] = dhmms;
  822.           partder[i][eff[i][1]] = dhmmp;
  823.           break;
  824.    case PSI:  rateq[i] = hmmsi;
  825.           partder[i][eff[i][0]] = dhmmsis;
  826.           partder[i][eff[i][1]] = dhmmsip;
  827.           partder[i][eff[i][2]] = dhmmsii;
  828.           break;
  829.    case PCI:  rateq[i]  = hmmci;
  830.           partder[i][eff[i][0]] = dhmmcis;
  831.           partder[i][eff[i][1]] = dhmmcip;
  832.           partder[i][eff[i][2]] = dhmmcii;
  833.           break;
  834.    case GOM:
  835.    case MXI:  rateq[i]  = hmmmi;
  836.           partder[i][eff[i][0]] = dhmmmis;
  837.           partder[i][eff[i][1]] = dhmmmip;
  838.           partder[i][eff[i][2]] = dhmmmii;
  839.           break;
  840.    case PSA:  rateq[i]  = hmmsa;
  841.           partder[i][eff[i][0]] = dhmmsas;
  842.           partder[i][eff[i][1]] = dhmmsap;
  843.           partder[i][eff[i][2]] = dhmmsaa;
  844.           break;
  845.    case PCA:  rateq[i]  = hmmca;
  846.           partder[i][eff[i][0]] = dhmmcas;
  847.           partder[i][eff[i][1]] = dhmmcap;
  848.           partder[i][eff[i][2]] = dhmmcaa;
  849.           break;
  850.    case MXA:  rateq[i]  = hmmma;
  851.           partder[i][eff[i][0]] = dhmmmas;
  852.           partder[i][eff[i][1]] = dhmmmap;
  853.           partder[i][eff[i][2]] = dhmmmaa;
  854.           break;
  855.    case HIL:  rateq[i]  = hill;
  856.           partder[i][eff[i][0]] = dhills;
  857.           break;
  858.    case UBS:  rateq[i]  = unibisa;
  859.           partder[i][eff[i][0]] = dunibisaa;
  860.           partder[i][eff[i][1]] = dunibisap;
  861.           partder[i][eff[i][2]] = dunibisaq;
  862.           break;
  863.    case UBM:  rateq[i]  = unibisb;
  864.           partder[i][eff[i][0]] = dunibisba;
  865.           partder[i][eff[i][1]] = dunibisbp;
  866.           break;
  867.    case ALI:  rateq[i]  = alinh;
  868.           for( j=0; j<3; j++ )
  869.            partder[i][eff[i][j]] = deff;
  870.               break;
  871.    default:   rateq[i]  = translate;
  872.           k = tree[kinetype[i]-MAX_TYP].nsub +
  873.           tree[kinetype[i]-MAX_TYP].npro +
  874.           tree[kinetype[i]-MAX_TYP].nmodf;
  875.           for( j=0; j<k; j++ )
  876.            partder[i][eff[i][j]] = deff;
  877.   }
  878.  }
  879. }
  880.